Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Conv.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_CONV
2#define TMVA_SOFIE_ROPERATOR_CONV
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <memory>
9#include <sstream>
10#include <algorithm>
11#include <stdexcept>
12#include <vector>
13#include <cassert>
14
15namespace TMVA {
16namespace Experimental {
17namespace SOFIE {
18
19template<typename T>
20class ROperator_Conv final : public ROperator
21{
22private:
23 std::string fAttrAutopad;
24 std::vector<size_t> fAttrDilations;
25 size_t fAttrGroup;
26 std::vector<size_t> fAttrKernelShape;
27 std::vector<size_t> fAttrPads;
28 std::vector<size_t> fAttrStrides;
29
30 std::string fNX;
31 std::string fNW;
32 std::string fNB;
33 std::string fNB2; // bias tensor name after broadcasting
34 std::string fNY;
35
36 std::vector<size_t> fShapeX;
37 std::vector<size_t> fShapeW;
38 std::vector<size_t> fShapeB;
39 std::vector<size_t> fShapeY;
40
41 std::string fType;
42
43 size_t fDim; // dimension of the convolution
44
45
46public:
47
49
50 ROperator_Conv(std::string autopad, std::vector<size_t> dilations,
51 size_t group, std::vector<size_t> kernelShape, std::vector<size_t> pads,
52 std::vector<size_t> strides, std::string nameX, std::string nameW,
53 std::string nameB, std::string nameY):
54 fAttrAutopad(autopad), fAttrDilations(dilations), fAttrGroup(group), fAttrKernelShape(kernelShape),
55 fAttrPads(pads), fAttrStrides(strides),
56 fNX(UTILITY::Clean_name(nameX)), fNW(UTILITY::Clean_name(nameW)),
57 fNB(UTILITY::Clean_name(nameB)), fNY(UTILITY::Clean_name(nameY))
58 {
59 if(std::is_same<T, float>::value) {
60 fType = "float";
61 } else {
62 throw
63 std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Conv operator");
64 }
65 }
66
67 ROperator_Conv(std::string autopad, std::vector<size_t> dilations,
68 size_t group, std::vector<size_t> kernelShape, std::vector<size_t> pads,
69 std::vector<size_t> strides, std::string nameX, std::string nameW,
70 std::string nameY):
71 fAttrAutopad(autopad), fAttrDilations(dilations), fAttrGroup(group), fAttrKernelShape(kernelShape),
72 fAttrPads(pads), fAttrStrides(strides),
73 fNX(UTILITY::Clean_name(nameX)), fNW(UTILITY::Clean_name(nameW)), fNY(UTILITY::Clean_name(nameY))
74 {
75 if(std::is_same<T, float>::value) {
76 fType = "float";
77 } else {
78 throw
79 std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Conv operator");
80 }
81 }
82
83 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) {
84 ETensorType out = input[0];
85 return {out};
86 }
87
88 // function returning output shape given input
89 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) {
90 // shape of convolution input has to be (according to ONNX): N x C x H x W
91 // Where N : batch size, C : input channels, H : input height, W : input width
92
93 if (input.size() > 3 ) {
94 throw
95 std::runtime_error("TMVA SOFIE Conv Op Shape inference need 2 or 3 input tensors");
96 }
97 for(size_t i = 0; i < input.size(); i++) {
98 if (input[i].size() -2 != fDim) {
99 throw
100 std::runtime_error("TMVA SOFIE Conv Op Shape inference - invalid inputs ");
101 }
102 }
103
104 if (fAttrGroup == 0) {
105 fAttrGroup = input[0][1] / input[1][1];
106 }
107
108 // kernel shape
109 size_t k1 = ((fAttrKernelShape.empty())? input[1][2] : fAttrKernelShape[0]);
110 size_t k2 = (fDim > 1) ? ((fAttrKernelShape.empty()) ? input[1][3] : fAttrKernelShape[1]) : 1;
111 size_t k3 = (fDim > 2) ? ((fAttrKernelShape.empty()) ? input[1][4] : fAttrKernelShape[2]) : 1;
112
113
114 size_t i1 = (fDim > 1) ? ((fDim > 2) ? 3 : 2) : 1;
115 size_t i2 = (fDim > 2) ? 4 : 3;
116 size_t i3 = 5;
117
118 if (fAttrDilations.empty()) {
119 fAttrDilations = {1, 1, 1};
120 }
121 fAttrDilations.resize(3);
122 if (fDim < 3) {
123 fAttrDilations.resize(3, 1);
124 }
125 // Shape of the kernel
126 fAttrKernelShape = {k1 + (fAttrDilations[0] - 1) * (k1 - 1),
127 k2 + (fAttrDilations[1] - 1) * (k2 - 1),
128 k3 + (fAttrDilations[2] - 1) * (k3 - 1)};
129
130 if (fAttrAutopad == "NOTSET") {
131 if (fAttrPads.empty()) {
132 fAttrPads = {1, 1, 1, 1, 1, 1};
133 }
134 } else if (fAttrAutopad == "SAME_UPPER" || fAttrAutopad == "SAME_LOWER") {
135 if (fDim == 1)
137 else if (fDim == 2)
139 else if (fDim == 3)
141 fAttrKernelShape[0] / 2, fAttrKernelShape[1] / 2, fAttrKernelShape[2] / 2};
142 // add extra padding at beginning or end (depending if SAME_UPPER or SAME_LOWER)
143 // need to check this!
144 if (fAttrKernelShape[0] % 2 == 1) {
145 (fAttrAutopad == "SAME_UPPER") ? fAttrPads[0]++ : fAttrPads[i1]++;
146 }
147 if (fDim > 1 && fAttrKernelShape[1] % 2 == 1) {
148 (fAttrAutopad == "SAME_UPPER") ? fAttrPads[1]++ : fAttrPads[i2]++;
149 }
150 if (fDim > 2 && fAttrKernelShape[2] % 2 == 1) {
151 (fAttrAutopad == "SAME_UPPER") ? fAttrPads[2]++ : fAttrPads[i3]++;
152 }
153 } else if (fAttrAutopad != "VALID") {
154 throw
155 std::runtime_error("TMVA SOFIE Conv Op invalid fAutopad");
156 }
157 // to be sure pad is vector of size 6
158 if (fDim < 3) fAttrPads.resize(6, 0);
159
160 if (fAttrStrides.empty()) {
161 fAttrStrides = {1, 1, 1};
162 }
163 if (fDim < 3)
164 fAttrStrides.resize(3, 1);
165
166
167 size_t input1 = input[0][2];
168 size_t input2 = (fDim > 1) ? input[0][3] : 1;
169 size_t input3 = (fDim > 2) ? input[0][4] : 1;
170
171 size_t pad1 = fAttrPads[0] + fAttrPads[i1];
172 size_t output1 = (input1 + pad1 - fAttrKernelShape[0]) / fAttrStrides[0] + 1;
173
174 size_t batch_size = input[0][0]; // first element in input tensor
175 size_t output_channels = input[1][0]; // first element in weight tensor
176
177 std::vector<std::vector<size_t>> ret({{ batch_size, output_channels, output1 }});
178
179 if (fDim == 1)
180 return ret;
181
182 size_t pad2 = fAttrPads[1] + fAttrPads[i2];
183 size_t output2 = (input2 + pad2 - fAttrKernelShape[1]) / fAttrStrides[1] + 1;
184 // output is N x M x OH x OW
185 ret[0].push_back(output2);
186 if (fDim == 2)
187 return ret;
188
189 size_t pad3 = fAttrPads[2] + fAttrPads[i3];
190 size_t output3 = (input3 + pad3 - fAttrKernelShape[2] ) / fAttrStrides[2] + 1;
191
192 // output is N x M x OH x OW x OD
193 ret[0].push_back(output3);
194 return ret;
195 }
196
197 void Initialize(RModel& model) {
198 fUseSession = model.UseSession();
199 if (!model.CheckIfTensorAlreadyExist(fNX)) {
200 throw
201 std::runtime_error("TMVA SOFIE Conv op Input Tensor " + fNX + " is not found in model");
202 }
203 fShapeX = model.GetTensorShape(fNX);
204 if (fShapeX.size() < 3 || fShapeX.size() > 5) {
205 std::cout << fNX << " : " << ConvertShapeToString(fShapeX) << std::endl;
206 throw
207 std::runtime_error("TMVA SOFIE Conv Op input data tensor" + fNX + " is not of 3,4 or 5 dimensions");
208 }
209 fDim = fShapeX.size() - 2;
210 if (!model.CheckIfTensorAlreadyExist(fNW)) {
211 throw
212 std::runtime_error("TMVA SOFIE Conv op Input weight Tensor " + fNW + " is not found in model");
213 }
214 fShapeW = model.GetTensorShape(fNW);
215 if (fShapeW.size() < 3 || fShapeW.size() > 5) {
216 std::cout << fNW << " : " << ConvertShapeToString(fShapeW) << std::endl;
217 throw std::runtime_error("TMVA SOFIE Conv Op input weight tensor" + fNW + " is not of 3,4 or 5 dimensions");
218 }
221 if (fNB != "") {
222 if (!model.CheckIfTensorAlreadyExist(fNB)) {
223 throw
224 std::runtime_error("TMVA SOFIE Conv op Input Tensor " + fNB + " is not found in model");
225 }
226 fShapeB = model.GetTensorShape(fNB);
227 std::vector<size_t> targetShape(fShapeY.begin() + 1, fShapeY.end());
228 bool broadcast_needed = !UTILITY::AreSameShape(fShapeB, targetShape);
229 if (broadcast_needed) {
230 auto original_data = model.GetInitializedTensorData(fNB);
231 // make bias shape equal to Y shape by adding 1
232 if (fShapeB.size() < 1)
233 throw std::runtime_error("TMVA SOFIE Conv op: Bias Tensor has empty shape");
234 // we assume bias tensor dimension is equal to number of filters that is the second dimension in
235 // the output tensor
236 if (fShapeB[0] != fShapeY[1])
237 throw std::runtime_error("TMVA SOFIE Conv op: Bias Tensor has wrong shape: " +
239 if (fType != "float")
240 throw std::runtime_error("TMVA SOFIE Conv op: Broadcasting for non-float type tensors is not supported");
241 // here is the actual broadcasting
242 if (!fUseSession) {
243 std::vector<size_t> shape(fDim + 1, 1);
244 shape[0] = fShapeB[0];
245 std::shared_ptr<void> new_data_ptr(
246 UTILITY::UnidirectionalBroadcast<float>(static_cast<float *>(original_data.get()), shape, targetShape),
247 std::default_delete<float[]>());
248 model.UpdateInitializedTensor(fNB, model.GetTensorType(fNB), targetShape, new_data_ptr);
249 fShapeB = model.GetTensorShape(fNB);
250 fNB2 = fNB; // use same name
251 }
252 else {
253 // In case of session add broadcasting code in Session constructor and in GenerateInitCode
254 // we need to add a new intermediate tensor for broadcasted bias tensor
255 fNB2 = fNB + "bcast";
256 model.AddIntermediateTensor(fNB2, model.GetTensorType(fNB), targetShape);
257 }
258 }
259 }
260 }
261
262 std::string GenerateInitCode() {
263 std::stringstream out;
264 // Generate initialization code for broadcasting of bias tensor
265 if (!fNB2.empty()) {
266 // include a separate scope to avoid defining unique operator temp variables
267 std::vector<size_t> shape(fDim + 1, 1);
268 shape[0] = fShapeB[0];
269 std::vector<size_t> targetShape(fShapeY.begin() + 1, fShapeY.end());
270 out << SP << "{\n";
271 out << SP << SP << "float * data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_"
272 << fNB << ", " << ConvertShapeToString(shape) << ", " << ConvertShapeToString(fShapeY) << ");\n";
273 out << SP << SP << "std::copy(data, data + " << ConvertShapeToLength(targetShape) << ", tensor_" << fNB2 << ");\n";
274 out << SP << SP << "delete[] data;\n";
275 out << SP << "}\n";
276 }
277 return out.str();
278 }
279
280 // Generate code for Session data members (e.g. internal vectors)
281 virtual std::string GenerateSessionMembersCode(std::string opName) {
282
283 size_t outputChannelSize = fShapeY[2]; // size/channel = D * H * W
284 size_t kernelSize = fAttrKernelShape[0];
285 for (size_t i = 1; i < fDim; i++) {
286 outputChannelSize *= fShapeY[2 + i];
287 kernelSize *= fAttrKernelShape[i];
288 }
289
290 opName = "op_" + opName;
291 std::stringstream out;
292 // matrix with convolution kernels
293 out << "std::vector<" << fType << "> fVec_" << opName << "_f = std::vector<" << fType << ">("
294 << fShapeW[0] * fShapeW[1] * kernelSize << ");\n";
295 // output matrix of im2col
296 out << "std::vector<" << fType << "> fVec_" << opName << "_xcol = std::vector<" << fType << ">("
297 << fShapeW[1] * kernelSize * outputChannelSize << ");\n";
298 out << "\n";
299
300 return out.str();
301 }
302
303 std::string Generate(std::string OpName) {
304 OpName = "op_" + OpName;
305
306 if (fShapeX.empty() || fShapeW.empty() || (fNB != "" && fShapeB.empty()) || fShapeY.empty()) {
307 throw
308 std::runtime_error("TMVA SOFIE Conv Op called to Generate without being initialized first");
309 }
310
311 std::stringstream out;
312 size_t bsize = fShapeX[0];
313 size_t kDepth = (fDim > 2) ? fShapeW[2] : 1; // kernel depth
314 size_t kHeight = (fDim > 1) ? fShapeW[fDim] : 1; // kernel height
315 size_t kWidth = fShapeW[fDim+1]; // kernel width
316 size_t iDepth = (fDim > 2) ? fShapeX[2] : 1; // input depth
317 size_t iHeight = (fDim > 1) ? fShapeX[fDim] : 1; // input height
318 size_t iWidth = fShapeX[fDim+1]; // input width
319 size_t oDepth = (fDim > 2) ? fShapeY[2] : 1; // output depth
320 size_t oHeight = (fDim > 1) ? fShapeY[fDim] : 1; // ouput height
321 size_t oWidth = fShapeY[fDim+1]; // output width
322
323 out << "\n//---- operator Conv " << OpName << "\n";
324
325 // create first matrix with convolution kernels
326 if (fUseSession)
327 out << SP << fType << " * " << OpName << "_f = fVec_" << OpName << "_f.data();\n";
328 else
329 out << SP << fType << " " << OpName << "_f[" << fShapeW[0] * fShapeW[1] * fAttrKernelShape[0] * fAttrKernelShape[1] << "] = {0};\n";
330
331 // vectorize the (dilated)convolution kernels into a matrix
332 // no need to transpose the matrix
333 // to fix for 1d and 3d
334
335 size_t id = (fDim > 2) ? fDim-3 : 2;
336 size_t ih = (fDim > 1) ? fDim-2 : 1;
337 size_t iw = fDim-1;
338
339 size_t wstrideDil = fAttrDilations[iw];
340 size_t hstride = kWidth;
341 size_t hstrideDil = fAttrDilations[ih] * fAttrKernelShape[iw]; // stride dilated in the height
342 size_t dstride = kHeight * kWidth;
343 size_t dstrideDil = fAttrDilations[id] * fAttrKernelShape[ih] * fAttrKernelShape[iw];
344 size_t icstride = kHeight * kWidth * kDepth;
345 size_t icstrideDil = fAttrKernelShape[id] * fAttrKernelShape[ih] * fAttrKernelShape[iw];
346 size_t ocstride = fShapeW[1] * icstride;
347 size_t ocstrideDil = fShapeW[1] * icstrideDil;
348
349 out << SP << "for (std::size_t oc = 0; oc < " << fShapeW[0] << "; oc++) {\n";
350 out << SP << SP << "for (std::size_t ic = 0; ic < " << fShapeW[1] << "; ic++) {\n";
351 if (fDim > 2)
352 out << SP << SP << SP << "for (std::size_t kd = 0; kd < " << kDepth << "; kd++) {\n";
353 if (fDim > 1)
354 out << SP << SP << SP << "for (std::size_t kh = 0; kh < " << kHeight << "; kh++) {\n";
355 out << SP << SP << SP << SP << "for (std::size_t kw = 0; kw < " << kWidth << "; kw++) {\n";
356
357 out << SP << SP << SP << SP << SP << OpName << "_f[oc * "
358 << ocstrideDil << " + ic * " << icstrideDil;
359 if (fDim > 2) out << " + kd * " << dstrideDil;
360 if (fDim > 1) out << " + kh * " << hstrideDil;
361 out << " + kw * " << wstrideDil << " ] = tensor_" << fNW << "[oc * " << ocstride << " + ic * " << icstride;
362 if (fDim > 2) out << " + kd * " << dstride;
363 if (fDim > 1) out << " + kh * " << hstride;
364 out << " + kw ];\n";
365
366 out << SP << SP << SP << SP << "}\n";
367 if (fDim > 1) out << SP << SP << SP << "}\n";
368 if (fDim > 2) out << SP << SP << SP << "}\n";
369 out << SP << SP << "}\n";
370 out << SP << "}\n";
371
372 //out << SP << "char " << OpName << "_transA = 'T';\n";
373 out << SP << "char " << OpName << "_transA = 'N';\n";
374 out << SP << "char " << OpName << "_transB = 'N';\n";
375 out << SP << "int " << OpName << "_m = " << oHeight * oWidth * oDepth << ";\n"; // output h*w
376 assert(fShapeY[1] == fShapeW[0]);
377 assert(fShapeW[1] == fShapeX[1] / fAttrGroup);
378 out << SP << "int " << OpName << "_n = " << fShapeW[0] << ";\n"; // output channels
379 out << SP << "int " << OpName << "_k = " << fShapeW[1] * fAttrKernelShape[0] * fAttrKernelShape[1] * fAttrKernelShape[2] << ";\n";
380 out << SP << "float " << OpName << "_alpha = 1.0;\n";
381 out << SP << "float " << OpName << "_beta = 0.0;\n";
382
383 if (fUseSession) {
384 out << SP << fType << " * " << OpName << "_xcol = fVec_" << OpName << "_xcol.data();\n";
385 }
386 else {
387 out << SP << fType << " " << OpName << "_xcol["
388 << fShapeX[1] * fAttrKernelShape[0] * fAttrKernelShape[1] * fAttrKernelShape[2] * oDepth * oHeight * oWidth
389 << "] = {0};\n";
390 }
391
392 // Loop on batch size
393 out << SP << "for (size_t n = 0; n < " << bsize << "; n++) {\n";
394
395 // IM2COL: Unroll the input tensor
396 // order input data as (e.g. kernel 2x2) and (xa,ya) is channel 1 and (xb,yb) is channel 2
397 // (xa1,..,xak,ya1,..yak)(xb1,...,xbk,yb1,..,ybk)
398 // (xa2,...xak+1,ya1,...yak)(......)
399 // trick for speed is using caffe im2col and output a matrix which contains filtered values as rows.
400 // By doing this one has consecutive memory reads and writes
401 // Resulting matrix op_xcol is (input channels * filter_h * filter_w , output_h * output_w)
402 if (fDim ==1) {
403 if (fAttrPads[0] != fAttrPads[1] ) {
404 std::cout << "TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding "
405 << std::endl;
406 fAttrPads[0] = (fAttrPads[0] + fAttrPads[1]) / 2;
407 }
408 fAttrPads[1] = 0;
409 fAttrStrides[1] = 1;
410 }
411 if (fDim == 2) {
412 if (fAttrPads[0] != fAttrPads[2] || fAttrPads[1] != fAttrPads[3]) {
413 std::cout << "TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding " << std::endl;
414 fAttrPads[0] = (fAttrPads[0] + fAttrPads[2]) / 2;
415 fAttrPads[1] = (fAttrPads[1] + fAttrPads[3]) / 2;
416 }
417 }
418 if (fDim == 3) {
419 if (fAttrPads[0] != fAttrPads[3] || fAttrPads[1] != fAttrPads[4] || fAttrPads[2] != fAttrPads[5]) {
420 std::cout << "TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding " << std::endl;
421 fAttrPads[0] = (fAttrPads[0] + fAttrPads[3]) / 2;
422 fAttrPads[1] = (fAttrPads[1] + fAttrPads[4]) / 2;
423 fAttrPads[2] = (fAttrPads[2] + fAttrPads[5]) / 2;
424 }
425 }
426 out << SP << SP << "size_t out_offset = n * " << fShapeY[1] * oDepth * oHeight * oWidth << ";\n";
427
428 if (fAttrGroup == 1) {
429 out << SP << SP << "size_t x_offset = n * " << fShapeX[1] * iHeight * iWidth << ";\n";
430 // when using im2col - resulting matrix is transposed, the dimension is (input_c * filter_h * filter_y, output_h *
431 // output_w)
432 if (fDim < 3) {
433 out << SP << SP << "TMVA::Experimental::SOFIE::UTILITY::Im2col<float>(tensor_" << fNX
434 << " + x_offset,"
435 // channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h,
436 // dilation_w,
437 //
438 << fShapeW[1] << "," << iHeight << "," << iWidth << ",";
439 if (fDim == 1)
440 out << "1, " << fAttrKernelShape[0] << ",0," << fAttrPads[0] << ",1," << fAttrStrides[0] << ",1,"
441 << fAttrDilations[0];
442 else // dim ==2
443 out << fAttrKernelShape[0] << "," << fAttrKernelShape[1] << "," << fAttrPads[0] << "," << fAttrPads[1]
444 << "," << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrDilations[0] << ","
445 << fAttrDilations[1];
446 out << "," << OpName << "_xcol);\n\n ";
447 } else {
448 // 3d im2col
449 out << SP << SP << "TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" << fNX
450 << " + x_offset,"
451 // channels, d, h, w, k_d, k_h, k_w, pad_d, pad_h, pad_w, stride_d, stride_h, stride_w,
452 // dilation_d, dilation_h, dilation_w,
453 //
454 << fShapeW[1] << "," << iDepth << "," << iHeight << "," << iWidth << ","
455 << fAttrKernelShape[0] << "," << fAttrKernelShape[1] << "," << fAttrKernelShape[2] << ","
456 << fAttrPads[0] << "," << fAttrPads[1] << "," << fAttrPads[2] << ","
457 << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrStrides[2] << ","
458 << fAttrDilations[0] << "," << fAttrDilations[1] << "," << fAttrDilations[2] << ","
459 << OpName << "_xcol);\n\n ";
460 }
461 // BLAS
462 out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName << "_m, &"
463 << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, " << OpName << "_xcol, &" << OpName
464 << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
465 out << SP << SP << SP << OpName << "_f, &" << OpName << "_k, &" << OpName << "_beta, tensor_" << fNY
466 << " + out_offset, &" << OpName << "_m);\n";
467 } else {
468 // case of group convolution
469 // Unroll (IM2COL) the input tensor- make loop on groups and repeat operations (IM2COL + GEMM for each
470 // group)
471 // out << SP << SP << "size_t out_offset = n * " << fShapeY[1] * oDepth * oHeight * oWidth << ";\n";
472 out << SP << SP << "for (size_t g = 0; g < " << fAttrGroup << "; g++) {\n";
473 out << SP << SP << "size_t x_offset = n * " << fShapeX[1] * iDepth * iHeight * iWidth << " + g * "
474 << fShapeW[1] * iDepth * iHeight * iWidth << ";\n ";
475 out << SP << SP << "size_t out_offset = n * " << fShapeY[1] * oDepth * oHeight * oWidth << " + g * "
476 << fShapeW[0] * oDepth * oHeight * oWidth / fAttrGroup << ";\n ";
477
478 if (fDim < 3) {
479 out << SP << SP << "TMVA::Experimental::SOFIE::UTILITY::Im2col<float>(tensor_" << fNX
480 << " + x_offset,"
481 // channels, height, width, kernel_h, kernel_w, pad_h, pad_w, stride_h, stride_w, dilation_h,
482 // dilation_w,
483 //
484 << fShapeW[1] << "," << iHeight << "," << iWidth << ",";
485 if (fDim == 1)
486 out << "1, " << fAttrKernelShape[0] << ",0," << fAttrPads[0] << ",1," << fAttrStrides[0] << ",1,"
487 << fAttrDilations[0];
488 else // dim ==2
489 out << fAttrKernelShape[0] << "," << fAttrKernelShape[1] << "," << fAttrPads[0] << "," << fAttrPads[1]
490 << "," << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrDilations[0] << ","
491 << fAttrDilations[1];
492 out << "," << OpName << "_xcol);\n\n ";
493 } else {
494 // 3d im2col
495 out << SP << SP << "TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" << fNX
496 << " + x_offset,"
497 // channels, d, h, w, k_d, k_h, k_w, pad_d, pad_h, pad_w, stride_d, stride_h, stride_w,
498 // dilation_d, dilation_h, dilation_w,
499 //
500 << fShapeW[1] << "," << iDepth << "," << iHeight << "," << iWidth << "," << fAttrKernelShape[0] << ","
501 << fAttrKernelShape[1] << "," << fAttrKernelShape[2] << "," << fAttrPads[0] << "," << fAttrPads[1]
502 << "," << fAttrPads[2] << "," << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrStrides[2]
503 << "," << fAttrDilations[0] << "," << fAttrDilations[1] << "," << fAttrDilations[2] << "," << OpName
504 << "_xcol);\n\n ";
505 }
506
507 // BLAS
508 // n must be divided by the number of groups
509 out << SP << SP << SP << OpName << "_n = " << fShapeW[0] / fAttrGroup << ";\n";
510 // offset g must be g * k * n
511 out << SP << SP << SP << "size_t offset_f = g * "
513 << ";\n";
514 out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName << "_m, &"
515 << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, " << OpName << "_xcol, &" << OpName
516 << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
517 out << SP << SP << SP << OpName << "_f + offset_f, &" << OpName << "_k, &" << OpName << "_beta, tensor_" << fNY
518 << " + out_offset"
519 << ", &" << OpName << "_m);\n";
520
521 out << SP << SP << "}\n"; // end of group loop
522 }
523
524 if (fNB2 != "") {
525 out << SP << "int " << OpName << "_size = " << fShapeY[1] * oDepth * oHeight * oWidth << ";\n";
526 out << SP << "float " << OpName << "_gamma = 1.0;\n";
527 out << SP << "int " << OpName << "_incx = 1;\n";
528 out << SP << "int " << OpName << "_incy = 1;\n";
529
530 out << SP << "BLAS::saxpy_(&" << OpName << "_size, &" << OpName << "_gamma, tensor_" << fNB2 << ", &"
531 << OpName << "_incx, tensor_" << fNY << " + out_offset, &" << OpName << "_incy);\n";
532
533 }
534 out << SP << "}\n"; // end of batch size loop
535
536 return out.str();
537 }
538
539 /*! \brief Returns the blas routines needed to compile the generated code
540 */
541 std::vector<std::string> GetBlasRoutines() { return { std::string("Gemm"), std::string("Axpy") }; }
542};
543
544} // namespace SOFIE
545} // namespace Experimental
546} // namespace TMVA
547
548#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:91
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:187
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:116
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:56
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:248
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:239
virtual std::string GenerateSessionMembersCode(std::string opName)
std::string Generate(std::string OpName)
ROperator_Conv(std::string autopad, std::vector< size_t > dilations, size_t group, std::vector< size_t > kernelShape, std::vector< size_t > pads, std::vector< size_t > strides, std::string nameX, std::string nameW, std::string nameB, std::string nameY)
ROperator_Conv(std::string autopad, std::vector< size_t > dilations, size_t group, std::vector< size_t > kernelShape, std::vector< size_t > pads, std::vector< size_t > strides, std::string nameX, std::string nameW, std::string nameY)
std::vector< std::string > GetBlasRoutines()
Returns the blas routines needed to compile the generated code.
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
bool fUseSession
flag to identify if using the session class
Definition ROperator.hxx:42
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations