xwm.encoders¶
Observation → latent. ViT encoders over 2-D patches or 3-D tubelets, and an MLP encoder over state vectors. All of them accept an arbitrary subset of the token grid.
Observation encoders: pixels or state vectors to latent tokens.
Every family in xwm.families takes an encoder from here. Which one you
pick is a question about the observation, not about the algorithm.
Modules:
| Name | Description |
|---|---|
image |
Image encoders. |
presets |
Standard ViT and predictor widths, shared across families. |
state |
Encoder for low-dimensional state observations. |
video |
Video encoders. |
vision |
The vision encoder every family shares: tokenise, add position, transform. |
Classes:
| Name | Description |
|---|---|
ImageEncoder |
A ViT over 2-D patches, maskable via |
StateEncoder |
Embed a state vector to |
VideoEncoder |
A ViT over space-time tubelets, maskable via |
VisionEncoder |
Tokenise, add position, run a transformer. Returns |
Functions:
| Name | Description |
|---|---|
image_encoder |
Build an |
preset |
Look up a size preset by name ( |
video_encoder |
Build a |
make_pos |
Build the additive table and/or the rotary embedding for |
ImageEncoder
¶
ImageEncoder(*, key: PRNGKey | None = None, img_size: int | tuple[int, int] = 224, patch_size: int = 16, in_channels: int = 3, embed_dim: int = 384, depth: int = 12, num_heads: int = 6, pos: PosKind = 'sincos', **kwargs)
Bases: VisionEncoder
A ViT over 2-D patches, maskable via keep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_size
|
int | tuple[int, int]
|
input resolution, |
224
|
patch_size
|
int
|
side length of a square patch. |
16
|
in_channels
|
int
|
input channels. |
3
|
embed_dim
|
int
|
token width. |
384
|
depth
|
int
|
transformer blocks. |
12
|
num_heads
|
int
|
attention heads. |
6
|
pos
|
PosKind
|
positional scheme -- |
'sincos'
|
Source code in xwm/encoders/image.py
StateEncoder
¶
StateEncoder(state_dim: int, embed_dim: int = 256, *, key: PRNGKey | None = None, hidden_dim: int | None = None, depth: int = 2, normalize: bool = True)
Bases: Module
Embed a state vector to (1, D) tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dim
|
int
|
length of the observation vector. |
required |
embed_dim
|
int
|
token width. |
256
|
hidden_dim
|
int | None
|
MLP width; defaults to |
None
|
depth
|
int
|
number of MLP blocks. |
2
|
normalize
|
bool
|
LayerNorm the input, which matters when the components have wildly different units (radians next to metres next to velocities). |
True
|
Source code in xwm/encoders/state.py
VideoEncoder
¶
VideoEncoder(*, key: PRNGKey | None = None, img_size: int | tuple[int, int] = 224, patch_size: int = 16, num_frames: int = 16, tubelet_size: int = 2, in_channels: int = 3, embed_dim: int = 384, depth: int = 12, num_heads: int = 6, pos: PosKind = 'sincos', **kwargs)
Bases: VisionEncoder
A ViT over space-time tubelets, maskable via keep.
Tokenising time jointly with space (tubelet_size > 1) is what makes the
token count tractable for clips, and it is what makes tube masking a
non-trivial prediction problem rather than a frame-copy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_size
|
int | tuple[int, int]
|
spatial resolution, |
224
|
num_frames
|
int
|
clip length in frames. |
16
|
tubelet_size
|
int
|
frames per token; |
2
|
Source code in xwm/encoders/video.py
VisionEncoder
¶
VisionEncoder(patch_embed: PatchEmbed, *, depth: int, num_heads: int, key: PRNGKey | None = None, pos: PosKind = 'sincos', mlp_ratio: float = 4.0, qk_norm: bool = False, dropout: float = 0.0, drop_path: float = 0.0, layer_scale: float | None = None, remat: bool = False)
Bases: Module
Tokenise, add position, run a transformer. Returns (N, D) tokens.
The one feature that separates this from a stock ViT is keep: the
encoder can be run on an arbitrary subset of the token grid. A JEPA
context encoder sees only visible tokens, so masked positions cost nothing
to compute -- which is where most of the training speedup over pixel
reconstruction comes from.
There is deliberately no [CLS] token. Predictive world models are
trained with no global objective to attach one to; pool the tokens instead
(xwm.nn.mean_pool or xwm.nn.AttentivePooler).
Source code in xwm/encoders/vision.py
image_encoder
¶
image_encoder(size: str = 'small', **kwargs) -> ImageEncoder
Build an ImageEncoder from a size preset.
preset
¶
Look up a size preset by name ("tiny" ... "huge").
Source code in xwm/encoders/presets.py
video_encoder
¶
video_encoder(size: str = 'small', **kwargs) -> VideoEncoder
make_pos
¶
make_pos(kind: PosKind, grid: tuple[int, ...], dim: int, num_heads: int, *, key: PRNGKey | None = None) -> tuple[SinCosPosEmbed | LearnedPosEmbed | None, AxialRoPE | None]
Build the additive table and/or the rotary embedding for kind.