coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Removed `torch.ao.quantization.quantizer` imports in coremltools/optimize/torch break on torch 2.11 — request for decision on removal/deprecation of CoreMLQuantizer

Open xyf5432 opened this issue 3 months ago • 4 comments

Summary

Three files under coremltools/optimize/torch/quantization/ import from torch.ao.quantization.quantizer, removed in torch 2.11 (PT2E quantization migrated to torchao):

File Lines
_annotation_config.py 11–13 from torch.ao.quantization.quantizer.quantizer import QuantizationSpec
_coreml_quantizer.py 12–13 from torch.ao.quantization.quantizer.quantizer import Quantizer / ...xnnpack_quantizer import _get_module_name_filter
_coreml_quantizer_utils.py 22–36 (8 lines: QuantizationSpec, xnnpack_quantizer, xnnpack_quantizer_utils imports)

Not wired into the public API, but still used by the test suite, and loading any of them on torch 2.11+ fails immediately: ModuleNotFoundError: No module named 'torch.ao.quantization.quantizer'. Verified with torch 2.11.0 (crash) vs 2.9.1 (control, succeeds).

How to reproduce

pip install torch==2.11.0  # or newer; 2.9.x does not reproduce
pip install coremltools
from coremltools.optimize.torch.quantization._annotation_config import AnnotationConfig
# ModuleNotFoundError: No module named 'torch.ao.quantization.quantizer'

Related discussion: PR #2634

A migration attempt already exists: pytorch/pytorch#169151 removed PT2E quantization from torch, and jerryzh168 (pytorch/ao maintainer) opened apple/coremltools#2634 "Update callsite for pt2e quant" migrating these files to torchao. It was closed without merging (2026-01-24); the stated plan is to remove CoreMLQuantizer from coremltools in favor of the ExecuTorch implementation (executorch#16473). See the PR for details.

Request

Please make a decision on the future of CoreMLQuantizer and act on it:

  1. Remove the three modules (and their tests) as planned in PR #2634, or formally deprecate them with a clear support statement (including a torch < 2.11 requirement or a torchao dependency for torch 2.11+); or
  2. At minimum, keep these files from crashing on torch 2.11+: delete the stale imports or gate them behind a version check with a clear error message pointing to the replacement.

We'd appreciate an explicit statement on the support status, since the current release allows torch 2.11+ (unbounded torch requirement) and ships files that crash on load.

Suggested fix

Given PR #2634, deletion is the preferred direction (migration is blocked by the observer/fake_quant incompatibility noted in the PR discussion). If keeping the modules, the minimal fix is a guarded import:

try:
    from torch.ao.quantization.quantizer.quantizer import (
        QuantizationSpec as _TorchQuantizationSpec,
    )
except ImportError:  # torch>=2.11: PT2E quantization moved to torchao
    from torchao.quantization.pt2e.quantizer.quantizer import (
        QuantizationSpec as _TorchQuantizationSpec,
    )

(torchao.quantization.pt2e.quantizer.quantizer.QuantizationSpec verified present in torchao 0.16.0.)

References

  • apple/coremltools#2634 — "Update callsite for pt2e quant" (closed, migration abandoned)
  • pytorch/executorch#16473 — CoreMLQuantizer moved to ExecuTorch
  • PyTorch migration plan: Torch.ao.quantization Migration Plan — PT2E quantization has been migrated to torchao
  • Deprecation notice in pytorch/pytorch#153892 ("torch.ao.quantization is deprecated")
  • torchao Quantizer API: torchao.quantization.pt2e.quantizer.quantizer.QuantizationSpec

xyf5432 avatar Aug 19 '26 07:08 xyf5432

The max version of PyTorch we support is 2.8. If you have a newer than that, you should get a warning when you import coremltools.

Related: #2615

TobyRoseman avatar Aug 19 '26 21:08 TobyRoseman

Thanks for confirming — the warning does exist in _deps/init.py. However, install_requires has no torch constraint, so pip installs coremltools alongside any torch version, and users only hit the limit at import time. Since this break is guaranteed (module removal, not "unexpected errors"), could you add a packaging constraint (e.g. torch<2.9 in install_requires) so pip prevents the incompatible install up front, and/or state the deprecation plan for CoreMLQuantizer (per #2634)?

xyf5432 avatar Aug 20 '26 02:08 xyf5432

We don't have a packaging constraint for torch because torch is not a dependency for coremltools. You don't need to have torch installed in order to use coremltools. User may want to convert models from other frameworks (ex TensorFlow or XGBoost).

TobyRoseman avatar Aug 20 '26 20:08 TobyRoseman

Thanks — agreed.

The supported range 2.1 – 2.8 is enforced in coremltools/_deps/init.py: _TORCH_MAX_VERSION = "2.8.0" warns at import time when torch is newer, and _CT_OPTIMIZE_TORCH_MIN_VERSION = "2.1.0" disables coremltools.optimize.torch with a warning when torch is older.

The README, the installation guide, and the PyTorch conversion page all omit this range, and the installation guide's "Install Third-party Packages" section is where users try to pick the torch version. Would you consider stating it there, e.g.:

coremltools is tested with PyTorch 2.1 – 2.8. Newer versions are not tested and may run into unexpected errors (a warning is emitted at import time). coremltools.optimize.torch requires PyTorch ≥ 2.1; importing it with an older version is disabled with a warning.

xyf5432 avatar Aug 21 '26 01:08 xyf5432