Skip to content

[feat] Add converter support for aten::logical_not #1705

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 28, 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
15 changes: 15 additions & 0 deletions core/conversion/converters/impl/unary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ auto reciprocal_registration TORCHTRT_UNUSED = RegisterNodeConversionPatterns().
return true;
}});

auto logical_not_registration TORCHTRT_UNUSED = RegisterNodeConversionPatterns().pattern(
{"aten::logical_not(Tensor self) -> Tensor", [](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto in = args[0].ITensorOrFreeze(ctx);
if (in->getType() != nvinfer1::DataType::kBOOL) {
// unary not layer only supports bool inputs
in = castITensor(ctx, in, nvinfer1::DataType::kBOOL, util::node_info(n).c_str());
}
auto unary_layer = ctx->net->addUnary(*in, nvinfer1::UnaryOperation::kNOT);
TORCHTRT_CHECK(unary_layer, "Unable to create logical_not layer from node: " << *n);
unary_layer->setName(util::node_info(n).c_str());
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], unary_layer->getOutput(0));
LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions());
return true;
}});

#define convert(unary, trt_type) \
auto unary##_registrations TORCHTRT_UNUSED = RegisterNodeConversionPatterns().pattern( \
{"aten::" #unary "(Tensor self) -> Tensor", \
Expand Down
16 changes: 16 additions & 0 deletions tests/core/conversion/converters/test_unary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ TEST(Converters, ATenSignConvertsZerosCorrectly) {
torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-6));
}

TEST(Converters, ATenLogicalNotBoolConvertsCorrectly) {
const auto graph = gen_test_graph("logical_not");
auto g = std::make_shared<torch::jit::Graph>();
torch::jit::parseIR(graph, g.get());
auto in = at::randint(0, 2, {7, 3, 1, 5}, {at::kCUDA}).to(torch::kBool);

auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in});

params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in});

ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0], 2e-6));
}

#define test_unary(unary, name) \
TEST(Converters, ATen##name##ConvertsCorrectly) { \
const auto graph = gen_test_graph(#unary); \
Expand Down Expand Up @@ -122,5 +137,6 @@ test_unary(erf, Erf);
test_unary(asinh, Asinh);
test_unary(acosh, Acosh);
test_unary(atanh, Atanh);
test_unary(logical_not, LogicalNot);

#undef test_unary