Skip to content

Expand Dropout test coverage and migrate to absltest#5281

Open
us wants to merge 1 commit intogoogle:mainfrom
us:worktree-stochastic-test
Open

Expand Dropout test coverage and migrate to absltest#5281
us wants to merge 1 commit intogoogle:mainfrom
us:worktree-stochastic-test

Conversation

@us
Copy link
Contributor

@us us commented Feb 26, 2026

What does this PR do?

Expands test coverage for nnx.Dropout in tests/nnx/nn/stochastic_test.py and migrates the test file from plain pytest to the project-standard absltest framework.

Changes:

  • Migrate from pytest classes to absltest/parameterized.TestCase
  • Replace bare assert with self.assertEqual/self.assertIsNotNone
  • Replace pytest.raises with self.assertRaises/self.assertRaisesRegex
  • Add if __name__ == '__main__': absltest.main() entry point

New tests (4 → 24):

  • test_rate_zero / test_rate_one — edge case rate limits
  • test_rate_one_gradient_not_nan — gradient safety at rate=1.0
  • test_deterministic_passthrough — deterministic mode returns input unchanged
  • test_dtypes — float32, float16, bfloat16 dtype preservation
  • test_broadcast_dims — 7 parameterized cases ported from Linen reference tests
  • test_rate_stats — statistical correctness across rates 0.1–0.9
  • test_rngs_as_jax_array — passing raw jax.Array key as rngs
  • test_rngs_as_nnx_rngs_in_call — passing nnx.Rngs directly to __call__
  • test_custom_rng_collection — non-default rng_collection parameter
  • test_invalid_rngs_type_constructor / test_invalid_rngs_type_call — TypeError for invalid rngs types

All code paths in flax/nnx/nn/stochastic.py are now covered.

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other checks if that's the case).
  • This change is discussed in a Github issue/discussion (please add a link).
  • The documentation and docstrings adhere to the documentation guidelines.
  • This change includes necessary high-coverage tests. (No quality testing = no merge!)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @us, 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 enhances the robustness and maintainability of the nnx.Dropout module by migrating its test suite to the absltest framework and substantially expanding its test coverage. The changes ensure that the dropout functionality is thoroughly validated across various scenarios, including edge cases and different configurations, aligning the testing practices with project standards and improving overall code quality.

Highlights

  • Test Framework Migration: Migrated the nnx.Dropout tests from pytest to the project-standard absltest framework, including the use of parameterized.TestCase for data-driven tests.
  • Assertion Standardization: Replaced bare assert statements and pytest.raises with absltest's assertion methods such as self.assertEqual, self.assertIsNotNone, self.assertRaises, and self.assertRaisesRegex.
  • Expanded Test Coverage: Significantly increased the test coverage for nnx.Dropout from 4 to 24 test cases, ensuring all code paths in flax/nnx/nn/stochastic.py are now covered.
  • New Test Cases: Introduced comprehensive new tests covering edge cases for dropout rates (0.0 and 1.0), deterministic mode behavior, gradient stability, dtype preservation, various RNG input types, custom RNG collections, invalid RNG type handling, broadcast dimensions, and statistical correctness of dropout rates.

🧠 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
  • tests/nnx/nn/stochastic_test.py
    • Updated imports to include jax, absltest, parameterized, and random, while removing pytest.
    • Renamed the test class from TestStochastic to TestDropout and made it inherit from parameterized.TestCase.
    • Replaced assert statements with self.assertIsNotNone and self.assertEqual for internal RNG checks.
    • Modified test_dropout_rng_override to use self.assertFalse for comparing arrays and updated input shape.
    • Converted pytest.raises to self.assertRaises and self.assertRaisesRegex in test_dropout_arg_override and test_dropout_arg_override_view.
    • Added np.testing.assert_array_equal checks for deterministic passthrough in test_dropout_arg_override and test_dropout_arg_override_view.
    • Introduced test_deterministic_passthrough to verify deterministic mode behavior.
    • Added test_rate_zero and test_rate_one for dropout rate edge cases.
    • Included test_rate_one_gradient_not_nan to check gradient stability at rate 1.0.
    • Implemented test_dtypes using parameterized.product to test various float types.
    • Added test_rngs_as_jax_array and test_rngs_as_nnx_rngs_in_call for different RNG input types.
    • Created test_custom_rng_collection for non-default RNG collection names.
    • Introduced test_invalid_rngs_type_constructor and test_invalid_rngs_type_call for invalid RNG type error handling.
    • Added test_broadcast_dims with multiple parameterized cases to verify broadcasting behavior.
    • Implemented test_rate_stats to statistically verify dropout rates.
    • Added if __name__ == '__main__': absltest.main() for absltest execution.
Activity
  • No human activity has been recorded on this pull request yet.
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 is a great improvement. It successfully migrates the tests for nnx.Dropout from pytest to absltest and significantly expands the test coverage with many valuable new test cases, including edge cases and statistical correctness checks. The changes are well-structured and align with the project's testing standards. I have one minor suggestion to improve the readability of a newly added test.

@us us force-pushed the worktree-stochastic-test branch 3 times, most recently from 4d175e9 to d8db01b Compare February 26, 2026 20:44
@us us force-pushed the worktree-stochastic-test branch from d8db01b to 24d9531 Compare February 26, 2026 20:51
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.

1 participant