Skip to content

Commit b09994d

Browse files
committed
Merge branch 'master' into param_input
2 parents ce63baa + fe966ed commit b09994d

File tree

140 files changed

+6712
-1474
lines changed

Some content is hidden

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

140 files changed

+6712
-1474
lines changed

.github/code-owners.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
"component: build system":
1111
- "narendasan"
12-
- "andi4191"
1312

1413
"component: conversion":
1514
- "narendasan"
@@ -29,7 +28,6 @@
2928
- "peri044"
3029

3130
"component: execution":
32-
- "andi4191"
3331
- "narendasan"
3432

3533
"component: lowering":
@@ -48,15 +46,12 @@
4846
- "peri044"
4947

5048
"component: runtime":
51-
- "andi4191"
5249
- "narendasan"
5350

5451
"component: tests":
55-
- "andi4191"
5652
- "narendasan"
5753

5854
"component: torchtrtc":
59-
- "andi4191"
6055
- "narendasan"
6156

6257
"component: dependencies":
@@ -74,24 +69,20 @@
7469
- "tanayvarshney"
7570

7671
"infrastructre":
77-
- "andi4191"
7872
- "narendasan"
7973

8074
"component: packaging":
8175
- "narendasan"
82-
- "andi4191"
8376
- "peri044"
8477

8578
"channel: NGC":
86-
- "andi4191"
8779
- "peri044"
8880

8981
"channel: linux-x86":
9082
- "narendasan"
9183
- "peri044"
9284

9385
"channel: linux-sbsa":
94-
- "andi4191"
9586
- "bowang007"
9687

9788
"channel: windows":
@@ -102,16 +93,13 @@
10293
- "bowang007"
10394

10495
"component: tooling":
105-
- "andi4191"
10696
- "narendasan"
10797

10898
"performance":
109-
- "andi4191"
11099
- "peri044"
111100
- "bowang007"
112101

113102
"channel: docker":
114-
- "andi4191"
115103
- "narendasan"
116104

117105
"ux":

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ These are the following dependencies used to verify the testcases. Torch-TensorR
122122

123123
Releases: https://github.com/pytorch/TensorRT/releases
124124

125+
```
126+
pip install torch-tensorrt==1.2.0 --find-links https://github.com/pytorch/TensorRT/releases/expanded_assets/v1.2.0
127+
```
128+
125129
## Compiling Torch-TensorRT
126130

127131
### Installing Dependencies

core/conversion/converters/impl/einsum.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ auto einsum_registrations TORCHTRT_UNUSED = RegisterNodeConversionPatterns().pat
1818
auto equation = args[0].unwrapToString();
1919
auto in = args[1].IValue()->toListRef();
2020

21+
TORCHTRT_CHECK(
22+
in.size() <= 2,
23+
"TensorRT currently supports up to 2 input tensors "
24+
<< "to einsum but operation had " << in.size()
25+
<< " input tensors, please specify torch_executed_ops=[\"aten::einsum\"] "
26+
<< "at compilation time to avoid this error.");
27+
2128
std::vector<nvinfer1::ITensor*> tensors;
2229

2330
// Populate vector of ITensor pointers

core/conversion/converters/impl/element_wise.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ auto element_wise_registrations TORCHTRT_UNUSED =
166166
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
167167
// Should implement self - alpha * other
168168
auto self = args[0].ITensorOrFreeze(ctx);
169-
auto scalar = args[2].unwrapToScalar().to<float>();
170169
auto other = args[1].ITensorOrFreeze(ctx);
170+
auto scalar = args[2].unwrapToScalar();
171171

