[RFC][CodeGen][CUDA]: Gate fast math intrinsic lowering behind target option#19565
Open
ConvolutedDog wants to merge 1 commit into
Open
[RFC][CodeGen][CUDA]: Gate fast math intrinsic lowering behind target option#19565ConvolutedDog wants to merge 1 commit into
ConvolutedDog wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces support for fast math intrinsics in the CUDA backend by adding an enable_fast_math attribute to the CUDA target and updating the intrinsic lowering pass to prioritize these variants. It includes registrations for several math operations such as exp, log, and sin, along with a new test suite. The review feedback correctly identifies a contradictory assertion in the target tests where an attribute was explicitly set to True but asserted as False, and it suggests fixing a recurring typo (num_inputes to num_inputs) throughout the new test file.
Member
|
cc @LeiWang1999 |
… option Fix CUDA lowering of standard TIR math intrinsics so they use precise CUDA math functions by default instead of fast-math `__*f` functions. This fixes the default behavior reported in apache#19546, where operators such as `tirx.exp` could lower to `__expf` even though fast math was not explicitly requested. This change adds a CUDA target attribute, `enable_fast_math`, which defaults to `false`. When the attribute is unset or false, standard math intrinsics lower through the normal CUDA math rule, for example `expf`, `logf`, `sinf`, `cosf`, `powf`, and `rsqrtf` for `float32`. When users explicitly enable the attribute on the target, the lowering pass also checks the `cuda.fastmath.FLowerIntrinsic` rules before the normal CUDA lowering rules. Users can opt in to fast math by constructing a CUDA target with the attribute: tvm.target.Target({"kind": "cuda", "enable_fast_math": True}) target = tvm.target.Target({ "tag": "nvidia/nvidia-a100", "enable_fast_math": True, }) The fast-math lowering path currently covers the CUDA math operators registered with `cuda.fastmath.FLowerIntrinsic`: `tirx.exp`, `tirx.exp10`, `tirx.log`, `tirx.log2`, `tirx.log10`, `tirx.tan`, `tirx.cos`, `tirx.sin`, `tirx.tanh`, and `tirx.pow`. `tirx.rsqrt` is also registered for CUDA lowering so it maps to the CUDA reciprocal-square-root intrinsic instead of being legalized as `1 / sqrt(x)`. Add CUDA codegen tests `tests/python/codegen/test_target_codegen_cuda_fastmath.py` that check the lowered IR, generated CUDA source, and runtime results for the supported math intrinsics across floating point dtypes and both default and fast-math targets. Note: fast math is only a CUDA target lowering option, not a runtime device property. This PR keep `enable_fast_math` canonicalized as a bool CUDA target option with default false.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix CUDA lowering of standard TIR math intrinsics so they use precise CUDA math functions by default instead of fast-math
__*ffunctions. This fixes the default behavior reported in #19546, where operators such astirx.expcould lower to__expfeven though fast math was not explicitly requested.This change adds a CUDA target attribute,
enable_fast_math, which defaults tofalse. When the attribute is unset or false, standard math intrinsics lower through the normal CUDA math rule, for exampleexpf,logf,sinf,cosf,powf, andrsqrtfforfloat32. When users explicitly enable the attribute on the target, the lowering pass also checks thecuda.fastmath.FLowerIntrinsicrules before the normal CUDA lowering rules.Users can opt in to fast math by constructing a CUDA target with the attribute:
The fast-math lowering path currently covers the CUDA math operators registered with
cuda.fastmath.FLowerIntrinsic:tirx.exp,tirx.exp10,tirx.log,tirx.log2,tirx.log10,tirx.tan,tirx.cos,tirx.sin,tirx.tanh, andtirx.pow.tirx.rsqrtis also registered for CUDA lowering so it maps to the CUDA reciprocal-square-root intrinsic instead of being legalized as1 / sqrt(x).Add CUDA codegen tests
tests/python/codegen/test_target_codegen_cuda_fastmath.pythat check the lowered IR, generated CUDA source, and runtime results for the supported math intrinsics across floating point dtypes and both default and fast-math targets.