Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Cross entropy: trace under torch.compile(dynamic=True) (#16059)

Daniel Hanbdea0c0bc128
diff --git a/tests/test_cross_entropy_compiled_grads.py b/tests/test_cross_entropy_compiled_grads.py
index 6e94be0868a..a5c5a17ec83 100644
--- a/tests/test_cross_entropy_compiled_grads.py
+++ b/tests/test_cross_entropy_compiled_grads.py
@@ -14,10 +14,16 @@ if not torch.cuda.is_available() or torch.version.hip is not None:
from unsloth.kernels import cross_entropy_loss as ce_mod
+_TORCH = tuple(int(x) for x in torch.__version__.split("+")[0].split(".")[:2])
+
@pytest.mark.parametrize("vocab", [32768, 65537, 151936])
@pytest.mark.parametrize("fullgraph", [False, True])
-def test_compiled_grads_match_the_fp64_reference(vocab, fullgraph):
[email protected]("dynamic", [None, True])
+def test_compiled_grads_match_the_fp64_reference(vocab, fullgraph, dynamic):
+ if dynamic and _TORCH < (2, 10):
+ # torch 2.6 Dynamo also rejects bool() of the symbolic softcap int; checked from 2.10 on.
+ pytest.skip("dynamic=True Triton cross entropy is checked on torch 2.10+")
torch._dynamo.reset()
generator = torch.Generator(device = "cuda").manual_seed(vocab)
# sm75 (T4) has no bf16 Triton under torch.compile.
@@ -35,7 +41,9 @@ def test_compiled_grads_match_the_fp64_reference(vocab, fullgraph):
)
expected.backward()
x = logits.clone().requires_grad_(True)
- loss = torch.compile(ce_mod.fast_cross_entropy_loss, fullgraph = fullgraph)(x, labels)
+ # dynamic=True: the vocab size is symbolic, so the host-side chunk counts must trace (no divmod).
+ compiled = torch.compile(ce_mod.fast_cross_entropy_loss, fullgraph = fullgraph, dynamic = dynamic)
+ loss = compiled(x, labels)
loss.backward()
torch.testing.assert_close(loss.double(), expected.detach(), rtol = 1e-4, atol = 1e-4)
rel = (x.grad.double() - reference.grad).norm() / reference.grad.norm()
diff --git a/unsloth/kernels/cross_entropy_loss.py b/unsloth/kernels/cross_entropy_loss.py
index 8c8ade294ad..087a758ec21 100644
--- a/unsloth/kernels/cross_entropy_loss.py
+++ b/unsloth/kernels/cross_entropy_loss.py
@@ -405,8 +405,8 @@ class Fast_CrossEntropyLoss(torch.autograd.Function):
logits = logits.contiguous()
labels = labels.to(device).contiguous()
- div, mod = divmod(vocab_size, MAX_FUSED_SIZE)
- n_chunks: int = div + (mod != 0)
+ # Ceil division, not divmod: Dynamo cannot trace divmod on a symbolic size (dynamic=True).
+ n_chunks: int = (vocab_size + MAX_FUSED_SIZE - 1) // MAX_FUSED_SIZE
losses = torch.empty(n_rows, dtype = torch.float32, device = device)
DO_SOFTCAPPING: bool = bool(logit_softcapping != 0)
@@ -440,8 +440,7 @@ class Fast_CrossEntropyLoss(torch.autograd.Function):
else:
# For large vocabs > 65336 like Gemma 256K
BLOCK_SIZE, num_warps = _chunked_forward_config(device, vocab_size)
- div, mod = divmod(vocab_size, BLOCK_SIZE)
- n_chunks = div + (mod != 0)
+ n_chunks = (vocab_size + BLOCK_SIZE - 1) // BLOCK_SIZE
logsumexp = torch.empty(
(
n_rows,
@@ -499,10 +498,7 @@ class Fast_CrossEntropyLoss(torch.autograd.Function):
dlogits = torch.empty_like(logits)
BLOCK_SIZE: int = 4096
- div: int
- mod: int
- div, mod = divmod(vocab_size, BLOCK_SIZE)
- n_blocks: int = div + (mod != 0)
+ n_blocks: int = (vocab_size + BLOCK_SIZE - 1) // BLOCK_SIZE
with torch_gpu_device(dlosses.device):
_cross_entropy_backward[