mstar.model.wan22.components#
Wan2.2-TI2V-5B component modules.
Native components: the video DiT (dit.py — patchify, 3D-RoPE attention
blocks, adaLN head, loaded through wan22.weight_loader) and the inline
UniPC solver (unipc.py). The UMT5 text encoder and the Wan2.2-VAE stay
thin diffusers wrappers, constructed in Wan22Model.get_submodule.
- class mstar.model.wan22.components.UniPCState(model_outputs, last_sample)[source]#
Bases:
objectLoop-carried UniPC solver state for one request.
model_outputsis the order-2 ring buffer of converted outputs (slot 1 is the previous step, slot 0 the one before).last_sampleis the sample the previous predictor was given, consumed by this step’s corrector. Both are zero before they are written, and the order ramp guarantees they are not read then.
- class mstar.model.wan22.components.Wan22DiT(config)[source]#
Bases:
ModuleThe dense 5B TI2V video DiT: Conv3d patchify -> 30 blocks -> adaLN output head + unpatchify. Built on the meta device and materialized by
weight_loader.build_wan22_dit(meta ->cast_serving_dtypes->to_empty(device)-> checkpoint load).- Parameters:
config (Wan22Config)
- cast_serving_dtypes()[source]#
bf16 everywhere except the fp32 islands (the checkpoint’s
_keep_in_fp32_modules: time_embedder, scale_shift_tables, block norm2 affines). Called on the meta module BEFOREto_emptyso storage is allocated directly in the serving dtypes.- Return type:
- property dtype: dtype#
Bulk compute dtype (the non-island weights); callers cast inputs to this, mirroring diffusers
ModelMixin.dtype.
- forward(hidden_states, timestep, encoder_hidden_states)[source]#
hidden_states[B, C, F, H, W](bf16 latents),timestep[B, post-patch seq]per-token grid (expand_timesteps— the only timestep form mstar’s dit submodule produces), text embeds[B, 512, text_dim]bf16. Returns[B, C_out, F, H, W]bf16.
- class mstar.model.wan22.components.Wan22DiTAttention(dim, num_heads, eps)[source]#
Bases:
ModuleMulti-head attention with across-heads RMSNorm on q/k (the checkpoint’s
rms_norm_across_heads: one 3072-wide norm before the head split, not per-head) and SDPA. Self-attention applies 3D RoPE; cross-attention reads k/v from the 512-token text stream and skips RoPE. All four projections carry bias (referenceWanAttention).- forward(hidden_states, encoder_hidden_states=None, rotary_emb=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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.wan22.components.Wan22DiTBlock(dim, ffn_dim, num_heads, eps)[source]#
Bases:
ModuleOne transformer block: adaLN-modulated self-attention, cross-attention to the text stream, adaLN-modulated FFN (reference
WanTransformerBlock, per-tokentemb.ndim == 4branch).The modulation runs fp32 (the scale-shift table is an fp32 island), the self-attention and FFN residual adds upcast to fp32, and the cross-attention residual stays bf16. All three follow the reference exactly.
- forward(hidden_states, encoder_hidden_states, timestep_proj, rotary_emb)[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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.wan22.components.Wan22RoPE3D(attention_head_dim, max_seq_len)[source]#
Bases:
object3D-factorized rotary tables for the post-patchify (t, h, w) grid.
The head dim splits into three axis bands —
h = w = 2 * (head_dim // 6),ttakes the remainder (44/42/42 for head_dim 128) — each with its own 1D table overmax_seq_lenpositions, computed in float64 and stored fp32 concatenated to[max_seq_len, head_dim](referenceWanRotaryPosEmbed.__init__). The CPU tables are built at init (bit-identical regardless of the eventual device) and copied to each device on first use there.Deliberately not an
nn.Module: the tables are derived state that must never ride throughto_empty/state_dict, and keeping them out of the module tree means no buffer for the loader or.to()to corrupt.
- class mstar.model.wan22.components.Wan22TimeTextEmbedding(dim, freq_dim, text_dim)[source]#
Bases:
ModuleCondition embedder: per-token timestep embedding + text projection (reference
WanTimeTextImageEmbedding, image branch absent on TI2V-5B).time_embedderis an fp32 island: the sinusoidal embedding and both its Linears run fp32;tembcrosses to bf16 attype_as(text), so the downstreamtime_projmodulation projection is a bf16 matmul.
- mstar.model.wan22.components.make_unipc_tables(num_inference_steps, flow_shift)[source]#
Per-request sigma and timestep tables.
Returns sigmas float32
[N+1]with the terminal zero appended, and timesteps int64[N]. Sigmas are computed float64 and cast at the end, and the timesteps are truncated by the int64 cast — both as the reference does, and both load-bearing for bit-exactness.
- mstar.model.wan22.components.unipc_convert_model_output(model_output, sample, sigmas, step_index)[source]#
Flow-prediction x0 conversion.
sigma_tstays a CPU 0-dim tensor on purpose, as the reference leaves it. A CPU scalar takes the CUDA kernel’s full-precision Scalar path; a device 0-dim tensor would instead be type-promoted to the bf16 operand’s dtype before the multiply. That is a bitwise difference, and it breaks lockstep with the reference. (It is also why this node cannot be compiled — seeWan22DitSubmodule.)
- mstar.model.wan22.components.unipc_corrector_step(state, this_model_output, this_sample, sigmas, step_index, order)[source]#
UniC correction of x_k using step k’s converted output (source
multistep_uni_c_bh_update). Runs before the ring shift, sostate.model_outputs[1]is step k-1’s output and[0]step k-2’s;state.last_sampleis the sample step k-1’s predictor started from.
- mstar.model.wan22.components.unipc_effective_order(step_index, num_inference_steps)[source]#
Predictor order at step k.
Capped both by the steps remaining (so the last step is order 1) and by the warmup ramp. Step k’s corrector reuses step k-1’s value.
- mstar.model.wan22.components.unipc_predictor_step(state, sample, sigmas, step_index, order)[source]#
UniP update x_k -> x_{k+1}.
state.model_outputs[1]must already hold step k’s converted output: the ring buffer is shifted before the predictor runs.
Modules