Skip to content

[feat] Add converter for aten::any.dim #1707

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
Mar 7, 2023
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: 34 additions & 1 deletion core/conversion/converters/impl/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ auto reduce_registrations TORCHTRT_UNUSED =
return true;
}})
.pattern(
{"aten::min(Tensor self) -> Tensor", [](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
{"aten::min(Tensor self) -> Tensor",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in_tensor = args[0].ITensorOrFreeze(ctx);
auto in_dims = util::toVec(in_tensor->getDimensions());

Expand All @@ -216,6 +217,38 @@ auto reduce_registrations TORCHTRT_UNUSED =
min_layer->setName(util::node_info(n).c_str());
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], min_layer->getOutput(0));

LOG_DEBUG("Output shape: " << out_tensor->getDimensions());
return true;
}})
.pattern(
{"aten::any.dim(Tensor self, int dim, bool keepdim=False) -> Tensor",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in_tensor = args[0].ITensorOrFreeze(ctx);
auto in_dims = in_tensor->getDimensions();
auto dim = args[1].unwrapToInt();
LOG_DEBUG("Dim to reduce (original): " << dim);
dim = dim < 0 ? (in_dims.nbDims + dim) : dim;
LOG_DEBUG("Dim to reduce (converted): " << dim);

uint32_t axis_mask = 1 << dim;
LOG_DEBUG("Axis Mask: " << std::bitset<32>(axis_mask));

auto keepdim = args[2].unwrapToBool();
LOG_DEBUG("Keep dims: " << keepdim);

// Reduce does not work on bool inputs
if (in_tensor->getType() == nvinfer1::DataType::kBOOL) {
in_tensor =
castITensor(ctx, in_tensor, nvinfer1::DataType::kINT32, (util::node_info(n) + "_in").c_str());
}
auto sum_layer = ctx->net->addReduce(*in_tensor, nvinfer1::ReduceOperation::kSUM, axis_mask, keepdim);

TORCHTRT_CHECK(sum_layer, "Unable to create sum layer from node: " << *n);

sum_layer->setName(util::node_info(n).c_str());
auto out_tensor = castITensor(
ctx, sum_layer->getOutput(0), nvinfer1::DataType::kBOOL, (util::node_info(n) + "_out").c_str());
out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], out_tensor);
LOG_DEBUG("Output shape: " << out_tensor->getDimensions());
return true;
}});
Expand Down
44 changes: 44 additions & 0 deletions tests/core/conversion/converters/test_reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,50 @@ TEST(Converters, ATenMeanDimNegIndexKeepDimsConvertsCorrectly) {
test_body(graph, in);
}

TEST(Converters, ATenAnyDimConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=1]()
%3 : bool = prim::Constant[value=0]()
%5 : Tensor = aten::any(%0, %1, %3)
return (%5))IR";
auto in = at::randint(0, 2, {4, 4, 4}, at::kCUDA);
test_body(graph, in);
}

TEST(Converters, ATenAnyDimAllFalseConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=2]()
%3 : bool = prim::Constant[value=0]()
%5 : Tensor = aten::any(%0, %1, %3)
return (%5))IR";
auto in = at::zeros({3, 7, 4}, at::kCUDA).to(torch::kBool);
test_body(graph, in);
}

TEST(Converters, ATenAnyDimKeepDimConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=1]()
%3 : bool = prim::Constant[value=1]()
%5 : Tensor = aten::any(%0, %1, %3)
return (%5))IR";
auto in = at::randint(0, 2, {4, 4, 4}, at::kCUDA).to(torch::kHalf);
test_body(graph, in);
}

TEST(Converters, ATenAnyDimNegIndexConvertsCorrectly) {
const auto graph = R"IR(
graph(%0 : Tensor):
%1 : int = prim::Constant[value=-1]()
%3 : bool = prim::Constant[value=1]()
%5 : Tensor = aten::any(%0, %1, %3)
return (%5))IR";
auto in = at::randint(-2, 2, {2, 32}, at::kCUDA);
test_body(graph, in);
}

TEST(Converters, UnpackVarLowersCorrectly) {
const auto graph = R"IR(
graph(%x.1 : Tensor):
Expand Down