:class:`FP16_Optimizer` is designed to wrap an existing PyTorch optimizer, and manage static or dynamic loss scaling and master weights in a manner transparent to the user. For standard use, only two lines must be changed: creating the :class:`FP16_Optimizer` instance, and chang
| 20 | |
| 21 | # TODO: Update overflow check + downscale to use Carl's fused kernel. |
| 22 | class FP16_Optimizer(object): |
| 23 | """ |
| 24 | :class:`FP16_Optimizer` is designed to wrap an existing PyTorch optimizer, |
| 25 | and manage static or dynamic loss scaling and master weights in a manner |
| 26 | transparent to the user. For standard use, only two lines must be changed: |
| 27 | creating the :class:`FP16_Optimizer` instance, and changing the call to |
| 28 | ``backward``. |
| 29 | |
| 30 | Example:: |
| 31 | |
| 32 | model = torch.nn.Linear(D_in, D_out).cuda().half() |
| 33 | optimizer = torch.optim.SGD(model.parameters(), lr=1e-3) |
| 34 | # Name the FP16_Optimizer instance to replace the existing optimizer |
| 35 | # (recommended but not required): |
| 36 | optimizer = FP16_Optimizer(optimizer, static_loss_scale = 128.0) |
| 37 | ... |
| 38 | # loss.backward() becomes: |
| 39 | optimizer.backward(loss) |
| 40 | ... |
| 41 | |
| 42 | Example with dynamic loss scaling:: |
| 43 | |
| 44 | ... |
| 45 | optimizer = FP16_Optimizer(optimizer, dynamic_loss_scale=True) |
| 46 | |
| 47 | Args: |
| 48 | init_optimizer (torch.optim.optimizer): Existing optimizer created with |
| 49 | the parameters to optimize. Internally, :class:`FP16_Optimizer` |
| 50 | replaces the passed optimizer's fp16 parameters, if any, with fp32 |
| 51 | master parameters copied from the original ones. |
| 52 | :class:`FP16_Optimizer` also stores references to the original fp16 |
| 53 | parameters, and updates these fp16 parameters from the master fp32 |
| 54 | copy at the end of each :attr:`step`. |
| 55 | static_loss_scale (float, optional, default=1.0): Loss scale used |
| 56 | internally to scale gradients computed by the model. Any fp16 |
| 57 | gradients will be copied to fp32, then downscaled before being |
| 58 | applied to the fp32 master params, so ``static_loss_scale`` should |
| 59 | not affect learning rate. |
| 60 | dynamic_loss_scale (bool, optional, default=False): Use dynamic loss |
| 61 | scaling. If True, this will override any ``static_loss_scale`` |
| 62 | option. |
| 63 | verbose (bool, optional, default=False): By default, FP16_Optimizer's |
| 64 | constructor prints out the parameters and parameter groups it is |
| 65 | ingesting, as a sanity check. If this becomes annoying (e.g. for |
| 66 | large models), it can be disabled by passing ``verbose=False``. |
| 67 | ``verbose=False`` will not disable printing when the loss scale is |
| 68 | readjusted during dynamic loss scaling. |
| 69 | |
| 70 | ``init_optimizer`` is expected to have been constructed in the ordinary way. |
| 71 | It is recommended (although not required) that the newly constructed |
| 72 | :class:`FP16_Optimizer` instance be |
| 73 | named to replace ``init_optimizer``, for two reasons: |
| 74 | First, it means that references to the same name later in the file will not |
| 75 | have to change. |
| 76 | Second, :class:`FP16_Optimizer` reserves the right (as an implementation |
| 77 | detail) to modify ``init_optimizer``. |
| 78 | If you do choose a unique name for the new :class:`FP16_Optimizer` instance, |
| 79 | you should only work with this new instance, because the preexisting |
nothing calls this directly
no outgoing calls
no test coverage detected