magi_human ¶
daVinci-MagiHuman DiT (base variant).
Ported from https://github.com/GAIR-NLP/daVinci-MagiHuman (inference/model/dit/dit_module.py, ~950 lines in the reference).
Architecture summary (verified against GAIR/daVinci-MagiHuman/base/ weights):
- 40 transformer layers, hidden 5120, head_dim 128. - GQA with 40 query heads and 8 KV heads. - Multi-modality "sandwich": layers 0..3 and 36..39 use 3-way modality experts (video/audio/text) packed inside each linear as weight[..., out * 3, in]. Middle layers share a single expert. - Per-head attention gating: the QKV projection emits an extra num_heads_q channels that are sigmoid-gated onto the attention output. - Activation is GELU7 on layers 0..3 (non-gated, intermediate=4hidden) and SwiGLU7 elsewhere (gated, intermediate=int(hidden4⅔)//44). - Position encoding is an element-wise Fourier embedding over 9-column coords (t,h,w + original TxHxW + reference TxHxW), not a standard 1D/3D RoPE. - Forward takes a flat concatenated token stream (video first, then audio, then text) plus a modality map; the internal ModalityDispatcher permutes by modality before each linear so per-expert chunks line up.
Deviations from the "use fastvideo.layers primitives everywhere" guideline in the add-model skill:
- The packed-expert linears store weight as [out * num_experts, in]. FastVideo's ReplicatedLinear does not model this layout; we use raw nn.Parameter with a small wrapper below. This is deliberate and scoped to this DiT: ReplicatedLinear still handles the adapter.* embedders and final_linear_{video,audio} (single-expert) here. - Self-attention is full-sequence and crosses modalities inside the flat concat stream; DistributedAttention assumes a clean spatial-sequence layout, so for the first port we use torch SDPA. Multi-GPU sequence parallelism is a follow-up. - torch.compile via magi_compiler is replaced with a plain nn.Module.
For the full history and shape-by-shape verification notes, see .claude/skills/add-model/SKILL.md and the scaffold PR description.
Classes¶
fastvideo.models.dits.magi_human.ElementWiseFourierEmbed ¶
Bases: Module
Element-wise Fourier embedding over 9-column coords (t, h, w, T, H, W, ref_T, ref_H, ref_W). Produces a per-token positional embedding that acts as the RoPE angle input for attention.
Weight: bands of shape [dim // 8] (fixed at init via freq_bands).
Source code in fastvideo/models/dits/magi_human.py
fastvideo.models.dits.magi_human.MagiAttention ¶
Bases: Module
Self-attention with GQA + optional per-head sigmoid gating.
Source code in fastvideo/models/dits/magi_human.py
fastvideo.models.dits.magi_human.MagiHumanDiT ¶
MagiHumanDiT(config: MagiHumanVideoConfig, hf_config: dict | None = None, **kwargs)
Bases: BaseDiT
Top-level DiT for daVinci-MagiHuman (base).
Forward signature mirrors the reference DiTModel.forward: it takes a flat token stream, its per-token coords and modality mapping, and returns per-modality outputs packed into a max-channel-width tensor.
This scaffold is single-GPU only; the ulysses_scheduler().dispatch(...) sequence-parallel wrapping in the reference has no equivalent here yet.
Source code in fastvideo/models/dits/magi_human.py
Methods:¶
fastvideo.models.dits.magi_human.MagiHumanDiT.forward ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | [L, max(V_ch, A_ch, T_ch)] | required |
coords_mapping | Tensor | [L, 9] | required |
modality_mapping | Tensor | [L] (int in {VIDEO, AUDIO, TEXT}) | required |
Returns: out: [L, max(V_ch, A_ch)] with video channels in video slots and audio channels in audio slots; text slots are zero.
Source code in fastvideo/models/dits/magi_human.py
fastvideo.models.dits.magi_human.ModalityDispatcher ¶
ModalityDispatcher(modality_mapping: Tensor, num_modalities: int)
Permute a flat token stream so same-modality tokens are contiguous.
The DiT's multi-expert linears apply a different weight chunk per modality. Instead of carrying a branch inside each Linear, we pre-permute tokens so each chunk sees a contiguous slice, then un-permute before computing RoPE/attention across the full sequence.
Source code in fastvideo/models/dits/magi_human.py
fastvideo.models.dits.magi_human.MultiModalityRMSNorm ¶
Bases: Module
RMSNorm with optional per-modality scale.
When num_modality == 1, behaves identically to a standard RMSNorm with weight initialized to zero (effective weight is 1 + weight, hence the learnable +1 offset baked into the forward path). When num_modality > 1, the weight tensor packs per-modality scales along its flat axis and the dispatcher selects the right chunk per modality.
Source code in fastvideo/models/dits/magi_human.py
fastvideo.models.dits.magi_human.PackedExpertLinear ¶
PackedExpertLinear(in_features: int, out_features: int, num_experts: int = 1, bias: bool = False, dtype: dtype = bfloat16)
Bases: Module
Linear where the weight is packed per-modality along the output axis.
Shapes
weight: [out_features * num_experts, in_features] bias: [out_features * num_experts] (optional)
When num_experts == 1, behaves exactly like nn.Linear. When num_experts > 1, forward dispatches the input via the supplied ModalityDispatcher, applies the per-modality weight/bias chunk, and gathers the outputs in original order.
Why not use ReplicatedLinear? Because the packed-expert layout is not what ReplicatedLinear (or any other fastvideo.layers.linear) is wired for. Using raw nn.Parameter keeps weight loading trivial (names map 1:1 to the upstream checkpoint) and avoids quantization-path assumptions that don't match this layout.
Source code in fastvideo/models/dits/magi_human.py
Functions:¶
fastvideo.models.dits.magi_human.swiglu7 ¶
Gated swish-GLU with OpenAI-OSS-style limits and +1 linear bias.