Skip to content

Commit

Permalink
Optimize summing of losses in run_step() & Fix typos (#862)
Browse files Browse the repository at this point in the history
Summary:
I noticed that the `run_step()` method uses `sum(loss for loss in loss_dict.values())` which could be simplified to `sum(loss_dict.values())`. A side benefit is faster execution.
Tested locally with a `loss_dict` consisting of 32 losses:

Proposed: `482 ns ± 53.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)`
Original: `2.02 µs ± 77.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)`

Plus I fixed some typos and an assertion along the way :)
Pull Request resolved: #862

Differential Revision: D19907088

Pulled By: ppwwyyxx

fbshipit-source-id: 403000087fe8fb302f5a7226a3320a3101951721
  • Loading branch information
kondela authored and facebook-github-bot committed Feb 14, 2020
1 parent 196ad03 commit d444a85
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions detectron2/engine/train_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,24 +204,24 @@ def run_step(self):
assert self.model.training, "[SimpleTrainer] model was changed to eval mode!"
start = time.perf_counter()
"""
If your want to do something with the data, you can wrap the dataloader.
If you want to do something with the data, you can wrap the dataloader.
"""
data = next(self._data_loader_iter)
data_time = time.perf_counter() - start

"""
If your want to do something with the losses, you can wrap the model.
If you want to do something with the losses, you can wrap the model.
"""
loss_dict = self.model(data)
losses = sum(loss for loss in loss_dict.values())
losses = sum(loss_dict.values())
self._detect_anomaly(losses, loss_dict)

metrics_dict = loss_dict
metrics_dict["data_time"] = data_time
self._write_metrics(metrics_dict)

"""
If you need accumulate gradients or something similar, you can
If you need to accumulate gradients or something similar, you can
wrap the optimizer with your custom `zero_grad()` method.
"""
self.optimizer.zero_grad()
Expand Down
2 changes: 1 addition & 1 deletion tools/plain_train_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def do_train(cfg, model, resume=False):
storage.step()

loss_dict = model(data)
losses = sum(loss for loss in loss_dict.values())
losses = sum(loss_dict.values())
assert torch.isfinite(losses).all(), loss_dict

loss_dict_reduced = {k: v.item() for k, v in comm.reduce_dict(loss_dict).items()}
Expand Down

0 comments on commit d444a85

Please sign in to comment.