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

Method backward

PATH/core/fp16/opt.py:471–541  ·  view source on GitHub ↗

:attr:`backward` performs the following conceptual steps: 1. fp32_loss = loss.float() (see first Note below) 2. scaled_loss = fp32_loss*loss_scale 3. scaled_loss.backward(), which accumulates scaled gradients into the ``.grad`` attributes of the model'

(self, loss, update_master_grads=True)

Source from the content-addressed store, hash-verified

469 return retval
470
471 def backward(self, loss, update_master_grads=True):
472 """
473 :attr:`backward` performs the following conceptual steps:
474
475 1. fp32_loss = loss.float() (see first Note below)
476 2. scaled_loss = fp32_loss*loss_scale
477 3. scaled_loss.backward(), which accumulates scaled gradients into the
478 ``.grad`` attributes of the model's leaves (which may be fp16, fp32,
479 or a mixture, depending how your model was defined).
480 4. fp16 grads are then copied to the master params' ``.grad`` attributes
481 (see second Note), which are guaranteed to be fp32.
482 5. Finally, master grads are divided by loss_scale.
483
484 In this way, after :attr:`backward`, the master params have fresh
485 gradients, and :attr:`step` may be called.
486
487 .. note::
488 :attr:`backward` internally converts the loss to fp32 before
489 applying the loss scale. This provides some additional safety
490 against overflow if the user has supplied an fp16 loss value.
491 However, for maximum overflow safety, the user should
492 compute the loss criterion (MSE, cross entropy, etc) in fp32 before
493 supplying it to :attr:`backward`.
494
495 .. warning::
496 The gradients found in a model's leaves after the call to
497 :attr:`backward` should not be regarded as valid in general,
498 because it's possible
499 they have been scaled (and in the case of dynamic loss scaling,
500 the scale factor may change over time).
501 If the user wants to inspect gradients after a call to :attr:`backward`,
502 only the master gradients should be regarded as valid. These can be
503 retrieved via :attr:`inspect_master_grad_data()`.
504
505 Args:
506 loss: The loss output by the user's model. loss may be either
507 float or half (but see first Note above). update_master_grads (bool,
508 optional, default=True): Option to copy fp16 grads to fp32 grads
509 on this call. By setting this to False, the user can delay the copy,
510 which is useful to eliminate redundant fp16->fp32 grad copies if
511 :attr:`backward` is being called on multiple losses in one iteration.
512 If set to False, the user becomes responsible for calling
513 :attr:`update_master_grads` before calling :attr:`step`.
514
515 Example::
516
517 # Ordinary operation:
518 optimizer.backward(loss)
519
520 # Naive operation with multiple losses (technically valid, but less
521 # efficient):
522 # fp32 grads will be correct after the second call, but
523 # the first call incurs an unnecessary fp16->fp32 grad copy.
524 optimizer.backward(loss1)
525 optimizer.backward(loss2)
526
527 # More efficient way to handle multiple losses:
528 # The fp16->fp32 grad copy is delayed until fp16 grads from all

Callers

nothing calls this directly

Calls 1

update_master_gradsMethod · 0.95

Tested by

no test coverage detected