diff --git a/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.cc b/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.cc index deb1724b217..f74d0374f73 100644 --- a/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.cc +++ b/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.cc @@ -34,6 +34,8 @@ #include "backend/common/pass/add_akg_kernel_attrs.h" #include "backend/common/pass/sparse_process.h" #include "backend/common/pass/insert_assign_for_custom_op.h" +#include "backend/common/optimizer/dynamic_shape/convert_custom_op.h" +#include "backend/common/optimizer/dynamic_shape/link_custom_op.h" #include "utils/ms_context.h" #include "include/common/debug/anf_ir_dump.h" @@ -164,5 +166,33 @@ void EliminateIllegalDataTypePass(const std::shared_ptr &k } #endif } + +void DynamicShapeConvertPass(const std::shared_ptr &kernel_graph) { + MS_EXCEPTION_IF_NULL(kernel_graph); + MS_LOG(INFO) << "Start dynamic shape convert for kernel graph id:" << kernel_graph->graph_id(); +#ifdef ENABLE_DUMP_IR + auto context_ptr = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context_ptr); + bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); + if (save_graphs) { + std::string file_name = + "hwopt_d_before_dynamic_shape_convert_graph_" + std::to_string(kernel_graph->graph_id()) + ".ir"; + DumpIR(file_name, kernel_graph); + } +#endif + auto optimizer = std::make_shared(); + auto dynamic_shape_convert_pm = std::make_shared("dynamic_shape_convert_pm"); + dynamic_shape_convert_pm->AddPass(std::make_shared()); + dynamic_shape_convert_pm->AddPass(std::make_shared()); + optimizer->AddPassManager(dynamic_shape_convert_pm); + (void)optimizer->Optimize(kernel_graph); +#ifdef ENABLE_DUMP_IR + if (save_graphs) { + std::string file_name = + "hwopt_d_after_dynamic_shape_convert_graph_" + std::to_string(kernel_graph->graph_id()) + ".ir"; + DumpIR(file_name, kernel_graph); + } +#endif +} } // namespace opt } // namespace mindspore diff --git a/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.h b/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.h index 5ad9f78f8eb..762fc6a2735 100644 --- a/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.h +++ b/mindspore/ccsrc/backend/common/optimizer/common_backend_optimization.h @@ -24,6 +24,7 @@ void CommonFinalOptimization(const std::shared_ptr &kernel void CommonUnifyMindIR(const std::shared_ptr &kernel_graph); void AddDynamicShapeAttrPass(const std::shared_ptr &kernel_graph); void EliminateIllegalDataTypePass(const std::shared_ptr &kernel_graph); +void DynamicShapeConvertPass(const std::shared_ptr &kernel_graph); } // namespace opt } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.cc b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.cc similarity index 50% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.cc rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.cc index 420517288a2..9744026c544 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.cc +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.cc @@ -14,30 +14,49 @@ * limitations under the License. */ -#include "plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.h" +#include "backend/common/optimizer/dynamic_shape/convert_custom_op.h" #include +#include #include "backend/common/session/anf_runtime_algorithm.h" #include "include/common/utils/anfalgo.h" #include "backend/common/optimizer/helper.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" +#include "backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h" +#include "utils/ms_context.h" namespace mindspore { namespace opt::dynamic_shape { -const BaseRef ConvertDynamicOp::DefinePattern() const { - VarPtr X = std::make_shared(IsDynamicOp); - return BaseRef({X}); +bool ConvertCustomOp::Run(const FuncGraphPtr &func_graph) { + MS_EXCEPTION_IF_NULL(func_graph); + auto node_list = TopoSort(func_graph->get_return()); + for (const auto &node : node_list) { + if (!IsRealCNode(node)) { + continue; + } + ConvertCustomOpForNode(node); + } + return true; } -const AnfNodePtr ConvertDynamicOp::Process(const FuncGraphPtr &graph, const AnfNodePtr &node, const EquivPtr &) const { - MS_EXCEPTION_IF_NULL(graph); +void ConvertCustomOp::ConvertCustomOpForNode(const AnfNodePtr &node) const { MS_EXCEPTION_IF_NULL(node); - auto infer_node = GenInferNode(node); - auto init_node = GenInitNode(node); - auto update_node = GenUpdateNode(node); + bool is_dynamic_node = common::AnfAlgo::IsDynamicShape(node); + AnfNodePtr infer_node = nullptr; + AnfNodePtr init_node = nullptr; + AnfNodePtr update_node = nullptr; + if (is_dynamic_node) { + infer_node = GenInferNode(node); + init_node = GenInitNode(node); + } + + auto ms_context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(ms_context); + if (ms_context->get_param(MS_CTX_DEVICE_TARGET) != kCPUDevice) { + update_node = GenUpdateNode(node); + } + RelatedCustomActorNode custom_nodes = {infer_node, init_node, update_node}; CustomActorNodeManager::Instance().Register(node, custom_nodes); - return node; } } // namespace opt::dynamic_shape } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.h b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.h similarity index 55% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.h rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.h index 392ed2be66f..e219b664e0d 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.h +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/convert_custom_op.h @@ -14,19 +14,21 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_GENERAL_OP_H -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_GENERAL_OP_H +#ifndef MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_CONVERT_CUSTOM_OP_H +#define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_CONVERT_CUSTOM_OP_H #include "ir/anf.h" #include "backend/common/optimizer/optimizer.h" namespace mindspore::opt::dynamic_shape { -class ConvertGeneralOp : public PatternProcessPass { +class ConvertCustomOp : public Pass { public: - explicit ConvertGeneralOp(bool multigraph = true) : PatternProcessPass("convert_general_op", multigraph) {} - ~ConvertGeneralOp() override = default; - const BaseRef DefinePattern() const override; - const AnfNodePtr Process(const FuncGraphPtr &graph, const AnfNodePtr &node, const EquivPtr &) const override; + ConvertCustomOp() : Pass("convert_custom_op") {} + ~ConvertCustomOp() override = default; + bool Run(const FuncGraphPtr &func_graph) override; + + private: + void ConvertCustomOpForNode(const AnfNodePtr &node) const; }; } // namespace mindspore::opt::dynamic_shape -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_GENERAL_OP_H +#endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_CONVERT_CUSTOM_OP_H diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.cc b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.cc similarity index 63% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.cc rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.cc index 11c68cf7634..c6d423f66e6 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.cc +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.cc @@ -14,16 +14,17 @@ * limitations under the License. */ -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" +#include "backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h" #include #include "backend/common/session/anf_runtime_algorithm.h" #include "include/common/utils/anfalgo.h" #include "include/common/utils/utils.h" #include "utils/anf_utils.h" +#include "kernel/kernel.h" namespace mindspore { -namespace { +namespace opt::dynamic_shape { bool IsRealCNode(const BaseRef &n) { if (utils::isa(n)) { CNodePtr cnode = utils::cast(n); @@ -31,45 +32,6 @@ bool IsRealCNode(const BaseRef &n) { } return false; } -} // namespace - -namespace opt::dynamic_shape { -bool IsGeneralOp(const BaseRef &n) { - if (IsDynamicOp(n)) { - return false; - } - - if (IsInheritedDynamicOp(n)) { - return false; - } - - return IsRealCNode(n); -} - -bool IsDynamicOp(const BaseRef &n) { - if (!IsRealCNode(n)) { - return false; - } - - CNodePtr cnode = utils::cast(n); - MS_EXCEPTION_IF_NULL(cnode); - auto op_name = common::AnfAlgo::GetCNodeName(cnode); - return kComputeDepend.find(op_name) != kComputeDepend.end(); -} - -bool IsInheritedDynamicOp(const BaseRef &n) { - if (IsDynamicOp(n)) { - return false; - } - - if (!IsRealCNode(n)) { - return false; - } - - CNodePtr cnode = utils::cast(n); - MS_EXCEPTION_IF_NULL(cnode); - return common::AnfAlgo::IsNodeInputDynamicShape(cnode) || AnfUtils::IsNodeOutputDynamicShape(cnode); -} AnfNodePtr GenInferNode(const AnfNodePtr &node, bool fake_flag) { MS_EXCEPTION_IF_NULL(node); @@ -89,18 +51,25 @@ AnfNodePtr GenInferNode(const AnfNodePtr &node, bool fake_flag) { return infer_node; } -AnfNodePtr GenInitNode(const AnfNodePtr &node) { +AnfNodePtr GenInitNode(const AnfNodePtr &node, bool fake_flag) { MS_EXCEPTION_IF_NULL(node); auto cnode = node->cast(); MS_EXCEPTION_IF_NULL(cnode); - auto kernel_mod = AnfAlgo::GetKernelMod(cnode); - MS_EXCEPTION_IF_NULL(kernel_mod); - auto init_node = AnfUtils::NewInitActorNode([kernel_mod](void *) { kernel_mod->InitOp(); }, cnode); + AnfUtils::CustomActorCallback actor_func; + if (fake_flag) { + actor_func = [](void *) -> void { return; }; + } else { + auto kernel_mod = AnfAlgo::GetKernelMod(cnode); + MS_EXCEPTION_IF_NULL(kernel_mod); + actor_func = [kernel_mod](void *) { kernel_mod->InitOp(); }; + } + + auto init_node = AnfUtils::NewInitActorNode(actor_func, cnode, fake_flag); init_node->set_kernel_info(std::make_shared()); return init_node; } -AnfNodePtr GenUpdateNode(const AnfNodePtr &node, bool just_sync_flag) { +AnfNodePtr GenUpdateNode(const AnfNodePtr &node) { // Some not dynamic shape node should sync after launch for latter node. // Use a flag `just_sync_flag` to distinguish them with dynamic ones. MS_EXCEPTION_IF_NULL(node); @@ -108,19 +77,18 @@ AnfNodePtr GenUpdateNode(const AnfNodePtr &node, bool just_sync_flag) { MS_EXCEPTION_IF_NULL(cnode); auto kernel_mod = AnfAlgo::GetKernelMod(cnode); MS_EXCEPTION_IF_NULL(kernel_mod); - auto update_node = - AnfUtils::NewUpdateActorNode([kernel_mod](void *) { kernel_mod->UpdateOp(); }, cnode, just_sync_flag); + auto update_node = AnfUtils::NewUpdateActorNode([kernel_mod](void *) { kernel_mod->UpdateOp(); }, cnode); update_node->set_kernel_info(std::make_shared()); return update_node; } -bool IsDynUpdate(const AnfNodePtr &node) { +bool IsNeedUpdateOp(const AnfNodePtr &node) { MS_EXCEPTION_IF_NULL(node); - auto custom_actor_type = AnfUtils::GetCustomActorType(node); - if (custom_actor_type != kUpdate) { - MS_LOG(EXCEPTION) << node->fullname_with_scope() << " is not a custom update node!"; - } - return !AnfUtils::GetCustomActorJustSyncFlag(node); + auto cnode = node->cast(); + MS_EXCEPTION_IF_NULL(cnode); + auto kernel_mod = AnfAlgo::GetKernelMod(cnode); + MS_EXCEPTION_IF_NULL(kernel_mod); + return kernel_mod->IsNeedUpdateOp(); } CustomActorNodeManager &CustomActorNodeManager::Instance() { diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h similarity index 76% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h index d1a4195ddac..b5641851d84 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_ASCEND_DYNAMIC_SHAPE_HELPER_H -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_ASCEND_DYNAMIC_SHAPE_HELPER_H +#ifndef MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H +#define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H #include #include "ir/anf.h" @@ -23,13 +23,11 @@ #include "backend/common/optimizer/optimizer.h" namespace mindspore::opt::dynamic_shape { -bool IsGeneralOp(const BaseRef &n); -bool IsDynamicOp(const BaseRef &n); -bool IsInheritedDynamicOp(const BaseRef &n); +bool IsRealCNode(const BaseRef &n); +bool IsNeedUpdateOp(const AnfNodePtr &node); AnfNodePtr GenInferNode(const AnfNodePtr &node, bool fake_flag = false); -AnfNodePtr GenInitNode(const AnfNodePtr &node); -AnfNodePtr GenUpdateNode(const AnfNodePtr &node, bool just_sync_flag = false); -bool IsDynUpdate(const AnfNodePtr &node); +AnfNodePtr GenInitNode(const AnfNodePtr &node, bool fake_flag = false); +AnfNodePtr GenUpdateNode(const AnfNodePtr &node); struct RelatedCustomActorNode { AnfNodePtr infer_node; @@ -60,4 +58,4 @@ class CustomActorNodeManager { OrderedMap custom_nodes_map_; }; } // namespace mindspore::opt::dynamic_shape -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_ASCEND_DYNAMIC_SHAPE_HELPER_H +#endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_DYNAMIC_SHAPE_HELPER_H diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.cc b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc similarity index 81% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.cc rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc index 45cb43c2351..1038615da9a 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.cc +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.h" +#include "backend/common/optimizer/dynamic_shape/link_custom_op.h" #include #include @@ -23,7 +23,7 @@ #include "backend/common/session/anf_runtime_algorithm.h" #include "include/common/utils/anfalgo.h" #include "backend/common/optimizer/helper.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" +#include "backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h" namespace mindspore { namespace opt::dynamic_shape { @@ -72,7 +72,7 @@ bool LinkInternalOp(const FuncGraphPtr &g, const AnfNodePtr &node, AnfNodePtrLis changed = true; } - if (IsDynUpdate(custom_nodes.update_node)) { + if (IsNeedUpdateOp(node) && custom_nodes.update_node != nullptr) { InsertDepend(g, node, custom_nodes.update_node, depend_nodes); // link launch => update changed = true; } @@ -93,19 +93,31 @@ bool LinkInputOp(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList *d for (size_t i = 0; i < input_num; ++i) { auto prev = common::AnfAlgo::GetPrevNodeOutput(cnode, i); const auto &prev_node = prev.first; - if (prev_node == nullptr || !CustomActorNodeManager::Instance().IsRegistered(prev_node)) { + if (prev_node == nullptr) { + continue; + } + if (!CustomActorNodeManager::Instance().IsRegistered(prev_node)) { continue; } auto prev_custom_nodes = CustomActorNodeManager::Instance().GetCustomActorNodes(prev_node); if (prev_custom_nodes.infer_node != nullptr) { - InsertDepend(g, prev_custom_nodes.infer_node, custom_nodes.infer_node, - depend_nodes); // link prev.infer => curr.infer + // link prev.infer => curr.infer + InsertDepend(g, prev_custom_nodes.infer_node, custom_nodes.infer_node, depend_nodes); changed = true; } - if (IsDynUpdate(prev_custom_nodes.update_node)) { - InsertDepend(g, prev_custom_nodes.update_node, custom_nodes.infer_node, - depend_nodes); // link prev.update => curr.infer - changed = true; + + if (IsNeedUpdateOp(prev_node)) { + if (prev_custom_nodes.update_node != nullptr) { + // link prev.update => curr.infer + InsertDepend(g, prev_custom_nodes.update_node, custom_nodes.infer_node, depend_nodes); + changed = true; + } else { + // for CPU, its Updateop is in Launch function, so its update_node is set to nullptr, for reduce the time cast + // of send messages between actors + // link prev.launch => curr.infer + InsertDepend(g, prev_node, custom_nodes.infer_node, depend_nodes); + changed = true; + } } } return changed; @@ -135,14 +147,21 @@ bool LinkDependSync(const FuncGraphPtr &g, const CNodePtr &cnode, AnfNodePtrList // If previous node is dynamic, so it was already link. auto prev_custom_nodes = CustomActorNodeManager::Instance().GetCustomActorNodes(prev_node); - if (IsDynUpdate(prev_custom_nodes.update_node)) { + if (IsNeedUpdateOp(prev_node)) { continue; } - // 1. Link prev_node => prev_node.update if its update is just sync. - InsertDepend(g, prev_node, prev_custom_nodes.update_node, depend_nodes); - // 1. Link prev_node => prev_node.update if its update is just sync. - // 2. Link prev_node.update => cur_node.infer. - InsertDepend(g, prev_custom_nodes.update_node, custom_nodes.infer_node, depend_nodes); + if (prev_custom_nodes.update_node != nullptr) { + // 1. Link prev_node => prev_node.update if its update is just sync. + InsertDepend(g, prev_node, prev_custom_nodes.update_node, depend_nodes); + // 2. Link prev_node.update => cur_node.infer. + InsertDepend(g, prev_custom_nodes.update_node, custom_nodes.infer_node, depend_nodes); + } else { + // for CPU, its Updateop is in Launch function, so its update_node is set to nullptr, for reduce the time cast of + // send messages between actors + // Link prev_node.launch => cur_node.infer. + InsertDepend(g, prev_node, custom_nodes.infer_node, depend_nodes); + } + changed = true; } return changed; diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.h b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h similarity index 82% rename from mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.h rename to mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h index a87ef819160..07240a6bc06 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.h +++ b/mindspore/ccsrc/backend/common/optimizer/dynamic_shape/link_custom_op.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H +#ifndef MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H +#define MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H #include "ir/anf.h" #include "backend/common/optimizer/optimizer.h" @@ -28,4 +28,4 @@ class LinkCustomOp : public Pass { bool Run(const FuncGraphPtr &func_graph) override; }; } // namespace mindspore::opt::dynamic_shape -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_GENERAL_OP_H +#endif // MINDSPORE_CCSRC_BACKEND_COMMON_OPTIMIZER_DYNAMIC_SHAPE_LINK_CUSTOM_OP_H diff --git a/mindspore/ccsrc/include/common/utils/utils.h b/mindspore/ccsrc/include/common/utils/utils.h index 0cdd5657713..d82ec9f9ce0 100644 --- a/mindspore/ccsrc/include/common/utils/utils.h +++ b/mindspore/ccsrc/include/common/utils/utils.h @@ -554,6 +554,8 @@ constexpr auto kAttrGroupRankIds = "group_rank_ids"; // TODO(dsj): for ms_function running in graph_mode. should be delete later constexpr auto kAttrMSFunction = "ms_function_graph"; +// for single_op_compile_and_run condition and set_context GraphMode. should be delete later +constexpr auto kAttrSingleOpCompile = "compile_and_run_in_single_op"; // custom operator func type constexpr auto kCustomTypeAOT = "aot"; diff --git a/mindspore/ccsrc/kernel/ascend_kernel_mod.cc b/mindspore/ccsrc/kernel/ascend_kernel_mod.cc index eb1cd7bfcf7..b41ad216e1a 100644 --- a/mindspore/ccsrc/kernel/ascend_kernel_mod.cc +++ b/mindspore/ccsrc/kernel/ascend_kernel_mod.cc @@ -17,6 +17,7 @@ #include "kernel/ascend_kernel_mod.h" #include "runtime/device/kernel_runtime.h" #include "runtime/rt.h" +#include "include/common/utils/anfalgo.h" namespace mindspore { namespace kernel { void AscendKernelMod::UpdateOp() { @@ -27,5 +28,18 @@ void AscendKernelMod::UpdateOp() { MS_LOG(EXCEPTION) << "Call runtime rtStreamSynchronize failed."; } } + +bool AscendKernelMod::IsNeedUpdateOp() { + auto node = anf_node_.lock(); + MS_EXCEPTION_IF_NULL(node); + auto cnode = node->cast(); + MS_EXCEPTION_IF_NULL(cnode); + + auto op_name = common::AnfAlgo::GetCNodeName(cnode); + if (kComputeDepend.find(op_name) != kComputeDepend.end()) { + is_need_updateop_ = true; + } + return is_need_updateop_; +} } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/kernel/ascend_kernel_mod.h b/mindspore/ccsrc/kernel/ascend_kernel_mod.h index f6b669ed2c3..ba457d220d9 100644 --- a/mindspore/ccsrc/kernel/ascend_kernel_mod.h +++ b/mindspore/ccsrc/kernel/ascend_kernel_mod.h @@ -47,6 +47,7 @@ class AscendKernelMod : public KernelMod { #endif } void UpdateOp() override; + bool IsNeedUpdateOp() override; void InitDynamicKernel(const CNodePtr &cnode_ptr, void *stream) { if (dynamic_kernel_ == nullptr) { diff --git a/mindspore/ccsrc/kernel/kernel.h b/mindspore/ccsrc/kernel/kernel.h index 3e4a0364f1c..1b8877aa0a1 100644 --- a/mindspore/ccsrc/kernel/kernel.h +++ b/mindspore/ccsrc/kernel/kernel.h @@ -219,6 +219,8 @@ class KernelMod { void set_stream(StreamType stream) { stream_ = stream; } StreamType stream() const { return stream_; } void SetAtomicCleanNodes(const std::vector &atomic_clean_node); + // set true if need to update output's shape after launch in dynamic_shape, like Unique + virtual bool IsNeedUpdateOp() { return is_need_updateop_; } protected: void InferShape(); @@ -238,6 +240,7 @@ class KernelMod { std::vector output_size_list_; std::vector workspace_size_list_; std::set depend_list_; + bool is_need_updateop_ = false; private: void InferShapeForNopNode(const AnfNodePtr &input_node); diff --git a/mindspore/ccsrc/plugin/device/ascend/hal/hardware/ascend_device_context.cc b/mindspore/ccsrc/plugin/device/ascend/hal/hardware/ascend_device_context.cc index 3d1b8cf96b2..2d2cc11cb5c 100644 --- a/mindspore/ccsrc/plugin/device/ascend/hal/hardware/ascend_device_context.cc +++ b/mindspore/ccsrc/plugin/device/ascend/hal/hardware/ascend_device_context.cc @@ -36,6 +36,7 @@ #include "plugin/device/ascend/hal/device/ascend_bucket.h" #include "common/util/error_manager/error_manager.h" #include "plugin/device/ascend/hal/device/ascend_memory_adapter.h" +#include "backend/common/optimizer/common_backend_optimization.h" #ifndef ENABLE_SECURITY #include "debug/data_dump/dump_json_parser.h" @@ -407,7 +408,7 @@ void AscendDeviceContext::PreprocessBeforeRunGraph(const KernelGraphPtr &graph) } else if (graph->is_dynamic_shape() && IsGraphMode()) { device::ascend::InsertAtomicCleanOps(graph->execution_order(), &node_atomics_); SetAtomicCleanToNodes(graph); // graph mode may can do it too, instead of update execorder - opt::AscendDynamicShapeConvert(graph); + opt::DynamicShapeConvertPass(graph); AscendStreamAssign::GetInstance().AssignStream(NOT_NULL(graph)); AssignOutputNopNodeDeviceAddress(graph); LaunchDeviceLibrary(); diff --git a/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/dynamic_tbe_kernel_mod.cc b/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/dynamic_tbe_kernel_mod.cc index 5b5b39d0cf1..36acd3597f9 100644 --- a/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/dynamic_tbe_kernel_mod.cc +++ b/mindspore/ccsrc/plugin/device/ascend/kernel/tbe/dynamic_tbe_kernel_mod.cc @@ -107,7 +107,7 @@ void DynamicTbeKernelMod::InitOp() { } // gen FuncStub - if (func_stub_ == nullptr || handle_ == nullptr) { + if (func_stub_ == nullptr && handle_ == nullptr) { auto func_stub = KernelManager::GenFuncStub(*kernel_pack_, false, &block_dim_, &handle_, &origin_key_); if (kernel_pack_->kernel_json_info().has_kernel_list) { if (func_stub != 1) { diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc index d0b66e47a53..f36b0e22d3c 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc +++ b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.cc @@ -153,10 +153,6 @@ #include "plugin/device/ascend/optimizer/mindir/bn_grad_unify_mindir.h" #include "plugin/device/ascend/optimizer/mindir/all_to_all_unify_mindir.h" #include "plugin/device/ascend/optimizer/mindir/neighbor_exchange_v2_unify_mindir.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/link_custom_op.h" #include "backend/common/pass/adjust_depend_for_parallel_optimizer_recompute_all_gather.h" #include "plugin/device/ascend/kernel/tbe/tbe_kernel_compile.h" #include "utils/ms_context.h" @@ -621,34 +617,5 @@ void AscendUnifyMindIR(const std::shared_ptr &graph) { } #endif } -void AscendDynamicShapeConvert(const std::shared_ptr &kernel_graph) { - auto context_ptr = MsContext::GetInstance(); - MS_EXCEPTION_IF_NULL(context_ptr); -#ifdef ENABLE_DUMP_IR - bool save_graphs = context_ptr->get_param(MS_CTX_SAVE_GRAPHS_FLAG); - if (save_graphs) { - std::string file_name = - "hwopt_d_before_dynamic_shape_convert_graph_" + std::to_string(kernel_graph->graph_id()) + ".ir"; - DumpIR(file_name, kernel_graph); - DumpIRProto(kernel_graph, "before_dynamic_shape_convert_hwopt_" + std::to_string(kernel_graph->graph_id())); - } -#endif - auto optimizer = std::make_shared(); - auto dynamic_shape_convert_pm = std::make_shared("dynamic_shape_convert_pm"); - dynamic_shape_convert_pm->AddPass(std::make_shared()); - dynamic_shape_convert_pm->AddPass(std::make_shared()); - dynamic_shape_convert_pm->AddPass(std::make_shared()); - dynamic_shape_convert_pm->AddPass(std::make_shared()); - optimizer->AddPassManager(dynamic_shape_convert_pm); - (void)optimizer->Optimize(kernel_graph); - kernel_graph->SetExecOrderByDefault(); -#ifdef ENABLE_DUMP_IR - if (save_graphs) { - std::string file_name = - "hwopt_d_after_dynamic_shape_convert_graph_" + std::to_string(kernel_graph->graph_id()) + ".ir"; - DumpIR(file_name, kernel_graph); - } -#endif -} } // namespace opt } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.h b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.h index d1a2e634edb..29c853db228 100644 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.h +++ b/mindspore/ccsrc/plugin/device/ascend/optimizer/ascend_backend_optimization.h @@ -28,7 +28,6 @@ void AscendBackendOptimization(const std::shared_ptr &kern void AscendBackendIRFusionOptimization(const std::shared_ptr &kernel_graph); void AscendBackendUBFusionOptimization(const std::shared_ptr &kernel_graph); void AscendUnifyMindIR(const std::shared_ptr &kernel_graph); -void AscendDynamicShapeConvert(const std::shared_ptr &kernel_graph); } // namespace opt } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.h b/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.h deleted file mode 100644 index e89ca5d524d..00000000000 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_dynamic_op.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 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. - * 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_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_DYNAMIC_OP_H -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_DYNAMIC_OP_H - -#include "ir/anf.h" -#include "backend/common/optimizer/optimizer.h" - -namespace mindspore::opt::dynamic_shape { -class ConvertDynamicOp : public PatternProcessPass { - public: - explicit ConvertDynamicOp(bool multigraph = true) : PatternProcessPass("convert_dynamic_op", multigraph) {} - ~ConvertDynamicOp() override = default; - const BaseRef DefinePattern() const override; - const AnfNodePtr Process(const FuncGraphPtr &graph, const AnfNodePtr &node, const EquivPtr &) const override; -}; -} // namespace mindspore::opt::dynamic_shape -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_DYNAMIC_OP_H diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.cc b/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.cc deleted file mode 100644 index 732191b406e..00000000000 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 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. - * 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 "plugin/device/ascend/optimizer/dynamic_shape/convert_general_op.h" - -#include -#include "backend/common/session/anf_runtime_algorithm.h" -#include "include/common/utils/anfalgo.h" -#include "backend/common/optimizer/helper.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" - -namespace mindspore { -namespace opt::dynamic_shape { -const BaseRef ConvertGeneralOp::DefinePattern() const { - VarPtr X = std::make_shared(IsGeneralOp); - return BaseRef({X}); -} - -const AnfNodePtr ConvertGeneralOp::Process(const FuncGraphPtr &graph, const AnfNodePtr &node, const EquivPtr &) const { - MS_EXCEPTION_IF_NULL(graph); - MS_EXCEPTION_IF_NULL(node); - auto stub_infer_node = GenInferNode(node, true); - auto init_node = GenInitNode(node); - auto sync_node = GenUpdateNode(node, true); // Use to call sync if needed. - RelatedCustomActorNode custom_nodes = {stub_infer_node, init_node, sync_node}; - CustomActorNodeManager::Instance().Register(node, custom_nodes); - return node; -} -} // namespace opt::dynamic_shape -} // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.cc b/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.cc deleted file mode 100644 index 9d40d3b35b2..00000000000 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.cc +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 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. - * 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 "plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.h" - -#include -#include "backend/common/session/anf_runtime_algorithm.h" -#include "include/common/utils/anfalgo.h" -#include "backend/common/optimizer/helper.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" - -namespace mindspore { -namespace opt::dynamic_shape { -const BaseRef ConvertInheritedDynamicOp::DefinePattern() const { - VarPtr X = std::make_shared(IsInheritedDynamicOp); - return BaseRef({X}); -} - -const AnfNodePtr ConvertInheritedDynamicOp::Process(const FuncGraphPtr &graph, const AnfNodePtr &node, - const EquivPtr &) const { - MS_EXCEPTION_IF_NULL(graph); - MS_EXCEPTION_IF_NULL(node); - auto infer_node = GenInferNode(node); - auto init_node = GenInitNode(node); - auto sync_node = GenUpdateNode(node, true); // Use to call sync if needed. - RelatedCustomActorNode custom_nodes = {infer_node, init_node, sync_node}; - CustomActorNodeManager::Instance().Register(node, custom_nodes); - return node; -} -} // namespace opt::dynamic_shape -} // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.h b/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.h deleted file mode 100644 index 5085d3ed78f..00000000000 --- a/mindspore/ccsrc/plugin/device/ascend/optimizer/dynamic_shape/convert_inherited_dynamic_op.h +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 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. - * 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_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_INHERITED_DYNAMIC_OP_H -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_INHERITED_DYNAMIC_OP_H - -#include "ir/anf.h" -#include "backend/common/optimizer/optimizer.h" - -namespace mindspore::opt::dynamic_shape { -class ConvertInheritedDynamicOp : public PatternProcessPass { - public: - explicit ConvertInheritedDynamicOp(bool multigraph = true) - : PatternProcessPass("convert_inherited_dynamic_op", multigraph) {} - ~ConvertInheritedDynamicOp() override = default; - const BaseRef DefinePattern() const override; - const AnfNodePtr Process(const FuncGraphPtr &graph, const AnfNodePtr &node, const EquivPtr &) const override; -}; -} // namespace mindspore::opt::dynamic_shape -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_DYNAMIC_SHAPE_CONVERT_GENERAL_OP_H diff --git a/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.cc b/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.cc index 0a434770b2a..9027a22fcc5 100644 --- a/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.cc +++ b/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.cc @@ -261,6 +261,13 @@ void CPUDeviceContext::PreprocessBeforeRunGraph(const KernelGraphPtr &graph) con graph->set_execution_order(execution_order); } +bool CPUDeviceContext::LaunchCustomFunc(const AnfNodePtr &kernel) const { + MS_EXCEPTION_IF_NULL(kernel); + auto custom_func = AnfUtils::GetCustomFunc(kernel); + custom_func(nullptr); + return true; +} + bool CPUDeviceContext::LaunchKernel(const CNodePtr &kernel, const std::vector &inputs, const std::vector &workspace, const std::vector &outputs, bool) const { diff --git a/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.h b/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.h index 4e94fc82321..72656e84950 100644 --- a/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.h +++ b/mindspore/ccsrc/plugin/device/cpu/hal/hardware/cpu_device_context.h @@ -62,6 +62,8 @@ class CPUDeviceContext : public DeviceContext { bool LoadCollectiveCommLib() override; + bool LaunchCustomFunc(const AnfNodePtr &kernel) const override; + private: DISABLE_COPY_AND_ASSIGN(CPUDeviceContext); diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/coalesce_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/coalesce_cpu_kernel.cc index 07854692b8a..65370691b8b 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/coalesce_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/coalesce_cpu_kernel.cc @@ -69,6 +69,7 @@ void CoalesceCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { auto indices_shape = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, 0); values_size_ = indices_shape[1]; shape_size_ = indices_shape[0]; + is_need_updateop_ = true; } void CoalesceCpuKernelMod::Check(const std::vector &inputs) { diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/cpu_kernel.cc index aabd61255f5..843a52a3e24 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/cpu_kernel.cc @@ -80,6 +80,7 @@ void NativeCpuKernelMod::Init(const CNodePtr &kernel_node) { cnode_ptr_ = kernel_node; } + workspace_size_list_.clear(); InitKernel(kernel_node); InitInputOutputSize(kernel_node); } diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/dynamic_stitch_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/dynamic_stitch_cpu_kernel.cc index 77ac452c41c..1a521c7a337 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/dynamic_stitch_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/dynamic_stitch_cpu_kernel.cc @@ -157,6 +157,7 @@ void DynamicStitchCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { } kernel_func_ = func_list_[index].second; + is_need_updateop_ = true; } MS_KERNEL_FACTORY_REG(NativeCpuKernelMod, DynamicStitch, DynamicStitchCpuKernelMod); diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/map_cache_idx_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/map_cache_idx_cpu_kernel.cc index 12c69ebb66e..39524790a6f 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/map_cache_idx_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/map_cache_idx_cpu_kernel.cc @@ -85,6 +85,7 @@ void MapCacheIdxCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { << "', the first dimension of 'HashMap' should be greater than 0, but got " << hashmap_length_; } dtype_ = AnfAlgo::GetInputDeviceDataType(kernel_node, 0); + is_need_updateop_ = true; } bool MapCacheIdxCpuKernelMod::Launch(const std::vector &inputs, diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/masked_select_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/masked_select_cpu_kernel.cc index 84b183745ec..80ebcdcc4fc 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/masked_select_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/masked_select_cpu_kernel.cc @@ -42,6 +42,7 @@ void MaskedSelectCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { MS_LOG(EXCEPTION) << "MaskedSelect does not support this kernel data type: " << kernel_attr; } kernel_func_ = func_list_[index].second; + is_need_updateop_ = true; } template diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/matrix_diag_part_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/matrix_diag_part_cpu_kernel.cc index 9b7643e0fda..12860325b74 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/matrix_diag_part_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/matrix_diag_part_cpu_kernel.cc @@ -43,6 +43,7 @@ void MatrixDiagPartCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { MS_LOG(EXCEPTION) << "MatrixDiagPart does not support this kernel data type: " << kernel_attr; } kernel_func_ = func_list_[index].second; + is_need_updateop_ = true; } template diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/pad_and_shift_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/pad_and_shift_cpu_kernel.cc index 25598c990e7..68e77303047 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/pad_and_shift_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/pad_and_shift_cpu_kernel.cc @@ -41,6 +41,7 @@ void PadAndShiftCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { << cum_sum_arr_shape.size() << "."; } cum_sum_size_ = cum_sum_arr_shape[0]; + is_need_updateop_ = true; } bool PadAndShiftCpuKernelMod::Launch(const std::vector &inputs, diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/rl/tensor_array_stack_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/rl/tensor_array_stack_kernel.cc index 89b96689459..df46e43335e 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/rl/tensor_array_stack_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/rl/tensor_array_stack_kernel.cc @@ -52,6 +52,7 @@ void TensorArrayStackCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { } output_size_list_.push_back(value_size_); input_size_list_.push_back(sizeof(int64_t)); + is_need_updateop_ = true; } void TensorArrayStackCpuKernelMod::PostExecute() { diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/sub_and_filter_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/sub_and_filter_cpu_kernel.cc index fa86977f750..94c4cb32098 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/sub_and_filter_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/sub_and_filter_cpu_kernel.cc @@ -30,6 +30,7 @@ void SubAndFilterCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node); node_wpt_ = kernel_node; input_x_dtype_ = AnfAlgo::GetInputDeviceDataType(kernel_node, 0); + is_need_updateop_ = true; } bool SubAndFilterCpuKernelMod::Launch(const std::vector &inputs, diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/unique_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/unique_cpu_kernel.cc index fefddf8351a..d75562efd0d 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/unique_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/unique_cpu_kernel.cc @@ -40,6 +40,7 @@ void UniqueCpuKernelMod::InitKernel(const CNodePtr &kernel_node) { if (common::AnfAlgo::HasNodeAttr(SORTED, kernel_node)) { sorted_ = common::AnfAlgo::GetNodeAttr(kernel_node, SORTED); } + is_need_updateop_ = true; } void UniqueCpuKernelMod::InitInputOutputSize(const CNodePtr &kernel_node) { @@ -63,6 +64,7 @@ bool UniqueCpuKernelMod::Launch(const std::vector &inputs, << "', the dtype of input should be float16, float32, int32, or int64, but got " << TypeIdToType(dtype_)->ToString(); } + if (!node_wpt_.expired()) { auto node_ = node_wpt_.lock(); if (!node_) { diff --git a/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.cc b/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.cc index 89afefd2558..e3da3a23c60 100644 --- a/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.cc +++ b/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.cc @@ -37,6 +37,7 @@ #include "profiler/device/gpu/gpu_profiling_utils.h" #include "backend/common/session/kernel_graph.h" #include "plugin/device/gpu/kernel/gpu_kernel.h" +#include "backend/common/optimizer/common_backend_optimization.h" #ifdef ENABLE_DUMP_IR #include "include/common/debug/rdr/recorder_manager.h" #include "debug/rdr/mem_address_recorder.h" @@ -265,6 +266,14 @@ void GPUDeviceContext::OptimizeGraph(const KernelGraphPtr &graph) const { device::gpu::AssignGpuStream(graph); } +void GPUDeviceContext::PreprocessBeforeRunGraph(const KernelGraphPtr &graph) const { + auto ms_context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(ms_context); + if (graph->is_dynamic_shape() && ms_context->get_param(MS_CTX_EXECUTION_MODE) == kGraphMode) { + opt::DynamicShapeConvertPass(graph); + } +} + void GPUDeviceContext::OptimizeGraphWithoutDeviceInfo(const KernelGraphPtr &graph) const { MS_EXCEPTION_IF_NULL(graph); // Operator fusion optimization. @@ -421,8 +430,21 @@ void GPUDeviceContext::UpdateDynamicShape(const CNodePtr &kernel) const { kernel::NativeGpuKernelMod *gpu_kernel = dynamic_cast(kernel_mod); MS_EXCEPTION_IF_NULL(gpu_kernel); - gpu_kernel->InferOp(); - gpu_kernel->InitOp(); + if (ms_context->get_param(MS_CTX_EXECUTION_MODE) == kPynativeMode || + common::AnfAlgo::GetBooleanAttr(kernel, kAttrSingleOpCompile)) { + gpu_kernel->InferOp(); + gpu_kernel->InitOp(); + } +} + +bool GPUDeviceContext::LaunchCustomFunc(const AnfNodePtr &kernel) const { + MS_EXCEPTION_IF_NULL(kernel); + auto custom_func = AnfUtils::GetCustomFunc(kernel); + if (!BindDeviceToCurrentThread()) { + return false; + } + custom_func(nullptr); + return true; } bool GPUDeviceContext::LaunchKernel(const CNodePtr &kernel, const std::vector &inputs, @@ -436,6 +458,7 @@ bool GPUDeviceContext::LaunchKernel(const CNodePtr &kernel, const std::vectorget_param(MS_CTX_EXECUTION_MODE) == kPynativeMode || + common::AnfAlgo::GetBooleanAttr(kernel, kAttrSingleOpCompile))) { kernel::NativeGpuKernelMod *gpu_kernel = dynamic_cast(kernel_mod); MS_EXCEPTION_IF_NULL(gpu_kernel); gpu_kernel->UpdateOp(); diff --git a/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.h b/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.h index 3bf546e6976..d88880abbad 100644 --- a/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.h +++ b/mindspore/ccsrc/plugin/device/gpu/hal/hardware/gpu_device_context.h @@ -78,6 +78,10 @@ class GPUDeviceContext : public DeviceContext { bool LoadCollectiveCommLib() override; + void PreprocessBeforeRunGraph(const KernelGraphPtr &graph) const override; + + bool LaunchCustomFunc(const AnfNodePtr &kernel) const override; + private: DISABLE_COPY_AND_ASSIGN(GPUDeviceContext); bool InitDevice(); diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/dynamic_range_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/dynamic_range_gpu_kernel.h index 991930f0280..79e67e7b9d8 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/dynamic_range_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/dynamic_range_gpu_kernel.h @@ -121,7 +121,7 @@ class DynamicRangeGpuKernelMod : public NativeGpuKernelMod { max_output_length_ = GetAttr(kernel_node, "maxlen"); kernel_node_ = kernel_node; InitSizeLists(); - + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/matrix_diag_part_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/matrix_diag_part_gpu_kernel.h index 69217cd8c63..823779607ee 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/matrix_diag_part_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/matrix_diag_part_gpu_kernel.h @@ -66,6 +66,7 @@ class MatrixDiagPartGpuKernelMod : public NativeGpuKernelMod { InitSizeLists(); alignment_ = GetAlignments(common::AnfAlgo::GetNodeAttr(kernel_node, kAlignment)); kernel_node_ = kernel_node; + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/unique_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/unique_gpu_kernel.h index 323034636e1..0416d9825e7 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/unique_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/arrays/unique_gpu_kernel.h @@ -61,6 +61,7 @@ class UniqueGpuKernelMod : public NativeGpuKernelMod { input_shapes.emplace_back(shape); helper_ptr_->CalMemSize(input_shapes, output_shapes); InitSizeLists(); + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/data/dataset_iterator_kernel.cc b/mindspore/ccsrc/plugin/device/gpu/kernel/data/dataset_iterator_kernel.cc index 31e16003110..cd5e4b92a21 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/data/dataset_iterator_kernel.cc +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/data/dataset_iterator_kernel.cc @@ -62,6 +62,8 @@ bool DatasetIteratorKernelMod::Init(const CNodePtr &kernel_node) { output_size_list_.push_back(bytes); } + is_need_updateop_ = true; + #ifndef ENABLE_SECURITY auto profiler_inst = profiler::gpu::GPUProfiler::GetInstance(); MS_EXCEPTION_IF_NULL(profiler_inst); diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcast_grad_args_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcast_grad_args_gpu_kernel.h index fb6b81ae392..8f97c414840 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcast_grad_args_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcast_grad_args_gpu_kernel.h @@ -78,6 +78,7 @@ class DynamicBroadcastGradientArgsGpuKernelMod : public NativeGpuKernelMod { input_size_list_.push_back(s1_size); output_size_list_.push_back(r0_shape[0] * sizeof(S)); output_size_list_.push_back(r1_shape[0] * sizeof(S)); + is_need_updateop_ = true; return true; } void ResetResource() noexcept override { diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcastto_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcastto_gpu_kernel.h index 3148ca14b4c..2f77c99f8a5 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcastto_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_broadcastto_gpu_kernel.h @@ -89,6 +89,7 @@ class DynamicBroadcastToGpuKernelMod : public NativeGpuKernelMod { } InitSizeLists(); + is_need_updateop_ = true; return true; } void ResetResource() noexcept override { diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_reshape_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_reshape_gpu_kernel.h index 1fd6e41b0bd..78e6aa733d2 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_reshape_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_reshape_gpu_kernel.h @@ -68,7 +68,7 @@ class DynamicReshapeKernelMod : public NativeGpuKernelMod { size_t output_size = std::accumulate(output_shape.begin(), output_shape.end(), data_type_size_, std::multiplies()); output_size_list_.push_back(output_size); - + is_need_updateop_ = true; return true; } void ResetResource() noexcept override { diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_stitch_gpu_kernel.cc b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_stitch_gpu_kernel.cc index b817753f37d..3fef0ad2d2d 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_stitch_gpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/other/dynamic_stitch_gpu_kernel.cc @@ -56,6 +56,7 @@ bool DynamicStitchKernelMod::Init(const CNodePtr &kernel_node) { workspace_size_list_.push_back(index_type_size); // One output output_size_list_.push_back(output_size); + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/other/gpu_convert_to_dynamic_shape_gpu_kernel.h b/mindspore/ccsrc/plugin/device/gpu/kernel/other/gpu_convert_to_dynamic_shape_gpu_kernel.h index cad07195596..546d3131c78 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/other/gpu_convert_to_dynamic_shape_gpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/other/gpu_convert_to_dynamic_shape_gpu_kernel.h @@ -77,7 +77,7 @@ class GpuConvertToDynamicShapeGpuKernelMod : public NativeGpuKernelMod { } InitSizeLists(); - + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/plugin/device/gpu/kernel/rl/tensor_array_stack_kernel.cc b/mindspore/ccsrc/plugin/device/gpu/kernel/rl/tensor_array_stack_kernel.cc index 2f53986b976..b455f248c0d 100644 --- a/mindspore/ccsrc/plugin/device/gpu/kernel/rl/tensor_array_stack_kernel.cc +++ b/mindspore/ccsrc/plugin/device/gpu/kernel/rl/tensor_array_stack_kernel.cc @@ -55,6 +55,7 @@ bool TensorArrayStackKernelMod::Init(const CNodePtr &kernel_node) { value_size_ = ele_size_ * LongToSize(size); } InitSizeLists(); + is_need_updateop_ = true; return true; } diff --git a/mindspore/ccsrc/runtime/graph_scheduler/graph_compiler.cc b/mindspore/ccsrc/runtime/graph_scheduler/graph_compiler.cc index e926cd41595..bad1061cf73 100644 --- a/mindspore/ccsrc/runtime/graph_scheduler/graph_compiler.cc +++ b/mindspore/ccsrc/runtime/graph_scheduler/graph_compiler.cc @@ -556,6 +556,11 @@ GraphId GraphCompiler::CompileGraph(const session::OpRunInfo &op_run_info, bool UpdateRefCountForGraphOutput(outputs_with_index); AnfAlgo::UpdateGraphValidRefPair(graph); + + const std::vector &kernels = graph->execution_order(); + for (const auto &kernel : kernels) { + common::AnfAlgo::SetNodeAttr(kAttrSingleOpCompile, MakeValue(true), kernel); + } return graph->graph_id(); } diff --git a/mindspore/core/utils/anf_utils.cc b/mindspore/core/utils/anf_utils.cc index 90c659dc50d..877b51b934b 100644 --- a/mindspore/core/utils/anf_utils.cc +++ b/mindspore/core/utils/anf_utils.cc @@ -56,8 +56,8 @@ class AbstractMutexManager { struct CustomActorInfo { CustomActorInfo(const AnfUtils::CustomActorCallback &func, const std::string &type_name, const CNodePtr &cnode, - bool is_fake = false, bool is_just_sync = false) - : actor_func(func), type_name(type_name), base_cnode_ptr_(cnode), is_fake(is_fake), is_just_sync(is_just_sync) {} + bool is_fake = false) + : actor_func(func), type_name(type_name), base_cnode_ptr_(cnode), is_fake(is_fake) {} ~CustomActorInfo() = default; // Key for user data. @@ -65,8 +65,7 @@ struct CustomActorInfo { AnfUtils::CustomActorCallback actor_func = {}; std::string type_name; CNodeWeakPtr base_cnode_ptr_; - bool is_fake{false}; // For infer - bool is_just_sync{false}; // For update + bool is_fake{false}; // For infer }; using CustomActorInfoPtr = std::shared_ptr; @@ -451,15 +450,13 @@ bool AnfUtils::IsCutomActorNodeSame(const AnfNodePtr &node1, const AnfNodePtr &n MS_EXCEPTION_IF_NULL(actor_info1); std::string actor_type1 = actor_info1->type_name; bool is_fake1 = actor_info1->is_fake; - bool is_just_sync1 = actor_info1->is_just_sync; auto actor_info2 = node2->user_data(); MS_EXCEPTION_IF_NULL(actor_info2); std::string actor_type2 = actor_info2->type_name; bool is_fake2 = actor_info2->is_fake; - bool is_just_sync2 = actor_info2->is_just_sync; - return (actor_type1 == actor_type2) && (is_fake1 == is_fake2) && (is_just_sync1 == is_just_sync2); + return (actor_type1 == actor_type2) && (is_fake1 == is_fake2); } std::string AnfUtils::GetCustomActorType(const AnfNodePtr &node) { @@ -498,16 +495,6 @@ CNodePtr AnfUtils::GetCustomActorBaseNode(const AnfNodePtr &node) { return actor_info->base_cnode_ptr_.lock(); } -bool AnfUtils::GetCustomActorJustSyncFlag(const AnfNodePtr &node) { - if (!IsCustomActorNode(node)) { - MS_LOG(EXCEPTION) << node->fullname_with_scope() << " is not a custom actor node!"; - } - - auto update_info = node->user_data(); - MS_EXCEPTION_IF_NULL(update_info); - return update_info->is_just_sync; -} - AnfUtils::CustomActorCallback AnfUtils::GetCustomFunc(const AnfNodePtr &node) { MS_EXCEPTION_IF_NULL(node); if (!IsCustomActorNode(node)) { @@ -519,9 +506,9 @@ AnfUtils::CustomActorCallback AnfUtils::GetCustomFunc(const AnfNodePtr &node) { return actor_info->actor_func; } -AnfNodePtr AnfUtils::NewInitActorNode(AnfUtils::CustomActorCallback f, const CNodePtr &base_cnode) { +AnfNodePtr AnfUtils::NewInitActorNode(AnfUtils::CustomActorCallback f, const CNodePtr &base_cnode, bool is_fake) { MS_EXCEPTION_IF_NULL(base_cnode); - auto actor_info = std::make_shared(f, kInit, base_cnode); + auto actor_info = std::make_shared(f, kInit, base_cnode, is_fake); return NewCustomActorNode(actor_info, base_cnode->func_graph()); } @@ -531,10 +518,9 @@ AnfNodePtr AnfUtils::NewInferActorNode(AnfUtils::CustomActorCallback f, const CN return NewCustomActorNode(actor_info, base_cnode->func_graph()); } -AnfNodePtr AnfUtils::NewUpdateActorNode(AnfUtils::CustomActorCallback f, const CNodePtr &base_cnode, - bool is_just_sync) { +AnfNodePtr AnfUtils::NewUpdateActorNode(AnfUtils::CustomActorCallback f, const CNodePtr &base_cnode) { MS_EXCEPTION_IF_NULL(base_cnode); - auto actor_info = std::make_shared(f, kUpdate, base_cnode, false, is_just_sync); + auto actor_info = std::make_shared(f, kUpdate, base_cnode, false); return NewCustomActorNode(actor_info, base_cnode->func_graph()); } } // namespace mindspore diff --git a/mindspore/core/utils/anf_utils.h b/mindspore/core/utils/anf_utils.h index e611f59c06e..bccf0c348bc 100644 --- a/mindspore/core/utils/anf_utils.h +++ b/mindspore/core/utils/anf_utils.h @@ -97,16 +97,15 @@ class MS_CORE_API AnfUtils { // Custom actor node is for dynamic shape. // Generate a Init custom actor node. - static AnfNodePtr NewInitActorNode(CustomActorCallback f, const CNodePtr &base_cnode); + static AnfNodePtr NewInitActorNode(CustomActorCallback f, const CNodePtr &base_cnode, bool is_fake); // Generate a Infer custom actor node. If `is_fake` is set to true, this node is a fake node without any infer action. static AnfNodePtr NewInferActorNode(CustomActorCallback f, const CNodePtr &base_cnode, bool is_fake); // Generate a Update custom actor node. If `is_just_sync` is set to true, this node is just for a stream-sync call. - static AnfNodePtr NewUpdateActorNode(CustomActorCallback f, const CNodePtr &base_cnode, bool is_just_sync); + static AnfNodePtr NewUpdateActorNode(CustomActorCallback f, const CNodePtr &base_cnode); static bool IsCustomActorNode(const AnfNodePtr &node); static std::string GetCustomActorType(const AnfNodePtr &node); static std::string GetCustomActorName(const AnfNodePtr &node); static CNodePtr GetCustomActorBaseNode(const AnfNodePtr &node); - static bool GetCustomActorJustSyncFlag(const AnfNodePtr &node); static CustomActorCallback GetCustomFunc(const AnfNodePtr &node); static bool IsCutomActorNodeSame(const AnfNodePtr &node1, const AnfNodePtr &node2); }; diff --git a/tests/st/ops/graph_kernel/custom/test_custom_akg.py b/tests/st/ops/graph_kernel/custom/test_custom_akg.py index 1c0c9f4d35a..c64456b4f86 100644 --- a/tests/st/ops/graph_kernel/custom/test_custom_akg.py +++ b/tests/st/ops/graph_kernel/custom/test_custom_akg.py @@ -333,7 +333,6 @@ def irbuilder_case(): raise ValueError("Precision error, compare result: {}".format(compare_res)) -@pytest.mark.level0 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.env_onecard @@ -347,7 +346,6 @@ def test_irbuilder_ascend_graph_mode(): irbuilder_case() -@pytest.mark.level0 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.env_onecard diff --git a/tests/ut/cpp/pre_activate/ascend/dynamic_shape/dynamic_shape_pass_test.cc b/tests/ut/cpp/pre_activate/ascend/dynamic_shape/dynamic_shape_pass_test.cc index fa6ea52fc99..ea48def03e7 100644 --- a/tests/ut/cpp/pre_activate/ascend/dynamic_shape/dynamic_shape_pass_test.cc +++ b/tests/ut/cpp/pre_activate/ascend/dynamic_shape/dynamic_shape_pass_test.cc @@ -14,10 +14,12 @@ * limitations under the License. */ #include +#include "utils/ms_context.h" #include "backend/common/session/anf_runtime_algorithm.h" -#include "plugin/device/ascend/optimizer/dynamic_shape/ascend_dynamic_shape_helper.h" -#include "plugin/device/ascend/optimizer/ascend_backend_optimization.h" +#include "backend/common/optimizer/common_backend_optimization.h" +#include "backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h" #include "plugin/device/ascend/kernel/tbe/tbe_kernel_mod.h" +#include "include/common/utils/anfalgo.h" #include "common/backend_common_test.h" #include "include/common/debug/anf_ir_dump.h" #include "include/common/debug/dump_proto.h" @@ -52,9 +54,16 @@ CNodePtr TestCreateCNode(const KernelGraphPtr &g, const std::string &prim_name, if (cnode == nullptr) { MS_LOG(ERROR) << "Cannot create cnode!"; } + if (prim_name != "TupleGetItem" && prim_name != "MakeTuple") { + common::AnfAlgo::SetNodeAttr(kAttrInputIsDynamicShape, MakeValue(true), cnode); + } + cnode->set_abstract(abstract); auto cnode_kernel_info = std::make_shared(); - cnode_kernel_info->set_kernel_mod(std::make_shared(std::make_shared())); + auto anf_node = cnode->cast(); + MS_EXCEPTION_IF_NULL(anf_node); + cnode_kernel_info->set_kernel_mod( + std::make_shared(std::make_shared(), anf_node)); cnode->set_kernel_info(cnode_kernel_info); return cnode; } @@ -128,6 +137,9 @@ class TestDynamicShapePass : public BackendCommon { /// \ / \ / \ | /// depend depend depend TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_0) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -140,7 +152,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_0) { before_fg->set_output(before_a_node); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -174,6 +186,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_0) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape @@ -186,6 +199,9 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_0) { /// \ / \ / /// depend depend TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_1) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -196,7 +212,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_1) { before_fg->set_output(before_a_node); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -205,7 +221,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_1) { auto after_a_node = TestCreateCNode(after_fg, "A", AnfNodePtrList{after_p}, TestCreateTensor(kFloat32, std::vector{1, 10})); - auto infer_a = dynamic_shape::GenInferNode(after_a_node, true); + auto infer_a = dynamic_shape::GenInferNode(after_a_node); auto init_a = dynamic_shape::GenInitNode(after_a_node); auto depend0 = TestCreateDepend(after_fg, AnfNodePtrList{init_a, infer_a}); @@ -219,6 +235,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_1) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape @@ -245,6 +262,9 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_1) { /// \ / Depend /// MakeTuple TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_2) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -265,7 +285,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_2) { before_fg->set_output(before_make_tuple); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -307,12 +327,16 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_2) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape /// Description: Complecate case case. /// Expectation: Graph as expected. TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -345,7 +369,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { before_fg->set_output(before_make_tuple); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -377,7 +401,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { auto after_make_tuple = TestCreateMakeTuple(after_fg, AnfNodePtrList{after_c, after_e}); - auto infer_a = dynamic_shape::GenInferNode(after_a, true); + auto infer_a = dynamic_shape::GenInferNode(after_a); auto init_a = dynamic_shape::GenInitNode(after_a); auto infer_tuple = dynamic_shape::GenInferNode(after_tuple); @@ -397,7 +421,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { auto init_de = dynamic_shape::GenInitNode(after_dync_end); auto update_de = dynamic_shape::GenUpdateNode(after_dync_end); - auto infer_e = dynamic_shape::GenInferNode(after_e, true); + auto infer_e = dynamic_shape::GenInferNode(after_e); auto init_e = dynamic_shape::GenInitNode(after_e); auto depend0 = TestCreateDepend(after_fg, AnfNodePtrList{init_a, infer_a}); @@ -444,6 +468,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape @@ -464,6 +489,9 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_3) { /// \ / \ / \ / \ | /// depend depend depend depend TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_depend) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -479,7 +507,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_depend) { before_fg->set_output(before_depend_node); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -522,6 +550,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_depend) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape @@ -547,6 +576,9 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_depend) { /// | / /// depend TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_monad) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -569,7 +601,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_monad) { before_fg->set_output(before_depend_node); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -612,12 +644,16 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_with_monad) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } /// Feature: Dynamic shape /// Description: Need sync case(contain op such as Tile...). /// Expectation: Graph as expected. TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_sync) { + auto context = MsContext::GetInstance(); + MS_EXCEPTION_IF_NULL(context); + context->set_param(MS_CTX_DEVICE_TARGET, kAscendDevice); // construct before graph auto before_fg = std::make_shared(); ASSERT_TRUE(before_fg != nullptr); @@ -641,7 +677,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_sync) { before_fg->set_output(before_add_node); // run pass - AscendDynamicShapeConvert(before_fg); + DynamicShapeConvertPass(before_fg); // construct after graph auto after_fg = std::make_shared(); @@ -669,17 +705,17 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_sync) { auto infer_tile1 = dynamic_shape::GenInferNode(after_tile1_node); auto init_tile1 = dynamic_shape::GenInitNode(after_tile1_node); - auto infer_a = dynamic_shape::GenInferNode(after_a_node, true); + auto infer_a = dynamic_shape::GenInferNode(after_a_node); auto init_a = dynamic_shape::GenInitNode(after_a_node); - auto infer_b = dynamic_shape::GenInferNode(after_b_node, true); + auto infer_b = dynamic_shape::GenInferNode(after_b_node); auto init_b = dynamic_shape::GenInitNode(after_b_node); - auto update_b = dynamic_shape::GenUpdateNode(after_b_node, true); + auto update_b = dynamic_shape::GenUpdateNode(after_b_node); - auto infer_tile2 = dynamic_shape::GenInferNode(after_tile2_node, true); + auto infer_tile2 = dynamic_shape::GenInferNode(after_tile2_node); auto init_tile2 = dynamic_shape::GenInitNode(after_tile2_node); - auto infer_add = dynamic_shape::GenInferNode(after_add_node, true); + auto infer_add = dynamic_shape::GenInferNode(after_add_node); auto init_add = dynamic_shape::GenInitNode(after_add_node); auto depend0 = TestCreateDepend(after_fg, AnfNodePtrList{init_uniq, infer_uniq}); @@ -721,6 +757,7 @@ TEST_F(TestDynamicShapePass, test_dynamic_shape_pass_sync) { // assert EXPECT_TRUE(CheckEqualGraph(after_fg, before_fg)); + context->set_param(MS_CTX_DEVICE_TARGET, kCPUDevice); } } // namespace opt } // namespace mindspore