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

Method clip_master_grads

PATH/core/fp16/opt.py:267–290  ·  view source on GitHub ↗

Clips fp32 master gradients via ``torch.nn.utils.clip_grad_norm``. Args: max_norm (float or int): max norm of the gradients norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for infinity norm. Returns:

(self, max_norm, norm_type=2)

Source from the content-addressed store, hash-verified

265 model_grads_to_master_grads(fp16_group, fp32_from_fp16_group)
266
267 def clip_master_grads(self, max_norm, norm_type=2):
268 """
269 Clips fp32 master gradients via ``torch.nn.utils.clip_grad_norm``.
270
271 Args:
272 max_norm (float or int): max norm of the gradients
273 norm_type (float or int): type of the used p-norm. Can be ``'inf'``
274 for infinity norm.
275
276 Returns:
277 Total norm of the current fp32 gradients (viewed as a single vector).
278
279 .. warning::
280 Returns -1 if the most recently computed fp16 gradients overflowed (
281 that is, if ``self.overflow`` is ``True``).
282 """
283 if not self.overflow:
284 fp32_params = []
285 for param_group in self.optimizer.param_groups:
286 for param in param_group['params']:
287 fp32_params.append(param)
288 return self.clip_grad_norm(fp32_params, max_norm, norm_type)
289 else:
290 return -1
291
292 def state_dict(self):
293 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected