[diffusion] fix: do not interleave comfy-kitchen MXFP8 scales twice (#38028)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Tianyang Gu
2026-10-07 17:56:39 +08:00
committed by GitHub
co-authored by Claude Fable 5.1
parent c389cbaf99
commit aa5551d9b6
2 changed files with 107 additions and 3 deletions
@@ -21,6 +21,7 @@ from sglang.srt.hardware_backend.npu.quantization.linear_method_npu import (
)
from sglang.srt.layers.quantization.fp8 import Fp8Config as SRTFp8Config
from sglang.srt.layers.quantization.fp8 import Fp8LinearMethod as SRTFp8LinearMethod
from sglang.srt.layers.utils import copy_or_rebind_param
class MXFP8Config(SRTFp8Config, QuantizationConfig):
@@ -82,7 +83,45 @@ class MXFP8Config(SRTFp8Config, QuantizationConfig):
)
return MXFP8OnlineLinearMethod(self)
return SRTFp8LinearMethod(self)
return ComfyMXFP8LinearMethod(self)
__all__ = ["MXFP8Config"]
class ComfyMXFP8LinearMethod(SRTFp8LinearMethod):
"""Load MXFP8 scales that the checkpoint already stores swizzled.
``comfy-kitchen`` serializes MXFP8 block scales in the ``SWIZZLE_32_4_4``
byte order that FlashInfer's cutlass and cutedsl kernels consume, but
safetensors keeps their logical ``[N, K // 32]`` shape. Nothing in the file
distinguishes those bytes from row-major scales, so
:meth:`Fp8LinearMethod._process_mxfp8_linear_weight_scale` interleaves them a
second time. The second permutation is silent -- the checkpoint loads, the
kernels run at full speed, and the sample decodes to noise.
Only checkpoints that carry comfy layer markers take this path;
:class:`MXFP8Config` resolved from a ``config.json`` has
``layer_markers is None`` and keeps SRT's behaviour byte for byte.
"""
def _process_mxfp8_linear_weight_scale(
self, layer: torch.nn.Module, scale_u8: torch.Tensor | None = None
) -> None:
backend = self.mxfp8_dense_backend
# an explicit scale_u8 is converted from block-FP8 (row-major) and needs SRT's interleave
if (
scale_u8 is None
and self.use_mxfp8
and self.quant_config.layer_markers is not None
and backend is not None
and (backend.is_flashinfer_cutlass() or backend.is_flashinfer_cutedsl())
):
# Hand FlashInfer the serialized bytes flattened, not re-interleaved.
copy_or_rebind_param(
layer,
"weight_scale_inv_swizzled",
layer.weight_scale_inv.data.contiguous().view(-1),
)
return
super()._process_mxfp8_linear_weight_scale(layer, scale_u8=scale_u8)
__all__ = ["ComfyMXFP8LinearMethod", "MXFP8Config"]
@@ -84,7 +84,10 @@ from sglang.multimodal_gen.runtime.layers.quantization.modelopt_quant import (
ModelOptFp8LinearMethod,
_prepare_nvfp4_weight_bytes,
)
from sglang.multimodal_gen.runtime.layers.quantization.mxfp8 import MXFP8Config
from sglang.multimodal_gen.runtime.layers.quantization.mxfp8 import (
ComfyMXFP8LinearMethod,
MXFP8Config,
)
from sglang.multimodal_gen.runtime.loader.component_loaders import transformer_loader
from sglang.multimodal_gen.runtime.loader.component_loaders.transformer_loader import (
TransformerLoader,
@@ -1195,6 +1198,68 @@ class TestTransformerQuantHelpers(unittest.TestCase):
self.assertIsInstance(method, NPUMXFP8LinearMethod)
def test_comfy_mxfp8_scales_are_not_interleaved_twice(self):
config = MXFP8Config(
is_checkpoint_fp8_serialized=True,
layer_markers={"blocks.0.attn.out_proj": {"format": "mxfp8"}},
)
method = config.get_quant_method(
LinearBase(input_size=64, output_size=32), "blocks.0.attn.out_proj"
)
self.assertIsInstance(method, ComfyMXFP8LinearMethod)
self.assertIsInstance(method, SRTFp8LinearMethod)
# Renaming the SRT hook would silently restore the double interleave.
self.assertTrue(
hasattr(SRTFp8LinearMethod, "_process_mxfp8_linear_weight_scale")
)
checkpoint_scale = torch.arange(64, dtype=torch.uint8).reshape(32, 2)
layer = torch.nn.Module()
layer.weight_scale_inv = torch.nn.Parameter(
checkpoint_scale.clone(), requires_grad=False
)
method.mxfp8_dense_backend = SimpleNamespace(
is_flashinfer_cutlass=lambda: True,
is_flashinfer_cutedsl=lambda: False,
)
method._process_mxfp8_linear_weight_scale(layer)
self.assertEqual(layer.weight_scale_inv_swizzled.shape, torch.Size([64]))
torch.testing.assert_close(
layer.weight_scale_inv_swizzled.data, checkpoint_scale.flatten()
)
# scales converted from block-FP8 are row-major, so SRT still interleaves them
converted = torch.zeros(32, 2, dtype=torch.uint8)
with patch.object(
SRTFp8LinearMethod, "_process_mxfp8_linear_weight_scale"
) as srt_hook:
method._process_mxfp8_linear_weight_scale(layer, scale_u8=converted)
srt_hook.assert_called_once_with(layer, scale_u8=converted)
def test_config_driven_mxfp8_keeps_srt_scale_processing(self):
config = MXFP8Config(is_checkpoint_fp8_serialized=True)
method = config.get_quant_method(
LinearBase(input_size=64, output_size=32), "blocks.0.attn.out_proj"
)
self.assertIsNone(config.layer_markers)
self.assertIsInstance(method, ComfyMXFP8LinearMethod)
layer = torch.nn.Module()
layer.weight_scale_inv = torch.nn.Parameter(
torch.arange(64, dtype=torch.uint8).reshape(32, 2), requires_grad=False
)
method.mxfp8_dense_backend = SimpleNamespace(
is_flashinfer_cutlass=lambda: True,
is_flashinfer_cutedsl=lambda: False,
)
with patch.object(
SRTFp8LinearMethod, "_process_mxfp8_linear_weight_scale"
) as srt_hook:
method._process_mxfp8_linear_weight_scale(layer)
srt_hook.assert_called_once_with(layer, scale_u8=None)
def test_comfy_full_precision_fp8_dequantizes_before_linear(self):
layer = torch.nn.Module()
layer.weight = torch.nn.Parameter(