Skip to content

Commit 4b61dda

Browse files
committed
Merge branch 'master' of https://github.com/NVIDIA/TRTorch into bowa_primif
2 parents 8992a1a + e63d8b2 commit 4b61dda

File tree

75 files changed

+2804
-339
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2804
-339
lines changed

.github/scripts/run_cpp_linter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
comment = '''There are some changes that do not conform to C++ style guidelines:\n ```diff\n{}```'''.format(output.stdout.decode("utf-8"))
2525
approval = 'REQUEST_CHANGES'
2626

27-
pr.create_review(commit, comment, approval)
28-
27+
try:
28+
pr.create_review(commit, comment, approval)
29+
except:
30+
print("Unable to submit in depth review, please review logs for linting issues")
31+
2932
if output.returncode != 0:
3033
exit(1)
3134
else:
32-
exit(0)
35+
exit(0)

.github/scripts/run_py_linter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
comment = '''There are some changes that do not conform to Python style guidelines:\n ```diff\n{}```'''.format(output.stdout.decode("utf-8"))
2525
approval = 'REQUEST_CHANGES'
2626

27-
pr.create_review(commit, comment, approval)
27+
try:
28+
pr.create_review(commit, comment, approval)
29+
except:
30+
print("Unable to submit in depth review, please review logs for linting issues")
2831

2932
if output.returncode != 0:
3033
exit(1)
3134
else:
32-
exit(0)
35+
exit(0)

BUILD

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ pkg_tar(
1515
"//core/conversion:include",
1616
"//core/conversion/conversionctx:include",
1717
"//core/conversion/converters:include",
18-
"//core/conversion/converters/impl/plugins:include",
19-
"//core/conversion/evaluators:include",
20-
"//core/conversion/tensorcontainer:include",
2118
"//core/conversion/var:include",
19+
"//core/conversion/tensorcontainer:include",
20+
"//core/conversion/evaluators:include",
21+
"//core/plugins:include",
2222
"//core/lowering:include",
2323
"//core/lowering/passes:include",
2424
"//core/runtime:include",
@@ -42,6 +42,7 @@ pkg_tar(
4242
"//conditions:default": [
4343
"//cpp/api/lib:libtrtorch.so",
4444
"//cpp/api/lib:libtrtorchrt.so",
45+
"//cpp/api/lib:libtrtorch_plugins.so",
4546
],
4647
}),
4748
mode = "0755",

core/conversion/conversionctx/ConversionCtx.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
#include "core/conversion/conversionctx/ConversionCtx.h"
12
#include <iostream>
23
#include <sstream>
34
#include <utility>
45

