Skip to content
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

fix: aten::where with differing-shape inputs bugfix #1533

Merged
merged 2 commits into from
Dec 12, 2022
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
14 changes: 14 additions & 0 deletions core/conversion/converters/impl/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,22 @@ auto select_registrations TORCHTRT_UNUSED =
{"aten::where.self(Tensor condition, Tensor self, Tensor other) -> (Tensor)",
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
auto condition = args[0].ITensorOrFreeze(ctx);
auto condition_nbDims = condition->getDimensions().nbDims;
auto x = args[1].ITensorOrFreeze(ctx);
auto x_nbDims = x->getDimensions().nbDims;
auto y = args[2].ITensorOrFreeze(ctx);
auto y_nbDims = y->getDimensions().nbDims;

// Get maximum rank of all input tensors
auto max_nbDims = std::max(condition_nbDims, std::max(x_nbDims, y_nbDims));

// TensorRT requires all inputs to Select layers to have the same rank, so for each
// tensor input, ensure that its rank is equal to the maximum number of dimensions
// If not, left-pad the tensor dimension with 1s until the max rank is achieved
condition =
addPadding(ctx, n, condition, max_nbDims, /*bool trailing =*/false, /*bool use_zeros =*/false);
x = addPadding(ctx, n, x, max_nbDims, /*bool trailing =*/false, /*bool use_zeros =*/false);
y = addPadding(ctx, n, y, max_nbDims, /*bool trailing =*/false, /*bool use_zeros =*/false);

auto layer = ctx->net->addSelect(*condition, *x, *y);

Expand Down
32 changes: 32 additions & 0 deletions tests/core/conversion/converters/test_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,3 +1224,35 @@ TEST(Converters, WhereConvertsCorrectly) {

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

TEST(Converters, WhereConvertsMismatchedShapesCorrectly) {
const auto graph = R"IR(
graph(%condition : Tensor,
%x : Tensor,
%y : Tensor):
%out : Tensor = aten::where(%condition, %x, %y)
return (%out))IR";

auto g = std::make_shared<torch::jit::Graph>();

torch::jit::parseIR(graph, g.get());

// As per Torch behavior, the input Tensors are expected to be broadcasted
// along their respective dimension in the largest-rank Tensor provided
auto condition = at::randint(0, 2, {7, 5}, {at::kCUDA}).to(torch::kBool);
auto x = at::randn({2, 7, 5}, {at::kCUDA});
auto y = at::randn({5}, {at::kCUDA});

auto jit_condition = at::clone(condition);
auto jit_x = at::clone(x);
auto jit_y = at::clone(y);
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {});
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {jit_condition, jit_x, jit_y});

auto trt_condition = at::clone(condition);
auto trt_x = at::clone(x);
auto trt_y = at::clone(y);
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {trt_condition, trt_x, trt_y});

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