You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "locnd182644 (via GitHub)" <gi...@apache.org> on 2026/01/08 08:05:02 UTC

[PR] [LLVM][Codegen] Cast NAN to bool gives true [tvm]

locnd182644 opened a new pull request, #18646:
URL: https://github.com/apache/tvm/pull/18646

   ### Summary
   Cast NAN to bool gives true to ensure consistency with the existing framework (C, C++, Python, Torch, NumPy, OnnxRuntime, ...).
   
   ### Steps to Reproduce
   - Python:
   ```
   bool(float('nan'))
   ```
   > True
   
   - Torch:
   ```
   torch.tensor(float("nan"), dtype=torch.float32).to(torch.bool)
   ```
   > tensor(True)
   
   - Numpy:
   ```
   import numpy as np
   bool(np.nan)
   ```
   > True
   
   - TVM:
   ```
   class Module:
       def main(x: R.Tensor((), dtype="float32")) -> R.Tensor((), dtype="bool"):
           with R.dataflow():
               gv: R.Tensor((), dtype="bool") = R.astype(x, dtype="bool")
               R.output(gv)
           return gv
   x = np.array(float("nan"), dtype="float32")
   ```
   > False
   
   ### Expected
   - TVM:
   ```
   class Module:
       def main(x: R.Tensor((), dtype="float32")) -> R.Tensor((), dtype="bool"):
           with R.dataflow():
               gv: R.Tensor((), dtype="bool") = R.astype(x, dtype="bool")
               R.output(gv)
           return gv
   x = np.array(float("nan"), dtype="float32")
   ```
   > True
   
   ### Resolved
   - Replace the instruction `fcmp one` with `fcmp une` in LLVM.
   - Citation: https://releases.llvm.org/20.1.0/docs/LangRef.html#fcmp-instruction
   <img width="400" height="200" alt="PR1-18605" src="https://github.com/user-attachments/assets/cffeebd8-dfe6-436e-9c4c-61e1e84d5439" />
   
   - Fixed: #18605


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "locnd182644 (via GitHub)" <gi...@apache.org>.
locnd182644 commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3889656381

   @tvm-bot rerun


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "mshr-h (via GitHub)" <gi...@apache.org>.
mshr-h commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3744124756

   @tvm-bot rerun


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NAN to bool gives true [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3722631823

   ## Summary of Changes
   
   Hello @locnd182644, I'm Gemini Code Assist[^1]! 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 `NaN` value to a boolean type resulted in `False`, contrary to common programming language conventions. By modifying the underlying LLVM comparison instruction, the system now correctly interprets `NaN` as `True` when converted to a boolean, enhancing compatibility and ensuring predictable behavior across different computational frameworks.
   
   ### Highlights
   
   * **Standardizing NaN to Bool Casting**: Aligns TVM's LLVM codegen behavior for casting `NaN` (Not a Number) to boolean with established frameworks like C, C++, Python, Torch, NumPy, and OnnxRuntime, where `NaN` evaluates to `True`.
   * **LLVM Instruction Update**: Implements this change by replacing the `fcmp one` instruction with `fcmp une` in the LLVM codegen, specifically within the `CreateCast` function for float-to-bool conversions.
   * **New Test Case**: Introduces `test_llvm_cast_float_to_bool` to validate the updated casting logic for various float values, including `0.0`, `1.0`, `NaN`, and `Infinity`, ensuring they correctly map to `False`, `True`, `True`, and `True` respectively.
   
   🧠 **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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   
   
   
   
   
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
   
   Feature | Command | Description
   --- | --- | ---
   Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
   Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
   Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
   Help | `/gemini help` | Displays a list of available commands.
   
   <b>Customization</b>
   
   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 `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>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](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen closed pull request #18646: [LLVM][Codegen] Cast NaN to bool gives true
URL: https://github.com/apache/tvm/pull/18646


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "locnd182644 (via GitHub)" <gi...@apache.org>.
locnd182644 commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3894530475

   @tqchen Thank you for feedback. I will create new PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #18646:
URL: https://github.com/apache/tvm/pull/18646#discussion_r2798572689


##########
tests/python/codegen/test_target_codegen_llvm.py:
##########
@@ -378,6 +378,31 @@ def check_llvm(n):
     check_llvm(64)
 
 
+@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")

Review Comment:
   use tvmscript instead of the te schedule in as in other part of the tests



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3890618772

   we had a few changes to CI recently, please consider reland the PR by opening new one on main, sorry about the trouble


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "locnd182644 (via GitHub)" <gi...@apache.org>.
locnd182644 commented on code in PR #18646:
URL: https://github.com/apache/tvm/pull/18646#discussion_r2802029593


##########
tests/python/codegen/test_target_codegen_llvm.py:
##########
@@ -378,6 +378,31 @@ def check_llvm(n):
     check_llvm(64)
 
 
+@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")

Review Comment:
   I will edit test code in new PR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NAN to bool gives true [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18646:
URL: https://github.com/apache/tvm/pull/18646#discussion_r2671332349


##########
tests/python/codegen/test_target_codegen_llvm.py:
##########
@@ -378,6 +378,31 @@ def check_llvm(n):
     check_llvm(64)
 
 
+@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)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   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.
   
   ```python
   @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)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3850784533

   @tvm-bot rerun


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [LLVM][Codegen] Cast NaN to bool gives true [tvm]

Posted by "locnd182644 (via GitHub)" <gi...@apache.org>.
locnd182644 commented on PR #18646:
URL: https://github.com/apache/tvm/pull/18646#issuecomment-3895156294

   This PR will close. The new PR #18772 has been created.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org