kups.core.effects
¶
Effect handlers for JAX.
Code performs an effect by binding a custom JAX primitive. An
Interpreter re-traces a function
and passes each primitive it has a handler for to that handler. Handlers read
and update a context that is carried through the whole program, including
jit, scan, while_loop, cond and shard_map. All other primitives are
bound unchanged.
kups.core.assertion is built on this: runtime_assert
performs an effect and with_runtime_assertions handles it.
Interpreter
¶
Evaluate a jaxpr while threading a context through effect handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handlers
|
Mapping[Primitive | str, Handler[Context]]
|
Handler per primitive. Every equation of a listed primitive goes to its handler; all other primitives are bound unchanged. |
required |
effects
|
Collection[Primitive | str]
|
Primitives that perform effects. Applies |
()
|
policy
|
InterpreterPolicy
|
What to do with such an unreachable effect. |
RAISE
|
label
|
str
|
Name used in tracing debug info. |
''
|
Source code in src/kups/core/effects/interpreter.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
InterpreterPolicy
¶
Bases: Enum
What to do with an effect the interpreter cannot reach.
That is an effect primitive without a handler, or an unhandled primitive whose sub-jaxprs contain an effect primitive.
Source code in src/kups/core/effects/interpreter.py
IGNORE = 'ignore'
class-attribute
instance-attribute
¶
Bind the primitive unchanged. Its effects are lost.
RAISE = 'raise'
class-attribute
instance-attribute
¶
Raise NotImplementedError.
WARN = 'warn'
class-attribute
instance-attribute
¶
Warn, then bind the primitive unchanged. Its effects are lost.
bind_primitive(eqn, invals)
¶
Bind eqn's primitive to invals unchanged.
Source code in src/kups/core/effects/interpreter.py
default_shard_map_handler(interpreter, ctx, eqn, invals, *, declare_ctx_in_specs=None, declare_ctx_out_specs=None, reduce_ctx=None)
¶
Reinterpret a shard_map equation through interpreter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interpreter
|
Interpreter[Context]
|
The interpreter walking the surrounding jaxpr. |
required |
ctx
|
Context
|
Input context (lives outside the manual axis scope, typically replicated across all shards). |
required |
eqn
|
JaxprEqn
|
The |
required |
invals
|
list[TracerValue]
|
User-data inputs to the equation. |
required |
declare_ctx_in_specs
|
Callable[[Context], Context | PartitionSpec] | None
|
Optional. Map the input context tree to the
|
None
|
declare_ctx_out_specs
|
Callable[[Context], Context | PartitionSpec] | None
|
Optional. Map the post-body context tree to
the |
None
|
reduce_ctx
|
Callable[[Context], Context] | None
|
Optional. Transform applied to the per-shard post-body
context inside the |
None
|