Cast batch token ids to int64 in the data loader#540
Merged
Conversation
Token ids are stored in the narrowest integer type that fits the vocab (int16 for vocab < 2**15 via get_unsigned_integer_type), but torch.embedding and the cross-entropy loss require int32/int64 indices. Any dataset with a sub-32768 vocab therefore crashed on the first forward step with "Expected ... Long, Int; but got torch.cuda.ShortTensor". examples/mistral.yaml triggers it directly (vocab 32000). TokenBatch._get_model_input's meta branch already declares tokens as int64, but the real collation path kept the dataset's narrow dtype. Normalize at the single chokepoint, TokenBatch.from_documents, so both token_ids and the derived labels honor the int64 contract. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Authored by Claude Opus 4.8 (1M context), at Joel's direction.
Problem
get_unsigned_integer_typestores token ids in the narrowest integer type that fits the vocab —int16for any vocab< 2**15.torch.embeddingand the cross-entropy loss only acceptint32/int64indices, so training a model whose vocab is below 32768 crashes on the first forward step:examples/mistral.yaml(vocab 32000,type: randomdata) triggers it directly. It is not random-data-specific — any dataset with a sub-32768 vocab hits it.Root cause
Token ids flow
dataset → TokenDocument.tokens → TokenBatch → token_ids kwarg / labelswith no dtype normalization.TokenBatch._get_model_input's meta branch already declarestokensasint64, but the real (non-meta) collation path kept the dataset's narrow dtype — so the real data wasn't honoring the int64 contract the meta path already assumes.Fix
Normalize at the single collation chokepoint,
TokenBatch.from_documents, so bothtoken_idsand the derivedlabels(a clone of the batch tokens) areint64:Testing
Ran
examples/mistral.yaml(Mistral-7B,type: random) for 10 steps on 8 GPUs: trains cleanly (lm_head_loss≈ 10.87 ≈ ln(32000), grad norm ~4.0, no NaN/skipped iterations). Without the fix it crashes on step 1.Separately, the same run surfaced what looks like a pre-existing, unrelated issue — the throughput line reports negative model FLOP/s for this config. Not addressed here; happy to file it separately.
🤖 Generated with Claude Code