cut-cross-entropy

Cross-entropy over a large vocabulary without ever materializing the logits, loadable through kernels. The reference baseline is F.cross_entropy on materialized fp32 logits, matched below 1e-5 relative on the loss with gradients at bf16 rounding. Companion to det-cross-entropy, which fixes the same reduction for reproducibility rather than memory. After Wijmans et al., Cut Your Losses in Large-Vocabulary Language Models (2024).

The loss of a language model is a softmax over E @ C.T, and the usual route forms the [N, V] logit tensor: at 65,536 tokens against a 128,256-token vocabulary that is 16 GB in bf16, held by autograd along with the log-softmax until backward. Nothing in the loss needs it to exist. This kernel blocks the vocabulary into panels, folds each panel into a running online-softmax state, and rebuilds panels during backward, so the training step that could not fit on the card at all simply runs, in gigabytes instead of tens of gigabytes.

Memory bars per token count: the materialized path grows past the 48 GB wall and then fails outright while the blocked path stays under 4 GB

Forward and backward at vocabulary 128,256, measured live: the materialized path climbs 16 to 63 GB, crawls at 14 s once past the card, and at 65,536 tokens raises CUDA out of memory; the blocked path runs every row in 1.2 to 3.4 GB, agreeing with the reference wherever both run.

Usage

import torch
from kernels import get_kernel

cce = get_kernel("phanerozoic/cut-cross-entropy", version=1, trust_remote_code=True)

hidden = model.model(input_ids).last_hidden_state      # [B, T, D] bf16
loss = cce.linear_cross_entropy(
    hidden.reshape(-1, hidden.shape[-1]),              # [N, D]
    model.lm_head.weight,                              # [V, D]
    labels.reshape(-1),                                # [N] int64
)
loss.backward()

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark. The classifier is passed at call time, so its gradient lands in lm_head.weight.grad as usual.

API

Symbol Purpose
linear_cross_entropy(E, C, targets, reduction, ignore_index) cross-entropy of E @ C.T against targets, logits never formed
cross_entropy(E, C, targets, ...) alias
CutCrossEntropyLoss(reduction, ignore_index) nn.Module form; stateless, classifier passed to forward
ops.cce_forward(E, C, targets, ignore_index) (loss [N], lse [N]) fp32, the state reused by backward
ops.cce_backward(E, C, targets, lse, dloss, ignore_index) (dE, dC)

Method

The forward needs only three per-token quantities: the row maximum, the row sum of exponentials, and the target logit; the backward needs only the softmax, a fixed function of that state rebuildable one block at a time. Both passes block the vocabulary into BV-column panels. Each panel's logits are one bf16 tensor-core GEMM (fp32 accumulate); the forward folds the panel into a running (max, sumexp) by the online-softmax rescale and captures the target logit as its column passes. The backward recomputes the panel, rebuilds p = exp(x - lse), subtracts the one-hot, and contracts into dE and dC; a panel owns a disjoint vocabulary slice, so its dC block is a single beta = 0 write with no atomics. Resident cost is the [N, BV] panel rather than [N, V].

Measured

Peak allocation and wall-clock for a full forward and backward against F.cross_entropy on materialized logits, bf16, D = 2048 unless noted:

tokens V torch peak this peak memory torch this
2,048 32,000 796 MB 618 MB 1.29x 7.3 ms 9.3 ms
4,096 32,000 1,186 MB 659 MB 1.80x 13.5 ms 20.9 ms
8,192 32,000 1,969 MB 739 MB 2.67x 29.0 ms 51.1 ms
4,096 128,256 4,194 MB 1,787 MB 2.35x 69.5 ms 101.4 ms
8,192 128,256 7,233 MB 1,867 MB 3.87x 159.8 ms 201.8 ms
16,384 128,256 13,309 MB 2,028 MB 6.56x 300.9 ms 450.1 ms
32,768 128,256 25,461 MB 2,395 MB 10.63x 615.6 ms 971.4 ms
65,536 128,256 49,766 MB 3,132 MB 15.89x 39,526 ms 1,998 ms
8,192 (D=4096) 151,936 9,779 MB 4,086 MB 2.39x 335.7 ms 459.7 ms

Two regimes. While the materialized path fits it is 1.3x to 1.6x faster, because this kernel recomputes each logit panel in the backward instead of storing it. At 65,536 tokens the materialized path wants 48.6 GB on a 48 GB card and takes 39.5 seconds where memory oversubscription exists, or fails outright where it does not; the blocked path uses 3.1 GB and 2.0 seconds. The crossover is not a tuning parameter but the point where [N, V] stops fitting.

Correctness

  • Loss: maximum relative deviation below 1e-5 across shapes to (16, 2048, 128256) and all three reductions; most cases bitwise.
  • Gradients: dE and dC agree with autograd through the materialized path to 2.6e-3 relative worst case (bf16 gradient rounding).
  • Masking: ignore_index rows contribute nothing and receive exactly zero gradient; a fully masked batch gives zero loss and finite zero gradients.
  • A ragged vocabulary with a target inside the short final panel matches to 1e-4; repeated forward passes are bitwise identical.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+ and bf16 tensor cores.
  • E [N, D] and C [V, D] bf16, targets [N] int64.
  • The [V, D] weight gradient is unavoidable; this kernel removes the [N, V] term. Small vocabularies leave little to save.
  • Slower than the materialized path whenever that path fits comfortably; use this where the logit tensor is the constraint.

References

Wijmans, Huval, Hertzberg, Koltun, Krähenbühl, "Cut Your Losses in Large-Vocabulary Language Models" (2024); Milakov and Gimelshein, "Online normalizer calculation for softmax" (2018).

License

Apache-2.0.

Downloads last month
-
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
19aaa64