1#ifndef TMVA_SOFIE_ROPERATOR_Split
2#define TMVA_SOFIE_ROPERATOR_Split
11namespace Experimental{
23 std::vector<std::string>
fNYs;
32 ROperator_Split(
const std::string & nameX,
const std::string & nameS,
int axis,
const std::vector<std::string> & namesY):
33 fAxis(axis),
fNX(UTILITY::Clean_name(nameX)),
fNSplit(UTILITY::Clean_name(nameS)) {
34 fNYs.reserve(namesY.size());
35 for (
auto &
name : namesY)
41 [](
const std::string& s) -> std::string_view { return s; });
45 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
46 throw std::runtime_error(
"TMVA SOFIE Split Op Input Tensor is not found in model");
52 if (fAxis < 0 || fAxis >=
static_cast<int>(
fInputShape.size()) )
53 throw std::runtime_error(
"TMVA SOFIE Split - invalid axis " + std::to_string(
fAxis));
57 throw std::runtime_error(
"TMVA SOFIE Split - splitting in dynamic axis is not supported");
62 size_t nsplit =
fNYs.size();
65 int64_t splitValue = 0;
66 if (origValue % nsplit == 0) {
67 splitValue = origValue/nsplit;
68 fSplit = std::vector<int64_t>(nsplit, splitValue);
71 splitValue = std::ceil(
double(origValue)/nsplit);
72 fSplit = std::vector<int64_t>(nsplit-1, splitValue);
73 fSplit.push_back(origValue % splitValue);
78 if (!model.IsInitializedTensor(
fNSplit))
79 throw std::runtime_error(
"TMVA SOFIE Split - non-initialized split tensors are not supported");
80 auto splitShape = model.GetTensorShape(
fNSplit);
81 if (splitShape.size() != 1 || splitShape[0] != nsplit)
82 throw std::runtime_error(
"TMVA SOFIE Split - split input tensor has invalid shape");
83 auto split_data =
static_cast<int64_t *
>(model.GetInitializedTensorData(
fNSplit).get());
84 fSplit = std::vector<int64_t>(split_data, split_data + nsplit);
88 for (
size_t i = 0; i <
fNYs.size(); i++) {
92 model.AddIntermediateTensor(
fNYs[i], model.GetTensorType(
fNX), outputShape);
95 if (tot_split != origValue)
96 throw std::runtime_error(
"TMVA SOFIE Split - Sum of split sizes must match the input dimension along the axis");
99 if (model.Verbose()) {
103 std::cout << std::endl;
108 std::string
Generate(std::string OpName)
override {
109 OpName =
"op_" + OpName;
111 throw std::runtime_error(
"TMVA SOFIE Operator Split called to Generate without being initialized first");
117 std::stringstream out;
118 out <<
"\n" <<
SP <<
"//------ Split\n";
119 out <<
SP <<
"size_t " << OpName <<
"_axis_offset = 0;\n";
121 for (
size_t i = 0; i <
fNYs.size(); i++) {
125 out <<
SP <<
"for (int id = 0; id < " <<
length <<
" ; id++){\n";
127 out <<
SP <<
SP <<
"int input_index = 0;\n";
128 out <<
SP <<
SP <<
"int remaining = id;\n";
131 out <<
SP <<
SP <<
"// dim " << k <<
"\n";
133 out <<
SP <<
SP <<
"input_index += (int(remaining / " << output_strides[k] <<
")";
135 if (k ==
static_cast<size_t>(
fAxis) && i > 0)
136 out <<
" + " << OpName <<
"_axis_offset";
137 out <<
") * " << input_strides[k] <<
";\n";
138 out <<
SP <<
SP <<
"remaining %= " << output_strides[k] <<
";\n";
141 out <<
SP <<
SP <<
"input_index += remaining";
142 if (k ==
static_cast<size_t>(
fAxis) && i > 0)
143 out <<
" + " << OpName <<
"_axis_offset";
148 out <<
SP <<
SP <<
"tensor_" <<
fNYs[i] <<
"[id] = tensor_" <<
fNX <<
"[input_index];\n";
150 if (i <
fNYs.size()-1) out <<
SP << OpName <<
"_axis_offset += " <<
fSplit[i] <<
";\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 char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
std::vector< std::vector< Dim > > fOutputShapes
std::string Generate(std::string OpName) override
ROperator_Split(const std::string &nameX, const std::string &nameS, int axis, const std::vector< std::string > &namesY)
void Initialize(RModel &model) override
std::vector< Dim > fInputShape
std::vector< std::string > fNYs
std::vector< int64_t > fSplit
std::vector< std::string_view > fInputTensorNames
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations