Skip to content

Commit 344b439

Browse files
authored
Support asym w4a8_int (Comfy-Org#15308)
* support asym w4a8_int * Simplify * Fixes
1 parent 93cb5ed commit 344b439

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

comfy/ops.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,26 @@ def pop_scale(name, dtype=None):
12111211
"quant_group_size": 64,
12121212
"linear_dtype": layer_conf.get("linear_dtype", params_conf.get("linear_dtype", "int4")),
12131213
}
1214+
elif module.quant_format == "asym_w4a8_int8":
1215+
# int4 weight (packed int8 [N,K/2]) + fp8 per-group scale (weight_s_rel),
1216+
# fp32 per-channel scale (weight_s_channel) + optional Lloyd-Max codebook.
1217+
scale = pop_scale("weight_s_rel")
1218+
if scale is None:
1219+
raise ValueError(f"Missing W4A8 group scale (weight_s_rel) for layer {layer_name}")
1220+
if scale.dtype == torch.uint8:
1221+
scale = scale.view(torch.float8_e4m3fn)
1222+
params_conf = layer_conf.get("params", {})
1223+
if not isinstance(params_conf, dict):
1224+
params_conf = {}
1225+
scales = {
1226+
"scale": scale,
1227+
"s_channel": pop_scale("weight_s_channel"),
1228+
"codebook": pop_scale("weight_codebook"),
1229+
"group_size": int(layer_conf.get("group_size", params_conf.get("group_size", 16))),
1230+
"convrot_groupsize": int(
1231+
layer_conf.get("convrot_groupsize", params_conf.get("convrot_groupsize", 256))
1232+
),
1233+
}
12141234
else:
12151235
raise ValueError(f"Unsupported quantization format: {module.quant_format}")
12161236

@@ -1262,6 +1282,9 @@ def _quantized_weight_state_dict(module, sd, prefix, extra_quant_conf=None, extr
12621282
linear_dtype = getattr(params, "linear_dtype", "int4")
12631283
if linear_dtype != "int4":
12641284
quant_conf["linear_dtype"] = linear_dtype
1285+
elif module.quant_format == "asym_w4a8_int8":
1286+
quant_conf["group_size"] = getattr(params, "group_size", 16)
1287+
quant_conf["convrot_groupsize"] = getattr(params, "convrot_groupsize", 256)
12651288
if extra_quant_conf:
12661289
quant_conf.update(extra_quant_conf)
12671290
sd[f"{prefix}comfy_quant"] = torch.tensor(list(json.dumps(quant_conf).encode("utf-8")), dtype=torch.uint8)

comfy/quant_ops.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def _rocm_kitchen_arch_supported():
2828
TensorCoreNVFP4Layout as _CKNvfp4Layout,
2929
TensorCoreConvRotW4A4Layout as _CKTensorCoreConvRotW4A4Layout,
3030
TensorWiseINT8Layout as _CKTensorWiseINT8Layout,
31+
AsymW4A8Int8Layout as _CKAsymW4A8Int8Layout,
3132
register_layout_op,
3233
register_layout_class,
3334
get_layout_class,
@@ -83,6 +84,9 @@ class _CKTensorWiseINT8Layout:
8384
class _CKTensorCoreConvRotW4A4Layout:
8485
pass
8586

87+
class _CKAsymW4A8Int8Layout:
88+
pass
89+
8690
def register_layout_class(name, cls):
8791
pass
8892

@@ -212,7 +216,7 @@ class TensorCoreFP8E5M2Layout(_TensorCoreFP8LayoutBase):
212216
TensorCoreFP8Layout = TensorCoreFP8E4M3Layout
213217
TensorWiseINT8Layout = _CKTensorWiseINT8Layout
214218
TensorCoreConvRotW4A4Layout = _CKTensorCoreConvRotW4A4Layout
215-
219+
AsymW4A8Int8Layout = _CKAsymW4A8Int8Layout
216220

217221
# ==============================================================================
218222
# Registry
@@ -226,6 +230,7 @@ class TensorCoreFP8E5M2Layout(_TensorCoreFP8LayoutBase):
226230
register_layout_class("TensorCoreConvRotW4A4Layout", _CKTensorCoreConvRotW4A4Layout)
227231
if _CK_MXFP8_AVAILABLE:
228232
register_layout_class("TensorCoreMXFP8Layout", TensorCoreMXFP8Layout)
233+
register_layout_class("AsymW4A8Int8Layout", _CKAsymW4A8Int8Layout)
229234

230235
QUANT_ALGOS = {
231236
"float8_e4m3fn": {
@@ -268,6 +273,13 @@ class TensorCoreFP8E5M2Layout(_TensorCoreFP8LayoutBase):
268273
"quantize_input": False,
269274
}
270275

276+
QUANT_ALGOS["asym_w4a8_int8"] = {
277+
"storage_t": torch.int8,
278+
"parameters": {"weight_scale"},
279+
"comfy_tensor_layout": "AsymW4A8Int8Layout",
280+
"quantize_input": False,
281+
}
282+
271283

272284
# ==============================================================================
273285
# Re-exports for backward compatibility
@@ -282,6 +294,7 @@ class TensorCoreFP8E5M2Layout(_TensorCoreFP8LayoutBase):
282294
"TensorCoreNVFP4Layout",
283295
"TensorCoreConvRotW4A4Layout",
284296
"TensorWiseINT8Layout",
297+
"AsymW4A8Int8Layout",
285298
"QUANT_ALGOS",
286299
"register_layout_op",
287300
]

0 commit comments

Comments
 (0)