Skip to content

Conversation

@nan2088
Copy link
Contributor

@nan2088 nan2088 commented Jan 16, 2026

This PR partially adds support for older versions of PyTorch (Enables successful import tvm_ffi). Previous discussion: #381

This is especially helpful in scenarios where the environment uses an older PyTorch version and patching/hacking PyTorch is not feasible.

Verified with PyTorch 1.10.2, 1.14.0a0, and 2.0

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @nan2088, 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 significantly improves the tvm_ffi library's compatibility with a wider range of PyTorch versions, including older ones like 1.10.2, 1.14.0a0, and 2.0. The core change involves making the internal data type mapping more robust by dynamically checking for the existence of specific PyTorch data types before attempting to map them. This prevents import errors in environments where certain newer data types might not be defined, ensuring that import tvm_ffi succeeds across different PyTorch setups.

Highlights

  • PyTorch Version Compatibility: The pull request enhances compatibility with older PyTorch versions by modifying how PyTorch data types are mapped to DLDataType. Previously, certain data types were directly included, which could cause issues if those types were not present in older PyTorch installations. Now, these mappings are conditionally added only if the specific data type exists in the torch module.
  • Refactored Dtype Mapping Logic: The direct inclusion of torch.uint16, torch.uint32, torch.uint64, and various torch.float8 types in the TORCH_DTYPE_TO_DL_DATA_TYPE dictionary has been removed. Instead, a new extra_types list is introduced, and a loop iterates through this list, adding types to the dictionary only if hasattr(torch, attr_name) confirms their presence in the current PyTorch environment.

🧠 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 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 is a great improvement for compatibility with older PyTorch versions. By conditionally registering dtypes that may not be present in all versions, you've made the tvm_ffi import more robust. The refactoring to use a single loop for all optional dtypes is a clean and maintainable approach. The implementation is correct and effectively solves the issue. As a potential follow-up, consider updating the tests in tests/python/test_dtype.py to also be conditional, which would ensure the test suite passes on environments with older PyTorch versions.

Comment on lines +193 to +203
extra_types = [
("uint16", DLDataType(1, 16, 1)),
("uint32", DLDataType(1, 32, 1)),
("uint64", DLDataType(1, 64, 1)),
("float8_e4m3fn", DLDataType(10, 8, 1)),
("float8_e4m3fnuz", DLDataType(11, 8, 1)),
("float8_e5m2", DLDataType(12, 8, 1)),
("float8_e5m2fnuz", DLDataType(13, 8, 1)),
("float8_e8m0fnu", DLDataType(14, 8, 1)),
("float4_e2m1fn_x2", DLDataType(17, 4, 2)),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For a constant data structure like extra_types, it is a common Python convention to use a tuple of tuples instead of a list of tuples. This makes the data structure immutable, which prevents accidental modifications at runtime and more clearly signals the intent that this data is constant. This can also offer a minor performance benefit.

    extra_types = (
        ("uint16", DLDataType(1, 16, 1)),
        ("uint32", DLDataType(1, 32, 1)),
        ("uint64", DLDataType(1, 64, 1)),
        ("float8_e4m3fn", DLDataType(10, 8, 1)),
        ("float8_e4m3fnuz", DLDataType(11, 8, 1)),
        ("float8_e5m2", DLDataType(12, 8, 1)),
        ("float8_e5m2fnuz", DLDataType(13, 8, 1)),
        ("float8_e8m0fnu", DLDataType(14, 8, 1)),
        ("float4_e2m1fn_x2", DLDataType(17, 4, 2)),
    )

@junrushao
Copy link
Member

Thanks for contributing! This PR looks good to me

@junrushao junrushao merged commit 4fec972 into apache:main Jan 17, 2026
8 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.

2 participants