mixle.models.streaming_transformer_leaf module¶
A streaming, non-buffering transformer-LM leaf – the keystone that removes the host-RAM materialization wall.
Where NeuralCategorical buffers the whole shard in the accumulator and news a fresh optimizer every M-step,
this inverts the EM abstraction: the M-step OWNS a long-lived module + optimizer, and the accumulator’s
seq_update IS one train step on a streamed micro-batch. value() returns (loss_sum, tokens) – two
floats, telemetry only, NEVER 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 (FSDP2) 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 log-density or log-mass at a single observation.
- sampler(seed=None)[source]
Return a sampler for drawing observations from this distribution.
- Parameters:
seed (int | None)
- Return type:
StreamingTransformerSampler
- seq_log_density(enc)[source]
Return vectorized log-density values for sequence-encoded observations.
- estimator(pseudo_count=None)[source]
Return an estimator for fitting this distribution from data.
- Parameters:
pseudo_count (float | None)
- Return type:
StreamingTransformerEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
StreamingTokenEncoder
- to_dict()[source]
Return a safe JSON-compatible representation of this distribution.
- class StreamingTransformerSampler(dist, seed=None)[source]
Bases:
DistributionSampler- Parameters:
dist (StreamingTransformer)
seed (int | None)
- sample(size=None, *, batched=True)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class StreamingTokenEncoder[source]
Bases:
DataSequenceEncoder
- 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]
- update(x, weight, estimate)[source]
- seq_initialize(enc, weights, rng)[source]
- acc_to_encoder()[source]
- Return type:
StreamingTokenEncoder
- class StreamingTransformerAccumulatorFactory(module, lr, device)[source]
Bases:
StatisticAccumulatorFactory- make()[source]
- Return type:
StreamingTransformerAccumulator
- class StreamingTransformerEstimator(module, lr=3e-3, device='cpu')[source]
Bases:
ParameterEstimator- accumulator_factory()[source]
- 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