pytorch - ✅(Solved) Fix torch.compile wraps IndexError as non-IndexError exceptions, breaking except IndexError handlers [1 pull requests, 2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#184340Fetched 2026-05-20 03:39:12
View on GitHub
Comments
2
Participants
3
Timeline
17
Reactions
0
Timeline (top)
subscribed ×6mentioned ×5commented ×2cross-referenced ×2

Error Message

import torch

device = "cuda" if torch.cuda.is_available() else "cpu" x = torch.randn(4, device=device)

print("torch:", torch.version) print("device:", device) if torch.cuda.is_available(): print("gpu:", torch.cuda.get_device_name(0))

def f(t): return torch.softmax(t, dim=10)

try: f(x) except IndexError as e: print("eager caught IndexError:", e)

compiled_f = torch.compile(f)

try: compiled_f(x) except IndexError as e: print("compile caught IndexError:", e) except Exception as e: print("compile was not caught by IndexError") print("type:", type(e).name) print("isinstance(e, IndexError):", isinstance(e, IndexError)) print(str(e).splitlines()[0])

Root Cause

This can break real user fallback or validation logic.

For example:

import torch

def safe_softmax(x, dim):
    try:
        return torch.softmax(x, dim=dim)
    except IndexError:
        return torch.softmax(x, dim=-1)

This works in eager mode, but fails after compiling if the invalid-dimension error escapes as BackendCompilerFailed, TorchRuntimeError, or InternalTorchDynamoError instead of IndexError.

Fix Action

Fixed

PR fix notes

PR #184452: Raise IndexError compile mode as Eager mode

Description (problem / solution / changelog)

fixes #184340 Identified various places where this mismatch is happening or indexError is not identified. Small fix with some tests.

Authored by Claude Sonnet 4.6

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

Changed files

  • test/dynamo/test_exception_contract.py (added, +54/-0)
  • torch/_dynamo/convert_frame.py (modified, +1/-0)
  • torch/_dynamo/output_graph.py (modified, +1/-1)
  • torch/_dynamo/utils.py (modified, +2/-0)
  • torch/_meta_registrations.py (modified, +3/-1)

Code Example

import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.randn(4, device=device)

print("torch:", torch.__version__)
print("device:", device)
if torch.cuda.is_available():
    print("gpu:", torch.cuda.get_device_name(0))

def f(t):
    return torch.softmax(t, dim=10)

try:
    f(x)
except IndexError as e:
    print("eager caught IndexError:", e)

compiled_f = torch.compile(f)

try:
    compiled_f(x)
except IndexError as e:
    print("compile caught IndexError:", e)
except Exception as e:
    print("compile was not caught by IndexError")
    print("type:", type(e).__name__)
    print("isinstance(e, IndexError):", isinstance(e, IndexError))
    print(str(e).splitlines()[0])

---

torch: 2.10.0+cu128
device: cuda
gpu: Tesla T4

eager caught IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 10)
compile was not caught by IndexError
type: BackendCompilerFailed
isinstance(e, IndexError): False
backend='inductor' raised:

---

softmax(dim=10): IndexError -> BackendCompilerFailed
tensor.size(-99): IndexError -> InternalTorchDynamoError
squeeze(99): IndexError -> TorchRuntimeError
unsqueeze(99): IndexError -> TorchRuntimeError
torch.amax(dim=99): IndexError -> TorchRuntimeError
torch.argmax(dim=99): IndexError -> TorchRuntimeError
torch.flip(dims=[99]): IndexError -> TorchRuntimeError
torch.cat(dim=99): IndexError -> TorchRuntimeError
torch.stack(dim=99): IndexError -> TorchRuntimeError

---

contract_failures: 9
contract_passes: 0
errors_or_timeouts: 0

---

isinstance(compiled_exception, IndexError) == False

---

Python: 3.12.13
PyTorch: 2.10.0+cu128
CUDA: 12.8
GPU: Tesla T4
Result: 9 contract failures, 0 errors/timeouts

---

PyTorch: 2.9.1+cu128
CUDA: 12.8
GPU: NVIDIA L4
Result: same contract pattern across the same 9 cases

---

import torch

def safe_softmax(x, dim):
    try:
        return torch.softmax(x, dim=dim)
    except IndexError:
        return torch.softmax(x, dim=-1)
