gensbi.models.tarflow.blocks#
Transformer blocks for the transformer flow.
Adapted from apple/ml-tarflow (TarFlow); see models/tarflow/LICENSE.apple. Prefix-concatenation conditioning and SOS shift adapted from apple/ml-starflow (STARFlow); see models/tarflow/LICENSE.starflow.
Attributes#
Classes#
Pre-norm residual block combining causal self-attention and an MLP. |
|
One exact autoregressive bijection over a token sequence. |
Module Contents#
- class gensbi.models.tarflow.blocks.AttentionBlock(channels, num_heads, expansion, rngs, param_dtype=jnp.float32, dtype=jnp.float32)[source]#
Bases:
flax.nnx.ModulePre-norm residual block combining causal self-attention and an MLP.
LayerNorm is applied over the channel axis only (not across tokens), so no future-token information leaks into earlier positions.
- Parameters:
channels (int) – Token embedding width. Must be divisible by
num_heads.num_heads (int) – Number of attention heads.
expansion (int) – MLP hidden-size multiplier; the MLP has
channels * expansionneurons in its hidden layer.rngs (nnx.Rngs) – Flax RNG container used to initialize all sub-layers.
param_dtype (DTypeLike, optional) – Dtype for all stored (master) kernel/bias/scale parameters. Default is
float32.dtype (DTypeLike, optional) – Compute dtype forwarded to each
Linear. Default isfloat32, matchingparam_dtype, so with default arguments this is a bit-identical no-op cast.norm1/norm2are fp32 islands and always run atfloat32regardless of this knob; their output self-heals when it feeds the following compute-dtypeLinear(promote_dtypedowncasts it there).
- __call__(x, mask=None, freqs_cis=None, return_kv=False)[source]#
Apply pre-norm residual self-attention followed by a residual MLP.
- Parameters:
x (Array) – Token sequence of shape
(B, T, C).mask (Array or None, optional) – Attention mask broadcastable to
(1, 1, T, T);Nonemeans standard causal.freqs_cis (Array or None, optional) – Rotary angles
(T, head_dim)applied to q/k before attention.return_kv (bool, optional) – If
True, also return the unrotated(k, v)— the KV cache stores unrotated keys (the cached path re-rotates the full cache each step, as the reference does). DefaultFalse.
- Returns:
outof shape(B, T, C), or(out, k, v)whenreturn_kv=True.- Return type:
Array or tuple
- _finish(x, attn)[source]#
Residual attention output + residual MLP (shared tail).
- Parameters:
x (jax.Array)
attn (jax.Array)
- Return type:
jax.Array
- _qkv(x)[source]#
Project to unrotated (q, k, v), each of shape
(B, S, nh, hd).- Parameters:
x (jax.Array)
- decode(x_new, k_cache, v_cache, index, freqs_cis=None)[source]#
Single-token decode step against a preallocated KV cache.
The cache stores unrotated k (reference behavior: rope is applied after the cache read, re-rotating the whole prefix each step). Slots beyond
indexare zero-filled and masked out of the attention.- Parameters:
x_new (Array) – New token, shape
(B, 1, C).k_cache (Array) – Caches of shape
(B, S, nh, hd)withStotal slots.v_cache (Array) – Caches of shape
(B, S, nh, hd)withStotal slots.index (int or traced scalar) – Slot to write; attention sees slots
<= index.freqs_cis (Array or None, optional) – Rotary angles
(S, head_dim)for all slots; the new token’s q uses rowindex.
- Returns:
(out, k_cache, v_cache)withoutof shape(B, 1, C).- Return type:
tuple
- class gensbi.models.tarflow.blocks.MetaBlock(F, channels, T, perm, conditioner, num_layers, num_heads, expansion, rngs, zero_init=True, use_softplus=True, soft_clip=4.0, rope=None, grid=None, param_dtype=jnp.float32, dtype=jnp.float32)[source]#
Bases:
flax.nnx.ModuleOne exact autoregressive bijection over a token sequence.
Implements the
Bijectiondirection contract:inverse()maps data to noise (density-evaluation direction) via a single parallel affine pass with a triangular Jacobian;forward()maps noise to data (sampling direction) via a sequential causal scan that re-runs the attention pass at each token position.The affine scale is computed via softplus by default (bounded tail, identity at zero-init) or via
expwhenuse_softplus=False(legacy).- Parameters:
F (int) – Feature dimension per token (number of input channels per token).
channels (int) – Internal embedding width for the transformer blocks.
T (int) – Number of tokens in the sequence.
perm (Array) – Token permutation applied before the affine transform. The inverse permutation is derived internally via
argsort.conditioner (AdditiveBiasConditioner or VectorConditioner or ImageConditioner) – Module that provides
(bias, prefix)conditioning signals via itsembedmethod.num_layers (int) – Number of
AttentionBlocklayers stacked inside this block.num_heads (int) – Number of attention heads passed to each
AttentionBlock.expansion (int) – MLP expansion factor passed to each
AttentionBlock.rngs (nnx.Rngs) – Flax RNG container used to initialize all sub-layers.
zero_init (bool, optional) – If
True(default), initialize the output projectionproj_outto zero so the block starts as the identity map.use_softplus (bool, optional) – If
True(default), use softplus to compute the affine scale (numerically stable, bounded tail). IfFalse, useexp(legacy behavior).soft_clip (float, optional) – Soft-clip magnitude applied via
tanhto raw network outputs before splitting into(a, b). Default is4.0. Set to0to disable clipping.rope (VisionRotaryEmbedding or None, optional) – If given, 2D rotary position embeddings replace the learned
pos_embed(which is then set toNone). Positions are laid out with theMprefix (conditioning) slots at the identity rotation (zero angles) followed by theTimage slots on the normalizedgrid(raster order, not permuted byperm). Default isNone(learnedpos_embed, unchanged behavior).grid (tuple of int or None, optional) –
(h, w)patch-grid shape used to build rope positions whenropeis given; required in that case. Default isNone.param_dtype (DTypeLike, optional) – Dtype for all stored (master) kernel/bias/embedding parameters. Default is
float32.dtype (DTypeLike, optional) – Compute dtype forwarded to
proj_in/proj_out/AttentionBlocklayers. Default isfloat32, matchingparam_dtype, so with default arguments this is a bit-identical no-op cast. The softplus/soft_clip affine-scale computation in_affine()stays unconditionally fp32 regardless of this knob.
- _affine(a)[source]#
Map raw log-scale
a->(scale, inv_scale, log_scale)in float32.scaleplays the role ofexp(a)(“1/sigma”): inverse multiplies byinv_scale, forward multiplies byscale, logdet sumslog_scale. softplus mode bounds the positive-scale tail and its gradient; theINV_SOFTPLUS_1offset makes it the identity ata == 0.- Parameters:
a (jax.Array)
- _embed_cond(cond)[source]#
Condition-only signals
(bias, prefix, mask)for_params_core().These depend on
condbut not on the modeled tokens, so the sampling scan computes them once instead of re-running the (potentially expensive, e.g. image-patchify) conditioner and rebuilding the prefix mask at every token step.- Parameters:
cond (jax.Array | None)
- _forward_reference(z, cond=None)[source]#
Map noise to data via full recompute (reference path for the KV cache).
forwardis the production path (KV-cached); this method is its correctness oracle, retained for the equivalence test suite — do not delete it.Sequentially scans over token positions via
jax.lax.scan, re-running the causal attention pass at each step so that tokeni’s parameters are conditioned on already-generated tokens0, …, i-1(mirrorsMaskedAutoregressive.forward).- Parameters:
z (Array) – Noise-space token sequence of shape
(B, T, F)or a flat array that will be reshaped to(B, T, F).cond (Array or None, optional) – Conditioning input, or
Nonefor an unconditional transform.
- Returns:
x (Array) – Data-space output of shape
(B, T, F).logabsdet (Array) – Log absolute determinant of the Jacobian of the forward map, shape
(B,). Equal to+Σ log_scaleover token and feature dimensions.
- _params(x_perm, cond)[source]#
(a, b) for the permuted tokens (single-shot; embeds the condition).
- Parameters:
x_perm (jax.Array)
cond (jax.Array | None)
- _params_core(x_perm, bias, prefix, mask)[source]#
(a, b) for the permuted tokens given precomputed conditioning.
SOS input-shift makes token i’s params depend only on tokens < i (and the condition).
bias/prefix/maskcome from_embed_cond().- Parameters:
x_perm (jax.Array)
- _prefix_mask(M, T)[source]#
Prefix-LM mask over [prefix(M); modeled(T)]: modeled is causal and sees all prefix; prefix is bidirectional among itself and never sees modeled (cond→x blocked).
- Parameters:
M (int)
T (int)
- Return type:
jax.Array
- forward(z, cond=None)[source]#
Map noise to data (the sampling direction), KV-cached.
Prefills the per-layer caches with the condition prefix (one parallel pass under the bidirectional prefix mask, matching the training-path mask rows), then scans over token positions decoding a single token per step against the caches. Verified equivalent to
_forward_reference()(full recompute) by the test suite.- Parameters:
z (Array) – Noise-space token sequence of shape
(B, T, F)or a flat array that will be reshaped to(B, T, F).cond (Array or None, optional) – Conditioning input, or
Nonefor an unconditional transform.
- Returns:
x (Array) – Data-space output of shape
(B, T, F).logabsdet (Array) – Log absolute determinant of the Jacobian of the forward map, shape
(B,). Equal to+Σ log_scaleover token and feature dimensions.
- inverse(x, cond=None)[source]#
Map data to noise (the density-evaluation direction).
Applies a per-token parallel affine transform
z = (x − b) · inv_scaleafter permuting tokens. Tokeni’s parameters(a, b)depend only on tokens at positions< i(causal attention on a shift-by-one input), giving a triangular Jacobian computable in a single forward pass.- Parameters:
x (Array) – Data-space token sequence of shape
(B, T, F)or a flat array that will be reshaped to(B, T, F).cond (Array or None, optional) – Conditioning input, or
Nonefor an unconditional transform.
- Returns:
z (Array) – Noise-space output of shape
(B, T, F).logabsdet (Array) – Log absolute determinant of the Jacobian of the inverse map, shape
(B,). Equal to-Σ log_scaleover token and feature dimensions.