Skip to content

fix: run select with a computed index - #4709

Open
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:upstream/select-runtime-index
Open

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:upstream/select-runtime-index

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Problem

select cannot run in TensorRT when its index is computed from another input. The index also needs the right shape: selecting one row from an (8, 4) tensor should return (4,), not (1, 4).

Change

Accept an index computed at runtime. Convert a one-element index tensor to a scalar before passing it to TensorRT's gather operation.

Record how many dimensions each scalar input expects. Use that information when calling the engine and when exporting the compiled model. This keeps integer and floating-point scalar inputs consistent across those paths.

Check that the model test builds an engine of the expected type. Runtime selection depends on native-library availability; the deprecated use_python_runtime option does not select a backend.

Tests

Passed 7/7 converter tests with each runtime. The computed-index model passed with both; a floating-point scalar control also passed with the native runtime. Earlier testing covered that scalar control with the Python runtime too. Assertions confirmed native ScriptObject or Python TRTEngine engines. The converter control fails on the base branch when it tries to convert a TensorRT index into a NumPy constant.

Direct execution matched PyTorch shape, data type, and values for indices 3, 5, and 0. A separate check used both exporters, legacy and nonlegacy, to export, save to BytesIO, reload, and execute the same changing indices. All four exporter/runtime combinations passed.

The rerun used Linux x86_64, Python 3.12, and PyTorch 2.15 nightly. Python-runtime checks used TensorRT 11.3. Native checks used the published Torch-TensorRT 2.15 nightly library with its TensorRT 11.2 dependency; they do not validate a native TensorRT 11.3 build.

Export checks bound indices to the valid range. Existing gather bounds behavior and conversion to 32-bit indices are unchanged. Windows, aarch64, TensorRT-RTX, TensorRT 10.x, changing input dimensions, different input devices, concurrent calls, older saved models, and filesystem durability were not tested.

@meta-cla meta-cla Bot added the cla signed label Sep 10, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Sep 10, 2026
@github-actions
github-actions Bot requested a review from narendasan September 10, 2026 05:14
## Problem

`select` cannot run in TensorRT when its index is computed from another input. The index also needs the right shape: selecting one row from an `(8, 4)` tensor should return `(4,)`, not `(1, 4)`.

## Change

Accept an index computed at runtime. Convert a one-element index tensor to a scalar (a value with no tensor dimensions) before passing it to TensorRT's gather operation.

Record how many dimensions each scalar input expects. Use that information when calling the engine and when exporting the compiled model. This keeps integer and floating-point scalar inputs consistent across those paths.

Check that the model test builds an engine of the expected type. Runtime selection depends on native-library availability; the deprecated `use_python_runtime` option does not select a backend.

## Tests

Passed 7/7 converter tests. The computed-index model and a floating-point scalar control both passed in separate native and Python-only package layouts. Assertions checked native-library availability and confirmed native `ScriptObject` engines or Python `TRTEngine` engines.

The compiled wrapper ran directly with inputs producing indices 3, 5, and 0. Shape, data type, and values matched ordinary PyTorch.

A separate export check also ran the wrapper directly. It then used both exporters, legacy and nonlegacy, to export, save, reload, and run the model with the same changing indices. All four exporter/backend combinations passed. Saving used `BytesIO`, an in-memory byte stream, not a file on disk.

That export check explicitly limits indices to the valid range. It does not test out-of-range behavior. Existing gather bounds behavior and conversion of indices to 32-bit integers are unchanged.

Tested on Linux x86_64 with Python 3.12, PyTorch 2.15 nightly, and TensorRT 11.2. Windows, aarch64, TensorRT-RTX, TensorRT 10.x, changing input dimensions, inputs on different devices, concurrent calls, older saved models, and filesystem durability were not tested.
@shoumikhin
shoumikhin force-pushed the upstream/select-runtime-index branch from a854f6f to a54a73e Compare September 12, 2026 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: runtime component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant