Pidoku

Video Generation Inference

Module 26 Lesson 26.d 1h 15m Difficulty 4/5

Prerequisites 26.c, 14.f, 13.l, 24.b


Before you start: words you need#

WordPlain meaning
3D VAEAn autoencoder that compresses video in time as well as space, for example 4 frames into 1 and 8×8 pixels into 1.
Latent frameOne time-slice of the compressed video the DiT works on.
3D full attentionEvery video token attends to every other token across all frames. Quadratic in the token count.
Sparse attentionComputing attention only for the token pairs likely to matter (nearby in space and time, or picked by a cheap first pass).
Sequence parallelism (SP)Splitting one video’s tokens across GPUs; Ulysses and Ring are two ways to do the attention (14.f).
Step distillationA student model trained to produce the result in a few steps instead of 50 or more (26.c).
I2V / T2VImage-to-video and text-to-video.

1. What is it?#

Video generators are image diffusion models (26.c) with one more dimension. A 3D VAE compresses the clip in time and space, a DiT denoises all latent frames together with attention across space and time, and the VAE decodes the result. The open models engineers serve in 2026 include the Wan family (Wan 2.1 and 2.2), HunyuanVideo 1.5, LTX-2 and Cosmos, served through diffusers, xDiT, FastVideo, LightX2V, SGLang Diffusion and vLLM-Omni.

Everything that made image diffusion expensive is multiplied: tens to hundreds of thousands of tokens per pass, attention that dominates the compute, and a job that runs for minutes. Facts checked on 13 November 2026.


2. Why does it matter?#

  • It is the most expensive inference request in common use. One five-second 720p clip from a 14B model, run naively, costs as much compute as thousands of chat requests (26.a).
  • Attention is the bottleneck, not the weights. The Sliding Tile Attention paper measured attention at 800 of 945 seconds for a five-second 720p clip with HunyuanVideo. Tricks from LLM decode do not apply; sparse attention and parallel attention do.
  • The speed-ups are enormous and recent. Combining sparse attention, step distillation and 8-bit kernels, the TurboDiffusion authors report 100–200x faster generation for Wan models on one RTX 5090 (their numbers, December 2025). Knowing which levers stack is the difference between minutes and seconds per clip.

3. Simple analogy#

A flip-book animator drawing every page at once, checking each page against every other page for consistency. With 20 pages it is fine. With 2,000 pages, the checking takes far longer than the drawing. Good animators only compare each page with its neighbours and a few key frames (sparse attention), split the pages among several artists who swap notes (sequence parallelism), and learn to draw in four passes instead of fifty (distillation).


4. Tiny example: a five-second 720p clip#

request      "a dog running on a beach at sunset", 1280 x 720, 81 frames at 16 fps
3D VAE       time / 4, space / 8  -> 21 latent frames of 90 x 160
patchify     1 x 2 x 2 patches    -> 21 x 45 x 80 = 75,600 tokens
DiT          Wan-2.1-14B shape: 40 layers, width 5120
sampling     50 steps x 2 (classifier-free guidance) = 100 passes over 75,600 tokens
VAE decode   21 latent frames -> 81 frames of 1280 x 720
encode       frames -> MP4 (CPU or NVENC), upload

Wan 2.2’s 5B model uses a higher-compression VAE (4×16×16; 4×32×32 including patchification), so the same clip is four times fewer tokens. Its authors state it generates a five-second 720p video in under nine minutes on one consumer GPU without special optimisation.


5. Technical explanation#

Where the time goes#

Per pass: linear layers cost about 2 × parameters × tokens; self-attention about 4 × layers × width × tokens². At 75,600 tokens with width 5,120, attention is over two thirds of the work and grows faster than anything else as clips get longer or sharper (see section 6).

Lever 1: sparse attention#

Attention in video DiTs concentrates on nearby positions in space and time. Methods that exploit it:

MethodIdeaNeeds training?
Sliding Tile Attention (STA)Each tile of tokens attends to a 3D window of neighbouring tiles; tile-shaped so it maps onto efficient kernelsNo (fine-tuning improves it)
Video Sparse Attention (VSA)A coarse pass pools tokens into tiles and picks important ones; a fine pass attends only inside them. Trainable, one kernel; authors report 85% of FlashAttention-3’s MFUYes (trained in)
Radial AttentionCompute density decays with spatial and temporal distance; O(n log n)Light fine-tune (LoRA)
Sparse-Linear Attention (SLA), SpargeAttnSparse blocks for important pairs, linear attention or skipping for the restVaries

FastVideo ships STA and VSA kernels; xDiT 0.7.0 added VSA variants for newer models. The 2025–26 trend matches LLMs (24.b): sparsity that is trained into the model holds quality better than sparsity added afterwards.

Lever 2: fewer steps#

Video distillation follows image distillation (26.c): consistency and distribution-matching methods (rCM, DMD-style) produce 3–8-step students. Distilled Wan checkpoints are distributed by LightX2V and FastVideo. TurboDiffusion’s paper uses rCM to reach 3–4 steps.

Lever 3: step caches#

PAB, FasterCache, TeaCache, MagCache, TaylorSeer and FirstBlockCache (26.c) were in large part designed for video, where adjacent steps are especially similar. Diffusers v0.43.0 also added SeaCache, tuned for Cosmos 3. Settings are model- and scheduler-specific.

Lever 4: more GPUs per clip#

Unlike images, a single clip often needs several GPUs to finish in reasonable time.

  • Ulysses sequence parallelism splits tokens across GPUs and uses all-to-all to give each GPU all tokens for a subset of heads (14.f). Wan’s reference script runs torchrun --nproc_per_node=8 generate.py ... --dit_fsdp --t5_fsdp --ulysses_size 8. Limited by the number of heads (40 for Wan 2.1 14B).
  • Ring attention / USP combine with Ulysses beyond the head limit (diffusers' ContextParallelConfig(ulysses_degree=..., ring_degree=...), xDiT’s USP).
  • CFG parallelism: conditional and unconditional passes on separate GPU groups.
  • Parallel VAE decode: split the decode spatially or temporally across GPUs; the VAE can otherwise become the serial tail of a parallel job.
  • FP8 communication: xDiT 0.7.0 sends the Ulysses all-to-all in FP8 to cut communication time.

SP reduces latency; it does not reduce GPU-seconds per clip, and communication makes it slightly worse. Sparse attention and distillation reduce both.

Lever 5: smaller latents#

A higher-compression VAE (Wan 2.2 5B) or a smaller resolution followed by a super-resolution stage (HunyuanVideo 1.5 has a 1080p super-resolution pipeline in FastVideo) cuts tokens at the source.

Model-specific wrinkles#

  • Wan 2.2’s A14B models are a two-expert MoE across timesteps: a high-noise expert for early steps and a low-noise expert for later ones, so the active compute per step stays at 14B. Serving both means holding both sets of weights or swapping them mid-job; TurboDiffusion notes this switching overhead in its measurements.
  • Joint audio and video (LTX-2 and newer models) adds an audio stream to the same denoising loop.
  • Autoregressive and streaming video (world models, real-time “AR-diffusion”) generate chunk by chunk, carry a cache between chunks and look more like streaming LLM serving. vLLM-Omni and FastVideo both added such pipelines in 2026; treat them as emerging.

6. Under the hood: why attention dominates, and what each lever is worth#

The program estimates token counts and pass times for a Wan-2.1-14B-shaped model on one H100 at 50% of BF16 peak, then applies the levers to the five-second 720p clip. The 90% sparsity and the 1.6x low-precision speed-up are assumptions; ideal SP ignores communication.

Go
package main

import "fmt"

// A Wan-2.1-14B-shaped video DiT: 40 layers of width 5120, a VAE that compresses
// 4x in time and 8x8 in space, and 1x2x2 patches. 16 frames per second.
const (
	params = 14e9
	layers = 40
	width  = 5120.0
	effTF  = 0.5 * 989e12 // H100 BF16 at 50% of peak
)

func tokens(w, h, seconds float64) float64 {
	frames := seconds*16 + 1
	latentT := (frames-1)/4 + 1
	return latentT * (h / 16) * (w / 16) // 8x VAE then 2x2 patch = 16 px per token side
}

// seconds for one forward pass; sparsity removes that fraction of attention work
func pass(n, sparsity float64) (sec, attnShare float64) {
	linear := 2 * params * n
	attn := 4 * layers * width * n * n * (1 - sparsity)
	return (linear + attn) / effTF, attn / (linear + attn)
}

func main() {
	fmt.Printf("%-16s %8s %7s %10s %14s\n", "clip", "tokens", "attn %", "s/pass", "50x2 passes")
	for _, c := range []struct {
		name       string
		w, h, secs float64
	}{
		{"480p, 5 s", 832, 480, 5},
		{"720p, 5 s", 1280, 720, 5},
		{"720p, 10 s", 1280, 720, 10},
		{"1080p, 5 s", 1920, 1088, 5},
	} {
		n := tokens(c.w, c.h, c.secs)
		s, share := pass(n, 0)
		fmt.Printf("%-16s %8.0f %6.0f%% %10.1f %12.0f s\n", c.name, n, 100*share, s, 100*s)
	}

	fmt.Println("\n720p, 5 s clip on one H100 -> recipes")
	n := tokens(1280, 720, 5)
	for _, r := range []struct {
		name           string
		passes, sparse float64
		gpus, speedup  float64
	}{
		{"baseline: 50 steps x CFG, dense", 100, 0, 1, 1},
		{"8 GPUs, Ulysses SP (ideal)", 100, 0, 8, 1},
		{"90% sparse attention", 100, 0.9, 1, 1},
		{"90% sparse + 4-step distilled", 4, 0.9, 1, 1},
		{"  + 8-bit matmul/attention (1.6x)", 4, 0.9, 1, 1.6},
	} {
		s, _ := pass(n, r.sparse)
		total := r.passes * s / r.gpus / r.speedup
		gpuSec := total * r.gpus
		fmt.Printf("  %-36s %8.1f s wall %8.0f GPU-s  $%.3f per clip at $2.5/GPU-h\n",
			r.name, total, gpuSec, gpuSec/3600*2.5)
	}
}

Output:

clip               tokens  attn %     s/pass    50x2 passes
480p, 5 s           32760     49%        3.6          363 s
720p, 5 s           75600     69%       13.7         1375 s
720p, 10 s         147600     81%       44.4         4445 s
1080p, 5 s         171360     83%       58.3         5835 s

720p, 5 s clip on one H100 -> recipes
  baseline: 50 steps x CFG, dense        1374.9 s wall     1375 GPU-s  $0.955 per clip at $2.5/GPU-h
  8 GPUs, Ulysses SP (ideal)              171.9 s wall     1375 GPU-s  $0.955 per clip at $2.5/GPU-h
  90% sparse attention                    522.8 s wall      523 GPU-s  $0.363 per clip at $2.5/GPU-h
  90% sparse + 4-step distilled            20.9 s wall       21 GPU-s  $0.015 per clip at $2.5/GPU-h
    + 8-bit matmul/attention (1.6x)        13.1 s wall       13 GPU-s  $0.009 per clip at $2.5/GPU-h

How to read it:

  • Doubling the duration more than triples the cost (1,375 s to 4,445 s), because attention is quadratic. Longer clips are where sparse attention pays most.
  • SP buys latency, not cost. Eight GPUs make the clip 8x faster (less in reality) at the same GPU-seconds.
  • Sparse attention and distillation multiply. Together with 8-bit kernels they take the clip from about 23 minutes to 13 seconds in this model, about 100x, the same order of magnitude as the TurboDiffusion authors’ measured 4,767 s to 24 s for Wan 2.1 14B 720p on an RTX 5090 (their latencies exclude text encoding and VAE decoding).
  • The estimate ignores the VAE decode and MP4 encoding, which matter once the DiT is fast.

7. Performance implications#

  • Attention kernels first. FlashAttention-3/4 or SageAttention, then sparse attention. On a dense model the attention kernel is most of the runtime.
  • Communication for SP. Ulysses all-to-all scales with tokens × width per layer; it wants NVLink inside a node. Across nodes, use Ring or keep SP inside one NVLink domain.
  • Memory. Activations for 75,000+ tokens are large; the VAE decode of many frames can peak higher than the DiT. Use tiled or parallel VAE decode.
  • GPU type. Video DiTs are compute-bound: tensor-core FLOPs and FP8/FP4 support matter more than HBM bandwidth. Consumer and workstation GPUs are viable for distilled models.

8. Production implications#

  • Jobs, not requests. Accept, queue, return a job ID, report progress per step, deliver a URL. A request that takes minutes cannot sit behind an HTTP timeout.
  • Checkpoint between steps. The state between steps is only the latent and the scheduler position. Saving it lets you pre-empt low-priority jobs, survive node failures and resume, which is far easier than for an LLM’s KV cache.
  • Price by output seconds and resolution, since cost grows faster than linearly with both.
  • Bin-pack by GPU count. Mixed jobs needing 1, 2, 4 or 8 GPUs fragment a cluster; schedule them like gang-scheduled training jobs (21.e).
  • Quality gates. Sparse attention, caches and distillation can cause flicker, drift and broken motion that still images do not show. Evaluate on video metrics and human review per release.
  • Safety and provenance. Video output usually needs content filters and watermarking or C2PA metadata; budget their cost.

9. Common mistakes#

  1. Planning capacity linearly in duration. Attention makes long clips super-linear.
  2. Using SP to cut cost. It cuts latency; GPU-seconds stay the same or rise.
  3. Applying sparse attention without fine-tuning and without a quality check.
  4. Ignoring the VAE. Decode memory and time become the tail once the DiT is optimised.
  5. Running long video jobs inside a synchronous API. Timeouts and retries will run the same expensive job several times.

10. Hands-on exercise#

  1. Run the program. Add a “720p, 20 s” row. What share of the pass is attention?
  2. Model Wan 2.2’s 5B model: 4×16×16 VAE compression with 2×2 patches (32 pixels per token side) and 5B parameters. Recompute tokens and the dense baseline for a five-second 720p clip.
  3. Add communication to the SP row: assume each of 40 layers does two all-to-alls, each moving tokens × width × 2 bytes ÷ 8 GPUs at 400 GB/s effective. How much does it add per pass?
  4. With FastVideo or diffusers on a GPU, generate a short low-resolution clip with dense attention and with a sparse-attention option. Compare time and inspect the motion frame by frame.

11. Interview questions#

  1. Why is attention, not weight loading or linear layers, the bottleneck of video DiTs?
  2. Compare STA and VSA. Why might a trained sparse attention hold quality better?
  3. Sequence parallelism made generation 6x faster on 8 GPUs, but finance says cost went up. Explain.
  4. How would you design preemption for a video-generation queue?
  5. Which levers would you combine to cut cost per clip by 100x, and in what order would you test them?

12. Further reading#

Checked on 13 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in