Skip to content

Adding support for aten::topk #302

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 5 commits into from
Feb 2, 2021
Merged

Adding support for aten::topk #302

merged 5 commits into from
Feb 2, 2021

Conversation

narendasan
Copy link
Collaborator

Description

Adds support for aten::topk

Type of change

Please delete options that are not relevant and/or add your own.

  • 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

@narendasan narendasan requested a review from peri044 January 29, 2021 03:51
@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 29, 2021
auto k = args[1].unwrapToInt();
auto dim = args[2].unwrapToInt();
auto largest = args[3].unwrapToBool();
// auto sorted = args[4].unwrapToBool(); # Currently unused
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@inocsin What is the role of this argument and why don't we use it? I commented it out because it was throwing warnings

Copy link
Collaborator

Choose a reason for hiding this comment

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

torch.topk has an argument sorted = True which returns the top k elements in a sorted order (decreasing). By default it is true. Setting it False will result in random order of output top k elements which we can't support in TensorRT. TensorRT always returns the top "k" max or min elements depending on "largest" argument. Maybe we can add this detail as a comment to explain why we aren't using args[4]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will this cause an issue to users? if they expect random does always giving them ordered matter? At the very least yeah there should be a comment/

Copy link
Collaborator

Choose a reason for hiding this comment

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

This won't cause an issue to users in my opinion. So far all usecases for this op have been gathering either top k largest elements or top k smallest elements, both of which are supported. We can revisit this if anyone requests such usecase.

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/core/conversion/converters/impl/topk.cpp b/tmp/changes.txt
index 2addbdc..d052dd9 100644
--- a/workspace/core/conversion/converters/impl/topk.cpp
+++ b/tmp/changes.txt
@@ -25,11 +25,12 @@ auto topk_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().patter

       auto selfDim = util::toVec(self->getDimensions());

-       //reduceAxes	The reduction dimensions. The bit in position i of bitmask reduceAxes corresponds to explicit dimension i of the result.
-       //E.g., the least significant bit corresponds to the first explicit dimension and the next to least significant bit corresponds to the second explicit dimension.
+       // reduceAxes	The reduction dimensions. The bit in position i of bitmask reduceAxes corresponds to explicit
+       // dimension i of the result. E.g., the least significant bit corresponds to the first explicit dimension and the
+       // next to least significant bit corresponds to the second explicit dimension.

       if (dim < 0) {
-           dim = selfDim.size() + dim;
+         dim = selfDim.size() + dim;
       }

       uint32_t shiftDim = 1 << dim;
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/core/conversion/converters/impl/topk.cpp b/tmp/changes.txt
index 2addbdc..d052dd9 100644
--- a/workspace/core/conversion/converters/impl/topk.cpp
+++ b/tmp/changes.txt
@@ -25,11 +25,12 @@ auto topk_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().patter

       auto selfDim = util::toVec(self->getDimensions());

-       //reduceAxes	The reduction dimensions. The bit in position i of bitmask reduceAxes corresponds to explicit dimension i of the result.
-       //E.g., the least significant bit corresponds to the first explicit dimension and the next to least significant bit corresponds to the second explicit dimension.
+       // reduceAxes	The reduction dimensions. The bit in position i of bitmask reduceAxes corresponds to explicit
+       // dimension i of the result. E.g., the least significant bit corresponds to the first explicit dimension and the
+       // next to least significant bit corresponds to the second explicit dimension.

       if (dim < 0) {
-           dim = selfDim.size() + dim;
+         dim = selfDim.size() + dim;
       }

       uint32_t shiftDim = 1 << dim;
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.

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

auto k = args[1].unwrapToInt();
auto dim = args[2].unwrapToInt();
auto largest = args[3].unwrapToBool();
// auto sorted = args[4].unwrapToBool(); # Currently unused
Copy link
Collaborator

Choose a reason for hiding this comment

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

torch.topk has an argument sorted = True which returns the top k elements in a sorted order (decreasing). By default it is true. Setting it False will result in random order of output top k elements which we can't support in TensorRT. TensorRT always returns the top "k" max or min elements depending on "largest" argument. Maybe we can add this detail as a comment to explain why we aren't using args[4]

inocsin and others added 4 commits February 2, 2021 13:07
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
in TensorRT

Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[email protected]>
Signed-off-by: Naren Dasan <[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.

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/core/conversion/converters/impl/topk.cpp b/tmp/changes.txt
index 9ca91a2..dcc83d2 100644
--- a/workspace/core/conversion/converters/impl/topk.cpp
+++ b/tmp/changes.txt
@@ -21,7 +21,8 @@ auto topk_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().patter
       auto k = args[1].unwrapToInt();
       auto dim = args[2].unwrapToInt();
       auto largest = args[3].unwrapToBool();
-       LOG_DEBUG("Note: sorted argument is not used in TensorRT for aten::topk, results will depend on the value of largest");
+       LOG_DEBUG(
+           "Note: sorted argument is not used in TensorRT for aten::topk, results will depend on the value of largest");
       // auto sorted = args[4].unwrapToBool(); # Currently unused

       auto selfDim = util::toVec(self->getDimensions());
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.

Code conforms to C++ style guidelines

@narendasan narendasan merged commit c73d0d1 into master Feb 2, 2021
@narendasan narendasan deleted the topk branch February 2, 2021 21:33
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.

3 participants