-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathadam.py
More file actions
67 lines (56 loc) · 2.19 KB
/
Copy pathadam.py
File metadata and controls
67 lines (56 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) 2026 Intel Corporation
#
# 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.
from typing import Union
import torch
from auto_round.algorithms.quantization.sign_round.quantizer import SignRoundQuantizer
from auto_round.schemes import QuantizationScheme
from auto_round.utils import check_is_cpu, htcore, is_hpex_available
from auto_round.utils.device_manager import device_manager
class AdamRoundQuantizer(SignRoundQuantizer):
def __init__(self, config):
super().__init__(config)
self.momentum = None # AdamW handles momentum internally
def _get_optimizer(self, optimizer):
if optimizer is None:
optimizer = torch.optim.AdamW
elif isinstance(optimizer, str):
optimizer = getattr(torch.optim, optimizer)
else:
optimizer = optimizer
return optimizer
def _get_scaler(self):
scaler = None
if self.model_context.amp and not check_is_cpu(device_manager.device):
from torch.cuda.amp import GradScaler
scaler = GradScaler(init_scale=1024, growth_interval=100000)
return scaler
def _scale_loss_and_backward(self, scaler, loss):
if scaler is not None:
loss = scaler.scale(loss)
loss.backward()
if is_hpex_available():
htcore.mark_step()
return loss
def _step(self, scaler, optimizer, lr_schedule):
if scaler is not None:
scaler.step(optimizer)
optimizer.zero_grad()
lr_schedule.step()
scaler.update()
else:
optimizer.step()
optimizer.zero_grad()
lr_schedule.step()
if is_hpex_available():
htcore.mark_step()