-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[LLVM][Codegen] Cast NaN to bool gives true #18646
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
base: main
Are you sure you want to change the base?
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 resolves an inconsistency in TVM's LLVM code generation where casting a floating-point 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. 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 addresses an inconsistency in how TVM handles casting NaN to a boolean value. The change from CreateFCmpONE to CreateFCmpUNE in the LLVM codegen aligns TVM's behavior with other major frameworks like C++, Python, and NumPy, ensuring that NaN correctly evaluates to true. The addition of a new test case is also a great step to verify this fix. I've added a suggestion to parameterize this new test to cover more floating-point types (float16, float32, float64), which will make it more robust.
| @tvm.testing.requires_llvm | ||
| def test_llvm_cast_float_to_bool(): | ||
| a_np = np.array([0.0, 1.0, np.nan, np.inf], dtype="float32") | ||
| n = a_np.shape[0] | ||
|
|
||
| A = te.placeholder((n,), name="A", dtype="float32") | ||
| C = te.compute((n,), lambda i: A[i].astype("bool"), name="C") | ||
|
|
||
| # Convert to TIR and create schedule | ||
| mod = te.create_prim_func([A, C]) | ||
| sch = tir.Schedule(mod) | ||
|
|
||
| # build and invoke the kernel. | ||
| f = tvm.compile(sch.mod, target="llvm") | ||
| dev = tvm.cpu(0) | ||
|
|
||
| # launch the kernel. | ||
| a = tvm.runtime.tensor(a_np, 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 case that covers the essential scenarios for casting floats to booleans. To make it even more comprehensive, I suggest parameterizing it to run against multiple float dtypes (float16, float32, and float64). This will ensure the fix holds for different precisions and improve test coverage.
@tvm.testing.requires_llvm
@pytest.mark.parametrize("dtype", ["float16", "float32", "float64"])
def test_llvm_cast_float_to_bool(dtype):
if dtype == "float16" and tvm.target.codegen.llvm_version_major() < 8:
pytest.skip("float16 support requires LLVM 8 or greater")
a_np = np.array([0.0, 1.0, np.nan, np.inf], dtype=dtype)
n = a_np.shape[0]
A = te.placeholder((n,), name="A", dtype=dtype)
C = te.compute((n,), lambda i: A[i].astype("bool"), name="C")
# Convert to TIR and create schedule
mod = te.create_prim_func([A, C])
sch = tir.Schedule(mod)
# build and invoke the kernel.
f = tvm.compile(sch.mod, target="llvm")
dev = tvm.cpu(0)
# launch the kernel.
a = tvm.runtime.tensor(a_np, 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)
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