Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_ConvTranspose.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_CONVTRANSPOSE_HXX
2#define TMVA_SOFIE_ROPERATOR_CONVTRANSPOSE_HXX
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
16
17/*! \brief Transposed Convolution operator
18 *
19 * Inference code generation for a transposed convolution layer.
20 * See the <a href="https://github.com/onnx/onnx/blob/main/docs/Operators.md#convtranspose">ONNX documentation</a> for
21 * details about the transposed conv layer.
22 */
23template <typename T>
24class ROperator_ConvTranspose final : public ROperator {
25private:
26 std::string fAttrAutopad;
27 std::vector<size_t> fAttrDilations;
28 size_t fAttrGroup;
29 std::vector<size_t> fAttrKernelShape;
30 std::vector<size_t> fAttrOutputPadding;
31 std::vector<size_t> fAttrOutputShape;
32 std::vector<size_t> fAttrPads;
33 std::vector<size_t> fAttrStrides;
34
35 std::string fNX;
36 std::string fNW;
37 std::string fNB;
38 std::string fNBroadcastedB;
39 std::string fNY;
40
41 std::string fConvK;
42 std::string fImcol;
43
44 std::vector<size_t> fShapeX;
45 std::vector<size_t> fShapeW;
46 std::vector<size_t> fShapeB;
47 std::vector<size_t> fShapeY;
48
49 std::string fType;
50
51 size_t fDim; // dimension of the convolution
52
53public:
54 /*! Default constructor of ROperator_ConvTranspose */
56
57 /*! \brief Constructor of ROperator_ConvTranspose from the attributes
58 *
59 * \param autopad padding
60 * \param dilations dilations of the kernel
61 * \param group number of groups
62 * \param kernelShape shape of the kernel
63 * \param outputPadding padding of the output
64 * \param outputShape shape of the output
65 * \param pads padding of the input
66 * \param strides strides
67 * \param nameX name of the input
68 * \param nameW name of the weight
69 * \param nameB name of the bias
70 * \param nameY name of the output
71 */
72 ROperator_ConvTranspose(std::string autopad, std::vector<size_t> dilations, size_t group,
73 std::vector<size_t> kernelShape, std::vector<size_t> outputPadding,
74 std::vector<size_t> outputShape, std::vector<size_t> pads, std::vector<size_t> strides,
75 std::string nameX, std::string nameW, std::string nameB, std::string nameY)
76 : fAttrAutopad(autopad),
77 fAttrDilations(dilations),
79 fAttrKernelShape(kernelShape),
80 fAttrOutputPadding(outputPadding),
81 fAttrOutputShape(outputShape),
82 fAttrPads(pads),
83 fAttrStrides(strides),
84 fNX(UTILITY::Clean_name(nameX)),
85 fNW(UTILITY::Clean_name(nameW)),
86 fNB(UTILITY::Clean_name(nameB)),
87 fNY(UTILITY::Clean_name(nameY))
88 {
91 if (!fNB.empty()) {
92 fInputTensorNames.emplace_back(fNB);
93 }
94
95 if (std::is_same<T, float>::value) {
96 fType = "float";
97 } else {
98 throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Conv operator");
99 }
100 }
101
102 /*! \brief Infers the shape of the input tensors
103 * \param input shape of the input tensors
104 */
105 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> /*input*/);
106
107 /*! \brief Initialize the model
108 * \param model Model
109 */
110 void Initialize(RModel &) override;
111
112 /*! \brief Generate code for initializing the op
113 */
114 std::string GenerateInitCode() override;
115
116 /*! \brief Generate the inference code
117 * \param opName name of the operator
118 */
119 std::string Generate(std::string opName) override;
120
121 /*! \brief Returns the blas routines needed to compile the generated code
122 */
123 std::vector<std::string> GetBlasRoutines() override { return {std::string("Gemm"), std::string("Axpy")}; }
124};
125
126template <typename T>
127auto ROperator_ConvTranspose<T>::ShapeInference(std::vector<std::vector<size_t>> input)
128 -> std::vector<std::vector<size_t>>
129{
130 const std::vector<size_t> &inputShape = input[0];
131 const std::vector<size_t> &weightShape = input[1];
132 size_t size = inputShape.size();
133 // Dimension of the conv transpose op
134 fDim = size - 2;
135 // Number of groups
136 if (fAttrGroup == 0)
137 fAttrGroup = 1;
138 if (fAttrStrides.empty()) {
139 fAttrStrides = std::vector<size_t>(fDim, 1);
140 }
141 if (fAttrDilations.empty()) {
142 fAttrDilations = std::vector<size_t>(fDim, 1);
143 }
144 // The shape of the kernel is kw for 1d image, kh x Kw for 2d images and kd x kh x kw for a 3d image
145 if (fAttrKernelShape.empty()) {
146 fAttrKernelShape.resize(fDim);
147 for (size_t i = 0; i < fDim; i++)
148 fAttrKernelShape[i] = fShapeW[i + 2] + (fAttrDilations[i] - 1) * (fShapeW[i + 2] - 1);
149 }
150 if (fAttrOutputPadding.empty())
151 fAttrOutputPadding = std::vector<size_t>(fDim, 0);
152
153 // The Shape of the output is batch_size x out_channel x out_w for a 1d image,
154 // batch_size x out_channel x out_h x out_w for a 2d image and
155 // batch_size x out_channel x out_d x out_h x out_w for a 3d image
156 // where out_channel = weight_shape[1] * group
157 std::vector<size_t> outShape(size);
158 outShape[0] = inputShape[0];
159 outShape[1] = weightShape[1] * fAttrGroup;
160
161 // Generate the padding
162 if (fAttrPads.empty()) {
163 fAttrPads = std::vector<size_t>(2 * fDim, 0);
164
165 if (fAttrAutopad != "NOTSET") {
166 throw std::runtime_error("ConvTranspose with padding SAME_UPPER or SMAE_LOWER not supported");
167 }
168 }
169 if (fAttrOutputShape.empty()) {
170 fAttrOutputShape.resize(fDim);
171 for (size_t i = 0; i < fDim; i++) {
172 size_t j = i + 2;
173 fAttrOutputShape[i] = fAttrStrides[i] * (inputShape[j] - 1) + fAttrKernelShape[i] + fAttrOutputPadding[i] -
174 fAttrPads[i] - fAttrPads[fDim + i];
175 }
176 } else {
177 // The shape of the output is explicitly set
178 fAttrPads = std::vector<size_t>(2 * fDim, 0);
179 for (size_t i = 0; i < fDim; ++i) {
180 size_t input_shape = inputShape[i + 2];
181 size_t output_shape = fAttrOutputShape[i];
182 size_t kernel_shape = weightShape[i + 2];
183
184 size_t stride = fAttrStrides[i];
185 size_t dilation = fAttrDilations[i];
186 size_t output_padding = fAttrOutputPadding[i];
187
188 size_t effective_kernel_shape = (kernel_shape - 1) * dilation + 1;
189 size_t expected_shape_without_pad = (input_shape - 1) * stride + output_padding + effective_kernel_shape;
190
191 if (expected_shape_without_pad < output_shape) {
192 throw std::runtime_error("ConvTranspose: explicitly set output_shape is too large for "
193 "the given input and kernel shapes.");
194 }
195
196 size_t total_padding = expected_shape_without_pad - output_shape;
197
198 fAttrPads[i + fDim] = total_padding / 2;
199 fAttrPads[i] = total_padding - fAttrPads[i + fDim];
200 }
201 }
202
203 for (size_t i = 0; i < fDim; i++)
204 outShape[i + 2] = fAttrOutputShape[i];
205 std::vector<std::vector<size_t>> ret({outShape});
206 return ret;
207}
208
209template <typename T>
211{
212
213 if (!model.CheckIfTensorAlreadyExist(fNX)) {
214 throw std::runtime_error("TMVA SOFIE Conv Transpose op Input Tensor " + fNX + " is not found in model");
215 }
216 fShapeX = model.GetTensorShape(fNX);
217 if (fShapeX.size() < 3 || fShapeX.size() > 5) {
218 std::cout << fNX << " : " << ConvertShapeToString(fShapeX) << std::endl;
219 throw std::runtime_error("TMVA SOFIE Conv Transpose Op input data tensor" + fNX +
220 " is not of 3,4 or 5 dimensions");
221 }
222 fDim = fShapeX.size() - 2;
223 if (!model.CheckIfTensorAlreadyExist(fNW)) {
224 throw std::runtime_error("TMVA SOFIE Conv op Input weight Tensor " + fNW + " is not found in model");
225 }
226 fShapeW = model.GetTensorShape(fNW);
227 if (fShapeW.size() < 3 || fShapeW.size() > 5) {
228 std::cout << fNW << " : " << ConvertShapeToString(fShapeW) << std::endl;
229 throw std::runtime_error("TMVA SOFIE Conv Transpose Op input weight tensor" + fNW +
230 " is not of 3,4 or 5 dimensions");
231 }
232 fShapeY = ShapeInference({fShapeX, fShapeW})[0];
233 if (fShapeX[1] != fShapeW[0] || fShapeW[0] % fAttrGroup != 0) {
234 throw std::runtime_error("TMVA SOFIE Conv Transpose Op: input channels " + std::to_string(fShapeX[1]) +
235 " must match weight shape[0] " + std::to_string(fShapeW[0]) +
236 " and be divisible by group " + std::to_string(fAttrGroup));
237 }
238
239 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY);
240 if (fNB != "") {
241 if (!model.CheckIfTensorAlreadyExist(fNB)) {
242 throw std::runtime_error("TMVA SOFIE ConvTrans op Input Tensor " + fNB + " is not found in model");
243 }
244 fShapeB = model.GetTensorShape(fNB);
245 if (fShapeB.size() < 1)
246 throw std::runtime_error("TMVA SOFIE ConvTrans op: Bias Tensor has empty shape");
247
248 size_t bsize = ConvertShapeToLength(fShapeB);
249 size_t ysize = ConvertShapeToLength(fShapeY);
250 // broadcasting is needed if first stride of B is not same of Y
251 bool broadcast_needed = (bsize != ysize);
252 // Broadcast the bias B
253 if (broadcast_needed) {
254 // we assume bias tensor size is equal to number of filters that is the second dimension in
255 // the output tensor
256 if (bsize != fShapeY[1])
257 throw std::runtime_error("TMVA SOFIE ConvTrans op: Bias Tensor has wrong shape: " +
258 ConvertShapeToString(fShapeB));
259
260 if (fType != "float")
261 throw std::runtime_error(
262 "TMVA SOFIE ConvTrans op: Broadcasting for non-float type tensors is not supported");
263 // Add broadcasting code in Session constructor and in GenerateInitCode:
264 // we need to add a new intermediate tensor for broadcasted bias tensor
265 fNBroadcastedB = "Broadcasted" + fNB;
266 model.AddIntermediateTensor(fNBroadcastedB, model.GetTensorType(fNB), fShapeY);
267 } else {
268 // bias tensor is already correct shape, no need to broadcast
269 if (fShapeY != fShapeB)
270 throw std::runtime_error("TMVA SOFIE ConvTrans op: Broadcasting is not needed but bias has wrong shape" +
271 ConvertShapeToString(fShapeB));
272 fNBroadcastedB = fNB;
273 }
274 }
275
276 size_t kernelSize = 1;
277 size_t inputSize = 1;
278 for (size_t i = 0; i < fDim; i++) {
279 inputSize *= fShapeX[2 + i];
280 kernelSize *= fAttrKernelShape[i];
281 }
282
283 std::vector<size_t> shape1 = {fShapeW[0], fShapeW[1], kernelSize};
284 std::vector<size_t> shape2 = {fShapeW[1], kernelSize, inputSize};
285 // private workspaces of this node, named after its output (see ROperator_Conv)
286 model.AddIntermediateTensor(fNY + "_f", ConvertStringToType(fType), shape1);
287 model.AddIntermediateTensor(fNY + "_xcol", ConvertStringToType(fType), shape2);
288 fConvK = fNY + "_f";
289 fImcol = fNY + "_xcol";
290 fOutputTensorNames.emplace_back(fConvK);
291 fOutputTensorNames.emplace_back(fImcol);
292
293 // register the inference helper functions used by the generated code
294 // (only the <3D case is supported, which uses col2im)
295 model.AddNeededHelperFunction("col2im");
296 if (!fNB.empty())
297 model.AddNeededHelperFunction("BroadcastConvBias");
298}
299
300template <typename T>
302{
303 std::stringstream out;
304 // generate initialization code for broadcasting of bias tensor
305 size_t bsize = ConvertShapeToLength(fShapeB);
306 size_t ysize = ConvertShapeToLength(fShapeY);
307 if (bsize != ysize && !fNBroadcastedB.empty()) {
308 // include a separate scope to avoid defining unique operator temp variables
309 out << SP << "{\n";
310 out << SP << SP << "float * data = UTILITY::BroadcastConvBias<float>(tensor_" << fNB << ", " << bsize << ", "
311 << ConvertShapeToString(fShapeY) << ");\n";
312 out << SP << SP << "std::copy(data, data + " << ConvertShapeToLength(fShapeY) << ", tensor_" << fNBroadcastedB
313 << ");\n";
314 out << SP << SP << "delete[] data;\n";
315 out << SP << "}\n";
316 }
317 return out.str();
318}
319
320template <typename T>
321std::string ROperator_ConvTranspose<T>::Generate(std::string OpName)
322{
323 OpName = "op_" + OpName;
324
325 if (fShapeX.empty() || fShapeW.empty() || (fNB != "" && fShapeB.empty()) || fShapeY.empty()) {
326 throw std::runtime_error("TMVA SOFIE Conv Op called to Generate without being initialized first");
327 }
328
329 std::stringstream out;
330
331 size_t bsize = fShapeX[0];
332 size_t kDepth = (fDim > 2) ? fShapeW[2] : 1; // kernel depth
333 size_t kHeight = (fDim > 1) ? fShapeW[fDim] : 1; // kernel height
334 size_t kWidth = fShapeW[fDim + 1]; // kernel width
335
336 size_t iDepth = (fDim > 2) ? fShapeX[2] : 1; // input depth
337 size_t iHeight = (fDim > 1) ? fShapeX[fDim] : 1; // input height
338 size_t iWidth = fShapeX[fDim + 1]; // input width
339
340 size_t oDepth = (fDim > 2) ? fShapeY[2] : 1; // output depth
341 size_t oHeight = (fDim > 1) ? fShapeY[fDim] : 1; // ouput height
342 size_t oWidth = fShapeY[fDim + 1]; // output width
343
344 out << "\n//---- operator ConvTranspose " << OpName << "\n";
345
346 // vectorize the (dilated)convolution kernels into a matrix
347 // The shape of the kernel is W for 1d image, H x W for 2d image and D x H x W
348 // for 3d image
349 size_t id = (fDim > 2) ? fDim - 3 : 2;
350 size_t ih = (fDim > 1) ? fDim - 2 : 1;
351 size_t iw = fDim - 1;
352 size_t wstrideDil = fAttrDilations[iw];
353 size_t hstride = kWidth;
354 size_t hstrideDil = fAttrKernelShape[iw];
355 if (fDim > 1)
356 hstrideDil *= fAttrDilations[ih];
357 // stride dilated in the height
358 size_t dstride = kHeight * kWidth;
359 size_t dstrideDil = fAttrKernelShape[iw];
360 if (fDim > 1)
361 dstrideDil *= fAttrKernelShape[ih];
362 if (fDim > 2)
363 dstrideDil *= fAttrDilations[id];
364 size_t icstride = kHeight * kWidth * kDepth;
365 size_t icstrideDil = 1;
366 for (size_t i = 0; i < fDim; i++)
367 icstrideDil *= fAttrKernelShape[i];
368 size_t ocstride = fShapeW[1] * icstride;
369 size_t ocstrideDil = fShapeW[1] * icstrideDil;
370
371 // The shape of f is [M/group, kHeight x kWidth]
372 out << SP << "for (std::size_t ic = 0; ic < " << fShapeW[0] << "; ic++) {\n";
373 out << SP << SP << "for (std::size_t oc = 0; oc < " << fShapeW[1] << "; oc++) {\n";
374 // out << SP << SP << SP << "size_t kIndex = 0;\n"; // filter index
375 if (fDim > 2)
376 out << SP << SP << SP << "for (std::size_t kd = 0; kd < " << kDepth << "; kd++) {\n";
377 if (fDim > 1)
378 out << SP << SP << SP << "for (std::size_t kh = 0; kh < " << kHeight << "; kh++) {\n";
379 out << SP << SP << SP << SP << "for (std::size_t kw = 0; kw < " << kWidth << "; kw++) {\n";
380
381 out << SP << SP << SP << SP << SP << "tensor_" << fConvK << "[ic * " << ocstrideDil << " + oc * " << icstrideDil;
382 if (fDim > 2)
383 out << " + kd * " << dstrideDil;
384 if (fDim > 1)
385 out << " + kh * " << hstrideDil;
386 out << " + kw * " << wstrideDil << " ] = tensor_" << fNW << "[ic * " << ocstride << " + oc * " << icstride;
387
388 if (fDim > 2)
389 out << " + kd * " << dstride;
390 if (fDim > 1)
391 out << " + kh * " << hstride;
392 out << " + kw ];\n";
393
394 // here we rotate the input kernel tranforming 0,1,2,...N-1 in N-1,N-2,...,2,1,0
395 // out << " + " << icstride -1 << " - kIndex ];\n"; // tranform 1,2,3,4 in 4,3,2,1
396 // out << SP << SP << SP << SP << SP << "kIndex++;\n"; // update input filter index
397
398 out << SP << SP << SP << SP << "}\n";
399 if (fDim > 1)
400 out << SP << SP << SP << "}\n";
401 if (fDim > 2)
402 out << SP << SP << SP << "}\n";
403
404 out << SP << SP << "}\n";
405 out << SP << "}\n";
406
407 out << SP << "char " << OpName << "_transA = 'N';\n";
408 out << SP << "char " << OpName << "_transB = 'T';\n";
409 out << SP << "int " << OpName << "_m = " << iHeight * iWidth * iDepth << ";\n";
410 out << SP << "int " << OpName << "_n = " << icstrideDil * fShapeW[1] << ";\n"; // output channels * filters
411 out << SP << "int " << OpName << "_k = " << fShapeW[0] / fAttrGroup << ";\n"; // input channels per group
412 out << SP << "float " << OpName << "_alpha = 1.0;\n";
413 out << SP << "float " << OpName << "_beta = 0.0;\n";
414
415 // Loop on batch size
416 out << SP << "for (size_t n = 0; n < " << bsize << "; n++) {\n";
417
418 // IM2COL: Unroll the input tensor
419 // order input data as (e.g. kernel 2x2) and (xa,ya) is channel 1 and (xb,yb) is channel 2
420 // (xa1,..,xak,ya1,..yak)(xb1,...,xbk,yb1,..,ybk)
421 // (xa2,...xak+1,ya1,...yak)(......)
422 // trick for speed is using caffe im2col and output a matrix which contains filtered values as rows.
423 // By doing this one has consecutive memory reads and writes
424 // Resulting matrix op_xcol is (output channels * filter_h * filter_w , output_h * output_w)
425
426 if (fAttrGroup == 1) {
427 out << SP << SP << "size_t x_offset = n * " << fShapeX[1] * iDepth * iHeight * iWidth << ";\n";
428 out << SP << SP << "size_t out_offset = n * " << fShapeY[1] * oDepth * oHeight * oWidth << ";\n";
429
430 // DO BLAS before:
431 // BLAS
432 out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName << "_m, &"
433 << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, "
434 << "tensor_" << fNX << " + x_offset, &" << OpName
435 << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
436 out << SP << SP << SP << "tensor_" << fConvK << ", &" << OpName << "_n, &" << OpName << "_beta, tensor_" << fImcol
437 << ", &" << OpName << "_m);\n";
438
439 // when using im2col - resulting matrix is transposed, is (input_c * filter_h * filter_w, output_h *
440 // output_w)
441 // before using col2im I need to transpose matrix
442 if (fDim < 3) {
443 out << SP << SP << "UTILITY::col2im<float>(tensor_" << fImcol
444 << ","
445 // channels, height, width, kernel_h, kernel_w, pad_h_begin, pad_h_end, pad_w_begin, pad_w_end,
446 // stride_h, stride_w, dilation_h, dilation_w,
447 << fShapeY[1] << "," << oHeight << "," << oWidth << ",";
448 if (fDim == 1)
449 out << "1, " << fAttrKernelShape[0] << ",0,0," << fAttrPads[0] << "," << fAttrPads[1] << ",1,"
450 << fAttrStrides[0] << ",1," << fAttrDilations[0];
451 else // dim ==2
452 out << fAttrKernelShape[0] << "," << fAttrKernelShape[1] << "," << fAttrPads[0] << "," << fAttrPads[2]
453 << "," << fAttrPads[1] << "," << fAttrPads[3] << "," << fAttrStrides[0] << "," << fAttrStrides[1] << ","
454 << fAttrDilations[0] << "," << fAttrDilations[1];
455 out << ", tensor_" << fNY << " + out_offset);\n\n ";
456 } else {
457 // 3d : needs a col2im for 3d
458 throw std::runtime_error("TMVA SOFIE 3D Conv Transpose not yet supported");
459 out << SP << SP << "UTILITY::Im2col_3d<float>(tensor_" << fNX
460 << " + x_offset,"
461 // channels, d, h, w, k_d, k_h, k_w, pad_d_begin, pad_d_end, pad_h_begin, pad_h_end,
462 // pad_w_begin, pad_w_end, stride_d, stride_h, stride_w, dilation_d, dilation_h, dilation_w,
463 //
464 << fShapeX[1] << "," << oDepth << "," << oHeight << "," << oWidth << "," << fAttrKernelShape[0] << ","
465 << fAttrKernelShape[1] << "," << fAttrKernelShape[2] << "," << fAttrPads[0] << "," << fAttrPads[3] << ","
466 << fAttrPads[1] << "," << fAttrPads[4] << "," << fAttrPads[2] << "," << fAttrPads[5] << ","
467 << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrStrides[2] << "," << fAttrDilations[0] << ","
468 << fAttrDilations[1] << "," << fAttrDilations[2] << ",tensor_" << fImcol << ");\n\n ";
469 }
470 // // BLAS
471 // out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName << "_m, &"
472 // << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, tensor_" << fImcol << ", &" << OpName
473 // << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
474 // out << SP << SP << SP <<"tensor_" << fConvK << ", &" << OpName << "_k, &" << OpName << "_beta, tensor_" << fNY
475 // << " + out_offset, &" << OpName << "_m);\n";
476 } else {
477 // case of group transposed convolution
478 // Unroll (IM2COL) the input tensor- make loop on groups and repeat operations (IM2COL + GEMM for each
479 // group)
480 out << SP << SP << "for (size_t g = 0; g < " << fAttrGroup << "; g++) {\n";
481 // each group maps Cin/group input channels to Cout/group (= fShapeW[1]) output channels
482 out << SP << SP << SP << "size_t x_offset = n * " << fShapeX[1] * iHeight * iWidth << " + g * "
483 << fShapeX[1] * iHeight * iWidth / fAttrGroup << ";\n";
484 out << SP << SP << SP << "size_t out_offset = n * " << fShapeY[1] * oHeight * oWidth << " + g * "
485 << fShapeY[1] * oHeight * oWidth / fAttrGroup << ";\n";
486 // the kernel matrix has layout [Cin, Cout/group, K]: group g uses the rows of its Cin/group input channels
487 out << SP << SP << SP << "size_t offset_f = g * " << (fShapeW[0] / fAttrGroup) * icstrideDil * fShapeW[1]
488 << ";\n";
489
490 out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName << "_m, &"
491 << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, "
492 << "tensor_" << fNX << " + x_offset, &" << OpName
493 << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
494 out << SP << SP << SP << "tensor_" << fConvK << " + offset_f, &" << OpName << "_n, &" << OpName
495 << "_beta, tensor_" << fImcol << " , &" << OpName << "_m);\n";
496
497 if (fDim < 3) {
498 out << SP << SP << "UTILITY::col2im<float>(tensor_" << fImcol
499 << ","
500 // channels, height, width, kernel_h, kernel_w, pad_h_begin, pad_h_end, pad_w_begin, pad_w_end,
501 // stride_h, stride_w, dilation_h, dilation_w,
502 << fShapeW[1] << "," << oHeight << "," << oWidth << ",";
503 if (fDim == 1)
504 out << "1, " << fAttrKernelShape[0] << ",0,0," << fAttrPads[0] << "," << fAttrPads[1] << ",1,"
505 << fAttrStrides[0] << ",1," << fAttrDilations[0];
506 else // dim ==2
507 out << fAttrKernelShape[0] << "," << fAttrKernelShape[1] << "," << fAttrPads[0] << "," << fAttrPads[2]
508 << "," << fAttrPads[1] << "," << fAttrPads[3] << "," << fAttrStrides[0] << "," << fAttrStrides[1] << ","
509 << fAttrDilations[0] << "," << fAttrDilations[1];
510 out << ", tensor_" << fNY << " + out_offset);\n\n ";
511 } else {
512 // 3d im2col
513 throw std::runtime_error("TMVA SOFIE 3D Conv Transpose not yet supported");
514
515 out << SP << SP << "UTILITY::Im2col_3d<float>(tensor_" << fNX
516 << " + x_offset,"
517 // channels, d, h, w, k_d, k_h, k_w, pad_d_begin, pad_d_end, pad_h_begin, pad_h_end,
518 // pad_w_begin, pad_w_end, stride_d, stride_h, stride_w, dilation_d, dilation_h, dilation_w,
519 //
520 << fShapeX[1] << "," << oDepth << "," << oHeight << "," << oWidth << "," << fAttrKernelShape[0] << ","
521 << fAttrKernelShape[1] << "," << fAttrKernelShape[2] << "," << fAttrPads[0] << "," << fAttrPads[3] << ","
522 << fAttrPads[1] << "," << fAttrPads[4] << "," << fAttrPads[2] << "," << fAttrPads[5] << ","
523 << fAttrStrides[0] << "," << fAttrStrides[1] << "," << fAttrStrides[2] << "," << fAttrDilations[0] << ","
524 << fAttrDilations[1] << "," << fAttrDilations[2] << "," << "tensor_" << fImcol << ");\n\n ";
525 }
526
527 // // BLAS
528 // // offset g must be g * k * n
529 // out << SP << SP << SP << "size_t offset_f = g * " << fShapeW[0] * fShapeW[1] * icstrideDil / fAttrGroup <<
530 // ";\n"; out << SP << SP << "BLAS::sgemm_(&" << OpName << "_transA, &" << OpName << "_transB, &" << OpName <<
531 // "_m, &"
532 // << OpName << "_n, &" << OpName << "_k, &" << OpName << "_alpha, tensor_" << fImcol << ", &" << OpName
533 // << "_m,\n"; // use m if op_xcol is not transpose , otherwise k
534 // out << SP << SP << SP << "tensor_" << fConvK << " + offset_f, &" << OpName << "_k, &" << OpName << "_beta,
535 // tensor_" << fNY
536 // << " + out_offset"
537 // << ", &" << OpName << "_m);\n";
538
539 out << SP << SP << "}\n"; // end of group loop
540 }
541
542 out << SP << "}\n"; // end of batch size loop
543
544 if (fNBroadcastedB != "") {
545 out << SP << "int " << OpName << "_size = " << fShapeY[0] * fShapeY[1] * oDepth * oHeight * oWidth << ";\n";
546 out << SP << "float " << OpName << "_gamma = 1.0;\n";
547 out << SP << "int " << OpName << "_incx = 1;\n";
548 out << SP << "int " << OpName << "_incy = 1;\n";
549
550 out << SP << "BLAS::saxpy_(&" << OpName << "_size, &" << OpName << "_gamma, tensor_" << fNBroadcastedB << ", &"
551 << OpName << "_incx, tensor_" << fNY << ", &" << OpName << "_incy);\n";
552 }
553
554 return out.str();
555}
556
557} // namespace TMVA::Experimental::SOFIE
558
559#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
std::vector< std::string > GetBlasRoutines() override
Returns the blas routines needed to compile the generated code.
ROperator_ConvTranspose(std::string autopad, std::vector< size_t > dilations, size_t group, std::vector< size_t > kernelShape, std::vector< size_t > outputPadding, std::vector< size_t > outputShape, std::vector< size_t > pads, std::vector< size_t > strides, std::string nameX, std::string nameW, std::string nameB, std::string nameY)
Constructor of ROperator_ConvTranspose from the attributes.
void Initialize(RModel &) override
Initialize the model.
ROperator_ConvTranspose()
Default constructor of ROperator_ConvTranspose.
std::string GenerateInitCode() override
Generate code for initializing the op.
std::string Generate(std::string opName) override
Generate the inference code.
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > >)
Infers the shape of the input tensors.
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:44
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:45
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
ETensorType ConvertStringToType(std::string type)
std::string ConvertShapeToString(const std::vector< size_t > &shape)