wan2.1-t2v-1.3b-phi-lattice-v2
4-bit phi-lattice v2 build of the 3D Video Diffusion Transformer (WanTransformer3DModel, 1,418,996,800 parameters across 825 tensors) from Wan-AI/Wan2.1-T2V-1.3B-Diffusers.
1. Repository Layout & Memory Footprint
wan2.1-t2v-1.3b-phi-lattice-v2/
├── transformer/
│ ├── config.json
│ ├── wan_transformer_phi_v2_packed_4bit.pt # 757.10 MiB (793.88 MB) 4-bit packed archive
│ └── diffusion_pytorch_model.safetensors # 2,706.60 MiB (2.84 GB) unrolled BF16 fallback
├── phi_loader.py # Standalone loader (triton_4bit, low_vram_4bit, unrolled_bf16)
├── phi_lut_gemm.py # Fused Triton 4-bit LUT-GEMM & dequantization kernels
├── tests/
│ ├── test_archive_roundtrip.py # CPU audit: structure, 166,624,550 zeros, 53.79 dB round-trip
│ ├── test_sqnr_vs_upstream.py # CPU audit: 19.65 dB SQNR vs upstream FP32 shards
│ └── selftest_blackwell_300layers.log # Canonical 300/300 layer GPU verification log (Blackwell)
├── assets/ # 65-frame (16 fps) side-by-side comparison videos
└── benchmark_results.json # Quantitative evaluation metrics
| Checkpoint Variant | Disk / Download Size | GPU VRAM Allocated (WanTransformer3DModel) | Savings vs Upstream FP32 |
|---|---|---|---|
Upstream Wan2.1-T2V-1.3B-Diffusers (FP32) | 5,676.0 MB (5,413.0 MiB) | 5,416.0 MiB | 1.00x |
transformer/diffusion_pytorch_model.safetensors (BF16 fallback) | 2,838.1 MB (2,706.6 MiB) | 2,708.0 MiB | 2.00x disk & VRAM |
transformer/wan_transformer_phi_v2_packed_4bit.pt (low_vram_4bit / triton_4bit) | 793.9 MB (757.1 MiB) | 761.7 MiB | 7.15x disk / 3.56x VRAM vs BF16 |
2. Visual Comparisons (Baseline BF16 | Uniform 4-bit | phi-lattice v2)
All three columns are rendered at 832x480, 65 frames (4.06s @ 16 fps), 25 inference steps, guidance_scale=5.0 using identical random seeds.
Scene 1: Tokyo Neon Street Walk (seed=77)
Scene 2: Himalayan Snow Leopard (seed=202)
Scene 3: Coral Reef Sea Turtle (seed=309)
3. Quantitative Video Benchmark
| Method | 4-bit Weight SQNR | Exact Sparsity (Zero Weights) | Video PSNR | Video SSIM | Pixel MAE | Temporal Consistency |
|---|---|---|---|---|---|---|
Baseline (BF16) | Reference | 0.00% (0) | Reference | 1.0000 | 0.00 | 1.0000 |
Standard Uniform (4-bit) | 19.19 dB | 0.00% (0) | 13.30 dB | 0.6827 | 31.09 | 0.0682 |
phi-lattice v2 (This Repo) | 19.65 dB | 11.97% (166,624,550) | 13.73 dB | 0.7285 | 29.35 | 0.0953 |
Prompt Scene (65 frames @ 16 fps) | Uniform PSNR / SSIM / MAE | phi-lattice v2 PSNR / SSIM / MAE | Temporal Consistency (Uniform vs v2) |
|---|---|---|---|
| 1. Tokyo Neon Street Walk | 12.20 dB / 0.4873 / 30.35 | 13.26 dB / 0.5649 / 26.51 | 0.0607 vs 0.0616 |
| 2. Himalayan Snow Leopard | 14.51 dB / 0.7047 / 32.66 | 14.63 dB / 0.7610 / 31.46 | 0.0855 vs 0.1828 (2.14x) |
| 3. Coral Reef Sea Turtle | 13.19 dB / 0.8561 / 30.26 | 13.29 dB / 0.8598 / 30.08 | 0.0585 vs 0.0414 |
Metric Definitions:
- Sparsity denominator:
166,624,550exact zeros out of1,391,984,640quantized linear weights (11.97%of quantized weights,11.74%of all1,418,996,800transformer parameters). - Video PSNR / SSIM / Pixel MAE: Frame-by-frame fidelity against the unquantized
Baseline (BF16)rollout under the identical seed (77,202,309). - Temporal Consistency: Mean cosine similarity between frame-to-frame motion vectors $\Delta \mathbf{F}t = \mathbf{F}{t+1} - \mathbf{F}t$ of the quantized model and the
Baseline (BF16)reference: $$\text{Temporal Consistency} = \frac{1}{N-1} \sum{t=1}^{N-1} \frac{\langle \Delta \mathbf{F}^{\text{quant}}_t,; \Delta \mathbf{F}^{\text{BF16}}_t \rangle}{|\Delta \mathbf{F}^{\text{quant}}_t|_2 \cdot |\Delta \mathbf{F}^{\text{BF16}}_t|_2}$$
4. Quick Start & Loading Modes
Option A: Using phi_loader.py (Downloads only 757.1 MiB, skips 2.84 GB safetensors)
import importlib.util
import torch
from diffusers import AutoencoderKLWan, WanPipeline
from huggingface_hub import hf_hub_download
repo_id = "ununununununununununununununununununun/wan2.1-t2v-1.3b-phi-lattice-v2"
base_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
loader_path = hf_hub_download(repo_id=repo_id, filename="phi_loader.py")
spec = importlib.util.spec_from_file_location("phi_loader", loader_path)
phi_loader = importlib.util.module_from_spec(spec)
spec.loader.exec_module(phi_loader)
# Available modes:
# - "triton_4bit" : 761.65 MiB VRAM (fused Triton LUT-GEMM in GPU registers, no BF16 weight materialization)
# - "low_vram_4bit": 761.65 MiB VRAM (PyTorch on-the-fly per-layer dequantization)
# - "unrolled_bf16": 2,708.02 MiB VRAM (unpacks once into standard BF16 nn.Linear on load for full cuBLAS speed)
transformer = phi_loader.load_wan_from_packed_4bit(
repo_id=repo_id,
mode="triton_4bit",
device="cuda",
dtype=torch.bfloat16,
)
vae = AutoencoderKLWan.from_pretrained(base_id, subfolder="vae", torch_dtype=torch.float32)
pipe = WanPipeline.from_pretrained(
base_id, vae=vae, transformer=transformer, torch_dtype=torch.bfloat16
).to("cuda")
Option B: Minimal 15-Line Unpacker (torch.load(..., weights_only=True))
Archive structure of transformer/wan_transformer_phi_v2_packed_4bit.pt (sha256: fd10a4f6d25bc5391dc0b43d0141d8678dcb6dc2aaae5e98325370ebb6ea83e2):
codebook:torch.float16lookup table of shape[15](0.0at index7)group_size:64protected_tensors:225tensors (norms, patch/time/text embeddings, modulation tables inbfloat16)quantized_layers:300linear layers (shape,packed_indicesinuint8low-nibble-first,scalesinfloat16 [N/64, 1], optionalbiasinfloat16)
import math, torch
from diffusers import WanTransformer3DModel
from huggingface_hub import hf_hub_download
REPO = "ununununununununununununununununununun/wan2.1-t2v-1.3b-phi-lattice-v2"
cfg = WanTransformer3DModel.load_config(REPO, subfolder="transformer")
transformer = WanTransformer3DModel.from_config(cfg).to(torch.bfloat16)
blob = torch.load(
hf_hub_download(REPO, "transformer/wan_transformer_phi_v2_packed_4bit.pt"),
map_location="cpu",
weights_only=True,
)
cb = blob["codebook"].float()
g = int(blob["group_size"])
sd = {k: v.to(torch.bfloat16) for k, v in blob["protected_tensors"].items()}
for name, item in blob["quantized_layers"].items():
n = math.prod(item["shape"])
ng = math.ceil(n / g)
p = item["packed_indices"]
idx = torch.stack([p & 0x0F, (p >> 4) & 0x0F], dim=-1).reshape(-1)[: ng * g].long()
w = (cb[idx].view(ng, g) * item["scales"].float()).reshape(-1)[:n]
sd[f"{name}.weight"] = w.view(item["shape"]).to(torch.bfloat16)
if "bias" in item:
sd[f"{name}.bias"] = item["bias"].to(torch.bfloat16)
transformer.load_state_dict(sd, strict=True)
5. Independent Verification & Interactive ZeroGPU Lab
You can verify the archive and the fused Triton kernel either directly in your browser on a live NVIDIA RTX PRO 6000 Blackwell via our Hugging Face Space or locally from the command line.
Interactive Browser Verification: Phi Verification Lab (ZeroGPU)
The Space provides 4 interactive verification tabs:
- Tab 1 —
Archive structure (CPU): Computes thesha256checksum ofwan_transformer_phi_v2_packed_4bit.pt(fd10a4f6...e2), inspects the 15-levelcodebook, and verifies that all300quantized layers (240 × [1536×1536],30 × [1536×8960],30 × [8960×1536]) satisfyK % 64 == 0. - Tab 2 —
Triton selftest (GPU): Runs the Triton dequantizer (phi_dequantize) and fused LUT-GEMM (phi_linear) across up to all300 / 300layers of the archive, checkingtorch.equalbit-exactness ofWand relative L2 error ofYagainst strictfp32-accumulate cuBLAS. - Tab 3 —
Micro-benchmark (GPU): Benchmarks single-layer latency (M = 1024tokens) of the fused 4-bit Triton kernel (~0.5 B/weightmemory traffic) against materializedBF16cuBLAS (2 B/weight). - Tab 4 —
Debug one layer (GPU): Compares bothphi_linearand default/strict cuBLASF.linearagainst afloat64ground-truth matrix multiplication oracle (x.double() @ W.double().T + b.double()).
Summary of Verified Results (Log: tests/selftest_blackwell_300layers.log)
| Verification Check | Measured Result |
|---|---|
| Deserialization safety | torch.load(..., weights_only=True, mmap=True) passed |
| Tensor & parameter count | 825 / 825 tensors (300 quantized weights + 300 biases + 225 protected = 1,418,996,800 params) |
Weight SQNR vs upstream Wan-AI (FP32) | 19.65 dB across 1,391,984,640 quantized weights (54.97 dB on protected tensors due to FP32 -> BF16 cast) |
| Exact zeros created by optimization | 166,624,550 (11.97%) vs 0.0000% in upstream Wan-AI |
Archive round-trip vs BF16 safetensors | 53.79 dB SQNR, `max |
GPU VRAM allocated (A100 / Blackwell) | 761.65 MiB (757.10 MiB archive + 4.55 MiB RoPE buffers) vs 2,708.02 MiB BF16 (3.56x VRAM reduction) |
Triton Dequant (phi_dequantize, 300/300 layers) | W-bitexact = True on all 300 / 300 layers (0 / 13,762,560 mismatched elements per layer) |
Fused Triton LUT-GEMM vs float64 oracle | relL2 = 1.662e-03 (`max |
Fused Triton LUT-GEMM vs strict fp32-acc cuBLAS | 300 / 300 layers PASS: relL2 spans 8.19e-06 (blocks.0.attn1.to_out.0) to 7.17e-05 (blocks.19.ffn.net.2) |
Latency vs materialized BF16 cuBLAS (M=1024) | [1536×1536]: 0.105 ms vs 0.063 ms (0.59x); [1536×8960]: 0.551 ms vs 0.278 ms (0.51x); [8960×1536]: 0.405 ms vs 0.150 ms (0.37x) — trades 1.7x–2.7x GEMM time for 3.56x lower VRAM |
Local CLI Verification Commands
# 1. CPU: verify archive structure, 53.79 dB round-trip vs BF16 safetensors, and 166,624,550 exact zeros
python tests/test_archive_roundtrip.py transformer/wan_transformer_phi_v2_packed_4bit.pt transformer/diffusion_pytorch_model.safetensors
# 2. CPU: verify 19.65 dB SQNR against upstream Wan-AI FP32 shards
python tests/test_sqnr_vs_upstream.py transformer/wan_transformer_phi_v2_packed_4bit.pt diffusion_pytorch_model-00001-of-00002.safetensors diffusion_pytorch_model-00002-of-00002.safetensors
# 3. GPU: run Triton LUT-GEMM bit-exact dequantization and strict fp32 oracle check
python phi_lut_gemm.py transformer/wan_transformer_phi_v2_packed_4bit.pt