Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tmva/sofie/inc/TMVA/ROperator_Gelu.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef TMVA_SOFIE_ROPERATOR_GELU
#define TMVA_SOFIE_ROPERATOR_GELU

#include "TMVA/SOFIE_common.hxx"
#include "TMVA/ROperator.hxx"
#include "TMVA/RModel.hxx"

#include <sstream>

namespace TMVA{
namespace Experimental{
namespace SOFIE{

template <typename T>
class ROperator_Gelu final : public ROperator
{

private:

std::string fNX;
std::string fNY;
std::vector<Dim> fShape;

public:
ROperator_Gelu(){}
ROperator_Gelu(std::string nameX, std::string nameY):
fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){
fInputTensorNames = { fNX };
fOutputTensorNames = { fNY };
}

std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
return input;
}

std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
auto ret = input; //suggest copy to compiler
return ret;
}

void Initialize(RModel& model) override {
if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor
throw std::runtime_error("TMVA SOFIE Gelu Op Input Tensor " + fNX + " is not found in model");
}

fShape = model.GetDimTensorShape(fNX);

model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
if (model.Verbose()) {
std::cout << "Gelu : " << fNX << " -> " << fNY << " " << ConvertShapeToString(fShape) << std::endl;
}
}

std::string Generate(std::string OpName) override {
OpName = "op_" + OpName;
if (fShape.empty()) {
throw std::runtime_error("TMVA SOFIE Operator Gelu called to Generate without being initialized first");
}
std::stringstream out;
auto length = ConvertDynamicShapeToLength(fShape);
out << "\n//------ GELU\n";
out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
out << SP << SP
<< "tensor_" << fNY << "[id] = 0.5 * tensor_" << fNX << "[id] * "
<< "(1 + std::tanh(0.7978845608 * "
<< "(tensor_" << fNX << "[id] + 0.044715 * "
<< "tensor_" << fNX << "[id] * tensor_" << fNX << "[id] * tensor_" << fNX << "[id])));\n";
out << SP << "}\n";
return out.str();
}

};

}//SOFIE
}//Experimental
}//TMVA


#endif //TMVA_SOFIE_ROPERATOR_GELU
1 change: 1 addition & 0 deletions tmva/sofie_parsers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ROOT_STANDARD_LIBRARY_PACKAGE(ROOTTMVASofieParser
src/ParsePool.cxx
src/ParseReduce.cxx
src/ParseRelu.cxx
src/ParseGelu.cxx
src/ParseReshape.cxx
src/ParseRNN.cxx
src/ParseSelu.cxx
Expand Down
39 changes: 39 additions & 0 deletions tmva/sofie_parsers/src/ParseGelu.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "TMVA/RModelParser_ONNX.hxx"
#include "TMVA/ROperator_Gelu.hxx"
#include "onnx_proto3.pb.h"

namespace TMVA {
namespace Experimental {
namespace SOFIE {

ParserFuncSignature ParseGelu = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
ETensorType input_type;

auto input_name = nodeproto.input(0);
if (parser.IsRegisteredTensorType(input_name)) {
input_type = parser.GetTensorType(input_name);
} else {
throw std::runtime_error("TMVA::SOFIE ONNX Parser Gelu op has input tensor" + input_name +
" but its type is not yet registered");
}

std::unique_ptr<ROperator> op;
std::string output_name = nodeproto.output(0);

switch (input_type) {
case ETensorType::FLOAT: op.reset(new ROperator_Gelu<float>(input_name, output_name)); break;
default:
throw std::runtime_error("TMVA::SOFIE - Unsupported - Operator Gelu does not yet support input type " +
std::to_string(static_cast<int>(input_type)));
}

if (!parser.IsRegisteredTensorType(output_name)) {
parser.RegisterTensorType(output_name, input_type);
}

return op;
};

} // namespace SOFIE
} // namespace Experimental
} // namespace TMVA
2 changes: 2 additions & 0 deletions tmva/sofie_parsers/src/RModelParser_ONNX.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern ParserFuncSignature ParseReduceProd;
extern ParserFuncSignature ParseBatchNormalization;
extern ParserFuncSignature ParseConstant;
extern ParserFuncSignature ParseTranspose;
extern ParserFuncSignature ParseGelu;
extern ParserFuncSignature ParseRelu;
extern ParserFuncSignature ParseTanh;
extern ParserFuncSignature ParseConv;
Expand Down Expand Up @@ -201,6 +202,7 @@ RModelParser_ONNX::RModelParser_ONNX() noexcept : fOperatorsMapImpl(std::make_un
RegisterOperator("AveragePool", ParsePool);
RegisterOperator("GlobalAveragePool", ParsePool);
RegisterOperator("MaxPool", ParsePool);
RegisterOperator("Gelu", ParseGelu);
RegisterOperator("Relu", ParseRelu);
RegisterOperator("Reshape", ParseReshape);
RegisterOperator("Flatten", ParseReshape);
Expand Down