[Model] Add InternVLA-A1 offline inference support (#2737)

Signed-off-by: zhuyangkun <zyk_120403228@163.com>
Signed-off-by: bl-wang1 <918395762@qq.com>
Co-authored-by: wangbolun <918395762@qq.com>
This commit is contained in:
yangkun.zhuandwangbolun authored and GitHub committed 2026-04-27 02:28:07 -07:00
1 parent 239abcb480
commit 0016006974
17 files changed
+3509

No files matched your search

@@ -0,0 +1,25 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""InternVLA-A1 diffusion model components."""
from vllm_omni.diffusion.models.internvla_a1.config import (
InternVLAA1Config,
InternVLAA1TrainMetadata,
)
from vllm_omni.diffusion.models.internvla_a1.model_internvla_a1 import (
InternVLAA1,
InternVLAA1Policy,
)
from vllm_omni.diffusion.models.internvla_a1.pipeline_internvla_a1 import (
InternVLAA1Pipeline,
get_internvla_a1_post_process_func,
)
__all__ = [
"InternVLAA1",
"InternVLAA1Config",
"InternVLAA1Pipeline",
"InternVLAA1TrainMetadata",
"InternVLAA1Policy",
"get_internvla_a1_post_process_func",
]
@@ -0,0 +1,252 @@
from __future__ import annotations
import torch
import torch.nn as nn
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
ALL_ATTENTION_FUNCTIONS,
BaseModelOutputWithPast,
Cache,
DynamicCache,
FlashAttentionKwargs,
Qwen3VLConfig,
Qwen3VLPreTrainedModel,
Qwen3VLTextConfig,
Qwen3VLTextRotaryEmbedding,
Qwen3VLVisionModel,
Unpack,
apply_rotary_pos_emb,
check_model_inputs,
create_causal_mask,
deprecate_kwarg,
eager_attention_forward,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLForConditionalGeneration as HFQwen3VLForConditionalGeneration,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLModel as HFQwen3VLModel,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLTextAttention as HFQwen3VLTextAttention,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLTextDecoderLayer as HFQwen3VLTextDecoderLayer,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLTextModel as HFQwen3VLTextModel,
)
from transformers.models.qwen3_vl.modeling_qwen3_vl import (
Qwen3VLTextRMSNorm as HFQwen3VLTextRMSNorm,
)
class Qwen3VLTextRMSNorm(HFQwen3VLTextRMSNorm):
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
input_dtype = hidden_states.dtype
hidden_states = hidden_states.to(torch.float32)
variance = hidden_states.pow(2).mean(-1, keepdim=True)
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
return (self.weight * hidden_states).to(input_dtype)
class Qwen3VLTextAttention(HFQwen3VLTextAttention):
def __init__(self, config: Qwen3VLTextConfig, layer_idx: int):
super().__init__(config, layer_idx)
self.q_norm = Qwen3VLTextRMSNorm(self.head_dim, eps=config.rms_norm_eps)
self.k_norm = Qwen3VLTextRMSNorm(self.head_dim, eps=config.rms_norm_eps)
@deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
attention_mask: torch.Tensor | None,
past_key_values: Cache | None = None,
cache_position: torch.LongTensor | None = None,
use_cache: bool = False,
**kwargs: Unpack[FlashAttentionKwargs],
) -> tuple[torch.Tensor, torch.Tensor | None]:
input_shape = hidden_states.shape[:-1]
hidden_shape = (*input_shape, -1, self.head_dim)
query_states = self.q_norm(self.q_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
key_states = self.k_norm(self.k_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
cos, sin = position_embeddings
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
if past_key_values is not None:
if use_cache:
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
key_states, value_states = past_key_values.update(
key_states, value_states, self.layer_idx, cache_kwargs
)
else:
key_states = torch.cat([past_key_values[self.layer_idx][0], key_states], dim=2)
value_states = torch.cat([past_key_values[self.layer_idx][1], value_states], dim=2)
attention_interface = eager_attention_forward
if self.config._attn_implementation != "eager":
attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
attn_output, attn_weights = attention_interface(
self,
query_states,
key_states,
value_states,
attention_mask,
dropout=0.0 if not self.training else self.attention_dropout,
scaling=self.scaling,
**kwargs,
)
attn_output = attn_output.reshape(*input_shape, -1).contiguous()
attn_output = self.o_proj(attn_output)
return attn_output, attn_weights
class Qwen3VLTextDecoderLayer(HFQwen3VLTextDecoderLayer):
def __init__(self, config: Qwen3VLTextConfig, layer_idx: int):
super().__init__(config, layer_idx)
self.self_attn = Qwen3VLTextAttention(config=config, layer_idx=layer_idx)
self.input_layernorm = Qwen3VLTextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.post_attention_layernorm = Qwen3VLTextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
class Qwen3VLTextModel(HFQwen3VLTextModel):
config: Qwen3VLTextConfig
_no_split_modules = ["Qwen3VLTextDecoderLayer"]
_repeated_blocks = ["Qwen3VLTextDecoderLayer"]
def __init__(self, config: Qwen3VLTextConfig):
Qwen3VLPreTrainedModel.__init__(self, config)
self.padding_idx = config.pad_token_id
self.vocab_size = config.vocab_size
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
self.layers = nn.ModuleList(
[Qwen3VLTextDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
)
self.norm = Qwen3VLTextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.rotary_emb = Qwen3VLTextRotaryEmbedding(config=config)
self.gradient_checkpointing = False
self.post_init()
@check_model_inputs
def forward(
self,
input_ids: torch.LongTensor = None,
attention_mask: torch.Tensor | None = None,
position_ids: torch.LongTensor | None = None,
past_key_values: Cache | None = None,
inputs_embeds: torch.FloatTensor | None = None,
use_cache: bool | None = None,
cache_position: torch.LongTensor | None = None,
visual_pos_masks: torch.Tensor | None = None,
deepstack_visual_embeds: list[torch.Tensor] | None = None,
**kwargs: Unpack[FlashAttentionKwargs],
) -> tuple | BaseModelOutputWithPast:
if (input_ids is None) ^ (inputs_embeds is not None):
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
if use_cache and past_key_values is None and not torch.jit.is_tracing():
past_key_values = DynamicCache(config=self.config)
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
if cache_position is None:
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
cache_position = torch.arange(
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
)
if position_ids is None:
position_ids = cache_position.view(1, 1, -1).expand(3, inputs_embeds.shape[0], -1)
elif position_ids.ndim == 2:
position_ids = position_ids[None, ...].expand(3, position_ids.shape[0], -1)
if position_ids.ndim == 3 and position_ids.shape[0] == 4:
text_position_ids = position_ids[0]
position_ids = position_ids[1:]
else:
text_position_ids = position_ids[0]
attention_mask = create_causal_mask(
config=self.config,
input_embeds=inputs_embeds,
attention_mask=attention_mask,
cache_position=cache_position,
past_key_values=past_key_values,
position_ids=text_position_ids,
)
hidden_states = inputs_embeds
position_embeddings = self.rotary_emb(hidden_states, position_ids)
for layer_idx, decoder_layer in enumerate(self.layers):
hidden_states = decoder_layer(
hidden_states,
attention_mask=attention_mask,
position_ids=text_position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
cache_position=cache_position,
position_embeddings=position_embeddings,
**kwargs,
)
if deepstack_visual_embeds is not None and layer_idx in range(len(deepstack_visual_embeds)):
hidden_states = self._deepstack_process(
hidden_states,
visual_pos_masks,
deepstack_visual_embeds[layer_idx],
)
hidden_states = self.norm(hidden_states)
return BaseModelOutputWithPast(
last_hidden_state=hidden_states,
past_key_values=past_key_values,
)
class Qwen3VLModel(HFQwen3VLModel):
base_model_prefix = ""
_checkpoint_conversion_mapping = {}
accepts_loss_kwargs = False
config: Qwen3VLConfig
_no_split_modules = ["Qwen3VLTextDecoderLayer", "Qwen3VLVisionBlock"]
def __init__(self, config):
Qwen3VLPreTrainedModel.__init__(self, config)
self.visual = Qwen3VLVisionModel._from_config(config.vision_config)
self.language_model = Qwen3VLTextModel._from_config(config.text_config)
self.rope_deltas = None
self.post_init()
class Qwen3VLForConditionalGeneration(HFQwen3VLForConditionalGeneration):
_checkpoint_conversion_mapping = {}
_tied_weights_keys = ["lm_head.weight"]
accepts_loss_kwargs = False
config: Qwen3VLConfig
def __init__(self, config):
Qwen3VLPreTrainedModel.__init__(self, config)
self.model = Qwen3VLModel(config)
self.lm_head = nn.Linear(config.text_config.hidden_size, config.text_config.vocab_size, bias=False)
self.post_init()
__all__ = [
"Qwen3VLVisionModel",
"Qwen3VLForConditionalGeneration",
"Qwen3VLModel",
"Qwen3VLPreTrainedModel",
"Qwen3VLTextModel",
"apply_rotary_pos_emb",
"eager_attention_forward",
]
@@ -0,0 +1,126 @@
from __future__ import annotations
import json
import os
from dataclasses import dataclass, field
from dataclasses import fields as dataclass_fields
from pathlib import Path
from typing import Any
from huggingface_hub.constants import HF_HOME
ACTION = "action"
OBS_STR = "observation"
OBS_PREFIX = OBS_STR + "."
OBS_STATE = OBS_STR + ".state"
OBS_TASK = OBS_STR + ".task"
OBS_IMAGE = OBS_STR + ".image"
OBS_IMAGES = OBS_IMAGE + "s"
OPENPI_ATTENTION_MASK_VALUE = -2.3819763e38
DEFAULT_QWEN3_VL_MODEL = os.getenv("INTERNVLA_A1_PROCESSOR_DIR", "Qwen/Qwen3-VL-2B-Instruct")
DEFAULT_COSMOS_REPO = "nvidia/Cosmos-Tokenizer-CI8x8"
DEFAULT_COSMOS_DIR = Path(
os.getenv(
"INTERNVLA_A1_COSMOS_DIR",
str(Path(HF_HOME) / "hub" / "Cosmos-Tokenizer-CI8x8"),
)
)
@dataclass
class InternVLAA1Config:
"""Standalone-compatible InternVLA-A1 config with a few fake-smoke defaults."""
type: str = "internvla_a1"
qwen3_vl_variant: str = "qwen3_vl_28l"
action_expert_variant: str = "qwen3_28l"
dtype: str = "bfloat16"
device: str = "cuda"
chunk_size: int = 50
n_action_steps: int = 50
max_state_dim: int = 32
max_action_dim: int = 32
num_inference_steps: int = 10
time_sampling_beta_alpha: float = 1.5
time_sampling_beta_beta: float = 1.0
time_sampling_scale: float = 0.999
time_sampling_offset: float = 0.001
min_period: float = 4e-3
max_period: float = 4.0
image_resolution: tuple[int, int] = (224, 224)
empty_cameras: int = 0
gradient_checkpointing: bool = False
compile_model: bool = False
compile_mode: str = "max-autotune"
enable_regional_compile: bool = False
regional_compile_dynamic: bool = True
enable_suffix_static_context_optimization: bool = False
attn_implementation: str = "eager"
tokenizer_max_length: int = 48
freeze_vision_encoder: bool = False
train_expert_only: bool = False
train_vlm_only: bool = False
scale_factor: int = 8
lambda_gen: float = 0.01
input_features: dict[str, Any] = field(default_factory=dict)
output_features: dict[str, Any] = field(default_factory=dict)
# Fake/smoke-only knobs used by the lightweight pipeline path.
vocab_size: int = 256
hidden_size: int = 128
intermediate_size: int = 256
num_attention_heads: int = 4
num_hidden_layers: int = 2
num_cameras: int = 3
image_history: int = 2
pixel_feature_dim: int = 48
@classmethod
def from_pretrained(cls, checkpoint_dir: str | Path) -> InternVLAA1Config:
checkpoint_dir = Path(checkpoint_dir)
with open(checkpoint_dir / "config.json", encoding="utf-8") as f:
raw = json.load(f)
return cls.from_model_config(raw)
@classmethod
def from_model_config(cls, model_config: dict[str, Any] | None) -> InternVLAA1Config:
if not model_config:
return cls()
raw = dict(model_config)
raw["image_resolution"] = tuple(raw.get("image_resolution", [224, 224]))
allowed = {item.name for item in dataclass_fields(cls)}
filtered = {key: value for key, value in raw.items() if key in allowed}
return cls(**filtered)
@dataclass
class InternVLAA1TrainMetadata:
action_mode: str = "delta"
processor_model_name: str = DEFAULT_QWEN3_VL_MODEL
@classmethod
def from_pretrained(cls, checkpoint_dir: str | Path) -> InternVLAA1TrainMetadata:
checkpoint_dir = Path(checkpoint_dir)
path = checkpoint_dir / "train_config.json"
if not path.exists():
return cls()
with open(path, encoding="utf-8") as f:
raw = json.load(f)
dataset_cfg = raw.get("dataset", {})
processor_model_name = DEFAULT_QWEN3_VL_MODEL
for transform in dataset_cfg.get("data_transforms", {}).get("inputs", []):
if transform.get("type") == "internvla_a1_processor":
processor_model_name = transform.get(
"pretrained_model_name_or_path",
processor_model_name,
)
break
return cls(
action_mode=dataset_cfg.get("action_mode", "delta"),
processor_model_name=processor_model_name,
)
@@ -0,0 +1,416 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# This file vendors the minimal Continuous Image (CI) tokenizer implementation
# needed by InternVLA-A1 from NVIDIA/Cosmos-Tokenizer. The code is intentionally
# reduced to the CI image path only so the runtime does not depend on the
# external `cosmos_tokenizer` package.
from __future__ import annotations
import math
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
_WAVELETS = {
"haar": torch.tensor([0.7071067811865476, 0.7071067811865476]),
"rearrange": torch.tensor([1.0, 1.0]),
}
_PERSISTENT = False
def nonlinearity(x: torch.Tensor) -> torch.Tensor:
return x * torch.sigmoid(x)
def normalize(in_channels: int, num_groups: int = 32) -> nn.GroupNorm:
return nn.GroupNorm(num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True)
class IdentityDistribution(nn.Module):
def forward(self, parameters: torch.Tensor) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
zeros = torch.tensor([0.0], device=parameters.device, dtype=parameters.dtype)
return parameters, (zeros, zeros)
class Patcher(nn.Module):
def __init__(self, patch_size: int = 1, patch_method: str = "haar") -> None:
super().__init__()
self.patch_size = patch_size
self.patch_method = patch_method
self.register_buffer("wavelets", _WAVELETS[patch_method], persistent=_PERSISTENT)
self.range = range(int(torch.log2(torch.tensor(self.patch_size)).item()))
self.register_buffer(
"_arange",
torch.arange(_WAVELETS[patch_method].shape[0]),
persistent=_PERSISTENT,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.patch_method == "haar":
return self._haar(x)
if self.patch_method == "rearrange":
return self._arrange(x)
raise ValueError(f"Unknown patch method: {self.patch_method}")
def _dwt(self, x: torch.Tensor, mode: str = "reflect", *, rescale: bool = False) -> torch.Tensor:
dtype = x.dtype
h = self.wavelets
n = h.shape[0]
g = x.shape[1]
hl = h.flip(0).reshape(1, 1, -1).repeat(g, 1, 1).to(dtype=dtype)
hh = (h * ((-1) ** self._arange)).reshape(1, 1, -1).repeat(g, 1, 1).to(dtype=dtype)
x = F.pad(x, pad=(n - 2, n - 1, n - 2, n - 1), mode=mode).to(dtype)
xl = F.conv2d(x, hl.unsqueeze(2), groups=g, stride=(1, 2))
xh = F.conv2d(x, hh.unsqueeze(2), groups=g, stride=(1, 2))
xll = F.conv2d(xl, hl.unsqueeze(3), groups=g, stride=(2, 1))
xlh = F.conv2d(xl, hh.unsqueeze(3), groups=g, stride=(2, 1))
xhl = F.conv2d(xh, hl.unsqueeze(3), groups=g, stride=(2, 1))
xhh = F.conv2d(xh, hh.unsqueeze(3), groups=g, stride=(2, 1))
out = torch.cat([xll, xlh, xhl, xhh], dim=1)
if rescale:
out = out / 2
return out
def _haar(self, x: torch.Tensor) -> torch.Tensor:
for _ in self.range:
x = self._dwt(x, rescale=True)
return x
def _arrange(self, x: torch.Tensor) -> torch.Tensor:
return rearrange(
x,
"b c (h p1) (w p2) -> b (c p1 p2) h w",
p1=self.patch_size,
p2=self.patch_size,
).contiguous()
class UnPatcher(nn.Module):
def __init__(self, patch_size: int = 1, patch_method: str = "haar") -> None:
super().__init__()
self.patch_size = patch_size
self.patch_method = patch_method
self.register_buffer("wavelets", _WAVELETS[patch_method], persistent=_PERSISTENT)
self.range = range(int(torch.log2(torch.tensor(self.patch_size)).item()))
self.register_buffer(
"_arange",
torch.arange(_WAVELETS[patch_method].shape[0]),
persistent=_PERSISTENT,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.patch_method == "haar":
return self._ihaar(x)
if self.patch_method == "rearrange":
return self._iarrange(x)
raise ValueError(f"Unknown patch method: {self.patch_method}")
def _idwt(self, x: torch.Tensor, *, rescale: bool = False) -> torch.Tensor:
dtype = x.dtype
h = self.wavelets
n = h.shape[0]
g = x.shape[1] // 4
hl = h.flip([0]).reshape(1, 1, -1).repeat([g, 1, 1]).to(dtype=dtype)
hh = (h * ((-1) ** self._arange)).reshape(1, 1, -1).repeat(g, 1, 1).to(dtype=dtype)
xll, xlh, xhl, xhh = torch.chunk(x.to(dtype), 4, dim=1)
yl = F.conv_transpose2d(xll, hl.unsqueeze(3), groups=g, stride=(2, 1), padding=(n - 2, 0))
yl += F.conv_transpose2d(xlh, hh.unsqueeze(3), groups=g, stride=(2, 1), padding=(n - 2, 0))
yh = F.conv_transpose2d(xhl, hl.unsqueeze(3), groups=g, stride=(2, 1), padding=(n - 2, 0))
yh += F.conv_transpose2d(xhh, hh.unsqueeze(3), groups=g, stride=(2, 1), padding=(n - 2, 0))
y = F.conv_transpose2d(yl, hl.unsqueeze(2), groups=g, stride=(1, 2), padding=(0, n - 2))
y += F.conv_transpose2d(yh, hh.unsqueeze(2), groups=g, stride=(1, 2), padding=(0, n - 2))
if rescale:
y = y * 2
return y
def _ihaar(self, x: torch.Tensor) -> torch.Tensor:
for _ in self.range:
x = self._idwt(x, rescale=True)
return x
def _iarrange(self, x: torch.Tensor) -> torch.Tensor:
return rearrange(
x,
"b (c p1 p2) h w -> b c (h p1) (w p2)",
p1=self.patch_size,
p2=self.patch_size,
)
class Upsample(nn.Module):
def __init__(self, in_channels: int) -> None:
super().__init__()
self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = x.repeat_interleave(2, dim=2).repeat_interleave(2, dim=3)
return self.conv(x)
class Downsample(nn.Module):
def __init__(self, in_channels: int) -> None:
super().__init__()
self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.conv(F.pad(x, (0, 1, 0, 1), mode="constant", value=0))
class ResnetBlock(nn.Module):
def __init__(self, *, in_channels: int, out_channels: int | None = None, dropout: float, **_: object) -> None:
super().__init__()
out_channels = in_channels if out_channels is None else out_channels
self.norm1 = normalize(in_channels)
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.norm2 = normalize(out_channels)
self.dropout = nn.Dropout(dropout)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.nin_shortcut = (
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
if in_channels != out_channels
else nn.Identity()
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
h = self.conv1(nonlinearity(self.norm1(x)))
h = self.conv2(self.dropout(nonlinearity(self.norm2(h))))
return self.nin_shortcut(x) + h
class AttnBlock(nn.Module):
def __init__(self, in_channels: int) -> None:
super().__init__()
self.norm = normalize(in_channels)
self.q = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0)
self.k = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0)
self.v = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0)
self.proj_out = nn.Conv2d(in_channels, in_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
h = self.norm(x)
q = self.q(h)
k = self.k(h)
v = self.v(h)
b, c, height, width = q.shape
q = q.reshape(b, c, height * width).permute(0, 2, 1)
k = k.reshape(b, c, height * width)
attn = torch.bmm(q, k) * (int(c) ** -0.5)
attn = F.softmax(attn, dim=2)
v = v.reshape(b, c, height * width)
attn = attn.permute(0, 2, 1)
h = torch.bmm(v, attn).reshape(b, c, height, width)
return x + self.proj_out(h)
class Encoder(nn.Module):
def __init__(
self,
in_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int,
**kwargs: object,
) -> None:
super().__init__()
self.num_resolutions = len(channels_mult)
self.num_res_blocks = num_res_blocks
patch_size = int(kwargs.get("patch_size", 1))
self.patcher = Patcher(patch_size, str(kwargs.get("patch_method", "rearrange")))
in_channels = in_channels * patch_size * patch_size
self.num_downsamples = int(math.log2(spatial_compression)) - int(math.log2(patch_size))
assert self.num_downsamples <= self.num_resolutions
self.conv_in = nn.Conv2d(in_channels, channels, kernel_size=3, stride=1, padding=1)
curr_res = resolution // patch_size
in_ch_mult = (1,) + tuple(channels_mult)
self.down = nn.ModuleList()
block_in = channels
for i_level in range(self.num_resolutions):
block = nn.ModuleList()
attn = nn.ModuleList()
block_in = channels * in_ch_mult[i_level]
block_out = channels * channels_mult[i_level]
for _ in range(self.num_res_blocks):
block.append(ResnetBlock(in_channels=block_in, out_channels=block_out, dropout=dropout))
block_in = block_out
if curr_res in attn_resolutions:
attn.append(AttnBlock(block_in))
down = nn.Module()
down.block = block
down.attn = attn
if i_level < self.num_downsamples:
down.downsample = Downsample(block_in)
curr_res //= 2
self.down.append(down)
self.mid = nn.Module()
self.mid.block_1 = ResnetBlock(in_channels=block_in, out_channels=block_in, dropout=dropout)
self.mid.attn_1 = AttnBlock(block_in)
self.mid.block_2 = ResnetBlock(in_channels=block_in, out_channels=block_in, dropout=dropout)
self.norm_out = normalize(block_in)
self.conv_out = nn.Conv2d(block_in, z_channels, kernel_size=3, stride=1, padding=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.patcher(x)
hs = [self.conv_in(x)]
for i_level in range(self.num_resolutions):
for i_block in range(self.num_res_blocks):
h = self.down[i_level].block[i_block](hs[-1])
if len(self.down[i_level].attn) > 0:
h = self.down[i_level].attn[i_block](h)
hs.append(h)
if i_level < self.num_downsamples:
hs.append(self.down[i_level].downsample(hs[-1]))
h = hs[-1]
h = self.mid.block_1(h)
h = self.mid.attn_1(h)
h = self.mid.block_2(h)
h = nonlinearity(self.norm_out(h))
return self.conv_out(h)
class Decoder(nn.Module):
def __init__(
self,
out_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int,
**kwargs: object,
) -> None:
super().__init__()
self.num_resolutions = len(channels_mult)
self.num_res_blocks = num_res_blocks
patch_size = int(kwargs.get("patch_size", 1))
self.unpatcher = UnPatcher(patch_size, str(kwargs.get("patch_method", "rearrange")))
out_ch = out_channels * patch_size * patch_size
self.num_upsamples = int(math.log2(spatial_compression)) - int(math.log2(patch_size))
assert self.num_upsamples <= self.num_resolutions
block_in = channels * channels_mult[self.num_resolutions - 1]
curr_res = (resolution // patch_size) // 2 ** (self.num_resolutions - 1)
self.z_shape = (1, z_channels, curr_res, curr_res)
self.conv_in = nn.Conv2d(z_channels, block_in, kernel_size=3, stride=1, padding=1)
self.mid = nn.Module()
self.mid.block_1 = ResnetBlock(in_channels=block_in, out_channels=block_in, dropout=dropout)
self.mid.attn_1 = AttnBlock(block_in)
self.mid.block_2 = ResnetBlock(in_channels=block_in, out_channels=block_in, dropout=dropout)
self.up = nn.ModuleList()
for i_level in reversed(range(self.num_resolutions)):
block = nn.ModuleList()
attn = nn.ModuleList()
block_out = channels * channels_mult[i_level]
for _ in range(self.num_res_blocks + 1):
block.append(ResnetBlock(in_channels=block_in, out_channels=block_out, dropout=dropout))
block_in = block_out
if curr_res in attn_resolutions:
attn.append(AttnBlock(block_in))
up = nn.Module()
up.block = block
up.attn = attn
if i_level >= (self.num_resolutions - self.num_upsamples):
up.upsample = Upsample(block_in)
curr_res *= 2
self.up.insert(0, up)
self.norm_out = normalize(block_in)
self.conv_out = nn.Conv2d(block_in, out_ch, kernel_size=3, stride=1, padding=1)
def forward(self, z: torch.Tensor) -> torch.Tensor:
h = self.conv_in(z)
h = self.mid.block_1(h)
h = self.mid.attn_1(h)
h = self.mid.block_2(h)
for i_level in reversed(range(self.num_resolutions)):
for i_block in range(self.num_res_blocks + 1):
h = self.up[i_level].block[i_block](h)
if len(self.up[i_level].attn) > 0:
h = self.up[i_level].attn[i_block](h)
if i_level >= (self.num_resolutions - self.num_upsamples):
h = self.up[i_level].upsample(h)
h = self.conv_out(nonlinearity(self.norm_out(h)))
return self.unpatcher(h)
class ContinuousImageTokenizer(nn.Module):
def __init__(self, z_channels: int, z_factor: int, latent_channels: int, **kwargs: object) -> None:
super().__init__()
self.latent_channels = latent_channels
self.encoder = Encoder(z_channels=z_factor * z_channels, **kwargs)
self.decoder = Decoder(z_channels=z_channels, **kwargs)
self.quant_conv = nn.Conv2d(z_factor * z_channels, z_factor * latent_channels, 1)
self.post_quant_conv = nn.Conv2d(latent_channels, z_channels, 1)
self.distribution = IdentityDistribution()
def encoder_module(self) -> nn.Sequential:
return nn.Sequential(
OrderedDict(
[
("encoder", self.encoder),
("quant_conv", self.quant_conv),
("distribution", self.distribution),
]
)
)
def decoder_module(self) -> nn.Sequential:
return nn.Sequential(
OrderedDict(
[
("post_quant_conv", self.post_quant_conv),
("decoder", self.decoder),
]
)
)
def encode(self, x: torch.Tensor) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
h = self.encoder(x)
moments = self.quant_conv(h)
return self.distribution(moments)
def decode(self, z: torch.Tensor) -> torch.Tensor:
return self.decoder(self.post_quant_conv(z))
def build_cosmos_ci_torch_model(spatial_compression: int) -> ContinuousImageTokenizer:
return ContinuousImageTokenizer(
attn_resolutions=[32],
channels=128,
channels_mult=[2, 4, 4],
dropout=0.0,
in_channels=3,
spatial_compression=spatial_compression,
num_res_blocks=2,
out_channels=3,
resolution=1024,
patch_size=4,
patch_method="haar",
latent_channels=16,
z_channels=16,
z_factor=1,
name="CI",
)
@@ -0,0 +1,97 @@
from __future__ import annotations
from pathlib import Path
import torch
from safetensors.torch import load_file
from .cosmos_ci_torch import build_cosmos_ci_torch_model
_SAFETENSORS_EXTENSION = ".safetensors"
def is_safetensors_checkpoint(path: str | Path) -> bool:
return Path(path).suffix.lower() == _SAFETENSORS_EXTENSION
def infer_cosmos_ci_spatial_compression(path: str | Path) -> int:
normalized = str(path).upper()
if "CI8X8" in normalized or "8X8" in normalized:
return 8
if "CI16X16" in normalized or "16X16" in normalized:
return 16
raise ValueError(f"Unable to infer Cosmos CI spatial compression from checkpoint path: {path}")
def _load_safetensors_checkpoint(path: str | Path, device: str) -> dict[str, torch.Tensor]:
return load_file(str(path), device=device)
def _load_native_cosmos_component(
checkpoint_filepath: str | Path,
*,
component: str,
device: str,
) -> torch.nn.Module:
full_model = build_cosmos_ci_torch_model(
spatial_compression=infer_cosmos_ci_spatial_compression(checkpoint_filepath)
)
if component == "encoder":
model = full_model.encoder_module()
elif component == "decoder":
model = full_model.decoder_module()
else:
raise ValueError(f"Unsupported Cosmos component: {component}")
if not is_safetensors_checkpoint(checkpoint_filepath):
raise ValueError(f"Native Cosmos torch backend expects `.safetensors` checkpoints. Got: {checkpoint_filepath}")
state_dict = _load_safetensors_checkpoint(checkpoint_filepath, device)
model.load_state_dict(state_dict, strict=False)
return model.eval().to(device)
def load_cosmos_component(
checkpoint_filepath: str | Path,
*,
component: str,
device: str = "cuda",
) -> torch.nn.Module:
return _load_native_cosmos_component(
checkpoint_filepath,
component=component,
device=device,
)
class ImageTokenizer(torch.nn.Module):
def __init__(
self,
checkpoint_enc: str,
checkpoint_dec: str,
device: str = "cuda",
) -> None:
super().__init__()
self._enc_model = load_cosmos_component(
checkpoint_enc,
component="encoder",
device=device,
)
self._dec_model = load_cosmos_component(
checkpoint_dec,
component="decoder",
device=device,
)
def decode(self, input_latent: torch.Tensor) -> torch.Tensor:
original_dtype = input_latent.dtype
model_dtype = next(self.parameters()).dtype
output_tensor = self._dec_model(input_latent.to(model_dtype))
return output_tensor.to(original_dtype)
def encode(self, input_tensor: torch.Tensor) -> torch.Tensor:
original_dtype = input_tensor.dtype
model_dtype = next(self.parameters()).dtype
output_latent = self._enc_model(input_tensor.to(model_dtype))
if isinstance(output_latent, torch.Tensor):
return output_latent.to(original_dtype)
return output_latent[0].to(original_dtype)
@@ -0,0 +1,962 @@
from __future__ import annotations
import math
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Literal
import torch
import torch._dynamo as dynamo
import torch.nn.functional as F
from einops import rearrange
from safetensors import safe_open
from torch import Tensor, nn
from transformers import Qwen3VLProcessor
from transformers.models.auto import CONFIG_MAPPING
from vllm.model_executor.models.utils import AutoWeightsLoader
from .adapter_qwen3_vl import (
Qwen3VLForConditionalGeneration,
Qwen3VLTextModel,
apply_rotary_pos_emb,
eager_attention_forward,
)
from .config import (
DEFAULT_COSMOS_DIR,
DEFAULT_COSMOS_REPO,
DEFAULT_QWEN3_VL_MODEL,
OBS_IMAGES,
OBS_PREFIX,
OBS_STATE,
OBS_TASK,
OPENPI_ATTENTION_MASK_VALUE,
InternVLAA1Config,
)
from .model_cosmos import ImageTokenizer
def get_safe_dtype(target_dtype: torch.dtype, device_type: str) -> torch.dtype:
if device_type == "mps" and target_dtype == torch.float64:
return torch.float32
if device_type == "cpu" and target_dtype == torch.bfloat16:
return torch.float32
return target_dtype
def create_sinusoidal_pos_embedding(
time: torch.Tensor,
dimension: int,
min_period: float,
max_period: float,
device: torch.device,
) -> Tensor:
if dimension % 2 != 0:
raise ValueError(f"dimension ({dimension}) must be divisible by 2")
dtype = get_safe_dtype(torch.float64, device.type)
fraction = torch.linspace(0.0, 1.0, dimension // 2, dtype=dtype, device=device)
period = min_period * (max_period / min_period) ** fraction
scaling_factor = 1.0 / period * 2 * math.pi
sin_input = scaling_factor[None, :] * time[:, None]
return torch.cat([torch.sin(sin_input), torch.cos(sin_input)], dim=1)
def make_att_2d_masks(pad_masks: torch.Tensor, att_masks: torch.Tensor) -> torch.Tensor:
cumsum = torch.cumsum(att_masks, dim=1)
att_2d_masks = cumsum[:, None, :] <= cumsum[:, :, None]
pad_2d_masks = pad_masks[:, None, :] * pad_masks[:, :, None]
return att_2d_masks & pad_2d_masks
def pad_vector(vector: torch.Tensor, new_dim: int) -> torch.Tensor:
if vector.shape[-1] >= new_dim:
return vector
return F.pad(vector, (0, new_dim - vector.shape[-1]))
def resize_with_pad(images: torch.Tensor, size: tuple[int, int]) -> torch.Tensor:
target_h, target_w = size
if images.ndim != 4:
raise ValueError(f"Expected [T, C, H, W], got {tuple(images.shape)}")
_, _, src_h, src_w = images.shape
scale = min(target_h / src_h, target_w / src_w)
resized_h = max(1, int(round(src_h * scale)))
resized_w = max(1, int(round(src_w * scale)))
resized = F.interpolate(images, size=(resized_h, resized_w), mode="bilinear", align_corners=False)
pad_h = target_h - resized_h
pad_w = target_w - resized_w
pad_top = pad_h // 2
pad_bottom = pad_h - pad_top
pad_left = pad_w // 2
pad_right = pad_w - pad_left
return F.pad(resized, (pad_left, pad_right, pad_top, pad_bottom))
class Qwen3VLInputBuilder:
def __init__(
self,
processor_model_name: str = DEFAULT_QWEN3_VL_MODEL,
max_length: int = 48,
spatial_merge_size: int = 2,
) -> None:
processor_model_name = os.getenv("INTERNVLA_A1_PROCESSOR_DIR", processor_model_name)
local_path = Path(processor_model_name).expanduser()
if local_path.exists():
self.processor = Qwen3VLProcessor.from_pretrained(str(local_path), local_files_only=True)
else:
if os.getenv("INTERNVLA_A1_PROCESSOR_DIR"):
raise FileNotFoundError(f"INTERNVLA_A1_PROCESSOR_DIR points to a missing path: {local_path}")
self.processor = Qwen3VLProcessor.from_pretrained(processor_model_name)
self.max_length = max_length
self.spatial_merge_size = spatial_merge_size
def build(self, camera_images: list[torch.Tensor], task: str) -> dict[str, torch.Tensor]:
input_ids: list[int] = []
attention_mask: list[int] = []
pixel_values: list[torch.Tensor] = []
image_grid_thw: list[torch.Tensor] = []
for image_history in camera_images:
current_image = image_history[1]
image_inputs = self.processor.image_processor(current_image, do_rescale=False)
pixel_values.append(image_inputs.pixel_values)
image_grid_thw.append(image_inputs.image_grid_thw)
num_img_token = int(torch.prod(image_inputs.image_grid_thw[0]).item()) // (self.spatial_merge_size**2)
input_ids.extend(
[self.processor.vision_start_token_id]
+ [self.processor.image_token_id] * num_img_token
+ [self.processor.vision_end_token_id]
)
attention_mask.extend([1] * (num_img_token + 2))
text_inputs = self.processor.tokenizer(
task,
max_length=self.max_length,
padding="max_length",
padding_side="right",
truncation=True,
)
input_ids.extend(text_inputs.input_ids)
attention_mask.extend(text_inputs.attention_mask)
return {
f"{OBS_PREFIX}pixel_values": torch.cat(pixel_values, dim=0),
f"{OBS_PREFIX}image_grid_thw": torch.cat(image_grid_thw, dim=0),
f"{OBS_PREFIX}input_ids": torch.tensor(input_ids, dtype=torch.long),
f"{OBS_PREFIX}attention_mask": torch.tensor(attention_mask, dtype=torch.long),
}
@dataclass
class SuffixStaticContext:
state_emb: torch.Tensor
full_att_2d_masks_4d: torch.Tensor
position_ids: torch.Tensor
def compute_layer_complete(
layer_idx: int,
inputs_embeds: list[torch.Tensor],
attention_mask: torch.Tensor,
position_ids: torch.Tensor,
und_expert: nn.Module,
gen_expert: nn.Module,
act_expert: nn.Module,
) -> list[torch.Tensor]:
models = [und_expert.language_model, gen_expert, act_expert]
query_states = []
key_states = []
value_states = []
for i, hidden_states in enumerate(inputs_embeds):
layer = models[i].layers[layer_idx]
hidden_states = layer.input_layernorm(hidden_states)
input_shape = hidden_states.shape[:-1]
hidden_shape = (*input_shape, -1, layer.self_attn.head_dim)
if layer.self_attn.q_proj.weight.dtype == torch.bfloat16:
hidden_states = hidden_states.to(dtype=torch.bfloat16)
query_state = layer.self_attn.q_norm(layer.self_attn.q_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
key_state = layer.self_attn.k_norm(layer.self_attn.k_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
value_state = layer.self_attn.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
query_states.append(query_state)
key_states.append(key_state)
value_states.append(value_state)
query_states = torch.cat(query_states, dim=2)
key_states = torch.cat(key_states, dim=2)
value_states = torch.cat(value_states, dim=2)
dummy_tensor = torch.zeros(
query_states.shape[0],
query_states.shape[2],
query_states.shape[-1],
device=query_states.device,
dtype=query_states.dtype,
)
cos, sin = und_expert.model.language_model.rotary_emb(dummy_tensor, position_ids)
query_states, key_states = apply_rotary_pos_emb(
query_states,
key_states,
cos,
sin,
unsqueeze_dim=1,
)
scaling = und_expert.language_model.layers[layer_idx].self_attn.scaling
att_output, _ = eager_attention_forward(
und_expert.language_model.layers[layer_idx].self_attn,
query_states,
key_states,
value_states,
attention_mask,
scaling,
)
head_dim = und_expert.language_model.layers[layer_idx].self_attn.head_dim
num_attention_heads = und_expert.language_model.layers[layer_idx].self_attn.config.num_attention_heads
batch_size = query_states.shape[0]
att_output = att_output.reshape(batch_size, -1, num_attention_heads * head_dim)
outputs_embeds = []
start_pos = 0
for i, hidden_states in enumerate(inputs_embeds):
layer = models[i].layers[layer_idx]
end_pos = start_pos + hidden_states.shape[1]
att_chunk = att_output[:, start_pos:end_pos]
if att_chunk.dtype != layer.self_attn.o_proj.weight.dtype:
att_chunk = att_chunk.to(layer.self_attn.o_proj.weight.dtype)
out_emb = layer.self_attn.o_proj(att_chunk)
out_emb = out_emb + hidden_states
residual = out_emb
out_emb = layer.post_attention_layernorm(out_emb)
if layer.mlp.up_proj.weight.dtype == torch.bfloat16:
out_emb = out_emb.to(dtype=torch.bfloat16)
out_emb = layer.mlp(out_emb)
out_emb = out_emb + residual
outputs_embeds.append(out_emb)
start_pos = end_pos
return outputs_embeds
class QwenConfig:
def __init__(
self,
head_dim: int,
hidden_size: int,
intermediate_size: int,
num_attention_heads: int,
num_hidden_layers: int,
num_key_value_heads: int,
) -> None:
self.head_dim = head_dim
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_attention_heads = num_attention_heads
self.num_hidden_layers = num_hidden_layers
self.num_key_value_heads = num_key_value_heads
def get_qwen_config(variant: str) -> QwenConfig:
num_hidden_layers = int(variant.split("_")[-1][:-1])
if variant.startswith("qwen3_vl"):
return QwenConfig(128, 2048, 6144, 16, num_hidden_layers, 8)
if variant.startswith("qwen3"):
return QwenConfig(128, 1024, 3072, 16, num_hidden_layers, 8)
raise ValueError(f"Unknown variant: {variant}")
def resolve_cosmos_checkpoint_paths(
*,
encoder_path: str | Path | None = None,
decoder_path: str | Path | None = None,
) -> tuple[Path, Path]:
encoder_override = encoder_path or os.getenv("INTERNVLA_A1_COSMOS_ENCODER_PATH")
decoder_override = decoder_path or os.getenv("INTERNVLA_A1_COSMOS_DECODER_PATH")
default_encoder_name = "encoder.safetensors"
default_decoder_name = "decoder.safetensors"
checkpoint_enc = (
Path(encoder_override).expanduser()
if encoder_override is not None
else DEFAULT_COSMOS_DIR / default_encoder_name
)
checkpoint_dec = (
Path(decoder_override).expanduser()
if decoder_override is not None
else DEFAULT_COSMOS_DIR / default_decoder_name
)
missing = [str(path) for path in (checkpoint_enc, checkpoint_dec) if not path.exists()]
if missing:
missing_display = ", ".join(missing)
raise FileNotFoundError(
"InternVLA-A1 requires local Cosmos tokenizer checkpoints. "
f"Missing: {missing_display}. "
f"Download {DEFAULT_COSMOS_REPO} and set INTERNVLA_A1_COSMOS_DIR to that directory, "
"or point INTERNVLA_A1_COSMOS_ENCODER_PATH / INTERNVLA_A1_COSMOS_DECODER_PATH "
"to explicit checkpoint files."
)
return checkpoint_enc, checkpoint_dec
class Qwen3VLWithExpertModel(nn.Module):
def __init__(
self,
vlm_config: QwenConfig,
action_expert_config: QwenConfig,
precision: Literal["bfloat16", "float32"] = "bfloat16",
) -> None:
super().__init__()
vlm_config_hf = CONFIG_MAPPING["qwen3_vl"]()
vlm_config_hf.text_config.hidden_size = vlm_config.hidden_size
vlm_config_hf.text_config.intermediate_size = vlm_config.intermediate_size
vlm_config_hf.text_config.num_attention_heads = vlm_config.num_attention_heads
vlm_config_hf.text_config.head_dim = vlm_config.head_dim
vlm_config_hf.text_config.num_hidden_layers = vlm_config.num_hidden_layers
vlm_config_hf.text_config.num_key_value_heads = vlm_config.num_key_value_heads
vlm_config_hf.text_config.max_position_embeddings = 262144
vlm_config_hf.text_config.rope_scaling = {
"mrope_interleaved": True,
"mrope_section": [24, 20, 20],
"rope_type": "default",
}
vlm_config_hf.text_config.tie_word_embeddings = True
vlm_config_hf.tie_word_embeddings = True
vlm_config_hf.vision_config.deepstack_visual_indexes = [5, 11, 17]
vlm_config_hf.vision_config.depth = 24
vlm_config_hf.vision_config.hidden_size = 1024
vlm_config_hf.vision_config.intermediate_size = 4096
vlm_config_hf.vision_config.out_hidden_size = 2048
self.und_expert = Qwen3VLForConditionalGeneration(config=vlm_config_hf)
gen_expert_config_hf = CONFIG_MAPPING["qwen3_vl_text"]()
gen_expert_config_hf.head_dim = action_expert_config.head_dim
gen_expert_config_hf.hidden_size = action_expert_config.hidden_size
gen_expert_config_hf.intermediate_size = action_expert_config.intermediate_size
gen_expert_config_hf.num_attention_heads = action_expert_config.num_attention_heads
gen_expert_config_hf.num_hidden_layers = action_expert_config.num_hidden_layers
gen_expert_config_hf.num_key_value_heads = action_expert_config.num_key_value_heads
gen_expert_config_hf.max_position_embeddings = self.und_expert.config.text_config.max_position_embeddings
gen_expert_config_hf.rope_scaling = self.und_expert.config.text_config.rope_scaling
self.gen_expert = Qwen3VLTextModel(config=gen_expert_config_hf)
self.gen_expert.embed_tokens = None
self.gen_expert.lm_head = None
act_expert_config_hf = CONFIG_MAPPING["qwen3_vl_text"]()
act_expert_config_hf.head_dim = action_expert_config.head_dim
act_expert_config_hf.hidden_size = action_expert_config.hidden_size
act_expert_config_hf.intermediate_size = action_expert_config.intermediate_size
act_expert_config_hf.num_attention_heads = action_expert_config.num_attention_heads
act_expert_config_hf.num_hidden_layers = action_expert_config.num_hidden_layers
act_expert_config_hf.num_key_value_heads = action_expert_config.num_key_value_heads
act_expert_config_hf.max_position_embeddings = self.und_expert.config.text_config.max_position_embeddings
act_expert_config_hf.rope_scaling = self.und_expert.config.text_config.rope_scaling
self.act_expert = Qwen3VLTextModel(config=act_expert_config_hf)
self.act_expert.embed_tokens = None
self.act_expert.lm_head = None
self.to_selected_precision(precision)
def to_selected_precision(self, precision: Literal["bfloat16", "float32"]) -> None:
if precision == "bfloat16":
self.to(dtype=torch.bfloat16)
elif precision == "float32":
# The fp32 path keeps the whole module in full precision, so no
# selective post-conversion fixups are required.
self.to(dtype=torch.float32)
return
else:
raise ValueError(f"Unsupported precision: {precision}")
# Match the original runtime numerics by keeping normalization layers
# in fp32 even when the rest of the module runs in bf16.
keep_fp32 = ["input_layernorm", "post_attention_layernorm", "model.norm"]
for name, param in self.named_parameters():
if any(key in name for key in keep_fp32):
param.data = param.data.to(dtype=torch.float32)
def forward(
self,
attention_mask: torch.Tensor | None,
position_ids: torch.LongTensor | None,
past_key_values: Any,
inputs_embeds: list[torch.Tensor | None],
use_cache: bool,
) -> tuple[list[torch.Tensor | None], Any]:
if inputs_embeds[1] is None and inputs_embeds[2] is None:
prefix_output = self.und_expert.language_model.forward(
inputs_embeds=inputs_embeds[0],
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
)
return [prefix_output.last_hidden_state, None, None], prefix_output.past_key_values
if inputs_embeds[0] is None and inputs_embeds[2] is None:
middle_output = self.gen_expert.forward(
inputs_embeds=inputs_embeds[1],
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
)
return [None, middle_output.last_hidden_state, None], middle_output.past_key_values
if inputs_embeds[0] is None and inputs_embeds[1] is None:
suffix_output = self.act_expert.forward(
inputs_embeds=inputs_embeds[2],
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
use_cache=use_cache,
)
return [None, None, suffix_output.last_hidden_state], None
models = [self.und_expert.language_model, self.gen_expert, self.act_expert]
stacked_inputs = [inputs_embeds[0], inputs_embeds[1], inputs_embeds[2]]
for layer_idx in range(self.und_expert.config.text_config.num_hidden_layers):
stacked_inputs = compute_layer_complete(
layer_idx,
stacked_inputs,
attention_mask,
position_ids,
und_expert=self.und_expert,
gen_expert=self.gen_expert,
act_expert=self.act_expert,
)
outputs = [models[i].norm(stacked_inputs[i]) for i in range(3)]
return outputs, None
class InternVLAA1(nn.Module):
def __init__(
self,
config: InternVLAA1Config,
*,
cosmos_encoder_path: str | Path | None = None,
cosmos_decoder_path: str | Path | None = None,
) -> None:
super().__init__()
self.config = config
vlm_config = get_qwen_config(config.qwen3_vl_variant)
action_expert_config = get_qwen_config(config.action_expert_variant)
self.qwen3_vl_with_expert = Qwen3VLWithExpertModel(vlm_config, action_expert_config, precision=config.dtype)
cosmos_encoder_path, cosmos_decoder_path = resolve_cosmos_checkpoint_paths(
encoder_path=cosmos_encoder_path,
decoder_path=cosmos_decoder_path,
)
self.cosmos = ImageTokenizer(
checkpoint_enc=str(cosmos_encoder_path),
checkpoint_dec=str(cosmos_decoder_path),
device=config.device,
)
hidden_size = action_expert_config.hidden_size
vae_dim = 16
ds = config.scale_factor
self.cosmos_in_proj = nn.Conv2d(vae_dim, hidden_size, kernel_size=1, stride=1, padding=0)
self.downsample_conv = nn.Conv2d(hidden_size, hidden_size, kernel_size=ds, stride=ds, padding=0)
self.upsample_conv = nn.ConvTranspose2d(hidden_size, hidden_size, kernel_size=ds, stride=ds, padding=0)
self.cosmos_out_proj = nn.Linear(hidden_size, vae_dim)
self.cosmos_out_layer_norm = nn.LayerNorm(hidden_size)
self.action_in_proj = nn.Linear(config.max_action_dim, hidden_size)
self.action_out_proj = nn.Linear(hidden_size, config.max_action_dim)
self.state_proj = nn.Linear(config.max_state_dim, hidden_size)
self.action_time_mlp_in = nn.Linear(2 * hidden_size, hidden_size)
self.action_time_mlp_out = nn.Linear(hidden_size, hidden_size)
if config.compile_model:
torch.set_float32_matmul_precision("high")
self.sample_actions = torch.compile(self.sample_actions, mode=config.compile_mode)
self.cosmos.eval()
for param in self.cosmos.parameters():
param.requires_grad = False
self.set_attention_implementation(config.attn_implementation)
def set_attention_implementation(self, attn_implementation: str) -> None:
self.config.attn_implementation = attn_implementation
self.qwen3_vl_with_expert.und_expert.config.text_config._attn_implementation = attn_implementation
self.qwen3_vl_with_expert.und_expert.language_model.config._attn_implementation = attn_implementation
self.qwen3_vl_with_expert.gen_expert.config._attn_implementation = attn_implementation
self.qwen3_vl_with_expert.act_expert.config._attn_implementation = attn_implementation
def _prepare_attention_masks_4d(
self,
att_2d_masks: torch.Tensor,
*,
dtype: torch.dtype,
) -> torch.Tensor:
att_2d_masks_4d = att_2d_masks[:, None, :, :]
bias_dtype = torch.float32 if self.config.attn_implementation == "eager" else dtype
valid_bias = torch.zeros((), dtype=bias_dtype, device=att_2d_masks.device)
invalid_bias = torch.tensor(
OPENPI_ATTENTION_MASK_VALUE,
dtype=bias_dtype,
device=att_2d_masks.device,
)
return torch.where(att_2d_masks_4d, valid_bias, invalid_bias)
def sample_noise(self, shape: tuple[int, ...], device: torch.device) -> torch.Tensor:
return torch.normal(0.0, 1.0, shape, dtype=torch.float32, device=device)
@dynamo.disable
def embed_prefix(
self,
pixel_values: torch.Tensor,
image_grid_thw: torch.Tensor,
lang_tokens: torch.Tensor,
lang_masks: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
image_token_id = self.qwen3_vl_with_expert.und_expert.config.image_token_id
pixel_values = pixel_values.view(-1, pixel_values.shape[-1])
image_grid_thw = image_grid_thw.view(-1, 3)
image_embs, _ = self.qwen3_vl_with_expert.und_expert.visual(pixel_values, image_grid_thw)
embs = self.qwen3_vl_with_expert.und_expert.get_input_embeddings()(lang_tokens)
batch_size, seq_len, hidden_dim = embs.shape
embs = embs.view(-1, hidden_dim)
lang_tokens_flat = lang_tokens.view(-1)
embs[lang_tokens_flat == image_token_id] = image_embs
embs = embs.view(batch_size, seq_len, hidden_dim)
pad_masks = lang_masks.to(torch.bool)
att_masks = torch.zeros_like(pad_masks, dtype=torch.bool, device=pad_masks.device)
return embs, pad_masks, att_masks
def get_cosmos_features(self, images: torch.Tensor) -> torch.Tensor:
shape = images.shape[:-3]
channels, height, width = images.shape[-3:]
images = images.reshape(-1, channels, height, width)
if (height, width) != (256, 256):
images = F.interpolate(images, size=(256, 256), mode="bilinear", align_corners=False)
images = images * 2 - 1
features = self.cosmos.encode(images)
channels, height, width = features.shape[-3:]
return features.view(*shape, channels, height, width)
def embed_middle(
self,
images: torch.Tensor,
img_masks: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
device = images.device
batch_size, n_view, timesteps = images.shape[:3]
features = self.get_cosmos_features(images)
features = rearrange(features, "b n t c h w -> (b n t) c h w")
features = self.cosmos_in_proj(features)
features = self.downsample_conv(features)
features = rearrange(features, "(b n t) c h w -> b n t c h w", b=batch_size, n=n_view, t=timesteps)
_, _, _, _, height, width = features.shape
embs = rearrange(features, "b n t c h w -> b (n t h w) c")
pad_masks = torch.zeros((batch_size, n_view, timesteps, height, width), dtype=torch.bool, device=device)
pad_masks[img_masks] = True
pad_masks = rearrange(pad_masks, "b n t h w -> b (n t h w)")
att_masks = torch.tensor([1] + [0] * (embs.shape[1] - 1), dtype=torch.bool, device=device)
att_masks = att_masks[None, :].expand(batch_size, att_masks.shape[0])
return embs, pad_masks, att_masks
def prepare_suffix_static_context(
self,
state: torch.Tensor,
prefix_pad_masks: torch.Tensor,
max_prefix_position_ids: torch.Tensor,
) -> SuffixStaticContext:
if self.state_proj.weight.dtype == torch.float32:
state = state.to(torch.float32)
state_emb = self.state_proj(state)
device = state_emb.device
batch_size = state_emb.shape[0]
suffix_len = self.config.chunk_size + 1
prefix_len = prefix_pad_masks.shape[1]
suffix_pad_masks = torch.ones(batch_size, suffix_len, dtype=torch.bool, device=device)
suffix_att_masks = torch.tensor([1] + [1] + [0] * (self.config.chunk_size - 1), dtype=torch.bool, device=device)
suffix_att_masks = suffix_att_masks[None, :].expand(batch_size, suffix_len)
prefix_pad_2d_masks = prefix_pad_masks[:, None, :].expand(batch_size, suffix_len, prefix_len)
suffix_att_2d_masks = make_att_2d_masks(suffix_pad_masks, suffix_att_masks)
full_att_2d_masks_4d = self._prepare_attention_masks_4d(
torch.cat([prefix_pad_2d_masks, suffix_att_2d_masks], dim=2),
dtype=state_emb.dtype,
)
position_ids = (
torch.arange(1, suffix_len + 1, device=max_prefix_position_ids.device)
.repeat(3, 1, 1)
.to(max_prefix_position_ids)
+ max_prefix_position_ids
)
return SuffixStaticContext(
state_emb=state_emb,
full_att_2d_masks_4d=full_att_2d_masks_4d,
position_ids=position_ids,
)
def embed_suffix(
self,
state: torch.Tensor,
noisy_actions: torch.Tensor,
timestep: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
if self.state_proj.weight.dtype == torch.float32:
state = state.to(torch.float32)
state_emb = self.state_proj(state)
batch_size = state_emb.shape[0]
device = state_emb.device
state_mask = torch.ones(batch_size, 1, dtype=torch.bool, device=device)
time_emb = create_sinusoidal_pos_embedding(
timestep,
self.action_in_proj.out_features,
min_period=self.config.min_period,
max_period=self.config.max_period,
device=timestep.device,
).to(dtype=timestep.dtype)
action_emb = self.action_in_proj(noisy_actions)
time_emb = time_emb[:, None, :].expand_as(action_emb)
action_time_emb = torch.cat([action_emb, time_emb], dim=2)
action_time_emb = self.action_time_mlp_out(F.silu(self.action_time_mlp_in(action_time_emb)))
action_mask = torch.ones(batch_size, action_time_emb.shape[1], dtype=torch.bool, device=device)
embs = torch.cat([state_emb[:, None, :], action_time_emb], dim=1)
pad_masks = torch.cat([state_mask, action_mask], dim=1)
att_masks = torch.tensor([1] + [1] + [0] * (self.config.chunk_size - 1), dtype=embs.dtype, device=device)
att_masks = att_masks[None, :].expand(batch_size, self.config.chunk_size + 1)
return embs, pad_masks, att_masks
def get_position_ids(
self,
lang_tokens: torch.Tensor,
image_grid_thw: torch.Tensor | None,
pad_masks: torch.Tensor,
) -> tuple[torch.Tensor, Any]:
seq_len = lang_tokens.shape[1]
padded_lang_tokens = torch.ones_like(pad_masks).to(lang_tokens) * 777
padded_lang_tokens[:, :seq_len] = lang_tokens
attention_mask = pad_masks.to(lang_tokens)
if image_grid_thw is not None:
image_grid_thw = image_grid_thw.view(-1, 3)
return self.qwen3_vl_with_expert.und_expert.model.get_rope_index(
padded_lang_tokens,
image_grid_thw,
attention_mask=attention_mask,
)
@torch.no_grad()
def sample_actions(
self,
images: torch.Tensor,
img_masks: torch.Tensor,
pixel_values: torch.Tensor,
image_grid_thw: torch.Tensor,
lang_tokens: torch.Tensor,
lang_masks: torch.Tensor,
state: torch.Tensor,
*,
noise: torch.Tensor | None = None,
num_steps: int | None = None,
decode_image: bool = False,
) -> tuple[torch.Tensor, torch.Tensor | None]:
if num_steps is None:
num_steps = self.config.num_inference_steps
batch_size = state.shape[0]
device = state.device
dtype = state.dtype
if noise is None:
noise = self.sample_noise((batch_size, self.config.chunk_size, self.config.max_action_dim), device)
prefix_embs, prefix_pad_masks, prefix_att_masks = self.embed_prefix(
pixel_values,
image_grid_thw,
lang_tokens,
lang_masks,
)
prefix_att_2d_masks = make_att_2d_masks(prefix_pad_masks, prefix_att_masks)
prefix_position_ids, _ = self.get_position_ids(lang_tokens, image_grid_thw, prefix_pad_masks)
prefix_att_2d_masks_4d = self._prepare_attention_masks_4d(
prefix_att_2d_masks,
dtype=prefix_embs.dtype,
)
_, past_key_values = self.qwen3_vl_with_expert.forward(
attention_mask=prefix_att_2d_masks_4d,
position_ids=prefix_position_ids,
past_key_values=None,
inputs_embeds=[prefix_embs, None, None],
use_cache=True,
)
max_prefix_position_ids = prefix_position_ids.max(dim=-1, keepdim=True).values
middle_embs, middle_pad_masks, middle_att_masks = self.embed_middle(images[:, :, :2], img_masks)
middle_len = middle_pad_masks.shape[1]
prefix_len = prefix_pad_masks.shape[1]
prefix_pad_2d_masks = prefix_pad_masks[:, None, :].expand(batch_size, middle_len, prefix_len)
middle_att_2d_masks = make_att_2d_masks(middle_pad_masks, middle_att_masks)
full_att_2d_masks = torch.cat([prefix_pad_2d_masks, middle_att_2d_masks], dim=2)
middle_position_ids = (
torch.arange(1, middle_len + 1, device=max_prefix_position_ids.device)
.repeat(3, 1, 1)
.to(max_prefix_position_ids)
+ max_prefix_position_ids
)
full_att_2d_masks_4d = self._prepare_attention_masks_4d(
full_att_2d_masks,
dtype=middle_embs.dtype,
)
(_, middle_out, _), past_key_values = self.qwen3_vl_with_expert.forward(
attention_mask=full_att_2d_masks_4d,
position_ids=middle_position_ids,
past_key_values=past_key_values,
inputs_embeds=[None, middle_embs, None],
use_cache=True,
)
max_position_ids = middle_position_ids.max(dim=-1, keepdim=True).values
curr_pad_masks = torch.cat([prefix_pad_masks, middle_pad_masks], dim=1)
dt = torch.tensor(-1.0 / num_steps, dtype=torch.float32, device=device)
x_t = noise
time = torch.tensor(1.0, dtype=torch.float32, device=device)
suffix_static = None
if self.config.enable_suffix_static_context_optimization:
suffix_static = self.prepare_suffix_static_context(state, curr_pad_masks, max_position_ids)
while time >= -dt / 2:
expanded_time = time.expand(batch_size)
if suffix_static is None:
v_t = self.denoise_step(
state,
curr_pad_masks,
past_key_values,
max_position_ids,
x_t.to(dtype),
expanded_time.to(dtype),
)
else:
v_t = self.denoise_step_optimized(
suffix_static, past_key_values, x_t.to(dtype), expanded_time.to(dtype)
)
x_t = x_t + dt * v_t
time += dt
return x_t, None if not decode_image else middle_out
def denoise_step(
self,
state: torch.Tensor,
prefix_pad_masks: torch.Tensor,
past_key_values: Any,
max_prefix_position_ids: torch.Tensor,
x_t: torch.Tensor,
timestep: torch.Tensor,
) -> torch.Tensor:
suffix_embs, suffix_pad_masks, suffix_att_masks = self.embed_suffix(state, x_t, timestep)
suffix_len = suffix_pad_masks.shape[1]
batch_size = prefix_pad_masks.shape[0]
prefix_len = prefix_pad_masks.shape[1]
prefix_pad_2d_masks = prefix_pad_masks[:, None, :].expand(batch_size, suffix_len, prefix_len)
suffix_att_2d_masks = make_att_2d_masks(suffix_pad_masks, suffix_att_masks)
full_att_2d_masks = torch.cat([prefix_pad_2d_masks, suffix_att_2d_masks], dim=2)
position_ids = (
torch.arange(1, suffix_len + 1, device=max_prefix_position_ids.device)
.repeat(3, 1, 1)
.to(max_prefix_position_ids)
+ max_prefix_position_ids
)
full_att_2d_masks_4d = self._prepare_attention_masks_4d(
full_att_2d_masks,
dtype=suffix_embs.dtype,
)
outputs_embeds, _ = self.qwen3_vl_with_expert.forward(
attention_mask=full_att_2d_masks_4d,
position_ids=position_ids,
past_key_values=past_key_values,
inputs_embeds=[None, None, suffix_embs],
use_cache=False,
)
suffix_out = outputs_embeds[2][:, -self.config.chunk_size :].to(dtype=torch.float32)
return self.action_out_proj(suffix_out)
def denoise_step_optimized(
self,
suffix_static: SuffixStaticContext,
past_key_values: Any,
x_t: torch.Tensor,
timestep: torch.Tensor,
) -> torch.Tensor:
state_emb = suffix_static.state_emb
time_emb = create_sinusoidal_pos_embedding(
timestep,
self.action_in_proj.out_features,
min_period=self.config.min_period,
max_period=self.config.max_period,
device=timestep.device,
).to(dtype=timestep.dtype)
action_emb = self.action_in_proj(x_t)
time_emb = time_emb[:, None, :].expand_as(action_emb)
action_time_emb = torch.cat([action_emb, time_emb], dim=2)
action_time_emb = self.action_time_mlp_out(F.silu(self.action_time_mlp_in(action_time_emb)))
suffix_embs = torch.cat([state_emb[:, None, :], action_time_emb], dim=1)
outputs_embeds, _ = self.qwen3_vl_with_expert.forward(
attention_mask=suffix_static.full_att_2d_masks_4d,
position_ids=suffix_static.position_ids,
past_key_values=past_key_values,
inputs_embeds=[None, None, suffix_embs],
use_cache=False,
)
suffix_out = outputs_embeds[2][:, -self.config.chunk_size :].to(dtype=torch.float32)
return self.action_out_proj(suffix_out)
class InternVLAA1Policy(nn.Module):
_AUTO_WEIGHTS_IGNORE_PREFIXES = ["model.cosmos."]
def __init__(
self,
config: InternVLAA1Config,
*,
processor_model_name: str = DEFAULT_QWEN3_VL_MODEL,
) -> None:
super().__init__()
self.config = config
self.input_builder = Qwen3VLInputBuilder(
processor_model_name=processor_model_name,
max_length=config.tokenizer_max_length,
)
cosmos_encoder_path, cosmos_decoder_path = resolve_cosmos_checkpoint_paths()
self.model = InternVLAA1(
config,
cosmos_encoder_path=cosmos_encoder_path,
cosmos_decoder_path=cosmos_decoder_path,
)
self.model.to(config.device)
@classmethod
def from_pretrained(
cls,
checkpoint_dir: str | Path,
*,
config: InternVLAA1Config | None = None,
processor_model_name: str = DEFAULT_QWEN3_VL_MODEL,
strict: bool = False,
) -> InternVLAA1Policy:
checkpoint_dir = Path(checkpoint_dir)
if config is None:
config = InternVLAA1Config.from_pretrained(checkpoint_dir)
instance = cls(config, processor_model_name=processor_model_name)
checkpoint_path = checkpoint_dir / "model.safetensors"
if not checkpoint_path.exists():
raise FileNotFoundError(f"InternVLAA1 checkpoint not found: {checkpoint_path}")
loader = AutoWeightsLoader(
instance,
ignore_unexpected_prefixes=cls._AUTO_WEIGHTS_IGNORE_PREFIXES,
)
with safe_open(str(checkpoint_path), framework="pt", device=str(config.device)) as f:
file_keys = list(f.keys())
loaded = loader.load_weights((name, f.get_tensor(name)) for name in file_keys)
if strict:
expected = {
name
for name, _ in instance.state_dict().items()
if not any(name.startswith(prefix) for prefix in cls._AUTO_WEIGHTS_IGNORE_PREFIXES)
}
missing = sorted(expected - loaded)
if missing:
preview = ", ".join(missing[:10])
suffix = " ..." if len(missing) > 10 else ""
raise RuntimeError(f"Missing weights after AutoWeightsLoader load: {preview}{suffix}")
instance.eval()
return instance
def to(self, *args, **kwargs): # type: ignore[override]
super().to(*args, **kwargs)
self.model.cosmos.to(torch.bfloat16)
self.model.action_out_proj.to(torch.float32)
return self
def _prepare_resized_histories(self, batch: dict[str, Any]) -> list[torch.Tensor]:
resized_images: list[torch.Tensor] = []
for i in range(3):
image_history = batch[f"{OBS_IMAGES}.image{i}"]
if image_history.shape[0] != 1:
raise ValueError(f"InternVLAA1Policy only supports bs=1, got image batch {tuple(image_history.shape)}")
resized_images.append(resize_with_pad(image_history[0], self.config.image_resolution))
return resized_images
def _preprocess_images(
self,
batch: dict[str, torch.Tensor],
*,
resized_histories: list[torch.Tensor] | None = None,
) -> tuple[torch.Tensor, torch.Tensor]:
if resized_histories is None:
resized_histories = self._prepare_resized_histories(batch)
images = torch.stack(resized_histories, dim=0).unsqueeze(0)
img_masks = torch.stack([batch[f"{OBS_IMAGES}.image{i}_mask"] for i in range(3)], dim=1)
return images, img_masks
def prepare_state(self, batch: dict[str, torch.Tensor]) -> torch.Tensor:
return pad_vector(batch[OBS_STATE], self.config.max_state_dim)
def _get_task(self, batch: dict[str, Any]) -> str:
task = batch[OBS_TASK]
if isinstance(task, str):
return task
if isinstance(task, (list, tuple)):
if len(task) != 1:
raise ValueError(f"InternVLAA1Policy only supports bs=1, got {len(task)} tasks")
item = task[0]
if not isinstance(item, str):
raise TypeError(f"Expected task string, got {type(item)!r}")
return item
raise TypeError(f"Unsupported task payload type: {type(task)!r}")
def _prepare_qwen_prefix_inputs(
self,
resized_histories: list[torch.Tensor],
task: str,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
qwen_inputs = self.input_builder.build(resized_histories, task)
device = self.model.action_in_proj.weight.device
return (
qwen_inputs[f"{OBS_PREFIX}pixel_values"]
.unsqueeze(0)
.to(device=device, dtype=self.model.action_in_proj.weight.dtype),
qwen_inputs[f"{OBS_PREFIX}image_grid_thw"].unsqueeze(0).to(device=device),
qwen_inputs[f"{OBS_PREFIX}input_ids"].unsqueeze(0).to(device=device),
qwen_inputs[f"{OBS_PREFIX}attention_mask"].unsqueeze(0).to(device=device),
)
@torch.inference_mode()
def forward(
self,
batch: dict[str, Any],
*,
noise: torch.Tensor | None = None,
decode_image: bool = False,
) -> tuple[torch.Tensor, torch.Tensor | None]:
task = self._get_task(batch)
resized_histories = self._prepare_resized_histories(batch)
pixel_values, image_grid_thw, lang_tokens, lang_masks = self._prepare_qwen_prefix_inputs(
resized_histories,
task,
)
state = self.prepare_state(batch)
images, img_masks = self._preprocess_images(batch, resized_histories=resized_histories)
return self.model.sample_actions(
images,
img_masks,
pixel_values,
image_grid_thw,
lang_tokens,
lang_masks,
state,
noise=noise,
decode_image=decode_image,
)
@@ -0,0 +1,257 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import json
import os
from typing import Any
import torch
from torch import nn
from vllm.logger import init_logger
from vllm_omni.diffusion.compile import regionally_compile
from vllm_omni.diffusion.data import DiffusionOutput, OmniDiffusionConfig
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import (
DiffusionPipelineProfilerMixin,
wrap_methods_by_paths,
)
from vllm_omni.diffusion.request import OmniDiffusionRequest
from .config import (
DEFAULT_QWEN3_VL_MODEL,
OBS_IMAGES,
OBS_STATE,
OBS_TASK,
InternVLAA1Config,
)
from .model_internvla_a1 import InternVLAA1Policy, resolve_cosmos_checkpoint_paths
logger = init_logger(__name__)
def get_internvla_a1_post_process_func(od_config: OmniDiffusionConfig):
del od_config
def post_process_func(x):
return x
return post_process_func
class InternVLAA1Pipeline(nn.Module, DiffusionPipelineProfilerMixin):
"""InternVLA-A1 pipeline wrapper for the policy implementation."""
def __init__(self, *, od_config: OmniDiffusionConfig, prefix: str = ""):
super().__init__()
self.od_config = od_config
self.prefix = prefix
self.model_dir = od_config.model
self.config = self._build_config(od_config)
custom_args = od_config.custom_pipeline_args or {}
self.strict_load = bool(custom_args.get("strict_load", False))
self.processor_model_name = str(custom_args.get("processor_model_name", DEFAULT_QWEN3_VL_MODEL))
enable_warmup = custom_args.get("enable_warmup")
self.enable_warmup = bool(enable_warmup) if isinstance(enable_warmup, bool) else False
self.setup_diffusion_pipeline_profiler(
profiler_targets=["forward"],
enable_diffusion_pipeline_profiler=self.od_config.enable_diffusion_pipeline_profiler,
)
self.policy = self._initialize_policy()
self._setup_policy_profiler_targets()
if self.enable_warmup:
self._warmup()
def _build_config(self, od_config: OmniDiffusionConfig) -> InternVLAA1Config:
config_dict = self._load_config_dict(od_config)
config = InternVLAA1Config.from_model_config(config_dict)
custom_args = od_config.custom_pipeline_args or {}
device = custom_args.get("device")
if isinstance(device, str):
config.device = device
dtype = custom_args.get("dtype")
if isinstance(dtype, str):
config.dtype = dtype
elif od_config.dtype is not None:
config.dtype = str(od_config.dtype).split(".")[-1]
compile_model = custom_args.get("compile_model")
if isinstance(compile_model, bool):
config.compile_model = compile_model
attn_implementation = custom_args.get("attn_implementation")
if isinstance(attn_implementation, str):
config.attn_implementation = attn_implementation
enable_regional_compile = custom_args.get("enable_regional_compile")
if isinstance(enable_regional_compile, bool):
config.enable_regional_compile = enable_regional_compile
regional_compile_dynamic = custom_args.get("regional_compile_dynamic")
if isinstance(regional_compile_dynamic, bool):
config.regional_compile_dynamic = regional_compile_dynamic
return config
def _load_config_dict(self, od_config: OmniDiffusionConfig) -> dict[str, Any]:
if od_config.model_config:
return dict(od_config.model_config)
model_path = od_config.model
if not model_path:
return {}
config_path = os.path.join(model_path, "config.json")
if not os.path.exists(config_path):
logger.info("InternVLAA1Pipeline config.json not found under %s; using defaults.", model_path)
return {}
with open(config_path, encoding="utf-8") as f:
return json.load(f)
def has_real_checkpoint(self) -> bool:
return bool(self.model_dir) and os.path.exists(os.path.join(self.model_dir, "model.safetensors"))
def runtime_mode(self) -> str:
return "real_checkpoint_loaded" if self.has_real_checkpoint() else "no_checkpoint_policy"
def _setup_policy_profiler_targets(self) -> None:
if not self.od_config.enable_diffusion_pipeline_profiler:
return
wrap_methods_by_paths(
self,
[
"policy.model.sample_actions",
"policy.model.embed_prefix",
"policy.model.embed_middle",
"policy.model.denoise_step",
],
)
def _apply_policy_optimizations(self, policy: InternVLAA1Policy) -> None:
policy.model.set_attention_implementation(self.config.attn_implementation)
if not self.config.enable_regional_compile:
return
compile_targets = [
"qwen3_vl_with_expert.und_expert.visual",
"qwen3_vl_with_expert.und_expert.language_model",
"qwen3_vl_with_expert.gen_expert",
"qwen3_vl_with_expert.act_expert",
]
for path in compile_targets:
current = policy.model
for part in path.split("."):
current = getattr(current, part, None)
if current is None:
break
if current is None:
continue
try:
regionally_compile(current, dynamic=self.config.regional_compile_dynamic)
logger.info("InternVLAA1Pipeline regional compile applied to %s", path)
except Exception as exc:
logger.warning("InternVLAA1Pipeline regional compile failed for %s: %s", path, exc)
def _initialize_policy(self) -> InternVLAA1Policy:
resolve_cosmos_checkpoint_paths()
if self.has_real_checkpoint():
logger.info("Loading InternVLA-A1 weights from %s", self.model_dir)
policy = InternVLAA1Policy.from_pretrained(
self.model_dir,
config=self.config,
processor_model_name=self.processor_model_name,
strict=self.strict_load,
)
else:
logger.info("Initializing InternVLA-A1 policy without checkpoint weights.")
policy = InternVLAA1Policy(self.config, processor_model_name=self.processor_model_name)
policy.to(self.config.device)
policy.to(getattr(torch, self.config.dtype))
policy.eval()
self._apply_policy_optimizations(policy)
return policy
def _build_fake_batch_inputs(self) -> dict[str, torch.Tensor]:
device = torch.device(self.config.device)
dtype = getattr(torch, self.config.dtype)
history = 2
channels = 3
image_size = self.config.image_resolution
fake_image = torch.zeros((1, history, channels, image_size[0], image_size[1]), device=device, dtype=dtype)
fake_mask = torch.ones((1,), device=device, dtype=torch.bool)
return {
OBS_STATE: torch.zeros((1, self.config.max_state_dim), device=device, dtype=dtype),
OBS_TASK: [""],
f"{OBS_IMAGES}.image0": fake_image.clone(),
f"{OBS_IMAGES}.image1": fake_image.clone(),
f"{OBS_IMAGES}.image2": fake_image.clone(),
f"{OBS_IMAGES}.image0_mask": fake_mask.clone(),
f"{OBS_IMAGES}.image1_mask": fake_mask.clone(),
f"{OBS_IMAGES}.image2_mask": fake_mask.clone(),
}
def _warmup(self) -> None:
logger.info("InternVLAA1Pipeline warmup started")
try:
batch_inputs = self._build_fake_batch_inputs()
noise = torch.zeros(
(1, self.config.chunk_size, self.config.max_action_dim),
device=self.config.device,
dtype=torch.float32,
)
with torch.inference_mode():
self.policy.forward(batch_inputs, noise=noise, decode_image=False)
except Exception as exc:
logger.warning("InternVLAA1Pipeline warmup failed: %s", exc)
return
logger.info("InternVLAA1Pipeline warmup finished")
def _predict_actions(
self,
batch_inputs: dict[str, Any],
*,
noise: torch.Tensor | None = None,
decode_image: bool = False,
) -> tuple[torch.Tensor, torch.Tensor | None]:
logger.debug("InternVLAA1Pipeline forward mode=%s", self.runtime_mode())
return self.policy.forward(
batch_inputs,
noise=noise,
decode_image=decode_image,
)
@torch.inference_mode()
def forward(self, req: OmniDiffusionRequest) -> DiffusionOutput:
if len(req.prompts) > 1:
logger.warning("InternVLAA1Pipeline only supports a single prompt/request; taking the first sample.")
extra_args = getattr(req.sampling_params, "extra_args", {}) or {}
batch_inputs = extra_args.get("batch_inputs")
if batch_inputs is None:
return DiffusionOutput(
error=(
"InternVLAA1Pipeline.forward expects sampling_params.extra_args['batch_inputs'] "
"with pre-built repo-side inputs."
),
post_process_func=get_internvla_a1_post_process_func(self.od_config),
)
output, decoded = self._predict_actions(
batch_inputs,
noise=extra_args.get("noise"),
decode_image=bool(extra_args.get("decode_image", False)),
)
custom_output: dict[str, Any] = {}
if decoded is not None:
custom_output["decoded"] = decoded
return DiffusionOutput(
output=output,
custom_output=custom_output,
post_process_func=get_internvla_a1_post_process_func(self.od_config),
)