nn.Embedding Out of Bounds Illegal Memory Access
An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error.
An index passed to a PyTorch `nn.
- Symptom
RuntimeError: CUDA error: an illegal memory access was encountered- Root cause
- An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g.
- Recommended fix
- Clamp indices or fix the data pipeline input_ids = torch.clamp(input_ids, min=0, max=vocab_size - 1) Ensures all token IDs are within the valid range of the embedding matrix, preventing out-of-bounds memory access.
- How Denpex helps
- Denpex matches nn.Embedding Out of Bounds Illegal Memory Access across every rank in a distributed run and reports which rank failed first, so you act on the initiating node instead of the loudest one.
What this failure is
nn.Embedding Out of Bounds Illegal Memory Access is a Memory failure seen during ML training runs. An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error. Common tags: Out Of Bounds Access.
Is this what broke your run? Paste your log.
You're reading about nn.Embedding Out of Bounds Illegal Memory Access. Paste your own crash log or traceback below and get the real root cause for YOUR run, not this generic entry. No account, no card. Logs are masked at ingress and never saved to account history.
Want 14 days on the Scale plan?
Request an evaluation code. A verified workplace organization activates up to 50 diagnoses a day, alerts, history, and follow-up questions. No credit card or automatic subscription.
Why it happens (the mechanism)
The stack trace almost never points to the embedding layer itself unless `CUDA_LAUNCH_BLOCKING=1` is used. Users typically waste hours debugging the layer where the crash surfaced, assuming a hardware failure or driver bug.
What you'll observe
- RuntimeError: CUDA error: an illegal memory access was encountered
- block: [0,0,0], thread: [32,0,0] Assertion `index >= -sizes[i] && index < sizes[i]` failed
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| The training script crashes suddenly without warning, often pointing to an arbitrary line of code (like a loss function or forward pass of a different layer). | An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error. |
| When running in a multi-GPU setup, it might trigger an NCCL timeout because one GPU goes down. | An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error. |
| The exact stack trace points to operations downstream of the embedding layer due to asynchronous CUDA execution. | An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error. |
Which systems are affected
- PyTorch
- CUDA
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Run the script with `CUDA_LAUNCH_BLOCKING=1 python train.py` to get the synchronous stack trace.
- ✓Run with `TORCH_USE_CUDA_DSA=1` to enable device-side assertions for better error messages.
- ✓Add a check `assert inputs.max() < embedding_layer.num_embeddings` right before the embedding lookup.
Searchable error signature
RuntimeError: CUDA error: an illegal memory access was encounteredUse this text as a lookup key in logs and upstream issue trackers. It is not presented as a captured customer log. Confirm the cause from your own preceding events, versions, configuration and the cited references.
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionRoot cause
- An index passed to a PyTorch `nn.Embedding` layer exceeds the configured `num_embeddings` (e.g., passing token ID 1000 to an embedding matrix of size 1000, where max valid is 999). Because CUDA executes asynchronously, the out-of-bounds kernel memory violation doesn't throw a Python exception immediately. Instead, the GPU's memory protection fault corrupts the CUDA context, causing a subsequent operation to raise the generic 'illegal memory access' error.
The fix and how to prevent it
Evaluate Denpex on your own logs
Request a Scale evaluation code. A verified workplace organization activates 14 days with up to 50 diagnoses a day. Every account keeps its current diagnosis allowance and gets a verification path. No card or automatic subscription.
Don't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.