Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_PRelu.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_PRelu
2#define TMVA_SOFIE_ROPERATOR_PRelu
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA {
11namespace Experimental {
12namespace SOFIE {
13
14template <typename T>
15class ROperator_PRelu final : public ROperator {
16private:
17 /* Attributes */
18 std::string fNX;
19 std::string fNSlope;
20 std::string fNY;
21 std::vector<size_t> fShapeX;
22 std::vector<size_t> fShapeSlope;
23 std::string fType;
24
25public:
27 ROperator_PRelu(std::string nameX, std::string nameSlope, std::string nameY)
28 : fNX(UTILITY::Clean_name(nameX)), fNSlope(UTILITY::Clean_name(nameSlope)), fNY(UTILITY::Clean_name(nameY))
29 {
30 if (std::is_same<T, float>::value) {
31 fType = "float";
32 } else {
33 throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a PRelu operator");
34 }
37 }
38 void Initialize(RModel &model) override
39 {
40 if (model.CheckIfTensorAlreadyExist(fNX) == false || model.CheckIfTensorAlreadyExist(fNSlope) == false) {
41 throw std::runtime_error("TMVA SOFIE PRelu Op Input Tensor is not found in model");
42 }
43 fShapeX = model.GetTensorShape(fNX);
44 fShapeSlope = model.GetTensorShape(fNSlope);
45
46 // check that slope is unidirectionally broadcastable to X (throws otherwise);
47 // this also prepends 1s to fShapeSlope so that it has the same rank as X
48 try {
50 } catch (const std::runtime_error &e) {
51 throw std::runtime_error("TMVA SOFIE PRelu Op slope tensor " + fNSlope +
52 " cannot be broadcast to input tensor " + fNX + ": " + e.what());
53 }
54
55 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeX);
56 }
57 std::string Generate(std::string OpName) override
58 {
59 OpName = "op_" + OpName;
60
61 if (fShapeX.empty() || fShapeSlope.empty()) {
62 throw std::runtime_error("TMVA SOFIE Operator PRelu called to Generate without being initialized first");
63 }
64
67
68 std::string slopeIdx;
70 slopeIdx = "id";
71 } else {
72 for (size_t k = 0; k < fShapeSlope.size(); k++) {
73 if (fShapeSlope[k] == 1)
74 continue;
75 if (!slopeIdx.empty())
76 slopeIdx += " + ";
77 if (stridesX[k] == 1)
78 slopeIdx += "id % " + std::to_string(fShapeX[k]);
79 else
80 slopeIdx += "(id / " + std::to_string(stridesX[k]) + ") % " + std::to_string(fShapeX[k]);
81 if (stridesSlope[k] != 1)
82 slopeIdx += " * " + std::to_string(stridesSlope[k]);
83 }
84 if (slopeIdx.empty())
85 slopeIdx = "0";
86 }
87 std::stringstream out;
89 out << "\n//------ PRELU\n";
90 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
91 out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX
92 << "[id] : tensor_" << fNSlope << "[" << slopeIdx << "] * tensor_" << fNX << "[id]);\n";
93 out << SP << "}\n";
94 return out.str();
95 }
96};
97
98} // namespace SOFIE
99} // namespace Experimental
100} // namespace TMVA
101
102#endif // TMVA_SOFIE_ROPERATOR_PRelu
#define e(i)
Definition RSha256.hxx:103
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
ROperator_PRelu(std::string nameX, std::string nameSlope, std::string nameY)
std::string Generate(std::string OpName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:44
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:40
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:45
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t > &, std::vector< size_t > &)
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::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
create variable transformations