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 investigates nn.Embedding Out of Bounds Illegal Memory Access using the evidence you provide or your connected workload collects. Earlier rank, host or application evidence is needed to distinguish an initiating failure from a downstream report.
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 traceback and relevant evidence for an investigation of your workload, with a next action or a specific missing fact. A reference entry does not establish your cause. No account or card for the free diagnosis. Review data handling before submitting sensitive logs.
Before uploading, review cloud data handling and local options.
Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.
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.
Root 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
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 extensionDon'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.