forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 1
Reduce operator 1 #23
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
Neel-Shah-29
wants to merge
8
commits into
lmoneta:master
Choose a base branch
from
Neel-Shah-29:Reduce-Operator-1
base: master
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
8 commits
Select commit
Hold shift + click to select a range
6e856a1
Reduce Operator Initial Code pushed
Neel-Shah-29 6e10bcc
Reduce Operator axis attribute added
Neel-Shah-29 3e02561
Made the required changes according the expected generated code and s…
Neel-Shah-29 e8167d2
Made the required changes to resolve the generated code errors
Neel-Shah-29 21e8ee9
Fix generated code for Reduce operator
lmoneta 75d47d7
Reduce Operator code cleaned and fixed the warning for int to size_t …
Neel-Shah-29 2333e21
Reduce ONNX Operator warning fixed related to size_t and int
Neel-Shah-29 0485065
Changes related to Reduce Prod done and sum updated to 1 for ReduceProd
Neel-Shah-29 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| #ifndef TMVA_SOFIE_ROPERATOR_Reduce | ||
| #define TMVA_SOFIE_ROPERATOR_Reduce | ||
|
|
||
| #include "TMVA/SOFIE_common.hxx" | ||
| #include "TMVA/ROperator.hxx" | ||
| #include "TMVA/RModel.hxx" | ||
|
|
||
| #include <memory> | ||
| #include <sstream> | ||
| #include <algorithm> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
| #include <cassert> | ||
|
|
||
| namespace TMVA{ | ||
| namespace Experimental{ | ||
| namespace SOFIE{ | ||
|
|
||
| enum EReduceOpMode { ReduceMean, ReduceSumsquare, ReduceProd, InvalidReduceOp }; | ||
|
|
||
| template <typename T, EReduceOpMode Op> | ||
| class ROperator_Reduce final : public ROperator | ||
| { | ||
| private: | ||
| /* Attributes*/ | ||
| int fkeepdims = 1; //default value | ||
| int fAttrAxes; | ||
| EReduceOpMode fReduceOpMode; | ||
| std::string fNX; | ||
| std::string fNY; | ||
| std::vector<size_t> fShapeX; | ||
| std::vector<size_t> fShapeY; | ||
|
|
||
|
|
||
| public: | ||
|
|
||
| std::string Name() { | ||
| if (fReduceOpMode == ReduceMean) return "ReduceMean"; | ||
| else if (fReduceOpMode == ReduceSumsquare ) return "ReduceSumsquare"; | ||
| else if (fReduceOpMode == ReduceProd ) return "ReduceProd"; | ||
| return "Invalid"; | ||
| } | ||
|
|
||
| ROperator_Reduce(){} | ||
| ROperator_Reduce(int keepdims,int attrAxes,std::string nameX, std::string nameY): | ||
| fkeepdims(keepdims), fAttrAxes(attrAxes), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)) { | ||
| fReduceOpMode = Op; | ||
| } | ||
|
|
||
| // type of output given input | ||
| std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){ | ||
| return input; | ||
| } | ||
|
|
||
| // shape of output tensors given input tensors | ||
| std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){ | ||
| auto ret = input; //suggest copy to compiler | ||
| ret[0][fAttrAxes] = 1; | ||
| return ret; | ||
| } | ||
| void Initialize(RModel& model){ | ||
|
|
||
| fUseSession = model.UseSession(); | ||
|
|
||
| if (model.CheckIfTensorAlreadyExist(fNX) == false){ //input must be a graph input, or already initialized intermediate tensor | ||
| throw std::runtime_error("TMVA SOFIE Reduce Op Input Tensor " + fNX + " is not found in model"); | ||
| } | ||
| fShapeX = model.GetTensorShape(fNX); | ||
| // find shape of Y and add it in the list of intermediate tensors | ||
| fShapeY = ShapeInference({fShapeX})[0]; | ||
| model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY); | ||
|
|
||
| } | ||
|
|
||
| std::string Generate(std::string OpName){ | ||
| OpName = "op_" + OpName; | ||
| if (fShapeX.empty() || fShapeY.empty()) { | ||
| throw std::runtime_error("TMVA SOFIE Reduce Op called to Generate without being initialized first"); | ||
| } | ||
|
|
||
| size_t outputLength = TMVA::Experimental::SOFIE::ConvertShapeToLength(fShapeY); | ||
|
|
||
| auto inputStrides = TMVA::Experimental::SOFIE::UTILITY::ComputeStrideFromShape(fShapeX); | ||
| auto outputStrides = TMVA::Experimental::SOFIE::UTILITY::ComputeStrideFromShape(fShapeY); | ||
|
|
||
| // write here according to size of shape | ||
| // in generation code can be done automatically | ||
| // i0 = i / s0 ; i1 = (i % s0) / s1 ; i2 = ( (i % s0) % s1 ) / s2 and so on | ||
| // and we have for the inverse | ||
| // i = i0 * s0 + i1 * s1 + i2 * s2 + i3 * s3 .... | ||
|
|
||
| // don't need to divide by last stride s[n-1] since it is 1 by definition | ||
|
|
||
| std::stringstream out; | ||
| out << "\n//---- operator " << Name() << " " << OpName << "\n"; | ||
| out << SP << "for (size_t i = 0; i < " << outputLength << "; i++) {\n"; | ||
|
|
||
| size_t dim = fShapeX.size(); // this is the input dimension (e.g. 2, 3 or 4 or more) | ||
|
|
||
| // here we find output indices | ||
| out << SP << SP << "size_t idx_0 = i / " << outputStrides[0] << ";\n" ; | ||
| out << SP << SP << "size_t itmp = i;\n"; | ||
| for (size_t k = 1; k < dim; k++) { | ||
| out << SP << SP << "itmp = itmp % " << outputStrides[k-1] << ";\n" ; | ||
| if (k < dim-1) | ||
| out << SP << SP << "size_t idx_" << k << " = itmp / " << outputStrides[k] << ";\n" ; | ||
| else | ||
| // to avoid division by 1 which is outputStrides[dim-1] | ||
| out << SP << SP << "size_t idx_" << k << " = itmp;\n"; | ||
| } | ||
|
|
||
| // compute reduction | ||
|
|
||
| if(fReduceOpMode == ReduceProd) | ||
| out << SP << SP << "float sum = 1;\n"; | ||
| else | ||
| out << SP << SP << "float sum = 0;\n"; | ||
|
|
||
| out << SP << SP << "for (size_t k = 0; k < " << fShapeX[fAttrAxes] <<"; k++) { \n"; | ||
| out << SP << SP << SP << "idx_" << fAttrAxes << " = k;\n"; | ||
| // compute input index j | ||
| out << SP << SP << SP << "size_t l = "; | ||
| for(int n = dim-1; n >=0; n--) { | ||
| if (n == int(dim-1)) | ||
| out << "idx_" << n; | ||
| else | ||
| out << " + " << "idx_" << n << " * " << inputStrides[n]; | ||
| } | ||
| out << ";\n"; | ||
|
|
||
| if(fReduceOpMode == ReduceMean){ | ||
| out << SP << SP << SP << "sum += tensor_" << fNX << "[l];\n"; | ||
| out << SP << SP << "}\n"; | ||
| out << SP << SP << "float reduceResult = sum/static_cast<float>(" << fShapeX[fAttrAxes] << ");\n"; | ||
| } | ||
| else if(fReduceOpMode == ReduceSumsquare){ | ||
| out << SP << SP << SP << "sum += tensor_" << fNX << "[l] * tensor_" << fNX << "[l];\n"; | ||
| out << SP << SP << "}\n"; | ||
| out << SP << SP << "float reduceResult = sum;\n"; | ||
| } | ||
| else if(fReduceOpMode == ReduceProd){ | ||
| out << SP << SP << SP << "sum *= tensor_" << fNX << "[l];\n"; | ||
| out << SP << SP << "}\n"; | ||
| out << SP << SP << "float reduceResult = sum;\n"; | ||
| } | ||
|
|
||
| out << SP << SP << "tensor_" << fNY << "[i] = reduceResult;\n"; | ||
| out << SP << "}\n"; | ||
| return out.str(); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| }//SOFIE | ||
| }//Experimental | ||
| }//TMVA | ||
|
|
||
|
|
||
| #endif //TMVA_SOFIE_ROPERATOR_Reduce | ||
|
|
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
Binary file not shown.
Binary file not shown.
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,5 @@ | ||
| namespace ReduceMean_ExpectedOutput{ | ||
| float output[] = { | ||
| 5.0, 3.5, 3.5 | ||
| }; | ||
| } // namespace Reduce_mean_ExpectedOutput |
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,5 @@ | ||
| namespace ReduceProd_ExpectedOutput{ | ||
| float output[] = { | ||
| 25.0, 10.0, 12.0 | ||
| }; | ||
| } // namespace Reduce_mean_ExpectedOutput |
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
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.
Uh oh!
There was an error while loading. Please reload this page.