Skip to content

util

util

Helpers shared across the intervention package.

Two concerns live here, both of them about the boundary between a trace body and the frame it was written in. The input helpers give a module's (args, kwargs) pair the single-value view users actually write against — .input reads the first argument and writes it back without the caller having to know the module's signature. shared_locals gives the blocks written in one frame — an outer trace and the tracer.invoke(...) blocks inside it — a place to see each other's names, without letting those names leak into the frame itself: what escapes a trace is what save marked, and nothing else.

first_input

first_input(args: tuple, kwargs: dict) -> Any

The first positional argument, or the first keyword one if none positional.

Raises StopIteration for a module called with no arguments at all — there is no first input to name. Read .inputs instead, which is ((), {}) there.

replace_first_input

replace_first_input(args: tuple, kwargs: dict, value: Any) -> tuple

(args, kwargs) with first_input swapped for value.

shared_locals

shared_locals(frame: FrameType) -> dict

The mapping blocks written in frame share.

A name bound by a sibling block — an earlier tracer.invoke(...) — has to reach the ones beside it, but must not reach the frame itself: what escapes a trace is what save marked, and push is the only thing that decides that.

PARAMETER DESCRIPTION
frame

The frame the blocks were written in.

TYPE: FrameType

RETURNS DESCRIPTION
dict

The dict those blocks share — the same object for the same frame, until

dict

the trace that owns them finishes.

clear_shared_locals

clear_shared_locals() -> None

Drop this thread's per-frame shared stores.

Called from InterleavingTracer.execute, once the trace whose body held the invokes is done with them — not at the outermost trace. Each trace body runs in a fresh mediator frame, so waiting for the outermost one would hold one dead frame per iteration of a loop of traces, and through its locals that iteration's tensors, until the session ended.