microsoft / microsoft/onnxruntime

torch.onnx.export produces an ONNX graph with out-of-bounds Gather for torch.as_strided after slicing

Open
#28,342 1 comment 0 reactions 1 assignee View on GitHub

@justinchuby is already working on this.

Since Jun 3, 2026.

Dominant language
C++
Stars
21.9k
Forks
4.2k
Avg merge
4d 11h
Merged PRs (30d)
184

Description

### Describe the issue

`torch.onnx.export` appears to produce a semantically invalid ONNX graph for a valid PyTorch eager program involving `torch.as_strided` on a sliced tensor.

In PyTorch eager mode, the model runs successfully and returns a tensor with shape `(6, 4)`. However, after ONNX export, ONNX Runtime fails during execution with an out-of-bounds Gather error:

indices element out of data bounds, idx=6 must be within the inclusive range [-6,5]

This looks like an exporter semantic mismatch. The eager operation is valid because `torch.as_strided` constructs a view based on the underlying storage. However, the exported ONNX graph appears to gather from the sliced tensor of length 6, causing generated indices such as 6 to become out of bounds.

### To reproduce

#### Minimal reproduction
```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import os
import platform
import traceback

import onnx
import onnxruntime as ort
import torch
import torch.nn as nn

SEED = 0

class MyModel(nn.Module):
def forward(self, x):
sliced = x[0:6]
return torch.as_strided(sliced, size=(6, 4), stride=(1, 1))

def print_runtime_info() -> None:
info = {
"python": platform.python_version(),
"platform": platform.platform(),
"torch_version": torch.__version__,
"onnx_version": onnx.__version__,
"onnxruntime_version": ort.__version__,
"torch_cuda_available": torch.cuda.is_available(),
"torch_cuda_device_count": torch.cuda.device_count(),
"cuda_visible_devices": os.environ.get("CUDA_VISIBLE_DEVICES", ""),
"ort_available_providers": ort.get_available_providers(),
"seed": SEED,
}
print("[runtime]", json.dumps(info, indent=2, sort_keys=True))

def export_model(model: nn.Module, x: torch.Tensor, path: str) -> None:
os.makedirs(os.path.dirname(path), exist_ok=True)
model.eval()
with torch.no_grad():
torch.onnx.export(
model,
(x,),
path,
input_names=["input_0"],
output_names=["output"],
opset_version=18,
)

def main() -> int:
torch.manual_seed(SEED)
print_runtime_info()

model = MyModel().eval()
x = torch.rand(10, dtype=torch.float32)

with torch.no_grad():
eager = model(x)

print(f"[input] shape={tuple(x.shape)} dtype={x.dtype}")
print(f"[eager] shape={tuple(eager.shape)} dtype={eager.dtype}")
print("[eager] value=")
print(eager)

onnx_path = os.path.abspath("as_strided_after_slice.onnx")
export_model(model, x, onnx_path)
print(f"[export] onnx_path={onnx_path}")

try:
sess = ort.InferenceSession(onnx_path, providers=["CPUExecutionProvider"])
feed = {sess.get_inputs()[0].name: x.numpy()}
out = sess.run(None, feed)
print("[not_reproduced] unexpected success", [getattr(v, "shape", None) for v in out])
return 1
except Exception as exc:
print("[reproduced] exception_type=", type(exc).__name__)
print("[reproduced] exception=", repr(exc))
print(traceback.format_exc())
return 0

if __name__ == "__main__":
raise SystemExit(main())
```

#### Actual behavior
```
PyTorch eager execution succeeds:

[eager] shape=(6, 4)

ONNX export also succeeds:

[torch.onnx] Obtain model graph for `MyModel()` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decomposition... ✅
[torch.onnx] Translate the graph into ONNX... ✅

However, ONNX Runtime fails when executing the exported model:

[ONNXRuntimeError] : 2 : INVALID_ARGUMENT :
Non-zero status code returned while running Gather node. Name:'n12_2'
Status Message: indices element out of data bounds, idx=6 must be within the inclusive range [-6,5]
```

### Urgency

_No response_

### Platform

Linux

### OS Version

Ubuntu 22.04.4 LTS (x86_64)

### ONNX Runtime Installation

Released Package

### ONNX Runtime Version or Commit ID

1.23.2

### ONNX Runtime API

Python

### Architecture

X64

### Execution Provider

Default CPU

### Execution Provider Library Version

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.