-
Notifications
You must be signed in to change notification settings - Fork 359
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
Einsum converter #1385
Merged
+169
−0
Merged
Einsum converter #1385
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79d1883
feat: Add converter for einsum operator
gs-olive 18fc9c7
Merge branch 'pytorch:master' into einsum
gs-olive 498ac59
Remove redundant imports
gs-olive 3be2f4c
Add newline at end of file
gs-olive 371fc38
Renamed registration and updated a comment
gs-olive 4a40381
Merge branch 'pytorch:master' into einsum
gs-olive 8c012e3
Merge branch 'pytorch:master' into einsum
gs-olive File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "core/conversion/converters/converters.h" | ||
#include "core/conversion/tensorcontainer/TensorContainer.h" | ||
#include "core/util/prelude.h" | ||
|
||
#include <vector> | ||
|
||
namespace torch_tensorrt { | ||
namespace core { | ||
namespace conversion { | ||
namespace converters { | ||
namespace impl { | ||
namespace { | ||
|
||
auto stack_registrations TORCHTRT_UNUSED = RegisterNodeConversionPatterns().pattern( | ||
{"aten::einsum(str equation, Tensor[] tensors) -> (Tensor)", | ||
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool { | ||
// Extract equation and list of tensors | ||
auto equation = args[0].unwrapToString(); | ||
auto in = args[1].IValue()->toListRef(); | ||
|
||
std::vector<nvinfer1::ITensor*> tensors; | ||
|
||
// Populate vector of ITensor pointers | ||
for (auto t : in) { | ||
nvinfer1::ITensor* itensor; | ||
|
||
// Tensor is either an ITensor (wrapped) or PyTorch Tensor | ||
if (t.isTensor()) { | ||
auto weight = Weights(ctx, t.toTensor()); | ||
|
||
auto const_layer = ctx->net->addConstant(weight.shape, weight.data); | ||
TORCHTRT_CHECK(const_layer, "Unable to create constant layer from node: " << *n); | ||
|
||
itensor = const_layer->getOutput(0); | ||
} else { | ||
auto cont = t.toCustomClass<TensorContainer>(); | ||
itensor = cont->tensor(); | ||
} | ||
|
||
tensors.push_back(itensor); | ||
} | ||
|
||
// Add Tensor-RT Einsum layer | ||
auto einsum_layer = ctx->net->addEinsum(tensors.data(), tensors.size(), equation.c_str()); | ||
TORCHTRT_CHECK(einsum_layer, "Unable to create einsum layer from node: " << *n); | ||
|
||
einsum_layer->setName(util::node_info(n).c_str()); | ||
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], einsum_layer->getOutput(0)); | ||
|
||
LOG_DEBUG("Output tensor shape: " << out_tensor->getDimensions()); | ||
return true; | ||
}}); | ||
|
||
} // namespace | ||
} // namespace impl | ||
} // namespace converters | ||
} // namespace conversion | ||
} // namespace core | ||
} // namespace torch_tensorrt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <string> | ||
#include "core/compiler.h" | ||
#include "gtest/gtest.h" | ||
#include "tests/util/util.h" | ||
#include "torch/csrc/jit/ir/irparser.h" | ||
|
||
TEST(Converters, ATenEinsumConvertsMatMulCorrectly) { | ||
const auto graph = R"IR( | ||
graph(%x.1 : Tensor, %x.2 : Tensor): | ||
%0 : str = prim::Constant[value="ij,jk->ik"]() | ||
%3 : Tensor[] = prim::ListConstruct(%x.1, %x.2) | ||
%4 : Tensor = aten::einsum(%0, %3) | ||
return (%4))IR"; | ||
|
||
auto g = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(graph, g.get()); | ||
|
||
// Test matrix multiplication via einsum | ||
auto in_0 = at::rand({12, 17}, {at::kCUDA}); | ||
auto in_1 = at::rand({17, 35}, {at::kCUDA}); | ||
|
||
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in_0, in_1}); | ||
|
||
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in_0, in_1}); | ||
|
||
ASSERT_TRUE( | ||
torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-6)); | ||
} | ||
|
||
TEST(Converters, ATenEinsumConvertsElementwiseProdCorrectly) { | ||
const auto graph = R"IR( | ||
graph(%x.1 : Tensor, %x.2 : Tensor): | ||
%0 : str = prim::Constant[value="abcd,abcd->abcd"]() | ||
%3 : Tensor[] = prim::ListConstruct(%x.1, %x.2) | ||
%4 : Tensor = aten::einsum(%0, %3) | ||
return (%4))IR"; | ||
|
||
auto g = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(graph, g.get()); | ||
|
||
// Test elementwise tensor product via einsum | ||
auto in_0 = at::rand({7, 5, 2, 8}, {at::kCUDA}); | ||
auto in_1 = at::rand({7, 5, 2, 8}, {at::kCUDA}); | ||
|
||
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in_0, in_1}); | ||
|
||
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in_0, in_1}); | ||
|
||
ASSERT_TRUE( | ||
torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-6)); | ||
} | ||
|
||
TEST(Converters, ATenEinsumConvertsTransposeCorrectly) { | ||
const auto graph = R"IR( | ||
graph(%x.1 : Tensor): | ||
%0 : str = prim::Constant[value="jk->kj"]() | ||
%3 : Tensor[] = prim::ListConstruct(%x.1) | ||
%4 : Tensor = aten::einsum(%0, %3) | ||
return (%4))IR"; | ||
|
||
auto g = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(graph, g.get()); | ||
|
||
// Test single-matrix transpose via einsum | ||
auto in_0 = at::rand({25, 28}, {at::kCUDA}); | ||
|
||
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in_0}); | ||
|
||
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in_0}); | ||
|
||
ASSERT_TRUE( | ||
torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-6)); | ||
} | ||
|
||
TEST(Converters, ATenEinsumConvertsVectorsCorrectly) { | ||
const auto graph = R"IR( | ||
graph(%x.1 : Tensor, %x.2 : Tensor): | ||
%0 : str = prim::Constant[value="a,b->ab"]() | ||
%3 : Tensor[] = prim::ListConstruct(%x.1, %x.2) | ||
%4 : Tensor = aten::einsum(%0, %3) | ||
return (%4))IR"; | ||
|
||
auto g = std::make_shared<torch::jit::Graph>(); | ||
torch::jit::parseIR(graph, g.get()); | ||
|
||
// Test vector outer product via einsum | ||
auto in_0 = at::rand({25}, {at::kCUDA}); | ||
auto in_1 = at::rand({4}, {at::kCUDA}); | ||
|
||
auto params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto jit_results = torch_tensorrt::tests::util::RunGraph(g, params, {in_0, in_1}); | ||
|
||
params = torch_tensorrt::core::ir::get_static_params(g->inputs(), {}); | ||
auto trt_results = torch_tensorrt::tests::util::RunGraphEngine(g, params, {in_0, in_1}); | ||
|
||
ASSERT_TRUE( | ||
torch_tensorrt::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-6)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should be
einsum_registrations