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, cache_positions=<factory>, batch_size=None, n_head=None, head_dim=None, device=None)[source]
Bases:
objectRaw per-layer KV caches and their explicit absolute positions.
- train_tbptt(mechanism, state, chunks, opt, *, detach_horizon=1, extra_loss=None)[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.extra_lossis an optional live tensor added to the FIRST backward only – a term earned outsidechunks(e.g. an auxiliary regularizer from a warm-up prefix this call does not train the LM on). Folding it in here rather than backpropagating it separately keeps the optimizer-step count independent of whether the term is present, so an ablation on it is not confounded by step count. It is excluded from the returnedlossestelemetry, which stays one entry per chunk.