Add tests, docstrings and validation to metrics.py#5284
Add tests, docstrings and validation to metrics.py#5284us wants to merge 1 commit intogoogle:mainfrom
Conversation
Summary of ChangesHello, 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 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. Changelog
Activity
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.
Code Review
This pull request significantly improves flax/nnx/training/metrics.py by adding comprehensive tests, docstrings, and validation logic. The added validation in MultiMetric prevents the use of reserved names and catches typos in keyword arguments passed to update, which is a great enhancement for usability. The new tests are thorough and cover a wide range of scenarios, including edge cases. The code is well-written and the changes are well-documented. I have one suggestion to make the keyword argument validation in MultiMetric even more robust for custom user-defined metrics.
8d076c5 to 20aecf5 Compare
What does this PR do?
Addresses the TODO at
flax/nnx/training/metrics.pyline 26 (# TODO: add tests and docstrings) and the MultiMetric validation TODOs at lines 389 and 410.Changes
flax/nnx/training/metrics.pyStatisticsdataclass (the only public class missing one)MultiMetric.__init__— prevents metric names that shadow methods (reset,update,compute,split) or internal attributes (_metric_names,_expected_kwargs)MultiMetric.update— catches typos by validating keyword arguments against the expected set derived from underlying metrics viainspect.signatureMultiMetric.updatedocstring to document the newTypeErrorbehaviortests/nnx/metrics_test.py— 25 new tests across 4 new test classes:TestAverage: initial NaN, single/multiple batch, reset, custom argname, missing argname, scalar float/intTestWelford: multiple batches (mean + std + SEM), reset (all 3 stats), custom argname, missing argnameTestAccuracy: int64 label conversion, invalid float labels, threshold type errorTestMultiMetric: reserved name errors (parameterized over all 4), internal name errors (parameterized over 2), unmatched kwarg with guard assertion, compute dict structure, split/merge preservation, validation-disabled-with-var-keywordChecklist