mixle.experimental.context_spine module¶
E1: the chunked-recurrent training spine every Track-E long-context mechanism plugs into.
See notes/designs/E1.md for the design decisions (RoPE over learned-absolute position embeddings,
windowed-mask derivation, why detach_horizon only ever cuts the backward graph and never the forward
one, and why this ships its own tiny TBPTT driver instead of routing through GradLeaf).
mixle.models.transformer.CausalLM and mixle.models.streaming_transformer_leaf.StreamingTransformer
both train bounded, independent micro-batches with a learned position table capped at block tokens –
neither carries state across calls. ContextMechanism is the minimal protocol that adds streaming +
carried state + truncated-backprop-through-time (TBPTT) on top, without touching either of those modules.
SlidingWindowSpine is the E1 baseline mechanism (Transformer-XL-style stop-gradient KV carry); E2-E6
differ only in what step’s carried state contains.
- class ContextMechanism(*args, **kwargs)[source]
Bases:
ProtocolThe substrate contract every Track-E long-context mechanism implements.
stepis per-position teacher-forced (returns the mean loss over every position in the chunk, not just the last one – unlikeCausalLM.forward, which returns only the last position’s logits).- init_state(batch_size, *, device='cpu')[source]
A fresh state for
batch_sizeindependent streams (empty cache / zero memory).
- step(state, chunk)[source]
chunk = (x, y),(batch, T)long tensors. Returns(new_state, mean_loss).
- class SlidingWindowState(cache_k=<factory>, cache_v=<factory>, pos=0)[source]
Bases:
objectPer-layer stop-gradient KV cache plus the running absolute position counter (see E1.md’s RoPE note).
- class SlidingWindowSpine(vocab, *, d_model=32, n_layer=2, n_head=2, window=64, cp_size=1)[source]
Bases:
ModuleE1 baseline: sliding-window exact attention with a stop-gradient carried KV cache (Transformer-XL style).
window=None(orwindow >= `` any sequence length this mechanism will ever see) makes ``stepcompute ordinary full causal self-attention with no truncation – the “full-attention-equivalent” configurationnotes/designs/E1.mduses as the acceptance baseline, computed by the exact same code path multi-chunk streaming uses (not a second, independently-written transformer).cp_size(E8,notes/designs/E8.md): context-parallel window sharding.cp_size=1(the default) is the original single-device path above, completely unchanged – byte-identical, not just numerically close, so E1’s existing behavior and tests are untouched.cp_size > 1shards the current step’s KV axis (cache ++ chunk) acrosscp_sizesimulated ranks viamixle.utils.parallel.context_parallel_spinefor the attention sub-step of every layer; nothing else (embeddings, LayerNorm, MLP, head, cache bookkeeping) changes, since those are all per-position and need no communication.
- train_tbptt(mechanism, state, chunks, opt, *, detach_horizon=1)[source]
Stream
chunksthroughmechanism, TBPTT-training with the given optimizer.Every
detach_horizonchunks (or at end of stream, whichever comes first): backward the mean accumulated loss, step the optimizer, thenmechanism.detach(state)to cut the graph before continuing.detach_horizon=1is literal per-chunk stop-gradient (Transformer-XL); a horizon spanning the whole stream means no mid-stream detach happens at all (seenotes/designs/E1.md). Returns{"losses": [float, ...], "state": final_state}– one loss per chunk, detached telemetry.