Skip to content

Commit 5660f55

Browse files
committed
Merge remote-tracking branch 'origin/master' into plugins
2 parents 5db928d + 72cb449 commit 5660f55

Some content is hidden

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

52 files changed

+1466
-146
lines changed

.github/scripts/run_cpp_linter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626

2727
pr.create_review(commit, comment, approval)
2828

29-
29+
if output.returncode != 0:
30+
exit(1)
31+
else:
32+
exit(0)

.github/scripts/run_py_linter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@
2525
approval = 'REQUEST_CHANGES'
2626

2727
pr.create_review(commit, comment, approval)
28+
29+
if output.returncode != 0:
30+
exit(1)
31+
else:
32+
exit(0)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ torch.jit.save(trt_ts_module, "trt_torchscript_module.ts")
7474
These are the following dependencies used to verify the testcases. TRTorch can work with other versions, but the tests are not guaranteed to pass.
7575

7676
- Bazel 4.0.0
77-
- Libtorch 1.8.0 (built with CUDA 11.1)
77+
- Libtorch 1.8.1 (built with CUDA 11.1)
7878
- CUDA 11.1 (10.2 on Jetson)
7979
- cuDNN 8.1
8080
- TensorRT 7.2.3

WORKSPACE

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,21 @@ workspace(name = "TRTorch")
33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
55

6-
git_repository(
7-
name = "rules_python",
8-
remote = "https://github.com/bazelbuild/rules_python.git",
9-
commit = "4fcc24fd8a850bdab2ef2e078b1de337eea751a6",
10-
shallow_since = "1589292086 -0400"
11-
)
12-
13-
load("@rules_python//python:repositories.bzl", "py_repositories")
14-
py_repositories()
6+
http_archive(
7+
name = "rules_python",
8+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.2.0/rules_python-0.2.0.tar.gz",
9+
sha256 = "778197e26c5fbeb07ac2a2c5ae405b30f6cb7ad1f5510ea6fdac03bded96cc6f",
10+
)
1511

16-
load("@rules_python//python:pip.bzl", "pip_repositories", "pip3_import")
17-
pip_repositories()
12+
load("@rules_python//python:pip.bzl", "pip_install")
1813

1914
http_archive(
2015
name = "rules_pkg",
21-
url = "https://github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
22-
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
16+
urls = [
17+
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.4.0/rules_pkg-0.4.0.tar.gz",
18+
"https://github.com/bazelbuild/rules_pkg/releases/download/0.4.0/rules_pkg-0.4.0.tar.gz",
19+
],
20+
sha256 = "038f1caa773a7e35b3663865ffb003169c6a71dc995e39bf4815792f385d837d",
2321
)
2422

2523
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
@@ -39,12 +37,6 @@ new_local_repository(
3937
build_file = "@//third_party/cuda:BUILD",
4038
)
4139

42-
new_local_repository(
43-
name = "cublas",
44-
path = "/usr",
45-
build_file = "@//third_party/cublas:BUILD",
46-
)
47-
4840
#############################################################################################################
4941
# Tarballs and fetched dependencies (default - use in cases when building from precompiled bin and tarballs)
5042
#############################################################################################################
@@ -53,16 +45,16 @@ http_archive(
5345
name = "libtorch",
5446
build_file = "@//third_party/libtorch:BUILD",
5547
strip_prefix = "libtorch",
56-
sha256 = "62a2c06761c32576b30f5884240cf675b937945d929e4b13cc776de8d9c2236c",
57-
urls = ["https://download.pytorch.org/libtorch/cu111/libtorch-cxx11-abi-shared-with-deps-1.8.0%2Bcu111.zip"],
48+
sha256 = "1f8aec376f9343538bd7c2fd3abb81ed3af11f575efe3aa72777c4d62044b832",
49+
urls = ["https://download.pytorch.org/libtorch/cu111/libtorch-cxx11-abi-shared-with-deps-1.8.1%2Bcu111.zip"],
5850
)
5951

