Status: active.
This crate is the executable companion for 07 Transformer.
- lesson module: 07 Transformer
- package:
rust_ml_transformer
It models the encoder-side Transformer path with:
- typed math containers:
DenseVector,DenseMatrix - typed architecture configuration:
TransformerConfig,LayerCount,HeadCount,FeedForwardWidth - typed expert routing:
ExpertScores,TopExpertRouter,ExpertRoute,ExpertBank - semantic model types:
TokenEmbedding,Query,Key,Value,TokenSequence - readable
std::opsarithmetic for typed projection, score, positional, and residual operations - expressive
thiserrordiagnostics throughModelError - public encoder trace review for learner-facing Transformer evidence
- standard scaled dot-product attention
- a simplified
LinearAttentionHeadfor architecture comparison - multi-head attention
- sinusoidal positional encodings
- layer normalization
- position-wise feed-forward layers
- one
TransformerEncoderBlock
src/
architecture.rs
error.rs
experts.rs
math.rs
types.rs
attention.rs
transformer.rs
lib.rs
examples/
architecture_config.rs
encoder_demo.rs
expert_routing.rs
public_encoder_trace.rs
architecture_configvalidates a tiny encoder architecture and estimates its repeated-block parameter budget.expert_routingroutes one token to the highest-scoring typed expert and applies that expert.encoder_demobuilds a tiny typed encoder block.public_encoder_tracerecords an encoder trace and rejects restricted or private traces before public release.- The unit tests exercise architecture checks, expert routing, vectors, matrices, attention heads, positional encodings, layer normalization, feed-forward maps, encoder blocks, encoder traces, and public trace review.
- The Transformer lessons explain the same path in English, algebra, and Rust.
Read the encoder as composed maps over token sequences:
TokenSequence + PositionEncoding -> TokenSequence
TokenSequence -> MultiHeadAttention -> TokenSequence
TokenSequence -> LayerNorm -> TokenSequence
TokenSequence -> FeedForward -> TokenSequence
EncoderBlock -> Encoder
The composition rule is d_model. Residual addition, attention output,
normalization, and feed-forward output must all return to the same token object
shape before the next block can run.
The public release boundary keeps the same distinction at Transformer scale:
ReviewedEncoderTrace -> PublicEncoderTrace
An EncoderTrace says the stack preserved token count and model width through
each block. A PublicEncoderTrace says that evidence was reviewed before it
entered learner-facing public material.
cargo test --manifest-path code/Cargo.toml -p rust_ml_transformercargo run --manifest-path code/Cargo.toml -p rust_ml_transformer --example encoder_democargo run --manifest-path code/Cargo.toml -p rust_ml_transformer --example architecture_configcargo run --manifest-path code/Cargo.toml -p rust_ml_transformer --example expert_routingcargo run --manifest-path code/Cargo.toml -p rust_ml_transformer --example public_encoder_traceThis crate is intentionally educational, not production-grade. It does not include:
- tokenization
- learned embeddings from vocabulary ids
- masking
- decoder cross-attention
- dropout
- autograd
- optimizers
- GPU kernels
The goal is architecture clarity first.
The crate keeps named functions such as add_token_embeddings and
add_sequences for learners who want explicit signposts. Internally, the same
ideas are also implemented with Rust operation traits:
&TokenEmbedding + &PositionEncoding -> TokenEmbedding
&TokenEmbedding + &TokenEmbedding -> TokenEmbedding
&TokenSequence + &TokenSequence -> TokenSequence
&QueryProjection * &TokenEmbedding -> Query
&KeyProjection * &TokenEmbedding -> Key
&ValueProjection * &TokenEmbedding -> Value
&OutputProjection * &ConcatenatedHeadOutput -> TokenEmbedding
&FeedForwardProjection1 * &TokenEmbedding -> HiddenPreActivation
&FeedForwardProjection2 * &HiddenActivation -> TokenEmbedding
&Query * &Key -> AttentionScore
&AttentionWeights * &ValueSequence -> AttentionOutput
&Query + &ProjectionBias -> Query
&Key + &ProjectionBias -> Key
&Value + &ProjectionBias -> Value
&TokenEmbedding + &ProjectionBias -> TokenEmbedding
&DenseMatrix * &DenseVector -> DenseVector
&DenseVector * &DenseVector -> ModelScalar
&DenseVector * ModelScalar -> Result<DenseVector, ModelError>
EncoderTrace + EncoderTraceVisibility -> PublicEncoderTrace
This makes the Transformer path read closer to the algebra while preserving the
newtype boundary: projections produce the role they are meant to produce,
query-key multiplication returns a scaled attention score, bias addition stays
typed, residual addition is checked, and mismatched shapes still return
ModelError.
Raw learner literals enter through explicit scalar validation such as
ModelScalar::try_from(...) and dimension validation such as
VectorLength::try_from(...). Vectors and matrices are then built with typed
constructors such as DenseVector::new([ModelScalar, ...]) and
DenseMatrix::from_rows([[ModelScalar, ...], ...]). Once values cross that
boundary, public constructors and operations use typed values rather than
generic raw primitive or raw container adapters.
Architecture hyperparameters follow the same rule. A learner validates layer
counts, head counts, model width, and feed-forward width before they become a
TransformerConfig. The config then exposes maps such as:
VectorLength / HeadCount -> VectorLength
TransformerConfig -> ParameterCount
That keeps the CS336-style architecture discussion concrete: hyperparameters are not loose numbers, they are typed choices with invariants.
The expert-routing example adds a small attention-alternatives bridge:
ExpertScores -> ExpertChoice
TokenIndex + ExpertChoice -> ExpertRoute
ExpertBank + ExpertRoute + TokenEmbedding -> TokenEmbedding
It intentionally teaches top-1 routing before distributed experts, load balancing, or auxiliary losses. The invariant is simple: the router must receive exactly one score per available expert, and the chosen route must point to an expert that exists in the bank.