MCPcopy Index your code
hub / github.com/apple/ml-pointersect / _gather_losses

Method _gather_losses

pointersect/script/train_v2.py:1234–1270  ·  view source on GitHub ↗

Return the values in losses_name in a dictionary.

(
            self,
            loss_dict: T.Dict[str, torch.Tensor],
            return_float: bool = True,
    )

Source from the content-addressed store, hash-verified

1232 return loss_dict
1233
1234 def _gather_losses(
1235 self,
1236 loss_dict: T.Dict[str, torch.Tensor],
1237 return_float: bool = True,
1238 ) -> T.Dict[str, float]:
1239 """
1240 Return the values in losses_name in a dictionary.
1241 """
1242 if loss_dict is None:
1243 return None
1244
1245 out_dict = dict()
1246 keys = sorted(list(loss_dict.keys()))
1247 for name in keys: # sorted to support dist.reduce
1248 val = loss_dict[name]
1249 if val is None:
1250 continue
1251
1252 if not isinstance(val, torch.Tensor):
1253 out_dict[name] = val
1254 else:
1255 if self.process_info['distributed_run']:
1256 reduced_loss = reduce_tensor(val.detach()) / self.process_info['n_gpus']
1257 out_dict[name] = reduced_loss
1258 else:
1259 out_dict[name] = val.detach()
1260
1261 if return_float:
1262 for key, val in out_dict.items():
1263 if isinstance(val, torch.Tensor):
1264 out_dict[key] = val.detach().cpu().item()
1265 elif isinstance(val, np.ndarray):
1266 out_dict[key] = val.item()
1267 elif isinstance(val, int):
1268 out_dict[key] = float(val)
1269
1270 return out_dict
1271
1272 def reset_outputs(self):
1273 """

Callers 1

Calls 2

reduce_tensorFunction · 0.90
detachMethod · 0.45

Tested by

no test coverage detected