Skip to content

Fix output spec adjustment for fixed qparams ops - #22

Merged
crowbat merged 3 commits into
apple:mainfrom
crowbat:u/k_hsieh/fix_ranges_for_fixed_qparam_ops
Jul 14, 2026
Merged

Fix output spec adjustment for fixed qparams ops#22
crowbat merged 3 commits into
apple:mainfrom
crowbat:u/k_hsieh/fix_ranges_for_fixed_qparam_ops

Conversation

@crowbat

@crowbat crowbat commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

This PR fixes the output qspec adjustment behavior for ops with known ranges like relu, relu6, sigmoid, tanh, etc.
Previously the code did not properly perform the adjustment. Qscheme was not being set correctly, and no fixed ranges were ever in place.

This PR handles correctly setting the qscheme and float range as applicable for each op.
Refactoring was also done to remove the independent qscheme attribute of fake quantize, instead having it read directly from its qparams_calculator.qscheme instead.

Note: the mnist test accuracy expectation post-prepare was updated from < 88% to < 94%. I checked that this large difference was due only to the hardtanh output quantizers now being set to asymmetric with float range (0.0, 1.0).

@crowbat
crowbat requested review from dengqiaoyu and u-simha July 1, 2026 16:14
@crowbat
crowbat force-pushed the u/k_hsieh/fix_ranges_for_fixed_qparam_ops branch from 3fac694 to 462226c Compare July 1, 2026 16:21
@crowbat
crowbat force-pushed the u/k_hsieh/fix_ranges_for_fixed_qparam_ops branch from 462226c to 8550f72 Compare July 13, 2026 17:09
u-simha
u-simha previously approved these changes Jul 13, 2026

@u-simha u-simha left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, few minor comments

torch.ops.aten.sigmoid_.default: _sigmoid_qspec,
torch.ops.aten.hardsigmoid.default: _sigmoid_qspec,
torch.ops.aten.hardsigmoid_.default: _sigmoid_qspec,
torch.ops.aten.tanh.default: (QuantizationScheme.SYMMETRIC, (-1.0, 1.0)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might be useful to have single line comments here (also acting as separators) - similar to what we have on line 162 & 165 for relu & relu6. This would help improve readability.

Comment thread tests/quantization/test_factory.py Outdated
assert fq_partial_out.dtype == x.dtype
assert fq_direct_out.dtype == x.dtype

def test_update_partial_qparams_calculator_single_attr(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think couple of these tests can be combined into a single one?

return spec.fake_quantize_cls(**common_args, **extra_args)

@classmethod
def update_partial_qparams_calculator(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we are returning a new partial, I wonder if it is better to call it something other than "update"? probably "reconstruct" - just so that it is obvious that the existing one is not updated (partials anyway can not be updated I'm guessing)

@crowbat
crowbat force-pushed the u/k_hsieh/fix_ranges_for_fixed_qparam_ops branch from 8550f72 to e9e8e7b Compare July 13, 2026 22:08
@crowbat
crowbat merged commit 0eabc57 into apple:main Jul 14, 2026
8 checks passed
@crowbat
crowbat deleted the u/k_hsieh/fix_ranges_for_fixed_qparam_ops branch July 14, 2026 02:04

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should use 22.fixed instead of 180525445.fixed‎ as the file name, where "22" is the PR number from GitHub.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, agreed

@eddieriley85-jpg

Copy link
Copy Markdown

Looks good, few minor comments

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.

4 participants