-
Notifications
You must be signed in to change notification settings - Fork 371
fix: Error with aten::div when using truncation with Int32 tensor inputs
#1442
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
Conversation
| auto abs = add_absolute_value(ctx, n, tmp_div->getOutput(0), util::node_info(n) + "_absolute_val"); | ||
|
|
||
| // In this case, we allow the floor unary on non-TRT Unary types, as it is needed for this | ||
| // specific function. Floor applied to non-float types equates to identity | ||
| nvinfer1::ILayer* floor; | ||
| if ((abs->getOutput(0)->getType() == nvinfer1::DataType::kINT32) || | ||
| (abs->getOutput(0)->getType() == nvinfer1::DataType::kBOOL)) { | ||
| LOG_GRAPH( | ||
| "Tensor is of unsupported type " << abs->getOutput(0)->getType() | ||
| << " for IUnaryLayer::kFLOOR. Using identity instead."); | ||
| floor = ctx->net->addIdentity(*abs->getOutput(0)); | ||
| TORCHTRT_CHECK(floor, "Unable to create identity layer from node: " << *n); | ||
| } else { | ||
| floor = ctx->net->addUnary(*abs->getOutput(0), nvinfer1::UnaryOperation::kFLOOR); | ||
| TORCHTRT_CHECK(floor, "Unable to create floor layer from node: " << *n); | ||
| } | ||
| floor->setName((util::node_info(n) + "_floor").c_str()); |
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.
In this code block, both the abs and the floor operators were encountering errors when both inputs are integer types. The solution for abs was a converter utility, whereas for floor, the solution only appears here. The reasoning for this choice was that Torch support for torch.floor() applied to an Int32 type differs between 1.13.0.dev20220921+cu116 and 1.14.0.dev20221018+cu116, so it is unclear if the aten::floor converter should generally support Int32 inputs or not, currently.
464fae5 to
01ee345
Compare
| nvinfer1::ITensor* other, | ||
| const std::string& name); | ||
|
|
||
| nvinfer1::ILayer* add_absolute_value( |
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.
rename to add_abs
| nvinfer1::ITensor* other, | ||
| const std::string& name); | ||
|
|
||
| nvinfer1::ILayer* add_absolute_value( |
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.
Return nvinfer1::ITensor*
| nvinfer1::ILayer* floor; | ||
| if ((abs->getOutput(0)->getType() == nvinfer1::DataType::kINT32) || | ||
| (abs->getOutput(0)->getType() == nvinfer1::DataType::kBOOL)) { | ||
| LOG_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.
LOG_DEBUG instead of LOG_GRAPH
|
|
||
| // In this case, we allow the floor unary on non-TRT Unary types, as it is needed for this | ||
| // specific function. Floor applied to non-float types equates to identity | ||
| nvinfer1::ILayer* floor; |
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.
Work with ITensor* instead ideally
| floor = ctx->net->addUnary(*abs->getOutput(0), nvinfer1::UnaryOperation::kFLOOR); | ||
| TORCHTRT_CHECK(floor, "Unable to create floor layer from node: " << *n); | ||
| } | ||
| floor->setName((util::node_info(n) + "_floor").c_str()); |
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 needs to only be applied to the unary layer on line 342 so as not to overwrite the info from the abs on 329
046b6dd to
aa9a01f
Compare
| TORCHTRT_CHECK(absolute_value_layer, "Unable to create max layer from node: " << *n); | ||
| } | ||
|
|
||
| return absolute_value_layer->getOutput(0); |
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.
Switched function schema to return the output of the absolute value layer and not the layer itself
| LOG_DEBUG( | ||
| "Tensor is of unsupported type " << abs->getType() | ||
| << " for IUnaryLayer::kFLOOR. Using identity instead."); | ||
| floor = abs; |
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.
Instead of using the identity function, the floor output tensor is just set to the absolute value result, to avoid unnecessary computation.
- `aten::div` with truncation on integer tensor inputs currently throws an error if both inputs are integer type, as the TRT unary operations for absolute value and floor do not apply to Int32 or Bool types - For absolute value, this is a legitimate bug as `aten::abs` is functional for integer types - For the floor operation, `aten::floor` does not explicitly support integer inputs, and `torch.floor()` does not work with Int32 inputs by default. However, `torch.div(..., rounding_mode="trunc")` with integer tensors does return an integer value, and so the corollary Torch-TRT converter should behave similarly - Modified `aten:abs` converter logic to be a utility, as it is used in multiple locations - Added regression test to ensure truncation divide with two integer tensors is functional - Address comments on PR - Update utility name to add_abs for conciseness - Refactor absolute value utility to return ITensor* - Update logging level for certain debug messages
aa9a01f to
dac2da0
Compare
narendasan
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.
LGTM
Description
aten::divwith truncation on integer tensor inputs currently throws an error if both inputs are integer type, as the TRT unary operations for absolute value and floor do not apply to Int32 or Bool typesaten::absis functional for integer typesaten::floordoes not explicitly support integer inputs, andtorch.floor()does not work with Int32 inputs by default (on1.13.0.dev20220921+cu116). However,torch.div(..., rounding_mode="trunc")with integer tensors does return an integer value, and so the corollary Torch-TRT converter should behave similarlyaten:absconverter logic to be a utility (moved file location), as the operator is used in multiple locationsNote: The behavior of
torch.floor()on Int32 types differs between1.13.0.dev20220921+cu116and1.14.0.dev20221018+cu116: the former does not by default support this operation, while the latter does. This PR does not fix the generalaten::flooroperator for Int32 inputs, but instead fixes theaten::divtruncation operator only.Fixes #1441
Note: The issue was traced to a problem with
aten::divwith truncation enabled, and notaten::floorType of change
Checklist: