pytorch - 💡(How to fix) Fix `torch.compile` crashes on `flip()` applied to 0-dimensional tensors

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…

Error Message

InductorError: IndexError: list index out of range

Fix Action

Workaround

# Unsqueeze before flip, squeeze after:
x.sum().unsqueeze(0).flip(0).squeeze(0)  # OK

Code Example

import torch

x = torch.tensor(3.14, device="cuda")

# Eager: works (flip on scalar is a no-op)
print(x.flip(0))  # tensor(3.14)

# Compiled: crashes
torch.compile(lambda x: x.flip(0), backend="inductor")(x)

---

InductorError: IndexError: list index out of range

---

def fn(x):
    result = x.sum()       # → scalar (0-d tensor)
    return result.flip(0)  # valid in eager, crashes compiled

x = torch.randn(4, 8, device="cuda", requires_grad=True)
fn(x).backward()                                            # Eager: OK
torch.compile(fn, backend="inductor")(x).backward()         # CRASH

---

x.flip(0)           # CRASH (x is 0-d)
x.flip([0])         # CRASH
torch.flip(x, [0])  # CRASH

---

x.flip([])          # OK (empty dims list, no flip needed)
x.flip(0)           # OK when x.ndim >= 1

---

# Unsqueeze before flip, squeeze after:
x.sum().unsqueeze(0).flip(0).squeeze(0)  # OK
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

🐛 Describe the bug

torch.compile(backend="inductor") crashes with IndexError: list index out of range when flip() is applied to a 0-dimensional (scalar) tensor. In eager mode, flip() on a scalar is a valid no-op (there are no elements along any dimension to reverse).

The bug is in Inductor's lowering/codegen for flip — it indexes into the tensor's dimension list without handling the ndim=0 case.

Minimal reproducer

import torch

x = torch.tensor(3.14, device="cuda")

# Eager: works (flip on scalar is a no-op)
print(x.flip(0))  # tensor(3.14)

# Compiled: crashes
torch.compile(lambda x: x.flip(0), backend="inductor")(x)

Error message

InductorError: IndexError: list index out of range

Backend isolation

BackendResult
eager✅ works
aot_eager✅ works
inductorcrashes

The bug is Inductor-specific (codegen/lowering), not in AOT Autograd.

Practical scenario

This pattern can appear when a reduction produces a scalar and subsequent operations are applied generically:

def fn(x):
    result = x.sum()       # → scalar (0-d tensor)
    return result.flip(0)  # valid in eager, crashes compiled

x = torch.randn(4, 8, device="cuda", requires_grad=True)
fn(x).backward()                                            # Eager: OK
torch.compile(fn, backend="inductor")(x).backward()         # CRASH

Affected variants

x.flip(0)           # CRASH (x is 0-d)
x.flip([0])         # CRASH
torch.flip(x, [0])  # CRASH

Non-triggering

x.flip([])          # OK (empty dims list, no flip needed)
x.flip(0)           # OK when x.ndim >= 1

Workaround

# Unsqueeze before flip, squeeze after:
x.sum().unsqueeze(0).flip(0).squeeze(0)  # OK

Versions

Versions

  • PyTorch: 2.13.0.dev20260513+cu126
  • Python: 3.11
  • CUDA: 12.6
  • GPU: Tesla T4

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING