gensbi.models.simformer.transformer#
Classes#
Self-attention block with a fixed fp32 compute island. |
|
MLP block (the FLOPs-dominant part of the transformer) with an fp32 |
|
A transformer stack. |
Module Contents#
- class gensbi.models.simformer.transformer.AttentionBlock(din, num_heads, features, skip_connection, rngs, dtype=jnp.float32, param_dtype=jnp.float32)[source]#
Bases:
flax.nnx.ModuleSelf-attention block with a fixed fp32 compute island.
flax.nnx.MultiHeadAttention(flax 0.12.7) computes its softmax in whateverdtypeit is given – there is no internal fp32 upcast for the attention logits/softmax the way some other implementations provide. Rather than bolt on a customattention_fnjust to force fp32 softmax, this block keeps all of its internal math (LayerNorm + MultiHeadAttention) atdtype=jnp.float32, ignoring thedtypecompute-precision knob for those internals. This is a deliberately larger fp32 island than a single softmax op, but the bulk of the model’s FLOPs live in theDenseBlockMLP stack (widening_factorx wider), which does honor the bf16dtypeknob, so this island has a small cost in practice.The island is for the math only: the block’s output (after the optional skip connection) is downcast to the requested
dtypebefore being returned, mirroring the codebase’s established fp32-island idiom (e.g.QKNorm.__call__’s.astype(v.dtype)influx1/layers.py). Without this downcast, the fp32 residual (x_in, captured post fp32-LayerNorm) would silently re-promote every downstream block’s output back to fp32 via JAX’s bf16+fp32 promotion rule on the skip-add, defeating the bf16 knob’s memory/bandwidth benefit for the whole inter-block residual stream.param_dtype(master-weight storage) is unaffected and still threads through normally.- Parameters:
din (int)
num_heads (int)
features (int)
skip_connection (bool)
rngs (flax.nnx.Rngs)
dtype (jax.typing.DTypeLike)
param_dtype (jax.typing.DTypeLike)
- class gensbi.models.simformer.transformer.DenseBlock(din, dcontext, num_hidden_layers, widening_factor, act, skip_connection, rngs, dtype=jnp.float32, param_dtype=jnp.float32)[source]#
Bases:
flax.nnx.ModuleMLP block (the FLOPs-dominant part of the transformer) with an fp32 LayerNorm island.
The LayerNorm math runs in fp32 (mirrors
AttentionBlock’s normalization treatment); the wide hidden-layer matmuls run in the requested computedtype. The block’s output (after the context-merge and optional skip connection) is downcast todtypebefore being returned – otherwise the fp32 residual (x_in, captured post fp32-LayerNorm) would silently re-promote the output back to fp32 via JAX’s bf16+fp32 promotion rule on the skip-add, defeating the bf16 knob’s memory/bandwidth benefit for the whole inter-block residual stream (seeAttentionBlockdocstring for the same pattern).- Parameters:
widening_factor (int)
act (Callable)
skip_connection (bool)
rngs (flax.nnx.Rngs)
dtype (jax.typing.DTypeLike)
param_dtype (jax.typing.DTypeLike)
- class gensbi.models.simformer.transformer.Transformer(din, dcontext, num_heads, num_layers, features, widening_factor=4, num_hidden_layers=1, act=jax.nn.gelu, skip_connection_attn=True, skip_connection_mlp=True, *, rngs, dtype=jnp.float32, param_dtype=jnp.float32)[source]#
Bases:
flax.nnx.ModuleA transformer stack.
- Parameters:
din (int)
dcontext (int)
num_heads (int)
num_layers (int)
features (int)
widening_factor (int)
num_hidden_layers (int)
act (Callable)
skip_connection_attn (bool)
skip_connection_mlp (bool)
rngs (flax.nnx.Rngs)
dtype (jax.typing.DTypeLike)
param_dtype (jax.typing.DTypeLike)
- __call__(inputs, context=None, mask=None)[source]#
- Parameters:
inputs (jaxtyping.Array)
context (Optional[jaxtyping.Array])
mask (jaxtyping.Array | None)
- Return type:
jax.Array