Pick allgather's device from the group's backend string; detect per-channel QKV by layout

- allgather compared the backend to "nccl", so a CUDA-only group whose
  backend reads "cuda:nccl" got CPU tensors. Use CPU only where the
  group's backend takes them (Gloo, MPI); test a cuda:nccl group too.
- A QKV weight quantizer with an axis and no block sizes has per-channel
  scales whatever its label (FP8 axis=(0,) resolves to plain "fp8"), so
  the preflight refuses it before packing. PP=2 case added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
This commit is contained in:
Keval Morabia
2026-12-03 02:59:37 -08:00
co-authored by Claude Opus 5.5
parent ec383e821e
commit f5b54f9264
4 changed files with 40 additions and 12 deletions
@@ -1620,12 +1620,15 @@ class GPTModelExporter:
quantizer, "block_sizes", None
):
unpackable.append(f"{fmt!r} {proj} weights with block scales")
elif proj == "linear_qkv" and fmt in (
QUANTIZATION_INT8_WO,
QUANTIZATION_INT8_SQ,
QUANTIZATION_FP8_PC_PT,
# Per-channel by layout too: an FP8 axis=(0,) weight resolves to plain "fp8".
elif proj == "linear_qkv" and (
fmt in (QUANTIZATION_INT8_WO, QUANTIZATION_INT8_SQ, QUANTIZATION_FP8_PC_PT)
or (
getattr(quantizer, "axis", None) is not None
and not getattr(quantizer, "block_sizes", None)
)
):
unpackable.append(f"{fmt!r} {proj} weights")
unpackable.append(f"{fmt!r} {proj} weights with per-channel scales")
return formats, unpackable
def _weight_layouts(self, module: torch.nn.Module) -> list[tuple[str | None, int, Any]]:
+4 -2
View File
@@ -258,8 +258,10 @@ def allgather(obj: Any, group=None) -> list[Any]:
if size(group) == 1:
return [obj]
# NCCL gathers CUDA tensors; other backends (e.g. Gloo) CPU ones.
device = "cuda" if torch.distributed.get_backend(group) == "nccl" else "cpu"
# CPU tensors where the group's backend takes them (e.g. "gloo", "cpu:gloo,cuda:nccl"), else
# CUDA ones (e.g. "nccl", "cuda:nccl").
group_backend = str(torch.distributed.get_backend(group))
device = "cpu" if "gloo" in group_backend or "mpi" in group_backend else "cuda"
# serialize
tensor = _serialize(obj).to(device)
@@ -147,6 +147,10 @@ def _test_broadcast_allgather_objects(rank, size):
assert out[1] == {"latency": 1.5}
assert out[2].choices == [1, 2, 4]
assert allgather({"rank": rank}) == [{"rank": r} for r in range(size)]
# A CUDA-only group names its backend per device; it must still get CUDA tensors.
cuda_only = torch.distributed.new_group(backend="cuda:nccl")
assert allgather({"rank": rank}, group=cuda_only) == [{"rank": r} for r in range(size)]
torch.distributed.destroy_process_group(cuda_only)
def test_broadcast_allgather_objects(dist_workers):
@@ -1340,6 +1340,19 @@ def test_unified_export_megatron_pp2_mtp_no_duplicate_tensors(dist_workers_size_
dist_workers_size_2.run(partial(_test_export_pp2_mtp_no_duplicate_tensors, tmp_path, model_dir))
# Per-channel FP8 on QKV whose tuple axis resolves to the plain "fp8" label.
_FP8_TUPLE_AXIS_QKV_CFG = {
"quant_cfg": [
{"quantizer_name": "*", "enable": False},
{
"quantizer_name": "*linear_qkv.weight_quantizer",
"cfg": {"num_bits": (4, 3), "axis": (0,)},
},
],
"algorithm": "max",
}
def _test_export_pp2_quant_config_spans_stages(
model_dir, export_dir, stage_cfgs, error, rank, size
):
@@ -1370,13 +1383,13 @@ def _test_export_pp2_quant_config_spans_stages(
transformer_impl="modelopt",
**extra_kwargs,
).cuda()
# A stage is a config, a (config, name) pair to quantize only the matching quantizers, or None
# for none. Weight-only formats: activation calibration would need a pipelined forward. Every
# rank quantizes, as that runs collectives.
# A stage is a config (by name or as a dict), a (config, name) pair to quantize only the
# matching quantizers, or None for none. Weight-only formats: activation calibration would
# need a pipelined forward. Every rank quantizes, as that runs collectives.
stage = stage_cfgs[rank]
spec = stage_cfgs[0] if stage is None else stage
cfg_name, keep = spec if isinstance(spec, tuple) else (spec, "")
mtq.quantize(model, getattr(mtq, cfg_name))
cfg, keep = spec if isinstance(spec, tuple) else (spec, "")
mtq.quantize(model, getattr(mtq, cfg) if isinstance(cfg, str) else cfg)
if keep:
mtq.disable_quantizer(model, lambda name: keep not in name)
if stage is None:
@@ -1405,6 +1418,12 @@ def _test_export_pp2_quant_config_spans_stages(
("INT8_WEIGHT_ONLY_CFG", "W4A16_NVFP4_CFG"),
(NotImplementedError, "int8_wo"),
),
# Detected by layout, not only by label.
(
"llama",
(_FP8_TUPLE_AXIS_QKV_CFG, "W4A16_NVFP4_CFG"),
(NotImplementedError, "per-channel"),
),
# Nor can it split 2-D block scales.
(
"llama",