File size: 6,954 Bytes
597f7c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Test-only loaders for alternate Qwen3-VL text-encoder artifacts.

The released mixed NVFP4/FP8 loader remains immutable.  This module reuses its
validated tensor mapping and module implementation while accepting the
ComfyUI scaled-FP8 policy (252 language projections, all FP8 E4M3).
"""

from __future__ import annotations

from collections import Counter
from pathlib import Path
from typing import Any

import torch
from accelerate import init_empty_weights
from safetensors import safe_open
from transformers import AutoConfig, AutoProcessor, AutoTokenizer


EXPECTED_FP8_SHA256 = (
    "54bd5144df0bbc25dd6ccadfcb826b521445a1b06ae5a42570bdd2974ca87094"
)
EXPECTED_FP8_PROJECTION_COUNT = 252


def _base_loader() -> Any:
    import quant_text_encoder

    return quant_text_encoder


def _install_scaled_fp8_linears(
    hf_module: torch.nn.Module,
    artifact_path: Path,
) -> dict[str, Any]:
    base = _base_loader()
    format_counts: Counter[str] = Counter()
    installed: list[str] = []
    storage_bytes = 0
    original_bf16_bytes = 0
    full_precision_matrix_mult_counts: Counter[bool] = Counter()

    with safe_open(str(artifact_path), framework="pt", device="cpu") as handle:
        config_keys = sorted(
            key for key in handle.keys() if key.endswith(".comfy_quant")
        )
        for config_key in config_keys:
            layer_key = config_key.removesuffix(".comfy_quant")
            quant_config = base._decode_quant_config(
                handle.get_tensor(config_key)
            )
            quant_format = quant_config["format"]
            if quant_format != "float8_e4m3fn":
                raise RuntimeError(
                    f"{layer_key}: expected float8_e4m3fn, got {quant_format}"
                )
            full_precision_matrix_mult_counts[
                bool(quant_config.get("full_precision_matrix_mult", False))
            ] += 1
            module_name = base._artifact_layer_to_hf_module(layer_key)
            parent, leaf = base._resolve_parent(hf_module, module_name)
            original = getattr(parent, leaf)
            if not isinstance(original, torch.nn.Linear):
                raise TypeError(
                    f"{module_name}: expected torch.nn.Linear, got "
                    f"{type(original).__name__}"
                )
            if original.bias is not None:
                raise ValueError(f"{module_name}: quantized projection has a bias")

            replacement = base.PublishedQuantLinear(
                in_features=original.in_features,
                out_features=original.out_features,
                quant_format=quant_format,
                qdata=handle.get_tensor(f"{layer_key}.weight"),
                weight_scale=handle.get_tensor(f"{layer_key}.weight_scale"),
                weight_scale_2=None,
            )
            setattr(parent, leaf, replacement)
            format_counts[quant_format] += 1
            installed.append(module_name)
            original_bf16_bytes += (
                original.in_features * original.out_features * 2
            )
            storage_bytes += replacement.qdata.nbytes
            storage_bytes += replacement.weight_scale.nbytes

    summary = {
        "installed_module_count": len(installed),
        "format_counts": dict(format_counts),
        "full_precision_matrix_mult_counts": {
            str(key).lower(): value
            for key, value in sorted(
                full_precision_matrix_mult_counts.items()
            )
        },
        "original_projection_bf16_bytes": original_bf16_bytes,
        "packed_projection_bytes": storage_bytes,
        "projection_saving_bytes": original_bf16_bytes - storage_bytes,
        "projection_saving_gib": (
            (original_bf16_bytes - storage_bytes) / float(1 << 30)
        ),
    }
    if (
        summary["installed_module_count"] != EXPECTED_FP8_PROJECTION_COUNT
        or summary["format_counts"]
        != {"float8_e4m3fn": EXPECTED_FP8_PROJECTION_COUNT}
        or summary["full_precision_matrix_mult_counts"] != {"false": 252}
    ):
        raise RuntimeError(f"unexpected scaled-FP8 policy: {summary}")
    return summary


def load_scaled_fp8_text_encoder(
    *,
    text_encoder_dir: str | Path,
    artifact_path: str | Path,
    tokenizer_max_length: int,
    dit_structure: dict[str, Any],
    use_packed_text_infer: bool,
    attn_type: str = "flash2",
) -> tuple[torch.nn.Module, dict[str, Any]]:
    """Build Mage's Qwen wrapper from the 252-projection scaled-FP8 file."""

    base = _base_loader()
    from mage_flow.models.modules.text_encoder import (
        CustomQwen3VLForConditionalGeneration,
        TextEncoder,
        _resolve_hf_attn_impl,
    )

    text_encoder_dir = Path(text_encoder_dir).resolve()
    artifact_path = Path(artifact_path).resolve()
    actual_sha256 = base.sha256(artifact_path)
    if actual_sha256 != EXPECTED_FP8_SHA256:
        raise RuntimeError(
            "scaled-FP8 text artifact SHA-256 mismatch: "
            f"{actual_sha256}"
        )

    config = AutoConfig.from_pretrained(
        str(text_encoder_dir),
        local_files_only=True,
    )
    hf_attn_implementation = _resolve_hf_attn_impl(attn_type)
    with init_empty_weights():
        hf_module = CustomQwen3VLForConditionalGeneration._from_config(
            config,
            attn_implementation=hf_attn_implementation,
            dtype=torch.bfloat16,
        )

    quant_summary = _install_scaled_fp8_linears(
        hf_module,
        artifact_path,
    )
    load_summary = base._load_nonquantized_weights(
        hf_module,
        artifact_path,
    )

    text_encoder = TextEncoder.__new__(TextEncoder)
    torch.nn.Module.__init__(text_encoder)
    text_encoder.model_name = str(text_encoder_dir)
    text_encoder.tokenizer_max_length = int(tokenizer_max_length)
    text_encoder.tokenizer = AutoTokenizer.from_pretrained(
        str(text_encoder_dir),
        local_files_only=True,
    )
    text_encoder.tokenizer.padding_side = "right"
    text_encoder.processor = AutoProcessor.from_pretrained(
        str(text_encoder_dir),
        local_files_only=True,
    )
    text_encoder.hf_module = hf_module.eval().requires_grad_(False)
    text_encoder.prompt_template_encode = ""
    text_encoder.prompt_template_encode_start_idx = 0
    text_encoder.dit_structure = dict(dit_structure)
    text_encoder.use_packed_text_infer = bool(use_packed_text_infer)
    text_encoder.eval().requires_grad_(False)

    return (
        text_encoder,
        {
            "artifact": str(artifact_path),
            "artifact_sha256": actual_sha256,
            "attention_backend": attn_type,
            "hf_attention_implementation": hf_attn_implementation,
            "quantized": quant_summary,
            "nonquantized": load_summary,
        },
    )


__all__ = [
    "EXPECTED_FP8_PROJECTION_COUNT",
    "EXPECTED_FP8_SHA256",
    "load_scaled_fp8_text_encoder",
]