diff --git a/cmake/package_lite.cmake b/cmake/package_lite.cmake index ed58843a4b7..b476f24f21d 100644 --- a/cmake/package_lite.cmake +++ b/cmake/package_lite.cmake @@ -314,6 +314,8 @@ elseif(WIN32) COMPONENT ${RUNTIME_COMPONENT_NAME}) install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/api COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE) + install(DIRECTORY ${TOP_DIR}/mindspore/core/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/api + COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${TOP_DIR}/mindspore/core/abstract/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/abstract COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${TOP_DIR}/mindspore/core/base/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/base @@ -429,6 +431,8 @@ else() PATTERN "train*" EXCLUDE PATTERN "delegate.h" EXCLUDE PATTERN "lite_session.h" EXCLUDE) install(DIRECTORY ${TOP_DIR}/include/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/api COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h" PATTERN "ops*" EXCLUDE) + install(DIRECTORY ${TOP_DIR}/mindspore/core/api/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/api + COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${TOP_DIR}/mindspore/core/abstract/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/abstract COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h") install(DIRECTORY ${TOP_DIR}/mindspore/core/base/ DESTINATION ${CONVERTER_ROOT_DIR}/include/core/base diff --git a/mindspore/core/api/ir/func_graph.h b/mindspore/core/api/ir/func_graph.h new file mode 100644 index 00000000000..bc59b712cac --- /dev/null +++ b/mindspore/core/api/ir/func_graph.h @@ -0,0 +1,50 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_API_FUNC_GRAPH_H_ +#define MINDSPORE_API_FUNC_GRAPH_H_ + +#include +#include +#include + +#include "api/ir/func_graph_manager.h" + +namespace mindspore::api { + +class FuncGraph { + public: + FuncGraph() = default; + virtual ~FuncGraph() = default; + + virtual const std::vector get_inputs() const = 0; + virtual const std::vector ¶meters() const = 0; + virtual void add_parameter(const ParameterPtr &p) = 0; + + virtual AnfNodePtr output() const = 0; + virtual CNodePtr get_return() const = 0; + virtual void set_output(const AnfNodePtr &value, bool force_new_ret = false) = 0; + + virtual CNodePtr NewCNode(const std::vector &inputs = std::vector()) = 0; + + virtual bool has_attr(const std::string &key) const = 0; + virtual ValuePtr get_attr(const std::string &key) const = 0; + virtual void set_attr(const std::string &key, const ValuePtr &value) = 0; + + virtual FuncGraphManagerPtr get_manager() const = 0; +}; +} // namespace mindspore::api +#endif // MINDSPORE_API_FUNC_GRAPH_H_ diff --git a/mindspore/core/api/ir/func_graph_manager.h b/mindspore/core/api/ir/func_graph_manager.h new file mode 100644 index 00000000000..ad54a9b4196 --- /dev/null +++ b/mindspore/core/api/ir/func_graph_manager.h @@ -0,0 +1,65 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_API_FUNC_GRAPH_MANAGER_H_ +#define MINDSPORE_API_FUNC_GRAPH_MANAGER_H_ + +#include +#include + +#include "utils/ordered_set.h" +#include "utils/ordered_map.h" +#include "ir/anf.h" + +namespace mindspore::api { + +class FuncGraph; +using FuncGraphPtr = std::shared_ptr; + +class FuncGraphManager; +using FuncGraphManagerPtr = std::shared_ptr; + +struct AnfNodeIndexPairHasher { + std::size_t operator()(const std::pair &p1) const { + return std::hash{}(p1.first.get()); + } +}; + +struct AnfNodeIndexPairEqual { + bool operator()(const std::pair &lhs, const std::pair &rhs) const { + return lhs == rhs; + } +}; + +using AnfNodeIndexSet = OrderedSet, AnfNodeIndexPairHasher, AnfNodeIndexPairEqual>; +using NodeUsersMap = OrderedMap; + +class FuncGraphManager { + public: + FuncGraphManager() = default; + virtual ~FuncGraphManager() = default; + + virtual bool Replace(const AnfNodePtr &old_node, const AnfNodePtr &new_node) = 0; + virtual void SetEdge(const AnfNodePtr &node, int index, const AnfNodePtr &value) = 0; + virtual void AddEdge(const AnfNodePtr &node, const AnfNodePtr &value) = 0; + virtual const NodeUsersMap &node_users() const = 0; + + static FuncGraphManagerPtr Manage(const FuncGraphPtr &func_graph, bool manage = true); +}; + +} // namespace mindspore::api + +#endif // MINDSPORE_API_FUNC_GRAPH_MANAGER_H_ diff --git a/mindspore/core/ir/func_graph.cc b/mindspore/core/ir/func_graph.cc index 0a7c0c5b457..703b679fe40 100644 --- a/mindspore/core/ir/func_graph.cc +++ b/mindspore/core/ir/func_graph.cc @@ -140,12 +140,12 @@ bool FuncGraph::has_flag(const std::string &key) { return false; } -bool FuncGraph::has_attr(const std::string &key) { +bool FuncGraph::has_attr(const std::string &key) const { auto iter = attrs_.find(key); return !(iter == attrs_.cend()); } -ValuePtr FuncGraph::get_attr(const std::string &key) { +ValuePtr FuncGraph::get_attr(const std::string &key) const { auto iter = attrs_.find(key); return iter == attrs_.cend() ? nullptr : iter->second; } diff --git a/mindspore/core/ir/func_graph.h b/mindspore/core/ir/func_graph.h index 26aa59255d0..1fa51f686ef 100644 --- a/mindspore/core/ir/func_graph.h +++ b/mindspore/core/ir/func_graph.h @@ -38,6 +38,7 @@ #include "base/effect_info.h" #include "ir/func_graph_cloner.h" #include "abstract/abstract_value.h" +#include "api/ir/func_graph.h" namespace mindspore { using BaseRefCounterMap = OrderedMap; @@ -151,7 +152,7 @@ class FuncGraphBase : public Value { MS_DECLARE_PARENT(FuncGraphBase, Value); }; -class FuncGraph : public FuncGraphBase, public EffectInfoHolder { +class FuncGraph : public api::FuncGraph, public FuncGraphBase, public EffectInfoHolder { public: FuncGraph(); using Drawer = std::function; @@ -164,15 +165,15 @@ class FuncGraph : public FuncGraphBase, public EffectInfoHolder { abstract::AbstractBasePtr ToAbstract() override; // get function graph inputs, but parameters - const std::vector get_inputs() const; + const std::vector get_inputs() const final; // Return the graph's output, or nullptr if not yet deduced. AnfNodePtr output() const; void set_output(const AnfNodePtr &value, bool force_new_ret = false); - const std::vector ¶meters() const { return parameters_; } + const std::vector ¶meters() const final { return parameters_; } // Append virtual ParameterPtr add_parameter(); - void add_parameter(const ParameterPtr &p); + void add_parameter(const ParameterPtr &p) final; void append_parameter(const ParameterPtr &p) { parameters_.push_back(p); } // Prepend virtual ParameterPtr InsertFrontParameter(); @@ -183,8 +184,8 @@ class FuncGraph : public FuncGraphBase, public EffectInfoHolder { ParameterPtr AddWeightParameter(const std::string &name); // Create a cnode with given inputs, bound to this graph. - virtual CNodePtr NewCNode(const std::vector &inputs = std::vector()); - virtual CNodePtr NewCNode(const PrimitivePtr &primitive, const std::vector &prim_inputs); + CNodePtr NewCNode(const std::vector &inputs = std::vector()) override; + CNodePtr NewCNode(const PrimitivePtr &primitive, const std::vector &prim_inputs); // Create a cnode with given inputs, bound to this graph and push back to order list. CNodePtr NewCNodeInOrder(const std::vector &inputs = std::vector()); @@ -240,21 +241,23 @@ class FuncGraph : public FuncGraphBase, public EffectInfoHolder { void set_flag(const std::string &key, bool flag) { attrs_[key] = MakeValue(flag); } void erase_flag(const std::string &key) { (void)attrs_.erase(key); } - bool has_attr(const std::string &key); - ValuePtr get_attr(const std::string &key); - void set_attr(const std::string &key, const ValuePtr &value) { attrs_[key] = value; } + bool has_attr(const std::string &key) const final; + ValuePtr get_attr(const std::string &key) const final; + void set_attr(const std::string &key, const ValuePtr &value) final { attrs_[key] = value; } std::unordered_map &transforms() { return transforms_; } void set_transforms(const std::unordered_map &transforms) { transforms_ = transforms; } - CNodePtr get_return() const { return return_; } + CNodePtr get_return() const final { return return_; } void set_return(const CNodePtr &cnode) { return_ = cnode; } FuncGraphManagerPtr manager() const { return manager_.lock(); } void set_manager(const FuncGraphManagerPtr &m) { manager_ = std::weak_ptr(m); } + api::FuncGraphManagerPtr get_manager() const final { return manager_.lock(); } + std::string ToString() const override; GraphDebugInfoPtr debug_info(); void set_debug_info(const GraphDebugInfoPtr &info) { diff --git a/mindspore/core/ir/manager.cc b/mindspore/core/ir/manager.cc index 5f513cc0e1d..c6f0fdff027 100644 --- a/mindspore/core/ir/manager.cc +++ b/mindspore/core/ir/manager.cc @@ -67,6 +67,10 @@ FuncGraphManagerPtr Manage(FuncGraphPtr func_graph, bool manage) { return Manage(func_graphs, manage); } +api::FuncGraphManagerPtr api::FuncGraphManager::Manage(const api::FuncGraphPtr &func_graph, bool manage) { + return mindspore::Manage(std::dynamic_pointer_cast(func_graph), manage); +} + FuncGraphManager::FuncGraphManager(const std::vector &roots, bool manage) : roots_(roots), is_manage_(manage) { Reset(); diff --git a/mindspore/core/ir/manager.h b/mindspore/core/ir/manager.h index 6a2d4afd55f..bc01966eba7 100644 --- a/mindspore/core/ir/manager.h +++ b/mindspore/core/ir/manager.h @@ -34,11 +34,12 @@ #include "utils/signal.h" #include "utils/ordered_set.h" #include "utils/ordered_map.h" +#include "ir/anf.h" #include "ir/graph_utils.h" #include "utils/counter.h" #include "utils/hashing.h" #include "base/base_ref.h" -#include "ir/anf.h" +#include "api/ir/func_graph_manager.h" namespace mindspore { struct Change; @@ -46,20 +47,9 @@ class FuncGraphTransaction; class FuncGraphManager; using FuncGraphManagerPtr = std::shared_ptr; -struct AnfNodeIndexPairHasher { - std::size_t operator()(const std::pair &p1) const { - return std::hash{}(p1.first.get()); - } -}; - -struct AnfNodeIndexPairEqual { - bool operator()(const std::pair &lhs, const std::pair &rhs) const { - return lhs == rhs; - } -}; -using AnfNodeIndexSet = OrderedSet, AnfNodeIndexPairHasher, AnfNodeIndexPairEqual>; +using AnfNodeIndexSet = api::AnfNodeIndexSet; // NodeUsersMap, for node B input i use node A, it will be one item in map with key: A, and value: (B, i) -using NodeUsersMap = OrderedMap; +using NodeUsersMap = api::NodeUsersMap; using FuncGraphSetPair = std::pair; using FuncGraphSetPtr = std::shared_ptr; using EdgeTuple = std::pair>; @@ -294,7 +284,7 @@ class FuncGraphJTotalComputer final : public DepComputer { bool SeekJ(const FuncGraphPtr &fg, size_t seen_num); }; -class FuncGraphManager : public std::enable_shared_from_this { +class FuncGraphManager : public std::enable_shared_from_this, public api::FuncGraphManager { public: explicit FuncGraphManager(const std::vector &roots, bool manage = true); ~FuncGraphManager() { @@ -314,9 +304,9 @@ class FuncGraphManager : public std::enable_shared_from_this { void AddParameter(const FuncGraphPtr &fg, const AnfNodePtr ¶meter); void InsertFrontParameter(const FuncGraphPtr &fg, const AnfNodePtr ¶meter); void MaybeDropFuncGraphs(const FuncGraphSet &func_graphs, bool ignore_users = false); - bool Replace(const AnfNodePtr &old_node, const AnfNodePtr &new_node); - void SetEdge(const AnfNodePtr &node, int index, const AnfNodePtr &value); - void AddEdge(const AnfNodePtr &node, const AnfNodePtr &value); + bool Replace(const AnfNodePtr &old_node, const AnfNodePtr &new_node) final; + void SetEdge(const AnfNodePtr &node, int index, const AnfNodePtr &value) final; + void AddEdge(const AnfNodePtr &node, const AnfNodePtr &value) final; void MoveAllCNodeDropGraph(FuncGraphPtr source, FuncGraphPtr target, const ScopePtr &scope); FuncGraphTransaction Transact(); @@ -332,6 +322,8 @@ class FuncGraphManager : public std::enable_shared_from_this { NodeUsersMap &node_users() { return node_users_; } + const NodeUsersMap &node_users() const final { return node_users_; } + FVTotalMap &free_variables_total() const; FuncGraphSet &func_graph_parents_total(const FuncGraphPtr &fg) const;