6052
http_archive(
6153
name = "libtorch_pre_cxx11_abi",
6254
build_file = "@//third_party/libtorch:BUILD",
6355
strip_prefix = "libtorch",
64-
sha256 = "1c8b0c0883dd17f5ce952d42ec5f7f0cc7ceb370307535cee26a66c10419f1f6",
65-
urls = ["https://download.pytorch.org/libtorch/cu111/libtorch-shared-with-deps-1.8.0%2Bcu111.zip"],
56+
sha256 = "3a6e0dc11859111e75caa640c8ce9bf904fbb6e9992b4345e444ed5410e4d77e",
57+
urls = ["https://download.pytorch.org/libtorch/cu111/libtorch-shared-with-deps-1.8.1%2Bcu111.zip"],
6658
)
6759

6860
# Download these tarballs manually from the NVIDIA website
@@ -71,15 +63,19 @@ http_archive(
7163

7264
http_archive(
7365
name = "cudnn",
74-
urls = ["https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.1.1.33/11.2_20210301/cudnn-11.2-linux-x64-v8.1.1.33.tgz",],
66+
urls = [
67+
"https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.1.1.33/11.2_20210301/cudnn-11.2-linux-x64-v8.1.1.33.tgz",
68+
],
7569
build_file = "@//third_party/cudnn/archive:BUILD",
7670
sha256 = "98a8784e92862f20018d20c281b30d4a0cd951f93694f6433ccf4ae9c502ba6a",
7771
strip_prefix = "cuda"
7872
)
7973

8074
http_archive(
8175
name = "tensorrt",
82-
urls = ["https://developer.nvidia.com/compute/machine-learning/tensorrt/secure/7.2.3/tars/TensorRT-7.2.3.4.Ubuntu-18.04.x86_64-gnu.cuda-11.1.cudnn8.1.tar.gz",],
76+
urls = [
77+
"https://developer.nvidia.com/compute/machine-learning/tensorrt/secure/7.2.3/tars/TensorRT-7.2.3.4.Ubuntu-18.04.x86_64-gnu.cuda-11.1.cudnn8.1.tar.gz",
78+
],
8379
build_file = "@//third_party/tensorrt/archive:BUILD",
8480
strip_prefix = "TensorRT-7.2.3.4",
8581
sha256 = "d3a1f478e304b48878604fac70ce7920fece71f9cac62f925c9c59c197f5d087"
@@ -123,26 +119,17 @@ http_archive(
123119
#########################################################################
124120
# Testing Dependencies (optional - comment out on aarch64)
125121
#########################################################################
126-
pip3_import(
122+
pip_install(
127123
name = "trtorch_py_deps",
128-
requirements = "//py:requirements.txt"
124+
requirements = "//py:requirements.txt",
129125
)
130126

131-
load("@trtorch_py_deps//:requirements.bzl", "pip_install")
132-
pip_install()
133-
134-
pip3_import(
127+
pip_install(
135128
name = "py_test_deps",
136-
requirements = "//tests/py:requirements.txt"
129+
requirements = "//tests/py:requirements.txt",
137130
)
138131

139-
load("@py_test_deps//:requirements.bzl", "pip_install")
140-
pip_install()
141-
142-
pip3_import(
143-
name = "pylinter_deps",
144-
requirements = "//tools/linter:requirements.txt",
132+
pip_install(
133+
name = "pylinter_deps",
134+
requirements = "//tools/linter:requirements.txt",
145135
)
146-
147-
load("@pylinter_deps//:requirements.bzl", "pip_install")
148-
pip_install()

core/compiler.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ c10::FunctionSchema GenerateGraphSchema(
4646
void AddEngineToGraph(
4747
torch::jit::script::Module mod,
4848
std::shared_ptr<torch::jit::Graph>& g,
49-
std::string& serialized_engine) {
49+
const std::string& serialized_engine) {
5050
auto engine_ptr = c10::make_intrusive<runtime::TRTEngine>(mod._ivalue()->name(), serialized_engine);
5151
// Get required metadata about the engine out
5252
auto num_io = engine_ptr->num_io;
@@ -173,6 +173,20 @@ torch::jit::script::Module CompileGraph(const torch::jit::script::Module& mod, C
173173
return new_mod;
174174
}
175175

176+
torch::jit::script::Module EmbedEngineInNewModule(const std::string& engine) {
177+
std::ostringstream engine_id;
178+
engine_id << reinterpret_cast<const int*>(&engine);
179+
torch::jit::script::Module new_mod("tensorrt_engine_mod_" + engine_id.str());
180+
auto new_g = std::make_shared<torch::jit::Graph>();
181+
AddEngineToGraph(new_mod, new_g, engine);
182+
auto new_method = new_mod._ivalue()->compilation_unit()->create_function("forward", new_g);
183+
auto schema = GenerateGraphSchema(new_mod, new_method->name(), new_g);
184+
new_mod.type()->addMethod(new_method);
185+
new_method->setSchema(schema);
186+
187+
return new_mod;
188+
}
189+
176190
void set_device(const int gpu_id) {
177191
TRTORCH_ASSERT(cudaSetDevice(gpu_id) == cudaSuccess, "Unable to set CUDA device: " << gpu_id);
178192
}

core/compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ std::string ConvertGraphToTRTEngine(const torch::jit::script::Module& mod, std::
1919

2020
torch::jit::script::Module CompileGraph(const torch::jit::script::Module& module, CompileSpec cfg);
2121

22+
torch::jit::script::Module EmbedEngineInNewModule(const std::string& engine);
23+
2224
void set_device(const int gpu_id);
2325

2426
} // namespace core

core/conversion/conversionctx/ConversionCtx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ std::ostream& operator<<(std::ostream& os, const BuilderSettings& s) {
1212
os << "Settings requested for TensorRT engine:" \
1313
<< "\n Operating Precision: " << s.op_precision \
1414
<< "\n TF32 Floating Point Computation Enabled: " << !s.disable_tf32 \
15+
<< "\n Truncate Long and Double: " << s.truncate_long_and_double \
1516
<< "\n Make Refittable Engine: " << s.refit \
1617
<< "\n Debuggable Engine: " << s.debug \
1718
<< "\n Strict Types: " << s.strict_types \

core/conversion/converters/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cc_library(
4545
"impl/normalize.cpp",
4646
"impl/pooling.cpp",
4747
"impl/reduce.cpp",
48+
"impl/replication_pad.cpp",
4849
"impl/shuffle.cpp",
4950
"impl/softmax.cpp",
5051
"impl/unary.cpp",
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include <ATen/ATen.h>
2+
#include <vector>
3+
#include "NvInfer.h"
4+
#include "core/conversion/converters/converters.h"
5+
#include "core/util/prelude.h"
6+
#include "torch/torch.h"
7+
8+
namespace trtorch {
9+
namespace core {
10+
namespace conversion {
11+
namespace converters {
12+
namespace impl {
13+
namespace {
14+
15+
bool replication_padXd(ConversionCtx* ctx, const torch::jit::Node* n, args& args, int x_dim) {
16+
auto in = args[0].ITensor();
17+
auto inDims = in->getDimensions();
18+
int64_t inRank = inDims.nbDims;
19+
auto padding = args[1].unwrapToIntList().vec();
20+
if (padding.size() == 1) {
21+
for (int64_t i = 0; i < x_dim * 2 - 1; i++)
22+
padding.push_back(padding[0]);
23+
}
24+
if (inRank == 3) {
25+
TRTORCH_CHECK(padding.size() == 2, "3D tensors expect 2 values for padding");
26+
} else if (inRank == 4) {
27+
TRTORCH_CHECK(padding.size() == 4, "4D tensors expect 4 values for padding");
28+
} else if (inRank == 5) {
29+
TRTORCH_CHECK(padding.size() == 6, "5D tensors expect 6 values for padding");
30+
} else {
31+
TRTORCH_THROW_ERROR("Only 3D, 4D, 5D padding with non-constant padding are supported for now");
32+
}
33+
34+
std::vector<nvinfer1::ITensor*> tensors_vec;
35+
// input: (N, C, D_in, H_in, W_in).
36+
// padding: (padding_left, padding_right, padding_top, padding_bottom, padding_front, padding_back)
37+
// When axis is inRank - 1, making W_out = W_in + padding_left + padding_right.
38+
// When axis is inRank - 2, making H_out = H_in + padding_top + padding_bottom.
39+
// When axis is inRank - 1, making D_out = D_in + padding_front + padding_back.
40+
for (int64_t i = 0; i < int(padding.size() / 2); i++) {
41+
int64_t axis = inRank - (i + 1); // axis = {inRank - 1, inRank - 2, inRank - 3}
42+
int64_t padding_index = i * 2;
43+
44+
if (padding[padding_index] > 0) { // left/top/front padding value
45+
tensors_vec.clear();
46+
at::Tensor left_indices = torch::tensor({0}, torch::kInt32);
47+
auto indicesTensor = tensor_to_const(ctx, left_indices);
48+
auto left_gather_layer = ctx->net->addGather(*in, *indicesTensor, axis);
49+
auto left_gather_out = left_gather_layer->getOutput(0);
50+
for (int i = 0; i < padding[padding_index]; i++) {
51+
tensors_vec.push_back(left_gather_out);
52+
}
53+
tensors_vec.push_back(in);
54+
auto concat_layer = ctx->net->addConcatenation(tensors_vec.data(), tensors_vec.size());
55+
concat_layer->setAxis(axis);
56+
in = concat_layer->getOutput(0);
57+
inDims = in->getDimensions();
58+
}
59+
60+
if (padding[padding_index + 1] > 0) { // right/bottom/back padding value
61+
tensors_vec.clear();
62+
tensors_vec.push_back(in);
63+
64+
nvinfer1::ITensor* indicesTensor = NULL;
65+
if (inDims.d[axis] == -1) {
66+
auto shapeTensor = ctx->net->addShape(*in)->getOutput(0);
67+
at::Tensor dimValue = torch::tensor({axis}, torch::kInt32);
68+
auto dimTensor = tensor_to_const(ctx, dimValue);
69+
indicesTensor = ctx->net->addGather(*shapeTensor, *dimTensor, 0)->getOutput(0);
70+
} else {
71+
auto indices = torch::tensor({inDims.d[axis] - 1}, torch::kInt32);
72+
indicesTensor = tensor_to_const(ctx, indices);
73+
}
74+
auto right_gather_layer = ctx->net->addGather(*in, *indicesTensor, axis);
75+
auto right_gather_out = right_gather_layer->getOutput(0);
76+
77+
for (int i = 0; i < padding[padding_index + 1]; i++) {
78+
tensors_vec.push_back(right_gather_out);
79+
}
80+
81+
auto concat_layer = ctx->net->addConcatenation(tensors_vec.data(), tensors_vec.size());
82+
concat_layer->setAxis(axis);
83+
in = concat_layer->getOutput(0);
84+
inDims = in->getDimensions();
85+
}
86+
}
87+
88+
auto out = ctx->AssociateValueAndTensor(n->outputs()[0], in);
89+
LOG_DEBUG("Output tensor shape: " << out->getDimensions());
90+
91+
return true;
92+
}
93+
94+
auto replication_pad_registrations TRTORCH_UNUSED =
95+
RegisterNodeConversionPatterns()
96+
.pattern({"aten::replication_pad1d(Tensor self, int[2] padding) -> (Tensor)",
97+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
98+
replication_padXd(ctx, n, args, 1);
99+
return true;
100+
}})
101+
.pattern({"aten::replication_pad2d(Tensor self, int[4] padding) -> (Tensor)",
102+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
103+
replication_padXd(ctx, n, args, 2);
104+
return true;
105+
}})
106+
.pattern({"aten::replication_pad3d(Tensor self, int[6] padding) -> (Tensor)",
107+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
108+
replication_padXd(ctx, n, args, 3);
109+
return true;
110+
}});
111+
112+
} // namespace
113+
} // namespace impl
114+
} // namespace converters
115+
} // namespace conversion
116+
} // namespace core
117+
} // namespace trtorch

0 commit comments

Comments
 (0)