-
Notifications
You must be signed in to change notification settings - Fork 6k
New class: op_version_registry, test=develop #26542
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
Merged
Merged
Changes from all commits
Commits
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,15 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #include "paddle/fluid/framework/op_version_registry.h" |
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,123 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <boost/any.hpp> | ||
| #include "paddle/fluid/framework/framework.pb.h" | ||
| #include "paddle/fluid/platform/enforce.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| namespace compatible { | ||
|
|
||
| struct OpUpdateRecord { | ||
| enum class Type { kInvalid = 0, kModifyAttr, kNewAttr }; | ||
| Type type_; | ||
| std::string remark_; | ||
| }; | ||
|
|
||
| struct ModifyAttr : OpUpdateRecord { | ||
| ModifyAttr(const std::string& name, const std::string& remark, | ||
| boost::any default_value) | ||
| : OpUpdateRecord({Type::kModifyAttr, remark}), | ||
| name_(name), | ||
| default_value_(default_value) { | ||
| // TODO(Shixiaowei02): Check the data type with proto::OpDesc. | ||
| } | ||
|
|
||
| private: | ||
| std::string name_; | ||
| boost::any default_value_; | ||
| }; | ||
| struct NewAttr : OpUpdateRecord { | ||
| NewAttr(const std::string& name, const std::string& remark) | ||
| : OpUpdateRecord({Type::kNewAttr, remark}), name_(name) {} | ||
|
|
||
| private: | ||
| std::string name_; | ||
| }; | ||
|
|
||
Shixiaowei02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| class OpVersionDesc { | ||
| public: | ||
| OpVersionDesc& ModifyAttr(const std::string& name, const std::string& remark, | ||
| boost::any default_value) { | ||
| infos_.push_back(std::shared_ptr<OpUpdateRecord>( | ||
| new compatible::ModifyAttr(name, remark, default_value))); | ||
| return *this; | ||
| } | ||
|
|
||
| OpVersionDesc& NewAttr(const std::string& name, const std::string& remark) { | ||
| infos_.push_back( | ||
| std::shared_ptr<OpUpdateRecord>(new compatible::NewAttr(name, remark))); | ||
| return *this; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<std::shared_ptr<OpUpdateRecord>> infos_; | ||
| }; | ||
|
|
||
| class OpVersion { | ||
| public: | ||
| OpVersion& AddCheckpoint(const std::string& note, | ||
| const OpVersionDesc& op_version_desc) { | ||
| checkpoints_.push_back(Checkpoint({note, op_version_desc})); | ||
| return *this; | ||
| } | ||
|
|
||
| private: | ||
| struct Checkpoint { | ||
| std::string note_; | ||
| OpVersionDesc op_version_desc_; | ||
| }; | ||
| std::vector<Checkpoint> checkpoints_; | ||
| }; | ||
|
|
||
| class OpVersionRegistrar { | ||
| public: | ||
| static OpVersionRegistrar& GetInstance() { | ||
| static OpVersionRegistrar instance; | ||
| return instance; | ||
| } | ||
| OpVersion& Register(const std::string& op_type) { | ||
| if (op_version_map_.find(op_type) != op_version_map_.end()) { | ||
| PADDLE_THROW("'%s' is registered in operator version more than once.", | ||
| op_type); | ||
| } | ||
| op_version_map_.insert({op_type, OpVersion()}); | ||
| return op_version_map_[op_type]; | ||
| } | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, OpVersion> op_version_map_; | ||
|
|
||
| OpVersionRegistrar() = default; | ||
| OpVersionRegistrar& operator=(const OpVersionRegistrar&) = delete; | ||
| }; | ||
|
|
||
| } // namespace compatible | ||
| } // namespace framework | ||
| } // namespace paddle | ||
|
|
||
| #define REGISTER_OP_VERSION(op_type) \ | ||
| static paddle::framework::compatible::OpVersion \ | ||
| RegisterOpVersion__##op_type = \ | ||
| paddle::framework::compatible::OpVersionRegistrar::GetInstance() \ | ||
| .Register(#op_type) | ||
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 @@ | ||
| /* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ | ||
|
|
||
| #include <glog/logging.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "paddle/fluid/framework/op_version_registry.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| namespace compatible { | ||
|
|
||
| TEST(test_operator_version, test_operator_version) { | ||
| REGISTER_OP_VERSION(test__) | ||
| .AddCheckpoint( | ||
| R"ROC( | ||
| Upgrade reshape, modified one attribute [axis] and add a new attribute [size]. | ||
| )ROC", | ||
| framework::compatible::OpVersionDesc() | ||
| .ModifyAttr("axis", | ||
| "Increased from the original one method to two.", -1) | ||
| .NewAttr("size", | ||
| "In order to represent a two-dimensional rectangle, the " | ||
| "parameter size is added.")) | ||
| .AddCheckpoint( | ||
| R"ROC( | ||
| Add a new attribute [height] | ||
| )ROC", | ||
| framework::compatible::OpVersionDesc().NewAttr( | ||
| "height", | ||
| "In order to represent a two-dimensional rectangle, the " | ||
| "parameter height is added.")); | ||
| } | ||
| } // namespace compatible | ||
| } // namespace framework | ||
| } // namespace paddle |
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.