-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[tmva][sofie] Add GELU activation operator #20788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Shlok-Saxena
wants to merge
4
commits into
root-project:master
from
Shlok-Saxena:feature/sofie-gelu-operator
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93f8ddf
TMVA(SOFIE): add GELU activation operator
Shlok-Saxena 43122f6
SOFIE: register GELU in operator list
Shlok-Saxena 76e8fc3
[tmva][sofie] Use erf-based GELU definition
Shlok-Saxena 2194bdb
[tmva][sofie] Handle ONNX Gelu approximate attribute
Shlok-Saxena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #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> | ||
| #include <cmath> | ||
|
|
||
| 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){ | ||
| 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 (exact, erf-based)\n"; | ||
| out << SP << "for (int id = 0; id < " << length << " ; id++){\n"; | ||
| out << SP << SP | ||
| << "tensor_" << fNY << "[id] = " | ||
| << "tensor_" << fNX << "[id] * 0.5 * " | ||
| << "(1.0 + std::erf(tensor_" << fNX << "[id] / std::sqrt(2.0)));\n"; | ||
| out << SP << "}\n"; | ||
|
|
||
| return out.str(); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| }//SOFIE | ||
| }//Experimental | ||
| }//TMVA | ||
|
|
||
| #endif //TMVA_SOFIE_ROPERATOR_GELU |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #include "TMVA/RModelParser_ONNX.hxx" | ||
| #include "TMVA/ROperator_Gelu.hxx" | ||
| #include "onnx_proto3.pb.h" | ||
|
|
||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace TMVA { | ||
| namespace Experimental { | ||
| namespace SOFIE { | ||
|
|
||
| ParserFuncSignature ParseGelu = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) { | ||
| ETensorType input_type; | ||
|
|
||
| // --- Handle ONNX Gelu attribute: approximate --- | ||
| // ONNX Gelu has attribute "approximate": "none" (default) or "tanh" | ||
| std::string approximate = "none"; | ||
| for (const auto &attr : nodeproto.attribute()) { | ||
| if (attr.name() == "approximate") { | ||
| if (attr.type() != onnx::AttributeProto::STRING) | ||
| throw std::runtime_error( | ||
| "TMVA::SOFIE ONNX Parser: Gelu attribute 'approximate' must be a string"); | ||
|
|
||
| approximate = attr.s(); | ||
| } | ||
| } | ||
|
|
||
| if (approximate != "none") { | ||
| if (approximate == "tanh") { | ||
| throw std::runtime_error( | ||
| "TMVA::SOFIE ONNX Parser: Gelu attribute approximate='tanh' not supported yet"); | ||
| } | ||
|
|
||
| throw std::runtime_error( | ||
| "TMVA::SOFIE ONNX Parser: Gelu attribute approximate='" + approximate + | ||
| "' not supported (expected 'none' or 'tanh')"); | ||
| } | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will be the additional changes we will need to enable supporting
tanhapproximation?