1#ifndef TMVA_SOFIE_ROPERATOR_NONZERO
2#define TMVA_SOFIE_ROPERATOR_NONZERO
38 throw std::runtime_error(
"TMVA SOFIE NonZero Op Input Tensor " +
fNX +
" is not found in model");
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++) {
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];
62 nonzero_indices.emplace_back(indices);
65 shapeY[1] = nonzero_indices.size();
66 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
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];
103 std::string codeDecl =
SP +
"size_t " +
fNonZeroParam +
" = " + inputLength +
";\n";
116 std::stringstream out;
117 out <<
SP <<
"size_t fV_NonZero_" <<
fNX <<
" = 0;\n";
122 std::string
Generate(std::string opName)
override {
126 opName =
"op_" + opName;
128 throw std::runtime_error(
"TMVA SOFIE Operator NonZero called to Generate without being initialized first");
130 std::stringstream out;
132 size_t inputLength = 0;
134 if (!intShapeX.empty())
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;
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 <<
"[";
156 if (inputLength > 0) {
157 out << inputLength * j;
159 out << s_inputLength;
160 if (j > 1) out <<
" * " << j;
164 out << vnonzero <<
"] = i_" << j <<
";\n";
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;
171 for (
size_t j = dims; j > 0; j--) {
172 for (
size_t k = 0; k <j; k++) out <<
SP;
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;
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
std::vector< Dim > GetDimTensorShape(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
void AddExtraCodeForDimShapes(const std::string &code)
bool IsConstantTensor(const std::string &name) const
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
std::string GenerateSessionMembersCode(std::string) override
std::vector< Dim > fShapeX
std::string Generate(std::string opName) override
void Initialize(RModel &model) override
ROperator_NonZero(std::string nameX, std::string nameY)
std::vector< Dim > fShapeY
std::string fNonZeroParam
std::vector< std::string_view > fInputTensorNames
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
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