PyTorch A100 MIG RuntimeError: Invalid Device String
Fix PyTorch RuntimeError: Invalid device string on NVIDIA A100 and H100 MIG instances. Set CUDA_VISIBLE_DEVICES to the MIG UUID and use cuda:0 in Python.
PyTorch torch.device() only accepts integer CUDA ordinals like cuda:0, not a MIG UUID string (MIG-...). Set CUDA_VISIBLE_DEVICES=MIG-<uuid> in the shell or pod environment before Python imports torch, then target cuda:0 inside PyTorch.
- Symptom
RuntimeError: Invalid device string: 'MIG-00000000-0000-0000-0000-000000000000'- Root cause
- A MIG UUID string was passed to torch.device() instead of exporting it via CUDA_VISIBLE_DEVICES before CUDA initialization.
- Recommended fix
CUDA_VISIBLE_DEVICES=<MIG-UUID>- How Denpex helps
- Denpex investigates PyTorch A100 MIG RuntimeError: Invalid Device String 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
A PyTorch RuntimeError raised when a MIG instance UUID or unsupported string is passed directly to torch.device() or .to().
Is this what broke your run? Paste your log.
You're reading about PyTorch A100 MIG RuntimeError: Invalid Device String. 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)
NVIDIA Multi-Instance GPU (MIG) partitions expose UUID identifiers like MIG-8a... in nvidia-smi -L and Kubernetes, whereas PyTorch parses device strings expecting cuda or cuda:<int>.
What you'll observe
- Training or inference scripts crash immediately on MIG nodes with RuntimeError: Invalid device string.
- Passing multiple MIG slices into one CUDA process also fails because CUDA restricts each process to a single MIG compute instance.
Common symptoms and what they mean
Check possible explanations against the incident evidence. A matching symptom alone does not establish its cause.
| Symptom | Possible explanation |
|---|---|
| RuntimeError: Invalid device string: 'MIG-...' | A MIG UUID string was passed to torch.device() instead of exporting it via CUDA_VISIBLE_DEVICES before CUDA initialization. |
| torch.cuda.device_count() returns 0 or 1 unexpected device on a MIG-partitioned A100/H100 | A MIG UUID string was passed to torch.device() instead of exporting it via CUDA_VISIBLE_DEVICES before CUDA initialization. |
Which systems are affected
- NVIDIA A100, H100, and H200 GPUs partitioned with Multi-Instance GPU (MIG)
- Kubernetes NVIDIA GPU Operator and Slurm GRES MIG allocations
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 CUDA_VISIBLE_DEVICES=<MIG-UUID> python3 -c "import torch; print(torch.cuda.device_count(), torch.cuda.get_device_name(0))".
- ✓Verify torch.cuda.get_device_name(0) matches the expected MIG profile (for example 3g.40gb).
Root cause
- A MIG UUID string was passed to torch.device() instead of exporting it via CUDA_VISIBLE_DEVICES before CUDA initialization.
The fix and how to prevent it
How to fix it
- List available MIG UUIDs with nvidia-smi -L and export CUDA_VISIBLE_DEVICES=<MIG-UUID> before launching Python.
- Inside Python, reference the exposed slice as torch.device('cuda:0') rather than passing the MIG string to torch.device().
- Ensure each process sees at most one MIG instance unless using separate processes per MIG slice, as NVIDIA CUDA allows only one MIG device per process.
Step-by-step remediation
- 1Run nvidia-smi -L on the host to copy the exact MIG-... identifier.
- 2Export CUDA_VISIBLE_DEVICES with a single MIG UUID before starting Python.
- 3Pass cuda:0 instead of the MIG string to torch.device() and verify get_device_name(0).
How to prevent it
- Have Slurm or Kubernetes GPU device plugins inject CUDA_VISIBLE_DEVICES automatically and standardize code on cuda:0 / LOCAL_RANK.
- Avoid passing raw device UUIDs directly into torch.device().
Save this diagnosis and catch the next crash automatically
All root cause, fix, and prevention steps above are free to read. Create a free account with any email or GitHub to keep your saved diagnosis history across the encyclopedia and run cloud checks, or use a work email below for the 30-day Team evaluation. No card required.
Or start a 30-day free Team trial (work email)
Verified work email unlocks all 14 fleet diagnostic tools, Slack alerts, and team history for 30 days. No card and no automatic charge.
Searchable error signature
RuntimeError: Invalid device string: 'MIG-00000000-0000-0000-0000-000000000000'Use 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.
Diagnose your own log automatically
The root cause and fix steps above stay free. Create a free account to analyze full job logs, attribute the first failed rank, and keep saved incident history.
Sign up free. Analyze your cluster logsNo credit card. Daily allowance follows verified trust tier. Instant access.
Why the recommended fix works
Letting the CUDA driver map the MIG UUID via CUDA_VISIBLE_DEVICES exposes a clean ordinal cuda:0 device that PyTorch device parsing supports natively.
Code examples
nvidia-smi -L
CUDA_VISIBLE_DEVICES=MIG-00000000-0000-0000-0000-000000000001 python3 -c "import torch; print(torch.device('cuda:0'), torch.cuda.get_device_name(0))"This snippet illustrates the entry-specific check. Verify the installed versions, permissions, target and workload context before using it. A command or API example is not proof of recovery.
Best practices by model family
| Model / Stack | Recommendation | Notes |
|---|---|---|
| Shell / scheduler | Pass MIG UUID in CUDA_VISIBLE_DEVICES | Must be set before Python initializes CUDA. |
| PyTorch code | Use cuda:0 | Each process can enumerate and use a single MIG slice as device 0. |
With the fix vs without the fix
| Dimension | With the fix | Without the fix |
|---|---|---|
| Device binding | CUDA_VISIBLE_DEVICES=MIG-... + torch.device('cuda:0') | torch.device('MIG-...') |
| Multi-slice scaling | One process per MIG slice | Requesting 2 MIG slices in one Python process |
Diagnostic note
“Grounded in NVIDIA MIG user guide process limits and PyTorch torch.device specification.”
Visual fingerprint
nvidia-smi -L (get MIG UUID) -> export CUDA_VISIBLE_DEVICES=MIG-... -> Python uses torch.device('cuda:0')Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to inspect matched failure patterns, supporting evidence and next checks without uploading the log. Verify recommendations against your workload before changing production.
Install the free VS Code extensionPyTorch GPU errors in context
PyTorch training errors commonly originate at tensor device placement, DataLoader /dev/shm IPC, or checkpoint state restoration.
Compare related pytorch gpu errorsFrequently asked questions
Questions engineers and on-call staff commonly ask about this failure.
Can one PyTorch process use two A100 MIG slices at once?
Why does CUDA_VISIBLE_DEVICES work when torch.device('MIG-...') fails?
Don't just read the fix, diagnose your run
The encyclopedia explains documented failure patterns. Denpex investigates your logs, configuration and stack, shows the supporting evidence, and asks for missing details when needed.