Skip to content

Add support for bool in elementwise ops #321

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 15 additions & 1 deletion core/conversion/converters/impl/element_wise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,22 @@ auto element_wise_registrations TRTORCH_UNUSED =
// Should implement self * other
auto self = args[0].ITensorOrFreeze(ctx);
auto other = args[1].ITensorOrFreeze(ctx);
auto mul =
nvinfer1::ILayer* mul = nullptr;
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 Are these changes that would only be relevant to mul or other ops in general? Also did you write tests for this? I didnt see them in the commits

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, I only find the Bool * Int operation in my models. I think other operation like Int / Bool or Int +/- Bool doesn't make any sense. I will add test case later.

Copy link
Contributor

Choose a reason for hiding this comment

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

The input type of trtorch is set to float in default, I have to modify the TRTorch/core/conversion/conversion.cpp:150

auto trt_in = ctx->net->addInput(name.c_str(), ctx->input_type, dims.input_shape);

to support input type of Bool

Copy link
Contributor

Choose a reason for hiding this comment

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

we can try this #327

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What does it mean to do bool * int? should that give you a bool out? like what does False * 8 mean?

Copy link
Contributor

@inocsin inocsin Feb 6, 2021

Choose a reason for hiding this comment

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

[True, False] * [8, 8] = [8, 0], this can be a mask operation, check demo graph here #327

Copy link
Contributor

Choose a reason for hiding this comment

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

we can close this PR if we merge #327

if (self->getType() ==nvinfer1::DataType::kBOOL || other->getType() == nvinfer1::DataType::kBOOL) {
auto self_id = ctx->net->addIdentity(*self);
auto other_id = ctx->net->addIdentity(*other);
if (self->getType() == nvinfer1::DataType::kBOOL) {
self_id->getOutput(0)->setType(nvinfer1::DataType::kINT32);
}
if (other->getType() == nvinfer1::DataType::kBOOL) {
other_id->getOutput(0)->setType(nvinfer1::DataType::kINT32);
}
mul =
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kPROD, self_id->getOutput(0), other_id->getOutput(0), util::node_info(n));
} else {
mul =
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kPROD, self, other, util::node_info(n));
}
TRTORCH_CHECK(mul, "Unable to create mul layer from node: " << *n);

mul->setName(util::node_info(n).c_str());
Expand Down