This is an unofficial implementation of the SWAN optimizer. The paper is below:
https://arxiv.org/abs/2412.13148
SWAN Optimizer can only optimize matrix parameters. Therefore, when using it, you need to split matrix parameters from other parameters and optimize the other parameters with another optimizer.
In this implementation, creating an optimizer requires 3 steps:
- Setting additional information to the parameters using unofficial_swan.set_parameter_info()
- Separation of matrix parameters and other parameters using unofficial_swan.split_parameters()
- Creating the actual instance of optimizers
An example is shown below:
import unofficial_swan
from torch.optim import AdamW
model = your_model()
# sets additional infomation into instances of torch.nn.Parameter instances
unofficial_swan.set_parameter_info(model)
# splits the parameters to matrix parameters and other parameters.
matrix_params, other_params = unofficial_swan.split_parameters(model, model.fc.weight)
# creates the optimizers
optimizers = [
unofficial_swan.Swan(matrix_params, lr=1e-3)
AdamW(other_params, lr=1e-3)
]
results of examples/cifar10.py
Optimizer | Accuracy | Elapsed Tim |
---|---|---|
AdamW | 78.99% | 20.69 min |
RAdamW | 86.52% | 20.63 min |
Muon | 90.78% | 21.02 min |
SWAN(diag=True) | 70.68% | 21.36% |
SWAN(diag=False) | 78.75% | 37.06 min |
These codes are licensed under CC0.
src/unofficial_swan/muon.py was copyed from https://github.com/KellerJordan/Muon and then was added some modifications. The original copyright is as follows:
MIT License
Copyright (c) 2024 Keller Jordan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
src/unofficial_swan/original_muon.py is original source code without modifications.