NVIDIA / NVIDIA/apex

Should I reduce the loss before the backward function?

Open
#664 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9k
Forks
1.5k
Avg merge
2d 4h
Merged PRs (30d)
3

Description

Recently, I do some experiments with the application of semantic segmentation.
There is over 1 point performance gap between two experiments:
1.

dist.all_reduce(loss, dist.ReduceOp.SUM)
loss.div_(world_size)

optimizer.zero_grad()
loss.backward()
optimizer.step()

Here, the loss applied backward function is the reduced loss.

# the reduced loss is just used for log
reduced_loss = loss.clone().detach()
dist.all_reduce(reduced_loss , dist.ReduceOp.SUM)
reduced_loss .div_(world_size)

optimizer.zero_grad()
loss.backward()
optimizer.step()

Here, the loss applied backward function is the original loss, while the reduced loss is simply used for logging.

Two experiments have the same setting excluding the loss reduction mentioned above.

According to my understanding of distributed training, the manner 2 is the correct one.
However, the manner 1 can achieve higher performance than manner 2.

Therefore, I want to know which one is correct? Or what is the correct method to handle the learning rate, batch size and loss?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files, tests, or entry points are mentioned. Start by reviewing the distributed-training guidance relevant to the two loss-reduction snippets, then document which approach is correct and how loss reduction relates to learning rate and batch size; done means the question has an explicit, reproducible explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.