From 67c528e7fc1391c1571fbf9f8d747a8e576d194b Mon Sep 17 00:00:00 2001 From: He Wei Date: Thu, 3 Mar 2022 10:39:04 +0800 Subject: [PATCH] Reduce object size of node 1. Remove unused fields, such as AnfNode::interpreted_node_, CNode::inputs_value_; 2. Reduce seen_ size from 64 bits to 32 bits; 3. Use bitset for boolean flags; 4. Use pointer for user data; 5. Make some cold fields as user data, such as AnfNode::kernel_info_. Object size decreased: AnfNode: 232 -> 144 ValueNode: 264 -> 176 Parameter: 384 -> 272 CNode: 576 -> 400 --- .../core/graph_kernel_splitter.cc | 1 - .../graph_kernel/core/shape_ops_splitter.cc | 1 - .../ccsrc/frontend/optimizer/ad/dfunctor.cc | 1 - mindspore/ccsrc/frontend/optimizer/opt.cc | 4 +- .../pipeline/jit/parse/function_block.cc | 3 +- .../jit/static_analysis/auto_monad.cc | 3 +- .../jit/static_analysis/order_enforce.cc | 4 +- .../gpu/optimizer/cudnn_inplace_fusion.cc | 4 +- mindspore/core/base/effect_info.h | 7 +- mindspore/core/base/user_data.h | 39 +++- mindspore/core/ir/anf.cc | 21 +- mindspore/core/ir/anf.h | 189 +++++++----------- mindspore/core/ir/func_graph.cc | 4 +- mindspore/core/ir/func_graph.h | 4 +- mindspore/core/ir/graph_utils.cc | 9 +- mindspore/core/ir/graph_utils_extends.cc | 4 +- mindspore/core/ir/manager.cc | 6 +- mindspore/core/ir/manager.h | 4 +- 18 files changed, 141 insertions(+), 167 deletions(-) diff --git a/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_splitter.cc b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_splitter.cc index bdfdf626a51..24b3413bb03 100644 --- a/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_splitter.cc +++ b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_splitter.cc @@ -88,7 +88,6 @@ CNodePtr NewRecomputeNode(const AnfNodePtr &orig_node, std::mapAddNode(cp_node); cp_node->set_abstract(cnode->abstract()); cp_node->set_forward(cnode->forward().first, cnode->forward().second); - cp_node->set_inputs_value(cnode->inputs_value()); ScopePtr scope = (orig_node->scope() != kDefaultScope) ? orig_node->scope() : kDefaultScope; cp_node->set_scope(scope); cp_node->set_kernel_info(cnode->kernel_info_ptr()); diff --git a/mindspore/ccsrc/common/graph_kernel/core/shape_ops_splitter.cc b/mindspore/ccsrc/common/graph_kernel/core/shape_ops_splitter.cc index a1c20c91f11..780fb52d08b 100644 --- a/mindspore/ccsrc/common/graph_kernel/core/shape_ops_splitter.cc +++ b/mindspore/ccsrc/common/graph_kernel/core/shape_ops_splitter.cc @@ -34,7 +34,6 @@ AnfNodePtr CloneCNode(const AnfNodePtr &anf_node) { CNodePtr node = func_graph->NewCNode(cnode->inputs()); node->set_abstract(cnode->abstract()); node->set_forward(cnode->forward().first, cnode->forward().second); - node->set_inputs_value(cnode->inputs_value()); ScopePtr scope = (anf_node->scope() != kDefaultScope) ? anf_node->scope() : kDefaultScope; node->set_scope(scope); node->set_kernel_info(cnode->kernel_info_ptr()); diff --git a/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc b/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc index 490af498e0a..b335e8185a8 100644 --- a/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc +++ b/mindspore/ccsrc/frontend/optimizer/ad/dfunctor.cc @@ -368,7 +368,6 @@ AdjointPtr DFunctor::MapMorphism(const AnfNodePtr &morph) { auto grad_exec = pynative_exec->grad_executor(); if (grad_exec->eliminate_forward()) { PynativeDFunctor::ReplaceEquivdout(k_app, cnode_morph); - cnode_morph->clear_inputs_value(); } } diff --git a/mindspore/ccsrc/frontend/optimizer/opt.cc b/mindspore/ccsrc/frontend/optimizer/opt.cc index d074292a0f6..fd17df6d8e9 100644 --- a/mindspore/ccsrc/frontend/optimizer/opt.cc +++ b/mindspore/ccsrc/frontend/optimizer/opt.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -163,7 +163,7 @@ static void UpdateTransformingListForIR(const AnfNodePtr &node, std::deque *todo, bool change, size_t seen) { + std::deque *todo, bool change, SeenNum seen) { if (!change) { return; } diff --git a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc index a3950d0bd97..2886f86e2e1 100644 --- a/mindspore/ccsrc/pipeline/jit/parse/function_block.cc +++ b/mindspore/ccsrc/pipeline/jit/parse/function_block.cc @@ -1,7 +1,7 @@ /** * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). * - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -325,7 +325,6 @@ AnfNodePtr FunctionBlock::MakeInterpret(const std::string &script_text, const An auto script_node = NewValueNode(script); auto node = func_graph_->NewCNodeInOrder( {NewValueNode(prim::kPrimPyInterpret), script_node, global_dict_node, local_dict_node}); - node->set_interpreted_node(orig_node); node->set_interpret_internal_type(orig_node->interpret_internal_type()); return node; } diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/auto_monad.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/auto_monad.cc index 78f6dee7949..ab96422710f 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/auto_monad.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/auto_monad.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020-2021 Huawei Technologies Co., Ltd + * Copyright 2020-2022 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. @@ -30,6 +30,7 @@ #include "utils/hash_set.h" #include "utils/ordered_map.h" #include "utils/ordered_set.h" +#include "base/effect_info.h" #include "base/core_ops.h" #include "abstract/abstract_value.h" diff --git a/mindspore/ccsrc/pipeline/jit/static_analysis/order_enforce.cc b/mindspore/ccsrc/pipeline/jit/static_analysis/order_enforce.cc index bb3c9a66f69..7aa4f608f1a 100644 --- a/mindspore/ccsrc/pipeline/jit/static_analysis/order_enforce.cc +++ b/mindspore/ccsrc/pipeline/jit/static_analysis/order_enforce.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 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. @@ -282,7 +282,7 @@ class OrderEnforcer { if (user_cnode == nullptr) { return false; } - size_t seen = NewSeenGeneration(); + auto seen = NewSeenGeneration(); std::queue q; user_cnode->seen_ = seen; q.push(user_cnode); diff --git a/mindspore/ccsrc/plugin/device/gpu/optimizer/cudnn_inplace_fusion.cc b/mindspore/ccsrc/plugin/device/gpu/optimizer/cudnn_inplace_fusion.cc index 59227074299..0645b6bb905 100644 --- a/mindspore/ccsrc/plugin/device/gpu/optimizer/cudnn_inplace_fusion.cc +++ b/mindspore/ccsrc/plugin/device/gpu/optimizer/cudnn_inplace_fusion.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 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. @@ -72,7 +72,7 @@ bool ExistRoute(const CNodePtr &src, const CNodePtr &dst) { return true; } - size_t seen = NewSeenGeneration(); + auto seen = NewSeenGeneration(); std::queue to_do; to_do.push(dst); while (!to_do.empty()) { diff --git a/mindspore/core/base/effect_info.h b/mindspore/core/base/effect_info.h index 3357f81509a..f9a2f778e37 100644 --- a/mindspore/core/base/effect_info.h +++ b/mindspore/core/base/effect_info.h @@ -18,7 +18,6 @@ #define MINDSPORE_CORE_EFFECT_INFO_H_ namespace mindspore { - struct EffectInfo { enum State : unsigned char { kUnknown = 0, @@ -49,15 +48,11 @@ class EffectInfoHolder { // Set effect info. void SetEffectInfo(const EffectInfo &info) { effect_info_ = info; } - // Unset effect info. - void UnsetEffectInfo() { effect_info_ = {EffectInfo::kUnknown, false, false}; } - - ~EffectInfoHolder() {} + ~EffectInfoHolder() = default; protected: EffectInfo effect_info_; }; - } // namespace mindspore #endif // MINDSPORE_CORE_EFFECT_INFO_H_ diff --git a/mindspore/core/base/user_data.h b/mindspore/core/base/user_data.h index 45835dc4d80..90d90f50356 100644 --- a/mindspore/core/base/user_data.h +++ b/mindspore/core/base/user_data.h @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -19,33 +19,58 @@ #include #include +#include #include "utils/hash_map.h" namespace mindspore { class UserData { public: + using DataMap = mindspore::HashMap>; + + UserData() = default; + UserData(const UserData &other) : data_(other.data_ ? std::make_unique(*other.data_) : nullptr) {} + UserData(UserData &&other) : data_(std::move(other.data_)) {} + UserData &operator=(const UserData &other) { + data_ = (other.data_ ? std::make_unique(*other.data_) : nullptr); + return *this; + } + UserData &operator=(UserData &&other) { + data_ = std::move(other.data_); + return *this; + } + ~UserData() = default; + template void set(const std::string &key, const std::shared_ptr &value) { + InitData(); if (value == nullptr) { - data_.erase(key); + data_->erase(key); } else { - data_.insert_or_assign(key, value); + data_->insert_or_assign(key, value); } } template std::shared_ptr get(const std::string &key) const { - auto iter = data_.find(key); - if (iter == data_.end()) { + if (data_ == nullptr) { + return nullptr; + } + auto iter = data_->find(key); + if (iter == data_->end()) { return nullptr; } return std::static_pointer_cast(iter->second); } - bool has(const std::string &key) const { return data_.find(key) != data_.end(); } + bool has(const std::string &key) const { return (data_ != nullptr) && (data_->find(key) != data_->end()); } private: - mindspore::HashMap> data_; + void InitData() { + if (data_ == nullptr) { + data_ = std::make_unique(); + } + } + std::unique_ptr data_; }; } // namespace mindspore diff --git a/mindspore/core/ir/anf.cc b/mindspore/core/ir/anf.cc index d5e4d7d0e7d..3cbe6acd666 100644 --- a/mindspore/core/ir/anf.cc +++ b/mindspore/core/ir/anf.cc @@ -1,7 +1,7 @@ /** * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). * - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -47,29 +47,20 @@ void AnfNode::set_abstract(const AbstractBasePtr &abs) { CNode::CNode(const std::vector &inputs, const FuncGraphPtr &func_graph) : AnfNode(func_graph), inputs_(inputs), - stop_gradient_(false), - output_value_(std::make_pair(nullptr, "")), primal_attrs_(PrimalAttrManager::GetInstance().GetCurrentPrimalAttr()), - primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()), - input_tensor_num_(-1) {} + primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()) {} CNode::CNode(std::vector &&inputs, const FuncGraphPtr &func_graph) : AnfNode(func_graph), inputs_(std::move(inputs)), - stop_gradient_(false), - output_value_(std::make_pair(nullptr, "")), primal_attrs_(PrimalAttrManager::GetInstance().GetCurrentPrimalAttr()), - primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()), - input_tensor_num_(-1) {} + primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()) {} CNode::CNode(std::vector &&inputs, const FuncGraphPtr &func_graph, NodeDebugInfoPtr &&debug_info) : AnfNode(func_graph, std::move(debug_info)), inputs_(std::move(inputs)), - stop_gradient_(false), - output_value_(std::make_pair(nullptr, "")), primal_attrs_(PrimalAttrManager::GetInstance().GetCurrentPrimalAttr()), - primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()), - input_tensor_num_(-1) {} + primal_debug_infos_(PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo()) {} // Check if CNode is an apply with the specific Primitive. bool CNode::IsApply(const PrimitivePtr &value) const { @@ -378,8 +369,8 @@ bool IsStateEquivalent(const AnfNodePtr &outer, const AnfNodePtr &inner) { [&monad, kMonadInput](const CNodePtr &load) { return load->inputs().at(kMonadInput) == monad; }); } -size_t NewSeenGeneration() { - static size_t seen_generation = 0; +SeenNum NewSeenGeneration() { + static SeenNum seen_generation = 0; return ++seen_generation; } diff --git a/mindspore/core/ir/anf.h b/mindspore/core/ir/anf.h index c99454c024a..621214776d0 100644 --- a/mindspore/core/ir/anf.h +++ b/mindspore/core/ir/anf.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "utils/hash_map.h" #include "utils/hash_set.h" @@ -57,6 +58,7 @@ using BaseShapePtr = std::shared_ptr; using AbstractBasePtr = std::shared_ptr; using AbstractBasePtrList = std::vector; using NodeDebugInfoSet = std::set; +using SeenNum = uint32_t; class Value; using ValuePtr = std::shared_ptr; @@ -114,12 +116,7 @@ class MS_CORE_API AnfNode : public Base { abstract_(nullptr), debug_info_(std::move(debug_info)), fullname_with_scope_(""), - scope_(ScopeManager::GetInstance().GetCurrentScope()), - kernel_info_(nullptr), - interpret_(false), - interpret_internal_type_(false), - interpret_special_type_(false), - interpreted_node_(nullptr) {} + scope_(ScopeManager::GetInstance().GetCurrentScope()) {} /// \brief Constructor. /// @@ -156,22 +153,22 @@ class MS_CORE_API AnfNode : public Base { /// \brief Obtain device kernel program information. /// /// \return Device kernel program information. - const KernelInfoDevice *kernel_info() const { return kernel_info_.get(); } + const KernelInfoDevice *kernel_info() const { return kernel_info_ptr().get(); } /// \brief Obtain device kernel program information. /// /// \return Device kernel program information. - KernelInfoDevice *kernel_info() { return kernel_info_.get(); } + KernelInfoDevice *kernel_info() { return kernel_info_ptr().get(); } /// \brief Obtain the pointer of KernelInfoDevice. /// /// \return The pointer of KernelInfoDevice. - const KernelInfoDevicePtr &kernel_info_ptr() const { return kernel_info_; } + KernelInfoDevicePtr kernel_info_ptr() const { return user_data(kKernelInfoKey); } /// \brief Set device kernel program information. /// /// \param[in] kernel_info New device kernel program information. - void set_kernel_info(const KernelInfoDevicePtr &kernel_info) { kernel_info_ = kernel_info; } + void set_kernel_info(const KernelInfoDevicePtr &kernel_info) { set_user_data(kKernelInfoKey, kernel_info); } /// \brief Obtain the inferred abstract value of this AnfNode. /// @@ -269,9 +266,6 @@ class MS_CORE_API AnfNode : public Base { return os; } - size_t seen_{0}; - size_t extra_seen_{0}; - /// \brief Set user data. /// /// \param[in] key The key of user data. @@ -328,46 +322,39 @@ class MS_CORE_API AnfNode : public Base { /// \brief Check if there is an interpret node. /// /// \return True if there is an interpret node, otherwise false. - bool interpret() const { return interpret_; } + bool interpret() const { return interpret_flags_[kInterpret]; } /// \brief Whether to use interpretation /// /// \param[in] interpret Boolean. - void set_interpret(const bool &interpret) { interpret_ = interpret; } + void set_interpret(const bool &interpret) { interpret_flags_[kInterpret] = interpret; } /// \brief Check if there is an interpret node related to the unsupported internal type. /// /// \return True if there is an interpret node related to the unsupported internal type, otherwise false. - bool interpret_internal_type() { return interpret_internal_type_; } + bool interpret_internal_type() { return interpret_flags_[kInterpretInternalType]; } /// \brief Whether there is an interpret node with unsupported internal type. /// /// \param[in] interpret_internal_type Boolean. void set_interpret_internal_type(const bool &interpret_internal_type) { - interpret_internal_type_ = interpret_internal_type; + interpret_flags_[kInterpretInternalType] = interpret_internal_type; } /// \brief Check if there is an interpret node related to the unsupported special type. /// /// \return True if there is an interpret node related to the unsupported special type, otherwise false. - bool interpret_special_type() { return interpret_special_type_; } + bool interpret_special_type() { return interpret_flags_[kInterpretSpecialType]; } /// \brief Whether there is an interpret node with unsupported internal type. /// /// \param[in] interpret_special_type Boolean. void set_interpret_special_type(const bool &interpret_special_type) { - interpret_special_type_ = interpret_special_type; + interpret_flags_[kInterpretSpecialType] = interpret_special_type; } - /// \brief Get interpreted node. - /// - /// \return Interpreted node. - AnfNodePtr interpreted_node() { return interpreted_node_; } - - /// \brief Set interpreted node. - /// - /// \param[in] Interpreted node. - void set_interpreted_node(const AnfNodePtr &node) { interpreted_node_ = node; } + SeenNum seen_{0}; + SeenNum extra_seen_{0}; protected: // Hold a weak ref to Graph as Graph also hold ref to AnfNode. @@ -378,13 +365,15 @@ class MS_CORE_API AnfNode : public Base { std::string fullname_with_scope_; private: + static constexpr size_t kInterpret = 0; + static constexpr size_t kInterpretInternalType = 1; + static constexpr size_t kInterpretSpecialType = 2; + static constexpr size_t kNumInterpretFlags = 3; + static constexpr auto kKernelInfoKey = "kernel_info"; + ScopePtr scope_; - KernelInfoDevicePtr kernel_info_; UserData user_data_; - bool interpret_; - bool interpret_internal_type_; - bool interpret_special_type_; - AnfNodePtr interpreted_node_; + std::bitset interpret_flags_; }; // CNode represents the complex node with a set of arguments. @@ -395,8 +384,8 @@ class MS_CORE_API AnfNode : public Base { // Using add_input(input) to append a new input for a CNode. // Using set_input(i, input) to change some input of these inputs. // Using set_inputs(inputs) to refresh all of the inputs of a CNode. -// func_graph_as_var_: used in opt pattern matching to match a real FuncGraph. -// stop_gradient_: a flag used to stop gradient. +// func_graph_as_var: used in opt pattern matching to match a real FuncGraph. +// stop_gradient: a flag used to stop gradient. // Using stop_gradient() to get this flag, mainly used in ad. // Using set_stop_gradient() to set this flag. class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { @@ -417,14 +406,10 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { /// /// \param[in] inputs Input nodes of this Cnode. /// \param[in] func_graph_as_var The FuncGraph of type VarPtr to which this CNode belongs, - CNode(const std::vector &inputs, const VarPtr &func_graph_as_var) - : AnfNode(nullptr), - inputs_(inputs), - func_graph_as_var_(func_graph_as_var), - stop_gradient_(false), - input_tensor_num_(-1) { + CNode(const std::vector &inputs, const VarPtr &func_graph_as_var) : AnfNode(nullptr), inputs_(inputs) { primal_attrs_ = PrimalAttrManager::GetInstance().GetCurrentPrimalAttr(); primal_debug_infos_ = PrimalDebugInfoManager::GetInstance().GetCurrentPrimalDebugInfo(); + set_user_data(kFuncGraphVarKey, func_graph_as_var); } /// \brief Constructor. @@ -477,47 +462,38 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { /// \param[in] inputs Input nodes. void set_inputs(const std::vector &inputs); - /// \brief Record the cnode input value and id to inputs_value_. - /// - /// \param[in] input_value Input value. - /// \param[in] id The id. - void add_input_value(const ValuePtr &input_value, const std::string &id) { - inputs_value_.push_back(std::make_pair(input_value, id)); - } - - /// \brief Clear the record of cnode input value in inputs_value_. - void clear_inputs_value() { inputs_value_.clear(); } - - /// \brief Set the record of cnode input value. - /// - /// \param[in] values New record. - void set_inputs_value(const std::vector> &values) { inputs_value_ = values; } - - /// \brief Get the record of input value of this CNode. - /// - /// \return The input values of this CNode. - const std::vector> &inputs_value() const { return inputs_value_; } + // output_value store cnode value and id in pynative mode. + using OutputValue = std::pair; /// \brief Record the cnode value and id to output_value_. /// /// \param[in] forward The cnode value. /// \param[in] id The id. - void set_forward(const ValueNodePtr &forward, const std::string &id) { output_value_ = std::make_pair(forward, id); } + void set_forward(const ValueNodePtr &forward, const std::string &id) { + set_user_data(kOutputValueKey, std::make_shared(forward, id)); + } /// \brief Get the record of output value of this CNode. /// /// \return The output value of this CNode. - const std::pair &forward() const { return output_value_; } + const OutputValue &forward() const { + static const OutputValue empty_value; + auto ptr = user_data(kOutputValueKey); + if (ptr == nullptr) { + return empty_value; + } + return *ptr; + } /// \brief Check if stop_gradient is set. /// /// \return True if stop_gradient is set, otherwise false. - bool stop_gradient() const { return stop_gradient_; } + bool stop_gradient() const { return flags_[kStopGradient]; } /// \brief Set stop_gradient. /// /// \param[in] stop_gradient Boolean. - void set_stop_gradient(bool stop_gradient) { stop_gradient_ = stop_gradient; } + void set_stop_gradient(bool stop_gradient) { flags_[kStopGradient] = stop_gradient; } std::string fullname_with_scope() override; @@ -532,25 +508,25 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { /// \brief Set in_forward_flag for this CNode. /// /// \param[in] flag Boolean. - void set_in_forward_flag(bool flag) { in_forward_flag_ = flag; } + void set_in_forward_flag(bool flag) { flags_[kInForwardFlag] = flag; } /// \brief Check if in_forward_flag is set. /// /// \return True if in_forward_flag is set, otherwise false. - bool in_forward_flag() const { return in_forward_flag_; } + bool in_forward_flag() const { return flags_[kInForwardFlag]; } /// \brief Check if the primitive of this CNode is load. /// /// \param[in] is_load Boolean. - void set_load_flag(bool is_load) { is_load_ = is_load; } + void set_load_flag(bool is_load) { flags_[kIsLoad] = is_load; } /// \brief Check if is_load_ is set. /// /// \return True if is_load_ is set, otherwise false. - bool get_load_flag() const { return is_load_; } + bool get_load_flag() const { return flags_[kIsLoad]; } /// \brief Get func_graph_as_var of this CNode. /// /// \return func_graph_as_var. - VarPtr func_graph_as_var() const { return func_graph_as_var_; } + VarPtr func_graph_as_var() const { return user_data(kFuncGraphVarKey); } /// \brief Get all attributes of this CNode. /// @@ -648,7 +624,6 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { MS_EXCEPTION_IF_NULL(node); set_abstract(node->abstract()); set_forward(node->forward().first, node->forward().second); - set_inputs_value(node->inputs_value()); set_attrs(node->attrs()); set_primal_attrs(node->primal_attrs()); set_load_flag(node->get_load_flag()); @@ -666,12 +641,12 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { /// \brief Is effect have been handled. /// /// \return True if effect have been handled, otherwise false. - bool IsEffectHandled() const { return effect_handled_; } + bool IsEffectHandled() const { return flags_[kEffectHandled]; } /// \brief Set effect handled or not. /// /// \param[in] handled Boolean. - void SetEffectHandled(bool handled) { effect_handled_ = handled; } + void SetEffectHandled(bool handled) { flags_[kEffectHandled] = handled; } /// \brief Get the debug infos of fused nodes. /// @@ -706,31 +681,31 @@ class MS_CORE_API CNode final : public AnfNode, public EffectInfoHolder { /// \brief Check whether this node is in ms_function or not in PyNative Mode. /// /// \return True if in ms_function, otherwise false. - bool is_parallel() const { return is_parallel_; } + bool is_parallel() const { return flags_[kIsParallel]; } /// \brief Set is_parallel_ for CNode. /// /// \param[in] is_parallel_ Boolean. - void set_parallel(bool parallel) { is_parallel_ = parallel; } + void set_parallel(bool parallel) { flags_[kIsParallel] = parallel; } private: + static constexpr size_t kStopGradient = 0; + static constexpr size_t kInForwardFlag = 1; + static constexpr size_t kEffectHandled = 2; + static constexpr size_t kIsLoad = 3; + static constexpr size_t kIsParallel = 4; + static constexpr size_t kNumFlags = 5; + static constexpr auto kFuncGraphVarKey = "fg_var"; + static constexpr auto kOutputValueKey = "out_value"; + std::vector inputs_; - VarPtr func_graph_as_var_; - bool stop_gradient_; - bool in_forward_flag_ = false; - bool effect_handled_ = false; - bool is_load_ = false; - // is_parallel represents whether this cnode lies in ms_function or not in PyNative Mode - bool is_parallel_ = false; - // inputs_value_ store cnode input value and id in pynative mode - // output_value_ store cnode value and id in pynative mode - std::vector> inputs_value_; - std::pair output_value_; + ssize_t input_tensor_num_ = -1; + std::bitset flags_; + mindspore::HashMap attrs_; mindspore::HashMap primal_attrs_; NodeDebugInfoSet primal_debug_infos_; NodeDebugInfoSet fused_debug_infos_; - ssize_t input_tensor_num_ = -1; }; // ANode represents the atomic node. It's derived Parameter and ValueNode. @@ -763,19 +738,13 @@ class MS_CORE_API Parameter final : public ANode { /// \brief Constructor. /// /// \param[in] func_graph The FuncGraph to which this Parameter belongs. - explicit Parameter(const FuncGraphPtr &func_graph) - : ANode(func_graph), name_(""), has_default_(false), default_param_(nullptr), used_graph_count_(0) {} + explicit Parameter(const FuncGraphPtr &func_graph) : ANode(func_graph) {} /// \brief Constructor. /// /// \param[in] func_graph The FuncGraph to which this Parameter belongs. /// \param[in] debug_info The debug info to be used for this Parameter. - Parameter(const FuncGraphPtr &func_graph, NodeDebugInfoPtr &&debug_info) - : ANode(func_graph, std::move(debug_info)), - name_(""), - has_default_(false), - default_param_(nullptr), - used_graph_count_(0) {} + Parameter(const FuncGraphPtr &func_graph, NodeDebugInfoPtr &&debug_info) : ANode(func_graph, std::move(debug_info)) {} /// \brief Destructor. ~Parameter() override = default; @@ -812,20 +781,20 @@ class MS_CORE_API Parameter final : public ANode { /// \brief Get the default parameter. /// /// \return The default parameter. - ValuePtr default_param() const { return default_param_; } + const ValuePtr &default_param() const { return default_param_; } /// \brief Get the parameter information. /// /// \return The parameter information. ParamInfoPtr param_info() const; - /// \brief Increase used_graph_count_. + /// \brief Increase used_graph_count. void IncreaseUsedGraphCount() { used_graph_count_++; } - /// \brief Decrease used_graph_count_. + /// \brief Decrease used_graph_count. void DecreaseUsedGraphCount() { used_graph_count_--; } - /// \brief Get used_graph_count_. + /// \brief Get used_graph_count. /// - /// \return used_graph_count_. + /// \return used_graph_count. int used_graph_count() const { return used_graph_count_; } bool is_top_graph_param() const { return is_top_graph_param_; } @@ -851,10 +820,7 @@ class MS_CORE_API Parameter final : public ANode { /// /// \param[in] graph_id True if used, otherwise false. bool IsUsedByRealKernelInGraph(uint32_t graph_id) const { - if (not_used_in_graphs_.find(graph_id) != not_used_in_graphs_.end()) { - return false; - } - return true; + return not_used_in_graphs_.find(graph_id) == not_used_in_graphs_.end(); } /// \brief Set whether this Parameter has a dynamic shape. @@ -904,14 +870,13 @@ class MS_CORE_API Parameter final : public ANode { int64_t hidden_size = 0; }; std::string name_; - bool has_default_; - std::set not_used_in_graphs_; - bool has_dynamic_shape_ = false; ValuePtr default_param_; - // The count of graphs using the parameter. - int used_graph_count_; - // some attrs used in special format + // Some attrs used in special format. FormatAttr format_attrs_; + std::set not_used_in_graphs_; + int used_graph_count_ = 0; + bool has_default_ = false; + bool has_dynamic_shape_ = false; bool is_top_graph_param_ = false; }; using ParameterPtr = std::shared_ptr; @@ -1046,8 +1011,8 @@ class MS_CORE_API ValueNode final : public ANode { private: ValuePtr value_; - bool has_new_value_ = false; size_t used_graph_count_{0}; + bool has_new_value_ = false; }; template @@ -1175,7 +1140,7 @@ inline S GetValueNode(const AnfNodePtr &node) { return s; } -MS_CORE_API size_t NewSeenGeneration(); +MS_CORE_API SeenNum NewSeenGeneration(); namespace id_generator { MS_CORE_API std::string get_id(const AnfNodePtr &node); diff --git a/mindspore/core/ir/func_graph.cc b/mindspore/core/ir/func_graph.cc index 6c504d2bd45..9995bb8baef 100644 --- a/mindspore/core/ir/func_graph.cc +++ b/mindspore/core/ir/func_graph.cc @@ -782,8 +782,8 @@ void FuncGraph::set_used_forward_nodes(const std::vector &used_forwa }); } -size_t NewFgSeenGeneration() { - static size_t fg_seen_generation = 0; +SeenNum NewFgSeenGeneration() { + static SeenNum fg_seen_generation = 0; return ++fg_seen_generation; } diff --git a/mindspore/core/ir/func_graph.h b/mindspore/core/ir/func_graph.h index 95cfe9e4456..f51cf88ade2 100644 --- a/mindspore/core/ir/func_graph.h +++ b/mindspore/core/ir/func_graph.h @@ -318,7 +318,7 @@ class MS_CORE_API FuncGraph : public deprecated::api::FuncGraph, public FuncGrap mindspore::HashMap transforms_; // Parameter default value. std::map parameter_default_value_; - size_t seen_; + SeenNum seen_; std::list GetOrderedCnodes(); void EraseUnusedNodeInOrder(const AnfNodePtr &n); @@ -469,7 +469,7 @@ inline CNodePtr NewCNode(std::vector &&inputs, const FuncGraphPtr &f return fg->NewCNode(std::move(inputs)); } -size_t NewFgSeenGeneration(); +SeenNum NewFgSeenGeneration(); // Find the root cnodes of a segment of cnodes. std::shared_ptr> FindRoots(const std::vector &segment); diff --git a/mindspore/core/ir/graph_utils.cc b/mindspore/core/ir/graph_utils.cc index 50990f166a6..c0550d44547 100644 --- a/mindspore/core/ir/graph_utils.cc +++ b/mindspore/core/ir/graph_utils.cc @@ -1,7 +1,7 @@ /** * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). * - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -18,16 +18,17 @@ #include "ir/graph_utils.h" +#include "ir/anf.h" +#include "ir/func_graph.h" #include "utils/hash_map.h" #include "utils/hash_set.h" -#include "ir/func_graph.h" #include "utils/log_adapter.h" #include "utils/ms_context.h" #include "include/common/utils/utils.h" namespace mindspore { // Dump the circle from the strike node `next`. -static size_t DumpSortingCircleList(const std::deque &todo, const AnfNodePtr &next, size_t seen) { +static size_t DumpSortingCircleList(const std::deque &todo, const AnfNodePtr &next, SeenNum seen) { size_t pos = 0; auto circle_node_it = std::find(todo.begin(), todo.end(), next); for (; circle_node_it != todo.end(); circle_node_it++) { @@ -47,7 +48,7 @@ std::vector TopoSort(const AnfNodePtr &root, const SuccFunc &succ, c return res; } res.reserve(kVecReserve); - size_t seen = NewSeenGeneration(); + auto seen = NewSeenGeneration(); std::deque todo; (void)todo.emplace_back(root); while (!todo.empty()) { diff --git a/mindspore/core/ir/graph_utils_extends.cc b/mindspore/core/ir/graph_utils_extends.cc index 8a2a379695b..b1c65347fd8 100644 --- a/mindspore/core/ir/graph_utils_extends.cc +++ b/mindspore/core/ir/graph_utils_extends.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020-2021 Huawei Technologies Co., Ltd + * Copyright 2020-2022 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. @@ -63,7 +63,7 @@ class DeepFirstSearcher : public AnfIrVisitor { } private: - size_t seen_{0}; + SeenNum seen_{0}; IncludeFunc include_; FilterFunc filter_; std::vector res_{}; diff --git a/mindspore/core/ir/manager.cc b/mindspore/core/ir/manager.cc index 79d93eb5b5e..ff8fd353b1c 100644 --- a/mindspore/core/ir/manager.cc +++ b/mindspore/core/ir/manager.cc @@ -1,7 +1,7 @@ /** * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). * - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -501,7 +501,7 @@ void FuncGraphManager::ProcessInputsEdgeRemove(const CNodePtr &cnode) { } } -static inline void FollowGraph(const FuncGraphPtr &fg, size_t seen, std::vector *nodes) { +static inline void FollowGraph(const FuncGraphPtr &fg, SeenNum seen, std::vector *nodes) { if (fg == nullptr) { return; } @@ -1088,7 +1088,7 @@ void RecursiveComputer::CheckRecursiveGraphs(const FuncGraphPtr &fg, std::listseen_ == seen_num) { MS_LOG(DEBUG) << fg->ToString() << " had been checked"; diff --git a/mindspore/core/ir/manager.h b/mindspore/core/ir/manager.h index 8f95063a3e1..facd054815b 100644 --- a/mindspore/core/ir/manager.h +++ b/mindspore/core/ir/manager.h @@ -1,7 +1,7 @@ /** * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). * - * Copyright 2019-2021 Huawei Technologies Co., Ltd + * Copyright 2019-2022 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. @@ -274,7 +274,7 @@ class FuncGraphJTotalComputer final : public DepComputer { void ExtraReset() override { j_total_analysis_.clear(); } void RealRecompute(FuncGraphPtr fg) override; - bool SeekJ(const FuncGraphPtr &fg, size_t seen_num); + bool SeekJ(const FuncGraphPtr &fg, SeenNum seen_num); }; class MS_CORE_API FuncGraphManager : public std::enable_shared_from_this,