Skip to content

Fix import for torch>=2.12: quantize_pt2e removed#289

Open
beausifutmez wants to merge 1 commit into
ROCm:masterfrom
beausifutmez:fix-quantize-pt2e-torch212
Open

Fix import for torch>=2.12: quantize_pt2e removed#289
beausifutmez wants to merge 1 commit into
ROCm:masterfrom
beausifutmez:fix-quantize-pt2e-torch212

Conversation

@beausifutmez

@beausifutmez beausifutmez commented Jun 29, 2026

Copy link
Copy Markdown

Fix import torch_migraphx on torch >= 2.12 (quantize_pt2e removed)

Problem

import torch_migraphx fails for all users on recent PyTorch, not only those doing quantization:

File ".../fx/converters/quant_ops_converters.py", line 49, in <module>
    import torch.ao.quantization.quantize_pt2e
ModuleNotFoundError: No module named 'torch.ao.quantization.quantize_pt2e'

The module was deleted in pytorch/pytorch#169151
("Delete pt2e flow… migrated to torchao", merged 0e38e02), which ships in the torch 2.9+ line —
matching the report in #279 (torch 2.9.1) and reproduced here on torch 2.12.1.

Note the import exists purely for a side effect: registering the torch.ops.quantized_decomposed
ops used by the converter decorators just below it. Guarding the import alone is not enough — the
module then fails at decorator time with
'_OpNamespace' 'quantized_decomposed' object has no attribute 'quantize_per_tensor'.

Fix

The two layers PyTorch split apart matter here:

  • The pt2e flow (prepare_pt2e/convert_pt2e) moved to torchao — that's what #169151 deleted.
  • The quantized_decomposed ops remain in torch core, registered by
    torch.ao.quantization.fx._decomposed.

This import only needs the latter, so pointing it at fx._decomposed is the canonical, in-core,
zero-dependency registrar (not a workaround). Guarded for back-compat with torch < 2.12:

# Import required to populate torch.ops.quantized_decomposed.
try:
    import torch.ao.quantization.quantize_pt2e        # torch < 2.12
except ModuleNotFoundError:
    import torch.ao.quantization.fx._decomposed       # torch >= 2.12

One file changed; no behavioral change on older torch (it takes the try path unchanged).

Verification

Verified on torch 2.12.1+rocm7.2 (HIP 7.2), ROCm 7.2.4, Radeon RX 7900 XTX (gfx1100), Linux:

  • import torch_migraphx succeeds; the migraphx dynamo backend loads.
  • torch.ops.quantized_decomposed is populated (quantize_per_tensor etc. present).
  • torch.compile(model, backend="migraphx") runs a conv+linear model and ResNet-50, output matching
    eager to fp32 epsilon (max |Δ| ≈ 1.2e-7).

fx._decomposed has existed across the affected range, so the same guard covers the torch 2.9.1
case reported in #279 (Windows / gfx1151) as well as torch 2.12 here.

Scope

This restores import + the existing quantized_decomposed converters only. Fully migrating
torch_migraphx's pt2e quantization flow to torchao is a separate, larger change and out of scope here.

Fixes #279

torch.ao.quantization.quantize_pt2e was removed in torch 2.12 (PT2E quant
migrated to torchao), so `import torch_migraphx` fails for all users on recent
torch, not only those using pt2e quantization.

The import's purpose is its side effect: populating torch.ops.quantized_decomposed
(used by the converter decorators below). In torch>=2.12 those ops are registered
by torch.ao.quantization.fx._decomposed instead. Guard the import to use the new
module when the old one is absent, keeping back-compat with torch<2.12.

Verified on torch 2.12.1+rocm7.2 / gfx1100: import succeeds, the migraphx dynamo
backend loads, torch.ops.quantized_decomposed is populated, and
torch.compile(backend="migraphx") matches eager to fp32 epsilon.

Fixes ROCm#279

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: torch.ao.quantization.quantize_pt2e is deprecated and will be migrated to torchao

1 participant