Skip to content

Support integer types in TIR expression operators#18390

Merged
cbalint13 merged 4 commits into
apache:mainfrom
jikechao:fix_exp
Oct 27, 2025
Merged

Support integer types in TIR expression operators#18390
cbalint13 merged 4 commits into
apache:mainfrom
jikechao:fix_exp

Conversation

@jikechao

Copy link
Copy Markdown
Member

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 #18381

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @jikechao, 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 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.
Using Gemini Code Assist

The 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 /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.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 addresses an important issue by adding support for integer types in tvm.tir.exp, preventing an InternalError during LLVM code generation. The approach of casting integer inputs to float64 aligns well with the behavior of libraries like NumPy and PyTorch. I've suggested a small improvement to make the integer type detection more robust. Additionally, for consistency across the API, it would be beneficial to consider applying this same integer-to-float casting logic to other transcendental functions (e.g., log, sin, cos) that likely have similar requirements for floating-point inputs, perhaps in a follow-up pull request.

Comment thread python/tvm/tir/op.py Outdated
@cbalint13 cbalint13 self-assigned this Oct 24, 2025
Comment thread python/tvm/tir/op.py Outdated

@cbalint13 cbalint13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jikechao

Copy link
Copy Markdown
Member Author

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.

Comment thread tests/python/tir-base/test_tir_intrin.py
Added tests for 'exp' function to verify output against NumPy.
@jikechao jikechao requested a review from cbalint13 October 27, 2025 07:11

@cbalint13 cbalint13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me !

@cbalint13 cbalint13 merged commit 30fcca2 into apache:main Oct 27, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Cannot find intrinsic declaration, possible type mismatch: llvm.exp

2 participants