Troubleshooting

CUDA device-side assert triggered, explained

CUDA device-side assert triggered means a check inside a GPU kernel failed, almost always because an index was out of range: a class label outside [0, C) for cross-entropy, a token ID beyond the embedding size, or a BCE input outside 0 to 1. Rerun with CUDA_LAUNCH_BLOCKING=1 to get the true failing line, check label and index ranges on the CPU, then restart the process.

A conveyor of data blocks entering a GPU, stopped by a tripped red gate where one oversized block jammed
A device-side assert is a kernel’s range check tripping on one bad value, usually a label or index. AI-generated illustration.

The exact error message

In PyTorch the failure usually appears as RuntimeError: CUDA error: device-side assert triggered, followed by two lines from PyTorch itself: CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Many builds add a line about compiling with TORCH_USE_CUDA_DSA.

Above the Python traceback, the terminal often shows the assertion that actually failed, printed by the kernel. Lines such as Assertion `t >= 0 && t < n_classes` failed or Assertion `srcIndex < srcSelectDimSize` failed are the most useful part of the output. Scroll up and read them first.

After this error the process is finished with the GPU. NVIDIA’s runtime documentation for cudaErrorAssert says the device cannot be used again, all existing allocations are invalid, and the process must be terminated and relaunched to continue using CUDA. Catching the exception and carrying on will not work.

References: PyTorch source: c10/cuda/CUDAMiscFunctions.cpp · CUDA Runtime API: error types (cudaErrorAssert)

What a device-side assert means

PyTorch’s CUDA kernels contain assertions that check their inputs while they run on the GPU. The negative log-likelihood loss behind cross-entropy asserts that each target is at least 0 and below the number of classes. Binary cross-entropy asserts that inputs and targets lie between 0 and 1. Index selection, which embedding lookups use, asserts that each index is below the size of the dimension it selects from.

When one of those checks fails, the kernel stops and CUDA records the error. Because GPU work is asynchronous by default, as PyTorch’s CUDA notes explain, Python does not hear about it until a later call synchronizes. That is why the traceback often points at an innocent line such as loss.item() or a print.

So the message tells you two things: your data broke a rule a kernel enforces, and the line in the traceback is probably not where it happened.

References: PyTorch source: aten/src/ATen/native/cuda/Loss.cu · PyTorch source: aten/src/ATen/native/cuda/Indexing.cu · PyTorch CUDA semantics: asynchronous execution

Causes of CUDA device-side assert triggered, most common first

First, class labels out of range. CrossEntropyLoss expects class indices between [0, C), where C is the number of classes. Labels numbered 1 to C instead of 0 to C-1, a stray -1 used for padding when ignore_index is the default -100, or a classifier head with fewer outputs than the dataset has classes all trigger it.

Second, token or category IDs beyond an embedding table. A tokenizer with added special tokens produces IDs larger than the model’s embedding size, or a categorical feature has a value never seen when the table was sized. Third, BCELoss given raw logits or targets outside 0 to 1. Fourth, hand-written indexing, gather or scatter with an index computed from data.

Rarer causes are bugs in custom CUDA extensions and, very rarely, hardware. NVIDIA’s Xid catalog says Xid 13 is typically an out-of-bounds application error but can occasionally reflect a hardware or driver fault.

Causes of CUDA device-side assert triggered, most common first
RankCauseAssertion you may see
1Class label outside [0, C)t >= 0 && t < n_classes
2Token or category ID beyond embedding sizesrcIndex < srcSelectDimSize
3BCE input or target outside 0 to 1input_val >= zero && input_val <= one
4Bad index in gather, scatter or indexingindex out of bounds
5Custom kernel bug or hardware faultVaries; check the kernel log

References: PyTorch torch.nn.CrossEntropyLoss · PyTorch source: aten/src/ATen/native/cuda/Loss.cu · PyTorch source: aten/src/ATen/native/cuda/Indexing.cu · NVIDIA: analyzing Xid errors with the Xid catalog

Diagnostic commands that find the real line

Make the error synchronous. Run CUDA_LAUNCH_BLOCKING=1 python train.py. PyTorch’s CUDA notes say this forces synchronous computation, which is handy when an error occurs on the GPU, so the traceback lands on the operation that failed. It slows everything down, so use it only to debug.

Check ranges before the GPU sees the data. For a classifier, print labels.min(), labels.max() and the number of model outputs. For a language model, compare input_ids.max() with the embedding’s num_embeddings. For BCE, confirm inputs come from a sigmoid or switch to BCEWithLogitsLoss.

Reproduce on the CPU. Move the model and the failing batch to the CPU and run the forward pass; an out-of-range index raises an ordinary Python error there, with a readable message. For custom kernels, run compute-sanitizer --tool memcheck python train.py; memcheck is the default tool and reports the access that went wrong.

References: PyTorch CUDA semantics: asynchronous execution · PyTorch torch.nn.CrossEntropyLoss · NVIDIA Compute Sanitizer user manual

An inspector measuring blocks against a fixed-width gauge before they reach a GPU conveyor
Checking label and index ranges on the CPU before training catches the bad value early. AI-generated illustration.

Symptom, check and fix table

Restart the Python process between attempts. After a device-side assert, every later CUDA call in that process fails.

