Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""Checkpoint payloads in the shapes the producers actually write.
Every loader test that wants a quantized layer has so far built one itself: five copies of the
convrot quantizer, five nvfp4 packers, six spellings of the fp8 scale, thirteen marker blobs. They
agree today, which is the problem -- they agree by having been copied, so a producer detail that is
wrong is wrong in all of them at once, and a test asserting against its own copy of the encoding
asserts nothing about the encoding.
These are mirrors of what ComfyUI's exporters emit, and each docstring says which part of the
producer it is mirroring. Nothing here imports a loader: a payload is what arrives on disk, and the
question of what a loader does with it belongs to the caller.
"""
import json
from collections.abc import Mapping
from typing import Any, NamedTuple
import numpy as np
import torch
from invokeai.backend.quantization.block_scale_tiles import check_tile_layout
from invokeai.backend.quantization.int8_convrot import CONVROT_GROUP_SIZE, build_regular_hadamard
# e4m3's largest finite magnitude. A per-tensor scaled-fp8 export divides by this, so that the
# largest weight lands on the top code rather than at infinity.
FP8_E4M3_MAX = 448.0
# int8 is quantized against 127, not 128: the exporters keep the range symmetric and leave -128
# unused, so a scale derived from 128 is off by one level on every weight.
INT8_LEVELS = 127.0
# int4 likewise: symmetric against 7, leaving -8 unused.
INT4_LEVELS = 7
# The two nvfp4 codes worth +1.0 and -1.0 in E2M1. Paired with a block scale and a global scale they
# give a weight whose exact value is known without depending on the rest of the E2M1 table.
NVFP4_CODE_PLUS_ONE = 2
NVFP4_CODE_MINUS_ONE = 10
# One MX block is 32 elements wide in every published build; the marker only cross-checks it.
MX_BLOCK_SIZE = 32
def comfy_quant_marker(marker: Mapping[str, Any], *, pad: int = 0) -> torch.Tensor:
"""The per-tensor ``.comfy_quant`` blob: UTF-8 JSON in a ``uint8`` tensor.
``pad`` appends NUL bytes, which is what Comfy does when it writes these at a fixed width. The
clone gives the tensor its own storage instead of a view onto the buffer built here, which is
what keeps a payload independent of the builder that made it.
"""
raw = json.dumps(dict(marker)).encode("utf-8") + b"\x00" * pad
return torch.frombuffer(bytearray(raw), dtype=torch.uint8).clone()
class Int8ConvrotPayload(NamedTuple):
"""What an ``int8_tensorwise`` + ``convrot`` layer stores, and what it must come back as.
``dequantized`` is the *round trip*, not the weight that went in: rounding to 127 levels is
lossy, so the original is something no correct loader can produce and comparing against it
measures the quantizer's error rather than the loader's.
"""
codes: torch.Tensor
scale: torch.Tensor
dequantized: torch.Tensor
def quantize_convrot(weight: torch.Tensor, *, group_size: int = CONVROT_GROUP_SIZE) -> Int8ConvrotPayload:
"""Mirror of comfy-quants: rotate along the input dim in groups, then per-output-channel int8.
The rotation is what makes this scheme different from plain int8, and it is also what a loader
can silently skip: applying the scale without un-rotating gives a weight of the right shape and
magnitude that correlates with the original at roughly nothing.
"""
out_features, in_features = weight.shape
hadamard = build_regular_hadamard(group_size, dtype=weight.dtype)
grouped = weight.view(out_features, in_features // group_size, group_size)
rotated = (grouped @ hadamard.T).view(out_features, in_features)
scale = rotated.abs().amax(dim=1, keepdim=True) / INT8_LEVELS
codes = torch.clamp(torch.round(rotated / scale), -128, 127).to(torch.int8)
restored = (codes.float() * scale).view(out_features, in_features // group_size, group_size) @ hadamard
return Int8ConvrotPayload(codes, scale.to(torch.float32), restored.view(out_features, in_features))
class Int4ConvrotPayload(NamedTuple):
"""What a packed int4 + convrot layer stores -- ``convrot_w4a4``, ``int4_tensorwise`` and ComfyUI-GGUF's
``Q4_CR`` all store the same bytes -- and the round trip it must come back as."""
packed: torch.Tensor
scale: torch.Tensor
dequantized: torch.Tensor
def quantize_convrot_int4(weight: torch.Tensor, *, group_size: int = CONVROT_GROUP_SIZE) -> Int4ConvrotPayload:
"""Mirror of comfy-kitchen's eager ``quantize_convrot_w4a4_weight``: the int8 recipe's rotation, then per-row
symmetric int4 against 7 levels, two codes per byte with element ``2j`` in the LOW nibble.
The packing is written out in unsigned arithmetic rather than shared with any decoder, so a decoder that reads
the nibbles in nvfp4's order -- high first -- disagrees with it instead of agreeing by construction.
"""
out_features, in_features = weight.shape
hadamard = build_regular_hadamard(group_size, dtype=torch.float32)
grouped = weight.float().view(out_features, in_features // group_size, group_size)
rotated = (grouped @ hadamard.T).view(out_features, in_features)
scale = rotated.abs().amax(dim=1, keepdim=True).clamp(min=1e-10) / INT4_LEVELS
codes = torch.clamp(torch.round(rotated / scale), -INT4_LEVELS, INT4_LEVELS).to(torch.int8)
nibbles = codes.view(torch.uint8) & 0x0F
packed = (nibbles[:, 0::2] | (nibbles[:, 1::2] << 4)).view(torch.int8)
restored = (codes.float() * scale).view(out_features, in_features // group_size, group_size) @ hadamard
return Int4ConvrotPayload(packed, scale, restored.view(out_features, in_features))
def convrot_w4a4_marker(*, group_size: int = CONVROT_GROUP_SIZE) -> dict[str, Any]:
"""comfy-kitchen's released int4 layout as the Starnodes converter names it: always rotated, so no ``convrot``
flag, and the kernel's activation group size beside it."""
return {"format": "convrot_w4a4", "convrot_groupsize": group_size, "quant_group_size": 64}
def int4_tensorwise_marker(*, group_size: int = CONVROT_GROUP_SIZE, convrot: bool = True) -> dict[str, Any]:
"""The same bytes under comfy-kitchen's unreleased ``int4_tensorwise`` name (comfy-quants' int4 exporter)."""
return {"format": "int4_tensorwise", "convrot": convrot, "convrot_groupsize": group_size}
def q4_cr_marker(rows: int, columns: int, *, group_size: int = CONVROT_GROUP_SIZE, **overrides: Any) -> dict[str, Any]:
"""The JSON ComfyUI-GGUF's converter stores per ``Q4_CR`` weight (``Q4_CR_W4A4`` in its ``tools/convert.py``):
the packed codes are written as ``I8`` ``[rows, columns / 2]``, so the unpacked shape travels in the JSON."""
return {
"format": "int4_cr",
"backing": "w4a4",
"convrot_groupsize": group_size,
"quant_group_size": 64,
"sym": True,
"orig_shape": [rows, columns],
**overrides,
}
class ScaledFp8Payload(NamedTuple):
"""What a per-tensor scaled-fp8 layer stores, and the weight it decodes back to.
``dequantized`` is the round trip rather than the input, for the same reason as
:class:`Int8ConvrotPayload`: e4m3 has three mantissa bits, so the original is not something a
correct loader can return.
"""
codes: torch.Tensor
scale: torch.Tensor
dequantized: torch.Tensor
def quantize_scaled_fp8(weight: torch.Tensor) -> ScaledFp8Payload:
"""Mirror of ComfyUI's per-tensor "scaled fp8": one float32 scalar beside an e4m3 weight.
The scale is stored as the multiplier the loader applies, so the codes are the weight *divided*
by it. Getting that direction wrong survives every shape check and every dtype check.
"""
scale = (weight.abs().max() / FP8_E4M3_MAX).to(torch.float32)
codes = (weight / scale).to(torch.float8_e4m3fn)
return ScaledFp8Payload(codes, scale, codes.float() * scale)
def quantize_per_row_fp8(weight: torch.Tensor) -> ScaledFp8Payload:
"""Mirror of Black Forest Labs' ``fp8r`` export: e4m3 codes and one float32 scale per output row, ``[out]``.
The export's manifest states the convention, ``weight.float() * weight_scale[:, None]``. A 1-D scale broadcast
the default way scales the input columns instead, which a square layer does not reveal by its shape.
"""
scale = (weight.abs().amax(dim=1) / FP8_E4M3_MAX).to(torch.float32)
codes = (weight / scale[:, None]).to(torch.float8_e4m3fn)
return ScaledFp8Payload(codes, scale, codes.float() * scale[:, None])
def nvfp4_codes(positive: torch.Tensor) -> torch.Tensor:
"""Codes for a layer of exactly +1.0 and -1.0 in E2M1, one per element of ``positive``."""
return torch.where(positive, NVFP4_CODE_PLUS_ONE, NVFP4_CODE_MINUS_ONE).to(torch.uint8)
def nvfp4_tensors(
path: str, codes: torch.Tensor, *, block_scale: float | torch.Tensor, global_scale: float
) -> dict[str, torch.Tensor]:
"""One nvfp4 layer in checkpoint layout: packed nibbles, a block-scale grid, a global scalar.
Two 4-bit codes share a byte, high nibble first, so the stored weight is half as wide as the
logical one. A number fills the grid uniformly; pass a grid to vary it. Either way the grid has
to *be* a whole number of cuBLAS tiles, so the smallest layer this can build is 128 rows by 64
columns -- `NVFP4Linear` refuses anything else rather than padding it.
No expected weight comes back, because stating one means restating the tile permutation the
decode applies (`unblock_scale_grid`), and an expectation derived from the implementation is no
expectation. Use :func:`nvfp4_signed_tensors` for a layer whose decoded weight is known outright.
"""
rows, in_features = codes.shape
grid = block_scale if isinstance(block_scale, torch.Tensor) else torch.full((rows, in_features // 16), block_scale)
return {
f"{path}.weight": (codes[:, 0::2] << 4) | codes[:, 1::2],
f"{path}.weight_scale": grid.to(torch.float8_e4m3fn),
f"{path}.weight_scale_2": torch.tensor(global_scale),
}
def nvfp4_signed_tensors(path: str, positive: torch.Tensor) -> tuple[dict[str, torch.Tensor], torch.Tensor]:
"""An nvfp4 layer whose every weight is exactly +0.5 or -0.5, and that expected weight.
A block scale of 2 and a global scale of 0.25 put the product at +-0.5, exact in every dtype
involved, so the assertion can be equality rather than a tolerance and a dropped
``weight_scale_2`` moves the result by a factor of four. The block scale is uniform, which is
what makes the expectation independent of the cuBLAS tile order -- that order is pinned on its
own in ``tests/backend/quantization/test_nvfp4.py`` rather than restated here.
"""
codes = nvfp4_codes(positive)
tensors = nvfp4_tensors(path, codes, block_scale=2.0, global_scale=0.25)
return tensors, torch.where(positive, 0.5, -0.5)
def nvfp4_modelopt_tensors(
path: str, codes: torch.Tensor, *, grid: torch.Tensor, global_scale: float
) -> dict[str, torch.Tensor]:
"""One nvfp4 layer as NVIDIA ModelOpt writes it: Black Forest Labs' FLUX.3 ``variants/nvfp4`` file.
The tensors and names of :func:`nvfp4_tensors`, laid out the other way on both counts that matter: element ``2j``
is the *lower* nibble, and the block-scale grid is row-major, ``grid[m, k]`` scaling elements ``16k`` to
``16k + 15`` of row ``m``. Both were read off BFL's file against Comfy-Org's repack of it. Since nothing here is
tiled, :func:`nvfp4_weight` states what such a layer means without restating any permutation.
"""
return {
f"{path}.weight": (codes[:, 1::2] << 4) | codes[:, 0::2],
f"{path}.weight_scale": grid.to(torch.float8_e4m3fn),
f"{path}.weight_scale_2": torch.tensor(global_scale),
}
def nvfp4_weight(positive: torch.Tensor, grid: torch.Tensor, global_scale: float) -> torch.Tensor:
"""What a layer of :func:`nvfp4_codes` means under a row-major ``grid``: +-1 times its block's scale times the
global scale. Exact in float32 when the scales are powers of two. For ComfyUI's layout, store the grid as
``stored_layout(grid)``."""
return torch.where(positive, 1.0, -1.0) * grid.repeat_interleave(16, dim=1) * global_scale
def stored_layout(grid: torch.Tensor) -> torch.Tensor:
"""Lay a row-major grid out the way checkpoints store it.
By the measured index formula: element ``[m, k]`` lands at flat position ``position``. Shared
with the nvfp4 and MXFP8 decode tests so both check against the same independent statement of
the layout rather than against each other.
"""
rows, blocks = grid.shape
flat = torch.full((rows * blocks,), float("nan"), dtype=grid.dtype)
# The formula over every (m, k) at once rather than in a Python loop, which a model-sized fixture would wait on.
m = torch.arange(rows).unsqueeze(1)
k = torch.arange(blocks).unsqueeze(0)
position = ((((m // 128) * (blocks // 4) + k // 4) * 32 + m % 32) * 4 + (m % 128) // 32) * 4 + k % 4
flat[position.flatten()] = grid.flatten()
return flat.reshape(rows, blocks)
def mxfp8_tensors(
path: str, exponents: torch.Tensor, *, block_size: int = MX_BLOCK_SIZE
) -> tuple[dict[str, torch.Tensor], torch.Tensor]:
"""One MXFP8 layer in checkpoint layout, and the block-wise float scale it decodes to.
This builder existed already, private to `tests/backend/quantization/test_fp8_scaled.py`, which
now imports it from here instead. That is the whole of the change: MXFP8 was tested, it just was
not available to the other suites, so a seam-level cell had to hand-roll the layout -- and the
layout is the part worth getting from one place, because getting it wrong is silent.
The grid is E8M0 exponent *bytes*, so 127 means `2**0`; folded as linear multipliers the weights
come out around 127x too large, at the right shape and the right dtype, with nothing raised and
nothing logged. And it is stored in cuBLAS tiles (:func:`stored_layout`), because that is how
both published builds store it; read row-major it pairs blocks with the wrong rows.
The weight is all ones, so the returned scale *is* the expected decoded weight -- an expectation
that moves by `2**(byte - 127)` per block if either half of the decode is skipped. It is
independent of the decode: the tile order comes from the hand-written index formula in
:func:`stored_layout`, and the bias is the spec's 127 rather than `_E8M0_BIAS`.
"""
rows, blocks = exponents.shape
# `stored_layout` is a bijection only over whole tiles. Off-tile, its `flat` keeps NaNs, and
# `.to(torch.uint8)` turns those into zeros -- every scale silently becomes `2**-127`.
check_tile_layout(rows, blocks)
tensors = {
f"{path}.weight": torch.ones(rows, blocks * block_size).to(torch.float8_e4m3fn),
f"{path}.weight_scale": stored_layout(exponents.to(torch.float64)).to(torch.uint8),
}
return tensors, torch.exp2(exponents.float() - 127)
def mxfp8_marker(*, block_size: int = MX_BLOCK_SIZE) -> dict[str, object]:
"""What a layer has to say about itself before the decode will read its grid.
A `uint8` tensor beside an fp8 weight is otherwise just an unknown producer's convention. The
format string is spelled out rather than imported from `fp8_scaled`: it is what the *producer*
writes, so a rename on our side has to be caught by a test, not mirrored by one.
"""
return {"format": "mxfp8", "block_size": block_size}
def q8_cr_marker(*, group_size: int = CONVROT_GROUP_SIZE, **overrides: Any) -> dict[str, Any]:
"""The JSON ComfyUI-GGUF's converter stores per ``Q8_CR`` weight (``quantize_int8_convrot`` in
github.com/molbal/ComfyUI-GGUF ``tools/convert.py``). Unlike a safetensors ``.comfy_quant`` it says
``weight_rotated`` explicitly; that converter's loader runs a ``convrot`` layer without it unrotated.
"""
return {
"format": "int8_tensorwise",
"convrot": True,
"weight_rotated": True,
"per_row": True,
"convrot_groupsize": group_size,
**overrides,
}
# gguf-py can read K-quants but not write them. A super-block's bytes are all valid codes, so random ones with sane
# fp16 super-scales make a real Q4_K tensor; what it means comes from gguf-py's reference reader, not from us.
_Q4_K_BLOCK_BYTES = 144 # d (fp16), dmin (fp16), 12 bytes of 6-bit scales and mins, 128 bytes of 4-bit codes
_Q4_K_SUPER_SCALE = np.float16(1e-3).tobytes()
def random_q4_k(shape: tuple[int, ...], rng: np.random.Generator) -> np.ndarray:
"""A packed ``Q4_K`` tensor of logical ``shape`` (rows, cols), cols a multiple of 256.
The super-scales are set in *every* block -- the 3-D view guarantees it -- since a random one decodes to
inf or NaN, which an equality check then skips silently.
"""
rows, cols = shape
blocks = rng.integers(0, 256, size=(rows, cols // 256, _Q4_K_BLOCK_BYTES), dtype=np.uint8)
blocks[..., 0:2] = np.frombuffer(_Q4_K_SUPER_SCALE, dtype=np.uint8)
blocks[..., 2:4] = np.frombuffer(_Q4_K_SUPER_SCALE, dtype=np.uint8)
return blocks.reshape(rows, -1)
def write_gguf(
path: Any,
tensors: Mapping[str, torch.Tensor],
*,
quant: Mapping[str, Mapping[str, Any] | str] | None = None,
q8_0: Mapping[str, torch.Tensor] | None = None,
architecture: str = "krea2",
metadata: Mapping[str, str] | None = None,
) -> None:
"""Write a GGUF the way ComfyUI-GGUF lays out a ``Q8_CR`` file.
``tensors`` are stored by dtype: int8 as GGML ``I8`` (how the converter stores Q8_CR codes), float32
as ``F32``, float16 as ``F16``. ``quant`` maps a weight name to its JSON, written as the string
``comfy.gguf.quant.<weight name>`` (a string is written as it is, for malformed metadata). ``q8_0`` tensors are GGML-quantized, which is what the converter's
target-size plans put beside Q8_CR layers in the same file. ``metadata`` adds string keys, such as the
``model_version`` some converters carry over from the source file's header.
"""
import gguf
qtypes = {
torch.int8: gguf.GGMLQuantizationType.I8,
torch.float32: gguf.GGMLQuantizationType.F32,
torch.float16: gguf.GGMLQuantizationType.F16,
}
writer = gguf.GGUFWriter(str(path), architecture)
for key, value in (metadata or {}).items():
writer.add_string(key, value)
for name, marker in (quant or {}).items():
writer.add_string(f"comfy.gguf.quant.{name}", marker if isinstance(marker, str) else json.dumps(dict(marker)))
for name, tensor in tensors.items():
writer.add_tensor(name, tensor.numpy(), raw_dtype=qtypes[tensor.dtype])
for name, tensor in (q8_0 or {}).items():
data = tensor.to(torch.float32).numpy()
packed = gguf.quants.quantize(data, gguf.GGMLQuantizationType.Q8_0)
writer.add_tensor(name, packed, raw_dtype=gguf.GGMLQuantizationType.Q8_0)
writer.write_header_to_file()
writer.write_kv_data_to_file()
writer.write_tensors_to_file()
writer.close()