Skip to content

Added support for data masking in Average, Accuracy and MultiMetric#5332

Merged
copybara-service[bot] merged 1 commit intomainfrom
metrics-mask
Mar 19, 2026
Merged

Added support for data masking in Average, Accuracy and MultiMetric#5332
copybara-service[bot] merged 1 commit intomainfrom
metrics-mask

Conversation

@vfdev-5
Copy link
Collaborator

@vfdev-5 vfdev-5 commented Mar 16, 2026

Try to reland reverted #5326
fixing reported error in the 3rd party repos

@gemini-code-assist
Copy link
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 re-introduces and refines data masking capabilities for key metric classes (Average, Accuracy, MultiMetric) within Flax's NNX module. The primary goal is to provide a robust mechanism for users to selectively ignore data points during metric computation, which is essential for handling variable-length sequences, padded inputs, or other scenarios where certain data should not contribute to the metric. The changes ensure that metric calculations accurately reflect only the relevant data, improving the flexibility and correctness of model evaluation.

Highlights

  • Data Masking in Average Metric: The Average metric's update method now accepts an optional mask argument, allowing specific values to be excluded from the total sum and count.
  • Data Masking in Accuracy Metric: The Accuracy metric's update method was enhanced to propagate an optional mask to its base Metric class, enabling masked accuracy calculations.
  • Flexible Masking in MultiMetric: The MultiMetric class now supports passing a mask argument to its update method, which can be a single jax.Array applied to all sub-metrics or a dictionary for per-metric masking.
  • Comprehensive Test Coverage: New and updated tests were added to validate the data masking functionality across Average, Accuracy, and MultiMetric, including a specific test for custom metrics.

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

Changelog
  • flax/nnx/training/metrics.py
    • Added an optional mask parameter to Average.update to enable masked averaging.
    • Modified Accuracy.update to accept and pass an optional mask to its superclass.
    • Implemented logic in MultiMetric.update to handle global or per-metric masks and forward them to sub-metrics.
  • tests/nnx/metrics_test.py
    • Parameterized test_multimetric to include tests for masked updates.
    • Added test_multimetric_with_custom_metric to verify mask handling with custom metric implementations.
    • Introduced test_average to thoroughly test the Average metric's masking behavior with various data types.
Activity
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 adds support for data masking in the Average, Accuracy, and MultiMetric metrics. The changes for Average and Accuracy are well-implemented, and the accompanying tests are thorough. However, I've identified a potential issue in MultiMetric.update where providing a global mask could lead to a TypeError if a sub-metric doesn't support masking. I've included a detailed comment with a suggested fix for this issue.

Comment on lines +430 to +434
metric_mask_kwarg = {}
metric_mask = mask.get(metric_name, None) if isinstance(mask, dict) else mask
if metric_mask is not None:
metric_mask_kwarg = {"mask": metric_mask}
getattr(self, metric_name).update(**(updates | metric_mask_kwarg))
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 current implementation for handling masks in MultiMetric.update can lead to a TypeError if a global mask is provided and one of the sub-metrics does not accept a mask keyword argument. The mask is unconditionally added to the keyword arguments passed to the sub-metric's update method.

To make this more robust, we should check if the sub-metric's update method actually accepts a mask argument before adding it. This can be done using inspect.signature.

      metric_update = getattr(self, metric_name).update
      metric_mask = mask.get(metric_name, None) if isinstance(mask, dict) else mask
      kwargs = updates
      if metric_mask is not None:
        import inspect
        sig = inspect.signature(metric_update)
        if 'mask' in sig.parameters or any(p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()):
          kwargs = {**updates, 'mask': metric_mask}
      metric_update(**kwargs)

@copybara-service copybara-service bot merged commit 2b3ffaf into main Mar 19, 2026
24 checks passed
@copybara-service copybara-service bot deleted the metrics-mask branch March 19, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants