Repository navigation
Conversation
1c19c0b to
2e03983
Compare
morgolock
left a comment
There was a problem hiding this comment.
Please address two points before merging:
-
FP32 regression in
impl.cpp:76–99. A row of three1000.1fvalues withepsilon=1e-8fshould produce zeros. On native Arm/GCC 11.5, the parent returns approximately0.000194558per element; this patch returns0.520978. The new centred variance amplifies the existing rounding error in the mean. Please stabilize the mean accumulation too. -
Add ACL regression coverage. Add FP16/FP32 cases for large offsets, constant and nearly constant rows, vector tails and in-place execution in
tests/validation/NEON/MeanStdDevNormalizationLayer.cpp, using a stable reference. The current reference still usesE[x²] - mean².
All 29 existing tests pass on both revisions, so they do not catch these cases.
The NEON kernels compute the variance in one pass as E[x^2] - mean^2. When the mean is large compared with the spread, the two terms nearly cancel and rounding can make the variance negative, so 1 / sqrt(var + epsilon) returns NaN for the whole row. The fp16 kernel also rounds the mean to fp16 before squaring it, so it fails at small offsets already, e.g. rows around 10 +- 0.1 or 100 +- 1. Use the corrected two-pass algorithm: compute the mean, then sum the centred values d = x - mean and their squares. The variance is sum(d^2) / N - (sum(d) / N)^2, and the output also subtracts sum(d) / N, which removes the rounding error of the mean, e.g. for constant rows. The fp16 kernel does this in fp32. Compute the reference in double with the variance from centred values, and add FP16/FP32 tests for large offsets, constant and nearly constant rows, vector tails and in-place execution. Reported in openvinotoolkit/openvino#40324 Signed-off-by: Umang <umangsharma1189@gmail.com>
2e03983 to
a2f8caf
Compare
|
Thanks for the review. Both points are addressed in the new revision. 1. FP32 regression with constant rows. Confirmed: three 1000.1f values with epsilon=1e-8f gave 0.5209779 per element here too. The rounding error δ of the mean ended up inside the centred variance, since Σ(x − m)²/N = var + δ², and from there in the output. The kernels now use the corrected two-pass algorithm (Chan, Golub and LeVeque). The second pass also accumulates Σd with d = x − m:
This corrects the mean by its own rounding error, instead of making the first-pass summation more precise, so it works whatever the summation order. The first-pass sum is unchanged. The case above now returns exact zeros. The FP16 kernel does the same in fp32. It also improves accuracy at large offsets: the worst FP32 error on my edge-case set (offsets up to 1e4) went from 3.4e-3 to 4.2e-6. 2. Regression coverage.
Results with
The results are identical on native arm64 Linux with GCC 11.5.0 ( Cost: in per-node profiling inside real models, the kernel now takes about 1.75x as long as the parent (1.6x to 1.9x, FP16 and FP32), up from about 1.65x in the previous revision. |
Problem
NEMeanStdDevNormalizationLayerreturns NaN for rows whose mean is large compared with their spread. The NEON kernels compute the variance in one pass:src/cpu/kernels/meanstddevnorm/generic/neon/impl.cpp(fp32):var = (sum_sq / N) - (mean * mean)src/cpu/kernels/meanstddevnorm/generic/neon/fp16.cpp(fp16): the same, withmeanrounded tofloat16_tbeforemean * meanThe two terms nearly cancel, rounding can make
varnegative, and1 / sqrt(var + epsilon)returns NaN for the whole row. In fp16 this starts at small offsets because the rounded mean is off by up to half an fp16 step (0.03 around 100), and its square is then off by more than the real variance. Rows around 10 ± 0.1 or 100 ± 1 already fail. In fp32 it needs |mean| / std of a few thousand, e.g. 300 ± 0.1.It was found through OpenVINO, which uses this function for LayerNorm / MVN on Arm CPUs. f16 is OpenVINO's default inference precision there: openvinotoolkit/openvino#40324.
Change
input == output) stays correct.MeanStdDevNormalizationLayerOffsetValidationFixturefills inputs uniformly in [offset − spread, offset + spread]. The existing fixture is unchanged, so the CL tests that share it are unaffected. New PRECOMMITFP16/RunLargeMeanandFP32/RunLargeMean(72 cases):5 files, +191 / −69.
Testing
arm_compute_validation --filter='.*MeanStdDevNormalizationLayer.*', with identical results in both setups:arch=arm64-v8.2-aos=linux arch=arm64-v8.2-a, defaultWerror=1, no warningsRunLargeMean)A row of three 1000.1f values with epsilon 1e-8f (GCC 11.5) gives 0.000194557622 per element for the parent, 0.520977914 for the first revision of this PR and 0 for this revision.
Through OpenVINO's CPU plugin (macOS), which calls this function for MVN:
clang-format14.0.6 andscripts/format_code.py --check_formatting --check_copyrightpass on all five files.Cost
Per-node profiling inside real models (all-MiniLM-L6-v2, DeiT-tiny, Whisper-tiny encoder): the kernel takes about 1.75x as long as before (1.6x to 1.9x, fp16 and fp32), because of the extra pass over each row. MVN is 0.4–1.2% of these models' run time, so whole-model time changes by about 1% or less.
Not tested: other target variants. SVE builds use the same NEON files.