1#ifndef TMVA_SOFIE_ROPERATOR_NONZERO
2#define TMVA_SOFIE_ROPERATOR_NONZERO
36 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
37 throw std::runtime_error(
"TMVA SOFIE NonZero Op Input Tensor " +
fNX +
" is not found in model");
42 if (model.IsConstantTensor(
fNX)) {
44 T *
data =
static_cast<T*
>(model.GetInitializedTensorData(
fNX).get());
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++) {
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];
61 nonzero_indices.emplace_back(indices);
64 shapeY[1] = nonzero_indices.size();
65 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
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];
79 model.AddConstantTensor(
fNY, shapeY, dataY);
80 if (model.Verbose()) {
96 fShapeY[1] =
Dim{std::string(
"v_NonZero_") +
fNX,
static_cast<size_t>(-1)};
99 if (model.Verbose()) {
109 std::stringstream out;
110 out <<
SP <<
"size_t fV_NonZero_" <<
fNX <<
" = " << inputLength <<
";\n";
114 std::string
Generate(std::string opName)
override {
118 opName =
"op_" + opName;
120 throw std::runtime_error(
"TMVA SOFIE Operator NonZero called to Generate without being initialized first");
122 std::stringstream out;
124 size_t inputLength = 0;
126 if (!intShapeX.empty())
130 out <<
"\n//------ NonZero\n";
132 std::string vnonzero =
"v_NonZero_" +
fNX;
135 out <<
SP <<
"size_t offset_" << opName <<
" = 0;\n";
136 out <<
SP <<
"size_t " << 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;
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 <<
"[";
148 if (inputLength > 0) {
149 out << inputLength * j;
151 out << s_inputLength;
152 if (j > 1) out <<
" * " << j;
156 out << vnonzero <<
"] = i_" << j <<
";\n";
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;
163 for (
size_t j = dims; j > 0; j--) {
164 for (
size_t k = 0; k <j; k++) out <<
SP;
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;
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::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::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