Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Gelu.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_GELU
2#define TMVA_SOFIE_ROPERATOR_GELU
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
15{
16
17private:
18
19 std::string fNX;
20 std::string fNY;
21 std::string fApproximate; // "none" (exact) or "tanh" (approximate)
22 std::vector<size_t> fShape;
23
24public:
26 ROperator_Gelu(std::string nameX, std::string nameY, std::string approximate = "none"):
27 fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)), fApproximate(approximate){
30 }
31
32
33 void Initialize(RModel& model) override {
34 if (model.CheckIfTensorAlreadyExist(fNX) == false){
35 throw std::runtime_error("TMVA SOFIE Gelu Op Input Tensor is not found in model");
36 }
37 fShape = model.GetTensorShape(fNX);
39 }
40
41 std::string Generate(std::string OpName) override {
42 OpName = "op_" + OpName;
43 if (fShape.empty()){
44 throw std::runtime_error("TMVA SOFIE Operator Gelu called to Generate without being initialized first");
45 }
46 std::stringstream out;
47 int length = 1;
48 for(auto& i: fShape){
49 length *= i;
50 }
51 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
52 if (fApproximate == "tanh") {
53 // Tanh approximation: 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
54 out << SP << SP << "float x = tensor_" << fNX << "[id];\n";
55 out << SP << SP << "tensor_" << fNY << "[id] = 0.5f * x * (1.0f + std::tanh(0.7978845608028654f * (x + 0.044715f * x * x * x)));\n";
56 } else {
57 // Exact: 0.5 * x * (1 + erf(x / sqrt(2)))
58 out << SP << SP << "tensor_" << fNY << "[id] = 0.5f * tensor_" << fNX << "[id] * (1.0f + std::erf(tensor_" << fNX << "[id] * 0.7071067811865475f));\n";
59 }
60 out << SP << "}\n";
61 return out.str();
62 }
63
64 std::vector<std::string> GetStdLibs() override { return { std::string("cmath") };}
65};
66
67}//SOFIE
68}//Experimental
69}//TMVA
70
71
72#endif //TMVA_SOFIE_ROPERATOR_GELU
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
Definition RModel.cxx:51
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:284
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:144
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:112
ROperator_Gelu(std::string nameX, std::string nameY, std::string approximate="none")
void Initialize(RModel &model) override
std::vector< std::string > GetStdLibs() override
std::string Generate(std::string OpName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:48
create variable transformations