Copy model gradients to master gradients. Args: model_params: List of model parameters created by :func:`prep_param_lists`. master_params: List of FP32 master parameters created by :func:`prep_param_lists`. If ``master_params`` was created with ``flat
(model_params, master_params, flat_master=False)
| 283 | |
| 284 | |
| 285 | def model_grads_to_master_grads(model_params, master_params, flat_master=False): |
| 286 | """ |
| 287 | Copy model gradients to master gradients. |
| 288 | |
| 289 | Args: |
| 290 | model_params: List of model parameters created by :func:`prep_param_lists`. |
| 291 | master_params: List of FP32 master parameters created by |
| 292 | :func:`prep_param_lists`. If ``master_params`` was created with |
| 293 | ``flat_master=True``, ``flat_master=True`` should also be supplied to |
| 294 | :func:`model_grads_to_master_grads`. |
| 295 | """ |
| 296 | if flat_master: |
| 297 | # The flattening may incur one more deep copy than is necessary. |
| 298 | master_params[0].grad.data.copy_( |
| 299 | _flatten_dense_tensors([p.grad.data for p in model_params])) |
| 300 | else: |
| 301 | for model, master in zip(model_params, master_params): |
| 302 | if model.grad is not None: |
| 303 | if master.grad is None: |
| 304 | master.grad = Variable(master.data.new(*master.data.size())) |
| 305 | master.grad.data.copy_(model.grad.data) |
| 306 | else: |
| 307 | master.grad = None |
| 308 | |
| 309 | |
| 310 | def master_params_to_model_params(model_params, master_params, flat_master=False): |
no test coverage detected