Skip to content

feat(lowering): optionally compute 16-bit pointwise regions in float32 - #5110

Open
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:pointwise-in-float32
Open

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:pointwise-in-float32

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

A float16 or bfloat16 model compiled with Torch-TensorRT rounds to 16 bits after almost every op, because each aten op becomes one 16-bit layer that rounds its own result. In a chain of pointwise ops (a residual add, an RMSNorm, a GELU) those roundings add up.

What this change does

It adds an opt-in setting, pointwise_in_float32. A pass runs on each graph right before it becomes a TensorRT engine, including the branches of a torch.cond. It finds each connected region of common pointwise, reduction, view and cast ops that computes in 16 bits, and makes it compute in float32:

  • each 16-bit value that enters the region is cast to float32 once,
  • casts inside the region become float32,
  • each value that leaves it is cast back to its own dtype.

Any other op, such as a matrix multiply, an attention, a softmax or a layer norm, ends a region and keeps its 16-bit inputs. A cast, an expand, and a view or concatenation of a value from outside the region join only when their result only feeds more region math. So a value cast for a matrix multiply is still rounded first, a cache extended for one is not cast in full, and a value is cast before it is expanded. A region gets no casts when its ops give the same result in float32 (a lone relu) or when no 16-bit value enters it. A cast of an outside value to 16 bits keeps its rounding, as fake quantization needs.

Code left to PyTorch and partitioning do not change, and a value that crosses an engine boundary keeps its 16-bit dtype. Refit runs the same pass.

trt_module = torch_tensorrt.dynamo.compile(
    exported_program, inputs, pointwise_in_float32=True
)

The cost: TensorRT does not always fuse the added casts, and a cast between a matrix multiply and the next add can stop that fusion. So run time and memory can grow; weights read by region math are stored in float32.

What was tested

On an A100 with TensorRT 11.4:

  • New unit tests cover the region rules, torch.cond branches, unchanged PyTorch code, refit, and a more than 2x lower error on a small 16-bit block.
  • The partitioning and lowering test folders pass, apart from failures that main has too.
  • A synthetic bfloat16 decoder (8 layers, random weights, 512 tokens, width 1024), against the same model in float32:
Build Relative error ms per call
PyTorch eager 0.00516 8.21
torch.compile 0.00515 1.98
Torch-TensorRT 0.00516 1.27
pointwise_in_float32=True 0.00144 1.29

Related to #5083.

@meta-cla meta-cla Bot added the cla signed label Nov 24, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 24, 2026
@github-actions
github-actions Bot requested a review from narendasan November 24, 2026 22:35
@shoumikhin
shoumikhin force-pushed the pointwise-in-float32 branch 2 times, most recently from fd9d81c to 4a75648 Compare November 25, 2026 10:26
@github-actions github-actions Bot added the component: conversion Issues re: Conversion stage label Nov 25, 2026
@shoumikhin
shoumikhin force-pushed the pointwise-in-float32 branch 3 times, most recently from 4cc5a42 to f2e40c4 Compare November 25, 2026 20:17
A float16 or bfloat16 model compiled with Torch-TensorRT rounds to 16 bits
after almost every op. The network is strongly typed, so each aten op becomes
one 16-bit TensorRT layer that rounds its own result, and in a chain of
pointwise ops, such as a residual add, an RMSNorm and a GELU, those roundings
add up. There is no switch to keep this math in float32 for a model that is
already 16-bit.

The new pointwise_in_float32 setting, off by default, runs a pass on each
graph right before it is converted to a TensorRT engine, including the
branches of each torch.cond in it. The pass finds each connected region of
common pointwise, reduction, view and cast ops that computes a 16-bit value,
and makes the region compute in float32. Each 16-bit value that enters the
region is cast to float32 once, casts inside it become float32, and each
value that leaves it is cast back to its own dtype. Any other op, such as a
matrix multiply, an attention, a softmax or a layer norm, ends a region and
keeps its 16-bit inputs.

A cast joins a region only when its result only feeds more region math, so a
value cast for a matrix multiply is still rounded to 16 bits first. A view
or concatenation joins when its inputs are already in the region, or when
one of them is and its result only feeds more region math, as when scaled
embeddings are concatenated with other embeddings. One that feeds a matrix
multiply, such as a cache being extended, stays out, so a large input is not
cast in full. A view whose memory an op such as as_strided reads, directly
or through the pieces of a split, stays out, since the cast back gives new
memory. An expand joins only when its result only feeds more region math, so
a value is cast, into a region or out of it, before it is expanded, on the
smaller tensor. A region whose 16-bit ops all give the same result in
float32, such as a lone relu between two convolutions, gets no casts, and so
does a region that no 16-bit value enters. A cast of a value from outside a
region to another 16-bit dtype stays out of it. So a rounding that a model asks
for, as in fake quantization, is kept.
Residual adds join the ops around a matrix multiply into one region, so a
residual stream stays in float32 from layer to layer.

Because the pass runs per engine, code left to PyTorch does not change at
all, and partitioning sees the original graph, so the setting does not move
work between PyTorch and TensorRT. A value that crosses an engine boundary
keeps its 16-bit dtype. Refit runs the same pass, so its weight mapping
matches the engine.

The setting can cost run time and memory. TensorRT does not always fuse the
added casts into the kernels around them, and a cast between a matrix
multiply and the add after it can stop TensorRT from fusing the two, mostly
for long inputs. Weights read by region math are stored in float32.

    trt_module = torch_tensorrt.dynamo.compile(
        exported_program, inputs, pointwise_in_float32=True
    )

With the setting off, the pass returns right away, so default compilation
does not change.

Signed-off-by: Anthony Shoumikhin <anthony@shoumikh.in>

This branch has not been deployed

No deployments
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: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant