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

Add clamp conversion functionality #293

merged 1 commit into from
Feb 2, 2021

Conversation

peri044
Copy link
Collaborator

@peri044 peri044 commented Jan 26, 2021

Description

Add clamp conversion

Fixes #274

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes

@github-actions github-actions bot added component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: tests Issues re: Tests labels Jan 26, 2021
@peri044 peri044 requested a review from narendasan January 26, 2021 01:07
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@@ -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

// 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

@narendasan narendasan self-requested a review January 28, 2021 22:34
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to C++ style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@narendasan
Copy link
Collaborator

@peri044 can you resolve merge conflicts? Ill merge after that.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to C++ style guidelines:

diff --git a/workspace/tests/core/conversion/converters/test_element_wise.cpp b/tmp/changes.txt
index 444a8d3..ea88bad 100644
--- a/workspace/tests/core/conversion/converters/test_element_wise.cpp
+++ b/tmp/changes.txt
@@ -173,21 +173,21 @@ TEST(Converters, ATenClampMinMaxConvertsCorrectly) {
            return (%4))IR";
  pointwise_test_helper(graph, true);

-TEST(Converters, ATenNeTensorConvertsCorrectly) {
-  const auto graph = R"IR(
+  TEST(Converters, ATenNeTensorConvertsCorrectly) {
+    const auto graph = R"IR(
    graph(%x.1 : Tensor,
      %y.1 : Tensor):
        %3 : Tensor = aten::ne(%x.1, %y.1)
        return (%3))IR";
-  pointwise_test_helper(graph, false, false, {3, 4}, {3, 4});
-  pointwise_test_helper(graph, false, true, {3, 4}, {3, 4});
-}
+    pointwise_test_helper(graph, false, false, {3, 4}, {3, 4});
+    pointwise_test_helper(graph, false, true, {3, 4}, {3, 4});
+  }

-TEST(Converters, ATenNeScalarConvertsCorrectly) {
-  const auto graph = R"IR(
+  TEST(Converters, ATenNeScalarConvertsCorrectly) {
+    const auto graph = R"IR(
    graph(%x.1 : Tensor):
            %2 : int = prim::Constant[value=2]()
            %3 : Tensor = aten::ne(%x.1, %2)
            return (%3))IR";
-  pointwise_test_helper(graph, true, false, {3, 4, 2});
-}
+    pointwise_test_helper(graph, true, false, {3, 4, 2});
+  }
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to C++ style guidelines:

diff --git a/workspace/tests/core/conversion/converters/test_element_wise.cpp b/tmp/changes.txt
index 444a8d3..ea88bad 100644
--- a/workspace/tests/core/conversion/converters/test_element_wise.cpp
+++ b/tmp/changes.txt
@@ -173,21 +173,21 @@ TEST(Converters, ATenClampMinMaxConvertsCorrectly) {
            return (%4))IR";
  pointwise_test_helper(graph, true);

-TEST(Converters, ATenNeTensorConvertsCorrectly) {
-  const auto graph = R"IR(
+  TEST(Converters, ATenNeTensorConvertsCorrectly) {
+    const auto graph = R"IR(
    graph(%x.1 : Tensor,
      %y.1 : Tensor):
        %3 : Tensor = aten::ne(%x.1, %y.1)
        return (%3))IR";
-  pointwise_test_helper(graph, false, false, {3, 4}, {3, 4});
-  pointwise_test_helper(graph, false, true, {3, 4}, {3, 4});
-}
+    pointwise_test_helper(graph, false, false, {3, 4}, {3, 4});
+    pointwise_test_helper(graph, false, true, {3, 4}, {3, 4});
+  }

-TEST(Converters, ATenNeScalarConvertsCorrectly) {
-  const auto graph = R"IR(
+  TEST(Converters, ATenNeScalarConvertsCorrectly) {
+    const auto graph = R"IR(
    graph(%x.1 : Tensor):
            %2 : int = prim::Constant[value=2]()
            %3 : Tensor = aten::ne(%x.1, %2)
            return (%3))IR";
-  pointwise_test_helper(graph, true, false, {3, 4, 2});
-}
+    pointwise_test_helper(graph, true, false, {3, 4, 2});
+  }
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@peri044 peri044 force-pushed the clamp branch 2 times, most recently from 7b076d2 to 96dbbf3 Compare February 1, 2021 23:10
Signed-off-by: Dheeraj Peri <[email protected]>

Apply linting

Signed-off-by: Dheeraj Peri <[email protected]>

Fix merge conflicts

Signed-off-by: Dheeraj Peri <[email protected]>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to C++ style guidelines:

diff --git a/workspace/tests/core/conversion/converters/test_element_wise.cpp b/tmp/changes.txt
index 0759b59..377c189 100644
--- a/workspace/tests/core/conversion/converters/test_element_wise.cpp
+++ b/tmp/changes.txt
@@ -163,37 +163,37 @@ TEST(Converters, ATenNeScalarConvertsCorrectly) {
  pointwise_test_helper(graph, true, false, {3, 4, 2});
  ;

-TEST(Converters, ATenClampMinConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }

-TEST(Converters, ATenClampMaxConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }

-TEST(Converters, ATenClampMinMaxConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }

-TEST(Converters, ATenNeTensorConvertsCorrectly) {
+  TEST(Converters, ATenNeTensorConvertsCorrectly) {
    const auto graph = R"IR(
    graph(%x.1 : Tensor,
      %y.1 : Tensor):
@@ -201,13 +201,13 @@ TEST(Converters, ATenNeTensorConvertsCorrectly) {
        return (%3))IR";
    pointwise_test_helper(graph, false, false, {3, 4}, {3, 4});
    pointwise_test_helper(graph, false, true, {3, 4}, {3, 4});
-}
+  }

-TEST(Converters, ATenNeScalarConvertsCorrectly) {
-  const auto graph = R"IR(
+  TEST(Converters, ATenNeScalarConvertsCorrectly) {
+    const auto graph = R"IR(
  graph(%x.1 : Tensor):
          %2 : int = prim::Constant[value=2]()
          %3 : Tensor = aten::ne(%x.1, %2)
          return (%3))IR";
-  pointwise_test_helper(graph, true, false, {3, 4, 2});
-}
+    pointwise_test_helper(graph, true, false, {3, 4, 2});
+  }
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

There are some changes that do not conform to C++ style guidelines:

diff --git a/workspace/tests/core/conversion/converters/test_element_wise.cpp b/tmp/changes.txt
index 0c28da7..edfb185 100644
--- a/workspace/tests/core/conversion/converters/test_element_wise.cpp
+++ b/tmp/changes.txt
@@ -163,32 +163,32 @@ TEST(Converters, ATenNeScalarConvertsCorrectly) {
  pointwise_test_helper(graph, true, false, {3, 4, 2});
  ;

-TEST(Converters, ATenClampMinConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }

-TEST(Converters, ATenClampMaxConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }

-TEST(Converters, ATenClampMinMaxConvertsCorrectly) {
-  const auto graph = R"IR(
+  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);
-}
+    pointwise_test_helper(graph, true);
+  }
ERROR: Some files do not conform to style guidelines

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Code conforms to Python style guidelines

@peri044
Copy link
Collaborator Author

peri044 commented Feb 1, 2021

Resolved conflicts

@narendasan narendasan merged commit 05ac70e into master Feb 2, 2021
@narendasan narendasan deleted the clamp branch February 2, 2021 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: tests Issues re: Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

↔ [Converter] Add support for clamp in TRTorch
2 participants