From 88d17ca30013ce4a78c6a97461c05edfe2e1cc4d Mon Sep 17 00:00:00 2001 From: npow Date: Thu, 27 Aug 2026 05:22:09 +0000 Subject: [PATCH] fix: avoid mandatory torchao dependency for quantized_decomposed registration #290 pointed PyTorch>=2.11 at torchao.quantization.pt2e.quantize_pt2e after torch.ao.quantization.quantize_pt2e was removed upstream (confirmed absent on 2.13.0+rocm7.2). That works, but makes torchao a hard new dependency for what is only a registration side effect (populating torch.ops.quantized_decomposed). torch.ao.quantization.fx._decomposed provides the exact same registration with zero extra dependencies -- confirmed live: torch.ops.quantized_decomposed .quantize_per_tensor is absent before this import and present after, on 2.13.0+rocm7.2 (ROCm/MI300X). It's also where quantize_pt2e itself sourced this from on older PyTorch, so it's a strictly more fundamental import, not a workaround. Practical motivation: torchao ships CUDA-specific compiled extensions (_C_mxfp8, _C_cutlass_90a) that fail to load on a ROCm build with no functional impact, which reads as a confusing/alarming warning for anyone who doesn't already depend on torchao for something else -- pure noise from an otherwise-unrelated import. Prefers the dependency-free path; falls back to the existing #290 behavior (torchao on >=2.11, in-tree quantize_pt2e below that) only if _decomposed is ever unavailable, so this is strictly additive and preserves existing behavior on any PyTorch where the new path doesn't apply. Tested on real MI300X hardware (ROCm 7.2.4, torch 2.13.0+rocm7.2): clean import with no torchao installed, torch.compile(backend="migraphx") compiles and runs correctly (cosine similarity 1.0 vs eager on a transformer-block shaped module). --- .../fx/converters/quant_ops_converters.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/py/torch_migraphx/fx/converters/quant_ops_converters.py b/py/torch_migraphx/fx/converters/quant_ops_converters.py index 54364700..14ccd42e 100644 --- a/py/torch_migraphx/fx/converters/quant_ops_converters.py +++ b/py/torch_migraphx/fx/converters/quant_ops_converters.py @@ -46,11 +46,26 @@ from ..mgx_module import MGXInstruction from torch_migraphx.fx.converters import acc_ops_converters -# Import required to populate torch.ops.quantized_decomposed -if version.parse(torch.__version__) >= version.parse("2.11.dev"): - import torchao.quantization.pt2e.quantize_pt2e -else: - import torch.ao.quantization.quantize_pt2e +# Import required to populate torch.ops.quantized_decomposed. On PyTorch < 2.11 this +# lived at torch.ao.quantization.quantize_pt2e; that module was removed upstream on +# newer PyTorch (confirmed absent on 2.13.0+rocm7.2), and #290 pointed newer PyTorch at +# torchao.quantization.pt2e.quantize_pt2e instead. That works, but pulls in torchao as a +# hard new dependency for what is only a registration side effect here -- and torchao +# ships CUDA-specific compiled extensions (_C_mxfp8, _C_cutlass_90a) that fail to load +# on a ROCm build with no functional impact (they're simply unused), which is confusing +# noise for anyone who doesn't already depend on torchao for something else. The same +# registration is available with zero extra dependencies via +# torch.ao.quantization.fx._decomposed, which is where quantize_pt2e itself sourced it +# from on older PyTorch too (confirmed: torch.ops.quantized_decomposed.quantize_per_tensor +# is absent before this import and present after, on 2.13.0+rocm7.2). Prefer that; only +# reach for torchao if a future PyTorch also drops _decomposed's own registration. +try: + import torch.ao.quantization.fx._decomposed # noqa: F401 +except ModuleNotFoundError: + if version.parse(torch.__version__) >= version.parse("2.11.dev"): + import torchao.quantization.pt2e.quantize_pt2e + else: + import torch.ao.quantization.quantize_pt2e _LOGGER = logging.getLogger(__name__)