1#ifndef TMVA_SOFIE_ROPERATOR_TOPK
2#define TMVA_SOFIE_ROPERATOR_TOPK
11namespace Experimental {
33 ROperator_TopK(
int attr_axis,
int attr_largest,
int attr_sorted, std::string nameK, std::string nameX, std::string nameVal, std::string nameInd)
37 fNK(UTILITY::Clean_name(nameK)),
38 fNX(UTILITY::Clean_name(nameX)),
39 fNVal(UTILITY::Clean_name(nameVal)),
40 fNInd(UTILITY::Clean_name(nameInd)){}
48 if (
input.size() != 2) {
49 throw std::runtime_error(
"TMVA SOFIE TopK Op Shape Inference needs exactly 2 input tensors");
52 auto shape =
input[0];
56 return {shape, shape};
64 throw std::runtime_error(
"TMVA SOFIE TopK Op Input Tensor is not found in model");
68 throw std::runtime_error(
"TMVA SOFIE TopK Op Input Tensor i.e. K is not found in model");
79 std::runtime_error(
"TMVA::SOFIE ONNX TopK op axis = "+ std::to_string(
fAttrAxis) +
" value exeeds size of tensor " +
fNX+
" of size "+
fShapeX.size()+
" .");
111 OpName =
"op_" + OpName;
113 throw std::runtime_error(
"TMVA SOFIE Operator TopK called to Generate without being initialized first");
115 std::stringstream out;
118 out <<
"\n" <<
SP <<
"//------ TopK\n";
122 for(
size_t i = 0; i < axis; i++)
127 size_t groupSize =
length/bound;
131 size_t numOfChecksInGrp=groupSize/jump;
132 size_t numOfCheckersInGrp=groupSize/numOfChecksInGrp;
138 out<<
SP<<
"size_t itr = 0, p = 0;\n";
139 out<<
SP<<
"std::vector<std::vector<std::pair<float,int>>>groupElements;\n";
140 out<<
SP<<
"for (size_t i = 0; i < "<<
length<<
"; i++) {\n";
142 out<<
SP<<
SP<<
"size_t tempitr=0, j=0;\n";
143 out<<
SP<<
SP<<
"std::vector<std::pair<float,int>>elements;\n";
144 out<<
SP<<
SP<<
"while(tempitr < "<<groupSize<<
"){\n elements.push_back({tensor_"<<
fNX<<
"[i+tempitr]"<<
",tempitr});\n"<<
SP<<
SP<<
SP<<
"j++;\n"<<
" tempitr = j*"<<jump<<
";\n"<<
"\n"<<
" }\n";
147 out<<
SP<<
SP <<
"std::sort(elements.begin(),elements.end(),[](std::pair<float,int>a,std::pair<float,int>b){return "
148 "a.first>b.first;});\n";
150 out<<
SP<<
SP <<
"std::sort(elements.begin(),elements.end(),[](std::pair<float,int>a,std::pair<float,int>b){return "
151 "a.first<b.first;});\n";
153 out<<
SP<<
SP <<
"std::sort(elements.begin(),elements.end());\n";
155 out<<
SP<<
SP<<
"itr++;\n";
156 out<<
SP<<
SP<<
"std::vector<std::pair<float,int>>kelems;\n";
157 out<<
SP<<
SP<<
"for (int j = 0; j < " <<
fK <<
"; j++){\n kelems.push_back({elements[j].first,elements[j].second});\n"<<
SP<<
SP<<
"}\n";
158 out<<
SP<<
SP<<
"groupElements.push_back(kelems);\n";
159 out<<
SP<<
SP<<
"if(itr == "<<numOfCheckersInGrp<<
"){\n itr = 0;\n i += "<<groupSize-numOfCheckersInGrp<<
";\n";
160 out<<
SP<<
SP<<
SP<<
"for (size_t j = 0; j < groupElements[0].size(); j++) {\n";
161 out<<
SP<<
SP<<
SP<<
SP<<
"for(size_t k = 0; k < groupElements.size(); k++) {\n";
162 out<<
SP<<
SP<<
SP<<
SP<<
SP<<
"tensor_"<<
fNVal<<
"[p] = (groupElements[k][j].first);\n";
163 out<<
SP<<
SP<<
SP<<
SP<<
SP<<
"tensor_"<<
fNInd<<
"[p++] = (groupElements[k][j].second);\n";
166 out<<
SP<<
SP<<
SP<<
"groupElements.clear();\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 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
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 SetNotWritableInitializedTensor(const std::string &tensor_name)
std::string Generate(std::string OpName)
std::vector< size_t > fShapeX
std::vector< size_t > fShapeY
std::vector< std::vector< size_t > > ShapeInference(const std::vector< std::vector< size_t > > input)
void Initialize(RModel &model)
ROperator_TopK(int attr_axis, int attr_largest, int attr_sorted, std::string nameK, std::string nameX, std::string nameVal, std::string nameInd)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
const std::string SP
space used to correctly indent the generated C++ code
std::string ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations