kups.core.result
¶
Result types for simulation computation management.
This module provides a system for managing simulation results and runtime assertions. The key component is:
- Result: Encapsulates computation results with assertions
Results integrate with JAX transformations while maintaining runtime validation through RuntimeAssertion.
Result
¶
A result of a computation, containing the actual result and a sequence of runtime assertions that will be checked after the computation is done (outside of JIT-compiled functions).
Source code in src/kups/core/result.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 | |
all_assertions_pass
property
¶
Returns True if all runtime assertions pass, i.e., their predicates evaluate to True. By compressing all predicates into a single boolean array, we only need to transfer one boolean value from device to host to check if all assertions passed.
failed_assertions
property
¶
Returns a tuple of runtime assertions that failed, i.e., those whose predicate evaluates to False.
Note: This method should be called outside of JIT-compiled functions.
fix_or_raise(state)
¶
Apply fixes for all failed assertions, raising for any without a fix function.
If all assertions pass, the state is returned unchanged. Otherwise each failed assertion's fix function is applied in sequence. Assertions that have no fix function registered will raise their configured exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state to repair. |
required |
Returns:
| Type | Description |
|---|---|
State
|
State with all fixable assertion failures corrected. |
Raises:
| Type | Description |
|---|---|
Exception
|
The configured exception of any failed assertion that has no fix function. |
Source code in src/kups/core/result.py
raise_assertion()
¶
Raises an assertion error if any of the runtime assertions failed.
Note: This method should be called outside of JIT-compiled functions.
Source code in src/kups/core/result.py
as_result_function(fn, policy=InterpreterPolicy.RAISE, context_sharding=None)
¶
Wrap a function to return a Result with runtime assertion tracking.
This decorator transforms a regular function into one that returns a Result object containing both the return value and any runtime assertions encountered during execution. Assertions are extracted through JAX tracing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[P, R]
|
Function to wrap |
required |
policy
|
InterpreterPolicy
|
Interpreter policy for handling assertions during tracing |
RAISE
|
context_sharding
|
PartitionSpec | None
|
Optional sharding specification for distributed execution |
None
|
Returns:
| Type | Description |
|---|---|
Callable[P, Result[Any, R]]
|
Wrapped function that returns Result[Any, R] instead of R |