File size: 2,789 Bytes
6febba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared helpers for ComfyUI ``.comfy_quant`` pre-quantized checkpoint specs.

Each quantized module in a ComfyUI checkpoint carries a tiny sibling tensor
``<module>.comfy_quant`` holding the uint8 bytes of a JSON object that declares the
module's quantization format. This module only decodes the JSON and classifies
formats so callers can route a checkpoint to the right loader; format-specific
validation and conversion live with the loaders (``convrot_int8_utils`` for ConvRot
INT8, ``nvfp4_utils`` for NVFP4 + per-row INT8 embeddings).
"""

import json
from collections.abc import Iterable
from pathlib import Path
from typing import Set, Union

import torch

from musubi_tuner.utils.safetensors_utils import MemoryEfficientSafeOpen

COMFY_QUANT_SUFFIX = ".comfy_quant"
COMFY_WEIGHT_SCALE_SUFFIX = ".weight_scale"

# canonical labels returned by classify_comfy_quant_spec / detect_comfy_quant_formats
FORMAT_CONVROT_INT8 = "convrot_int8"
FORMAT_INT8_TENSORWISE = "int8_tensorwise"
FORMAT_NVFP4 = "nvfp4"


def decode_comfy_quant_spec(key: str, tensor: torch.Tensor) -> dict:
    """Decode a ``.comfy_quant`` tensor (uint8 bytes of JSON) into a dict, format-agnostic."""
    if tensor.dtype != torch.uint8 or tensor.ndim != 1:
        raise ValueError(f"Invalid comfy_quant tensor for {key}: expected 1D uint8, got {tensor.dtype} ndim={tensor.ndim}")
    try:
        spec = json.loads(bytes(tensor.tolist()).decode("utf-8"))
    except (UnicodeDecodeError, json.JSONDecodeError) as e:
        raise ValueError(f"Invalid comfy_quant JSON for {key}: {e}") from e
    if not isinstance(spec, dict):
        raise ValueError(f"Invalid comfy_quant spec for {key}: expected a JSON object, got {type(spec).__name__}")
    return spec


def classify_comfy_quant_spec(spec: dict) -> str:
    """Map a decoded spec to a canonical format label (unknown formats pass through as-is)."""
    quant_format = spec.get("format")
    if quant_format == FORMAT_INT8_TENSORWISE and spec.get("convrot"):
        return FORMAT_CONVROT_INT8
    return str(quant_format)


def detect_comfy_quant_formats(files: Iterable[Union[str, Path]], *, disable_numpy_memmap: bool = False) -> Set[str]:
    """Collect the canonical format labels a checkpoint declares (empty set: not pre-quantized).

    Routing only — the spec tensors are tiny, so reading them here is cheap; per-format
    validation happens again inside the selected loader.
    """
    formats: Set[str] = set()
    for file in files:
        with MemoryEfficientSafeOpen(str(file), disable_numpy_memmap=disable_numpy_memmap) as f:
            for key in f.keys():
                if key.endswith(COMFY_QUANT_SUFFIX):
                    formats.add(classify_comfy_quant_spec(decode_comfy_quant_spec(key, f.get_tensor(key))))
    return formats