1#ifndef TMVA_SOFIE_ROPERATOR_NONZERO
2#define TMVA_SOFIE_ROPERATOR_NONZERO
11namespace Experimental{
30 fNX(UTILITY::Clean_name(nameX)),
fNY(UTILITY::Clean_name(nameY)){
38 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
39 throw std::runtime_error(
"TMVA SOFIE NonZero Op Input Tensor " +
fNX +
" is not found in model");
44 if (model.IsConstantTensor(
fNX)) {
46 T *
data =
static_cast<T*
>(model.GetInitializedTensorData(
fNX).get());
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++) {
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];
63 nonzero_indices.emplace_back(indices);
66 shapeY[1] = nonzero_indices.size();
67 std::vector<int64_t> dataY(shapeY[0]* shapeY[1]);
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];
81 model.AddConstantTensor(
fNY, shapeY, dataY);
82 if (model.Verbose()) {
109 std::string codeDecl =
SP +
"size_t " +
fNonZeroParam +
" = " + inputLength +
";\n";
111 model.AddExtraCodeForDimShapes(codeDecl);
117 if (model.Verbose()) {
126 std::stringstream out;
127 out <<
SP <<
"size_t fV_NonZero_" <<
fNX <<
" = 0;\n";
132 std::string
Generate(std::string opName)
override {
136 opName =
"op_" + opName;
138 throw std::runtime_error(
"TMVA SOFIE Operator NonZero called to Generate without being initialized first");
140 std::stringstream out;
142 size_t inputLength = 0;
144 if (!intShapeX.empty())
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;
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 <<
"[";
167 if (inputLength > 0) {
168 out << inputLength * j;
170 out << s_inputLength;
171 if (j > 1) out <<
" * " << j;
175 out << vnonzero <<
"] = i_" << j <<
";\n";
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;
182 for (
size_t j = dims; j > 0; j--) {
183 for (
size_t k = 0; k <j; k++) out <<
SP;
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;
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::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