kups.core.assertion.primitives
¶
JAX primitives that record runtime assertions in traced code.
check_assertions(like=None)
¶
A primitive that returns a scalar bool. When not wrapped in with_runtime_assertions, it always returns True. When wrapped, it will return the conjunction of all runtime assertions in the current context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
Array | None
|
An array whose device placement and sharding will be used for the output. If None, the output will be placed on the default device. |
None
|
Returns: A scalar boolean array indicating whether all assertions pass.
Source code in src/kups/core/assertion/primitives.py
runtime_assert(predicate, message='', fmt_args=None, exception_type=AssertionError, static_info=None, fix_fn=None, fix_args=NO_ARGS, loop_merge=ELEMENTWISE_MAX)
¶
Create a runtime assertion that integrates with JAX transformations.
This function creates assertions that can be traced through JAX transformations including JIT compilation, automatic differentiation, and vectorization. The assertion acts as an identity function during execution but records assertion metadata for later inspection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Array
|
A boolean array indicating whether the assertion passes |
required |
message
|
str
|
Error message with optional format placeholders (e.g., "Value {val} is invalid") |
''
|
fmt_args
|
dict[str, Array] | None
|
Dictionary mapping format placeholder names to values |
None
|
exception_type
|
type[Exception]
|
Type of exception to raise if assertion fails during checking |
AssertionError
|
static_info
|
dict[str, Any] | None
|
Additional metadata for debugging (not traced by JAX) |
None
|
fix_fn
|
Fix[State, FixArgs] | None
|
Optional function to repair state when assertion fails |
None
|
fix_args
|
FixArgs | _NO_ARGS
|
Arguments for the fix function (can be complex PyTree structures) |
NO_ARGS
|
loop_merge
|
LoopMerge
|
How |
ELEMENTWISE_MAX
|
Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Type of state that can be modified by the fix function |
required | |
FixArgs
|
Type of arguments passed to the fix function (supports PyTree structures) |
required |
Example
Basic assertion:
x = jnp.array(5.0)
runtime_assert(
predicate=x > 0,
message="Value must be positive, got {val}",
fmt_args={"val": x}
)
Assertion with fixing:
runtime_assert(
predicate=x > threshold,
message="Value {val} below threshold {thresh}",
fmt_args={"val": x, "thresh": threshold},
fix_fn=lambda state, args: jnp.maximum(state, args["min_val"]),
fix_args={"min_val": jnp.array(1.0)}
)
Complex PyTree fix_args:
Note
The fix_args parameter supports arbitrarily nested PyTree structures including dictionaries, tuples, and arrays. These are automatically flattened and unflattened during JAX transformations.
Source code in src/kups/core/assertion/primitives.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |