Skip to content

Set eps in end-to-end QAT flow #2180

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

Merged
merged 2 commits into from
May 9, 2025
Merged

Set eps in end-to-end QAT flow #2180

merged 2 commits into from
May 9, 2025

Conversation

andrewor14
Copy link
Contributor

@andrewor14 andrewor14 commented May 6, 2025

Summary: This commit does two things:

(1) Allow users to set eps in FakeQuantizeConfig
(2) For other parts of the QAT flow, set eps to torch.finfo(torch.float32).eps for input linear activations to match the existing hardcoded input activation scale dtype (which is fp32)

The motivation is to enable users who wish to lower their models to XNNPACK. This would require them to use the following combination of dtypes during training for end-to-end numerical match:

  • input activations: bf16
  • input activation scales: fp32
  • input activation eps: torch.finfo(torch.float32).eps
  • weight: bf16
  • weight scales: bf16
  • weight eps: torch.finfo(torch.bfloat16).eps

However, today there is no way to specify the above in any of the QAT flows. For the recommended FakeQuantizeConfig flow, we always use torch.finfo(x.dtype).eps, where x is bf16 in this case, and there is no way for users to configure this. This is resolved by (1).

For the legacy Int8DynActInt4QATQuantizer flow, we hardcode input activation scales to always use fp32 in
#2085, but did not set the corresponding eps. Today, this also uses torch.finfo(x.dtype).eps by default, where x is bf16, and so we use the wrong eps value. This is resolved by (2).

Test Plan:
python test/quantization/test_qat.py -k test_fake_quantize_config_eps
python test/quantization/test_qat.py -k test_qat_8da4w_eps

Copy link

pytorch-bot bot commented May 6, 2025

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit 4cc36b8 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 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 May 6, 2025
@andrewor14 andrewor14 requested a review from metascroy May 6, 2025 22:05
@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@@ -539,7 +540,8 @@ def get_group_qparams_symmetric(
assert n_bit <= 8, f"unsupported n_bit: {n_bit}"

block_size = (1, groupsize)
eps = torch.finfo(w.dtype).eps
if eps is None:
eps = torch.finfo(w.dtype).eps
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this already the behavarior inside choose_qparams_affine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I could just change FakeQuantizer to use that instead, but seems like a bigger change, prefer to leave that in a future PR to keep this one isolated

@andrewor14 andrewor14 force-pushed the qat-eps branch 2 times, most recently from 13d9cb2 to f782422 Compare May 6, 2025 22:22
@andrewor14 andrewor14 added the topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories) label May 6, 2025
@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@andrewor14 andrewor14 force-pushed the qat-eps branch 2 times, most recently from eb6431c to 17e9ccb Compare May 6, 2025 22:49
@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@metascroy
Copy link
Contributor

@andrewor14 maybe we can relax the failing test_export_compile_aoti_PackedLinearInt8DynamicActivationIntxWeightLayout test to just look for the op name?

@andrewor14
Copy link
Contributor Author

maybe we can relax the failing test_export_compile_aoti_PackedLinearInt8DynamicActivationIntxWeightLayout test to just look for the op name?

Looks like the actual failing test is test_export_QDQLayout? I think it's complaining because there's no eps in the expected lines. The one you mentioned seems to be just a warning so I won't fix it here :)

@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

**Summary:** This commit does two things:

(1) Allow users to set eps in `FakeQuantizeConfig`
(2) For other parts of the QAT flow, set eps to
    `torch.finfo(torch.float32).eps` for input linear
    activations to match the existing hardcoded input
    activation scale dtype (which is fp32)

The motivation is to enable users who wish to lower their
models to XNNPACK. This would require them to use the following
combination of dtypes during training for end-to-end numerical
match:

- input activations: bf16
- input activation scales: fp32
- input activation eps: `torch.finfo(torch.float32).eps`
- weight: bf16
- weight scales: bf16
- weight eps: `torch.finfo(torch.bfloat16).eps`

However, today there is no way to specify the above in any
of the QAT flows. For the recommended `FakeQuantizeConfig`
flow, we always use `torch.finfo(x.dtype).eps`, where x is
bf16 in this case, and there is no way for users to configure
this. This is resolved by (1).

For the legacy `Int8DynActInt4QATQuantizer` flow, we hardcode
input activation scales to always use fp32 in
#2085, but did not set the
corresponding eps. Today, this also uses `torch.finfo(x.dtype).eps`
by default, where x is bf16, and so we use the wrong eps value.
This is resolved by (2).

**Test Plan:**
python test/quantization/test_qat.py -k test_fake_quantize_config_eps
python test/quantization/test_qat.py -k test_qat_8da4w_eps
@andrewor14
Copy link
Contributor Author

Hey @metascroy, looks like there's a new test failure and it seems real? I think it's complaining that qdq ops are not fused. I wonder if it's because there's a new eps value that broke the pattern matching somehow. Can you take a look?

python torchao/experimental/tests/test_quant_passes.py -k test_replace_q_dq_patterns_with_quantized_linear_ops_pass

@andrewor14 andrewor14 changed the title Allow setting eps in FakeQuantizeConfig Set eps in end-to-end QAT flow May 7, 2025
@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@metascroy
Copy link
Contributor

Hey @metascroy, looks like there's a new test failure and it seems real? I think it's complaining that qdq ops are not fused. I wonder if it's because there's a new eps value that broke the pattern matching somehow. Can you take a look?

python torchao/experimental/tests/test_quant_passes.py -k test_replace_q_dq_patterns_with_quantized_linear_ops_pass

Traveling today, but I'll have a look later in afternoon.

@metascroy
Copy link
Contributor

Hey @metascroy, looks like there's a new test failure and it seems real? I think it's complaining that qdq ops are not fused. I wonder if it's because there's a new eps value that broke the pattern matching somehow. Can you take a look?

python torchao/experimental/tests/test_quant_passes.py -k test_replace_q_dq_patterns_with_quantized_linear_ops_pass

Traveling today, but I'll have a look later in afternoon.

Pushed a fix to your PR

@facebook-github-bot
Copy link
Contributor

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@andrewor14
Copy link
Contributor Author

Ok, remaining test failure seems unrelated and happens on other PR. I think this is good to land.

@andrewor14 andrewor14 merged commit 45b39b1 into main May 9, 2025
18 of 19 checks passed
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. topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants