Skip to content

fix: preserve results when evaluating constant tensor operations - #4708

Open
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:upstream/elementwise-python-fold
Open

fix: preserve results when evaluating constant tensor operations#4708
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:upstream/elementwise-python-fold

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Problem

The converter can evaluate an operation during compilation when both inputs are constants. This is called constant folding. Python's max, min, and logical operators do not work on tensors element by element, so folding can fail or return a whole input tensor.

Simply converting every constant through the engine's input helper would create another problem. Python numbers would become one-element tensors with fixed data types. That can overflow integers, change float16 arithmetic, and turn boolean results into integers.

Change

Keep Python arithmetic and comparison operators for scalar values (single numbers). These operators also preserve PyTorch's choice of data type when a tensor is combined with a scalar.

Use PyTorch's element-by-element max, min, and logical operations for tensor inputs. Do not change how inputs are prepared for operations that run inside TensorRT.

Use non-trainable parameters for the logical tests' constants. This keeps their operations in the traced graph so the converter, not tracing, evaluates them.

Tests

Passed 119/119 tests in the binary-operator module. Direct folding tests check exact values, data types, dimensions, both input orders, and mixed scalar/tensor inputs. Engine tests check float16 overflow and boolean addition, plus max/min/logical operations over multiple elements.

The five max/min/logical engine cases fail on the base branch because Python tries to interpret a multi-element tensor as one boolean. They pass with this change.

Earlier tests that removed only the scalar-preservation logic failed the scalar regressions, including both engine-output checks. Scalar arithmetic itself also works on the base branch; those checks protect existing behavior.

The focused rerun used Linux x86_64, Python 3.12, PyTorch 2.15 nightly, and TensorRT 11.3 with the Python runtime. Earlier native-runtime coverage used TensorRT 11.2; a native TensorRT 11.3 build was not tested.

Windows, aarch64, TensorRT-RTX, and TensorRT 10.x 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: 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 04:12
## Problem

The converter can evaluate an operation during compilation when both inputs are constants. This is called constant folding. Python's `max`, `min`, and logical operators do not work on tensors element by element, so folding can fail or return a whole input tensor.

Simply converting every constant through the engine's input helper would create another problem. Python numbers would become one-element tensors with fixed data types. That can overflow integers, change float16 arithmetic, and turn boolean results into integers.

## Change

Keep Python arithmetic and comparison operators for scalar values (single numbers). These operators also preserve PyTorch's choice of data type when a tensor is combined with a scalar.

Use PyTorch's element-by-element max, min, and logical operations for tensor inputs. Do not change how inputs are prepared for operations that run inside TensorRT.

Use non-trainable parameters for the logical tests' constants. This keeps their operations in the traced graph so the converter, not tracing, evaluates them.

## Tests

Passed 119/119 tests in the binary-operator module. Direct folding tests check exact values, data types, dimensions, both input orders, and mixed scalar/tensor inputs. Engine tests check float16 overflow and boolean addition, plus max/min/logical operations over multiple elements.

The scalar regression tests fail without the scalar fix, including both engine-output checks. The five max/min/logical engine cases fail without the element-by-element fix.

Tested on Linux x86_64 with Python 3.12, TensorRT 11.2, and the native runtime. Windows, aarch64, TensorRT-RTX, TensorRT 10.x, and the Python-only runtime were not tested.
@shoumikhin
shoumikhin force-pushed the upstream/elementwise-python-fold branch from f9bc85a to 47e572d 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: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant