kups.core.assertion.runtime_assertion
¶
Runtime assertion record and the rules loops use to fold it.
ELEMENTWISE_MAX = LoopMerge(_lowest_like, _keep_largest_failure)
module-attribute
¶
Report the elementwise maximum over failing iterations (the default).
Suits fixes that must cover every iteration, such as capacity requirements.
FIRST_FAILURE = LoopMerge(jnp.empty_like, _keep_first_failure)
module-attribute
¶
Report the arguments of the first failing iteration.
LAST_FAILURE = LoopMerge(jnp.empty_like, _keep_last_failure)
module-attribute
¶
Report the arguments of the last failing iteration.
NO_ARGS = _NO_ARGS()
module-attribute
¶
Sentinel instance to distinguish between no arguments and None as an argument.
LoopMerge
dataclass
¶
How a loop folds an assertion's fmt_args/fix_args across iterations.
The predicate of an assertion inside scan or while_loop always
accumulates as a conjunction over iterations. LoopMerge decides, leaf by
leaf, which values the reported fmt_args and fix_args carry.
Attributes:
| Name | Type | Description |
|---|---|---|
init |
Callable[[Array], Array]
|
Value of an argument leaf before the first iteration, given a leaf from the loop body. |
combine |
Callable[[Array, Array, Array, Array], Array]
|
|
Source code in src/kups/core/assertion/runtime_assertion.py
RuntimeAssertion
¶
A runtime assertion that validates computations with optional automatic fixing.
This class encapsulates a predicate that should evaluate to True, along with metadata for error reporting and optional repair mechanisms. Assertions are designed to work seamlessly with JAX transformations while providing rich debugging information.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
The type of state that can be modified by the fix function |
required | |
FixArgs
|
The types of arguments passed to the fix function (can be a PyTree) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
predicate |
Array
|
A scalar boolean array indicating whether the assertion passes |
message |
str
|
Human-readable error message with optional format placeholders |
fmt_args |
dict[str, Array]
|
Dictionary of values to substitute into the message format string |
exception_type |
type[Exception]
|
Type of exception to raise on assertion failure |
static_info |
dict[str, Any]
|
Additional metadata for debugging (not traced by JAX) |
fix_fn |
Fix[State, FixArgs] | None
|
Optional function to repair the state when assertion fails |
fix_args |
FixArgs | _NO_ARGS
|
Arguments to pass to the fix function (can be complex PyTree structures) |
loop_merge |
LoopMerge
|
How loops fold |
Example
Note
The fix_args can be complex PyTree structures including nested dictionaries, tuples, and arrays. The assertion system properly handles flattening and unflattening these structures during JAX transformations.
Source code in src/kups/core/assertion/runtime_assertion.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 | |
exception
property
¶
Create the exception instance that would be raised on assertion failure.
Returns:
| Type | Description |
|---|---|
Exception
|
An exception instance with the formatted error message |
__str__()
¶
check()
¶
Check the assertion and raise an exception if it fails.
Raises:
| Type | Description |
|---|---|
Exception
|
The configured exception type if the assertion fails |
Source code in src/kups/core/assertion/runtime_assertion.py
failed()
¶
fix(state)
¶
Attempt to fix the assertion failure by modifying the provided state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
The current state that needs to be repaired |
required |
Returns:
| Type | Description |
|---|---|
State
|
The modified state after applying the fix function |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If no fix function is available |
AssertionError
|
If fix arguments are missing when a fix function exists |