1#ifndef TMVA_SOFIE_ROPERATOR_CONVTRANSPOSE_I
2#define TMVA_SOFIE_ROPERATOR_CONVTRANSPOSE_I
14namespace Experimental {
19 -> std::vector<std::vector<size_t>>
21 const std::vector<size_t> &inputShape =
input[0];
22 const std::vector<size_t> &weightShape =
input[1];
23 size_t size = inputShape.size();
38 for (
size_t i = 0;
i <
fDim;
i++)
48 std::vector<size_t> outShape(
size);
49 outShape[0] = inputShape[0];
60 std::runtime_error(
"ConvTranspose with output_shape explicitly set not yet supported.");
83 std::runtime_error(
"ConvTranspose with padding SAME_UPPER or SMAE_LOWER not supported");
88 for (
size_t i = 0;
i <
fDim;
i++) {
96 std::runtime_error(
"ConvTranspose with output_shape explicitly set not yet supported.");
99 for (
size_t i = 0;
i <
fDim;
i++)
101 std::vector<std::vector<size_t>> ret({outShape});
110 throw std::runtime_error(
"TMVA SOFIE Conv Transpose op Input Tensor " +
fNX +
" is not found in model");
115 throw std::runtime_error(
"TMVA SOFIE Conv Transpose Op input data tensor" +
fNX +
116 " is not of 3,4 or 5 dimensions");
120 throw std::runtime_error(
"TMVA SOFIE Conv op Input weight Tensor " +
fNW +
" is not found in model");
125 throw std::runtime_error(
"TMVA SOFIE Conv Transpose Op input weight tensor" +
fNW +
126 " is not of 3,4 or 5 dimensions");
133 throw std::runtime_error(
"TMVA SOFIE ConvTrans op Input Tensor " +
fNB +
" is not found in model");
137 throw std::runtime_error(
"TMVA SOFIE ConvTrans op: Bias Tensor has empty shape");
142 bool broadcast_needed = (bsize != ysize);
144 if (broadcast_needed) {
148 throw std::runtime_error(
"TMVA SOFIE ConvTrans op: Bias Tensor has wrong shape: " +
153 if (
fType !=
"float")
154 throw std::runtime_error(
"TMVA SOFIE ConvTrans op: Broadcasting for non-float type tensors is not supported");
158 std::shared_ptr<void> new_data_ptr(
160 std::default_delete<
float[]>());
175 throw std::runtime_error(
"TMVA SOFIE ConvTrans op: Broadcasting is not needed but bias has wrong shape" +
185 std::stringstream out;
192 out <<
SP <<
SP <<
"float * data = TMVA::Experimental::SOFIE::UTILITY::BroadcastConvBias<float>(tensor_"
195 out <<
SP <<
SP <<
"delete[] data;\n";
207 size_t kernelSize = 1;
208 size_t inputSize = 1;
209 for (
size_t i = 0;
i <
fDim;
i++) {
214 opName =
"op_" + opName;
215 std::stringstream out;
217 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_f = std::vector<" <<
fType <<
">("
220 out <<
"std::vector<" <<
fType <<
"> fVec_" << opName <<
"_xcol = std::vector<" <<
fType <<
">("
221 << kernelSize *
fShapeW[1] * inputSize <<
");\n";
230 OpName =
"op_" + OpName;
233 throw std::runtime_error(
"TMVA SOFIE Conv Op called to Generate without being initialized first");
236 std::stringstream out;
251 out <<
"\n//---- operator ConvTranspose " << OpName <<
"\n";
255 out <<
SP <<
fType <<
" * " << OpName <<
"_f = fVec_" << OpName <<
"_f.data();\n";
260 out <<
SP <<
fType <<
" " << OpName <<
"_f[" <<
fShapeW[0] *
fShapeW[1] * kernelSize <<
"] = {0};\n";
266 size_t id = (
fDim > 2) ?
fDim - 3 : 2;
267 size_t ih = (
fDim > 1) ?
fDim - 2 : 1;
268 size_t iw =
fDim - 1;
270 size_t hstride = kWidth;
275 size_t dstride = kHeight * kWidth;
281 size_t icstride = kHeight * kWidth * kDepth;
282 size_t icstrideDil = 1;
283 for (
size_t i = 0;
i <
fDim;
i++)
285 size_t ocstride =
fShapeW[1] * icstride;
286 size_t ocstrideDil =
fShapeW[1] * icstrideDil;
289 out <<
SP <<
"for (std::size_t ic = 0; ic < " <<
fShapeW[0] <<
"; ic++) {\n";
290 out <<
SP <<
SP <<
"for (std::size_t oc = 0; oc < " <<
fShapeW[1] <<
"; oc++) {\n";
293 out <<
SP <<
SP <<
SP <<
"for (std::size_t kd = 0; kd < " << kDepth <<
"; kd++) {\n";
295 out <<
SP <<
SP <<
SP <<
"for (std::size_t kh = 0; kh < " << kHeight <<
"; kh++) {\n";
296 out <<
SP <<
SP <<
SP <<
SP <<
"for (std::size_t kw = 0; kw < " << kWidth <<
"; kw++) {\n";
298 out <<
SP <<
SP <<
SP <<
SP <<
SP << OpName <<
"_f[ic * " << ocstrideDil <<
" + oc * " << icstrideDil;
300 out <<
" + kd * " << dstrideDil;
302 out <<
" + kh * " << hstrideDil;
303 out <<
" + kw * " << wstrideDil <<
" ] = tensor_" <<
fNW <<
"[ic * " << ocstride <<
" + oc * " << icstride;
306 out <<
" + kd * " << dstride;
308 out <<
" + kh * " << hstride;
315 out <<
SP <<
SP <<
SP <<
SP <<
"}\n";
317 out <<
SP <<
SP <<
SP <<
"}\n";
319 out <<
SP <<
SP <<
SP <<
"}\n";
321 out <<
SP <<
SP <<
"}\n";
324 out <<
SP <<
"char " << OpName <<
"_transA = 'N';\n";
325 out <<
SP <<
"char " << OpName <<
"_transB = 'T';\n";
326 out <<
SP <<
"int " << OpName <<
"_m = " << iHeight * iWidth * iDepth <<
";\n";
327 out <<
SP <<
"int " << OpName <<
"_n = " << icstrideDil*
fShapeW[1] <<
";\n";
328 out <<
SP <<
"int " << OpName <<
"_k = " <<
fShapeW[0] <<
";\n";
329 out <<
SP <<
"float " << OpName <<
"_alpha = 1.0;\n";
330 out <<
SP <<
"float " << OpName <<
"_beta = 0.0;\n";
333 out <<
SP <<
fType <<
" * " << OpName <<
"_xcol = fVec_" << OpName <<
"_xcol.data();\n";
335 out <<
SP <<
fType <<
" " << OpName <<
"_xcol[" <<
fShapeW[0]*icstrideDil * oDepth * oHeight * oWidth <<
"] = {0};\n";
339 out <<
SP <<
"for (size_t n = 0; n < " << bsize <<
"; n++) {\n";
350 std::cout <<
"TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding "
358 std::cout <<
"TMVA SOFIE Operator ConvTranspose: asymmetric padding not supported. Assume an average padding "
366 std::cout <<
"TMVA SOFIE Operator ConvTranspose: asymmetric padding not supported. Assume an average padding "
375 out <<
SP <<
SP <<
"size_t x_offset = n * " <<
fShapeX[1] * iDepth * iHeight * iWidth <<
";\n";
376 out <<
SP <<
SP <<
"size_t out_offset = n * " <<
fShapeY[1] * oDepth * oHeight * oWidth <<
";\n";
380 out <<
SP <<
SP <<
"BLAS::sgemm_(&" << OpName <<
"_transA, &" << OpName <<
"_transB, &" << OpName <<
"_m, &"
381 << OpName <<
"_n, &" << OpName <<
"_k, &" << OpName <<
"_alpha, "
382 <<
"tensor_" <<
fNX <<
" + x_offset, &" << OpName <<
"_m,\n";
383 out <<
SP <<
SP <<
SP << OpName <<
"_f, &" << OpName <<
"_n, &" << OpName <<
"_beta, "
384 << OpName <<
"_xcol, &" << OpName <<
"_m);\n";
390 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::col2im<float>(" << OpName <<
"_xcol,"
393 <<
fShapeY[1] <<
"," << oHeight <<
"," << oWidth <<
",";
401 out <<
", tensor_" <<
fNY <<
" + out_offset);\n\n ";
404 throw std::runtime_error(
"TMVA SOFIE 3D Conv Transpose not yet supported");
405 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" <<
fNX
426 out <<
SP <<
SP <<
"for (size_t g = 0; g < " <<
fAttrGroup <<
"; g++) {\n";
427 out <<
SP <<
SP <<
"size_t x_offset = n * " <<
fShapeX[1] * iHeight * iWidth <<
" + g * "
429 out <<
SP <<
SP <<
"size_t out_offset = n * " <<
fShapeY[1] * oHeight * oWidth <<
" + g * "
433 out <<
SP <<
SP <<
"BLAS::sgemm_(&" << OpName <<
"_transA, &" << OpName <<
"_transB, &" << OpName <<
"_m, &"
434 << OpName <<
"_n, &" << OpName <<
"_k, &" << OpName <<
"_alpha, "
435 <<
"tensor_" <<
fNX <<
" + x_offset, &" << OpName
437 out <<
SP <<
SP <<
SP << OpName <<
"_f, &" << OpName <<
"_n, &" << OpName
438 <<
"_beta, " << OpName <<
"_xcol , &" << OpName <<
"_m);\n";
441 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::col2im<float>(" << OpName <<
"_xcol,"
444 <<
fShapeY[1] <<
"," << oHeight <<
"," << oWidth <<
",";
452 out <<
", tensor_" <<
fNY <<
" + out_offset);\n\n ";
455 throw std::runtime_error(
"TMVA SOFIE 3D Conv Transpose not yet supported");
457 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" <<
fNX
479 out <<
SP <<
SP <<
"}\n";
485 out <<
SP <<
"int " << OpName <<
"_size = " <<
fShapeY[0] *
fShapeY[1] * oDepth * oHeight * oWidth <<
";\n";
486 out <<
SP <<
"float " << OpName <<
"_gamma = 1.0;\n";
487 out <<
SP <<
"int " << OpName <<
"_incx = 1;\n";
488 out <<
SP <<
"int " << OpName <<
"_incy = 1;\n";
490 out <<
SP <<
"BLAS::saxpy_(&" << OpName <<
"_size, &" << OpName <<
"_gamma, tensor_" <<
fNBroadcastedB <<
", &"
491 << OpName <<
"_incx, tensor_" <<
fNY <<
", &" << OpName <<
"_incy);\n";
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)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
std::vector< size_t > fAttrOutputPadding
std::vector< size_t > fAttrPads
void Initialize(RModel &) override
Initialize the model.
std::string GenerateSessionMembersCode(std::string) override
Generate code for Session data members (e.g.
std::vector< size_t > fAttrKernelShape
std::vector< size_t > fAttrDilations
std::string fNBroadcastedB
std::string GenerateInitCode() override
Generate code for initializing the op.
std::string Generate(std::string opName) override
Generate the inference code.
std::vector< size_t > fAttrStrides
std::vector< size_t > fShapeX
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > >) override
Infers the shape of the input tensors.
std::vector< size_t > fShapeW
std::vector< size_t > fShapeB
std::vector< size_t > fShapeY
std::vector< size_t > fAttrOutputShape
const std::string SP
space used to correctly indent the generated C++ code
bool fUseSession
flag to identify if using the session class
T * BroadcastConvBias(const T *data, const size_t channel, const std::vector< size_t > &targetShape)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations