Text2Text Generation
Transformers
PyTorch
Safetensors
French
flash_t5
custom_code
File size: 12,288 Bytes
0743270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import torch
import math

from torch.optim import Optimizer
from torch.optim.optimizer import _default_to_fused_or_foreach
from torch.utils._foreach_utils import _group_tensors_by_device_and_dtype
from typing import Iterable, Tuple
from torch import nn, Tensor

class AdamWScale(Optimizer):
    """
    This AdamW implementation is copied from Huggingface.
    We modified it with Adagrad scaling by rms of a weight tensor

    Implements Adam algorithm with weight decay fix as introduced in [Decoupled Weight Decay
    Regularization](https://arxiv.org/abs/1711.05101).

    Parameters:
        params (`Iterable[nn.parameter.Parameter]`):
            Iterable of parameters to optimize or dictionaries defining parameter groups.
        lr (`float`, *optional*, defaults to 1e-3):
            The learning rate to use.
        betas (`Tuple[float,float]`, *optional*, defaults to (0.9, 0.999)):
            Adam's betas parameters (b1, b2).
        eps (`float`, *optional*, defaults to 1e-6):
            Adam's epsilon for numerical stability.
        weight_decay (`float`, *optional*, defaults to 0.0):
            Decoupled weight decay to apply.
        kahan_sum (`bool`, *optional*, defaults to False):
            Whether to use Kahan summation for updating parameters.
        foreach (`bool`, *optional*, defaults to False):
            Whether to use the foreach implementation.
        correct_bias (`bool`, *optional*, defaults to True):
            Whether to correct bias in Adam.
        use_state_dtype (`torch.dtype`, *optional*, defaults to None):
            The dtype to use for optimizer state. If None, use the default dtype.
    """

    def __init__(
        self,
        params: Iterable[nn.parameter.Parameter],
        lr: float = 1e-3,
        betas: Tuple[float, float] = (0.9, 0.999),
        eps: float = 1e-6,
        weight_decay: float = 0.0,
        kahan_sum: bool = False,
        foreach: bool = False,
        correct_bias: bool = True,
        use_state_dtype: torch.dtype = None
    ):
        if lr < 0.0:
            raise ValueError(f"Invalid learning rate: {lr} - should be >= 0.0")
        if not 0.0 <= betas[0] < 1.0:
            raise ValueError(f"Invalid beta parameter: {betas[0]} - should be in [0.0, 1.0)")
        if not 0.0 <= betas[1] < 1.0:
            raise ValueError(f"Invalid beta parameter: {betas[1]} - should be in [0.0, 1.0)")
        if not 0.0 <= eps:
            raise ValueError(f"Invalid epsilon value: {eps} - should be >= 0.0")

        assert not (foreach and use_state_dtype is not None), "foreach is not supported with use_state_dtype"

        defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, foreach=foreach, \
            kahan_sum=kahan_sum, correct_bias=correct_bias, use_state_dtype=use_state_dtype)

        super().__init__(params, defaults)

    @staticmethod
    def _rms(tensor):
        return tensor.norm(2) / (tensor.numel() ** 0.5)

    @torch.no_grad()
    def step(self, closure=None):
        """
        Performs a single optimization step.

        Arguments:
            closure (`Callable`, *optional*): A closure that reevaluates the model and returns the loss.
        """
        loss = None
        if closure is not None:
            loss = closure()

        for group in self.param_groups:
            params, grads, exp_avgs, exp_avg_sqs, steps, kahan_comps = [], [], [], [], [], []

            # Initialization
            for p in group['params']:
                if p.grad is None:
                    continue

                params.append(p)
                if p.grad.is_sparse:
                    raise RuntimeError('AdamWScale does not support sparse gradients')
                grads.append(p.grad)

                state = self.state[p]

                # State initialization
                if "kahan_comp" not in state:
                    state['step'] = torch.tensor(0, dtype=torch.int32, device=p.device)

                    if group["use_state_dtype"] in [torch.float16, torch.bfloat16]:
                        state['exp_avg'] = torch.zeros_like(p, device=p.device, dtype=group["use_state_dtype"])
                        state['exp_avg_sq'] = torch.zeros_like(p, device=p.device, dtype=group["use_state_dtype"])
                    else:
                        state['exp_avg'] = torch.zeros_like(p, memory_format=torch.preserve_format)
                        state['exp_avg_sq'] = torch.zeros_like(p, memory_format=torch.preserve_format)

                    if group["kahan_sum"] and p.dtype in [torch.float16, torch.bfloat16]:
                        state["kahan_comp"] = torch.zeros_like(p, memory_format=torch.preserve_format)
                    else:
                        state["kahan_comp"] = None
                        group["kahan_sum"] = False

                exp_avgs.append(state['exp_avg'])
                exp_avg_sqs.append(state['exp_avg_sq'])
                kahan_comps.append(state["kahan_comp"])
                steps.append(state["step"])

            torch._foreach_add_(steps, 1)

            # AdamW step
            if group["foreach"] and _default_to_fused_or_foreach(params, False, False):
                self._foreach_adamwscaled(params,
                                          grads,
                                          exp_avgs,
                                          exp_avg_sqs,
                                          steps,
                                          kahan_comps,
                                          group["lr"],
                                          group["betas"][0],
                                          group["betas"][1],
                                          group["weight_decay"],
                                          group["eps"],
                                          group["kahan_sum"],
                                          group["correct_bias"])
            else:
                self._adamwscaled(params,
                                  grads,
                                  exp_avgs,
                                  exp_avg_sqs,
                                  steps,
                                  kahan_comps,
                                  group["lr"],
                                  group["betas"][0],
                                  group["betas"][1],
                                  group["weight_decay"],
                                  group["eps"],
                                  group["kahan_sum"],
                                  group["correct_bias"])

        return loss

    def _adamwscaled(self,
                    params: list[Tensor],
                    grads: list[Tensor],
                    exp_avgs: list[Tensor],
                    exp_avg_sqs: list[Tensor],
                    steps: list[Tensor],
                    kahan_comps: list[Tensor],
                    lr: float,
                    beta1: float,
                    beta2: float,
                    weight_decay: float,
                    eps: float,
                    do_kahan_sum: bool,
                    correct_bias: bool):

        for i, p in enumerate(params):

            exp_avg, exp_avg_sq, grad, step, kahan_comp = exp_avgs[i], exp_avg_sqs[i], grads[i], steps[i], kahan_comps[i]

            # Decay the first and second moment running average coefficient
            # In-place operations to update the averages at the same time
            exp_avg.mul_(beta1).add_(grad, alpha=(1.0 - beta1))
            exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=(1.0 - beta2))
            denom = exp_avg_sq.sqrt().add_(eps)

            step_size = lr
            if correct_bias:  # No bias correction for Bert
                bias_correction1 = 1.0 - beta1 ** step
                bias_correction2 = 1.0 - beta2 ** step
                step_size = step_size * math.sqrt(bias_correction2) / bias_correction1

            # Adapt Step from Adafactor
            step_size = step_size * max(1e-3, self._rms(p.data))

            if do_kahan_sum:
                # Adam step
                kahan_comp.addcdiv_(exp_avg, denom, value=-step_size)

                # update weights with kahan compensation using dev_grads as temp buffer
                grad.copy_(p)
                p.add_(kahan_comp)

                # save error back to kahan compensation for next iteration
                grad.sub_(p, alpha=1)
                kahan_comp.add_(grad, alpha=1)
            else:
                p.addcdiv_(exp_avg, denom, value=-step_size)

            # Just adding the square of the weights to the loss function is *not*
            # the correct way of using L2 regularization/weight decay with Adam,
            # since that will interact with the m and v parameters in strange ways.
            #
            # Instead we want to decay the weights in a manner that doesn't interact
            # with the m/v parameters. This is equivalent to adding the square
            # of the weights to the loss with plain (non-momentum) SGD.
            # Add weight decay at the end (fixed version)
            if weight_decay > 0.0:
                p.add_(p, alpha=(-lr * weight_decay))

    def _foreach_adamwscaled(self,
                    params: list[Tensor],
                    grads: list[Tensor],
                    exp_avgs: list[Tensor],
                    exp_avg_sqs: list[Tensor],
                    steps: list[Tensor],
                    kahan_comps: list[Tensor],
                    lr: float,
                    beta1: float,
                    beta2: float,
                    weight_decay: float,
                    eps: float,
                    do_kahan_sum: bool,
                    correct_bias: bool):

        grouped_tensors = _group_tensors_by_device_and_dtype([params, grads, exp_avgs, exp_avg_sqs, kahan_comps])

        for (_, dtype), ((dev_params, dev_grads, dev_exp_avgs, dev_exp_avg_sqs, dev_kahan_comps), _) in grouped_tensors.items():
            # Foreach implementation
            torch._foreach_mul_(dev_exp_avgs, beta1)
            torch._foreach_add_(dev_exp_avgs, dev_grads, alpha=1 - beta1)

            torch._foreach_mul_(dev_exp_avg_sqs, beta2)
            torch._foreach_addcmul_(dev_exp_avg_sqs, dev_grads, dev_grads, 1 - beta2)

            # Compute denominator
            torch._foreach_copy_(dev_grads, dev_exp_avg_sqs)
            torch._foreach_sqrt_(dev_grads)
            torch._foreach_add_(dev_grads, eps)

            step_size = [torch.tensor(lr, dtype=torch.float32, device=p.device) for p in dev_params]

            if correct_bias:
                torch._foreach_mul_(step_size,
                                   [torch.tensor((math.sqrt(1 - beta2 ** steps[i].item()) / (1 - beta1 ** steps[i].item()) ), dtype=torch.float32, device=p.device)
                                        for i, p in enumerate(dev_params)])

            # Adapt step size using RMS of parameters
            rms_p = torch._foreach_norm(dev_params)
            numel = [torch.tensor(math.sqrt(p.numel())) for p in dev_params]
            torch._foreach_div_(rms_p, numel)
            torch._foreach_maximum_(rms_p, 1e-3)

            torch._foreach_mul_(step_size, rms_p)
            torch._foreach_div_(dev_grads, step_size)

            # explicitly delete tensors when not used
            del rms_p
            del numel
            del step_size

            # Update parameters
            if do_kahan_sum:
                # Adam step
                torch._foreach_addcdiv_(dev_kahan_comps, dev_exp_avgs, dev_grads, value=-1)

                # update weights with kahan compensation using dev_grads as temp buffer
                torch._foreach_copy_(dev_grads, dev_params)
                torch._foreach_add_(dev_params, dev_kahan_comps, alpha=1)

                # save error back to kahan compensation for next iteration
                torch._foreach_sub_(dev_grads, dev_params, alpha=1)
                torch._foreach_add_(dev_kahan_comps, dev_grads, alpha=1)
            else:
                torch._foreach_addcdiv_(dev_params, dev_exp_avgs, dev_grads, value=-1)

            # Weight decay
            if weight_decay > 0.0:
                torch._foreach_add_(dev_params, dev_params, alpha=-weight_decay * lr)