-
Notifications
You must be signed in to change notification settings - Fork 369
feat: Autocast #3878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Autocast #3878
Conversation
| enable_autocast: bool = _defaults.ENABLE_AUTOCAST, | ||
| low_precision_type: Optional[ | ||
| Union[torch.dtype, dtype] | ||
| ] = _defaults.LOW_PRECISION_TYPE, | ||
| nodes_to_exclude: Collection[str] = _defaults.NODES_TO_EXCLUDE, | ||
| targets_to_exclude: Collection[Target] = _defaults.TARGETS_TO_EXCLUDE, | ||
| data_max: float = _defaults.DATA_MAX, | ||
| max_depth_of_reduction: Optional[int] = _defaults.MAX_DEPTH_OF_REDUCTION, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before merging, these args should be added to other compile functions in this file.
| ]: | ||
| # GEMM: A (M, K) @ B (K, N) = C (M, N) | ||
| self.reduction_depth = input_0_dims[-1] | ||
| # TODO: Add more reduction ops here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should any more reduction targets be added?
| assert ( | ||
| contiguous_inputs[i].dtype == self.input_dtypes[i] | ||
| ), f"Dtype mismatch for {i}th input({input_name}). Expect {self.input_dtypes[i]}, got {contiguous_inputs[i].dtype}." | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This precision check was removed because after autocasting, if the first layer runs in fp16 but the original input is fp32, input_dtypes will become fp16 but contiguous_inputs is still fp32.
Similarly, other runtimes also removed the check.
| # nodes = list(gm.graph.nodes) | ||
| # # insert enter autocast node in the beginning of the graph | ||
| # with gm.graph.inserting_before(nodes[0]): | ||
| # enter_autocast_node = gm.graph.call_function(torch.amp.autocast_mode._enter_autocast, args=("cuda", torch.float16, True, True)) | ||
| # enter_autocast_node.meta.update(getattr(nodes[0], "meta", {})) | ||
|
|
||
| # # insert exit autocast node before the return node, assuming the return node is the last node | ||
| # with gm.graph.inserting_before(nodes[-1]): | ||
| # exit_autocast_node = gm.graph.call_function(torch.amp.autocast_mode._exit_autocast, args=(enter_autocast_node,)) | ||
| # exit_autocast_node.meta.update(getattr(nodes[-1], "meta", {})) | ||
|
|
||
| # gm = clean_up_graph_after_modifications(gm) | ||
| # gm, new_signature = replace_autocast_with_hop_pass(gm, None) | ||
| # logger.debug("Graph after replace_autocast_with_hop_pass:\n%s", gm.graph) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If using Pytorch autocast to wrap the whole model, pytorch will control the precision of each node per the doc and I didn't find a way to customize based on our ruleset.
peri044
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also update the documentation at https://github.com/pytorch/TensorRT/blob/main/docsrc/user_guide/mixed_precision.rst
| auto expected_type = | ||
| util::TRTDataTypeToScalarType(compiled_engine->exec_ctx->getEngine().getTensorDataType(name.c_str())); | ||
| TORCHTRT_CHECK( | ||
| inputs[i].dtype() == expected_type, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not necessary now ?
py/torch_tensorrt/dynamo/lowering/passes/rule_based_autocast.py
Outdated
Show resolved
Hide resolved
py/torch_tensorrt/dynamo/lowering/passes/rule_based_autocast.py
Outdated
Show resolved
Hide resolved
py/torch_tensorrt/dynamo/lowering/passes/rule_based_autocast.py
Outdated
Show resolved
Hide resolved
|
For Tests
L1 or L2 tests |
Description
Weak typing behavior in TensorRT is deprecated. However it is a good way to maximize performance. Therefore, we want to create similar PyTorch native system to use with Torch-TensorRT that recovers some of this behavior.
Fixes #3869
Type of change
Checklist: