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::string fNonZeroParam; // name of the parameter used to store the number of non zero elements when output is not constant
23 bool fDeclaresParam = false; // true when this operator is the one declaring fNonZeroParam
24 std::vector<Dim> fShapeX;
25 std::vector<Dim> fShapeY;
26
27public:
29 ROperator_NonZero(std::string nameX, std::string nameY):
30 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){
33 }
34
35
36
37 void Initialize(RModel& model) override {
38 if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
39 throw std::runtime_error("TMVA SOFIE NonZero Op Input Tensor " + fNX + " is not found in model");
40 }
41
42
43 // case input is constant
44 if (model.IsConstantTensor(fNX)) {
45 // compute output directly
46 T * data = static_cast<T*>(model.GetInitializedTensorData(fNX).get());
47 // shape is fully known
48 auto shapeX = model.GetTensorShape(fNX);
49 std::vector<size_t> shapeY(2);
50 shapeY[0] = shapeX.size();
53 std::vector<std::vector<int64_t>> nonzero_indices;
54 for (size_t i = 0; i < length; i++) {
55 if (data[i] != 0) {
56 // get indices
57 size_t flat_index = i;
58 std::vector<int64_t> indices(shapeX.size());
59 for (size_t j = 0; j < shapeX.size(); ++j) {
60 indices[j] = flat_index / strides[j];
61 flat_index %= strides[j];
62 }
63 nonzero_indices.emplace_back(indices);
64 }
65 }
66 shapeY[1] = nonzero_indices.size();
67 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
68 size_t k = 0;
69 for (size_t i = 0; i < shapeY[0]; i++) {
70 for (size_t j = 0; j < shapeY[1]; j++) {
71 dataY[k] = nonzero_indices[j][i];
72 k++;
73 }
74 }
75 if (dataY.empty()) {
76 // no zero elements found
77 dataY.resize(1);
78 shapeY.clear(); // use an empty shape
79 }
80
81 model.AddConstantTensor(fNY, shapeY, dataY);
82 if (model.Verbose()) {
83 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertShapeToString(shapeY)
84 << " : " << ConvertValuesToString(dataY) << std::endl;
85 }
86 fIsOutputConstant = true;
87
88 } else {
89
90 fShapeX = model.GetDimTensorShape(fNX);
91
92 // output shape(-1) depends on number of elements of non zero values
93 // first dim is rank of input
94 fShapeY.resize(2);
95 fShapeY[0] = fShapeX.size();
96
97 // identify as -1 since we will declare maximum as size of input
98 // we will compute at run time the actual number of non zero and rearrange the output vector accordingly
99 // named after the input: two NonZero nodes reading the same tensor count the same
100 // elements, so they share one parameter and shape comparisons see them as equal
101 fNonZeroParam = "v_NonZero_" + fNX;
102 fShapeY[1] = Dim{fNonZeroParam, static_cast<size_t>(-1)};
103
104 // only the first operator on this input declares the parameter
105 if (!model.IsComputedShapeParam(fNonZeroParam)) {
106 fDeclaresParam = true;
107 // declare the parameter for number of non zero elements, used when output is not constant
109 std::string codeDecl = SP + "size_t " + fNonZeroParam + " = " + inputLength + ";\n";
110 codeDecl += SP + "fV_NonZero_" + fNX + " = " + fNonZeroParam + ";\n";
111 model.AddExtraCodeForDimShapes(codeDecl);
112 // computed here, so it must not also be a Session constructor argument
113 model.AddComputedShapeParam(fNonZeroParam);
114 }
115
116 model.AddIntermediateTensor(fNY, ETensorType::INT64, fShapeY);
117 if (model.Verbose()) {
118 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertDimShapeToString(fShapeY) << std::endl;
119 }
120 }
121 }
122
123 std::string GenerateSessionMembersCode(std::string /*opName*/) override {
125 return "";
126 std::stringstream out;
127 out << SP << "size_t fV_NonZero_" << fNX << " = 0;\n";
128 return out.str();
129 }
130
131
132 std::string Generate(std::string opName) override {
133 if (fIsOutputConstant) {
134 return "";
135 }
136 opName = "op_" + opName;
137 if (fShapeX.empty()) {
138 throw std::runtime_error("TMVA SOFIE Operator NonZero called to Generate without being initialized first");
139 }
140 std::stringstream out;
142 size_t inputLength = 0;
144 if (!intShapeX.empty())
146
147 size_t dims = fShapeX.size();
148 out << "\n//------ NonZero -> " << ConvertDimShapeToString(fShapeY) << "\n";
149
150 // the others fill their output with a private counter of the same value
151 std::string vnonzero = fDeclaresParam ? fNonZeroParam : ("nonzero_count_" + opName);
152
153 // loop on input indices
154 out << SP << "size_t offset_" << opName << " = 0;\n";
155 out << SP << "size_t " << vnonzero << " = 0;\n";
156 for (size_t j = 0; j < dims; j++) {
157 std::string index = "i_" + std::to_string(j);
158 for (size_t k = 0; k <= j; k++) out << SP;
159 out << "for (size_t " << index << " = 0; " << index << " < " << fShapeX[j] << "; " << index << "++) {\n";
160 }
161 for (size_t k = 0; k <= dims; k++) out << SP;
162 out << "if (tensor_" << fNX << "[offset_" << opName << "++]) {\n";
163 for (size_t j = 0; j < dims; j++) {
164 for (size_t k = 0; k <= dims+1; k++) out << SP;
165 out << "tensor_" << fNY << "[";
166 if (j > 0) {
167 if (inputLength > 0) {
168 out << inputLength * j;
169 } else {
170 out << s_inputLength;
171 if (j > 1) out << " * " << j;
172 }
173 out << " + ";
174 }
175 out << vnonzero << "] = i_" << j << ";\n";
176 }
177 for (size_t k = 0; k <= dims+1; k++) out << SP;
178 out << vnonzero << "++;\n";
179 for (size_t k = 0; k <= dims; k++) out << SP;
180 out << "}\n";
181 //end loops
182 for (size_t j = dims; j > 0; j--) {
183 for (size_t k = 0; k <j; k++) out << SP;
184 out << "}\n";
185 }
186 // now we need to rearrange the vector if nonzero is less than length of input
187 out << SP << "if (" << vnonzero << " < " << s_inputLength << "){\n";
188 for (size_t j = 1; j < dims; j++) {
189 out << SP << SP << "std::copy(tensor_" << fNY;
190 if (j>0) out << " + " << s_inputLength;
191 if (j>1) out << " * " << j;
192 out << ", tensor_" << fNY;
193 if (j>0) out << " + " << s_inputLength;
194 if (j>1) out << " * " << j;
195 out << " + " << vnonzero << ", tensor_" << fNY;
196 if (j>0) out << " + " << vnonzero;
197 if (j>1) out << "* " << j;
198 out << ");\n";
199 }
200 out << SP << "}\n";
201
202 return out.str();
203 }
204
205};
206
207}//SOFIE
208}//Experimental
209}//TMVA
210
211
212#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::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::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
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