Skip to content

Conversation

@guan404ming
Copy link
Member

@guan404ming guan404ming commented Jan 21, 2026

Why

The batch_flatten operator was needed for CoreML backend support but was not implemented in Relax, causing the CoreML pattern and converter to be commented out.

How

  • Add C++ operator relax.nn.batch_flatten with struct info inference
  • Add Python wrapper in relax.op.nn
  • Add legalization via topi.reshape
  • Enable CoreML pattern and converter for nn.batch_flatten
  • Add unit tests for operator, struct info inference, and legalization

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guan404ming, 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 implements the batch_flatten operator within the Relax framework, addressing a critical dependency for the CoreML backend. The new operator flattens all dimensions of a tensor except the batch dimension, which was previously a missing feature preventing full CoreML integration. The changes encompass the entire lifecycle of the operator, from its low-level C++ definition and Python bindings to its shape inference, legalization, and thorough unit testing, ensuring its correct and robust operation within the system.

Highlights

  • New batch_flatten Operator: Introduced relax.nn.batch_flatten to flatten all dimensions except the batch dimension, crucial for neural network architectures.
  • CoreML Backend Support Enabled: The absence of batch_flatten previously caused CoreML patterns and converters to be commented out; this PR enables them.
  • Comprehensive Implementation: The operator includes a C++ definition, Python wrapper, struct info inference, and legalization via topi.reshape.
  • Extensive Unit Testing: New unit tests cover operator correctness, struct info inference (including symbolic shapes), and legalization, ensuring robust functionality.

🧠 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 introduces the relax.nn.batch_flatten operator, which is a great addition for CoreML backend support. The implementation is clean and follows the existing structure for Relax operators well. You've included comprehensive changes across the C++ implementation, Python bindings, legalization, and enabled it for the CoreML backend. The unit tests are thorough, covering correctness, struct info inference with various static and symbolic shapes, and legalization.

I found one potential issue in the legalization logic that could lead to a crash when dealing with inputs of unknown shape. I've left a specific comment with a suggested fix. Other than that, the changes look excellent.

Comment on lines 781 to 784
def _nn_batch_flatten(bb: BlockBuilder, call: Call) -> Expr:
return bb.call_te(topi.reshape, call.args[0], call.struct_info.shape.values)
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 legalization for relax.nn.batch_flatten assumes that call.struct_info.shape is always available. However, if the input tensor to batch_flatten has an unknown number of dimensions or unknown shape values, the struct info inference for batch_flatten will correctly produce a TensorStructInfo without a concrete shape. In this scenario, call.struct_info.shape will be None, and attempting to access call.struct_info.shape.values will result in an AttributeError, crashing the compiler.

To make the legalization more robust, you should add a check to ensure call.struct_info.shape is defined before attempting to use it. If it's not defined, the operator should not be legalized, and the original call should be returned. This is a common pattern in this file for other operators.

It would also be great to add a new test case in tests/python/relax/test_transform_legalize_ops_nn.py to cover this scenario, for example with an input tensor of R.Tensor(ndim=4, dtype="float32"), and assert that batch_flatten is not legalized in this case.

Suggested change
def _nn_batch_flatten(bb: BlockBuilder, call: Call) -> Expr:
return bb.call_te(topi.reshape, call.args[0], call.struct_info.shape.values)
def _nn_batch_flatten(bb: BlockBuilder, call: Call) -> Expr:
if call.struct_info.shape is None:
return call
return bb.call_te(topi.reshape, call.args[0], call.struct_info.shape.values)

@guan404ming guan404ming force-pushed the feat/relax-nn-batch-flatten branch from b665ce8 to 21c4563 Compare January 21, 2026 16:59
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