Skip to content

[Relax][Onnx][BatchNorm] Pass momentum and training_mode into BatchNorm Operator#18704

Merged
tlopex merged 1 commit intoapache:mainfrom
locnd182644:Relax/Onnx/BatchNorm
Feb 2, 2026
Merged

[Relax][Onnx][BatchNorm] Pass momentum and training_mode into BatchNorm Operator#18704
tlopex merged 1 commit intoapache:mainfrom
locnd182644:Relax/Onnx/BatchNorm

Conversation

@locnd182644
Copy link
Contributor

Description

  • Onnx model have training_mode atrr = False, but Relax model after convert have training = True
  • Momentum values ​​in Relax module are not the same as onnx model

Steps to Reproduce

BatchNorm
  • Relax model:
class Module:
    def main(X: R.Tensor((2, 3, 4, 4), dtype="float32")) -> R.Tensor((2, 3, 4, 4), dtype="float32"):
        R.func_attr({"num_input": 1})
        with R.dataflow():
            lv: R.Tuple(R.Tensor((2, 3, 4, 4), dtype="float32"), R.Tensor((3,), dtype="float32"), R.Tensor((3,), dtype="float32")) = R.nn.batch_norm(X, metadata["relax.expr.Constant"][0], metadata["relax.expr.Constant"][1], metadata["relax.expr.Constant"][2], metadata["relax.expr.Constant"][3], axis=1, epsilon=9.9999997473787516e-06, center=True, scale=True, momentum=0.10000000000000001, training=True)
            lv1: R.Tensor((2, 3, 4, 4), dtype="float32") = lv[0]
            lv2: R.Tensor((3,), dtype="float32") = lv[1]
            lv3: R.Tensor((3,), dtype="float32") = lv[2]
            gv: R.Tensor((2, 3, 4, 4), dtype="float32") = lv1
            R.output(gv)
        return gv

Resolved

…rm Operator

- Get Attributes and Pass momentum/training_mode with default value into BatchNorm Operator
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 resolves an issue where the BatchNorm operator in Relax models, after conversion from ONNX, did not accurately reflect the momentum and training_mode parameters of the original ONNX model. By explicitly extracting and passing these attributes, the conversion process now ensures greater fidelity between the ONNX and Relax representations, preventing discrepancies in model behavior and addressing the reported bug.

Highlights

  • BatchNorm Parameter Propagation: The momentum and training_mode attributes are now correctly extracted from ONNX model attributes and passed to the relax.op.nn.batch_norm operator during conversion.
  • Default Value Handling: Default values (0.9 for momentum and 0 for training_mode) are applied if these attributes are not explicitly present in the ONNX model, ensuring robust 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 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 correctly addresses a bug in the ONNX frontend for the BatchNormalization operator. Previously, the momentum and training_mode attributes were not being passed from the ONNX model, causing the Relax operator to use incorrect default values. Your changes fix this by retrieving these attributes from the ONNX node and forwarding them to relax.op.nn.batch_norm. This aligns the behavior with the ONNX specification. I've added one minor suggestion to improve code clarity by explicitly casting the training_mode to a boolean.

axis=1,
epsilon=epsilon,
momentum=momentum,
training=training_mode,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For clarity and type safety, it's better to explicitly cast training_mode to a boolean. The training parameter of relax.op.nn.batch_norm expects a boolean, and while Python's truthiness handles the integer 0 or 1 correctly, an explicit cast makes the intent clearer and guards against potential issues if the operator's signature becomes stricter in the future.

Suggested change
training=training_mode,
training=bool(training_mode),

@tlopex tlopex merged commit c575973 into apache:main Feb 2, 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.

[Bug][Relax][ONNX] BatchNormalization ignores training_mode attribute, always uses training=True

2 participants