Skip to content

fix: Preserve precision in NCHW quantized average pooling - #1320

Open
vera-rubin wants to merge 1 commit into
ARM-software:mainfrom
vera-rubin:fix/nchw-avg-precision
Open

vera-rubin wants to merge 1 commit into
ARM-software:mainfrom
vera-rubin:fix/nchw-avg-precision

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

NCHW quantized AVG pooling rounds its mean to an input byte before applying output quantization. For a 2×2 QASYMM8 input {128, 127, 127, 128} with input quantization (0.25, 128) and output quantization (1/128, 128), we get 96 instead of the quantized reference's 112 for the exact mean -0.125.

We keep the fractional mean until output quantization in the 2×2, 3×3 and generic NCHW kernels.

We tested the reproducer, a deterministic regression and the existing NEON pooling validation tests.

— ChipLab

@morgolock

Copy link
Copy Markdown
Contributor

Could you share more about your use case for ACL?

  • What application, models, or workloads are you running?
  • Are you using ACL directly or through a framework?
  • Which hardware do you target, and do you require CPU acceleration, GPU acceleration, or both?
  • What functionality or performance requirements would a replacement need to preserve?

As ACL approaches end of life, we want to help users identify suitable migration paths, including KleidiAI or LiteRT where appropriate. Understanding your requirements will help us assess which options could work for you.

@vera-rubin

Copy link
Copy Markdown
Contributor Author

Same answer as on #1319: we don't ship ACL in a product, so there's nothing to migrate on our side.

Comment thread src/cpu/kernels/pool2d/neon/quantized.h Resolved
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Same change here as on #1319: the SPDX-FileCopyrightText line is added to quantized.h and PoolingLayer.cpp, and Arm's line in quantized.h is back to 2021-2022, 2024. The copyright for these changes is held by Donmakino LLC dba ChipLab.

@vera-rubin
vera-rubin force-pushed the fix/nchw-avg-precision branch from 7f59bf2 to b5ae8c6 Compare November 23, 2026 05:25
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Squashed this one into a single commit too, same as #1319.

@morgolock morgolock 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.

Please fix the saturation regression in the new scalar quantization at src/cpu/kernels/pool2d/neon/quantized.h:369.

Qasymm8QuantizationHelper<T>::quantize narrows the lround result to int before clamping to the output byte range. With a large input/output scale ratio, this can wrap and saturate at the wrong end of the range.

Reproduced on native AArch64 with GCC 11.5.0:

  • NCHW QASYMM8, 2x2 input containing four values of 64.
  • Input quantization (0.25, 0), output quantization (2^-27, 0).
  • AVG, 2x2 window, stride 1, no padding.

The expected output is 255. The previous code returns 255, but this PR returns 0. The 3x3 kernel also fails. With signed data, positive values can produce -128 instead of 127, and negative values can produce the reverse. These configurations pass NEPoolingLayer::validate.

Please clamp the quantized floating-point result before integer narrowing, or use a saturating conversion, and add regression tests for large scale ratios and both signs.

The copyright changes address my earlier comment. All 24 added regression tests and 648 other targeted pooling validation tests passed; the saturation cases above are additional coverage.

@vera-rubin

Copy link
Copy Markdown
Contributor Author

Thanks for the reproducer. The scalar requantization now clamps value / scale to the output range in float before the integer conversion, at all three NCHW scalar sites: the 2x2/3x3 AVG path, the generic AVG path, and the generic MAX requantization, which already used the same helper before this PR. The new PoolingDifferentQuantizationInfoSaturates test covers QASYMM8 and QASYMM8_SIGNED, AVG and MAX, pool sizes 2, 3 and 4, and both signs at an output scale of 2^-27; 16 of its 24 cases fail on the previous head, and NEON/PoolingLayer/.* passes 1530/1530 in precommit mode (AArch64 cross build run under qemu).

NCHW quantized AVG pooling rounds the mean to an input byte before
applying output quantization. A 2x2 QASYMM8 input containing 128, 127,
127, 128 with input quantization (0.25, 128) and output quantization
(1/128, 128) returns 96 instead of the reference result 112.

Keep the fractional mean until output quantization in the 2x2, 3x3 and
generic NCHW kernels. In the scalar requantization of the 2x2/3x3 AVG,
generic AVG and generic MAX NCHW paths, clamp value / scale to the
output type's range in float before the integer conversion, so a large
ratio between the input and output scales saturates instead of
wrapping.

Add deterministic regressions covering signed and unsigned inputs,
strides 1 and 2, padding, partial vectors, and positive and negative
saturation for AVG and MAX with pool sizes 2, 3 and 4.

Signed-off-by: Nathan Chandrasekar <322284620+vera-rubin@users.noreply.github.com>
@vera-rubin
vera-rubin force-pushed the fix/nchw-avg-precision branch from 3b09a34 to afd19fb Compare November 28, 2026 12:46
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Squashed the saturation fix into the single commit for the squash check; the code is unchanged from the previous head.

This branch has not been deployed

No deployments
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.

2 participants