Symptom, check and fix table
SymptomCheckFix
Fails on the first batch with cross-entropylabels.min(), labels.max() vs output sizeRemap labels to 0..C-1; match the head to C
Fails only on some batchesFind the batch; check its labels and IDsClean the outlier samples; validate at load time
Fails after adding special tokensinput_ids.max() vs num_embeddingsResize the token embeddings to the tokenizer
Fails with BCELossAre inputs probabilities?Apply sigmoid or use BCEWithLogitsLoss
Padding labels break the lossPadding value vs ignore_indexUse -100 or set ignore_index to your pad value
Every CUDA call fails after the first errorSame process still running?Terminate and relaunch the process

References: PyTorch torch.nn.CrossEntropyLoss · PyTorch source: aten/src/ATen/native/cuda/Loss.cu · PyTorch source: aten/src/ATen/native/cuda/Indexing.cu · CUDA Runtime API: error types (cudaErrorAssert)

Fixes that stop it coming back

Validate data on the CPU, once, before training. Assert that labels fall in [0, C), that the maximum token ID is below the embedding size, and that BCE targets are between 0 and 1. A check over the whole dataset takes seconds and saves hours on a GPU.

Keep model shape and data in sync. Derive the number of output classes from the dataset rather than hard-coding it. If you add tokens to a Hugging Face tokenizer, resize the model’s token embeddings to match before training. Set ignore_index deliberately when you pad labels, since the default is -100, not -1 or 0.

Prefer numerically safe losses. BCEWithLogitsLoss takes raw logits, so you never feed out-of-range values to the BCE kernel. When an error does appear, checkpoint often enough that a restart costs little; GPU training checkpoints covers the schedule.

References: PyTorch torch.nn.CrossEntropyLoss

When it is not your code, and what to ask a provider

If the data checks pass, the same code runs on another GPU, and the CPU reproduction is clean, look at the host. On Linux, grep the kernel log for NVRM: Xid. Repeated Xid 13 or Xid 31 on one GPU across different jobs is a pattern NVIDIA’s catalog routes to hardware diagnostics, which NVIDIA Xid errors explains.

On a rented GPU you may not see the host log. Ask the provider whether they can share Xid events for your GPU during the failure window, whether they run diagnostics before reassigning a GPU, and whether they will move you to another node if the same GPU keeps faulting. Different errors on the same card, such as CUDA out of memory, are usually unrelated and need their own checks.

References: NVIDIA: analyzing Xid errors with the Xid catalog · NVIDIA: working with Xid errors

Checklist and next step

Read the kernel assertion line above the traceback. Rerun with CUDA_LAUNCH_BLOCKING=1. Check label ranges against the number of classes and token IDs against the embedding size. Reproduce on the CPU. Validate the dataset before training. Restart the process after every device-side assert. Check the kernel log only when the code and data are clean.

Once the job runs cleanly, NeuraMesh rents GPUs for training by the hour: choose a GPU, plan, count, hours and region on rent GPUs and you receive a confirmed quotation, typically within one business day, with nothing charged until you approve. If you are unsure which GPU your model needs, ask the compute advisor.

NeuraMesh prices for these GPUs

NeuraMesh prices per GPU-hour in USD, excluding taxes, as of 2026-11-27. Live prices are on the rental pages.
GPUMemoryData centerValue
RTX 409024 GB$0.46$0.40
L40S48 GB$1.26$0.54
A10080 GB$1.56$0.98
H10080 GB$2.88$2.00

Every order is confirmed in a quotation before you pay. See all GPU prices.

Questions readers ask

What does CUDA device-side assert triggered mean?

A check inside a GPU kernel failed. In PyTorch this is almost always an out-of-range value: a class label outside [0, C), a token ID beyond the embedding size, or a BCE input outside 0 to 1. The kernel stops and the CUDA context becomes unusable.

Why does the traceback point to the wrong line?

CUDA work is asynchronous, so the error surfaces at the next call that synchronizes, such as .item(). Run with CUDA_LAUNCH_BLOCKING=1 to force synchronous execution, and the traceback lands on the failing operation.

Can I recover from a device-side assert without restarting?

No. NVIDIA’s documentation for cudaErrorAssert says the device cannot be used again and existing allocations are invalid; the process must be terminated and relaunched. In a notebook, restart the kernel.

How do I fix a device-side assert with CrossEntropyLoss?

Make every target an integer from 0 to C-1, where C is the model’s number of outputs. Labels starting at 1 or padding values other than ignore_index, which defaults to -100, are the usual culprits.

Could a faulty GPU cause a device-side assert?

Rarely. Check data and code first. If the same GPU logs repeated Xid 13 or Xid 31 errors across different jobs while other GPUs run the same code cleanly, report it to the operator for hardware diagnostics.

Sources and editorial notes

Primary documentation reviewed November 24, 2026. Provider descriptions reflect published documentation. Recommendations are our analysis; worked figures are illustrative unless explicitly stated otherwise. No independent provider benchmark or uptime ranking is claimed.

Read the editorial policy.

Need this GPU? Rent it from NeuraMesh: choose a GPU and plan, get a confirmed quotation, and pay only when you approve. See pricing.