pytorch - ✅(Solved) Fix torch.compile skips out-of-range negative index check in F.embedding [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#183608Fetched 2026-05-14 03:28:07
View on GitHub
Comments
2
Participants
3
Timeline
80
Reactions
0
Timeline (top)
mentioned ×35subscribed ×35labeled ×6commented ×2

Error Message

import torch

w = torch.tensor([ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0], [70.0, 80.0], [90.0, 100.0], ])

idx = torch.tensor([2, -2, 4])

def fn(i, w): return torch.nn.functional.embedding(i, w)

torch._dynamo.reset()

eager_err = False

try: eager = fn(idx, w) print("eager:", eager.tolist()) except Exception as ex: eager_err = True print("eager err:", type(ex).name, str(ex)[:80])

torch._dynamo.reset()

compiled = torch.compile(fn, backend="inductor", fullgraph=True)(idx, w)

print("compile:", compiled.tolist())

assert eager_err

Fix Action

Fixed

PR fix notes

PR #183636: Fix Inductor embedding negative index checks

Description (problem / solution / changelog)

Stack from ghstack (oldest at bottom):

  • -> #183636

Make the Inductor embedding lowering preserve index_select semantics by disabling negative-index wrapping for embedding row lookups. This lets the generated bounds check reject invalid negative and positive indices instead of returning wrapped rows.

Fixes #183608

Generated by my agent

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

Changed files

  • test/inductor/test_torchinductor.py (modified, +28/-0)
  • torch/_inductor/lowering.py (modified, +3/-3)

Code Example

import torch

w = torch.tensor([
    [10.0, 20.0],
    [30.0, 40.0],
    [50.0, 60.0],
    [70.0, 80.0],
    [90.0, 100.0],
])

idx = torch.tensor([2, -2, 4])

def fn(i, w):
    return torch.nn.functional.embedding(i, w)

torch._dynamo.reset()

eager_err = False

try:
    eager = fn(idx, w)
    print("eager:", eager.tolist())
except Exception as ex:
    eager_err = True
    print("eager err:", type(ex).__name__, str(ex)[:80])

torch._dynamo.reset()

compiled = torch.compile(fn, backend="inductor", fullgraph=True)(idx, w)

print("compile:", compiled.tolist())

assert eager_err
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.compile with the Inductor backend produces a result for torch.nn.functional.embedding with an out-of-range negative index, while eager execution correctly raises an IndexError.

For the input index -2, eager F.embedding raises: IndexError: index out of range in self

However, the compiled version returns a valid row from the embedding table instead of raising an error.

import torch

w = torch.tensor([
    [10.0, 20.0],
    [30.0, 40.0],
    [50.0, 60.0],
    [70.0, 80.0],
    [90.0, 100.0],
])

idx = torch.tensor([2, -2, 4])

def fn(i, w):
    return torch.nn.functional.embedding(i, w)

torch._dynamo.reset()

eager_err = False

try:
    eager = fn(idx, w)
    print("eager:", eager.tolist())
except Exception as ex:
    eager_err = True
    print("eager err:", type(ex).__name__, str(ex)[:80])

torch._dynamo.reset()

compiled = torch.compile(fn, backend="inductor", fullgraph=True)(idx, w)

print("compile:", compiled.tolist())

assert eager_err

Error logs

eager err: IndexError index out of range in self compile: [[50.0, 60.0], [70.0, 80.0], [90.0, 100.0]]

Versions

PyTorch version: 2.11.0+cu130 Is debug build: False CUDA used to build PyTorch: 13.0 ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.2 LTS (x86_64) GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 Clang version: Could not collect CMake version: Could not collect Libc version: glibc-2.39

Python version: 3.10.20 (main, Mar 11 2026, 17:46:40) [GCC 14.3.0] (64-bit runtime) Python platform: Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.39 Is CUDA available: False CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: N/A GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3080 Laptop GPU Nvidia driver version: 545.92 cuDNN version: Could not collect Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

Versions of relevant libraries: [pip3] numpy==2.2.6 [pip3] onnx==1.21.0 [pip3] onnx2torch==1.5.15 [pip3] onnxruntime==1.23.2 [pip3] torch==2.11.0 [pip3] torchvision==0.26.0 [pip3] triton==3.6.0

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

pytorch - ✅(Solved) Fix torch.compile skips out-of-range negative index check in F.embedding [1 pull requests, 2 comments, 3 participants]