172-
if (1 != scalar) {
173-
auto alphaTensor = tensor_to_const(ctx, torch::tensor({scalar}));
172+
if (1 != scalar.to<float>()) {
173+
auto alphaTensor = scalar_to_tensor(ctx, scalar);
174174
auto scaleLayer = add_elementwise(
175175
ctx,
176176
nvinfer1::ElementWiseOperation::kPROD,
@@ -214,11 +214,11 @@ auto element_wise_registrations TORCHTRT_UNUSED =
214214
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
215215
// Should implement self - alpha * other
216216
auto self = args[0].ITensorOrFreeze(ctx);
217-
auto scalar = args[2].unwrapToScalar().to<float>();
218217
auto other = args[1].ITensorOrFreeze(ctx);
218+
auto scalar = args[2].unwrapToScalar();
219219

220-
if (1 != scalar) {
221-
auto alphaTensor = tensor_to_const(ctx, torch::tensor({scalar}));
220+
if (1 != scalar.to<float>()) {
221+
auto alphaTensor = scalar_to_tensor(ctx, scalar);
222222
auto scaleLayer = add_elementwise(
223223
ctx,
224224
nvinfer1::ElementWiseOperation::kPROD,
@@ -351,8 +351,7 @@ auto element_wise_registrations TORCHTRT_UNUSED =
351351
{"aten::div.Scalar(Tensor self, Scalar other) -> (Tensor)",
352352
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
353353
auto self = args[0].ITensorOrFreeze(ctx);
354-
auto otherScalar = args[1].unwrapToScalar().to<float>();
355-
auto other = tensor_to_const(ctx, torch::tensor({otherScalar}));
354+
auto other = scalar_to_tensor(ctx, args[1].unwrapToScalar());
356355
auto div = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kDIV, self, other, util::node_info(n));
357356
TORCHTRT_CHECK(div, "Unable to create div layer from node: " << *n);
358357

@@ -381,8 +380,7 @@ auto element_wise_registrations TORCHTRT_UNUSED =
381380
{"aten::div_.Scalar(Tensor(a!) self, Scalar other) -> Tensor(a!)",
382381
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
383382
auto self = args[0].ITensorOrFreeze(ctx);
384-
auto otherScalar = args[1].unwrapToScalar().to<float>();
385-
auto other = tensor_to_const(ctx, torch::tensor({otherScalar}));
383+
auto other = scalar_to_tensor(ctx, args[1].unwrapToScalar());
386384
auto div = add_elementwise(ctx, nvinfer1::ElementWiseOperation::kDIV, self, other, util::node_info(n));
387385
TORCHTRT_CHECK(div, "Unable to create div layer from node: " << *n);
388386

@@ -481,18 +479,12 @@ auto element_wise_registrations TORCHTRT_UNUSED =
481479
{"aten::ne.Scalar(Tensor self, Scalar other) -> (Tensor)",
482480
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
483481
auto self = args[0].ITensorOrFreeze(ctx);
484-
auto scalar = args[1].unwrapToScalar();
485-
nvinfer1::ITensor* scalar_tensor;
486-
if (self->getType() == nvinfer1::DataType::kFLOAT || self->getType() == nvinfer1::DataType::kHALF) {
487-
scalar_tensor = tensor_to_const(ctx, torch::tensor({scalar.to<float>()}));
488-
} else {
489-
scalar_tensor = tensor_to_const(ctx, torch::tensor({scalar.to<int>()}));
490-
}
482+
auto other = scalar_to_tensor(ctx, args[1].unwrapToScalar());
491483
auto equal = add_elementwise(
492484
ctx,
493485
nvinfer1::ElementWiseOperation::kEQUAL,
494486
self,
495-
scalar_tensor,
487+
other,
496488
util::node_info(n) + std::string("is_equal"));
497489
TORCHTRT_CHECK(equal, "Unable to create elementwise equal layer from node: " << *n);
498490
// XOR with ones negates and produces not_equal result
@@ -534,8 +526,7 @@ auto element_wise_registrations TORCHTRT_UNUSED =
534526
{"aten::pow.Tensor_Scalar(Tensor self, Scalar exponent) -> (Tensor)",
535527
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
536528
auto self = args[0].ITensorOrFreeze(ctx);
537-
auto exponentScalar = args[1].unwrapToScalar().to<float>();
538-
auto exponent = tensor_to_const(ctx, torch::tensor({exponentScalar}));
529+
auto exponent = scalar_to_tensor(ctx, args[1].unwrapToScalar());
539530
auto pow =
540531
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kPOW, self, exponent, util::node_info(n));
541532
TORCHTRT_CHECK(pow, "Unable to create Power layer from node: " << *n);
@@ -681,9 +672,9 @@ auto element_wise_registrations TORCHTRT_UNUSED =
681672
{"aten::eq.Scalar(Tensor self, Scalar other) -> (Tensor)",
682673
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
683674
auto self = args[0].ITensorOrFreeze(ctx);
684-
auto otherScalar = args[1].unwrapToScalar().to<float>();
685-
auto other = tensor_to_const(ctx, torch::tensor({otherScalar}));
675+
auto other = scalar_to_tensor(ctx, args[1].unwrapToScalar());
686676
if (self->getType() == nvinfer1::DataType::kBOOL) {
677+
auto otherScalar = args[1].unwrapToScalar().to<float>();
687678
if (otherScalar == 0 || otherScalar == 1) {
688679
LOG_DEBUG("Since input tensor is type bool, casting input tensor and scalar to int32");
689680
other = castITensor(ctx, other, nvinfer1::DataType::kINT32);

core/conversion/converters/impl/normalize.cpp

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,118 @@ void create_plugin(
5353
LOG_DEBUG("Normalize layer output tensor shape: " << layer_output->getDimensions());
5454
}
5555

56-
auto normalize_registrations TORCHTRT_UNUSED = RegisterNodeConversionPatterns().pattern(
57-
{"aten::norm.ScalarOpt_dim(Tensor self, Scalar? p, int[1] dim, bool keepdim=False) -> (Tensor)",
58-
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
59-
auto in = args[0].ITensor();
60-
auto in_shape = util::toVec(in->getDimensions());
61-
auto order = args[1].unwrapToScalar().to<int32_t>();
62-
auto axes_values = args[2].unwrapToIntList().vec();
63-
std::vector<int32_t> axes(axes_values.begin(), axes_values.end());
64-
auto keep_dims = (int32_t)args[3].unwrapToBool();
65-
LOG_DEBUG("Order of normalize_plugin: " << order);
66-
LOG_DEBUG("Axis: " << axes);
67-
LOG_DEBUG("keep_dims: " << keep_dims);
68-
create_plugin(ctx, n, in, order, axes, keep_dims, "NormalizePluginTorchTRT");
69-
return true;
70-
}
71-
72-
});
56+
int32_t axes_mask_from_axes_values(
57+
const torch::jit::Node* n,
58+
int32_t nb_dims,
59+
const std::vector<int64_t>& axes_values) {
60+
int32_t axes_mask = 0;
61+
for (size_t i = 0UL; i < axes_values.size(); ++i) {
62+
auto axis = axes_values[i];
63+
if (axis < 0) {
64+
axis += nb_dims;
65+
}
66+
TORCHTRT_CHECK(
67+
axis < nb_dims, util::node_info(n) << " axis " << i << " with value: " << axis << " exceeds input rank");
68+
axes_mask += 1 << axis;
69+
}
70+
return axes_mask;
71+
}
72+
73+
nvinfer1::ITensor* frobenius_norm(
74+
ConversionCtx* ctx,
75+
const torch::jit::Node* n,
76+
nvinfer1::ITensor* self,
77+
int32_t axes_mask,
78+
bool keep_dims) {
79+
auto squared_layer =
80+
add_elementwise(ctx, nvinfer1::ElementWiseOperation::kPROD, self, self, util::node_info(n) + "_squared");
81+
TORCHTRT_CHECK(squared_layer, "Unabled to create square layer from node: " << *n);
82+
auto squared_output = squared_layer->getOutput(0);
83+
84+
auto sum_layer = ctx->net->addReduce(*squared_output, nvinfer1::ReduceOperation::kSUM, axes_mask, keep_dims);
85+
TORCHTRT_CHECK(sum_layer, "Unable to create sum layer from node: " << *n);
86+
sum_layer->setName((util::node_info(n) + "_sum").c_str());
87+
auto sum_output = sum_layer->getOutput(0);
88+
LOG_DEBUG("SUM SHAPE: " << sum_output->getDimensions());
89+
90+
auto sqrt_layer = ctx->net->addUnary(*sum_output, nvinfer1::UnaryOperation::kSQRT);
91+
TORCHTRT_CHECK(sqrt_layer, "Unable to create sqrt layer from node: " << *n);
92+
sqrt_layer->setName((util::node_info(n) + "_sqrt").c_str());
93+
auto sqrt_output = sqrt_layer->getOutput(0);
94+
return sqrt_output;
95+
}
96+
97+
auto normalize_registrations TORCHTRT_UNUSED =
98+
RegisterNodeConversionPatterns()
99+
.pattern(
100+
{"aten::norm.ScalarOpt_dim(Tensor self, Scalar? p, int[1] dim, bool keepdim=False) -> (Tensor)",
101+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
102+
auto in = args[0].ITensorOrFreeze(ctx);
103+
auto in_shape = util::toVec(in->getDimensions());
104+
auto order = args[1].unwrapToScalar().to<int32_t>();
105+
auto axes_values = args[2].unwrapToIntList().vec();
106+
std::vector<int32_t> axes(axes_values.begin(), axes_values.end());
107+
auto keep_dims = (int32_t)args[3].unwrapToBool();
108+
LOG_DEBUG("Order of normalize_plugin: " << order);
109+
LOG_DEBUG("Axis: " << axes);
110+
LOG_DEBUG("keep_dims: " << keep_dims);
111+
create_plugin(ctx, n, in, order, axes, keep_dims, "NormalizePluginTorchTRT");
112+
return true;
113+
}
114+
115+
})
116+
.pattern(
117+
{"aten::frobenius_norm.dim(Tensor self, int[1] dim, bool keepdim=False) -> (Tensor)",
118+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
119+
auto self = args[0].ITensorOrFreeze(ctx);
120+
auto axes_values = args[1].unwrapToIntList().vec();
121+
auto keep_dims = args[2].unwrapToBool();
122+
123+
auto axes_mask = axes_mask_from_axes_values(n, self->getDimensions().nbDims, axes_values);
124+
125+
auto norm = frobenius_norm(ctx, n, self, axes_mask, keep_dims);
126+
auto out = ctx->AssociateValueAndTensor(n->outputs()[0], norm);
127+
LOG_DEBUG("Output tensor shape: " << out->getDimensions());
128+
return true;
129+
}})
130+
.pattern(
131+
{"aten::linalg_norm(Tensor self, Scalar? ord=None, int[1]? dim=None, bool keepdim=False, *, int? dtype=None) -> (Tensor)",
132+
[](ConversionCtx* ctx, const torch::jit::Node* n, args& args) -> bool {
133+
// https://pytorch.org/docs/stable/generated/torch.linalg.norm.html
134+
auto self = args[0].ITensorOrFreeze(ctx);
135+
TORCHTRT_CHECK(
136+
args[1].IValue()->isNone(),
137+
"aten::linalg_norm converter does not yet support non-None 'ord' arguments. Add aten::linalg_norm to torch_executed_ops to force it to fallback.");
138+
auto keep_dims = args[3].unwrapToBool();
139+
auto self_nb_dims = self->getDimensions().nbDims;
140+
141+
if (!args.back().IValue()->isNone()) {
142+
// If specified, the input tensor is cast to dtype before performing the operation, and the returned
143+
// tensor’s type will be dtype
144+
auto dtype = args.back().unwrapToScalar().to<int64_t>();
145+
auto trt_dtype = util::ScalarTypeToTRTDataType(static_cast<at::ScalarType>(dtype));
146+
self = castITensor(ctx, self, trt_dtype);
147+
}
148+
149+
int32_t axes_mask = 0;
150+
if (args[2].IValue()->isNone()) {
151+
// If dim= None and ord= None, self will be flattened to 1D and the 2-norm of the resulting vector will
152+
// be computed.
153+
axes_mask = 1;
154+
keep_dims = true; // the single output dim is always preserved
155+
auto flatten_layer = ctx->net->addShuffle(*self);
156+
TORCHTRT_CHECK(flatten_layer, "Unable to create shuffle layer from node: " << *n);
157+
flatten_layer->setReshapeDimensions(util::toDims(std::vector<int64_t>({-1})));
158+
flatten_layer->setName((util::node_info(n) + "_flatten").c_str());
159+
self = flatten_layer->getOutput(0);
160+
} else {
161+
axes_mask = axes_mask_from_axes_values(n, self_nb_dims, args[2].unwrapToIntList().vec());
162+
}
163+
auto norm = frobenius_norm(ctx, n, self, axes_mask, keep_dims);
164+
auto out = ctx->AssociateValueAndTensor(n->outputs()[0], norm);
165+
LOG_DEBUG("Output tensor shape: " << out->getDimensions());
166+
return true;
167+
}});
73168

74169
} // namespace
75170
} // namespace impl

0 commit comments

Comments
 (0)