Skip to content

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

Closed
tqchen wants to merge 2 commits intoapache:mainfrom
tqchen:pep563-remove-args-converter
Closed

[PYTHON] Fix PEP 563 compat and remove args_converter#18846
tqchen wants to merge 2 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 significantly refactors the argument conversion and type checking mechanisms within the Relax framework. It enhances compatibility with modern Python type annotation practices by correctly resolving postponed evaluations and replaces an implicit, decorator-based argument conversion system with explicit function calls. This change aims to improve the robustness, clarity, and maintainability of the codebase by making argument handling more transparent and less reliant on automatic inference.

Highlights

  • PEP 563 Compatibility for @type_checked: The @type_checked decorator has been updated to correctly handle type annotations under PEP 563 (from __future__ import annotations) by utilizing typing.get_type_hints() for annotation resolution.
  • Removal of @args_converter.auto: The @args_converter.auto decorator has been entirely removed from the codebase. Argument conversion is now handled explicitly through direct calls to convert_to_expr or _wrap_inline_arg_tuple at approximately 9 call sites.
  • Codebase Cleanup: The python/tvm/relax/type_converter.py file and its corresponding test file (tests/python/relax/test_expr_args_converter.py) have been deleted, simplifying the project structure.

🧠 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 the import of args_converter.
  • python/tvm/relax/op/base.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto decorators 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.
    • Added explicit _wrap_inline_arg_tuple or convert_to_expr calls for argument handling in affected functions.
  • python/tvm/relax/op/builtin/builtin.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto from alloc_tensor.
    • Explicitly converted the shape argument to an Expr in alloc_tensor.
  • python/tvm/relax/op/distributed/distributed.py
    • Removed the args_converter import.
    • Removed @args_converter.auto from call_tir_local_view.
    • Explicitly converted args to RxTuple in call_tir_local_view.
  • python/tvm/relax/op/index.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto from strided_slice.
    • Explicitly converted axes, begin, end, and strides arguments to Expr in strided_slice.
  • python/tvm/relax/op/memory/memory.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto from alloc_storage, alloc_tensor, kill_storage, and kill_tensor.
    • Explicitly converted size and shape arguments to Expr in alloc_storage and alloc_tensor respectively.
  • python/tvm/relax/op/sampling.py
    • Removed the args_converter import.
    • Removed @args_converter.auto decorator from multinomial_from_uniform.
  • python/tvm/relax/op/unary.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto from clip.
    • Explicitly converted min and max arguments to Expr in clip.
  • python/tvm/relax/op/vm/vm.py
    • Replaced args_converter import with convert_to_expr.
    • Removed @args_converter.auto from alloc_storage, alloc_tensor, and call_tir_dyn.
    • Explicitly converted shape and func arguments to Expr in alloc_storage, alloc_tensor, and call_tir_dyn respectively.
  • python/tvm/relax/type_converter.py
    • Deleted the entire file.
  • python/tvm/relax/utils.py
    • Removed the import and re-export of args_converter.
  • python/tvm/s_tir/schedule/_type_checker.py
    • Updated the type_checked decorator to use typing.get_type_hints() for resolving type annotations, improving compatibility with PEP 563.
  • python/tvm/script/ir_builder/relax/distributed/ir.py
    • Removed the args_converter import.
    • Removed @args_converter.auto from call_tir.
    • Explicitly converted args to RxTuple in call_tir.
  • python/tvm/script/ir_builder/relax/ir.py
    • Replaced args_converter import with convert_to_expr.
    • Defined _CONVERTIBLE_TYPES for explicit conversion checks.
    • Removed @args_converter.auto from call_packed and call_py_func.
    • Explicitly converted args to Expr in call_packed and call_py_func.
  • tests/python/relax/test_expr_args_converter.py
    • Deleted the entire test file.
Activity
  • The first commit addressed the @type_checked decorator's compatibility with PEP 563, ensuring proper resolution of type annotations, and all 632 s_tir/schedule tests passed.
  • The second commit focused on removing the @args_converter.auto decorator, replacing its functionality with explicit convert_to_expr or _wrap_inline_arg_tuple calls across 10 files, with 4892 relax tests passing (excluding 10 pre-existing failures).
  • CI is expected to pass, indicating the changes are stable and functional.
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 large refactoring that removes the @args_converter.auto decorator and replaces it with explicit calls to convert_to_expr, and also fixes PEP 563 compatibility in the @type_checked decorator. The changes are generally well-executed. However, I've identified a couple of potential bugs in the distributed operators where arguments from Python lists/tuples are not converted to relax.Expr, which could cause runtime errors. I've also found numerous instances where the new explicit conversion logic is more complex than necessary and have suggested simplifications to improve code readability and maintainability. The rest of the changes look good.

