vllm_omni.diffusion.models.magi2.modeling_magi2 ¶
Native MAGI-2 Preview diffusion transformer.
The architecture and mHC/MoE equations are adapted from SandAI's Apache-2.0 MAGI-2 Preview implementation. This modified vLLM-Omni version removes the external inference package, MagiCompiler, and SandAI process manager while preserving the released checkpoint module names and tensor layouts.
Magi2Attention ¶
Bases: Module
k_norm instance-attribute ¶
k_norm = MultiModalityRMSNorm(
self.head_dim,
num_modality=num_modality,
out_dtype=torch.float32,
)
linear_g instance-attribute ¶
linear_g = make_grouped_linear(
config.hidden_size,
config.num_heads_q,
num_experts=num_modality,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
)
linear_proj instance-attribute ¶
linear_proj = make_grouped_linear(
global_q_size,
config.hidden_size,
num_experts=num_modality,
bias=False,
dtype=config.params_dtype,
parallel_mode="row",
)
linear_qkv instance-attribute ¶
linear_qkv = make_grouped_linear(
config.hidden_size,
global_q_size + 2 * global_kv_size,
num_experts=num_modality,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
qkv_splits=(
global_q_size,
global_kv_size,
global_kv_size,
),
)
num_heads_kv instance-attribute ¶
packed_attention instance-attribute ¶
packed_attention = Attention(
num_heads=self.num_heads_q,
num_kv_heads=self.num_heads_kv,
head_size=self.head_dim,
causal=False,
softmax_scale=self.head_dim**-0.5,
qkv_layout="THD",
skip_sequence_parallel=True,
disable_kv_quant=True,
custom_attention=Magi2PackedAttentionKernel(
config.attention_softcap
),
)
pre_norm instance-attribute ¶
pre_norm = MultiModalityRMSNorm(
config.hidden_size, num_modality=num_modality
)
q_norm instance-attribute ¶
q_norm = MultiModalityRMSNorm(
self.head_dim,
num_modality=num_modality,
out_dtype=torch.float32,
)
sinks instance-attribute ¶
sinks = nn.Parameter(
torch.empty(
config.attention_sink_tokens,
self.num_heads_q,
dtype=torch.float32,
)
)
attend ¶
attend(
q: Tensor,
k: Tensor,
v: Tensor,
varlen_handler: VarlenHandler,
cp_split_sizes: list[int] | Tensor,
) -> Tensor
output ¶
output(
attention: Tensor,
gates: Tensor,
modality_dispatcher: ModalityDispatcher,
) -> Tensor
project ¶
project(
hidden_states: Tensor,
rope: Tensor,
modality_dispatcher: ModalityDispatcher,
) -> tuple[Tensor, Tensor, Tensor, Tensor]
Magi2MLP ¶
Bases: Module
down_proj instance-attribute ¶
down_proj = make_grouped_linear(
intermediate_size,
config.hidden_size,
num_experts=num_modality,
bias=False,
dtype=config.params_dtype,
parallel_mode="row",
)
pre_norm instance-attribute ¶
pre_norm = MultiModalityRMSNorm(
config.hidden_size, num_modality=num_modality
)
up_gate_proj instance-attribute ¶
up_gate_proj = make_grouped_linear(
config.hidden_size,
2 * intermediate_size,
num_experts=num_modality,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
)
Magi2MultiHeadMoELayer ¶
Bases: Module
local_modality_shared_expert_intermediate_size instance-attribute ¶
local_modality_shared_expert_intermediate_size = (
moe.modality_shared_expert_intermediate_size // tp_size
)
local_shared_expert_intermediate_size instance-attribute ¶
merge_linear instance-attribute ¶
merge_linear = make_grouped_linear(
config.hidden_size,
config.hidden_size,
bias=False,
dtype=config.params_dtype,
parallel_mode="row",
)
modality_specific_shared_expert_fc1 instance-attribute ¶
modality_specific_shared_expert_fc1 = make_grouped_linear(
config.hidden_size,
2 * moe.modality_shared_expert_intermediate_size,
num_experts=3,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
)
modality_specific_shared_expert_fc2 instance-attribute ¶
modality_specific_shared_expert_fc2 = make_grouped_linear(
moe.modality_shared_expert_intermediate_size,
config.hidden_size,
num_experts=3,
bias=False,
dtype=config.params_dtype,
parallel_mode="row",
)
moe_mlp instance-attribute ¶
moe_mlp = Magi2MultiHeadMoE(
Magi2MultiHeadMoEConfig(
hidden_size=config.hidden_size,
num_heads=moe.num_heads,
num_experts=moe.num_experts,
top_k=moe.top_k,
expert_intermediate_size=moe.expert_intermediate_size,
params_dtype=config.params_dtype,
score_func=moe.score_function,
route_norm=moe.normalize_routing_weights,
route_scale=moe.routing_scale,
)
)
shared_expert_fc1 instance-attribute ¶
shared_expert_fc1 = make_grouped_linear(
config.hidden_size,
2 * moe.shared_expert_intermediate_size,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
)
shared_expert_fc2 instance-attribute ¶
shared_expert_fc2 = make_grouped_linear(
moe.shared_expert_intermediate_size,
config.hidden_size,
bias=False,
dtype=config.params_dtype,
parallel_mode="row",
)
split_linear instance-attribute ¶
split_linear = make_grouped_linear(
config.hidden_size,
config.hidden_size,
bias=False,
dtype=config.params_dtype,
parallel_mode="column",
)
route_input ¶
route_input(
hidden_states: Tensor, dispatcher: ModalityDispatcher
) -> tuple[Tensor, Tensor]
Magi2PostAdapter ¶
Bases: Module
final_linear_audio instance-attribute ¶
final_linear_audio = nn.Linear(
self.adapter_dim,
config.audio_in_channels,
bias=False,
dtype=torch.float32,
)
final_linear_video instance-attribute ¶
final_linear_video = nn.Linear(
self.adapter_dim,
config.video_in_channels,
bias=False,
dtype=torch.float32,
)
Magi2PreAdapter ¶
Bases: Module
audio_embedder instance-attribute ¶
audio_embedder = nn.Linear(
config.audio_in_channels,
self.adapter_dim,
bias=True,
dtype=torch.float32,
)
text_embedder instance-attribute ¶
text_embedder = nn.Linear(
config.text_in_channels,
self.adapter_dim,
bias=True,
dtype=torch.float32,
)
video_embedder instance-attribute ¶
video_embedder = nn.Linear(
config.video_in_channels,
self.adapter_dim,
bias=True,
dtype=torch.float32,
)
forward ¶
forward(
packed: Tensor,
video_indices: Tensor,
audio_indices: Tensor,
text_indices: Tensor,
) -> Tensor
Magi2PreviewTransformer ¶
Bases: Module
Native preview DiT with the released checkpoint hierarchy.
compile_regions ¶
compile_regions(**compile_kwargs: Any) -> None
Compile the dense compute between the eager attention and MoE kernels.
forward ¶
forward(
x: Tensor,
coords_mapping: Tensor,
modality_mapping: Tensor,
varlen_handler: VarlenHandler,
time_token_sequence: Tensor | None = None,
) -> Tensor
load_weights ¶
Strictly load and TP/MoE-slice the released Preview checkpoint.
This is the ordinary loader used by resident and rank-local DLO deployments. DLO+AllGather uses the pipeline's mmap mapping and the same per-parameter transforms before orthogonal DP sharding.
Magi2TransformerBlock ¶
Bases: Module
layers instance-attribute ¶
layers = nn.ModuleList(
Magi2TransformerLayer(config, index)
for index in range(config.num_layers)
)
forward ¶
forward(
hidden_states: Tensor,
rope: Tensor,
varlen_handler: VarlenHandler,
modality_dispatcher: ModalityDispatcher,
cp_split_sizes: list[int] | Tensor,
) -> Tensor
Magi2TransformerLayer ¶
Bases: Module
region_methods instance-attribute ¶
forward ¶
forward(
hidden_states: Tensor,
rope: Tensor,
varlen_handler: VarlenHandler,
modality_dispatcher: ModalityDispatcher,
cp_split_sizes: list[int] | Tensor,
) -> Tensor
Modality ¶
VarlenHandler dataclass ¶
Packed-sequence metadata consumed by MAGI-2 attention.