xwm.masking¶
What a JEPA predicts: 2-D blocks, 3-D tubes, temporal splits. Masks are batch-shared int32 index arrays with static shapes, so a training step compiles once. See Masking.
Mask samplers: the choice of what a predictive world model predicts.
All samplers share one signature -- sampler(key) -> MaskBatch -- and return
static shapes, so a sampled mask can be fed straight into a jitted step.
Modules:
| Name | Description |
|---|---|
base |
Mask representation and shared helpers. |
block |
Multi-block masking for images (I-JEPA). |
random |
Uniform random masking. |
temporal |
Temporal context/target splits for action-conditioned prediction. |
tube |
Tube masking for video (V-JEPA). |
Classes:
| Name | Description |
|---|---|
MaskBatch |
A context/target split of one token grid. |
MultiBlockMask2d |
I-JEPA style multi-block sampler over a 2-D patch grid. |
RandomMask |
Sample a uniformly random context set; predict everything else. |
TemporalSplit |
Context is the first |
TubeMask3d |
V-JEPA style tube sampler over a |
Functions:
| Name | Description |
|---|---|
block_shapes |
All integer |
boolean_mask |
Convert an index array to a boolean |
complement |
Indices of |
gather |
Select rows of an |
subsample |
Draw exactly |
random_split |
Partition |
frame_indices |
Flat token indices belonging to temporal position |
long_range_tubes |
V-JEPA long-range preset: 2 tubes covering ~40% of the spatial grid each. |
short_range_tubes |
V-JEPA short-range preset: 8 tubes covering ~15% of the spatial grid each. |
MaskBatch
¶
Bases: NamedTuple
A context/target split of one token grid.
Attributes:
| Name | Type | Description |
|---|---|---|
context |
Array
|
|
targets |
Array
|
|
MultiBlockMask2d
¶
MultiBlockMask2d(grid: tuple[int, int], *, n_targets: int = 4, target_scale: float = 0.15, aspect_range: tuple[float, float] = (0.75, 1.5), context_scale: float | None = None, max_tries: int = 100)
Bases: Module
I-JEPA style multi-block sampler over a 2-D patch grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple[int, int]
|
|
required |
n_targets
|
int
|
number of target blocks to predict (I-JEPA uses 4). |
4
|
target_scale
|
float
|
fraction of the grid covered by each target block. |
0.15
|
aspect_range
|
tuple[float, float]
|
allowed width/height ratios for target blocks. |
(0.75, 1.5)
|
context_scale
|
float | None
|
fraction of all tokens kept as context. |
None
|
max_tries
|
int
|
resampling budget when a draw leaves too little context. |
100
|
Every target block holds exactly target_size tokens -- the closest
usable size to round(target_scale * n_tokens). The aspect ratio is
redrawn each call from the divisor pairs of that area, so block geometry
varies while every returned array's shape stays static, which is what lets
the training step be jitted once.
Source code in xwm/masking/block.py
RandomMask
¶
Bases: Module
Sample a uniformly random context set; predict everything else.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_tokens
|
int
|
size of the token grid. |
required |
mask_ratio
|
float
|
fraction of tokens to predict. |
0.75
|
n_targets
|
int
|
split the masked tokens into this many equal target blocks. |
1
|
Source code in xwm/masking/random.py
TemporalSplit
¶
Bases: Module
Context is the first n_context_frames; targets are the frames after.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple[int, int, int]
|
|
required |
n_context_frames
|
int
|
number of leading temporal positions kept visible. |
1
|
horizon
|
int | None
|
how many future frames to predict; |
None
|
Deterministic -- the key argument exists only so every sampler in xwm
shares one call signature.
Source code in xwm/masking/temporal.py
TubeMask3d
¶
TubeMask3d(grid: tuple[int, int, int], *, n_targets: int = 8, spatial_scale: float = 0.15, temporal_extent: int | None = None, aspect_range: tuple[float, float] = (0.75, 1.5), context_scale: float | None = None, max_tries: int = 100)
Bases: Module
V-JEPA style tube sampler over a (gt, gh, gw) token grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
tuple[int, int, int]
|
token grid, e.g. |
required |
n_targets
|
int
|
number of tubes to predict. |
8
|
spatial_scale
|
float
|
fraction of the spatial grid each tube covers. |
0.15
|
temporal_extent
|
int | None
|
tube length in temporal tokens; |
None
|
aspect_range
|
tuple[float, float]
|
allowed width/height ratios for the tube cross-section. |
(0.75, 1.5)
|
context_scale
|
float | None
|
fraction of all tokens kept as context. |
None
|
max_tries
|
int
|
resampling budget when a draw leaves too little context. |
100
|
Attributes:
| Name | Type | Description |
|---|---|---|
target_size |
int
|
Tokens per tube: cross-section area times temporal extent. |
Source code in xwm/masking/tube.py
block_shapes
¶
block_shapes(area: int, aspect_range: tuple[float, float], bounds: tuple[int, int] | None = None) -> list[tuple[int, int]]
All integer (h, w) with h * w == area, aspect in range, fitting bounds.
Fixing the area while varying the aspect ratio is how xwm gets I-JEPA's
varied block geometry without varying array shapes: every candidate yields
exactly area tokens, so the sampler's output shape is static.
Source code in xwm/masking/base.py
boolean_mask
¶
complement
¶
Indices of range(n_tokens) not present in idx.
gather
¶
subsample
¶
Draw exactly size indices from idx without replacement.
Raises if idx is too small -- samplers are constructed so this cannot
happen, and a loud failure beats a silently reshaped batch.
Source code in xwm/masking/base.py
random_split
¶
Partition n_tokens into n_context visible and the rest masked.
Uses the argsort-of-noise trick, so both outputs have static shapes and the whole thing is jittable and differentiable-through-free.
Source code in xwm/masking/random.py
frame_indices
¶
Flat token indices belonging to temporal position t.
long_range_tubes
¶
long_range_tubes(grid: tuple[int, int, int], **kwargs) -> TubeMask3d
V-JEPA long-range preset: 2 tubes covering ~40% of the spatial grid each.
Source code in xwm/masking/tube.py
short_range_tubes
¶
short_range_tubes(grid: tuple[int, int, int], **kwargs) -> TubeMask3d
V-JEPA short-range preset: 8 tubes covering ~15% of the spatial grid each.