Skip to content

[MetaXGPU] Split the fast-math and precise lowering paths, as CUDA does#45

Open
Lfan-ke wants to merge 1 commit into
MetaX-MACA:devfrom
Lfan-ke:fix/maca-precise-math-default
Open

[MetaXGPU] Split the fast-math and precise lowering paths, as CUDA does#45
Lfan-ke wants to merge 1 commit into
MetaX-MACA:devfrom
Lfan-ke:fix/maca-precise-math-default

Conversation

@Lfan-ke

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

Copy link
Copy Markdown

Problem

tirx.enable_fast_math defaults to false, and IntrinInjecter only consults <target>.fastmath.FLowerIntrinsic when it is set (src/tirx/transform/lower_intrin.cc:57-60). The MACA rules register their fast-math variants under the plain maca.FLowerIntrinsic key instead, so the flag never reaches them: seven ops lower to the approximate device function unconditionally.

On a C500, with the default PassContext:

op upstream expected
exp __expf expf
exp10 __exp10f exp10f
log __logf logf
log2 __log2f log2f
log10 __log10f log10f
sin __sinf sinf
cos __cosf cosf

A program that never asked for fast math gets it anyway, and setting the flag changes nothing. The CUDA rules do not have this problem: src/backend/cuda/codegen/intrin_rule_cuda.cc carries ten cuda.fastmath.FLowerIntrinsic registrations alongside the precise ones.

Changes

Each of these ops now registers twice, the way CUDA does: MACAFastMath on maca.fastmath.FLowerIntrinsic, MACAMath on maca.FLowerIntrinsic. The default becomes precise and tirx.enable_fast_math becomes the thing that selects the approximation.

pow gains a fast-math lowering it did not have before; __powf compiles on a C500.

tan keeps MACAFastMathTan on the fast path, which yields tanf at fp32 — the same function the precise path emits — so the registration only affects it at other widths. tanh stays precise on both paths, because MACA has no __tanhf and mxcc rejects the symbol.

Test Plan

tests/python/codegen/test_target_codegen_maca_fastmath.py on a MetaX C500 (MACA 3.5.3.20), over the eight ops that have both forms:

  • under the default PassContext, the generated source calls expf and never __expf
  • under PassContext(config={"tirx.enable_fast_math": True}), it calls __expf

Test Result

16 passed in 7.38s

Reverting intrin_rule_maca.cc and rebuilding turns eight of them red — the seven ops above lose their precise default, and pow loses the opt-in path:

8 failed, 8 passed in 7.27s
FAILED test_precise_math_is_the_default[exp]
FAILED test_precise_math_is_the_default[exp10]
FAILED test_precise_math_is_the_default[log]
FAILED test_precise_math_is_the_default[log2]
FAILED test_precise_math_is_the_default[log10]
FAILED test_precise_math_is_the_default[sin]
FAILED test_precise_math_is_the_default[cos]
FAILED test_fast_math_is_opt_in[pow]

@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 separates the lowering paths for several mathematical intrinsics (such as exp, log, tan, cos, and sin) into standard and fast-math variants. The reviewer pointed out that using MACAFastMathTan for the fast-math path of tirx.tan breaks bfloat16 support because it lacks support for kDLBfloat, and suggested using MACAMath for both paths instead.

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.

Comment on lines +207 to +210
TVM_REGISTER_OP("tirx.tan")
.set_attr<FLowerIntrinsic>("maca.fastmath.FLowerIntrinsic",
DispatchPureExtern<MACAFastMathTan>)
.set_attr<FLowerIntrinsic>("maca.FLowerIntrinsic", DispatchPureExtern<MACAMath>);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The MACAFastMathTan struct is functionally identical to MACAMath for float types (since it avoids __tanf and uses tanf instead), but it lacks support for other types like kDLBfloat (returning "" instead of "htan"). This breaks bfloat16 support for tan when fast-math is enabled.

We can simplify this registration and fix the bfloat16 bug by using MACAMath for both the default and fast-math paths. Additionally, MACAFastMathTan (lines 83-101) can be completely removed from this file as it is no longer needed.

Suggested change
TVM_REGISTER_OP("tirx.tan")
.set_attr<FLowerIntrinsic>("maca.fastmath.FLowerIntrinsic",
DispatchPureExtern<MACAFastMathTan>)
.set_attr<FLowerIntrinsic>("maca.FLowerIntrinsic", DispatchPureExtern<MACAMath>);
TVM_REGISTER_OP("tirx.tan")
.set_attr<FLowerIntrinsic>("maca.fastmath.FLowerIntrinsic",
DispatchPureExtern<MACAMath>)
.set_attr<FLowerIntrinsic>("maca.FLowerIntrinsic", DispatchPureExtern<MACAMath>);

@Lfan-ke

Lfan-ke commented Jul 13, 2026

Copy link
Copy Markdown
Author

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

@Lfan-ke
Lfan-ke force-pushed the fix/maca-precise-math-default branch from bcaa6e9 to 7d30c04 Compare July 13, 2026 17:39
@Lfan-ke Lfan-ke changed the title [MetaXGPU] Keep the default lowering path precise for exp, log and trig [MetaXGPU] Split the fast-math and precise lowering paths, as CUDA does Jul 13, 2026
@Lfan-ke
Lfan-ke force-pushed the fix/maca-precise-math-default branch from 7d30c04 to e382dcc Compare July 13, 2026 23:25
Signed-off-by: 林晨 (Leo Cheng) <chengkelfan@qq.com>
@Lfan-ke
Lfan-ke force-pushed the fix/maca-precise-math-default branch from e382dcc to a5b6a93 Compare July 14, 2026 07:48
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