-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax][Onnx] Support Local Response Normalization (LRN) #18668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Implement Local Response Norm Operator - Test LRN Node
Summary of ChangesHello @locnd182644, 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 ONNX frontend's capabilities by introducing support for the Local Response Normalization (LRN) operator. This addition allows the Relax framework to seamlessly import and execute ONNX models that incorporate LRN layers, thereby broadening its compatibility with various neural network architectures. The implementation carefully translates the LRN operation into Relax expressions, utilizing average pooling for its core computation. Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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 adds support for the ONNX Local Response Normalization (LRN) operator to the Relax frontend. The implementation correctly uses average pooling for the normalization computation and includes a corresponding test case. My review focuses on improving robustness by addressing potential data type mismatches and handling of dynamic input shapes. I've also suggested enhancing the test coverage to include more scenarios.
| if hasattr(data.struct_info, "ndim"): | ||
| ndim = data.struct_info.ndim | ||
| else: | ||
| ndim = len(data.struct_info.shape) | ||
|
|
||
| if ndim not in [3, 4]: | ||
| raise ValueError(f"LRN only supports 3D or 4D input, got {ndim}D.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current logic for determining the input tensor's rank does not handle cases where the rank is unknown (ndim == -1). This could lead to incorrect behavior or errors when processing models with dynamic-rank tensors, as the code would default to the 4D logic path. It's better to explicitly check for an unknown rank and raise an error. Using data.struct_info.ndim directly is also a cleaner way to get the rank.
| if hasattr(data.struct_info, "ndim"): | |
| ndim = data.struct_info.ndim | |
| else: | |
| ndim = len(data.struct_info.shape) | |
| if ndim not in [3, 4]: | |
| raise ValueError(f"LRN only supports 3D or 4D input, got {ndim}D.") | |
| ndim = data.struct_info.ndim | |
| if ndim == -1: | |
| raise ValueError("The input tensor to LRN must have a known rank.") | |
| if ndim not in [3, 4]: | |
| raise ValueError(f"LRN only supports 3D or 4D input, got {ndim}D.") |
| const_alpha = relax.const(alpha, dtype="float32") | ||
| const_bias = relax.const(bias, dtype="float32") | ||
| const_beta = relax.const(beta, dtype="float32") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The data type for the alpha, bias, and beta constants is hardcoded to float32. The ONNX LRN operator supports various floating-point types (e.g., float16, float64). Hardcoding the dtype can cause type mismatches or precision loss if the input tensor has a different float dtype. These constants should use the same data type as the input tensor data.
| const_alpha = relax.const(alpha, dtype="float32") | |
| const_bias = relax.const(bias, dtype="float32") | |
| const_beta = relax.const(beta, dtype="float32") | |
| const_alpha = relax.const(alpha, dtype=data.struct_info.dtype) | |
| const_bias = relax.const(bias, dtype=data.struct_info.dtype) | |
| const_beta = relax.const(beta, dtype=data.struct_info.dtype) |
This PR supported Local Response Normalization operator for ONNX.
Description
Implement and Test Local Response Normalization operator for ONNX frontend.
Implement
Reference
Implement same as Pytorch: https://discuss.pytorch.org/t/why-use-avgpool2d-and-avgpool3d-in-local-response-norm/97236