1#ifndef TMVA_SOFIE_ROPERATOR_GELU
2#define TMVA_SOFIE_ROPERATOR_GELU
26 ROperator_Gelu(std::string nameX, std::string nameY, std::string approximate =
"none"):
34 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
35 throw std::runtime_error(
"TMVA SOFIE Gelu Op Input Tensor is not found in model");
38 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShape);
41 std::string
Generate(std::string OpName)
override {
42 OpName =
"op_" + OpName;
44 throw std::runtime_error(
"TMVA SOFIE Operator Gelu called to Generate without being initialized first");
46 std::stringstream out;
51 out <<
SP <<
"for (int id = 0; id < " << length <<
" ; id++){\n";
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";
58 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[id] = 0.5f * tensor_" <<
fNX <<
"[id] * (1.0f + std::erf(tensor_" <<
fNX <<
"[id] * 0.7071067811865475f));\n";
64 std::vector<std::string>
GetStdLibs()
override {
return { std::string(
"cmath") };}
ROperator_Gelu(std::string nameX, std::string nameY, std::string approximate="none")
void Initialize(RModel &model) override
std::vector< size_t > fShape
std::vector< std::string > GetStdLibs() override
std::string Generate(std::string OpName) override
std::vector< std::string_view > fInputTensorNames
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
create variable transformations