1#ifndef TMVA_SOFIE_ROperator_Where
2#define TMVA_SOFIE_ROperator_Where
12namespace Experimental{
63 if (!model.CheckIfTensorAlreadyExist(
fNX)){
64 throw std::runtime_error(std::string(
"TMVA SOFIE Where Op Input Tensor ") +
fNX +
"is not found in model");
66 if (!model.CheckIfTensorAlreadyExist(
fNY)) {
67 throw std::runtime_error(std::string(
"TMVA SOFIE Where Op Input Tensor ") +
fNY +
"is not found in model");
69 if (!model.CheckIfTensorAlreadyExist(
fNC)) {
70 throw std::runtime_error(std::string(
"TMVA SOFIE Where Op Input Tensor ") +
fNC +
"is not found in model");
73 if (model.IsReadyInputTensor(
fNC))
81 if (model.IsDynamicTensor(
fNC)) {
88 if (model.IsDynamicTensor(
fNX)) {
95 if (model.IsDynamicTensor(
fNY)) {
104 if (model.Verbose()) {
127 auto padToRank = [&](std::vector<size_t> &shape) {
128 if (shape.size() <
fShapeZ.size()) {
130 shape.insert(shape.begin(),
nPrepend, 1);
144 if (model.IsInitializedTensor(
fNX)) {
145 auto data = model.GetInitializedTensorData(
fNX);
148 std::default_delete<T[]>());
157 if (model.IsInitializedTensor(
fNY)) {
158 auto data = model.GetInitializedTensorData(
fNY);
161 std::default_delete<T[]>());
170 if (model.IsInitializedTensor(
fNC)) {
171 auto data = model.GetInitializedTensorData(
fNC);
174 std::default_delete<T[]>());
184 if (model.IsInitializedTensor(
fNC)) {
186 auto dataC =
static_cast<bool *
>(model.GetInitializedTensorData(
nameC).get());
187 model.SetNotWritableInitializedTensor(
nameC);
192 if (model.IsInitializedTensor(
fNX)) {
194 dataX =
static_cast<T *
>(model.GetInitializedTensorData(
nameX).get());
196 model.SetNotWritableInitializedTensor(
nameX);
197 }
else if (model.IsShapeTensor(
fNX)) {
200 if (model.IsInitializedTensor(
fNY)) {
202 dataY =
static_cast<T *
>(model.GetInitializedTensorData(
nameY).get());
203 model.SetNotWritableInitializedTensor(
nameY);
204 }
else if (model.IsShapeTensor(
fNY)) {
207 std::vector<T>
dataZ;
216 for (
size_t i = 0; i <
dataZ.size(); i++)
222 for (
size_t i = 0; i <
shapeDataZ.size(); i++) {
231 for (
size_t i = 0; i <
shapeDataZ.size(); i++) {
240 for (
size_t i = 0; i <
shapeDataZ.size(); i++) {
250 if (
dataZ.size() > 0)
260 << ((
dataZ.size() > 0) ?
" (constant)" :
" (shape)") << std::endl;
269 model.AddIntermediateTensor(
fNZ, model.GetTensorType(
fNX),
fShapeZ);
291 for (
auto &
input : model.GetInputTensorNames())
292 for (
auto &s : model.GetDimTensorShape(
input))
293 if (s.isParam && s.param ==
p)
return true;
296 for (
size_t i = 0; i <
fDimShapeZ.size(); i++) {
298 if (s.isParam && s.param.find(
"std::max") != std::string::npos) {
333 std::stringstream out;
340 std::stringstream out;
354 for (
size_t i = 0; i <
fDimShapeZ.size(); i++) {
360 <<
"throw std::runtime_error(\"SOFIE Where: cannot broadcast A dim " << i <<
" in " <<
opName <<
"\");\n";
367 <<
"throw std::runtime_error(\"SOFIE Where: cannot broadcast B dim " << i <<
" in " <<
opName <<
"\");\n";
374 <<
"throw std::runtime_error(\"SOFIE Where: cannot broadcast C dim " << i <<
" in " <<
opName <<
"\");\n";
389 const std::vector<Dim> &strides,
390 size_t rankZ) -> std::string {
393 [](
Dim d) { return d.dim == 1 || d.GetVal() ==
"1"; }))
397 for (
size_t i = 0; i <
dimShape.size(); ++i) {
400 if (strides[i].GetVal() !=
"1")
401 expr +=
" * " + strides[i].GetVal();
404 if (
expr.size() >= 3)
405 for (
int j = 0;
j < 3;
j++)
expr.pop_back();
419 [](
Dim d) { return d.dim == 1 || d.GetVal() ==
"1"; })) {
422 for (
size_t i = 0; i <
fDimShapeZ.size(); ++i) {
426 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i
427 <<
" < " <<
fDimShapeZ[i] <<
"; ++idx_" << i <<
") {\n";
428 idxZ +=
"idx_" + std::to_string(i);
434 if (
idxZ.size() >= 3)
435 for (
int j = 0;
j < 3;
j++)
idxZ.pop_back();
439 for (
int j = 0;
j <
nloop + 1;
j++) out <<
SP;
440 out <<
"tensor_" <<
fNZ <<
"[" <<
idxZ <<
"] = "
441 <<
"tensor_" <<
fNC <<
"[" <<
idxC <<
"] ? "
442 <<
"tensor_" <<
fNX <<
"[" <<
idxX <<
"] : "
443 <<
"tensor_" <<
fNY <<
"[" <<
idxY <<
"];\n";
446 for (
int i =
nloop; i > 0; i--) {
447 for (
int j = 0;
j < i;
j++) out <<
SP;
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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 input
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 offset
const_iterator begin() const
const_iterator end() const
std::vector< Dim > fDimShapeX
std::vector< size_t > fShapeY
std::vector< size_t > fShapeX
std::vector< size_t > fShapeC
std::string fNBroadcastedX
std::string Generate(std::string opName) override
std::vector< Dim > fDimShapeY
ROperator_Where(const std::string &nameC, const std::string &nameX, const std::string &nameY, const std::string &nameZ)
void Initialize(RModel &model) override
std::vector< Dim > fDimShapeZ
std::string fNBroadcastedC
std::string fNBroadcastedY
std::vector< Dim > fDimShapeC
std::vector< size_t > fShapeZ
std::string GenerateInitCode() override
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
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
T * UnidirectionalBroadcast(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape)
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< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations