Skip to content

[Frontend][TFLite] Fix undefined symbols and Relay API remnants in TFLite frontend#18929

Merged
MasterJH5574 merged 1 commit into
apache:mainfrom
tlopex:tf1
Mar 25, 2026
Merged

[Frontend][TFLite] Fix undefined symbols and Relay API remnants in TFLite frontend#18929
MasterJH5574 merged 1 commit into
apache:mainfrom
tlopex:tf1

Conversation

@tlopex

@tlopex tlopex commented Mar 25, 2026

Copy link
Copy Markdown
Member

Summary

The TFLite frontend (#18868) was ported from Relay but contains several undefined symbols and Relay-specific APIs that cause runtime errors. This PR cleans up these issues so that working code paths are clean and broken paths fail with clear
NotImplementedError instead of NameError.

Changes

Fixed (now working):

  • convert_batch_matmul: Rewrote to use relax.op.matmul and struct_info.shape instead of Relay's shape_of / _fold_constant
    / _infer_type / _qnn.op.batch_matmul
  • flatten_to_nd: Rewrote to use struct_info.shape instead of _infer_type / _fold_constant
  • convert_shape: Replaced undefined shape_of() with relax.op.shape_of()
  • get_scalar_from_constant: Replaced _expr.Constant with relax.Constant
  • _convert_resize: Fixed undefined tflite.BuiltinOptions reference
  • Added missing from tflite.TensorType import TensorType in convert_gather, convert_sparse_to_dense,
    convert_transpose_conv, convert_dequantize

Guarded with explicit NotImplementedError:

  • convert_detection_postprocess: References relax.op.vision.{multibox_transform_loc, non_max_suppression, get_valid_counts} which don't exist yet
  • convert_nms_v5: Same issue
  • Quantized convert_batch_matmul path: References _qnn.op.batch_matmul
  • Quantization error message improved for clarity

Lint cleanup:

  • F821 noqa retained only for _qnn/_expr in unreachable code paths (guarded by NotImplementedError), with comment
    explaining why
  • Added noqa: F841 for standard dataflow context variable df

@gemini-code-assist

Copy link
Copy Markdown
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 enhances the robustness and clarity of the TFLite frontend by resolving numerous issues related to undefined symbols and outdated Relay API usages. The changes focus on porting existing functionalities to native Relax operations, providing explicit error handling for unsupported features, and improving overall code quality and maintainability. The goal is to ensure a more stable and predictable conversion process for TFLite models within the Relax framework.

Highlights

  • TFLite Frontend Refinements: The TFLite frontend has been significantly cleaned up, addressing several undefined symbols and remnants of Relay-specific APIs that caused runtime errors. This ensures that working code paths are robust and broken paths fail gracefully with clear NotImplementedError messages.
  • Batch Matmul Conversion: The convert_batch_matmul function was entirely rewritten to leverage relax.op.matmul and struct_info.shape for more accurate and native Relax tensor operations, replacing older Relay-specific methods. Support for quantized batch matmul is now explicitly guarded with a NotImplementedError.
  • Shape and Constant Handling: Functions like flatten_to_nd and convert_shape were updated to use struct_info.shape and relax.op.shape_of respectively, improving consistency and correctness. The get_scalar_from_constant utility now correctly references relax.Constant.
  • Missing Imports and Undefined References: Crucial TensorType imports were added to several conversion functions (convert_gather, convert_sparse_to_dense, convert_transpose_conv, convert_dequantize) to resolve undefined symbol issues. A reference to tflite.BuiltinOptions in _convert_resize was also corrected.
  • Explicitly Unsupported Operations: Conversion functions for DETECTION_POSTPROCESS and NON_MAX_SUPPRESSION_V5 are now explicitly guarded with NotImplementedError messages, clearly indicating that these operations require vision ops not yet available in Relax.
  • Linting and Error Message Improvements: Ruff noqa comments were updated for clarity, specifically for F821 in unreachable code paths. A noqa: F841 was added for the standard dataflow context variable df, and the error message for unsupported quantized TFLite models was made more informative.
  • New Test Cases: New test cases test_batch_matmul and test_batch_matmul_adj were added to validate the BATCH_MATMUL operator conversion in the TFLite frontend.

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

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 refactors the TFLite frontend, notably simplifying the flatten_to_nd function and significantly overhauling the convert_batch_matmul logic to use relax.op.matmul with improved shape handling and broadcasting. It also updates NotImplementedError messages for quantized operators and vision-related operations (DETECTION_POSTPROCESS, NON_MAX_SUPPRESSION_V5), adding placeholders for tracking issues. Other changes include minor import adjustments, a more Pythonic attribute check, and a fix for relax.Constant usage. New test cases for batch matrix multiplication have been added. The reviewer suggests replacing the XXXX placeholders in the NotImplementedError messages with actual GitHub issue numbers for better tracking.

raise NotImplementedError(
"DETECTION_POSTPROCESS requires vision ops (multibox_transform_loc, "
"non_max_suppression, get_valid_counts) not yet available in Relax. "
"See https://github.com/apache/tvm/issues/XXXX"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It's good practice to link to a tracking issue for NotImplementedError. Please replace XXXX with the actual GitHub issue number that tracks the implementation of the missing vision ops. This will help developers track progress and contribute more effectively.

raise NotImplementedError(
"NON_MAX_SUPPRESSION_V5 requires vision ops (get_valid_counts, "
"non_max_suppression) not yet available in Relax. "
"See https://github.com/apache/tvm/issues/XXXX"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the comment on convert_detection_postprocess, please replace the XXXX placeholder with a specific GitHub issue number for tracking the implementation of the required vision ops. This provides a clear reference for tracking the required feature.

@tlopex

tlopex commented Mar 25, 2026

Copy link
Copy Markdown
Member Author

cc @MasterJH5574

@MasterJH5574 MasterJH5574 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

@MasterJH5574 MasterJH5574 merged commit e53cfe1 into apache:main Mar 25, 2026
10 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