-
Notifications
You must be signed in to change notification settings - Fork 63
[QEff. Finetune]: Adding optimizer registry and its test cases #649
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
Merged
+121
−0
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c3d819c
Adding optimizer registry and its test cases
tchawada 9e402bc
Adding optimizer registry and its test cases
tchawada 34f15c4
Adding optimizer registry and its test cases
tchawada 1d7d4c2
[QEff. Finetuning]: Optimizer registry and test case inclusion
tchawada cd3ce48
Updated optimizer.py
tchawada 22f0e3f
Updated test_optimizer.py
tchawada f2822be
Updated optimizer.py
tchawada 1a770ad
Updated test_optimizer.py
tchawada 1b96768
Updated test_optimizer.py
tchawada 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,93 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # | ||
| # Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import pytest | ||
| import torch.nn as nn | ||
| import torch.optim as optim | ||
|
|
||
| from QEfficient.finetune.experimental.core.component_registry import registry | ||
| from QEfficient.finetune.experimental.core.optimizer import get_optimizer | ||
|
|
||
| OPTIMIZER_CONFIGS = { | ||
| "Adam": { | ||
| "optimizer_name": "Adam", | ||
| "opt_cls": optim.Adam, | ||
| "lr": 1e-4, | ||
| "weight_decay": 0.01, | ||
| "betas": (0.9, 0.999), | ||
| "eps": 1e-8, | ||
| "amsgrad": False, | ||
| }, | ||
| "AdamW": { | ||
| "optimizer_name": "AdamW", | ||
| "opt_cls": optim.AdamW, | ||
| "lr": 1e-4, | ||
| "weight_decay": 0.01, | ||
| "betas": (0.9, 0.999), | ||
| "eps": 1e-8, | ||
| "amsgrad": False, | ||
| }, | ||
| "SGD": { | ||
| "optimizer_name": "SGD", | ||
| "opt_cls": optim.SGD, | ||
| "lr": 1e-4, | ||
| "momentum": 0.9, | ||
| "weight_decay": 0.01, | ||
| "dampening": 0.0, | ||
| "nesterov": False, | ||
| }, | ||
| "RMSprop": { | ||
| "optimizer_name": "RMSprop", | ||
| "opt_cls": optim.RMSprop, | ||
| }, | ||
| } | ||
|
|
||
| REGISTRY_CONFIG = { | ||
| "RMSprop": { | ||
| "optimizer_name": "RMSprop", | ||
| "opt_cls": optim.RMSprop, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dummy_model(): | ||
| return nn.Sequential( | ||
| nn.Linear(10, 5), | ||
| nn.ReLU(), | ||
| nn.Linear(5, 1), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("opt_name", OPTIMIZER_CONFIGS.keys()) | ||
| def test_optimizers(opt_name, dummy_model): | ||
| """Test that all registered optimizers can be created with their configs.""" | ||
| config = OPTIMIZER_CONFIGS[opt_name] | ||
tchawada marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| config.pop("opt_cls") | ||
| try: | ||
| optimizer_class_and_kwargs = get_optimizer(config) | ||
| assert optimizer_class_and_kwargs is not None | ||
| except ValueError as e: | ||
| assert "Unknown optimizer" in str(e) | ||
| return | ||
| optimizer_class = optimizer_class_and_kwargs[0] | ||
| opt_inst = optimizer_class(dummy_model.parameters(), **optimizer_class_and_kwargs[1]) | ||
| assert isinstance(opt_inst, optim.Optimizer) | ||
| assert len(list(opt_inst.param_groups)) == 1 | ||
|
|
||
| for key in ["lr", "weight_decay", "betas", "eps", "momentum", "dampening", "nesterov", "amsgrad"]: | ||
| if key in config: | ||
| assert opt_inst.param_groups[0][key] == config[key], f"{key} mismatch" | ||
|
Contributor
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. Good cleanup |
||
|
|
||
|
|
||
| @pytest.mark.parametrize("opt_name, opt_cls", REGISTRY_CONFIG.items()) | ||
| def test_registered_optimizer(opt_name, opt_cls): | ||
| """Test that the optimizer registerd correctly.""" | ||
| registry.optimizer(opt_name)(opt_cls) | ||
| optimizer_class = registry.get_optimizer(opt_name) | ||
| assert optimizer_class is not None | ||
| assert optimizer_class == opt_cls | ||
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.
Uh oh!
There was an error while loading. Please reload this page.