Skip to content

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.

Quick answer

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.
Environment#pytorch#mig#a100#h100#cuda-visible-devices

What this failure is

A PyTorch RuntimeError raised when a MIG instance UUID or unsupported string is passed directly to torch.device() or .to().

Live diagnosis, no signup

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.

training_logs.txt
No log to hand? Try one:

Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.

3 free cloud diagnoses per day

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.

SymptomPossible 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/H100A 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

  1. 1Run nvidia-smi -L on the host to copy the exact MIG-... identifier.
  2. 2Export CUDA_VISIBLE_DEVICES with a single MIG UUID before starting Python.
  3. 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.

Use your company email, for example alex@yourcompany.com.

Who qualifies?

Company email domains activate the free 30-day Team evaluation (up to 50 diagnoses a day) after an email and domain check, whatever mail provider you use. If we cannot verify your domain automatically, support reviews it: write to support@denpex.com. Free local diagnosis in the editor extension needs no account.

Using a personal email (Gmail, Outlook, Naver, QQ) or solo cluster? Create a free $0 account (no work email required) to save 7-day history and unlock AI fallback, or send a cluster crash log for a live walkthrough.
Already have an account? Sign in

Searchable error signature

search key
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 logs

No 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

snippet
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 / StackRecommendationNotes
Shell / schedulerPass MIG UUID in CUDA_VISIBLE_DEVICESMust be set before Python initializes CUDA.
PyTorch codeUse cuda:0Each process can enumerate and use a single MIG slice as device 0.

With the fix vs without the fix

DimensionWith the fixWithout the fix
Device bindingCUDA_VISIBLE_DEVICES=MIG-... + torch.device('cuda:0')torch.device('MIG-...')
Multi-slice scalingOne process per MIG sliceRequesting 2 MIG slices in one Python process

Diagnostic note

“Grounded in NVIDIA MIG user guide process limits and PyTorch torch.device specification.”

Visual fingerprint

PyTorch MIG binding flow
nvidia-smi -L (get MIG UUID) -> export CUDA_VISIBLE_DEVICES=MIG-... -> Python uses torch.device('cuda:0')
Pass the MIG UUID in CUDA_VISIBLE_DEVICES outside Python so PyTorch sees a standard cuda:0 ordinal.

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 extension

PyTorch 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 errors

Frequently asked questions

Questions engineers and on-call staff commonly ask about this failure.

Can one PyTorch process use two A100 MIG slices at once?
No. NVIDIA CUDA restricts a single OS process to enumerating one MIG compute instance at a time; launch separate worker processes for separate MIG slices.
Why does CUDA_VISIBLE_DEVICES work when torch.device('MIG-...') fails?
The CUDA driver parses MIG UUIDs from CUDA_VISIBLE_DEVICES at runtime initialization and presents the chosen slice as ordinal 0 to PyTorch.

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.