MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / step

Method step

PATH/core/optimizers/adafactor.py:131–230  ·  view source on GitHub ↗

r"""Performs a single optimization step. Arguments: closure: A closure that reevaluates the model and returns the loss.

(self, closure: OptLossClosure = None)

Source from the content-addressed store, hash-verified

129 torch.mul(r_factor, c_factor, out=output)
130
131 def step(self, closure: OptLossClosure = None) -> OptFloat:
132 r"""Performs a single optimization step.
133
134 Arguments:
135 closure: A closure that reevaluates the model and returns the loss.
136 """
137 loss = None
138 if closure is not None:
139 loss = closure()
140
141 for group in self.param_groups:
142 for p in group['params']:
143 if p.grad is None:
144 continue
145 grad = p.grad.data
146 if grad.is_sparse:
147 raise RuntimeError(
148 'Adafactor does not support sparse gradients.'
149 )
150
151 state = self.state[p]
152 grad_shape = grad.shape
153
154 factored, use_first_moment = self._get_options(
155 group, grad_shape
156 )
157 # State Initialization
158 if len(state) == 0:
159 state['step'] = 0
160
161 if use_first_moment:
162 # Exponential moving average of gradient values
163 state['exp_avg'] = torch.zeros_like(
164 grad, memory_format=torch.preserve_format
165 )
166 if factored:
167 state['exp_avg_sq_row'] = torch.zeros(
168 grad_shape[:-1]
169 ).type_as(grad)
170 state['exp_avg_sq_col'] = torch.zeros(
171 grad_shape[:-2] + grad_shape[-1:]
172 ).type_as(grad)
173 else:
174 state['exp_avg_sq'] = torch.zeros_like(
175 grad, memory_format=torch.preserve_format
176 )
177
178 state['RMS'] = 0
179
180 state['step'] += 1
181 state['RMS'] = self._rms(p.data)
182 lr = self._get_lr(group, state)
183
184 beta2t = 1.0 - math.pow(state['step'], group['decay_rate'])
185
186 if group['clip_beta2'] != False:
187 beta2t = min(beta2t, group['clip_beta2'])
188

Callers

nothing calls this directly

Calls 4

_get_optionsMethod · 0.95
_rmsMethod · 0.95
_get_lrMethod · 0.95
_approx_sq_gradMethod · 0.95

Tested by

no test coverage detected