kups.core.assertion.tracing
¶
Collect runtime assertions by reinterpreting traced functions.
cond_handler(interpreter, ctx, eqn, invals)
¶
Custom cond handler that unions assertions across branches.
Branches may declare different assertions. The output context holds the incoming assertions followed by every branch's assertions concatenated in branch order. Each branch fills its own slots with real predicates and the slots owned by other branches with passing placeholders (True predicate, zeroed fmt_args/fix_args), so jax.lax.cond observes matching pytree structures across branches. An unchosen branch thus contributes only passing assertions.
Source code in src/kups/core/assertion/tracing.py
scan_handler(interpreter, ctx, eqn, invals)
¶
Carry only the body's assertions through the scan.
Each iteration folds the new assertions into the accumulated ones with their
loop_merge. Assertions created before the scan stay outside it. The body
is traced once: the dry run that discovers its assertions is reused by the
scan.
Source code in src/kups/core/assertion/tracing.py
while_handler(interpreter, ctx, eqn, invals)
¶
Thread only the loop's own assertions, packed, through the carry.
Each iteration folds the new assertions into the accumulated ones with their
loop_merge. Assertions created before the loop stay outside it.
Source code in src/kups/core/assertion/tracing.py
with_runtime_assertions(fn, policy=InterpreterPolicy.RAISE, context_sharding=None)
¶
Decorator that enables runtime assertion tracing for JAX functions.
This decorator wraps a function to intercept and collect all runtime assertions
created with runtime_assert during execution. The wrapped function returns
both the original result and a tuple of all assertions that were evaluated,
allowing for post-execution analysis, debugging, and optional error recovery.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[P, R]
|
The function to wrap with assertion tracing capabilities |
required |
policy
|
InterpreterPolicy
|
What to do with assertions the interpreter cannot reach, e.g.
inside |
RAISE
|
context_sharding
|
PartitionSpec | None
|
Optional sharding specification for distributed contexts. When provided, assertion contexts are sharded according to this spec for multi-device computations. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[P, tuple[R, tuple[RuntimeAssertion[Any, Any], ...]]]
|
A wrapped function that returns a tuple of (original_result, assertions_tuple). |
Callable[P, tuple[R, tuple[RuntimeAssertion[Any, Any], ...]]]
|
The assertions tuple contains all RuntimeAssertion instances encountered |
Callable[P, tuple[R, tuple[RuntimeAssertion[Any, Any], ...]]]
|
during execution, preserving order and enabling post-hoc analysis. |
Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
P
|
Parameter specification of the wrapped function (ParamSpec) |
required | |
R
|
Return type of the wrapped function |
required |
Example
Basic usage:
@with_runtime_assertions
def validate_computation(x):
runtime_assert(x > 0, "x must be positive")
return x ** 2
result, assertions = validate_computation(jnp.array(5.0))
# result = 25.0, assertions contains one RuntimeAssertion
With custom policy:
traced_fn = with_runtime_assertions(
my_function,
policy=InterpreterPolicy.WARN
)
result, assertions = traced_fn(inputs)
Distributed computation:
sharded_fn = with_runtime_assertions(
distributed_computation,
context_sharding=jax.sharding.PartitionSpec('data', None)
)
Error analysis and recovery:
result, assertions = traced_fn(initial_state)
# Check for failures
failed_assertions = [a for a in assertions if a.failed()]
if failed_assertions:
# Attempt automatic fixing
fixed_state = initial_state
for assertion in failed_assertions:
if assertion.fix_fn is not None:
fixed_state = assertion.fix(fixed_state)
# Re-run with fixed state
result, _ = traced_fn(fixed_state)
Note
The decorator integrates seamlessly with JAX transformations including jit, vmap, grad, and scan. Assertions are properly threaded through control flow operations and maintain correct semantics under transformations.
Source code in src/kups/core/assertion/tracing.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |