Download diffusion/model/dc_ae/efficientvit/apps/utils/opt.py from gen6scp/sana-zero: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/spaces/gen6scp/sana-zero/resolve/main/diffusion/model/dc_ae/efficientvit/apps/utils/opt.py
- Command line
-
hf download hf://spaces/gen6scp/sana-zero/diffusion/model/dc_ae/efficientvit/apps/utils/opt.py
-
curl -L -o opt.py https://huggingface.co/spaces/gen6scp/sana-zero/resolve/main/diffusion/model/dc_ae/efficientvit/apps/utils/opt.py
1.62 kB
| # Copyright 2024 MIT Han Lab | |
| # | |
| # 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 | |
| # | |
| # http://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. | |
| # | |
| # SPDX-License-Identifier: Apache-2.0 | |
| from typing import Any, Optional | |
| import torch | |
| __all__ = ["REGISTERED_OPTIMIZER_DICT", "build_optimizer"] | |
| # register optimizer here | |
| # name: optimizer, kwargs with default values | |
| REGISTERED_OPTIMIZER_DICT: dict[str, tuple[type, dict[str, Any]]] = { | |
| "sgd": (torch.optim.SGD, {"momentum": 0.9, "nesterov": True}), | |
| "adam": (torch.optim.Adam, {"betas": (0.9, 0.999), "eps": 1e-8, "amsgrad": False}), | |
| "adamw": (torch.optim.AdamW, {"betas": (0.9, 0.999), "eps": 1e-8, "amsgrad": False}), | |
| } | |
| def build_optimizer( | |
| net_params, optimizer_name: str, optimizer_params: Optional[dict], init_lr: float | |
| ) -> torch.optim.Optimizer: | |
| optimizer_class, default_params = REGISTERED_OPTIMIZER_DICT[optimizer_name] | |
| optimizer_params = {} if optimizer_params is None else optimizer_params | |
| for key in default_params: | |
| if key in optimizer_params: | |
| default_params[key] = optimizer_params[key] | |
| optimizer = optimizer_class(net_params, init_lr, **default_params) | |
| return optimizer | |