Skip to content

[PYTHON] Fix PEP 563 compat and remove args_converter#18847

Merged
tqchen merged 3 commits intoapache:mainfrom
tqchen:pep563-remove-args-converter
Feb 28, 2026
Merged

[PYTHON] Fix PEP 563 compat and remove args_converter#18847
tqchen merged 3 commits intoapache:mainfrom
tqchen:pep563-remove-args-converter

Conversation

@tqchen
Copy link
Member

@tqchen tqchen commented Feb 28, 2026

Summary

Fix type_checked for PEP 563 compatibility and remove args_converter.auto decorator.

Changes

  • Resolve PEP 563 string annotations via typing.get_type_hints at decoration time
  • Replace implicit auto-conversion decorator with explicit convert_to_expr at call sites

This PR resolves string annotations from PEP 563 (from __future__ import
annotations) at decoration time using typing.get_type_hints(), preventing
TypeError crashes when @type_checked passes stringified annotations to
isinstance().
This PR removes the @args_converter.auto decorator that auto-converted
Python types to Relax Expr by inspecting annotations at decoration time.
This was PEP 563-incompatible (annotations become strings). The FFI/C++
layer handles type conversion directly, with minimal explicit conversion
added at call sites for Python tuples/lists and primitive types.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 refactors the argument conversion strategy within the Relax framework. It addresses compatibility issues with Python's PEP 563 for type annotations and transitions from an implicit auto-conversion decorator to explicit conversion calls. This change improves the clarity and maintainability of the code by making argument type handling more direct, while also streamlining the type-checking process.

Highlights

  • PEP 563 Compatibility: The type_checked decorator has been updated to correctly resolve PEP 563 string annotations using typing.get_type_hints, ensuring better compatibility with postponed evaluation of type annotations.
  • Removal of args_converter.auto: The implicit argument auto-conversion mechanism provided by the args_converter.auto decorator has been removed. This includes the deletion of the type_converter.py module and its associated test file.
  • Explicit Argument Conversion: All call sites that previously relied on args_converter.auto for implicit conversion of Python primitives to Relax Expr types now explicitly use convert_to_expr or similar logic to perform these conversions, making the conversion process more transparent and controlled.

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

Changelog
  • python/tvm/relax/init.py
    • Removed import of args_converter.
  • python/tvm/relax/op/base.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from call_tir, call_tir_with_grad, call_tir_inplace, call_dps_packed, call_py_func, call_builtin_with_ctx, make_closure, invoke_closure, call_inplace_packed, call_pure_packed, and invoke_pure_closure functions.
    • Introduced explicit calls to _wrap_inline_arg_tuple and convert_to_expr for argument handling in various functions.
  • python/tvm/relax/op/builtin/builtin.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from alloc_tensor.
    • Added explicit convert_to_expr call for the shape argument in alloc_tensor.
  • python/tvm/relax/op/distributed/distributed.py
    • Removed import of args_converter.
    • Removed @args_converter.auto decorator from call_tir_local_view.
    • Modified call_tir_local_view to explicitly handle tuple or list arguments by wrapping them in RxTuple.
  • python/tvm/relax/op/index.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from strided_slice.
    • Added explicit convert_to_expr calls for axes, begin, end, and strides arguments in strided_slice.
  • python/tvm/relax/op/memory/memory.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from alloc_storage, alloc_tensor, kill_storage, and kill_tensor.
    • Added explicit convert_to_expr calls for size in alloc_storage and shape in alloc_tensor.
  • python/tvm/relax/op/sampling.py
    • Removed import of args_converter.
    • Removed @args_converter.auto decorator from multinomial_from_uniform.
  • python/tvm/relax/op/unary.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from clip.
    • Added explicit convert_to_expr calls for min and max arguments in clip.
  • python/tvm/relax/op/vm/vm.py
    • Replaced import of args_converter with convert_to_expr.
    • Removed @args_converter.auto decorator from alloc_storage, alloc_tensor, and call_tir_dyn.
    • Added explicit convert_to_expr calls for shape in alloc_storage and alloc_tensor, and for func in call_tir_dyn.
  • python/tvm/relax/type_converter.py
    • Removed the entire file, which contained the _ArgsConverter class and args_converter utility.
  • python/tvm/relax/utils.py
    • Removed import and re-export of args_converter.
  • python/tvm/s_tir/schedule/_type_checker.py
    • Modified type_checked decorator to use typing.get_type_hints for resolving type annotations, improving PEP 563 compatibility.
  • python/tvm/script/ir_builder/relax/distributed/ir.py
    • Removed import of args_converter.
    • Removed @args_converter.auto decorator from call_tir.
    • Modified call_tir to explicitly handle tuple or list arguments by wrapping them in RxTuple.
  • python/tvm/script/ir_builder/relax/ir.py
    • Replaced import of args_converter with convert_to_expr.
    • Added _CONVERTIBLE_TYPES constant for clarity in type checking.
    • Removed @args_converter.auto decorator from call_packed and call_py_func.
    • Added explicit convert_to_expr calls for args in call_packed and call_py_func.
  • tests/python/relax/test_expr_args_converter.py
    • Removed the entire test file, as the args_converter utility it tested has been removed.
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 nice refactoring that removes the implicit args_converter decorator in favor of explicit conversions, which improves code clarity. It also includes an important fix for PEP 563 compatibility in the type checker. My review focuses on ensuring the new explicit conversions are correct and complete across all modified files. I've identified several instances where the conversion logic was either incomplete (e.g., not handling PrimExpr) or missing, which could lead to runtime errors. I've provided suggestions to fix these issues. Additionally, I've included some recommendations for code simplification and improving robustness.

- Fix distributed ops (distributed.py, distributed/ir.py): apply
  convert_to_expr element-wise when args is a list/tuple before
  constructing RxTuple, so non-Expr elements are properly converted.
- Simplify redundant isinstance(x, Expr) guards before convert_to_expr
  calls (convert_to_expr is idempotent on Expr inputs) in:
  memory/memory.py, op/base.py, op/index.py, op/unary.py, op/vm/vm.py,
  script/ir_builder/relax/ir.py.
@tqchen tqchen force-pushed the pep563-remove-args-converter branch from 7adf2c9 to 8aba2c4 Compare February 28, 2026 13:58
@tqchen tqchen merged commit 2fd4e11 into apache:main Feb 28, 2026
10 checks passed
@tqchen tqchen deleted the pep563-remove-args-converter branch February 28, 2026 21:34
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