-
Notifications
You must be signed in to change notification settings - Fork 16
added muon optimizer component #449
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
BlueCrescent
wants to merge
1
commit into
main
Choose a base branch
from
muon
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.
+174
−3
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
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from pathlib import Path | ||
| from typing import Literal | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
@@ -14,7 +15,8 @@ | |
| from modalities.models.coca.coca_model import CoCa, CoCaConfig | ||
| from modalities.models.gpt2.gpt2_model import GPT2LLM | ||
| from modalities.models.model_factory import ModelFactory | ||
| from modalities.optimizers.optimizer_factory import get_optimizer_groups | ||
| from modalities.optimizers import optimizer_factory as optimizer_factory_module | ||
| from modalities.optimizers.optimizer_factory import OptimizerFactory, get_optimizer_groups | ||
| from modalities.registry.components import COMPONENTS | ||
| from modalities.registry.registry import Registry | ||
| from modalities.running_env.env_utils import MixedPrecisionSettings | ||
|
|
@@ -72,6 +74,131 @@ def test_get_optimizer_groups( | |
| ) | ||
|
|
||
|
|
||
| def test_get_adam_builds_optimizer_from_groups(monkeypatch): | ||
| wrapped_model = MagicMock() | ||
| optimizer_groups = [{"params": [MagicMock()], "weight_decay": 0.1}] | ||
| optimizer = MagicMock() | ||
| get_optimizer_groups_mock = MagicMock(return_value=optimizer_groups) | ||
| adam_mock = MagicMock(return_value=optimizer) | ||
|
|
||
| monkeypatch.setattr(optimizer_factory_module, "get_optimizer_groups", get_optimizer_groups_mock) | ||
| monkeypatch.setattr(optimizer_factory_module, "Adam", adam_mock) | ||
|
|
||
| result = OptimizerFactory.get_adam( | ||
| lr=1e-3, | ||
| betas=(0.9, 0.95), | ||
| eps=1e-8, | ||
| weight_decay=0.1, | ||
| weight_decay_groups_excluded=["embedding"], | ||
| wrapped_model=wrapped_model, | ||
| foreach=True, | ||
| fused=False, | ||
| ) | ||
|
|
||
| assert result is optimizer | ||
| get_optimizer_groups_mock.assert_called_once_with(wrapped_model, 0.1, ["embedding"]) | ||
| adam_mock.assert_called_once_with( | ||
| params=optimizer_groups, | ||
| lr=1e-3, | ||
| betas=(0.9, 0.95), | ||
| eps=1e-8, | ||
| foreach=True, | ||
| fused=False, | ||
| ) | ||
|
|
||
|
|
||
| def test_get_adam_w_builds_optimizer_from_groups(monkeypatch): | ||
| wrapped_model = MagicMock() | ||
| optimizer_groups = [{"params": [MagicMock()], "weight_decay": 0.2}] | ||
| optimizer = MagicMock() | ||
| get_optimizer_groups_mock = MagicMock(return_value=optimizer_groups) | ||
| adam_w_mock = MagicMock(return_value=optimizer) | ||
|
|
||
| monkeypatch.setattr(optimizer_factory_module, "get_optimizer_groups", get_optimizer_groups_mock) | ||
| monkeypatch.setattr(optimizer_factory_module, "AdamW", adam_w_mock) | ||
|
|
||
| result = OptimizerFactory.get_adam_w( | ||
| lr=2e-4, | ||
| betas=(0.8, 0.99), | ||
| eps=1e-6, | ||
| weight_decay=0.2, | ||
| weight_decay_groups_excluded=["layernorm"], | ||
| wrapped_model=wrapped_model, | ||
| foreach=False, | ||
| fused=True, | ||
| ) | ||
|
|
||
| assert result is optimizer | ||
| get_optimizer_groups_mock.assert_called_once_with(wrapped_model, 0.2, ["layernorm"]) | ||
| adam_w_mock.assert_called_once_with( | ||
| params=optimizer_groups, | ||
| lr=2e-4, | ||
| betas=(0.8, 0.99), | ||
| eps=1e-6, | ||
| foreach=False, | ||
| fused=True, | ||
| ) | ||
|
|
||
|
|
||
| def test_get_muon_builds_optimizer_from_groups(monkeypatch): | ||
| wrapped_model = MagicMock() | ||
| optimizer_groups = [{"params": [MagicMock()], "weight_decay": 0.05}] | ||
| optimizer = MagicMock() | ||
| get_optimizer_groups_mock = MagicMock(return_value=optimizer_groups) | ||
| muon_mock = MagicMock(return_value=optimizer) | ||
|
|
||
| monkeypatch.setattr(optimizer_factory_module, "get_optimizer_groups", get_optimizer_groups_mock) | ||
| monkeypatch.setattr(optimizer_factory_module, "Muon", muon_mock) | ||
|
|
||
| result = OptimizerFactory.get_muon( | ||
| lr=3e-4, | ||
| weight_decay=0.05, | ||
| momentum=0.95, | ||
| nesterov=True, | ||
| ns_coefficients=(1.0, 0.5, 0.25), | ||
| eps=1e-9, | ||
| ns_steps=7, | ||
| adjust_lr_fn="cosine", | ||
| weight_decay_groups_excluded=["embedding"], | ||
| wrapped_model=wrapped_model, | ||
| ) | ||
|
|
||
| assert result is optimizer | ||
| get_optimizer_groups_mock.assert_called_once_with(wrapped_model, 0.05, ["embedding"]) | ||
| muon_mock.assert_called_once_with( | ||
| params=optimizer_groups, | ||
| lr=3e-4, | ||
| weight_decay=0.05, | ||
| momentum=0.95, | ||
| nesterov=True, | ||
| ns_coefficients=(1.0, 0.5, 0.25), | ||
| eps=1e-9, | ||
| ns_steps=7, | ||
| adjust_lr_fn="cosine", | ||
| ) | ||
|
|
||
|
|
||
| def test_get_fsdp1_checkpointed_optimizer_loads_optimizer_state(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we upgrade this to FSDP2 since FSDP1 is anyways deprecated? |
||
| checkpoint_loading = MagicMock() | ||
| checkpoint_path = Path("/tmp/checkpoint") | ||
| wrapped_model = MagicMock() | ||
| optimizer = MagicMock() | ||
|
|
||
| result = OptimizerFactory.get_fsdp1_checkpointed_optimizer_( | ||
| checkpoint_loading=checkpoint_loading, | ||
| checkpoint_path=checkpoint_path, | ||
| wrapped_model=wrapped_model, | ||
| optimizer=optimizer, | ||
| ) | ||
|
|
||
| assert result is optimizer | ||
| checkpoint_loading.load_optimizer_checkpoint_.assert_called_once_with( | ||
| file_path=checkpoint_path, | ||
| optimizer=optimizer, | ||
| model=wrapped_model, | ||
| ) | ||
|
|
||
|
|
||
| def _run_single_optimizer_group_case( | ||
| process_id: int, | ||
| world_size: int, | ||
|
|
||
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.
not sure if these optimizer_builds_optimizer_from_groups tests are important. To me they don't really test anything apart from making sure that the factory calls the right opimizer functions.