镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Fix NaN sign comparison for Sum/Average/Product in TensorValueReductionsPreserveScalarOrder - #136650

Merged
lewing merged 1 commit into
dotnet:mainfrom
lewing:lewing-tensor-sum-nan-test-fix
Oct 29, 2026
Merged

lewing merged 1 commit into
dotnet:mainfrom
lewing:lewing-tensor-sum-nan-test-fix

Conversation

@lewing

@lewing lewing commented Oct 29, 2026

Copy link
Copy Markdown
Member

Follow-up to #136438 (Dot) and #136552 (CosineSimilarity).

TensorValueReductionsPreserveScalarOrder fails on linux-arm64 libraries-jitstress legs at the Tensor.Sum assertion (TensorTests.cs:1207): expected 0x7FF8000000000000 (+NaN), actual 0xFFF8000000000000 (-NaN).

For the input [-Inf, +Inf, -0.0, 0.0, NaN, -3], -Inf + +Inf constructs a canonical NaN that is later combined with the input double.NaN. When both operands are NaN, which one propagates depends on operand order, and JitStress changes that order.

Following the guidance in #136438 (keep exact NaN payload checks except where a canonical NaN may be constructed), this switches only these assertions to the existing AssertBitsOrNaN helper:

  • Tensor.Sum: -Inf + +Inf (the failing assertion)
  • Tensor.Average: Sum / n. It fails next once Sum is relaxed.
  • Tensor.Product: Inf * 0 constructs a canonical NaN that then combines with the input NaN. It did not fail locally, but it has the same order sensitivity.

Unchanged (exact bits): Norm and Distance never construct a NaN from this input, so only the input NaN propagates. StdDev applies Abs before squaring in both the product code and the test, which normalizes the sign. The min/max family only selects input values.

Validation (osx-arm64, Release runtime/libs with a Checked libclrjit swapped into testhost)

  • Before the fix: DOTNET_JitStress=1 or 2 with DOTNET_TieredCompilation=0 reproduces the CI failure exactly (layouts 0, 1, 2, 4, 5 fail at line 1207 with the same bits).
  • With only Sum relaxed: Average fails at line 1210 with the same bits.
  • With this change: the test passes 6/6 under JitStress=1/2, tiered and untiered, TailcallStress, ReadyToRun=0, and with no stress.
  • Full System.Numerics.Tensors.Tests: 6505 passed, 0 failed, under JitStress=2 + TieredCompilation=0 and again with the Release JIT.

Resolves #136647

Note

This PR description was generated with GitHub Copilot.

…rder test

For the [-Inf, +Inf, -0.0, 0.0, NaN, -3] input, Sum (-Inf + +Inf) and
Product (Inf * 0) construct a canonical NaN that then combines with the
input NaN. Which NaN propagates depends on operand order, which JitStress
perturbs on arm64. Average is Sum / n. Use AssertBitsOrNaN for these, as
was done for Dot and CosineSimilarity; keep exact-bit checks for Norm,
Distance, StdDev and the min/max family, which never construct a NaN from
this input (StdDev applies Abs before squaring).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
See info in area-owners.md if you want to be subscribed.

Copilot AI 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.

🟢 Approval recommended

No active review issues were identified for the test-only change to tolerate NaN sign and payload differences.

No open issues 🎉

What changed in this PR

Updates the tensor scalar-order reduction test to tolerate NaN sign and payload differences for Sum, Average, and Product, extending the earlier fixes for Dot and CosineSimilarity.

Changes:

  • Uses the existing AssertBitsOrNaN helper for the three affected assertions while preserving exact-bit comparisons for non-NaN results.
  • Leaves other reduction assertions and production code unchanged.
File Description
src/​libraries/​System.Numerics.Tensors/​tests/​TensorTests.cs Adjusts three assertions to tolerate NaN differences caused by operand ordering under JIT stress.

Review effort: Balanced

@lewing

lewing commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@tannergooding I think this is correct?

@lewing
lewing merged commit e522423 into dotnet:main Oct 29, 2026
85 of 87 checks passed
@lewing
lewing deleted the lewing-tensor-sum-nan-test-fix branch October 29, 2026 23:31
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ci-scan] Test failure: System.Numerics.Tensors.Tests.TensorTests.TensorValueReductionsPreserveScalarOrder — Tensor.Sum NaN sign under JitStress

3 participants