|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +import torch |
| 20 | +from torch.optim.optimizer import ParamsT |
| 21 | + |
| 22 | +from emerging_optimizers.mixin import WeightDecayT |
| 23 | +from emerging_optimizers.orthogonalized_optimizers import muon |
| 24 | +from emerging_optimizers.orthogonalized_optimizers.orthogonalized_optimizer import OrthogonalizedOptimizer, _args_doc |
| 25 | + |
| 26 | + |
| 27 | +__all__ = ["MOP"] |
| 28 | + |
| 29 | + |
| 30 | +class MOP(OrthogonalizedOptimizer): |
| 31 | + """MOP: Momentum Orthogonalized by Polar decomposition |
| 32 | +
|
| 33 | + warning: |
| 34 | + This optimizer is experimental and not yet thoroughly tested. |
| 35 | +
|
| 36 | +
|
| 37 | + Args: |
| 38 | + {_args_doc} |
| 39 | + scale_mode: The type of scale factor to use for the update. Defaults to "spectral" style scaling. |
| 40 | + extra_scale_factor: The additional scale factor to use for the update. |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + params: ParamsT, |
| 46 | + lr: float = 3e-4, |
| 47 | + momentum_beta: float = 0.95, |
| 48 | + weight_decay: float = 0.01, |
| 49 | + *, |
| 50 | + use_nesterov: bool = False, |
| 51 | + weight_decay_method: WeightDecayT = "decoupled", |
| 52 | + fp32_matmul_prec: str = "highest", |
| 53 | + scale_mode: str = "spectral", |
| 54 | + extra_scale_factor: float = 1.0, |
| 55 | + ) -> None: |
| 56 | + def scaled_orthogonalize_fn(grad: torch.Tensor) -> torch.Tensor: |
| 57 | + orth_grad, _ = polar_via_svd(grad, False) |
| 58 | + |
| 59 | + scale_factor = muon.get_muon_scale_factor(grad.size(-2), grad.size(-1), mode=scale_mode) |
| 60 | + return orth_grad * scale_factor * extra_scale_factor |
| 61 | + |
| 62 | + super().__init__( |
| 63 | + params, |
| 64 | + lr, |
| 65 | + momentum_beta, |
| 66 | + use_nesterov=use_nesterov, |
| 67 | + weight_decay=weight_decay, |
| 68 | + weight_decay_method=weight_decay_method, |
| 69 | + fp32_matmul_prec=fp32_matmul_prec, |
| 70 | + scaled_orthogonalize_fn=scaled_orthogonalize_fn, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +MOP.__doc__ = MOP.__doc__.format(_args_doc=_args_doc) # type: ignore[union-attr] |
| 75 | + |
| 76 | + |
| 77 | +def polar_via_svd(A: torch.Tensor, return_p: bool = False) -> tuple[torch.Tensor, Optional[torch.Tensor]]: |
| 78 | + """Compute polar decomposition via SVD |
| 79 | +
|
| 80 | + Args: |
| 81 | + A: The input tensor to compute the polar decomposition of. |
| 82 | + return_p: Whether to return the positive-semidefinite part of the polar decomposition. p is not needed |
| 83 | + by the MOP optimizer, so by default it is not calculated to save computation. The option is provided to |
| 84 | + return full polar decomposition to match the function name. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + A tuple containing: |
| 88 | + - The unitary part of the polar decomposition. |
| 89 | + - The positive-semidefinite part of the polar decomposition, if return_p is True. |
| 90 | + """ |
| 91 | + U_svd, S, Vh = torch.linalg.svd(A, full_matrices=False) |
| 92 | + U_polar = U_svd @ Vh |
| 93 | + |
| 94 | + if not return_p: |
| 95 | + return U_polar, None |
| 96 | + else: |
| 97 | + p = Vh.mH @ torch.diag(S) @ Vh |
| 98 | + return U_polar, p |
0 commit comments