Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_NonZero.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_NONZERO
2#define TMVA_SOFIE_ROPERATOR_NONZERO
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14template<class T>
16{
17
18private:
19
20 std::string fNX;
21 std::string fNY;
22 std::vector<Dim> fShapeX;
23 std::vector<Dim> fShapeY;
24
25public:
27 ROperator_NonZero(std::string nameX, std::string nameY):
28 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){
31 }
32
33
34
35 void Initialize(RModel& model) override {
36 if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
37 throw std::runtime_error("TMVA SOFIE NonZero Op Input Tensor " + fNX + " is not found in model");
38 }
39
40
41 // case input is constant
42 if (model.IsConstantTensor(fNX)) {
43 // compute output directly
44 T * data = static_cast<T*>(model.GetInitializedTensorData(fNX).get());
45 // shape is fully known
46 auto shapeX = model.GetTensorShape(fNX);
47 std::vector<size_t> shapeY(2);
48 shapeY[0] = shapeX.size();
51 std::vector<std::vector<int64_t>> nonzero_indices;
52 for (size_t i = 0; i < length; i++) {
53 if (data[i] != 0) {
54 // get indices
55 size_t flat_index = i;
56 std::vector<int64_t> indices(shapeX.size());
57 for (size_t j = 0; j < shapeX.size(); ++j) {
58 indices[j] = flat_index / strides[j];
59 flat_index %= strides[j];
60 }
61 nonzero_indices.emplace_back(indices);
62 }
63 }
64 shapeY[1] = nonzero_indices.size();
65 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
66 size_t k = 0;
67 for (size_t i = 0; i < shapeY[0]; i++) {
68 for (size_t j = 0; j < shapeY[1]; j++) {
69 dataY[k] = nonzero_indices[j][i];
70 k++;
71 }
72 }
73 if (dataY.empty()) {
74 // no zero elements found
75 dataY.resize(1);
76 shapeY.clear(); // use an empty shape
77 }
78
80 if (model.Verbose()) {
81 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertShapeToString(shapeY)
82 << " : " << ConvertValuesToString(dataY) << std::endl;
83 }
84 fIsOutputConstant = true;
85
86 } else {
87
89
90 // output shape(-1) depends on number of elements of non zero values
91 // first dim is rank of input
92 fShapeY.resize(2);
93 fShapeY[0] = fShapeX.size();
94
95 // identify as -1 since we will declare maximum as size of input
96 fShapeY[1] = Dim{std::string("v_NonZero_") + fNX, static_cast<size_t>(-1)};
97
99 if (model.Verbose()) {
100 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertShapeToString(fShapeY) << std::endl;
101 }
102 }
103 }
104 std::string GenerateSessionMembersCode(std::string /*opName*/) override {
105 if (fIsOutputConstant) return "";
106 // define output value used as max non zero with max size = input shape * N
108 std::stringstream out;
109 out << SP << "size_t v_NonZero_" << fNX << " = " << inputLength << ";\n";
110 return out.str();
111 }
112
113
114 std::string Generate(std::string opName) override {
115 if (fIsOutputConstant) {
116 return "";
117 }
118 opName = "op_" + opName;
119 if (fShapeX.empty()) {
120 throw std::runtime_error("TMVA SOFIE Operator NonZero called to Generate without being initialized first");
121 }
122 std::stringstream out;
124 size_t inputLength = 0;
126 if (!intShapeX.empty())
128
129 size_t dims = fShapeX.size();
130 out << "\n//------ NonZero\n";
131
132 std::string vnonzero = "v_NonZero_" + fNX;
133
134 // loop on input indices
135 out << SP << "size_t offset_" << opName << " = 0;\n";
136 out << SP << vnonzero << " = 0;\n";
137 for (size_t j = 0; j < dims; j++) {
138 std::string index = "i_" + std::to_string(j);
139 for (size_t k = 0; k <= j; k++) out << SP;
140 out << "for (size_t " << index << " = 0; " << index << " < " << fShapeX[j] << "; " << index << "++) {\n";
141 }
142 for (size_t k = 0; k <= dims; k++) out << SP;
143 out << "if (tensor_" << fNX << "[offset_" << opName << "++]) {\n";
144 for (size_t j = 0; j < dims; j++) {
145 for (size_t k = 0; k <= dims+1; k++) out << SP;
146 out << "tensor_" << fNY << "[";
147 if (j > 0) {
148 if (inputLength > 0) {
149 out << inputLength * j;
150 } else {
151 out << s_inputLength;
152 if (j > 1) out << " * " << j;
153 }
154 out << " + ";
155 }
156 out << vnonzero << "] = i_" << j << ";\n";
157 }
158 for (size_t k = 0; k <= dims+1; k++) out << SP;
159 out << vnonzero << "++;\n";
160 for (size_t k = 0; k <= dims; k++) out << SP;
161 out << "}\n";
162 //end loops
163 for (size_t j = dims; j > 0; j--) {
164 for (size_t k = 0; k <j; k++) out << SP;
165 out << "}\n";
166 }
167 // now we need to rearrange the vector if nonzero is less than length of input
168 out << SP << "if (" << vnonzero << " < " << s_inputLength << "){\n";
169 for (size_t j = 1; j < dims; j++) {
170 out << SP << SP << "std::copy(tensor_" << fNY;
171 if (j>0) out << " + " << s_inputLength;
172 if (j>1) out << " * " << j;
173 out << ", tensor_" << fNY;
174 if (j>0) out << " + " << s_inputLength;
175 if (j>1) out << " * " << j;
176 out << " + " << vnonzero << ", tensor_" << fNY;
177 if (j>0) out << " + " << vnonzero;
178 if (j>1) out << "* " << j;
179 out << ");\n";
180 }
181 out << SP << "}\n";
182
183 return out.str();
184 }
185
186};
187
188}//SOFIE
189}//Experimental
190}//TMVA
191
192
193#endif //TMVA_SOFIE_ROPERATOR_NonZero
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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< size_t > GetTensorShape(const std::string &name) const
Definition RModel.cxx:29
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:65
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:262
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:193
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:238
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:327
std::string GenerateSessionMembersCode(std::string) override
std::string Generate(std::string opName) override
ROperator_NonZero(std::string nameX, std::string nameY)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:44
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:48
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::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations