Skip to content

Add clamp conversion functionality #293

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

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions core/conversion/converters/impl/element_wise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,41 @@ auto element_wise_registrations TRTORCH_UNUSED =
LOG_DEBUG("Output tensor shape: " << out->getDimensions());
return true;
}})
.pattern({"aten::clamp(Tensor self, Scalar? min=None, Scalar? max=None) -> (Tensor)",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we possibly lower to hardtanh or is the functionality different?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardtanh looks a bit different https://pytorch.org/docs/stable/generated/torch.nn.Hardtanh.html
hardtanh compares the elements to -1 and 1 and replaces with default values (thresholds) where as clamp compares the input elements to thresholds directly. They both probably can match in certain scenarios

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hardtanh lets you configure the min and max values. really my point is we should be able to lower clamp and hardtanh to the same clip activation converter. This is something we could leave for later though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally our converter library should be as small as possible and we should do as much as we can through lowering

[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
// Compute min(max(min_threshold, input), max_threshold)
auto self = args[0].ITensorOrFreeze(ctx);
auto clamp_layer_out = self;
if (args[1].isIValue() && args[1].IValue()->isScalar()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be able to do !isNone on the IValue instead of two checks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, isNone() didn't work for me. I will double check again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The op does basically this min(max(min_threshold, input), max_threshold)
In the case when min_threshold or max_threshold is not given, args[1] or args[2] will be registered as an IValue.
The isNone() check will check if the type is one of { kITensor, kIValue, kNone }. The type in this case would be IValue so isNone() will not return true.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

auto minScalar = args[1].unwrapToScalar().to<float>();
auto minTensor = tensor_to_const(ctx, torch::tensor({minScalar}));
auto max_layer = add_elementwise(
ctx,
nvinfer1::ElementWiseOperation::kMAX,
clamp_layer_out,
minTensor,
util::node_info(n) + std::string("_max"));
TRTORCH_CHECK(max_layer, "Unable to create elementwise max layer for node: " << *n);
clamp_layer_out = max_layer->getOutput(0);
}

if (args[2].isIValue() && args[2].IValue()->isScalar()) {
auto maxScalar = args[2].unwrapToScalar().to<float>();
auto maxTensor = tensor_to_const(ctx, torch::tensor({maxScalar}));
auto min_layer = add_elementwise(
ctx,
nvinfer1::ElementWiseOperation::kMIN,
clamp_layer_out,
maxTensor,
util::node_info(n) + std::string("_min"));
TRTORCH_CHECK(min_layer, "Unable to create elementwise min layer for node: " << *n);
clamp_layer_out = min_layer->getOutput(0);
}

auto out = ctx->AssociateValueAndTensor(n->outputs()[0], clamp_layer_out);
LOG_DEBUG("Clamp layer output tensor shape: " << clamp_layer_out->getDimensions());
return true;
}})
.pattern({"aten::sub.Tensor(Tensor self, Tensor other, Scalar alpha=1) -> "
"Tensor",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
Expand Down
29 changes: 29 additions & 0 deletions tests/core/conversion/converters/test_element_wise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,33 @@ TEST(Converters, ATenNeScalarConvertsCorrectly) {
return (%3))IR";
pointwise_test_helper(graph, true, false, {3, 4, 2});
;

TEST(Converters, ATenClampMinConvertsCorrectly) {
const auto graph = R"IR(
graph(%x.1 : Tensor):
%2 : int = prim::Constant[value=-2]()
%3 : None = prim::Constant()
%4 : Tensor = aten::clamp(%x.1, %2, %3)
return (%4))IR";
pointwise_test_helper(graph, true);
}

TEST(Converters, ATenClampMaxConvertsCorrectly) {
const auto graph = R"IR(
graph(%x.1 : Tensor):
%2 : int = prim::Constant[value=3]()
%3 : None = prim::Constant()
%4 : Tensor = aten::clamp(%x.1, %3, %2)
return (%4))IR";
pointwise_test_helper(graph, true);
}

TEST(Converters, ATenClampMinMaxConvertsCorrectly) {
const auto graph = R"IR(
graph(%x.1 : Tensor):
%2 : int = prim::Constant[value=3]()
%3 : int = prim::Constant[value=-2]()
%4 : Tensor = aten::clamp(%x.1, %3, %2)
return (%4))IR";
pointwise_test_helper(graph, true);
}