Loads a state_dict created by an earlier call to state_dict(). If ``fp16_optimizer_instance`` was constructed from some ``init_optimizer``, whose parameters in turn came from ``model``, it is expected that the user will call ``model.load_state_dict()`` before
(self, state_dict)
| 311 | return state_dict |
| 312 | |
| 313 | def load_state_dict(self, state_dict): |
| 314 | """ |
| 315 | Loads a state_dict created by an earlier call to state_dict(). |
| 316 | If ``fp16_optimizer_instance`` was constructed from some ``init_optimizer``, |
| 317 | whose parameters in turn came from ``model``, it is expected that the user |
| 318 | will call ``model.load_state_dict()`` before |
| 319 | ``fp16_optimizer_instance.load_state_dict()`` is called. |
| 320 | |
| 321 | Example:: |
| 322 | |
| 323 | model = torch.nn.Linear(D_in, D_out).cuda().half() |
| 324 | optimizer = torch.optim.SGD(model.parameters(), lr=1e-3) |
| 325 | optimizer = FP16_Optimizer(optimizer, static_loss_scale = 128.0) |
| 326 | ... |
| 327 | checkpoint = torch.load("saved.pth") |
| 328 | model.load_state_dict(checkpoint['model']) |
| 329 | optimizer.load_state_dict(checkpoint['optimizer']) |
| 330 | """ |
| 331 | # I think it should actually be ok to reload the optimizer before the model. |
| 332 | self.loss_scaler = state_dict['loss_scaler'] |
| 333 | self.dynamic_loss_scale = state_dict['dynamic_loss_scale'] |
| 334 | self.overflow = state_dict['overflow'] |
| 335 | self.first_closure_call_this_step = state_dict['first_closure_call_this_step'] |
| 336 | self.optimizer.load_state_dict(state_dict['optimizer_state_dict']) |
| 337 | # At this point, the optimizer's references to the model's fp32 parameters |
| 338 | # are up to date. The optimizer's hyperparameters and internal buffers |
| 339 | # are also up to date. However, the fp32 master copies of the model's |
| 340 | # fp16 params stored by the optimizer are still out of date. There are |
| 341 | # two options. |
| 342 | # 1: Refresh the master params from the model's fp16 params. |
| 343 | # This requires less storage but incurs precision loss. |
| 344 | # 2: Save and restore the fp32 master copies separately. |
| 345 | # We choose option 2. |
| 346 | # |
| 347 | # Pytorch Optimizer.load_state_dict casts saved buffers (e.g. momentum) |
| 348 | # to the type and device of their associated parameters, because it's |
| 349 | # possible those buffers might not exist yet in the current optimizer |
| 350 | # instance. In our case, as long as the current FP16_Optimizer has been |
| 351 | # constructed in the same way as the one whose state_dict we are loading, |
| 352 | # the same master params are guaranteed to exist, so we can just copy_() |
| 353 | # from the saved master params. |
| 354 | for current_group, saved_group in zip(self.fp32_from_fp16_groups, |
| 355 | state_dict['fp32_from_fp16']): |
| 356 | for current, saved in zip(current_group, saved_group): |
| 357 | current.data.copy_(saved.data) |
| 358 | |
| 359 | def step(self, closure=None): # could add clip option. |
| 360 | """ |
no outgoing calls
no test coverage detected