mstar.model.vjepa2.components.ac_predictor#

V-JEPA 2 action-conditioned predictor (for V-JEPA 2-AC).

Port of VisionTransformerPredictorAC and supporting blocks from vjepa2/src/models/ac_predictor.py + vjepa2/src/models/utils/modules.py. The HuggingFace Transformers port does NOT include the AC variant, so this file stays close to the upstream naming to preserve checkpoint-key parity with the upstream vjepa2-ac-vitg weights.

Key differences from the masked predictor:

  • Fused qkv Linear (dim -> dim*3) per layer (upstream layout).

  • Action + state + (optional) extrinsics tokens are interleaved into the spatial sequence per timestep: [a, s, x_0, ..., x_{H*W-1}] (+ e if use_extrinsics). Action tokens rotate only along the depth axis.

  • Causal attention across frames via build_action_block_causal_attention_mask.

  • Uses F.scaled_dot_product_attention (SDPA) — the attention mask is always present, so the eager fallback is unreachable.

Functions

build_action_block_causal_attention_mask(...)

Build a [N, N] boolean mask where frame t attends only to frames 0..t.

Classes

ACBlock(dim, num_heads, mlp_ratio, qkv_bias, ...)

ACRoPEAttention(dim, num_heads[, qkv_bias, ...])

VisionTransformerPredictorAC(config)

Action-conditioned V-JEPA 2 predictor.

class mstar.model.vjepa2.components.ac_predictor.ACBlock(dim, num_heads, mlp_ratio, qkv_bias, layer_norm_eps, grid_size)[source]#

Bases: Module

Parameters:
forward(x, attn_mask, t, h, w, action_tokens, t_0=0, label=None, d_pos=None, h_pos=None, w_pos=None, time_pos=None)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:
Return type:

Tensor

class mstar.model.vjepa2.components.ac_predictor.ACRoPEAttention(dim, num_heads, qkv_bias=True, grid_size=16)[source]#

Bases: Module

Parameters:
bind_resources(resources)[source]#
Parameters:

resources (dict)

Return type:

None

forward(x, attn_mask, t, h, w, action_tokens, t_0=0, label=None, d_pos=None, h_pos=None, w_pos=None, time_pos=None)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:
Return type:

Tensor

forward_cached(x, d_pos, h_pos, w_pos, time_pos, action_tokens)[source]#

Single-frame cached attention with pre-computed position tensors.

All position tensors are expected to already be on the correct device. Callers must compute them via _compute_positions (or the model-level _compute_rope_positions) and may store them in static GPU buffers updated via .copy_() so the surrounding CUDA graph sees the new values.

Parity with the regular forward was partially validated in test/modular/vjepa2/test_ac_rope_parity.py and more thoroughly in test/modular/vjepa2/test_ac_kv_cache_parity.py.

Parameters:
Return type:

Tensor

class mstar.model.vjepa2.components.ac_predictor.VisionTransformerPredictorAC(config)[source]#

Bases: Module

Action-conditioned V-JEPA 2 predictor.

Forward signature matches the upstream class so parity tests can pass outputs directly. Expects encoder context embeddings plus per-timestep action / state (and optional extrinsics) tensors.

Parameters:

config (VJepa2ACPredictorConfig)

property attn_mask: Tensor | None#

Back-compat accessor used by tests. Builds the mask on CPU if it hasn’t been built yet.

forward(x, actions, states, extrinsics=None, t_0=0, label=None)[source]#
Parameters:
  • x (Tensor) – encoder context embeddings [B, N_ctxt, embed_dim].

  • actions (Tensor) – [B, T, action_embed_dim].

  • states (Tensor) – [B, T, action_embed_dim].

  • extrinsics (Tensor | None) – [B, T, action_embed_dim - 1] (only when use_extrinsics=True).

  • t_0 (int)

  • label (str | None)

Returns:

Predicted embeddings, [B, N_ctxt, embed_dim].

Return type:

Tensor

make_block_loop_fn(label, static_pos_bufs, cond_tokens)[source]#

Return a closure capturing the block loop for PiecewiseCudaGraphRunner.

The returned fn(x) -> x reads position tensors from static_pos_bufs (which the runner updates via .copy_() before each replay) and attends under label, whose plan the runner drives outside the graph before each replay.

The stream advance is NOT done inside this closure — the runner commits the step after graph.replay(), outside the captured region.

Parameters:
  • label (str | None)

  • static_pos_bufs (dict)

  • cond_tokens (int)

mstar.model.vjepa2.components.ac_predictor.build_action_block_causal_attention_mask(grid_depth, grid_height, grid_width, add_tokens=1)[source]#

Build a [N, N] boolean mask where frame t attends only to frames 0..t.

Each frame contributes add_tokens + grid_height * grid_width tokens.

Parameters:
  • grid_depth (int)

  • grid_height (int)

  • grid_width (int)

  • add_tokens (int)

Return type:

Tensor