mixle.models.streaming_transformer_leaf module¶
A streaming, non-buffering transformer-LM leaf for avoiding host-RAM materialization.
Where NeuralCategorical buffers a shard, this leaf keeps the corpus out of the accumulator. Each
seq_update computes a weighted gradient numerator at a pinned base-model digest without changing the
module. value() returns model-sized gradients plus weighted telemetry; combine() sums only compatible
worker states, and estimate() applies one step through a persistent AdamW optimizer. Thus partitioned
and unpartitioned accumulation have explicit data-parallel semantics instead of silently discarding
worker-trained weights. stream_fit() preserves the familiar one-step-per-micro-batch behavior by
estimating after every streamed batch.
- class StreamingGradientState(base_sha256, parameter_names, gradient_sums, loss_sum, effective_weight, rows, batches)[source]
Bases:
objectFoldable weighted-gradient state computed at one exact model revision.
- Parameters:
- property mean_loss: float
Return the consistently weighted mean loss.
- class StreamingTransformer(module, device='cpu', *, lr=3.0e-3, optimizer_state=None)[source]
Bases:
SequenceEncodableProbabilityDistributionWraps a live, persistently-trained module.
seq_log_density= next-tokenlog p(eval/telemetry).- 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:
SequenceEncodableStatisticAccumulatorAccumulate foldable weighted gradients at one exact model revision.
- seq_update(enc, weights, estimate)[source]
Accumulate one weighted gradient numerator without mutating the shared model.
- update(x, weight, estimate)[source]
Accumulate one weighted context/token gradient.
- initialize(x, weight, rng)[source]
Initialize through the same gradient accumulation path.
- seq_initialize(enc, weights, rng)[source]
Initialize a batch through the same gradient accumulation path.
- combine(other)[source]
Sum worker gradients only when they were computed at the identical base revision.
- Parameters:
other (Any)
- Return type:
StreamingTransformerAccumulator
- value()[source]
Return model-sized gradients and weighted telemetry, never the corpus.
- Return type:
StreamingGradientState
- from_value(v)[source]
Restore a complete foldable gradient state.
- Parameters:
v (Any)
- 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', optimizer_state=None)[source]
Bases:
ParameterEstimatorPersistent-optimizer estimator applying foldable worker gradient sums.
- accumulator_factory()[source]
Return workers pinned to the estimator’s current model revision.
- 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 one persistent-optimizer step per streamed micro-batch without buffering the corpus.
- StreamingTransformerLeaf
alias of
StreamingTransformer
- StreamingTransformerLeafEstimator
alias of
StreamingTransformerEstimator