-
Notifications
You must be signed in to change notification settings - Fork 17
Add Alpaka backend support for HardSigmoid, HardSwish, and Softplus #13
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
Open
AdityaDRathore
wants to merge
3
commits into
ML4EP:gpu/alpaka
Choose a base branch
from
AdityaDRathore:feature/hardsigmoid-hardwish-softplus-op
base: gpu/alpaka
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,101 @@ | ||
| #ifndef SOFIE_ROPERATOR_HARDSIGMOID | ||
| #define SOFIE_ROPERATOR_HARDSIGMOID | ||
|
|
||
| #include <SOFIE/SOFIE_common.hxx> | ||
| #include <SOFIE/ROperator.hxx> | ||
| #include <SOFIE/RModel.hxx> | ||
|
|
||
| #include <sstream> | ||
|
|
||
| namespace SOFIE { | ||
|
|
||
| template <typename T> | ||
| class ROperator_HardSigmoid final : public ROperator | ||
| { | ||
|
|
||
| private: | ||
|
|
||
| std::string fNX; | ||
| std::string fNY; | ||
| std::vector<size_t> fShape; | ||
| float fAlpha; | ||
| float fBeta; | ||
|
|
||
| public: | ||
| ROperator_HardSigmoid(){} | ||
| ROperator_HardSigmoid(std::string nameX, std::string nameY, float alpha, float beta): | ||
| fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)), fAlpha(alpha), fBeta(beta){ | ||
| fInputTensorNames = { fNX }; | ||
| fOutputTensorNames = { fNY }; | ||
| fKind = OperatorKind::HARDSIGMOID; | ||
| } | ||
|
|
||
| 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 { | ||
| return input; | ||
| } | ||
|
|
||
| void Initialize(RModel& model) override { | ||
| if (!model.CheckIfTensorAlreadyExist(fNX)){ | ||
| throw std::runtime_error("SOFIE HardSigmoid Op Input Tensor " + fNX + " is not found in model"); | ||
| } | ||
| fShape = model.GetTensorShape(fNX); | ||
| model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape); | ||
| } | ||
|
|
||
| std::string Generate(std::string OpName) override { | ||
| OpName = "op_" + OpName; | ||
| if (fShape.empty()){ | ||
| throw std::runtime_error("SOFIE HardSigmoid operator called to Generate without being initialized first"); | ||
| } | ||
| std::stringstream out; | ||
| size_t length = ConvertShapeToLength(fShape); | ||
|
|
||
| // HardSigmoid: y = max(0, min(1, alpha * x + beta)) | ||
| out << "\n//------ HardSigmoid\n"; | ||
| out << SP << "for (int id = 0; id < " << length << " ; id++){\n"; | ||
| out << SP << SP << "tensor_" << fNY << "[id] = std::fmax(0x0p+0f, std::fmin(0x1p+0f, " | ||
| << fAlpha << "f * tensor_" << fNX << "[id] + " << fBeta << "f));\n"; | ||
| out << SP << "}\n"; | ||
| return out.str(); | ||
| } | ||
| std::string Generate_GPU_Kernel_ALPAKA(std::string /*opName*/) override { | ||
| std::string op = "\n//------ HARDSIGMOID_KERNEL_ALPAKA\n"; | ||
| op += SP + "struct HardSigmoidKernel{\n"; | ||
| op += SP + SP + "template<typename TAcc, typename T>\n"; | ||
| op += SP + SP + "ALPAKA_FN_ACC void operator()(TAcc const & acc, T const * data, T * out, std::size_t numElements, T const alpha, T const beta) const {\n"; | ||
| op += SP + SP + SP + "const auto idx = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];\n"; | ||
| op += SP + SP + SP + "if (idx < numElements) {\n"; | ||
| op += SP + SP + SP + SP + "T x = data[idx];\n"; | ||
| op += SP + SP + SP + SP + "T h = alpha * x + beta;\n"; | ||
| op += SP + SP + SP + SP + "out[idx] = (h < T(0)) ? T(0) : ((h > T(1)) ? T(1) : h);\n"; | ||
| op += SP + SP + SP + "}\n"; | ||
| op += SP + SP + "}\n"; | ||
| op += SP + "};\n"; | ||
| return op; | ||
| } | ||
|
|
||
| std::string Generate_GPU_Kernel_Definitions_ALPAKA(std::string /*opName*/) override { | ||
| return SP + "HardSigmoidKernel hardSigmoidKernel;\n"; | ||
| } | ||
|
|
||
| std::string Generate_GPU_ALPAKA(std::string OpName) override { | ||
| std::stringstream out; | ||
| auto length = ConvertShapeToLength(fShape); | ||
| out << "\n//------ op_" << OpName << "_ALPAKA\n"; | ||
| out << SP << "auto const elementsPerThread_" << fNX << " = alpaka::Vec<Dim, Idx>::all(static_cast<Idx>(1));\n"; | ||
| out << SP << "auto const elementsPerGrid_" << fNX << " = alpaka::Vec<Dim, Idx>::all(static_cast<Idx>(" << length << "));\n"; | ||
| out << SP << "auto const workDiv_" << fNX << " = sofie_workdiv(elementsPerGrid_" << fNX << ");\n"; | ||
| out << SP << "auto task_op_" << OpName << " = alpaka::createTaskKernel<Acc>(workDiv_" << fNX << ", hardSigmoidKernel, alpaka::getPtrNative(deviceBuf_" << fNX << "), alpaka::getPtrNative(deviceBuf_" << fNY << "), static_cast<std::size_t>(" << length << "), static_cast<float>(" << fAlpha << "), static_cast<float>(" << fBeta << "));\n"; | ||
| out << SP << "alpaka::enqueue(queue, task_op_" << OpName << ");\n"; | ||
| return out.str(); | ||
| } | ||
| std::vector<std::string> GetStdLibs() override { return { std::string("cmath") };} | ||
| }; | ||
|
|
||
| } // namespace SOFIE | ||
|
|
||
| #endif |
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,103 @@ | ||
| #ifndef SOFIE_ROPERATOR_HARDSWISH | ||
| #define SOFIE_ROPERATOR_HARDSWISH | ||
|
|
||
| #include <SOFIE/SOFIE_common.hxx> | ||
| #include <SOFIE/ROperator.hxx> | ||
| #include <SOFIE/RModel.hxx> | ||
|
|
||
| #include <sstream> | ||
|
|
||
| namespace SOFIE { | ||
|
|
||
| template <typename T> | ||
| class ROperator_HardSwish final : public ROperator | ||
| { | ||
|
|
||
| private: | ||
|
|
||
| std::string fNX; | ||
| std::string fNY; | ||
| std::vector<size_t> fShape; | ||
|
|
||
| public: | ||
| ROperator_HardSwish(){} | ||
| ROperator_HardSwish(std::string nameX, std::string nameY): | ||
| fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){ | ||
| fInputTensorNames = { fNX }; | ||
| fOutputTensorNames = { fNY }; | ||
| fKind = OperatorKind::HARDSWISH; | ||
| } | ||
|
|
||
| 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 { | ||
| return input; | ||
| } | ||
|
|
||
| void Initialize(RModel& model) override { | ||
| if (!model.CheckIfTensorAlreadyExist(fNX)){ | ||
| throw std::runtime_error("SOFIE HardSwish Op Input Tensor " + fNX + " is not found in model"); | ||
| } | ||
| fShape = model.GetTensorShape(fNX); | ||
| model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape); | ||
| } | ||
|
|
||
| std::string Generate(std::string OpName) override { | ||
| OpName = "op_" + OpName; | ||
| if (fShape.empty()){ | ||
| throw std::runtime_error("SOFIE HardSwish operator called to Generate without being initialized first"); | ||
| } | ||
| std::stringstream out; | ||
| size_t length = ConvertShapeToLength(fShape); | ||
|
|
||
| // HardSwish: y = x * max(0, min(1, x/6 + 0.5)) | ||
| // Split topology for debuggability | ||
| out << "\n//------ HardSwish\n"; | ||
| out << SP << "for (int id = 0; id < " << length << " ; id++){\n"; | ||
| out << SP << SP << "float h = 0x1.5555555555555p-3f * tensor_" << fNX << "[id] + 0x1p-1f;\n"; | ||
| out << SP << SP << "tensor_" << fNY << "[id] = tensor_" << fNX | ||
| << "[id] * std::fmax(0x0p+0f, std::fmin(0x1p+0f, h));\n"; | ||
| out << SP << "}\n"; | ||
| return out.str(); | ||
| } | ||
|
|
||
| std::string Generate_GPU_Kernel_ALPAKA(std::string /*opName*/) override { | ||
| std::string op = "\n//------ HARDSWISH_KERNEL_ALPAKA\n"; | ||
| op += SP + "struct HardSwishKernel{\n"; | ||
| op += SP + SP + "template<typename TAcc, typename T>\n"; | ||
| op += SP + SP + "ALPAKA_FN_ACC void operator()(TAcc const & acc, T const * data, T * out, std::size_t numElements) const {\n"; | ||
| op += SP + SP + SP + "const auto idx = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[0];\n"; | ||
| op += SP + SP + SP + "if (idx < numElements) {\n"; | ||
| op += SP + SP + SP + SP + "T x = data[idx];\n"; | ||
| op += SP + SP + SP + SP + "T h = T(0x1.5555555555555p-3) * x + T(0.5);\n"; | ||
| op += SP + SP + SP + SP + "out[idx] = x * ((h < T(0)) ? T(0) : ((h > T(1)) ? T(1) : h));\n"; | ||
| op += SP + SP + SP + "}\n"; | ||
| op += SP + SP + "}\n"; | ||
| op += SP + "};\n"; | ||
| return op; | ||
| } | ||
|
|
||
| std::string Generate_GPU_Kernel_Definitions_ALPAKA(std::string /*opName*/) override { | ||
| return SP + "HardSwishKernel hardSwishKernel;\n"; | ||
| } | ||
|
|
||
| std::string Generate_GPU_ALPAKA(std::string OpName) override { | ||
| std::stringstream out; | ||
| auto length = ConvertShapeToLength(fShape); | ||
| out << "\n//------ op_" << OpName << "_ALPAKA\n"; | ||
| out << SP << "auto const elementsPerThread_" << fNX << " = alpaka::Vec<Dim, Idx>::all(static_cast<Idx>(1));\n"; | ||
| out << SP << "auto const elementsPerGrid_" << fNX << " = alpaka::Vec<Dim, Idx>::all(static_cast<Idx>(" << length << "));\n"; | ||
| out << SP << "auto const workDiv_" << fNX << " = sofie_workdiv(elementsPerGrid_" << fNX << ");\n"; | ||
| out << SP << "auto task_op_" << OpName << " = alpaka::createTaskKernel<Acc>(workDiv_" << fNX << ", hardSwishKernel, alpaka::getPtrNative(deviceBuf_" << fNX << "), alpaka::getPtrNative(deviceBuf_" << fNY << "), static_cast<std::size_t>(" << length << "));\n"; | ||
| out << SP << "alpaka::enqueue(queue, task_op_" << OpName << ");\n"; | ||
| return out.str(); | ||
| } | ||
|
|
||
| std::vector<std::string> GetStdLibs() override { return { std::string("cmath") };} | ||
| }; | ||
|
|
||
| } // namespace SOFIE | ||
|
|
||
| #endif |
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,6 +33,8 @@ target_include_directories(SOFIE_parsers | |||||||||
| set(sources_cxx | ||||||||||
| src/RModelParser_ONNX.cxx | ||||||||||
| src/ParseBasicUnary.cxx | ||||||||||
| ParseHardSigmoid.cxx | ||||||||||
| ParseHardSwish.cxx | ||||||||||
|
Comment on lines
+36
to
+37
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| src/ParseBasicBinary.cxx | ||||||||||
| src/ParseBasicIs.cxx | ||||||||||
| src/ParseBatchNormalization.cxx | ||||||||||
|
|
||||||||||
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,47 @@ | ||
| #include "SOFIE/RModelParser_ONNX.hxx" | ||
| #include "SOFIE/ROperator_HardSigmoid.hxx" | ||
| #include "onnx_proto3.pb.h" | ||
|
|
||
| namespace SOFIE { | ||
|
|
||
| ParserFuncSignature ParseHardSigmoid = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) { | ||
| ETensorType input_type; | ||
|
|
||
| // ONNX spec defaults: alpha=0.2, beta=0.5 | ||
| float alpha = 0.2f; | ||
| float beta = 0.5f; | ||
|
|
||
| for (int_t i = 0; i < nodeproto.attribute_size(); i++) { | ||
| std::string attribute_name = nodeproto.attribute(i).name(); | ||
| if (attribute_name == "alpha") | ||
| alpha = nodeproto.attribute(i).f(); | ||
| else if (attribute_name == "beta") | ||
| beta = nodeproto.attribute(i).f(); | ||
| } | ||
|
|
||
| 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 HardSigmoid 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_HardSigmoid<float>(input_name, output_name, alpha, beta)); break; | ||
| default: | ||
| throw std::runtime_error("TMVA::SOFIE - Unsupported - Operator HardSigmoid 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 |
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,35 @@ | ||
| #include "SOFIE/RModelParser_ONNX.hxx" | ||
| #include "SOFIE/ROperator_HardSwish.hxx" | ||
| #include "onnx_proto3.pb.h" | ||
|
|
||
| namespace SOFIE { | ||
|
|
||
| ParserFuncSignature ParseHardSwish = [](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 HardSwish 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_HardSwish<float>(input_name, output_name)); break; | ||
| default: | ||
| throw std::runtime_error("TMVA::SOFIE - Unsupported - Operator HardSwish 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 |
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
Oops, something went wrong.
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.