Skip to content

Conversation

@metascroy
Copy link
Contributor

Summary

Migrate CoreMLQuantizer to use observer/fake-quantize primitives from torchao instead of torch.ao.quantization.

Background

The files in backends/apple/coreml/quantizer/ were initially copied from the corresponding quantizer files in coremltools. This PR updates those files to use the new torchao quantization primitives.

Motivation

The quantization primitives in torch.ao.quantization are being deprecated in favor of the torchao library. This PR updates the CoreML backend's quantizer to use the new torchao.quantization.pt2e APIs for observer and fake-quantize operations, ensuring compatibility with the PyTorch ecosystem going forward.

Changes

_annotation_config.py

Updated imports to use torchao.quantization.pt2e:

  • FakeQuantize from torchao.quantization.pt2e.fake_quantize
  • Observers (MinMaxObserver, MovingAverageMinMaxObserver, PerChannelMinMaxObserver, MovingAveragePerChannelMinMaxObserver) from torchao.quantization.pt2e.observer
  • QuantizationSpec from torchao.quantization.pt2e.quantizer

_coreml_quantizer.py

Updated imports to use:

  • Quantizer base class from torchao.quantization.pt2e.quantizer.quantizer
  • get_module_name_filter from torchao.quantization.pt2e.quantizer.utils

_coreml_quantizer_utils.py

Updated imports and usage:

  • QuantizationAnnotation, QuantizationSpec, QuantizationSpecBase, SharedQuantizationSpec, FixedQParamsQuantizationSpec, and Q_ANNOTATION_KEY from torchao.quantization.pt2e.quantizer.quantizer
  • get_module_name_filter from torchao.quantization.pt2e.quantizer.utils
  • _get_aten_graph_module_for_pattern from torchao.quantization.pt2e.utils
  • Replaced all hardcoded "quantization_annotation" strings with Q_ANNOTATION_KEY constant for consistency

test_coreml_quantizer.py

Updated test imports:

  • prepare_pt2e, prepare_qat_pt2e, convert_pt2e from torchao.quantization.pt2e.quantize_pt2e
  • FakeQuantizeBase from torchao.quantization.pt2e.fake_quantize
  • Added test_fake_quantize_modules_inserted_after_prepare test to verify FakeQuantizeBase modules are correctly inserted after the prepare step

Note

Configuration classes (LinearQuantizerConfig, ModuleLinearQuantizerConfig, QuantizationScheme) remain imported from coremltools.optimize.torch as they are config-level abstractions that don't depend on the deprecated torch.ao primitives.

Test Plan

  • Added new test test_fake_quantize_modules_inserted_after_prepare that verifies FakeQuantizeBase modules from torchao are correctly inserted after both prepare_pt2e (PTQ) and prepare_qat_pt2e (QAT) steps
  • Existing tests (test_conv_relu, test_linear) continue to pass, validating end-to-end quantization flow with the new torchao primitives
pytest backends/apple/coreml/test/test_coreml_quantizer.py -v

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 6, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16473

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures, 1 Unrelated Failure

As of commit 24d83be with merge base 913436a (image):

NEW FAILURES - The following jobs have failed:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 6, 2026
@metascroy metascroy requested a review from jerryzh168 January 6, 2026 20:20
@github-actions
Copy link

github-actions bot commented Jan 6, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@meta-codesync
Copy link

meta-codesync bot commented Jan 6, 2026

@metascroy has imported this pull request. If you are a Meta employee, you can view this in D90200393.


from torchao.quantization.pt2e.fake_quantize import FakeQuantize as _FakeQuantize

from torchao.quantization.pt2e.observer import (
Copy link
Contributor

@jerryzh168 jerryzh168 Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better for these to continue to use torch.ao since we are planning to deprecate these in torchao/pt2e

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you say more on the deprecation plan?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait, I remember there might be some incompatibilities of the observer in torchao/pt2e v.s. torch.ao

does the previous torch.ao import work for CoreMLQuantizer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try it, I assumed it wouldn't work. In all other quantizers we migrated to use observers in torchao.ao

Were the observers removed from torch.ao?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I think they don't work.

observers are not removed from torch.ao, it's just we'd like to deprecate them together with all the fx / eager flows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated my PR: apple/coremltools#2634, it seems that coreml is currently using the same observer for both fx and pt2e flow, and since torchao pt2e uses a different set of observer/fake_quant, we can't make all tests pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any other concerns with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants