-
Notifications
You must be signed in to change notification settings - Fork 359
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
fix: Error with aten::div
when using truncation with Int32 tensor inputs
#1442
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
@@ -42,6 +42,12 @@ nvinfer1::ILayer* add_elementwise( | |||
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
@@ -42,6 +42,12 @@ nvinfer1::ILayer* add_elementwise( | |||
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
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::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 typesaten::abs
is functional for integer typesaten::floor
does 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:abs
converter 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+cu116
and1.14.0.dev20221018+cu116
: the former does not by default support this operation, while the latter does. This PR does not fix the generalaten::floor
operator for Int32 inputs, but instead fixes theaten::div
truncation operator only.Fixes #1441
Note: The issue was traced to a problem with
aten::div
with truncation enabled, and notaten::floor
Type of change
Checklist: