Skip to content

feat(autocast): autocast_vector_norm_in_low_precision computes a low precision 2-norm in that type - #5067

Closed
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:autocast-vector-norm
Closed

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:autocast-vector-norm

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

What is wrong today

PyTorch's decomposition of torch.linalg.vector_norm squares and sums a float16 or bfloat16 input in float32, as eager does. So a 2-norm that autocast runs in float16, such as the one inside F.normalize, still adds casts to float32 and back around it:

x = F.normalize(features, dim=1)  # the 2-norm runs in float32 between casts

ONNX tools such as ModelOpt AutoCast keep such a norm in float16, as a ReduceL2, so the same model can build an engine with more Cast layers through Torch-TensorRT.

What this change does

It adds a setting, off by default. With autocast_vector_norm_in_low_precision=True, a 2-norm that autocast runs in the low precision type is written as sqrt(sum(x * x)) in that type. TensorRT then runs it in that type and decides how to square and sum.

trt_model = torch_tensorrt.dynamo.compile(
    exported_program,
    enable_autocast=True,
    autocast_low_precision_type=torch.float16,
    autocast_vector_norm_in_low_precision=True,
)

A float16 sum of squares becomes inf for a norm above about 256. That is why the setting is off by default. A norm can still be kept in FP32 with the autocast exclusions. Other norms, such as the 1-norm, are not changed.

How it was tested

  • A new test in test_autocast.py builds a model with a 1-norm, an excluded 2-norm and F.normalize. With the setting, only the F.normalize norm is rewritten and its sum runs in float16. Without it, nothing is rewritten. Both models are built and compared to eager.
  • A small correlation model (convolutions, then F.normalize(x.float()), then a matmul), in float16 on Linux x86_64 (H100), builds 2 Cast layers with this setting and autocast_model_casts="reclassify" (fix(autocast): keep copies that in-place writes need; add opt-in settings for model casts and the 2-norm #5058), instead of 6 with that setting alone. The output matches eager within float16 error.
  • The new test and the autocast region tests in test_autocast.py pass on Torch-TensorRT 2.15.0 with this change applied, on Linux x86_64.

…precision 2-norm in that type

PyTorch's decomposition of torch.linalg.vector_norm squares and sums a float16
or bfloat16 input in float32, as eager does. So a 2-norm that autocast runs in
float16, such as the one in F.normalize, still adds casts to float32 and back
around it.

The new opt-in setting autocast_vector_norm_in_low_precision=True writes such a
norm out as sqrt(sum(x * x)), which TensorRT runs in the low precision type,
like the ReduceL2 of an ONNX model. In float16 the sum of squares becomes inf
for a norm above about 256, so the setting is off by default, and a norm can
still be kept in FP32 with the exclusions.

Signed-off-by: Anthony Shoumikhin <anthony@shoumikh.in>
@meta-cla meta-cla Bot added the cla signed label Nov 17, 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 17, 2026
@shoumikhin

Copy link
Copy Markdown
Contributor Author

Folded into #5058.

@shoumikhin shoumikhin closed this Nov 18, 2026
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: 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