Repository navigation
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
Closed
feat(autocast): autocast_vector_norm_in_low_precision computes a low precision 2-norm in that type#5067shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
…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>
Contributor
Author
|
Folded into #5058. |
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
PyTorch's decomposition of
torch.linalg.vector_normsquares 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 insideF.normalize, still adds casts to float32 and back around it: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 assqrt(sum(x * x))in that type. TensorRT then runs it in that type and decides how to square and sum.A float16 sum of squares becomes
inffor 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
test_autocast.pybuilds a model with a 1-norm, an excluded 2-norm andF.normalize. With the setting, only theF.normalizenorm is rewritten and its sum runs in float16. Without it, nothing is rewritten. Both models are built and compared to eager.F.normalize(x.float()), then a matmul), in float16 on Linux x86_64 (H100), builds 2 Cast layers with this setting andautocast_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.test_autocast.pypass on Torch-TensorRT 2.15.0 with this change applied, on Linux x86_64.