"""
if isinstance(args, Expr) and not isinstance(args, RxTuple): # type: ignore
if isinstance(args, tuple | list):
args = RxTuple(list(args))
Copy link
Contributor

Choose a reason for hiding this comment

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

high

When args is a tuple or list, its elements are not being converted to relax.Expr before being passed to RxTuple. This can lead to a TypeError if args contains non-Expr elements like integers or floats. You can use convert_to_expr to handle this conversion, which will also create the relax.Tuple. You will also need to add from ...utils import convert_to_expr at the top of the file.

Suggested change
args = RxTuple(list(args))
args = convert_to_expr(args)


if isinstance(args, Expr) and not isinstance(args, RxTuple): # type: ignore
if isinstance(args, tuple | list):
args = RxTuple(list(args))
Copy link
Contributor

Choose a reason for hiding this comment

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

high

When args is a tuple or list, its elements are not being converted to relax.Expr before being passed to RxTuple. This can lead to a TypeError if args contains non-Expr elements like integers or floats. You can use convert_to_expr to handle this conversion, which will also create the relax.Tuple. You will also need to add from tvm.relax.utils import convert_to_expr at the top of the file.

Suggested change
args = RxTuple(list(args))
args = convert_to_expr(args)

Comment on lines +97 to +98
if not isinstance(shape, Expr):
shape = convert_to_expr(shape)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This check is redundant because convert_to_expr already handles Expr inputs by returning them as is. You can simplify this by calling convert_to_expr unconditionally.

    shape = convert_to_expr(shape)

Comment on lines +786 to +789
args = tuple(
convert_to_expr(a) if isinstance(a, int | float | str | tuple | list) else a
for a in args
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to a previous comment, this argument conversion logic can be simplified. convert_to_expr is idempotent for Expr inputs, so you can apply it to all arguments unconditionally.

    args = tuple(convert_to_expr(a) for a in args)

Comment on lines +103 to +107
axes = convert_to_expr(axes) if not isinstance(axes, Expr) else axes
begin = convert_to_expr(begin) if not isinstance(begin, Expr) else begin
end = convert_to_expr(end) if not isinstance(end, Expr) else end
if strides is not None and not isinstance(strides, Expr):
strides = convert_to_expr(strides)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This conversion logic can be simplified. convert_to_expr is idempotent on Expr inputs, so the isinstance checks are not necessary.

Suggested change
axes = convert_to_expr(axes) if not isinstance(axes, Expr) else axes
begin = convert_to_expr(begin) if not isinstance(begin, Expr) else begin
end = convert_to_expr(end) if not isinstance(end, Expr) else end
if strides is not None and not isinstance(strides, Expr):
strides = convert_to_expr(strides)
axes = convert_to_expr(axes)
begin = convert_to_expr(begin)
end = convert_to_expr(end)
if strides is not None:
strides = convert_to_expr(strides)

Comment on lines +97 to +98
if not isinstance(shape, Expr):
shape = convert_to_expr(shape)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This check is redundant because convert_to_expr already handles Expr inputs by returning them as is. You can simplify this by calling convert_to_expr unconditionally.

    shape = convert_to_expr(shape)

Comment on lines +138 to +139
if not isinstance(func, Expr):
func = convert_to_expr(func)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This check is redundant because convert_to_expr already handles Expr inputs by returning them as is. You can simplify this by calling convert_to_expr unconditionally.

    func = convert_to_expr(func)

Comment on lines +732 to +735
args = tuple(
convert_to_expr(a) if isinstance(a, int | float | str | tuple | list) else a
for a in args
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This argument conversion logic can be simplified. convert_to_expr handles Expr inputs by returning them directly, so you don't need to conditionally call it. Applying it to all arguments is simpler and has the same effect.

    args = tuple(convert_to_expr(a) for a in args)

Comment on lines +432 to +434
args = py_tuple(
convert_to_expr(a) if isinstance(a, _CONVERTIBLE_TYPES) else a for a in args
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This argument conversion logic can be simplified. convert_to_expr handles Expr inputs by returning them directly, and also handles all types in _CONVERTIBLE_TYPES, so you don't need to conditionally call it. Applying it to all arguments is simpler and has the same effect.

    args = py_tuple(convert_to_expr(a) for a in args)

Comment on lines +491 to +493
args = py_tuple(
convert_to_expr(a) if isinstance(a, _CONVERTIBLE_TYPES) else a for a in args
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to a previous comment, this argument conversion logic can be simplified. convert_to_expr is idempotent for Expr inputs, so you can apply it to all arguments unconditionally.

    args = py_tuple(convert_to_expr(a) for a in args)

@tqchen tqchen closed this Feb 28, 2026
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.

1 participant