Lightning-AI / Lightning-AI/pytorch-lightning

Loss balancer

Open
#16,466 4 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature optimization
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Description & Motivation

It's commonplace to combine many losses (l1 loss, feature matching loss, multiscale losses, etc.). However, when they are on different scales, it is fiddly to choose the weighting. And, the weighting might change over time.

A loss balancer would be a useful addition, using EMA.

The API could be this simple:

loss = self.balancer(loss1, loss2, ...)

(By default, each loss would have equal weight, but this could be parameterized in the balancer class.)

Pitch

Meta's encodec has a loss balancer and it makes it very easy for them to mix many different scaled losses.

This would probably be an easy win for many lightning users that mix losses.

Alternatives

There is available code for the balancer: https://github.com/facebookresearch/encodec/blob/main/encodec/balancer.py

However: a) the licensing is not permissive and b) it doesn't work in DDP.

Additional context

https://arxiv.org/pdf/2210.13438.pdf

cc @borda

Contributor guide

Open the contributing guide

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

Start by reading the referenced encodec/balancer.py implementation and the issue's proposed self.balancer(loss1, loss2, ...) API. Investigate how the balancer should support EMA weighting and DDP, then identify the appropriate Lightning entry point and tests. Done means a documented loss-balancing feature that combines differently scaled losses and works in distributed training.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.