deepspeedai / deepspeedai/DeepSpeed

F.cross_entropy returns infs sometimes due to it summing the losses.

Open
#962 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
43.1k
Forks
5k
Avg merge
4d 15h
Merged PRs (30d)
112

Description

Hello,

F.cross_entropy returns infs sometimes due to it summing the losses.

Findings:

The following SOMETIMES returns inf loss (the default options in F.cross_entropy)

import torch.nn.functional as F
import torch as th

logits = th.randn(32, 1000)
labels = th.randint(low=0, high=1000, size=(32, ))
labels[18] = -100

loss = F.cross_entropy(logits, labels, ignore_index=-100, reduction="mean")
print(loss)

The following will NOT return inf

loss = F.cross_entropy(logits, labels, ignore_index=-100, reduction="none")
loss = loss.mean()
print(loss)

The following SOMETIMES returns inf

loss = F.cross_entropy(logits, labels, ignore_index=-100, reduction="none")
loss = loss.sum()
print(loss)

The solution I am currently using to do normal cross entropy in DeepSpeed:

loss = F.cross_entropy(logits, labels, reduction="none")
numel = labels.numel()
numel_no_mask = labels.ne(-100).sum()
norm = numel_no_mask / numel

loss = loss.mean() / norm
print(loss)

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

Reproduce the reported F.cross_entropy examples with ignore_index=-100 and compare the mean, none, and sum reductions. Then trace the DeepSpeed loss path referenced in the report to determine whether the correction belongs in DeepSpeed or upstream PyTorch; done should mean masked cross-entropy no longer produces inf values and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.