-
Notifications
You must be signed in to change notification settings - Fork 94
Wire total_weights into ML Diagnostics metrics #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
richaguptaa17
wants to merge
1
commit into
AI-Hypercomputer:main
Choose a base branch
from
richaguptaa17:mldiag-metric-weights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
| from maxdiffusion.trainers.dreambooth_trainer import DreamboothTrainer | ||
|
|
||
| UNET_PARAMS = 1000 | ||
| TEXT_ENCODER_PARAMS = 500 | ||
|
|
||
|
|
||
| class MockConfig: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| for k, v in kwargs.items(): | ||
| setattr(self, k, v) | ||
|
|
||
|
|
||
| class DreamboothTrainerTest(unittest.TestCase): | ||
|
|
||
| @patch("maxdiffusion.trainers.dreambooth_trainer.train_utils") | ||
| @patch("maxdiffusion.trainers.dreambooth_trainer.max_utils") | ||
| @patch("maxdiffusion.trainers.dreambooth_trainer.jax") | ||
| @patch("maxdiffusion.trainers.dreambooth_trainer.os") | ||
| def test_training_loop_total_weights(self, mock_os, mock_jax, mock_max_utils, mock_train_utils): | ||
| """total_weights includes the text encoder only when it is trained.""" | ||
| mock_jax.process_index.return_value = 0 | ||
| mock_jax.random.split.return_value = ("dummy1", "dummy2") | ||
| mock_os.environ = {"LIBTPU_INIT_ARGS": ""} | ||
| mock_max_utils.profiler_enabled.return_value = False | ||
| mock_max_utils.calculate_num_params_from_pytree.side_effect = lambda params: { | ||
| "unet_params": UNET_PARAMS, | ||
| "text_encoder_params": TEXT_ENCODER_PARAMS, | ||
| }[params] | ||
| mock_train_utils.get_first_step.return_value = 0 | ||
|
|
||
| unet_state = MagicMock() | ||
| unet_state.params = "unet_params" | ||
| text_encoder_state = MagicMock() | ||
| text_encoder_state.params = "text_encoder_params" | ||
| train_states = {"unet_state": unet_state, "text_encoder_state": text_encoder_state} | ||
|
|
||
| p_train_step = MagicMock() | ||
| p_train_step.return_value = (unet_state, text_encoder_state, {}, "rngs") | ||
|
|
||
| for train_text_encoder, expected_total_weights in ( | ||
| (False, UNET_PARAMS), | ||
| (True, UNET_PARAMS + TEXT_ENCODER_PARAMS), | ||
| ): | ||
| with self.subTest(train_text_encoder=train_text_encoder): | ||
| mock_train_utils.record_scalar_metrics.reset_mock() | ||
| config = MockConfig( | ||
| train_text_encoder=train_text_encoder, | ||
| max_train_steps=1, | ||
| per_device_batch_size=1, | ||
| checkpoint_every=-1, | ||
| write_metrics=False, | ||
| metrics_file=None, | ||
| gcs_metrics=None, | ||
| skip_first_n_steps_for_profiler=999, | ||
| profiler_steps=10, | ||
| ) | ||
|
|
||
| with patch("maxdiffusion.trainers.dreambooth_trainer.BaseStableDiffusionTrainer.__init__", return_value=None): | ||
| trainer = DreamboothTrainer(config) | ||
| trainer.config = config | ||
| trainer.total_train_batch_size = 1 | ||
| trainer.per_device_tflops = 1.0 | ||
| trainer.rng = "rng" | ||
| trainer.checkpoint_manager = MagicMock() | ||
| trainer.save_checkpoint = MagicMock() | ||
|
|
||
| trainer.training_loop(p_train_step, None, None, train_states, MagicMock(), MagicMock()) | ||
|
|
||
| kwargs = mock_train_utils.record_scalar_metrics.call_args.kwargs | ||
| self.assertEqual(kwargs.get("total_weights"), expected_total_weights) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/maxdiffusion/tests/stable_diffusion_trainer_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| """Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
| from maxdiffusion.trainers.stable_diffusion_trainer import StableDiffusionTrainer | ||
|
|
||
|
|
||
| class MockConfig: | ||
|
|
||
| def __init__(self, **kwargs): | ||
| for k, v in kwargs.items(): | ||
| setattr(self, k, v) | ||
|
|
||
|
|
||
| class StableDiffusionTrainerTest(unittest.TestCase): | ||
|
|
||
| @patch("maxdiffusion.trainers.stable_diffusion_trainer.train_utils") | ||
| @patch("maxdiffusion.trainers.stable_diffusion_trainer.max_utils") | ||
| @patch("maxdiffusion.trainers.stable_diffusion_trainer.jax") | ||
| @patch("maxdiffusion.trainers.stable_diffusion_trainer.os") | ||
| def test_training_loop_total_weights(self, mock_os, mock_jax, mock_max_utils, mock_train_utils): | ||
| # Setup mocks | ||
| mock_jax.process_index.return_value = 0 | ||
| mock_jax.random.split.return_value = ("dummy1", "dummy2") | ||
| mock_os.environ = {"LIBTPU_INIT_ARGS": ""} | ||
|
|
||
| mock_max_utils.profiler_enabled.return_value = False | ||
|
|
||
| def fake_calc_params(pytree): | ||
| if pytree == "unet_params": | ||
| return 1000 | ||
| elif pytree == "text_encoder_params": | ||
| return 500 | ||
| return 0 | ||
|
|
||
| mock_max_utils.calculate_num_params_from_pytree.side_effect = fake_calc_params | ||
|
|
||
| # We want the loop to hit exactly 1 step then exit. | ||
| mock_train_utils.get_first_step.return_value = 0 | ||
|
|
||
| unet_state = MagicMock() | ||
| unet_state.params = "unet_params" | ||
|
|
||
| vae_state = MagicMock() | ||
|
|
||
| text_encoder_state = MagicMock() | ||
| text_encoder_state.params = "text_encoder_params" | ||
|
|
||
| train_states = { | ||
| "unet_state": unet_state, | ||
| "vae_state": vae_state, | ||
| "text_encoder_state": text_encoder_state, | ||
| } | ||
|
|
||
| p_train_step = MagicMock() | ||
| # p_train_step returns: unet_state, text_encoder_state, train_metric, train_rngs | ||
| p_train_step.return_value = (unet_state, text_encoder_state, {}, "rngs") | ||
|
|
||
| data_iterator = MagicMock() | ||
| lr_scheduler = MagicMock() | ||
| lr_scheduler.return_value = 0.001 | ||
|
|
||
| # Instance of trainer | ||
| with patch("maxdiffusion.trainers.stable_diffusion_trainer.BaseStableDiffusionTrainer.__init__") as mock_init: | ||
| mock_init.return_value = None | ||
|
|
||
| # Test train_text_encoder = False | ||
| config_false = MockConfig( | ||
| train_text_encoder=False, | ||
| max_train_steps=1, | ||
| per_device_batch_size=1, | ||
| checkpoint_every=-1, | ||
| write_metrics=False, | ||
| metrics_file=None, | ||
| gcs_metrics=None, | ||
| skip_first_n_steps_for_profiler=999, | ||
| profiler_steps=10, | ||
| ) | ||
| trainer = StableDiffusionTrainer(config_false) | ||
| trainer.config = config_false | ||
| trainer.total_train_batch_size = 1 | ||
| trainer.per_device_tflops = 1.0 | ||
| trainer.rng = "rng" | ||
| trainer.checkpoint_manager = MagicMock() | ||
| trainer.checkpoint_manager.reached_preemption.return_value = False | ||
| trainer.save_checkpoint = MagicMock() | ||
|
|
||
| trainer.training_loop(p_train_step, None, None, train_states, data_iterator, lr_scheduler) | ||
|
|
||
| # Verify total_weights recorded | ||
| mock_train_utils.record_scalar_metrics.assert_called() | ||
| kwargs = mock_train_utils.record_scalar_metrics.call_args.kwargs | ||
| self.assertEqual(kwargs.get("total_weights"), 1000) | ||
|
|
||
| # Reset mocks | ||
| mock_train_utils.record_scalar_metrics.reset_mock() | ||
|
|
||
| # Test train_text_encoder = True | ||
| config_true = MockConfig( | ||
| train_text_encoder=True, | ||
| max_train_steps=1, | ||
| per_device_batch_size=1, | ||
| checkpoint_every=-1, | ||
| write_metrics=False, | ||
| metrics_file=None, | ||
| gcs_metrics=None, | ||
| skip_first_n_steps_for_profiler=999, | ||
| profiler_steps=10, | ||
| ) | ||
| trainer_true = StableDiffusionTrainer(config_true) | ||
| trainer_true.config = config_true | ||
| trainer_true.total_train_batch_size = 1 | ||
| trainer_true.per_device_tflops = 1.0 | ||
| trainer_true.rng = "rng" | ||
| trainer_true.checkpoint_manager = MagicMock() | ||
| trainer_true.checkpoint_manager.reached_preemption.return_value = False | ||
| trainer_true.save_checkpoint = MagicMock() | ||
|
|
||
| trainer_true.training_loop(p_train_step, None, None, train_states, data_iterator, lr_scheduler) | ||
|
|
||
| mock_train_utils.record_scalar_metrics.assert_called() | ||
| kwargs = mock_train_utils.record_scalar_metrics.call_args.kwargs | ||
| self.assertEqual(kwargs.get("total_weights"), 1500) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same counting issue applies here:
num_model_parametersincludes only the UNet, while DreamBooth also updates the text encoder whentrain_text_encoder=True. Please conditionally includetext_encoder_state.paramssototal_weightsreflects all trainable parameters.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done