Skip to content

[DOCS] Update tutorial for exporting and loading back Relax executables#18412

Merged
tqchen merged 3 commits intoapache:mainfrom
tlopex:tut3
Nov 2, 2025
Merged

[DOCS] Update tutorial for exporting and loading back Relax executables#18412
tqchen merged 3 commits intoapache:mainfrom
tlopex:tut3

Conversation

@tlopex
Copy link
Member

@tlopex tlopex commented Nov 1, 2025

This pr updates relax.build to tvm.compile, and fixes some bugs when we get output and on the website

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @tlopex, 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 updates the tutorial for exporting and loading Relax executables. The primary goal is to reflect recent API changes by migrating the compilation step from relax.build to tvm.compile. Additionally, it includes a fix to improve the reliability of output extraction from the TVM runtime, ensuring the tutorial remains accurate and functional.

Highlights

  • API Update: The relax.build function has been replaced with tvm.compile for generating executables, aligning the tutorial with the latest TVM API.
  • Output Handling Improvement: The logic for extracting the result_tensor from tvm_output has been enhanced to more robustly handle cases where the output might be an empty sequence or a non-sequence type, fixing a potential bug.
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 updates the tutorial for exporting and loading Relax executables by replacing the deprecated relax.build with tvm.compile. While this is a good update, the associated changes to handle model outputs are buggy. The logic for unpacking the output tensor is not robust and can lead to incorrect behavior for single-tensor outputs. Additionally, the example script in the documentation contains incorrect variable names that would cause it to fail. I have provided suggestions to fix these issues.

Comment on lines +179 to +182
if hasattr(tvm_output, "__len__") and len(tvm_output) > 0:
result_tensor = tvm_output[0]
else:
result_tensor = tvm_output
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The logic to extract the result tensor is not robust. The check hasattr(tvm_output, "__len__") is too broad and can lead to incorrect behavior for single tensor outputs. For example, a tvm.runtime.NDArray has a __len__ attribute, and this check would cause it to be sliced, which is likely not the intended behavior for a single tensor output.

A more precise way to handle this is to check if the output is a sequence type that is not a tensor, like tvm.runtime.Array, tuple, or list.

I suggest the following change for a more robust implementation:

Suggested change
if hasattr(tvm_output, "__len__") and len(tvm_output) > 0:
result_tensor = tvm_output[0]
else:
result_tensor = tvm_output
if isinstance(tvm_output, (tvm.runtime.Array, tuple, list)):
result_tensor = tvm_output[0]
else:
result_tensor = tvm_output

Comment on lines +268 to +271
# if hasattr(tvm_output, "__len__") and len(tvm_output) > 0:
# result_tensor = tvm_output[0]
# else:
# result_tensor = tvm_output
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This example code has a few issues:

  1. It uses tvm_output, but the variable defined in this example script is output (from output = vm["main"](input_tensor, *params)).
  2. It defines result_tensor, but the following print statements use result. This will cause a NameError.
  3. The logic for unpacking the output is incorrect for single tensor outputs, similar to the issue in the main script.

I suggest correcting the variable names and using a more robust check for the output type.

Suggested change
# if hasattr(tvm_output, "__len__") and len(tvm_output) > 0:
# result_tensor = tvm_output[0]
# else:
# result_tensor = tvm_output
# if isinstance(output, (tvm.runtime.Array, tuple, list)):
# result = output[0]
# else:
# result = output

@tlopex
Copy link
Member Author

tlopex commented Nov 1, 2025

cc @tqchen @MasterJH5574

Remove print statement for skipping model conversion.
@tqchen tqchen merged commit 8f1145d into apache:main Nov 2, 2025
12 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