RAW_BUFFERClick to expand / collapse

Describe the bug

torch.compile changes several eager-mode IndexError failures into non-IndexError wrapper exceptions such as BackendCompilerFailed, TorchRuntimeError, or InternalTorchDynamoError.

This breaks normal Python control flow. Code that correctly handles invalid dimension/index errors with except IndexError in eager mode does not catch the compiled-mode failure.

The underlying failure is still an invalid-dimension/index error, and in several cases the inner error is still an IndexError, but the exception escaping from torch.compile is not an IndexError subclass.

Minimal repro

import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
x = torch.randn(4, device=device)

print("torch:", torch.__version__)
print("device:", device)
if torch.cuda.is_available():
    print("gpu:", torch.cuda.get_device_name(0))

def f(t):
    return torch.softmax(t, dim=10)

try:
    f(x)
except IndexError as e:
    print("eager caught IndexError:", e)

compiled_f = torch.compile(f)

try:
    compiled_f(x)
except IndexError as e:
    print("compile caught IndexError:", e)
except Exception as e:
    print("compile was not caught by IndexError")
    print("type:", type(e).__name__)
    print("isinstance(e, IndexError):", isinstance(e, IndexError))
    print(str(e).splitlines()[0])

Actual behavior

On CUDA, eager mode raises and catches IndexError, but the compiled path leaks a non-IndexError wrapper exception.

Observed output on Colab T4:

torch: 2.10.0+cu128
device: cuda
gpu: Tesla T4

eager caught IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 10)
compile was not caught by IndexError
type: BackendCompilerFailed
isinstance(e, IndexError): False
backend='inductor' raised:

Additional confirmed cases

I reproduced the same exception-contract break across 9 out-of-range dimension/index cases:

softmax(dim=10): IndexError -> BackendCompilerFailed
tensor.size(-99): IndexError -> InternalTorchDynamoError
squeeze(99): IndexError -> TorchRuntimeError
unsqueeze(99): IndexError -> TorchRuntimeError
torch.amax(dim=99): IndexError -> TorchRuntimeError
torch.argmax(dim=99): IndexError -> TorchRuntimeError
torch.flip(dims=[99]): IndexError -> TorchRuntimeError
torch.cat(dim=99): IndexError -> TorchRuntimeError
torch.stack(dim=99): IndexError -> TorchRuntimeError

Batch summary:

contract_failures: 9
contract_passes: 0
errors_or_timeouts: 0

Each failure means:

isinstance(compiled_exception, IndexError) == False

even though eager mode raised IndexError.

Reproduced environments

Primary reproduction:

Python: 3.12.13
PyTorch: 2.10.0+cu128
CUDA: 12.8
GPU: Tesla T4
Result: 9 contract failures, 0 errors/timeouts

Earlier fuzzing run:

PyTorch: 2.9.1+cu128
CUDA: 12.8
GPU: NVIDIA L4
Result: same contract pattern across the same 9 cases

So this does not appear to be specific to one GPU model or one PyTorch build. The failure seems to happen in the torch.compile / Dynamo / FakeTensor / Inductor path before an actual CUDA kernel needs to run.

Expected behavior

torch.compile should preserve the eager exception contract for user-visible input errors.

For these cases, if eager mode raises IndexError, compiled mode should raise either:

  • IndexError, or
  • an exception subclass that is still catchable by except IndexError.

Ideally, user code that handles IndexError in eager mode should not stop working after enabling torch.compile.

Why this matters

This can break real user fallback or validation logic.

For example:

import torch

def safe_softmax(x, dim):
    try:
        return torch.softmax(x, dim=dim)
    except IndexError:
        return torch.softmax(x, dim=-1)

This works in eager mode, but fails after compiling if the invalid-dimension error escapes as BackendCompilerFailed, TorchRuntimeError, or InternalTorchDynamoError instead of IndexError.

Related issues / prior art

I found related torch.compile / internal IndexError reports, but I did not find an exact duplicate for this user-visible exception-contract issue.

cc @chauhang @penguinwu

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

torch.compile should preserve the eager exception contract for user-visible input errors.

For these cases, if eager mode raises IndexError, compiled mode should raise either:

  • IndexError, or
  • an exception subclass that is still catchable by except IndexError.

Ideally, user code that handles IndexError in eager mode should not stop working after enabling torch.compile.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING