Skip to content
module

compile

12 members
lucid.compile

lucid.compile — graph-capture compile path for Apple Silicon.

Lowers a Lucid model's forward pass (and optionally its backward + optimizer-update graph) into a single MPSGraphExecutable that is cached per input signature. Subsequent calls with the same signature reuse the cached executable, skipping Python dispatch entirely for every op inside the captured region.

Notes

  • compile — the top-level entry. Wraps either an nn.Module or a plain callable; returns a CompiledModule. Also usable as a decorator (@lucid.compile or @lucid.compile(dynamic=False)).
  • CompiledModule — module-shaped wrapper exposing cache_info() / timing() / clear_cache() plus model-delegated APIs (parameters / state_dict / to / train / eval).
  • compile_optimizer — wraps the update step of an optimizer into a single cached executable (8 optimizers supported; see lucid.compile._optim.compiler). Forward + backward stay eager.
  • fused_step — single executable covering forward + loss + backward (via MPSGraph autodiff) + optimizer update via the ghost-grad placeholder mechanism.
  • compiled_step — convenience wrapper combining compile + make_step for one-line training loops.
  • compiled_decode_step — one cached executable per decode step of an autoregressive model over a StaticCache; what generate(cache_implementation="compiled") uses (LCD-660).
  • _tracing — low-level context manager that installs a thread-local Tracer; used internally by every entry above. Public-but-underscored so external callers know they're touching an unstable surface.

The MPSGraph builder + executable cache + per-op emitters live in lucid/_C/compile/. The fallback policy is in compile's notes: a call that needs gradients, or that the compiler cannot lower, runs eagerly.

Forward hooks and pre-hooks are traced with the forward only when lucid itself declares them pure tensor rewrites (the prompt-learning sites of lucid.peft); the hook set and the state such a hook reads are part of the cache key, so adding, removing or reordering one retraces. Any other forward hook or pre-hook may act by side effect - log, count, record - which a replay would silently skip, so a call with one runs eagerly, with a one-time notice. Backward hooks, global module hooks and tensor gradient hooks always run eagerly.

A compiled replay never reads an index back, so on Metal an index outside its axis answers policy B's NaN or dropped write instead of the CPU's IndexError. With LUCID_STRICT_INDEX=1 set, a program that holds an index op (gather, embedding, scatter*, one_hot, ...) is not compiled: its calls run eagerly, with a one-time notice, where the index is checked. The switch is read at every call and is part of the cache key. A program restored with load_compiled has no trace to inspect and is not checked.

Memory. An MPSGraphExecutable keeps the buffers MPSGraph planned for its intermediates until it is released, so every signature a long-lived wrapper has compiled pins memory in proportion to its tensors. The executables are therefore bounded, oldest out, in two places: the process-wide session cache (LUCID_COMPILE_MAX_CACHE entries, LUCID_COMPILE_MAX_CACHE_BYTES of estimated pinned memory) and each wrapper's own cache. A CompiledModule, make_step step and compiled_decode_step step keep the least recently used LUCID_COMPILE_MAX_SIGNATURES signatures (default 256) within LUCID_COMPILE_MAX_SIGNATURE_BYTES (default an eighth of the machine's memory); 0 turns a cap off, and the values are read when the wrapper is made. fused_step obeys the byte cap but keeps at most 8 plans, whatever LUCID_COMPILE_MAX_SIGNATURES says. A dropped signature is compiled again on its next call, so a loop that cycles over more shapes than a cap allows recompiles on every call. Bounded by count alone: the update plans of compile_optimizer, 16 per optimizer, first in first out, with no byte cap. Not bounded: a dynamic=True module that shares one symbolic-batch executable (MPSGraph specialises it per runtime shape inside), and a segmented step's per-segment caches (make_step(segments > 1)).

Classes

Functions