mixle.models.streaming_transformer_leaf module¶
A streaming, non-buffering transformer-LM leaf for avoiding host-RAM materialization.
Where NeuralCategorical buffers the whole shard in the accumulator and creates a fresh optimizer every
M-step, this leaf keeps a long-lived module and optimizer in the M-step. The accumulator’s seq_update
is one train step on a streamed micro-batch. value() returns (loss_sum, tokens) – two telemetry
floats rather than the corpus – and estimate() is a no-op that wraps the live module.
This deliberately voids the sufficient-statistic algebra (value/combine are telemetry, not a foldable
statistic): a sanctioned non-leaf carve-out (like NeuralGaussian.sample() raising), not an ABC change. It is the
single-process prerequisite for the distributed neural handle: each rank keeps its streamed shard
resident and the only cross-rank collective becomes the in-backward gradient reduce-scatter, never a
gather-suff-stats-to-root.
- class StreamingTransformer(module, device='cpu')[source]
Bases:
SequenceEncodableProbabilityDistributionWraps a live, persistently-trained module.
seq_log_density= next-tokenlog p(eval/telemetry).- Parameters:
module (Any)
device (str)
- classmethod from_config(vocab, *, d_model=128, n_layer=4, n_head=4, block=64, embedding=None, device='cpu')[source]
Build the leaf from hyperparameters (no hand-built torch module) – the declarative estimator surface.
embeddingoptionally ties a sharedCategoricalEmbeddingacross leaves.
- log_density(xy)[source]
Return the next-token log probability for one
(context, token)pair.
- predict(x)[source]
Return argmax next-token predictions for one or more contexts.
- sampler(seed=None)[source]
Return the sampler for the conditional next-token model.
- Parameters:
seed (int | None)
- Return type:
StreamingTransformerSampler
- seq_log_density(enc)[source]
Return per-row next-token log probabilities for encoded context/token pairs.
- estimator(pseudo_count=None)[source]
Return the streaming estimator that trains the live module in accumulator updates.
- Parameters:
pseudo_count (float | None)
- Return type:
StreamingTransformerEstimator
- dist_to_encoder()[source]
Return the encoder for context/token training pairs.
- Return type:
StreamingTokenEncoder
- to_dict()[source]
Serialize the module bytes and device for registry-based round trips.
- class StreamingTransformerSampler(dist, seed=None)[source]
Bases:
DistributionSamplerSampler facade for a conditional next-token transformer leaf.
- Parameters:
dist (StreamingTransformer)
seed (int | None)
- class StreamingTokenEncoder[source]
Bases:
DataSequenceEncoderEncode context/token pairs for streaming transformer scoring and training.
- class StreamingTransformerAccumulator(module, lr, device)[source]
Bases:
SequenceEncodableStatisticAccumulatorseq_update= ONE train step against the PERSISTENT module + optimizer;value()= telemetry only.- seq_update(enc, weights, estimate)[source]
Run one optimizer step on an encoded micro-batch.
- update(x, weight, estimate)[source]
Train on one weighted context/token pair through
seq_update().
- initialize(x, weight, rng)[source]
No-op initialization hook for the streaming training path.
- seq_initialize(enc, weights, rng)[source]
No-op batch initialization hook for the streaming training path.
- combine(other)[source]
Merge telemetry from another streaming accumulator.
- Parameters:
other (Any)
- Return type:
StreamingTransformerAccumulator
- value()[source]
Return
(loss_sum, token_count)telemetry without storing the corpus.
- from_value(v)[source]
Restore telemetry counters from a value tuple.
- Parameters:
v (tuple)
- Return type:
StreamingTransformerAccumulator
- acc_to_encoder()[source]
Return the encoder expected by this accumulator.
- Return type:
StreamingTokenEncoder
- class StreamingTransformerAccumulatorFactory(module, lr, device)[source]
Bases:
StatisticAccumulatorFactoryFactory for streaming transformer accumulators sharing a live module.
- make()[source]
Create a fresh accumulator around the shared live module.
- Return type:
StreamingTransformerAccumulator
- class StreamingTransformerEstimator(module, lr=3e-3, device='cpu')[source]
Bases:
ParameterEstimatorEstimator whose accumulator trains a live streaming transformer module in place.
- accumulator_factory()[source]
Return an accumulator factory for streamed context/token micro-batches.
- Return type:
StreamingTransformerAccumulatorFactory
- class TransformerLMEstimator(vocab, *, d_model=128, n_layer=4, n_head=4, block=64, embedding=None, lr=3e-3, device='cpu')[source]
Bases:
StreamingTransformerEstimatorA Transformer language model as a fit-ready estimator:
TransformerLMEstimator(vocab, d_model=..., ...).The clean, declarative surface – no hand-built torch module, no
Leaf(...).estimator()two-step. Drops intoMixtureEstimator/CompositeEstimatorlike any other*Estimator.embeddingoptionally ties a sharedCategoricalEmbedding(e.g. one word embedding across a mixture’s experts).TransformerLMEstimator(V, embedding=emb)andStreamingTransformer.from_config(V, embedding=emb).estimator()build the same thing.
- stream_fit(module, token_source, *, lr=3e-3, device='cpu', report_every=200, log=None)[source]
Train
moduleby streaming micro-batches fromtoken_source(a generator). The accumulator holds the PERSISTENT optimizer and trains incrementally; its payload stays(loss_sum, tokens)– the corpus is never buffered. Returns(StreamingTransformer, (loss_sum, tokens)).
- StreamingTransformerLeaf
alias of
StreamingTransformer
- StreamingTransformerLeafEstimator
alias of
StreamingTransformerEstimator