Skip to content

Fix Customize Optimization tutorial import error #18584#18697

Open
AshwiniBokka wants to merge 14 commits intoapache:mainfrom
AshwiniBokka:clean-tutorial-fix-18584
Open

Fix Customize Optimization tutorial import error #18584#18697
AshwiniBokka wants to merge 14 commits intoapache:mainfrom
AshwiniBokka:clean-tutorial-fix-18584

Conversation

@AshwiniBokka
Copy link
Contributor

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.

- Provide better solution: suggest pip install tvm-ffi first
- Only fall back to full TVM build if needed
- Clearer error message with numbered options
- Thanks @yongwww for the insights on tvm_ffi installation
@AshwiniBokka
Copy link
Contributor Author

@yongwww Thanks for the insights! Updated to:

  1. Suggest pip install tvm-ffi first
  2. Only fall back to full TVM build
  3. Clearer error message with options

CI was failing with: SyntaxError: invalid non-printable character U+FEFF
Re-saved file as UTF-8 without BOM in VS Code.
- Remove broken 'else: raise' that always raised even on success
- Remove unreachable sys.exit(1) after raise
- Keep single clean try-except for CUDA check
- Restore simple import for tutorial code
- All syntax and formatting checks pass
@AshwiniBokka
Copy link
Contributor Author

Thank you @yongwww @tqchen for the guidance. This PR is now ready for merge:

  1. All CI checks passed
  2. Better user experience for missing CUDA
  3. Clean error handling without breaking tutorial flow
  4. Fixed existing bugs in tutorial

The tutorial now:

  1. Helpfully guides users to install tvm-ffi first
  2. Falls back to full TVM build if needed
  3. Exits early with clear instructions if CUDA missing
  4. Maintains all original functionality when CUDA available

Ready for final review/merge! 🙏

@AshwiniBokka AshwiniBokka requested a review from yongwww February 1, 2026 15:54
mshr-h pushed a commit that referenced this pull request Feb 8, 2026
## Summary
Adds Windows-specific build notes to the TVM installation documentation
(`docs/install/from_source.rst`).

## Changes
- Added "Windows-Specific Build Notes" section after "Step 5. Extra
Python Dependencies"
- Covers common Windows issues and solutions based on contributor
experience

## Content Includes:
1. **File Encoding**: UTF-8 without BOM (prevents `SyntaxError: invalid
non-printable character U+FEFF` in CI)
2. **Path Conventions**: Forward vs backslash usage in Python/CMake
3. **CUDA Configuration**: Environment setup for CUDA builds on Windows
4. **CMake & Compiler**: Visual Studio generator setup and Python path
configuration
5. **Common Issues**: Solutions for frequent Windows build problems
6. **Development Tips**: Git configuration for line endings, VS Code
settings
7. **WSL2 Alternative**: Linux-like environment option for Windows users

## Motivation
As a Windows contributor to TVM, I encountered several platform-specific
issues that could be prevented with better documentation. These notes
will help future Windows users:
- Avoid BOM character errors that cause CI failures
- Configure paths correctly for TVM's build system
- Set up CUDA development environment on Windows
- Troubleshoot common Windows-specific build failures

## Testing
- Verified RST syntax and formatting
- Checked logical placement within document flow  
- Content based on actual Windows development experience with TVM

## Related
- Previous contribution: #18697 (improved tutorial error handling)
- Addresses undocumented Windows-specific considerations in build
process
@AshwiniBokka
Copy link
Contributor Author

@tlopex @yongwww Update: The PR now contains only the tutorial fix as requested.

Changes:

  • Better error handling for CUDA import in customize_opt.py
  • Helpful guidance for users (pip install tvm-ffi or build TVM)
  • No LICENSE/README modifications
  • All CI checks pass

Ready for final review! 🙏

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

3 participants