Skip to content

Cannot correctly compute cross entropy loss (backward) when the reduction used is "sum" and torch executor is used. #2056

Open
@protonu

Description

@protonu

Note: If you have a model or program that is not supported yet but should be, please use the program coverage template.

🐛 Bug

With the torch executor, Thunder cannot correctly compute cross entropy loss (backward) when the reduction used is "sum". It works correctly with "mean"

To Reproduce

Run the code snippet below.

Code sample

device='cuda'
executor=TorchExecutor


def cross_entropy_fn(logits, labels):
    return torch.nn.functional.cross_entropy(logits, labels, reduction="sum")


sequence_length, vocab_size = 8192, 32064
logits = torch.rand((sequence_length, vocab_size), device=device, dtype=thunder_dtype, requires_grad=True)
labels = torch.randint(0, sequence_length, (sequence_length,), requires_grad=False, device=device)

fn = executor.make_callable(cross_entropy_fn)
a = fn(logits, labels)
a.backward()
print(logits.grad)

Expected behavior

Should run correctly to completion with output matching that of torch compile.

Environment

  • PyTorch Version (e.g., 1.0):
  • OS (e.g., Linux):
  • How you installed PyTorch (conda, pip, source):
  • Build command you used (if compiling from source):
  • Python version:
  • CUDA/cuDNN version:
  • GPU models and configuration:
  • Any other relevant information:

Additional context

The error seen is:
https://gist.github.com/protonu/ff88fbc9508947f45e39a6190acc6508

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions