Skip to content

[MetaXGPU] Use ties-to-even rounding for tirx.round on the MACA target#43

Open
Lfan-ke wants to merge 1 commit into
MetaX-MACA:devfrom
Lfan-ke:fix/maca-round-ties-to-even
Open

[MetaXGPU] Use ties-to-even rounding for tirx.round on the MACA target#43
Lfan-ke wants to merge 1 commit into
MetaX-MACA:devfrom
Lfan-ke:fix/maca-round-ties-to-even

Conversation

@Lfan-ke

@Lfan-ke Lfan-ke commented Jul 13, 2026

Copy link
Copy Markdown

Problem

MACAMath lowers tirx.round to round / roundf, which round halfway cases away from zero. Every other backend in the tree lowers it to nearbyint, which under the default rounding mode rounds halfway cases to even:

  • src/backend/cuda/codegen/intrin_rule_cuda.cc:41-45 returns nearbyint for fp64 and nearbyintf for fp32
  • src/target/llvm/intrin_rule_llvm.cc:91-93 dispatches tirx.round to llvm::Intrinsic::nearbyint

So the same graph rounds 0.5 to 1.0 on a MACA device and to 0.0 everywhere else, and a value that gets constant-folded at compile time disagrees with the same value computed on device. Only exact halfway inputs diverge, so this stays invisible under random test data.

fp16 is already correct: it maps to hrint, which is ties-to-even.

Changes

MACAMath returns nearbyint for fp64 and nearbyintf for fp32, matching the CUDA rule it was forked from. tirx.nearbyint is unaffected — it already lowered to the same function.

Test Plan

tests/python/codegen/test_target_codegen_maca_round.py on a MetaX C500 (MACA 3.5.3.20). Both cases feed the eight halfway values ±0.5, ±1.5, ±2.5, ±3.5, which are the only inputs the two rules disagree on:

  • the generated MACA source calls nearbyintf and does not call roundf
  • the kernel returns 0, 2, 2, 4, -0, -2, -2, -4

Test Result

2 passed in 1.19s

Reverting the two lines in intrin_rule_maca.cc and rebuilding turns both cases red, so the test does exercise the changed lowering:

Mismatched elements: 4 / 8 (50%)
 [0]: 1.0 (ACTUAL), 0.0 (DESIRED)
 [2]: 3.0 (ACTUAL), 2.0 (DESIRED)
 [4]: -1.0 (ACTUAL), -0.0 (DESIRED)
 [6]: -3.0 (ACTUAL), -2.0 (DESIRED)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the MACA math codegen rules in intrin_rule_maca.cc to map the 'round' function to 'nearbyint' (for 64-bit floats) and 'nearbyintf' (for 32-bit floats) to use ties-to-even rounding, matching constant-folding semantics. There are no review comments, and I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@Lfan-ke

Lfan-ke commented Jul 13, 2026

Copy link
Copy Markdown
Author

@codex review please. read code at pr and repositories.

Signed-off-by: 林晨 (Leo Cheng) <chengkelfan@qq.com>
@Lfan-ke
Lfan-ke force-pushed the fix/maca-round-ties-to-even branch from bce586a to 6284d5b Compare July 14, 2026 07:41
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.

1 participant