You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "locnd182644 (via GitHub)" <gi...@apache.org> on 2026/01/17 05:57:05 UTC

[PR] [Relax][Onnx] Support Local Response Normalization (LRN) [tvm]

locnd182644 opened a new pull request, #18668:
URL: https://github.com/apache/tvm/pull/18668

   This PR supported Local Response Normalization operator for ONNX.
   
   ### Description
   Implement and Test Local Response Normalization operator for ONNX frontend. 
   
   ### Implement
   - Using avg_pool operator to compute LRN
   - Pseudocode:
   ```
   def local_response_norm(input, size, alpha, beta, k):
       dim = input.dim()
       check_only_support_3D_4D()
       div = input.mul(input)
       div = expand_dim(div, 1)
       pad_len = size // 2
       if dim == 3:
           div = avg_pool2d(div,
                           (size, 1),
                           stride=1,
                           padding=(pad_len, 0, pad_len, 0))
       else:
           div = avg_pool3d(div,
                           (size, 1, 1),
                           stride=1,
                           padding=(pad_len, 0, 0, pad_len, 0, 0))
   
       div = squeeze_dim(div, 1)
       div = div.mul(alpha).add(k).pow(beta)
       return input / div
   ```
   
   ### Reference
   Implement same as Pytorch: https://discuss.pytorch.org/t/why-use-avgpool2d-and-avgpool3d-in-local-response-norm/97236


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Onnx] Support Local Response Normalization (LRN) [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #18668:
URL: https://github.com/apache/tvm/pull/18668


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Onnx] Support Local Response Normalization (LRN) [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #18668:
URL: https://github.com/apache/tvm/pull/18668#issuecomment-3762716191

   ## Summary of Changes
   
   Hello @locnd182644, I'm Gemini Code Assist[^1]! 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
   
   * **ONNX LRN Operator Support**: Implemented support for the Local Response Normalization (LRN) operator in the ONNX frontend, allowing Relax to process ONNX models containing LRN layers.
   * **LRN Implementation Details**: The LRN operator is implemented using `relax.op.nn.avg_pool2d` for 3D inputs and `relax.op.nn.avg_pool3d` for 4D inputs, following a common approach for LRN computation.
   * **Input Dimension Handling**: The implementation includes logic to handle both 3D and 4D input tensors, raising a ValueError for unsupported dimensions.
   * **New Test Case**: A new test case, `test_local_response_norm`, has been added to `tests/python/relax/test_frontend_onnx.py` to verify the correctness of the LRN operator conversion.
   
   🧠 **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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   
   
   
   
   
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   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.
   
   <b>Customization</b>
   
   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](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>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](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Onnx] Support Local Response Normalization (LRN) [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18668:
URL: https://github.com/apache/tvm/pull/18668#discussion_r2700674778


##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -2399,6 +2399,63 @@ def _impl_v9(cls, bb, inputs, attr, params):
         return (data - data_mean) / relax.op.sqrt(data_squared_mean - data_mean_squared)
 
 
+class LocalResponseNormalization(OnnxOpConverter):
+    """Converts an onnx LocalResponseNormalization node into an equivalent Relax expression."""
+
+    @classmethod
+    def _impl_v13(cls, bb, inputs, attr, params):
+        data = inputs[0]
+        size = attr["size"]
+        alpha = attr.get("alpha", 0.0001)
+        beta = attr.get("beta", 0.75)
+        bias = attr.get("bias", 1.0)
+
+        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.")

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   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.
   
   ```suggestion
           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.")
   ```



##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -2399,6 +2399,63 @@ def _impl_v9(cls, bb, inputs, attr, params):
         return (data - data_mean) / relax.op.sqrt(data_squared_mean - data_mean_squared)
 
 
+class LocalResponseNormalization(OnnxOpConverter):
+    """Converts an onnx LocalResponseNormalization node into an equivalent Relax expression."""
+
+    @classmethod
+    def _impl_v13(cls, bb, inputs, attr, params):
+        data = inputs[0]
+        size = attr["size"]
+        alpha = attr.get("alpha", 0.0001)
+        beta = attr.get("beta", 0.75)
+        bias = attr.get("bias", 1.0)
+
+        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.")
+
+        data_squared = relax.op.multiply(data, data)
+        data_expanded = relax.op.expand_dims(data_squared, axis=1)
+        pad_len = size // 2
+        if ndim == 3:
+            pool_padding = [pad_len, 0, pad_len, 0]
+            pool_op = relax.op.nn.avg_pool2d
+            pool_size = (size, 1)
+            layout = "NCHW"
+            strides = (1, 1)
+        else:
+            pool_padding = [pad_len, 0, 0, pad_len, 0, 0]
+            pool_op = relax.op.nn.avg_pool3d
+            pool_size = (size, 1, 1)
+            layout = "NCDHW"
+            strides = (1, 1, 1)
+
+        data_avgpool = pool_op(
+            data_expanded,
+            pool_size=pool_size,
+            strides=strides,
+            padding=pool_padding,
+            layout=layout,
+            ceil_mode=False,
+            count_include_pad=True,
+        )
+        data_squeezed = relax.op.squeeze(data_avgpool, axis=1)
+
+        const_alpha = relax.const(alpha, dtype="float32")
+        const_bias = relax.const(bias, dtype="float32")
+        const_beta = relax.const(beta, dtype="float32")

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   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`.
   
   ```suggestion
           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)
   ```



##########
tests/python/relax/test_frontend_onnx.py:
##########
@@ -1673,6 +1673,33 @@ def verify_embedlayernormalization(
     )
 
 
+def test_local_response_norm():
+    lrn_node = helper.make_node(
+        op_type="LRN",
+        inputs=["input"],
+        outputs=["output"],
+        name="LRN_Node",
+        alpha=0.0001,
+        beta=0.75,
+        bias=1.0,
+        size=3,
+    )
+
+    graph = helper.make_graph(
+        [lrn_node],
+        "local_response_norm_test",
+        inputs=[
+            helper.make_tensor_value_info("input", TensorProto.FLOAT, [1, 3, 32, 32]),
+        ],
+        outputs=[
+            helper.make_tensor_value_info("output", TensorProto.FLOAT, [1, 3, 32, 32]),
+        ],
+    )
+
+    model = helper.make_model(graph, producer_name="local_response_norm_test")
+    check_correctness(model)
+

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The current test for `LocalResponseNormalization` only covers a single case with a 4D input and default attributes. To ensure the implementation is robust, it would be beneficial to expand the test coverage. I suggest parameterizing the test to include 3D inputs and variations in the LRN attributes (`size`, `alpha`, `beta`, `bias`).
   
   ```python
   @pytest.mark.parametrize(
       "shape, size, alpha, beta, bias",
       [
           ([1, 3, 32, 32], 3, 0.0001, 0.75, 1.0),  # 4D case
           ([1, 5, 16], 5, 0.0002, 0.5, 2.0),  # 3D case
       ],
   )
   def test_local_response_norm(shape, size, alpha, beta, bias):
       lrn_node = helper.make_node(
           op_type="LRN",
           inputs=["input"],
           outputs=["output"],
           name="LRN_Node",
           alpha=alpha,
           beta=beta,
           bias=bias,
           size=size,
       )
   
       graph = helper.make_graph(
           [lrn_node],
           "local_response_norm_test",
           inputs=[
               helper.make_tensor_value_info("input", TensorProto.FLOAT, shape),
           ],
           outputs=[
               helper.make_tensor_value_info("output", TensorProto.FLOAT, shape),
           ],
       )
   
       model = helper.make_model(graph, producer_name="local_response_norm_test")
       check_correctness(model)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][Onnx] Support Local Response Normalization (LRN) [tvm]

Posted by "yongwww (via GitHub)" <gi...@apache.org>.
yongwww commented on PR #18668:
URL: https://github.com/apache/tvm/pull/18668#issuecomment-3766085789

   @tvm-bot rerun


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org