Skip to content

[MLAS] test: accept canonical NaN in activation NaN round-trip check#28538

Merged
hariharans29 merged 1 commit into
microsoft:mainfrom
qiurui144:test-pr/mlas-test-activation-nan
May 19, 2026
Merged

[MLAS] test: accept canonical NaN in activation NaN round-trip check#28538
hariharans29 merged 1 commit into
microsoft:mainfrom
qiurui144:test-pr/mlas-test-activation-nan

Conversation

@qiurui144
Copy link
Copy Markdown
Contributor

Description

MlasActivationTest.ExecuteShort (test_activation.cpp) feeds NaN inputs
through MlasActivation and asserts the output matches the expected value
bit-for-bit. This change adds one accepted case: when the expected value is a
NaN, any NaN output passes.

Non-NaN comparisons are unchanged — a finite output where a NaN is expected
(or the reverse) still fails. Test-only change, no library behavior impact.

Verified: onnxruntime_mlas_test --gtest_filter=Activation.ShortExecute on
SpacemiT K3 (riscv64, RVV VLEN=256), rv-gcc 15.2 — FAILED before, PASSED
after (re-run x3). x86/x64 behavior unaffected.

Motivation and Context

The bit-exact assertion (Buffer[i].u == TestData[i][kind].u) implicitly
assumes the input NaN payload survives the activation. For kinds evaluated by
floating-point arithmetic — LeakyRelu (alpha * x), HardSigmoid
(alpha * x + beta) — that only holds on ISAs that propagate NaN payloads
(x86, ARM).

IEEE-754 does not require NaN payload propagation. RISC-V's F extension
mandates that any FP operation producing a NaN yields the canonical quiet NaN
(0x7fc00000 for f32), discarding the payload. So on riscv64 these kinds emit
0x7fc00000 for a NaN input — a correct "NaN in → NaN out" result whose bit
pattern simply differs from the input — and the bit-exact check fails.

Accepting any NaN where a NaN is expected restores the test to the portable
IEEE-754 contract.

MlasActivationTest feeds NaN inputs and asserts the output matches the
expected value bit-for-bit. For activation kinds that go through floating
-point arithmetic (e.g. LeakyRelu, HardSigmoid) this assumes the input NaN
payload is preserved. IEEE-754 does not require NaN payload propagation and
some ISAs canonicalize it -- RISC-V, for instance, produces the canonical
quiet NaN (0x7fc00000) for any arithmetic NaN result -- so the bit-exact
check fails there even though a NaN input correctly produced a NaN output.

Accept any NaN output when the expected value is a NaN. Non-NaN comparisons
are unchanged.

Signed-off-by: qiurui144 <happyqiurui@163.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates an MLAS activation unit test to accept architecture-valid NaN canonicalization behavior during scalar activation checks.

Changes:

  • Allows any NaN output to pass when the expected activation result is also NaN.
  • Keeps existing exact and tolerance-based comparisons unchanged for non-NaN outputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hariharans29 hariharans29 enabled auto-merge (squash) May 19, 2026 16:56
@hariharans29 hariharans29 merged commit b38731b into microsoft:main May 19, 2026
91 checks passed
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.

3 participants