Spaces:
Running on Zero
Running on Zero
File size: 15,765 Bytes
275a757 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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 289 290 | """On-Policy Codec Distillation for Efficient All-in-One Video Generation (CodecOPSD).
Authors: Boyang Wang, Kevin Blackburn-Matzen, Oindrila Saha, Matheus Gadelha, Zezhou Cheng
Copyright (c) 2026 The CodecOPSD Authors. All rights reserved.
FP8 linears for the text encoder of MiniMax-H3 / MiniMax-H3-Image (51 decoder layers: 50.3 GB in bf16).
Every linear of the decoder layers is stored float8_e4m3fn with one scale per output row (about 26 GB). Two ways to run it:
- `matmul="fp8"`: the input is cast to FP8 too (one scale per token) and the product runs on the FP8 tensor cores
(`torch._scaled_mm`, accumulation in fp32, output in the input dtype). Needs FP8 tensor cores (Ada, Hopper, Blackwell);
falls back to `"bf16"` elsewhere.
- `matmul="bf16"`: storage only, the weight is cast back to the input dtype for a bf16 product (any GPU).
The vision encoder, the embeddings and the norms stay bf16; the language-model head, never read by MiniMax-H3, is dropped.
Converting at load needs the 66 GB bf16 release in CPU memory first (read, cropped, then converted), more than many
machines with a consumer GPU have (and, under strict overcommit, more commit headroom). So the entry points read an
FP8 VLM that is already converted instead (`load_fp8_text_encoder`, `codecopsd.download.vlm_fp8_source`):
- the third-party ModelOpt copy `feizhai123/MiniMax-H3-ModelOpt-Mixed9-Dynamic-FP8` (folder Ref2VA/text_encoder/,
29.6 GB), downloaded into models/. Checked tensor by tensor against our conversion: its 316 FP8 linears (F8_E4M3
weight [out, in], F32 `weight_scale` [out]) are byte-identical to `Fp8Linear`'s `weight_fp8` and `scale`; the 34
linears it keeps in bf16 are byte-identical to the release, and are converted here at load (`Fp8Linear`, as
`text_encoder_to_fp8` does); every other tensor is identical. It holds decoder layers 0-49 only (MiniMax-H3 reads
the input of layer 50, the pipelines' `vlm_prompt_embeds`) and an unused language-model head, skipped;
- a folder saved once by `save_fp8_text_encoder` (`python -m inference.vlm_fp8`, models/MiniMax-H3-VLM-FP8);
- else the bf16 release, converted at load as above (`text_encoder_to_fp8`).
Both FP8 folders are read as views of read-only file mappings (`codecopsd.offload.map_safetensors`): no bf16 read, no
copy in process memory, and the pinned offload can page-lock them in place or leave them in the page cache.
"""
# TODO (RTX PRO 6000 / RTX 5090): FastVideo runs MiniMax-H3 on these GPUs with NVFP4 weights for the DiT, the text
# encoder, and the VAE decoder by default. Measure speed, GPU memory, and quality against bf16 on an RTX PRO 6000 before
# adopting any of them (on a B200 the NVFP4 DiT was 1.14-1.3x faster per step but changed the content).
import json
from pathlib import Path
import torch
import torch.nn.functional as F
from torch import nn
try:
import triton
import triton.language as tl
except ImportError: # CPU-only installs: the PyTorch cast below
triton = None
FP8 = torch.float8_e4m3fn
FP8_MAX = torch.finfo(FP8).max
USE_TRITON = triton is not None # one fused kernel for the per-token cast; the PyTorch ops take ~10x longer
def fp8_tensor_cores(capability):
"""Whether a GPU of compute capability `capability` ((major, minor)) has FP8 tensor cores: 8.9 and up (Ada, Hopper,
Blackwell). Without them `matmul="fp8"` computes in bf16 from the FP8 weights."""
return tuple(capability) >= (8, 9)
if triton is not None:
@triton.jit
def _quantize_rows_kernel(x_ptr, q_ptr, s_ptr, K, stride, BLOCK: tl.constexpr):
row = tl.program_id(0)
offs = tl.arange(0, BLOCK)
mask = offs < K
x = tl.load(x_ptr + row * stride + offs, mask=mask, other=0.0).to(tl.float32)
scale = tl.div_rn(tl.maximum(tl.max(tl.abs(x), axis=0), 1e-12), 448.0)
tl.store(q_ptr + row * K + offs, tl.div_rn(x, scale).to(tl.float8e4nv), mask=mask)
tl.store(s_ptr + row, scale)
def quantize_rows(x: torch.Tensor):
"""[M, K] -> (float8_e4m3fn [M, K], float32 scale [M, 1]): one scale per row, the row's largest value maps to 448."""
if USE_TRITON and x.is_cuda and x.stride(-1) == 1:
M, K = x.shape
q = torch.empty((M, K), device=x.device, dtype=FP8)
s = torch.empty((M, 1), device=x.device, dtype=torch.float32)
if M:
_quantize_rows_kernel[(M,)](x, q, s, K, x.stride(0), BLOCK=triton.next_power_of_2(K), num_warps=16)
return q, s
s = x.abs().amax(dim=1, keepdim=True).float().clamp_min(1e-12) / FP8_MAX
return (x.float() / s).to(FP8), s
class Fp8Linear(nn.Module):
"""`nn.Linear` with its weight stored in FP8, one scale per output row."""
rowwise = None # whether torch._scaled_mm takes per-row / per-token scales on this GPU (probed on first use)
@classmethod
def empty(cls, linear: nn.Linear, matmul: str = "fp8"):
"""An `Fp8Linear` of the shape of `linear` with no weights yet (meta tensors), filled by
`load_fp8_text_encoder`."""
self = cls.__new__(cls)
nn.Module.__init__(self)
self.register_buffer("weight_fp8", torch.empty(linear.out_features, linear.in_features, dtype=FP8,
device="meta"))
self.register_buffer("scale", torch.empty(1, linear.out_features, dtype=torch.float32, device="meta"))
self.bias = linear.bias
self.matmul = matmul
return self
def __init__(self, linear: nn.Linear, matmul: str = "fp8"):
super().__init__()
device = "cuda" if torch.cuda.is_available() else linear.weight.device
w = linear.weight.detach().to(device, torch.float32)
# times the reciprocal, as PyTorch's CUDA kernel divides by a number: the same bytes on the CPU (a ZeroGPU
# Space builds the pipeline without a GPU) as on the GPU
scale = w.abs().amax(dim=1, keepdim=True).clamp_min(1e-12) * (1.0 / FP8_MAX)
home = linear.weight.device
self.register_buffer("weight_fp8", (w / scale).to(FP8).to(home)) # [out, in]
self.register_buffer("scale", scale.t().contiguous().to(home)) # [1, out], float32
self.bias = linear.bias
self.matmul = matmul
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.matmul != "fp8" or not x.is_cuda or not fp8_tensor_cores(torch.cuda.get_device_capability(x.device)):
return F.linear(x, self.weight_fp8.to(x.dtype) * self.scale.t().to(x.dtype), self.bias)
shape = x.shape
x = x.reshape(-1, shape[-1])
x_fp8, x_scale = quantize_rows(x) # one scale per token
out = self._mm(x_fp8, x_scale, x.dtype)
if self.bias is not None:
out = out + self.bias
return out.reshape(*shape[:-1], out.shape[-1])
def _mm(self, x_fp8, x_scale, dtype):
w = self.weight_fp8.t() # [in, out], column-major as _scaled_mm wants
if Fp8Linear.rowwise is not False:
try:
out = torch._scaled_mm(x_fp8, w, scale_a=x_scale, scale_b=self.scale, out_dtype=dtype)
Fp8Linear.rowwise = True
return out
except RuntimeError:
if Fp8Linear.rowwise:
raise
Fp8Linear.rowwise = False # per-tensor scales only: unit scales in the product, the real ones after it
one = torch.ones((), device=x_fp8.device)
out = torch._scaled_mm(x_fp8, w, scale_a=one, scale_b=one, out_dtype=dtype)
return out.mul_(x_scale.to(dtype)).mul_(self.scale.to(dtype))
def text_encoder_to_fp8(text_encoder, matmul: str = "fp8") -> int:
"""Swap every linear of the text encoder's decoder layers for an `Fp8Linear`, one layer at a time (the bf16 copy of a layer
is freed before the next is converted), and drop the unused language-model head. Returns the number of linears."""
if matmul not in ("fp8", "bf16"):
raise ValueError(f"matmul must be 'fp8' or 'bf16', got {matmul!r}")
text_encoder.lm_head = nn.Identity()
count = 0
for layer in text_encoder.model.language_model.layers:
for parent in list(layer.modules()):
for name, child in list(parent.named_children()):
if isinstance(child, nn.Linear):
setattr(parent, name, Fp8Linear(child, matmul))
count += 1
torch.cuda.empty_cache()
return count
FP8_FORMAT = "codecopsd_fp8" # config key of a saved FP8 text encoder: the decoder layers kept, the scale layout
def save_fp8_text_encoder(text_encoder, folder, shard_gb=4.0):
"""Write a text encoder converted by `text_encoder_to_fp8` (and cropped to the layers MiniMax-H3 reads, as the
pipelines do) to `folder`: its config and every tensor exactly as the converted model holds it, in .safetensors
shards of at most `shard_gb` GB with an index (`load_fp8_text_encoder` reads it back)."""
from safetensors.torch import save_file
folder = Path(folder)
folder.mkdir(parents=True, exist_ok=True)
shards, size = [{}], 0
for name, tensor in text_encoder.state_dict().items():
nbytes = tensor.numel() * tensor.element_size()
if shards[-1] and size + nbytes > shard_gb * 2**30:
shards.append({})
size = 0
shards[-1][name] = tensor.detach().to("cpu").contiguous()
size += nbytes
weight_map = {}
for i, shard in enumerate(shards):
file = f"model-{i + 1:05d}-of-{len(shards):05d}.safetensors"
save_file(shard, str(folder / file), metadata={"format": "pt"})
weight_map.update(dict.fromkeys(shard, file))
total = sum(t.numel() * t.element_size() for shard in shards for t in shard.values())
(folder / "model.safetensors.index.json").write_text(
json.dumps({"metadata": {"total_size": total}, "weight_map": weight_map}, indent=2))
text_encoder.config.save_pretrained(folder)
config = json.loads((folder / "config.json").read_text())
config[FP8_FORMAT] = dict(layers=len(text_encoder.model.language_model.layers),
weights="float8_e4m3fn [out, in], float32 scale [1, out] per output row")
(folder / "config.json").write_text(json.dumps(config, indent=2))
return folder
def fp8_folder_format(folder):
""""modelopt" (the downloaded ModelOpt copy: FP8 per output channel, dynamic per-token activations), "saved"
(`save_fp8_text_encoder`) or None (no FP8 text encoder in `folder`), from its config.json."""
try:
config = json.loads((Path(folder) / "config.json").read_text())
except (OSError, ValueError):
return None
if FP8_FORMAT in config:
return "saved"
quant = config.get("quantization_config") or {}
if quant.get("quant_method") == "modelopt" and quant.get("quant_algo") == "FP8_PER_CHANNEL_PER_TOKEN":
return "modelopt"
return None
def load_fp8_text_encoder(folder, matmul: str = "fp8", dtype=torch.bfloat16):
"""The FP8 text encoder in `folder`, in CPU memory, the same tensors as `text_encoder_to_fp8` makes from the bf16
release (`matmul` as there): a folder of `save_fp8_text_encoder` (51 decoder layers) or the downloaded ModelOpt
copy (50 decoder layers; see the module docstring). Every weight read from the files is a view of a read-only
shared mapping of them (`codecopsd.offload.map_safetensors`): nothing is converted or copied, except the linears
the ModelOpt copy keeps in bf16, converted to `Fp8Linear` here exactly as `text_encoder_to_fp8` converts them."""
from accelerate import init_empty_weights
from transformers import AutoConfig, AutoModelForImageTextToText
from ..offload import map_safetensors
if matmul not in ("fp8", "bf16"):
raise ValueError(f"matmul must be 'fp8' or 'bf16', got {matmul!r}")
folder = Path(folder)
kind = fp8_folder_format(folder)
if kind is None:
raise ValueError(f"{folder} holds no FP8 text encoder (neither a ModelOpt FP8 copy nor a saved one)")
config = AutoConfig.from_pretrained(folder)
if kind == "modelopt":
# The copy's ModelOpt quantization_config is not applied: transformers would build its own FP8 modules. The
# model is built as the bf16 release and its FP8 tensors become our `Fp8Linear` buffers below.
del config.quantization_config
index = json.loads((folder / "model.safetensors.index.json").read_text())["weight_map"]
with init_empty_weights(include_buffers=False): # weights on the meta device, buffers (rotary tables) computed
text_encoder = AutoModelForImageTextToText.from_config(config, dtype=dtype)
text_encoder.lm_head = nn.Identity() # never read by MiniMax-H3 (the copy's lm_head.weight is skipped)
keep_bf16 = [] # (parent, name) of the linears the ModelOpt copy keeps in bf16: converted after loading
for i, layer in enumerate(text_encoder.model.language_model.layers):
for path, parent in list(layer.named_modules()):
for name, child in list(parent.named_children()):
if not isinstance(child, nn.Linear):
continue
key = f"model.language_model.layers.{i}.{path + '.' if path else ''}{name}"
if kind == "modelopt" and f"{key}.weight_scale" not in index:
keep_bf16.append((parent, name))
else:
setattr(parent, name, Fp8Linear.empty(child, matmul))
expected = text_encoder.state_dict(keep_vars=True) # every tensor the files must give (not the rotary tables)
wanted = {} # file key -> (model key, layout)
for key in index:
if key.startswith("lm_head."):
continue
base, _, leaf = key.rpartition(".")
if kind == "modelopt" and leaf == "weight" and f"{base}.weight_scale" in index:
wanted[key] = (f"{base}.weight_fp8", "fp8")
elif kind == "modelopt" and leaf == "weight_scale":
wanted[key] = (f"{base}.scale", "scale")
else:
wanted[key] = (key, "same")
missing = sorted(set(expected) - {target for target, _ in wanted.values()})
unexpected = sorted(target for target, _ in wanted.values() if target not in expected)
if missing or unexpected:
raise ValueError(f"{folder} does not match its config: missing {missing[:3]}, unexpected {unexpected[:3]}")
for file in sorted(set(index.values())):
for key, tensor in map_safetensors(folder / file).items():
if key not in wanted:
continue
target, layout = wanted[key]
if layout == "scale": # ModelOpt: float32 [out]; Fp8Linear: float32 [1, out] (the same bytes)
tensor = tensor.view(1, -1)
meta = expected[target]
if tensor.dtype != meta.dtype or tuple(tensor.shape) != tuple(meta.shape):
raise ValueError(f"{folder}: {key} is {tensor.dtype} {tuple(tensor.shape)}, expected {meta.dtype} "
f"{tuple(meta.shape)}")
path, _, leaf = target.rpartition(".")
module = text_encoder.get_submodule(path)
if leaf in module._parameters:
module._parameters[leaf] = nn.Parameter(tensor, requires_grad=False)
else:
module._buffers[leaf] = tensor
left = [n for n, t in [*text_encoder.named_parameters(), *text_encoder.named_buffers()] if t.is_meta]
if left:
raise ValueError(f"{folder}: no weights for {left[:3]}")
for parent, name in keep_bf16: # as text_encoder_to_fp8: on the GPU when there is one, the result in CPU memory
setattr(parent, name, Fp8Linear(getattr(parent, name), matmul))
torch.cuda.empty_cache()
return text_encoder.eval()
|