triton_fused_norm ¶
Triton-fused residual + LayerNorm + scale/shift for inference.
Collapses the eager chain
residual_out = residual + x * gate (or residual + x)
normalized = FP32LayerNorm(residual_out)
modulated = normalized * (1 + scale) + shift (optional)
... followed by the caller's .to(orig_dtype) casts ...
into a single kernel with two outputs, both already in the stream dtype, so the caller-side casts become no-ops. All arithmetic is fp32 regardless of I/O dtype.
Numerics: for the two Wan input classes the eager path's rounding points are replicated exactly. When the gate is a fp32 tensor, eager type promotion keeps the whole chain in fp32 with one final round -- the kernel does the same. When the gate is the scalar 1 and the stream is bf16, eager materializes bf16 intermediates (the residual sum and the norm output), so the kernel round-trips through bf16 at the same two points. A bf16 tensor gate with a bf16 stream is only half-modelled: eager rounds the product x * gate to bf16 before the add, while the kernel keeps the product in fp32 and rounds the sum once, so the fused residual can differ from eager by up to one bf16 ulp of the product (the fused value is slightly more accurate, not wrong). The remaining differences from eager are the reduction order inside mean/variance (last-ulp fp32) and that product-rounding case.
Inference-only: callers must gate on torch.is_grad_enabled(); there is no backward. Disable globally with FASTVIDEO_DISABLE_FUSED_NORM=1.
Functions:¶
fastvideo.layers.triton_fused_norm.fused_path_supported ¶
fused_path_supported(residual: Tensor, x: Tensor, gate: Tensor | int, shift: Tensor | None, scale: Tensor | None, norm: Module) -> bool
Cheap eligibility check; any False falls back to the eager path.
Source code in fastvideo/layers/triton_fused_norm.py
fastvideo.layers.triton_fused_norm.fused_residual_norm_mod ¶
fused_residual_norm_mod(residual: Tensor, x: Tensor, gate: Tensor | int, shift: Tensor | None, scale: Tensor | None, norm: Module) -> tuple[Tensor, Tensor]
Run the fused kernel. Caller must have checked fused_path_supported().
Returns (modulated_or_normalized, residual_out), both in the stream dtype.
Source code in fastvideo/layers/triton_fused_norm.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |