You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "jikechao (via GitHub)" <gi...@apache.org> on 2025/10/24 07:10:50 UTC
[PR] Support integer types in TIR expression operators [tvm]
jikechao opened a new pull request, #18390:
URL: https://github.com/apache/tvm/pull/18390
This PR addresses the issue where `tvm.tir.exp` does not support integer types (e.g., int32, int64), causing an InternalError during LLVM code generation with the message.
The issue arises because the `llvm.exp` intrinsic expects floating-point inputs, but no type conversion is performed for integer inputs.
I opened this PR to solve it via type conversion. This change aligns the behavior of `tir.exp` with libraries like **PyTorch** and **NumPy**, which implicitly convert integer inputs to floating-point types for their exponential functions.
Fix https://github.com/apache/tvm/issues/18381
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18390:
URL: https://github.com/apache/tvm/pull/18390#discussion_r2459111818
##########
python/tvm/tir/op.py:
##########
@@ -2116,6 +2116,8 @@ def exp(x):
The result.
"""
x = tir.convert(x)
+ if "int" in x.dtype:
+ x = tir.Cast("float64", x)
Review Comment:

The current check `if "int" in x.dtype:` is fragile as it only handles signed integer types. It will not correctly handle unsigned integer types (e.g., `uint32`) or boolean types (`bool`), which should also be cast to float for the `exp` operation. A more robust approach is to use `tvm.DataType` to check if the dtype is any integer type (signed or unsigned). This ensures all relevant integer-like types are correctly handled.
```suggestion
if tvm.DataType(x.dtype).is_int() or tvm.DataType(x.dtype).is_uint():
x = tir.Cast("float64", x)
```
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "cbalint13 (via GitHub)" <gi...@apache.org>.
cbalint13 commented on code in PR #18390:
URL: https://github.com/apache/tvm/pull/18390#discussion_r2462703146
##########
tests/python/tir-base/test_tir_intrin.py:
##########
Review Comment:
The ```exp``` is not tested with ints within whole ```test_unary_intrin()```.
* Add similar test like this exception here for the ```exp``` part, using ```np.randim.int()``` or similar
* Could also make an extra assertion that result is coming back as ```float32``` type always for now
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "jikechao (via GitHub)" <gi...@apache.org>.
jikechao commented on PR #18390:
URL: https://github.com/apache/tvm/pull/18390#issuecomment-3445895604
> Idea for future,
>
> Beside this casting to float (simplest) I think we could try using a LUT like implementation assuring that we truly emit integer ISA counter part from llvm (staying in the int domain), so we could also tackle on a good integer `softmax()` implementation, TVM being a compiler I would expect such capability.
>
> See: https://github.com/google/gemmlowp/blob/16e8662c34917be0065110bfcd9cc27d30f52fdf/fixedpoint/fixedpoint.h#L781-L800
Hi @cbalint13, thanks for your review and suggestion. While LUT is well-suited for softmax due to its normalized output and constrained input range, implementing `exp(x)` for arbitrary integers is challenging and may not yield optimal performance because of the exponential function's vast output range and the complexity of fixed-point arithmetic for large values.
For a quick fix, I prepared this patch as a workaround.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #18390:
URL: https://github.com/apache/tvm/pull/18390#issuecomment-3441440982
## Summary of Changes
Hello @jikechao, 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 introduces a crucial enhancement to the `tvm.tir.exp` operator, enabling it to seamlessly process integer input types. Previously, attempting to use integer inputs with `tir.exp` would lead to an `InternalError` during LLVM code generation due to the underlying `llvm.exp` intrinsic expecting floating-point values. The implemented solution automatically casts integer inputs to `float64`, ensuring robust and consistent behavior, and bringing `tir.exp`'s functionality in line with established deep learning frameworks.
### Highlights
* **Integer Type Support for tir.exp**: The `tvm.tir.exp` operator now correctly handles integer input types, resolving an `InternalError` during LLVM code generation.
* **Automatic Type Conversion**: Integer inputs are implicitly converted to `float64` before the exponential operation, ensuring compatibility with the underlying `llvm.exp` intrinsic.
* **Behavior Alignment**: This change aligns `tir.exp`'s behavior with other popular libraries like PyTorch and NumPy, which also perform implicit type conversion for exponential functions.
<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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "cbalint13 (via GitHub)" <gi...@apache.org>.
cbalint13 commented on code in PR #18390:
URL: https://github.com/apache/tvm/pull/18390#discussion_r2462703146
##########
tests/python/tir-base/test_tir_intrin.py:
##########
Review Comment:
The ```exp``` is not tested with ints within whole ```test_unary_intrin()```.
* Add similar test like this exception here for the ```exp``` ints part, using ```np.randim.int()``` or similar
* Could also make an extra assertion that result is coming back as ```float32``` type always for now
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "cbalint13 (via GitHub)" <gi...@apache.org>.
cbalint13 commented on code in PR #18390:
URL: https://github.com/apache/tvm/pull/18390#discussion_r2461838490
##########
python/tvm/tir/op.py:
##########
@@ -2116,6 +2116,8 @@ def exp(x):
The result.
"""
x = tir.convert(x)
+ if "int" in x.dtype:
+ x = tir.Cast("float64", x)
Review Comment:
Wouldn't be```float32``` just enough (I am not sure), it would be more HW friendly.
But in any case, please introduce a integer test to cover this ```tir.exp``` here:
https://github.com/apache/tvm/blob/main/tests/python/tir-base/test_tir_intrin.py#L67
Lets make sure that slipping to integer domain will hold up with 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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Support integer types in TIR expression operators [tvm]
Posted by "cbalint13 (via GitHub)" <gi...@apache.org>.
cbalint13 merged PR #18390:
URL: https://github.com/apache/tvm/pull/18390
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org