-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuartznetBlock.cpp
221 lines (179 loc) · 4.95 KB
/
QuartznetBlock.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "QuartnetBlock.h"
int computeNewKernelSize(int kernelSize, double kernelWidth){
int newKernelSize = std::max((int)(kernelSize * kernelWidth), 1);
if (newKernelSize % 2 == 0){
newKernelSize += 1;
}
return newKernelSize;
}
int getSamePadding(int kernelSize, int stride, int dilation){
if (stride > 1 && dilation > 1){
throw std::invalid_argument("Only stride OR dilation may be greater than 1");
}
if (dilation > 1){
return ((dilation * kernelSize) / 2) -1;
}
return kernelSize / 2;
}
//using namespace fl;
QuartznetBlock::QuartznetBlock(
int inChannels,
int outChannels,
int repeat,
int kernelSize,
int stride,
int dilation,
double dropOut,
bool residual,
bool separable,
std::string residualMode,
bool lNormIncludeTime){
this->residual = residual;
double kernelSizeFactor = 1.0; // fix kernel size factor.
int newKernelSize = computeNewKernelSize(kernelSize, kernelSizeFactor);
std::cout << "gia tri new kernel size: " << newKernelSize << std::endl;
int paddingVal = getSamePadding(kernelSize, stride, dilation);
std::cout << "gia tri padding: " << paddingVal << std::endl;
fl::Sequential convList;
int inChannelsLoop = inChannels;
for (int i=0; i<(repeat - 1); i++){
auto listConvsFirst = this->getConvNormLayer(inChannelsLoop,
outChannels,
newKernelSize,
stride,
dilation,
paddingVal,
separable,
lNormIncludeTime);
convList.add(listConvsFirst);
auto layerActDrop = this->getActivationAndDropoutLayer(dropOut);
convList.add(layerActDrop);
inChannelsLoop = outChannels;
}
auto lastConvLayer = this->getConvNormLayer(inChannelsLoop,
outChannels,
newKernelSize,
stride,
dilation,
paddingVal,
separable,
lNormIncludeTime);
convList.add(lastConvLayer);
add(convList);
if (residual){
if (residualMode.compare("stride_add")){
stride = stride;
} else {
stride = 1;
}
auto layerConvResidual = this->getConvNormLayer(
inChannels,
outChannels,
1, // kernelSize = 1
stride,
1,
0,
false, // separable = false
lNormIncludeTime);
std::cout << "thong so residual"<< inChannels << "-"<< outChannels << "-"<< stride << std::endl;
add(layerConvResidual);
}
auto lastDropoutLayer = this->getActivationAndDropoutLayer(dropOut);
add(lastDropoutLayer);
}
fl::Sequential QuartznetBlock::getConvNormLayer(
int inChannels,
int outChannels,
int kernelSize,
int stride,
int dilation,
int padding,
bool separable,
bool normIncludeTime){
fl::Sequential mListConvs;
if (separable){
auto depthWiseConv2d = fl::Conv2D(
inChannels,
inChannels,
kernelSize,
1,
stride,
1,
padding,
0,
dilation,
1,
false, // bias
1); // groups
auto pointWiseConv2d = fl::Conv2D(
inChannels,
outChannels,
1, // kernel size x
1, // kernel size y
stride,
1,
0,
0,
dilation,
1,
false, // bias
1); //groups
mListConvs.add(depthWiseConv2d);
mListConvs.add(pointWiseConv2d);
} else {
auto normalConv2d = fl::Conv2D(
inChannels,
outChannels,
kernelSize,
1,
stride,
1,
padding,
0,
dilation,
1,
false,
1);
mListConvs.add(normalConv2d);
}
if (normIncludeTime){
auto layerNormWithTime = fl::LayerNorm(std::vector<int>{0, 1, 2});
mListConvs.add(layerNormWithTime);
} else {
auto layerNormWithoutTime = fl::LayerNorm(std::vector<int>{1, 2});
mListConvs.add(layerNormWithoutTime);
}
// need implement group shuffle later.
return mListConvs;
}
fl::Sequential QuartznetBlock::getActivationAndDropoutLayer(double dropOut){
fl::Sequential listActDrop;
auto layerActivation = fl::ReLU();
auto layerDropOut = fl::Dropout(dropOut);
listActDrop.add(layerActivation);
listActDrop.add(layerDropOut);
return listActDrop;
}
std::vector<fl::Variable> QuartznetBlock::forward(const std::vector<fl::Variable>& inputs) {
std::cout << "so module: " << this->modules().size() << std::endl;
auto out = inputs[0];
std::cout << "shape input: " << out.dims() << std::endl;
auto outputConv = module(0)->forward({out})[0];
std::cout << "shape after component outputConv: " << outputConv.dims() << std::endl;
auto outputAfterConv = outputConv;
if (this->residual){
std::cout << "shape input before residual: "<< out.dims()<< std::endl;
auto outputResidual = module(1)->forward({out})[0];
std::cout << "shape after residual: " << outputResidual.dims() << std::endl;
outputAfterConv = outputAfterConv + outputResidual;
std::cout << "shape after cat with residual: " << outputAfterConv.dims() << std::endl;
auto outputFinal = module(2)->forward({outputAfterConv});
return outputFinal;
}
auto outputFinal = module(1)->forward({outputAfterConv});
return outputFinal;
}
std::string QuartznetBlock::prettyString() const {
std::ostringstream ss;
return ss.str();
}