(self, param_group)
| 54 | self._add_state(param, "step", initializer=0.0) |
| 55 | |
| 56 | def _updates(self, param_group): |
| 57 | lr = param_group["lr"] |
| 58 | weight_decay = param_group["weight_decay"] |
| 59 | eps = param_group["eps"] |
| 60 | beta0, beta1 = param_group["betas"] |
| 61 | |
| 62 | def make_scalar(val): |
| 63 | return tensor(val, dtype="float32") |
| 64 | |
| 65 | # since `conver_inputs` is disabled for param updates, |
| 66 | # scalar should be explicitly tansforred to tensor |
| 67 | |
| 68 | _lr, _neg_lr = map(make_scalar, (lr, -lr)) |
| 69 | _weight_decay = make_scalar(weight_decay) |
| 70 | _eps = make_scalar(eps) |
| 71 | _beta0, _beta1 = map(make_scalar, (beta0, beta1)) |
| 72 | |
| 73 | c1, c05 = map(make_scalar, (1.0, 0.5)) |
| 74 | |
| 75 | inplace_mode = int(os.getenv("MEGENGINE_INPLACE_UPDATE", "0")) |
| 76 | if inplace_mode: |
| 77 | # reduce device sync |
| 78 | c1_sub_beta0, c1_sub_beta1 = map(make_scalar, (1 - beta0, 1 - beta1)) |
| 79 | |
| 80 | for param in param_group["params"]: |
| 81 | |
| 82 | if param.grad is None: |
| 83 | continue |
| 84 | |
| 85 | grad = param.grad |
| 86 | |
| 87 | states = self._state[param] |
| 88 | |
| 89 | step, exp_avg, exp_avg_sq = ( |
| 90 | states["step"], |
| 91 | states["exp_avg"], |
| 92 | states["exp_avg_sq"], |
| 93 | ) |
| 94 | |
| 95 | if inplace_mode: |
| 96 | _inplace_add_(step, c1, alpha=c1, beta=c1) |
| 97 | _inplace_add_(exp_avg, grad, alpha=_beta0, beta=c1_sub_beta0) |
| 98 | _inplace_add_( |
| 99 | exp_avg_sq, grad * grad, alpha=_beta1, beta=c1_sub_beta1, |
| 100 | ) |
| 101 | |
| 102 | delta = (exp_avg / (c1 - _beta0 ** step)) / ( |
| 103 | (exp_avg_sq / (c1 - _beta1 ** step)) ** c05 + _eps |
| 104 | ) |
| 105 | if is_tracing() or weight_decay != 0.0: |
| 106 | delta += param * _weight_decay |
| 107 | _inplace_add_(param, delta, alpha=c1, beta=_neg_lr) |
| 108 | continue |
| 109 | |
| 110 | # step = step + c1 |
| 111 | step += c1 |
| 112 | |
| 113 | # exp_avg = _beta0 * exp_avg + grad * (c1 - _beta0) |
nothing calls this directly
no test coverage detected