-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[LLVM][Codegen] Cast NaN to bool gives true #18772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Ensure consistency with the existing framework (C/C++/Python/Torch/Numpy/...).
Summary of ChangesHello @locnd182644, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an inconsistency in TVM's handling of NaN-to-boolean conversions compared to other popular frameworks. By modifying the LLVM codegen and adding a new test case, the PR ensures that TVM now correctly casts NaN to Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
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 correctly aligns TVM's behavior for casting NaN to bool with other major frameworks by changing the LLVM comparison instruction from fcmp one to fcmp une. The change is sound and the new test case effectively validates the behavior for NaN, 0.0, and inf. I've added one suggestion to parameterize the new test to improve coverage across different float types.
| @tvm.testing.requires_llvm | ||
| def test_llvm_cast_float_to_bool(): | ||
| @I.ir_module | ||
| class Module: | ||
| @T.prim_func | ||
| def main(A: T.Buffer((4,), "float32"), C: T.Buffer((4,), "bool")): | ||
| T.func_attr({"tir.noalias": True}) | ||
| for i in range(4): | ||
| with T.sblock("C"): | ||
| v_i = T.axis.spatial(4, i) | ||
| T.reads(A[v_i]) | ||
| T.writes(C[v_i]) | ||
| C[v_i] = T.Cast("bool", A[v_i]) | ||
|
|
||
| n = 4 | ||
| f = tvm.compile(Module, target="llvm") | ||
| dev = tvm.cpu(0) | ||
| a = tvm.runtime.tensor(np.array([0.0, 1.0, np.nan, np.inf], dtype="float32"), dev) | ||
| c = tvm.runtime.empty((n,), dtype="bool", device=dev) | ||
| f(a, c) | ||
| c_np = np.array([False, True, True, True], dtype="bool") | ||
| tvm.testing.assert_allclose(c.numpy(), c_np) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great test for verifying the new behavior. To improve test coverage, consider parameterizing it to run against different float types (float16, float32, float64), since the change in codegen_llvm.cc affects all float types.
@tvm.testing.requires_llvm
@pytest.mark.parametrize("dtype", ["float16", "float32", "float64"])
def test_llvm_cast_float_to_bool(dtype):
@I.ir_module
class Module:
@T.prim_func
def main(A: T.Buffer((4,), dtype), C: T.Buffer((4,), "bool")):
T.func_attr({"tir.noalias": True})
for i in range(4):
with T.sblock("C"):
v_i = T.axis.spatial(4, i)
T.reads(A[v_i])
T.writes(C[v_i])
C[v_i] = T.Cast("bool", A[v_i])
n = 4
f = tvm.compile(Module, target="llvm")
dev = tvm.cpu(0)
a = tvm.runtime.tensor(np.array([0.0, 1.0, np.nan, np.inf], dtype=dtype), dev)
c = tvm.runtime.empty((n,), dtype="bool", device=dev)
f(a, c)
c_np = np.array([False, True, True, True], dtype="bool")
tvm.testing.assert_allclose(c.numpy(), c_np)
Due to some recent changes to CI, PR #18646 will be closed and a new PR created. This PR was created by opening a new pull request on the main branch.
Summary
Cast NaN to bool gives true to ensure consistency with the existing framework (C, C++, Python, Torch, NumPy, OnnxRuntime, ...).
Steps to Reproduce
Expected
Resolved
fcmp onewithfcmp unein LLVM.Related:
Fixed: [Bug] ONNX Cast treats NaN inconsistently in TVM LLVM codegen: Constant(NaN)->True but computed NaN->False #18605