Repository navigation
feat(lowering): optionally compute 16-bit pointwise regions in float32 - #5110
Open
shoumikhin wants to merge 1 commit into
Open
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
shoumikhin
force-pushed
the
pointwise-in-float32
branch
2 times, most recently
from
November 25, 2026 10:26
fd9d81c to
4a75648
Compare
shoumikhin
force-pushed
the
pointwise-in-float32
branch
3 times, most recently
from
November 25, 2026 20:17
4cc5a42 to
f2e40c4
Compare
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>
shoumikhin
force-pushed
the
pointwise-in-float32
branch
from
November 26, 2026 08:24
f2e40c4 to
7a77d2c
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 atorch.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: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.
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:
torch.condbranches, unchanged PyTorch code, refit, and a more than 2x lower error on a small 16-bit block.torch.compilepointwise_in_float32=TrueRelated to #5083.