Skip to content

Conversation

@AshwiniBokka
Copy link

Fixes #18584

Problem

The tutorial docs/how_to/tutorials/customize_opt.py fails with "No module named 'tvm_ffi'" when TVM is not built. This happens because the tutorial imports tvm.relax.backend.cuda.cublas which requires TVM to be built with CUDA support.

Solution

  1. Add clear prerequisite note about building TVM with CUDA
  2. Add helpful error handling with guidance when import fails

Changes

  • Added note after imports explaining build requirements
  • Wrapped problematic import with try/except
  • Provides clear error message pointing to build instructions
  • Follows Python best practices (stderr, proper indentation)

Testing

  • File compiles without syntax errors
  • Error message clearly guides users to solution

Before

import tvm.relax.backend.cuda.cublas as _cublas
# Users get: No module named 'tvm_ffi'

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @AshwiniBokka, 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 addresses a common ImportError encountered by users of the customize_opt.py tutorial when TVM is not built with CUDA. The changes improve the user experience by adding clear documentation about the CUDA build requirement and implementing a try-except block to gracefully handle the missing module, providing actionable guidance. Additionally, it updates license-related URLs to use HTTPS.

Highlights

  • Enhanced Tutorial Robustness: The customize_opt.py tutorial now includes explicit prerequisites and robust error handling for the tvm.relax.backend.cuda.cublas import, guiding users if TVM is not built with CUDA support.
  • Improved Error Messaging: A try-except block has been added to catch ImportError related to tvm_ffi, providing a clear error message to stderr with instructions on how to build TVM with CUDA.
  • URL Protocol Update: Several http links to Apache licenses in LICENSE and README.md files have been updated to https for improved security and consistency.

🧠 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 addresses an import error in the 'Customize Optimization' tutorial that occurs when TVM is not built with CUDA support. The changes include adding a note about the CUDA dependency and implementing a try-except block to catch the ImportError and provide a user-friendly error message. The changes are well-aligned with the problem description. My review includes a critical fix for a Byte Order Mark (BOM) character that was introduced, and a suggestion to make the error handling more robust by checking for CUDA feature availability directly instead of parsing the exception message.

@@ -1,4 +1,4 @@
# Licensed to the Apache Software Foundation (ASF) under one
# Licensed to the Apache Software Foundation (ASF) under one
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The file starts with a UTF-8 Byte Order Mark (BOM), which can cause issues with script execution in some environments. It should be removed.

Suggested change
# Licensed to the Apache Software Foundation (ASF) under one
# Licensed to the Apache Software Foundation (ASF) under one

Comment on lines 112 to 122
try:
import tvm.relax.backend.cuda.cublas as _cublas
except ImportError as e:
if "tvm_ffi" in str(e):
import sys
print("Error: TVM needs to be built with CUDA support.", file=sys.stderr)
print("Please build TVM from source with CUDA enabled.", file=sys.stderr)
print("See: https://tvm.apache.org/docs/install/from_source.html", file=sys.stderr)
sys.exit(1)
else:
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Checking the exception string for "tvm_ffi" is a bit brittle, as the error message could change in future TVM versions. A more robust approach is to use tvm.runtime.enabled("cuda") to check if CUDA support is compiled in. This directly checks for the feature availability rather than relying on a specific error message string.

Suggested change
try:
import tvm.relax.backend.cuda.cublas as _cublas
except ImportError as e:
if "tvm_ffi" in str(e):
import sys
print("Error: TVM needs to be built with CUDA support.", file=sys.stderr)
print("Please build TVM from source with CUDA enabled.", file=sys.stderr)
print("See: https://tvm.apache.org/docs/install/from_source.html", file=sys.stderr)
sys.exit(1)
else:
raise
try:
import tvm.relax.backend.cuda.cublas as _cublas
except ImportError:
import tvm.runtime
if not tvm.runtime.enabled("cuda"):
import sys
print("Error: This tutorial requires TVM to be built with CUDA support.", file=sys.stderr)
print("Please build TVM from source with CUDA enabled.", file=sys.stderr)
print("See: https://tvm.apache.org/docs/install/from_source.html", file=sys.stderr)
sys.exit(1)
else:
raise

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
https://www.apache.org/licenses/LICENSE-2.0
Copy link
Member

Choose a reason for hiding this comment

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

why do we need to update license here?

from tvm.relax.frontend import nn

# Note: This tutorial requires TVM to be built with CUDA support.
# If you encounter 'No module named ''tvm_ffi''' error,
Copy link
Member

Choose a reason for hiding this comment

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

The statement might not be correct, tvm_ffi is now a independent repo (and 3rdparty in tvm), I think we just need to pip install tvm-ffi if we find the No module named tvm_ffi error.

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] Customize Optimization Tutorial Error

2 participants