5-
#include "core/conversion/conversionctx/ConversionCtx.h"
6-
76
namespace trtorch {
87
namespace core {
98
namespace conversion {

core/conversion/converters/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ cc_library(
3636
"impl/concat.cpp",
3737
"impl/constant.cpp",
3838
"impl/conv_deconv.cpp",
39+
"impl/cumsum.cpp",
3940
"impl/element_wise.cpp",
4041
"impl/expand.cpp",
4142
"impl/interpolate.cpp",
43+
"impl/layer_norm.cpp",
4244
"impl/linear.cpp",
4345
"impl/lstm_cell.cpp",
4446
"impl/matrix_multiply.cpp",
47+
"impl/normalize.cpp",
4548
"impl/pooling.cpp",
4649
"impl/reduce.cpp",
4750
"impl/replication_pad.cpp",
@@ -64,7 +67,7 @@ cc_library(
6467
"//core/conversion/var",
6568
"//core/conversion/tensorcontainer",
6669
"//core/conversion/conversionctx",
67-
"//core/conversion/converters/impl/plugins",
70+
"//core/plugins:trtorch_plugins",
6871
] + select({
6972
":use_pre_cxx11_abi": ["@libtorch_pre_cxx11_abi//:libtorch"],
7073
"//conditions:default": ["@libtorch//:libtorch"],

core/conversion/converters/converter_util.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "core/conversion/converters/converter_util.h"
22
#include "core/conversion/converters/converters.h"
33
#include "core/util/prelude.h"
4+
#include "torch/torch.h"
45

56
namespace trtorch {
67
namespace core {
@@ -59,6 +60,68 @@ nvinfer1::ITensor* addUnpadding(
5960
}
6061
}
6162

63+
nvinfer1::ILayer* add_elementwise(
64+
ConversionCtx* ctx,
65+
nvinfer1::ElementWiseOperation op,
66+
nvinfer1::ITensor* self,
67+
nvinfer1::ITensor* other,
68+
const std::string& name) {
69+
// ensure self to have larger number of dimension
70+
bool swapSelfOther = false;
71+
if (self->getDimensions().nbDims < other->getDimensions().nbDims) {
72+
std::swap(self, other);
73+
swapSelfOther = true;
74+
}
75+
auto selfDim = util::toVec(self->getDimensions());
76+
auto otherDim = util::toVec(other->getDimensions());
77+
if (selfDim.size() != otherDim.size()) {
78+
// other is with dynamic shape, need to expand its dimension now and get its
79+
// shape at runtime
80+
if (otherDim.end() != std::find(otherDim.begin(), otherDim.end(), -1)) {
81+
auto thOtherStaticShapeMask = torch::ones(selfDim.size(), torch::kInt32);
82+
auto thOtherDynamicShapeMask = torch::zeros(selfDim.size(), torch::kInt32);
83+
for (size_t start = selfDim.size() - otherDim.size(), idx = 0; idx < otherDim.size(); ++idx) {
84+
if (-1 != otherDim[idx]) {
85+
thOtherStaticShapeMask[start + idx] = otherDim[idx];
86+
} else {
87+
thOtherStaticShapeMask[start + idx] = 0;
88+
thOtherDynamicShapeMask[start + idx] = 1;
89+
}
90+
}
91+
auto otherStaticShapeMask = tensor_to_const(ctx, thOtherStaticShapeMask);
92+
auto otherDynamicShapeMask = tensor_to_const(ctx, thOtherDynamicShapeMask);
93+
auto selfShape = ctx->net->addShape(*self)->getOutput(0);
94+
// size of dynamic dimension of other need to the same as that of
95+
// corresponding dimension of self
96+
auto otherDynamicShape =
97+
ctx->net->addElementWise(*selfShape, *otherDynamicShapeMask, nvinfer1::ElementWiseOperation::kPROD)
98+
->getOutput(0);
99+
auto targetOtherShape =
100+
ctx->net->addElementWise(*otherDynamicShape, *otherStaticShapeMask, nvinfer1::ElementWiseOperation::kSUM)
101+
->getOutput(0);
102+
103+
auto otherShuffle = ctx->net->addShuffle(*other);
104+
otherShuffle->setName(std::string("Reshape other tensor to have the same nDim as self for " + name).c_str());
105+
otherShuffle->setInput(1, *targetOtherShape);
106+
other = otherShuffle->getOutput(0);
107+
} else {
108+
// other is with static shape, expand dimension to make tow tensor have
109+
// the same number of dimension
110+
auto otherShuffle = ctx->net->addShuffle(*other);
111+
otherShuffle->setReshapeDimensions(util::toDimsPad(otherDim, selfDim.size()));
112+
other = otherShuffle->getOutput(0);
113+
}
114+
}
115+
if (swapSelfOther) {
116+
// swap back
117+
std::swap(self, other);
118+
swapSelfOther = false;
119+
}
120+
auto ele = ctx->net->addElementWise(*self, *other, op);
121+
ele->setName(name.c_str());
122+
return ele;
123+
}
124+
62125
} // namespace converters
63126
} // namespace conversion
64127
} // namespace core

core/conversion/converters/converter_util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ nvinfer1::ITensor* addUnpadding(
3535
bool trailing = true,
3636
bool use_zeros = true);
3737

38+
nvinfer1::ILayer* add_elementwise(
39+
ConversionCtx* ctx,
40+
nvinfer1::ElementWiseOperation op,
41+
nvinfer1::ITensor* self,
42+
nvinfer1::ITensor* other,
43+
const std::string& name);
44+
3845
} // namespace converters
3946
} // namespace conversion
4047
} // namespace core

core/conversion/converters/impl/activation.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ auto acthardtanh TRTORCH_UNUSED =
146146

147147
auto new_layer = ctx->net->addActivation(*self, nvinfer1::ActivationType::kLEAKY_RELU);
148148
new_layer->setAlpha(negative_slopeScalar);
149-
150149
new_layer->setName(util::node_info(n).c_str());
151150
auto out_tensor = new_layer->getOutput(0);
152151
out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], out_tensor);
@@ -167,6 +166,35 @@ auto acthardtanh TRTORCH_UNUSED =
167166
auto out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], new_layer->getOutput(0));
168167
LOG_DEBUG("Output shape: " << out_tensor->getDimensions());
169168
return true;
169+
}})
170+
.pattern({"aten::gelu(Tensor self) -> (Tensor)",
171+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
172+
auto in = args[0].ITensorOrFreeze(ctx);
173+
nvinfer1::DataType type = in->getType();
174+
TRTORCH_CHECK(
175+
type == nvinfer1::DataType::kFLOAT || type == nvinfer1::DataType::kHALF,
176+
"gelu only supports kFLOAT and kHALF");
177+
std::string pluginName = "CustomGeluPluginDynamic";
178+
nvinfer1::PluginFieldCollection fc;
179+
std::vector<nvinfer1::PluginField> f;
180+
int type_id = ctx->settings.op_precision == nvinfer1::DataType::kFLOAT
181+
? 0
182+
: 1; // Integer encoding the DataType (0: FP32, 1: FP16)
183+
f.emplace_back(nvinfer1::PluginField("type_id", &type_id, nvinfer1::PluginFieldType::kINT32, 1));
184+
fc.nbFields = f.size();
185+
fc.fields = f.data();
186+
187+
auto creator = getPluginRegistry()->getPluginCreator("CustomGeluPluginDynamic", "1", "");
188+
auto gelu_plugin = creator->createPlugin("gelu", &fc);
189+
190+
TRTORCH_CHECK(gelu_plugin, "Unable to create gelu plugin from TensorRT plugin registry" << *n);
191+
auto new_layer =
192+
ctx->net->addPluginV2(reinterpret_cast<nvinfer1::ITensor* const*>(&in), 1, *gelu_plugin);
193+
new_layer->setName("gelu");
194+
auto out_tensor = new_layer->getOutput(0);
195+
out_tensor = ctx->AssociateValueAndTensor(n->outputs()[0], out_tensor);
196+
LOG_DEBUG("Output shape: " << out_tensor->getDimensions());
197+
return true;
170198
}});
171199

172200
} // namespace
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "NvInfer.h"
2+
#include "core/conversion/converters/converters.h"
3+
#include "core/conversion/tensorcontainer/TensorContainer.h"
4+
#include "core/util/prelude.h"
5+
#include "core/util/trt_util.h"
6+
#include "torch/torch.h"
7+
8+
#include <ATen/ATen.h>
9+
#include <vector>
10+
11+
namespace trtorch {
12+
namespace core {
13+
namespace conversion {
14+
namespace converters {
15+
namespace impl {
16+
namespace {
17+
18+
auto cumsum_registrations TRTORCH_UNUSED = RegisterNodeConversionPatterns().pattern(
19+
{"aten::cumsum(Tensor self, int dim, *, int? dtype=None) -> (Tensor)",
20+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
21+
auto in = args[0].ITensorOrFreeze(ctx);
22+
auto input_dims = in->getDimensions();
23+
int dim = args[1].unwrapToInt();
24+
TRTORCH_CHECK(
25+
(dim >= 0 && dim < input_dims.nbDims) || (dim < 0 && (input_dims.nbDims + dim >= 0)),
26+
"Dimension out of range (expected to be in range of [" << -input_dims.nbDims << ", " << input_dims.nbDims - 1
27+
<< "], but got " << dim << ")");
28+
if (dim < 0) {
29+
dim += input_dims.nbDims;
30+
}
31+
32+
// Scan through each slice across summation axis and add it to the running sum
33+
auto loop = ctx->net->addLoop();
34+
nvinfer1::ITensor* tripLimit = NULL;
35+
if (input_dims.d[dim] > 0) {
36+
torch::Tensor axis = torch::tensor(input_dims.d[dim], torch::kInt32);
37+
tripLimit = tensor_to_const(ctx, axis);
38+
} else {
39+
nvinfer1::ITensor* inpShape = ctx->net->addShape(*in)->getOutput(0);
40+
torch::Tensor dimValue = torch::tensor(dim, torch::kInt32);
41+
nvinfer1::ITensor* axis = tensor_to_const(ctx, dimValue);
42+
tripLimit = ctx->net->addGather(*inpShape, *axis, 0)->getOutput(0);
43+
}
44+
45+
loop->addTripLimit(*tripLimit, nvinfer1::TripLimit::kCOUNT);
46+
47+
auto iterator = loop->addIterator(*in, dim, false);
48+
auto data = iterator->getOutput(0);
49+
auto newDims = data->getDimensions();
50+
51+
torch::Tensor zeroValue = at::full(util::toVec(newDims), 0, torch::kFloat32);
52+
auto zeroTensor = tensor_to_const(ctx, zeroValue);
53+
auto runningSum = loop->addRecurrence(*zeroTensor);
54+
auto runningSumTensor = runningSum->getOutput(0);
55+
56+
auto curSum = ctx->net->addElementWise(*data, *runningSumTensor, nvinfer1::ElementWiseOperation::kSUM);
57+
runningSum->setInput(1, *curSum->getOutput(0));
58+
59+
nvinfer1::ILoopOutputLayer* loopOut =
60+
loop->addLoopOutput(*curSum->getOutput(0), nvinfer1::LoopOutput::kCONCATENATE, dim);
61+
loopOut->setInput(1, *tripLimit);
62+
63+
auto layer_output = ctx->AssociateValueAndTensor(n->outputs()[0], loopOut->getOutput(0));
64+
65+
LOG_DEBUG("Output tensor shape: " << layer_output->getDimensions());
66+
return true;
67+
}});
68+
69+
} // namespace
70+
} // namespace impl
71+
} // namespace converters
72+
} // namespace conversion
73+
} // namespace core
74+
} // namespace trtorch

