-
Notifications
You must be signed in to change notification settings - Fork 545
/
RNNHelpers.cpp
180 lines (157 loc) · 8.29 KB
/
RNNHelpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include "RNNHelpers.hpp"
#include "LoopHelpers.hpp"
#include "importerUtils.hpp"
#include <array>
namespace onnx2trt
{
nvinfer1::ITensor* addRNNInput(ImporterContext* ctx, const ::ONNX_NAMESPACE::NodeProto& node, nvinfer1::ILoop* loop,
std::vector<TensorOrWeights>& inputs, const std::string& direction)
{
// In the forward/reverse cases, we only use a single iterator. In the bidirectional case, a forward and reverse
// iterator must be concatenated.
// Input dimensions: [1, B, E]
nvinfer1::ITensor* iterationInput{nullptr};
nvinfer1::ITensor* input = &convertToTensor(inputs.at(0), ctx);
const int sequenceLenIndex = 4;
bool isRagged = inputs.size() > sequenceLenIndex && inputs.at(sequenceLenIndex);
if (direction == "forward")
{
iterationInput = unsqueezeTensor(ctx, node, *N_CHECK(loop->addIterator(*input)->getOutput(0)), std::vector<int>{0});
if (isRagged)
{
nvinfer1::ITensor* seqLens = &convertToTensor(inputs.at(sequenceLenIndex), ctx);
auto maxLen = getAxisLength(ctx, input, 0);
iterationInput = clearMissingSequenceElements(ctx, node, loop, seqLens, iterationInput, maxLen);
}
}
else if (direction == "reverse")
{
nvinfer1::IIteratorLayer* reverseIterator = N_CHECK(loop->addIterator(*input));
reverseIterator->setReverse(true);
auto reverseIteratorOutput = N_CHECK(reverseIterator->getOutput(0));
iterationInput = unsqueezeTensor(ctx, node, *reverseIteratorOutput, std::vector<int>{0});
if (isRagged)
{
nvinfer1::ITensor* seqLens = &convertToTensor(inputs.at(sequenceLenIndex), ctx);
auto maxLen = getAxisLength(ctx, input, 0);
iterationInput = clearMissingSequenceElements(ctx, node, loop, seqLens, iterationInput, maxLen, true);
}
}
else if (direction == "bidirectional")
{
nvinfer1::IIteratorLayer* forward = N_CHECK(loop->addIterator(*input));
nvinfer1::IIteratorLayer* reverse = N_CHECK(loop->addIterator(*input));
reverse->setReverse(true);
auto forwardInput = unsqueezeTensor(ctx, node, *N_CHECK(forward->getOutput(0)), std::vector<int>{0});
auto reverseInput = unsqueezeTensor(ctx, node, *N_CHECK(reverse->getOutput(0)), std::vector<int>{0});
if (isRagged)
{
nvinfer1::ITensor* seqLens = &convertToTensor(inputs.at(sequenceLenIndex), ctx);
auto counter = addLoopCounter(ctx, loop);
auto maxLen = getAxisLength(ctx, input, 0);
forwardInput = clearMissingSequenceElements(ctx, node, loop, seqLens, forwardInput, maxLen, false, counter);
reverseInput = clearMissingSequenceElements(ctx, node, loop, seqLens, reverseInput, maxLen, true, counter);
}
// Stack on the 0th axis to create a (numDirections, B, E) tensor.
std::array<nvinfer1::ITensor*, 2> tensors{{forwardInput, reverseInput}};
nvinfer1::IConcatenationLayer* concat = N_CHECK(ctx->network()->addConcatenation(tensors.data(), 2));
concat->setAxis(0);
iterationInput = N_CHECK(concat->getOutput(0));
}
if (iterationInput)
{
LOG_VERBOSE("Input shape: " << iterationInput->getDimensions());
}
return iterationInput;
}
nvinfer1::ITensor* clearMissingSequenceElements(ImporterContext* ctx, const ::ONNX_NAMESPACE::NodeProto& node,
nvinfer1::ILoop* loop, nvinfer1::ITensor* seqLens, nvinfer1::ITensor* toMask, nvinfer1::ITensor* maxLen,
bool reverse, nvinfer1::ITensor* counter)
{
nvinfer1::ITensor* zero
= addConstantScalar(ctx, 0.f, ::ONNX_NAMESPACE::TensorProto::FLOAT, nvinfer1::Dims3(1, 1, 1))->getOutput(0);
nvinfer1::ITensor* seqMask = getRaggedMask(ctx, node, loop, seqLens, maxLen, reverse, counter);
auto selectLayer = N_CHECK(ctx->network()->addSelect(*seqMask, *toMask, *zero));
return N_CHECK(selectLayer->getOutput(0));
}
nvinfer1::ITensor* maskRNNHidden(ImporterContext* ctx, const ::ONNX_NAMESPACE::NodeProto& node, nvinfer1::ILoop* loop,
nvinfer1::ITensor* seqLens, nvinfer1::ITensor* prevH, nvinfer1::ITensor* Ht, nvinfer1::ITensor* maxLen,
bool reverse, nvinfer1::ITensor* counter)
{
// maxLen must be provided if reverse is true
// Forwards previous hidden state if invalid
nvinfer1::ITensor* valid = getRaggedMask(ctx, node, loop, seqLens, maxLen, reverse, counter);
auto selectLayer = N_CHECK(ctx->network()->addSelect(*valid, *Ht, *prevH));
return N_CHECK(selectLayer->getOutput(0));
}
nvinfer1::ITensor* maskBidirRNNHidden(ImporterContext* ctx, const ::ONNX_NAMESPACE::NodeProto& node,
nvinfer1::ILoop* loop, nvinfer1::ITensor* seqLens, nvinfer1::ITensor* maxLen, nvinfer1::ITensor* Ht1,
nvinfer1::ITensor* Ht, nvinfer1::ITensor* singlePassShape)
{
// Splits hidden state into forward and backward states, masks each accordingly, then concatenates
nvinfer1::ITensor* forwardStart = addConstant(ctx, std::vector<int32_t>{0, 0, 0},
::ONNX_NAMESPACE::TensorProto::INT32,
nvinfer1::Dims{1, {3}})->getOutput(0);
nvinfer1::ITensor* reverseStart = addConstant(ctx, std::vector<int32_t>{1, 0, 0},
::ONNX_NAMESPACE::TensorProto::INT32,
nvinfer1::Dims{1, {3}})->getOutput(0);
nvinfer1::ISliceLayer* HtForwardLayer
= N_CHECK(ctx->network()->addSlice(*Ht, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{1, 1, 1}));
HtForwardLayer->setInput(1, *forwardStart);
HtForwardLayer->setInput(2, *singlePassShape);
nvinfer1::ISliceLayer* HtBackwardLayer
= N_CHECK(ctx->network()->addSlice(*Ht, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{1, 1, 1}));
HtBackwardLayer->setInput(1, *reverseStart);
HtBackwardLayer->setInput(2, *singlePassShape);
nvinfer1::ISliceLayer* Ht1ForwardLayer
= N_CHECK(ctx->network()->addSlice(*Ht1, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{1, 1, 1}));
Ht1ForwardLayer->setInput(1, *forwardStart);
Ht1ForwardLayer->setInput(2, *singlePassShape);
nvinfer1::ISliceLayer* Ht1BackwardLayer
= N_CHECK(ctx->network()->addSlice(*Ht1, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{0, 0, 0}, nvinfer1::Dims3{1, 1, 1}));
Ht1BackwardLayer->setInput(1, *reverseStart);
Ht1BackwardLayer->setInput(2, *singlePassShape);
auto forwardHt = N_CHECK(HtForwardLayer->getOutput(0));
auto backwardHt = N_CHECK(HtBackwardLayer->getOutput(0));
auto forwardHt1 = N_CHECK(Ht1ForwardLayer->getOutput(0));
auto backwardHt1 = N_CHECK(Ht1BackwardLayer->getOutput(0));
auto counter = addLoopCounter(ctx, loop, 0);
forwardHt = maskRNNHidden(ctx, node, loop, seqLens, forwardHt1, forwardHt, maxLen, false, counter);
backwardHt = maskRNNHidden(ctx, node, loop, seqLens, backwardHt1, backwardHt, maxLen, true, counter);
std::array<nvinfer1::ITensor*, 2> tensors{{forwardHt, backwardHt}};
nvinfer1::IConcatenationLayer* concat = N_CHECK(ctx->network()->addConcatenation(tensors.data(), 2));
concat->setAxis(0);
return N_CHECK(concat->getOutput(0));
}
nvinfer1::ITensor* getRaggedMask(ImporterContext* ctx, const ::ONNX_NAMESPACE::NodeProto& node, nvinfer1::ILoop* loop,
nvinfer1::ITensor* seqLens, nvinfer1::ITensor* maxLen, bool reverse, nvinfer1::ITensor* counter)
{
// Returns a bool tensor which is true where the elements are valid (within the sequence) and false when outside the
// sequence.
// maxLen must be provided if reverse is true
assert(!reverse || maxLen);
if (!counter)
{
counter = addLoopCounter(ctx, loop, 0);
}
// ONNX spec currently requires seqLens to be int32
counter = castHelper(ctx, counter, nvinfer1::DataType::kINT32);
// Create Mask
nvinfer1::ITensor* seqMask;
if (reverse)
{
counter
= getElementWiseResult(ctx, *unsqueezeTensor(ctx, node, *maxLen, {0}), *counter, nvinfer1::ElementWiseOperation::kSUB);
seqMask = getElementWiseResult(ctx, *seqLens, *counter, nvinfer1::ElementWiseOperation::kLESS);
seqMask = getUnaryResult(ctx, *seqMask, nvinfer1::UnaryOperation::kNOT);
}
else
{
seqMask = getElementWiseResult(ctx, *counter, *seqLens, nvinfer1::ElementWiseOperation::kLESS);
}
return unsqueezeTensor(ctx, node, *seqMask, std::vector<int>{0, 2});
}
} // namespace onnx2trt