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 std::vector<Dim> fShapeX;
24 std::vector<Dim> fShapeY;
25
26public:
28 ROperator_NonZero(std::string nameX, std::string nameY):
29 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){
32 }
33
34
35
36 void Initialize(RModel& model) override {
37 if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
38 throw std::runtime_error("TMVA SOFIE NonZero Op Input Tensor " + fNX + " is not found in model");
39 }
40
41
42 // case input is constant
43 if (model.IsConstantTensor(fNX)) {
44 // compute output directly
45 T * data = static_cast<T*>(model.GetInitializedTensorData(fNX).get());
46 // shape is fully known
47 auto shapeX = model.GetTensorShape(fNX);
48 std::vector<size_t> shapeY(2);
49 shapeY[0] = shapeX.size();
52 std::vector<std::vector<int64_t>> nonzero_indices;
53 for (size_t i = 0; i < length; i++) {
54 if (data[i] != 0) {
55 // get indices
56 size_t flat_index = i;
57 std::vector<int64_t> indices(shapeX.size());
58 for (size_t j = 0; j < shapeX.size(); ++j) {
59 indices[j] = flat_index / strides[j];
60 flat_index %= strides[j];
61 }
62 nonzero_indices.emplace_back(indices);
63 }
64 }
65 shapeY[1] = nonzero_indices.size();
66 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
67 size_t k = 0;
68 for (size_t i = 0; i < shapeY[0]; i++) {
69 for (size_t j = 0; j < shapeY[1]; j++) {
70 dataY[k] = nonzero_indices[j][i];
71 k++;
72 }
73 }
74 if (dataY.empty()) {
75 // no zero elements found
76 dataY.resize(1);
77 shapeY.clear(); // use an empty shape
78 }
79
81 if (model.Verbose()) {
82 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertShapeToString(shapeY)
83 << " : " << ConvertValuesToString(dataY) << std::endl;
84 }
85 fIsOutputConstant = true;
86
87 } else {
88
90
91 // output shape(-1) depends on number of elements of non zero values
92 // first dim is rank of input
93 fShapeY.resize(2);
94 fShapeY[0] = fShapeX.size();
95
96 // identify as -1 since we will declare maximum as size of input
97 // we will compute at run time the actual number of non zero and rearrange the output vector accordingly
98 fNonZeroParam = "v_NonZero_" + fNX;
99 fShapeY[1] = Dim{fNonZeroParam, static_cast<size_t>(-1)};
100
101 // declare the parameter for number of non zero elements, used when output is not constant
103 std::string codeDecl = SP + "size_t " + fNonZeroParam + " = " + inputLength + ";\n";
104 codeDecl += SP + "fV_NonZero_" + fNX + " = " + fNonZeroParam + ";\n";
106
108 if (model.Verbose()) {
109 std::cout << "NonZero : " << fNX << " -> " << fNY << " " << ConvertDimShapeToString(fShapeY) << std::endl;
110 }
111 }
112 }
113
114 std::string GenerateSessionMembersCode(std::string /*opName*/) override {
115 if (fIsOutputConstant) return "";
116 std::stringstream out;
117 out << SP << "size_t fV_NonZero_" << fNX << " = 0;\n";
118 return out.str();
119 }
120
121
122 std::string Generate(std::string opName) override {
123 if (fIsOutputConstant) {
124 return "";
125 }
126 opName = "op_" + opName;
127 if (fShapeX.empty()) {
128 throw std::runtime_error("TMVA SOFIE Operator NonZero called to Generate without being initialized first");
129 }
130 std::stringstream out;
132 size_t inputLength = 0;
134 if (!intShapeX.empty())
136
137 size_t dims = fShapeX.size();
138 out << "\n//------ NonZero -> " << ConvertDimShapeToString(fShapeY) << "\n";
139
140 std::string vnonzero = fNonZeroParam;
141
142 // loop on input indices
143 out << SP << "size_t offset_" << opName << " = 0;\n";
144 out << SP << "size_t " << vnonzero << " = 0;\n";
145 for (size_t j = 0; j < dims; j++) {
146 std::string index = "i_" + std::to_string(j);
147 for (size_t k = 0; k <= j; k++) out << SP;
148 out << "for (size_t " << index << " = 0; " << index << " < " << fShapeX[j] << "; " << index << "++) {\n";
149 }
150 for (size_t k = 0; k <= dims; k++) out << SP;
151 out << "if (tensor_" << fNX << "[offset_" << opName << "++]) {\n";
152 for (size_t j = 0; j < dims; j++) {
153 for (size_t k = 0; k <= dims+1; k++) out << SP;
154 out << "tensor_" << fNY << "[";
155 if (j > 0) {
156 if (inputLength > 0) {
157 out << inputLength * j;
158 } else {
159 out << s_inputLength;
160 if (j > 1) out << " * " << j;
161 }
162 out << " + ";
163 }
164 out << vnonzero << "] = i_" << j << ";\n";
165 }
166 for (size_t k = 0; k <= dims+1; k++) out << SP;
167 out << vnonzero << "++;\n";
168 for (size_t k = 0; k <= dims; k++) out << SP;
169 out << "}\n";
170 //end loops
171 for (size_t j = dims; j > 0; j--) {
172 for (size_t k = 0; k <j; k++) out << SP;
173 out << "}\n";
174 }
175 // now we need to rearrange the vector if nonzero is less than length of input
176 out << SP << "if (" << vnonzero << " < " << s_inputLength << "){\n";
177 for (size_t j = 1; j < dims; j++) {
178 out << SP << SP << "std::copy(tensor_" << fNY;
179 if (j>0) out << " + " << s_inputLength;
180 if (j>1) out << " * " << j;
181 out << ", tensor_" << fNY;
182 if (j>0) out << " + " << s_inputLength;
183 if (j>1) out << " * " << j;
184 out << " + " << vnonzero << ", tensor_" << fNY;
185 if (j>0) out << " + " << vnonzero;
186 if (j>1) out << "* " << j;
187 out << ");\n";
188 }
189 out << SP << "}\n";
190
191 return out.str();
192 }
193
194};
195
196}//SOFIE
197}//Experimental
198}//TMVA
199
200
201#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:64
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:100
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:301
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:157
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:232
void AddExtraCodeForDimShapes(const std::string &code)
Definition RModel.hxx:113
bool IsConstantTensor(const std::string &name) const
Definition RModel.cxx:277
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:366
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:50
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:47
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:45
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:51
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