core/conversion/converters/impl/element_wise.cpp

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <torch/torch.h>
2+
#include "core/conversion/converters/converter_util.h"
23
#include "core/conversion/converters/converters.h"
34
#include "core/util/prelude.h"
45

@@ -9,65 +10,6 @@ namespace converters {
910
namespace impl {
1011
namespace {
1112

12-
nvinfer1::ILayer* add_elementwise(
13-
ConversionCtx* ctx,
14-
nvinfer1::ElementWiseOperation op,
15-
nvinfer1::ITensor* self,
16-
nvinfer1::ITensor* other,
17-
const std::string& name) {
18-
// ensure self to have larger number of dimension
19-
bool swapSelfOther = false;
20-
if (self->getDimensions().nbDims < other->getDimensions().nbDims) {
21-
std::swap(self, other);
22-
swapSelfOther = true;
23-
}
24-
auto selfDim = util::toVec(self->getDimensions());
25-
auto otherDim = util::toVec(other->getDimensions());
26-
if (selfDim.size() != otherDim.size()) {
27-
// other is with dynamic shape, need to expand its dimension now and get its shape at runtime
28-
if (otherDim.end() != std::find(otherDim.begin(), otherDim.end(), -1)) {
29-
auto thOtherStaticShapeMask = torch::ones(selfDim.size(), torch::kInt32);
30-
auto thOtherDynamicShapeMask = torch::zeros(selfDim.size(), torch::kInt32);
31-
for (size_t start = selfDim.size() - otherDim.size(), idx = 0; idx < otherDim.size(); ++idx) {
32-
if (-1 != otherDim[idx]) {
33-
thOtherStaticShapeMask[start + idx] = otherDim[idx];
34-
} else {
35-
thOtherStaticShapeMask[start + idx] = 0;
36-
thOtherDynamicShapeMask[start + idx] = 1;
37-
}
38-
}
39-
auto otherStaticShapeMask = tensor_to_const(ctx, thOtherStaticShapeMask);
40-
auto otherDynamicShapeMask = tensor_to_const(ctx, thOtherDynamicShapeMask);
41-
auto selfShape = ctx->net->addShape(*self)->getOutput(0);
42-
// size of dynamic dimension of other need to the same as that of corresponding dimension of self
43-
auto otherDynamicShape =
44-
ctx->net->addElementWise(*selfShape, *otherDynamicShapeMask, nvinfer1::ElementWiseOperation::kPROD)
45-
->getOutput(0);
46-
auto targetOtherShape =
47-
ctx->net->addElementWise(*otherDynamicShape, *otherStaticShapeMask, nvinfer1::ElementWiseOperation::kSUM)
48-
->getOutput(0);
49-
50-
auto otherShuffle = ctx->net->addShuffle(*other);
51-
otherShuffle->setName(std::string("Reshape other tensor to have the same nDim as self for " + name).c_str());
52-
otherShuffle->setInput(1, *targetOtherShape);
53-
other = otherShuffle->getOutput(0);
54-
} else {
55-
// other is with static shape, expand dimension to make tow tensor have the same number of dimension
56-
auto otherShuffle = ctx->net->addShuffle(*other);
57-
otherShuffle->setReshapeDimensions(util::toDimsPad(otherDim, selfDim.size()));
58-
other = otherShuffle->getOutput(0);
59-
}
60-
}
61-
if (swapSelfOther) {
62-
// swap back
63-
std::swap(self, other);
64-
swapSelfOther = false;
65-
}
66-
auto ele = ctx->net->addElementWise(*self, *other, op);
67-
ele->setName(name.c_str());
68-
return ele;
69-
}
70-
7113
nvinfer1::ITensor* clamp_util(
7214
ConversionCtx* ctx,
7315
const torch::jit::Node* n,

0 commit comments

Comments
 (0)