Skip to content

Arm_inductor_quantizer for Pt2e quantization #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

choudhary-devang
Copy link
Collaborator

@choudhary-devang choudhary-devang commented Apr 28, 2025

Title: Enable PyTorch 2 Export Quantization path for ARM CPUs.

Description:

  • This PR extends the PyTorch 2 Export Quantization (PT2E Quantization) workflow—originally available only on x86 CPUs—to support ARM platforms. PT2E Quantization is an automated, full-graph quantization solution in PyTorch that improves on Eager Mode Quantization by adding support for functionals and automating the overall process. It is part of the torch.ao module and fully supports quantization when using the compile mode.

Key Changes:

  • Introduces ARM-specific support by leveraging oneDNN kernels for matmuls and convolution.

  • Integrates pre-defined configuration selection to automatically choose the best quantization settings based on the selected quantization method.

Provides customization options via two flags:

  • qat_state: Indicates whether to use Quantization Aware Training (if set to True) or Post Training Quantization (if set to False). The default remains False.
  • dynamic_state: Selects between dynamic quantization (if True) and static quantization (if False). The default is also set to False.
    Screenshot 2025-01-22 105543

These options allow users to tailor the quantization process for their specific workload requirements (e.g., using QAT for fine-tuning or PTQ for calibration-based quantization).

Testing and Validation:

The new ARM flow has been thoroughly tested across a range of models with all combinations:
NLP: Models such as BERT and T5.
Vision: Models like ResNet and ViT.
Custom Models: user defined models with various operators.

example script:

import torch
from transformers import BertModel
import copy
import time
from torchao.quantization.pt2e.quantize_pt2e import prepare_pt2e, convert_pt2e
import torchao.quantization.pt2e.quantizer.arm_inductor_quantizer as aiq
from torchao.quantization.pt2e.quantizer.arm_inductor_quantizer import ArmInductorQuantizer
import torch.profiler
import torch._inductor.config as config
# Enable C++ wrapper for Inductor
config.cpp_wrapper = True
config.freezing=True

model_name = "bert-base-uncased"
model = BertModel.from_pretrained(model_name)

# Set the model to eval mode
model = model.eval()

# Create the data, using dummy data here as an example
traced_bs = 32
seq_length = 128
x = torch.randint(0, 10000, (traced_bs, seq_length))
attention_mask = torch.ones((traced_bs, seq_length))
example_inputs = (x, attention_mask)

# Capture the FX Graph to be quantized
with torch.no_grad():
    exported_model = torch.export.export_for_training(model, example_inputs).module()

    # Set up the quantizer and prepare the model for post-training quantization
    quantizer = ArmInductorQuantizer()
    quantizer.set_global(aiq.get_default_arm_inductor_quantization_config(is_dynamic=True))
    prepared_model = prepare_pt2e(exported_model, quantizer)

    # Run the prepared model to apply the quantization
    prepared_model(*example_inputs)

    # Convert the model to the quantized version
    converted_model = convert_pt2e(prepared_model)
    optimized_model = torch.compile(converted_model)
    st = time.time()
    optimized_model(*example_inputs)
    et = time.time()
    print(f"Average time required for inference = {et-st}\n")


cc: @jerryzh168, @fadara01, @Xia-Weiwen

Copy link

pytorch-bot bot commented Apr 28, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2139

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

❌ 1 New Failure

As of commit 12ecd5a with merge base c9b9adc (image):

NEW FAILURE - The following job has failed:

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

@facebook-github-bot
Copy link
Contributor

Hi @choudhary-devang!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@choudhary-devang
Copy link
Collaborator Author

This pr is moved from torch to torchao due to the Pt2e migration to torchao

old pr link
(pytorch/pytorch#146690)

@facebook-github-bot facebook-github-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 Apr 29, 2025
@choudhary-devang
Copy link
Collaborator Author

Hi @jerryzh168, can you please add the required labels to trigger the pipelines.

@jerryzh168 jerryzh168 added pt2e_quant pt2 export quantization topic: new feature Use this tag if this PR adds a new feature labels May 5, 2025
@jerryzh168
Copy link
Contributor

please fix ruff errors, you can install pre-commit so it can run automatically when you add a new commit

@choudhary-devang choudhary-devang force-pushed the Arm_inductor_quantizer branch 2 times, most recently from 09d29c5 to 14f1713 Compare May 6, 2025 09:39
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: arm_inductor

@jerryzh168
Copy link
Contributor

for ruff, we are using 0.11.6 and you can run ruff format locally to fix the issue

@choudhary-devang choudhary-devang force-pushed the Arm_inductor_quantizer branch from 14f1713 to faba744 Compare May 7, 2025 06:09
rename test_arminductor_quantizer.py  -->  test_arm_inductor_quantizer.py
applied ruff format
@choudhary-devang choudhary-devang force-pushed the Arm_inductor_quantizer branch from faba744 to 12ecd5a Compare May 7, 2025 06:18
@maajidkhann
Copy link

for ruff, we are using 0.11.6 and you can run ruff format locally to fix the issue

@jerryzh168 Looks like Devang has pushed the fix. Can you now trigger the pipelines again.

@jerryzh168
Copy link
Contributor

done, I also just added @choudhary-devang as a contributor

@jerryzh168
Copy link
Contributor

btw, for inductor fusion passes, you can add to torchao/quantization/pt2e/inductor_passes/ folder for now

@fadara01
Copy link

fadara01 commented May 7, 2025

How will this be tested in CI?
We need to make sure that intel changing X86InductorQuantizer (which we inherit from), won't break our functionality

@choudhary-devang
Copy link
Collaborator Author

btw, for inductor fusion passes, you can add to torchao/quantization/pt2e/inductor_passes/ folder for now

Hi @jerryzh168, thanks for adding me as a contributor, and sure i will add the lowering changes for weight as per_tensor in this path in my next pr.

@choudhary-devang
Copy link
Collaborator Author

How will this be tested in CI? We need to make sure that intel changing X86InductorQuantizer (which we inherit from), won't break our functionality

I think currently this isn't covered in CI, Once the test file (https://github.com/pytorch/ao/pull/2139/files#diff-af6ea96fb55af02d2e1c7837c198f7d73a38d542b5ea7000e683422a6b7ca17b) is made part of the CI execution. Then, any change to X86InductorQuantizer will trigger test failures if it breaks ArmInductorQuantizer.

These test files both from x86 and ARM might need to be added to CI execution later on.

@maajidkhann
Copy link

Looks like the CUDA nightly pipeline failing is unrelated to this PR and is failing in all other open PR's as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. pt2e_quant pt2 export quantization topic: new feature Use this tag if this PR adds a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants