xwm.nn¶
Layers: attention, transformer blocks, RoPE, patch and tubelet embeddings, normalisations. Unbatched, vmaped by the caller.
Generic neural building blocks: layers that know nothing about world models.
These are the pieces you compose into encoders and predictors. Every module is written for a single unbatched sample; vmap for batches.
Modules:
| Name | Description |
|---|---|
attention |
Self- and cross-attention over unbatched token sequences |
drop |
Stochastic-depth (drop-path) regularisation. |
embed |
Positional information: fixed sin-cos tables, learned tables, axial RoPE. |
mlp |
Feed-forward blocks. |
norm |
Normalisation layers. |
patch |
Patch and tubelet embeddings -- the entry point from pixels to tokens. |
transformer |
Pre-norm transformer blocks and stacks. |
Classes:
| Name | Description |
|---|---|
Attention |
Multi-head attention with optional QK-norm and axial RoPE. |
CrossAttention |
Attention from a query sequence into a separate key/value sequence. |
DropPath |
Drop a residual branch entirely, with probability |
AxialRoPE |
Axial rotary embeddings for 1-D, 2-D (image) or 3-D (video) grids. |
LearnedPosEmbed |
A trainable |
SinCosPosEmbed |
A frozen factorised sin-cos table over a grid. |
Mlp |
Two-layer position-wise MLP, applied to each token independently. |
SwiGLU |
Gated feed-forward (SwiGLU), a common drop-in for |
LayerNorm |
Layer normalisation over the last axis, with optional affine params. |
RMSNorm |
Root-mean-square normalisation (no mean subtraction). |
SimNorm |
Simplicial normalisation: softmax over fixed-size groups of channels. |
PatchEmbed2d |
Split an image into non-overlapping patches and linearly embed them. |
PatchEmbed3d |
Tubelet embedding: patches spanning |
AttentivePooler |
Pool a token sequence into |
Block |
Pre-norm self-attention block: |
CrossBlock |
Pre-norm cross-attention block, then an MLP. |
LayerScale |
Per-channel learnable residual gain, initialised near zero. |
Transformer |
A stack of |
Functions:
| Name | Description |
|---|---|
attend |
Scaled dot-product attention on |
block_causal_attention_mask |
Causal across blocks, fully connected within a block. |
causal_attention_mask |
Boolean |
linear_droppath_schedule |
Linearly increasing drop-path rates across depth (the ViT default). |
grid_coords |
|
sincos_pos_embed |
Fixed sin-cos table for an n-dimensional grid, flattened row-major. |
l2_normalize |
Project onto the unit sphere along |
unpatchify_2d |
Fold |
mean_pool |
Average over the token axis of an |
Attention
¶
Attention(dim: int, num_heads: int, *, key: PRNGKey | None = None, qkv_bias: bool = True, qk_norm: bool = False, dropout: float = 0.0, rope: AxialRoPE | None = None)
Bases: Module
Multi-head attention with optional QK-norm and axial RoPE.
Set rope to an AxialRoPE to use relative
positions; leave it None and add an absolute table to the inputs
instead (what I-JEPA and V-JEPA do).
Source code in xwm/nn/attention.py
CrossAttention
¶
CrossAttention(dim: int, num_heads: int, *, key: PRNGKey | None = None, kv_dim: int | None = None, qkv_bias: bool = True, qk_norm: bool = False, dropout: float = 0.0)
Bases: Module
Attention from a query sequence into a separate key/value sequence.
Used by the attentive probe (a learned query pools an encoder's tokens) and by predictors that keep context tokens read-only.
Source code in xwm/nn/attention.py
DropPath
¶
Bases: Module
Drop a residual branch entirely, with probability p.
Applied per-sample; since xwm modules are unbatched this is a single Bernoulli draw that zeroes (or rescales) the whole branch.
Source code in xwm/nn/drop.py
AxialRoPE
¶
Bases: Module
Axial rotary embeddings for 1-D, 2-D (image) or 3-D (video) grids.
The head dimension is split across axes by axis_dims and each chunk
is rotated by its axis' coordinate. Unlike additive tables, RoPE acts inside
attention and encodes relative position, which extrapolates better to
resolutions and clip lengths unseen during training.
Source code in xwm/nn/embed.py
LearnedPosEmbed
¶
SinCosPosEmbed
¶
Mlp
¶
Mlp(dim: int, hidden_dim: int | None = None, out_dim: int | None = None, *, key: PRNGKey | None = None, act: Callable[[Array], Array] = gelu, dropout: float = 0.0)
Bases: Module
Two-layer position-wise MLP, applied to each token independently.
Source code in xwm/nn/mlp.py
SwiGLU
¶
SwiGLU(dim: int, hidden_dim: int | None = None, out_dim: int | None = None, *, key: PRNGKey | None = None, dropout: float = 0.0)
Bases: Module
Gated feed-forward (SwiGLU), a common drop-in for Mlp.
Source code in xwm/nn/mlp.py
LayerNorm
¶
Bases: Module
Layer normalisation over the last axis, with optional affine params.
Source code in xwm/nn/norm.py
RMSNorm
¶
SimNorm
¶
Bases: Module
Simplicial normalisation: softmax over fixed-size groups of channels.
TD-MPC2's latent normalisation, and the detail its stability rests on. The
latent is split into groups of group_size channels and each group is
softmaxed, so the latent becomes a concatenation of points on probability
simplices.
Why that helps where LayerNorm does not: the representation is bounded (every
entry in [0, 1], every group summing to one), so a long recurrent unroll
cannot drift to infinity -- but it also cannot collapse to a single point,
because each group must keep its mass distributed to stay off the simplex
corners. Bounded without being degenerate is exactly what a model that gets
unrolled inside a planner needs.
Source code in xwm/nn/norm.py
PatchEmbed2d
¶
PatchEmbed2d(img_size: int | tuple[int, int], patch_size: int, in_channels: int, embed_dim: int, *, key: PRNGKey | None = None, bias: bool = True)
Bases: Module
Split an image into non-overlapping patches and linearly embed them.
Implemented as a reshape plus a matmul rather than a strided convolution:
identical arithmetic, but it makes the token ordering explicit (row-major
over (H // p, W // p)), which the masking code depends on.
Source code in xwm/nn/patch.py
PatchEmbed3d
¶
PatchEmbed3d(img_size: int | tuple[int, int], patch_size: int, num_frames: int, tubelet_size: int, in_channels: int, embed_dim: int, *, key: PRNGKey | None = None, bias: bool = True)
Bases: Module
Tubelet embedding: patches spanning tubelet_size frames.
Video JEPAs tokenise space and time jointly, so a clip of T frames at
tubelet_size = 2 yields T // 2 temporal positions. Token order is
row-major over (T // ts, H // p, W // p).
Source code in xwm/nn/patch.py
AttentivePooler
¶
AttentivePooler(dim: int, num_heads: int, *, key: PRNGKey | None = None, kv_dim: int | None = None, n_queries: int = 1, depth: int = 1, mlp_ratio: float = 4.0)
Bases: Module
Pool a token sequence into n_queries vectors via cross-attention.
The standard read-out head for frozen JEPA encoders: a learned query attends over the tokens, which recovers substantially more of the representation than mean-pooling for the same probe budget.
Source code in xwm/nn/transformer.py
Block
¶
Block(dim: int, num_heads: int, *, key: PRNGKey | None = None, mlp_ratio: float = 4.0, qkv_bias: bool = True, qk_norm: bool = False, dropout: float = 0.0, drop_path: float = 0.0, layer_scale: float | None = None, rope: AxialRoPE | None = None, act: Callable[[Array], Array] = gelu)
Bases: Module
Pre-norm self-attention block: x + attn(ln(x)), x + mlp(ln(x)).
Source code in xwm/nn/transformer.py
CrossBlock
¶
CrossBlock(dim: int, num_heads: int, *, key: PRNGKey | None = None, kv_dim: int | None = None, mlp_ratio: float = 4.0, dropout: float = 0.0)
Bases: Module
Pre-norm cross-attention block, then an MLP.
Source code in xwm/nn/transformer.py
LayerScale
¶
Transformer
¶
Transformer(dim: int, depth: int, num_heads: int, *, key: PRNGKey | None = None, mlp_ratio: float = 4.0, qkv_bias: bool = True, qk_norm: bool = False, dropout: float = 0.0, drop_path: float = 0.0, layer_scale: float | None = None, rope: AxialRoPE | None = None, final_norm: bool = True, remat: bool = False)
Bases: Module
A stack of Block s with a final norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth
|
int
|
number of blocks. |
required |
drop_path
|
float
|
maximum stochastic-depth rate; rates increase linearly with depth, as in the ViT/DeiT recipe. |
0.0
|
remat
|
bool
|
wrap each block in |
False
|
Source code in xwm/nn/transformer.py
attend
¶
Scaled dot-product attention on (heads, N, head_dim) inputs.
mask is a boolean (N_q, N_kv) array where True means attend.
Delegates to jax.nn.dot_product_attention so the fused backends are
used where available.
Source code in xwm/nn/attention.py
block_causal_attention_mask
¶
Causal across blocks, fully connected within a block.
This is the mask an action-conditioned video predictor wants: all tokens of a frame see each other, and frames only see the past.
Source code in xwm/nn/attention.py
causal_attention_mask
¶
linear_droppath_schedule
¶
Linearly increasing drop-path rates across depth (the ViT default).
grid_coords
¶
sincos_pos_embed
¶
Fixed sin-cos table for an n-dimensional grid, flattened row-major.
The width is split across axes by axis_dims. For a video grid
(T, H, W) this is the factorised 3-D embedding used by V-JEPA.
Returns (prod(grid), dim).
Source code in xwm/nn/embed.py
l2_normalize
¶
unpatchify_2d
¶
Fold (N, C * p * p) patch values back into a (C, H, W) image.
Only needed for visualisation and for pixel-space baselines -- predictive world models in xwm never reconstruct pixels during training.