From 6d14de5f210149a218bf01a774b9bb1518ae380e Mon Sep 17 00:00:00 2001 From: Margaret_wangrui Date: Thu, 9 Jul 2020 16:51:18 +0800 Subject: [PATCH] handle switch input to partial --- mindspore/ccsrc/session/session_basic.cc | 48 +++++++++++++++++++++++- mindspore/ccsrc/session/session_basic.h | 4 ++ mindspore/ccsrc/utils/utils.h | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/mindspore/ccsrc/session/session_basic.cc b/mindspore/ccsrc/session/session_basic.cc index 91e430182..728306d20 100644 --- a/mindspore/ccsrc/session/session_basic.cc +++ b/mindspore/ccsrc/session/session_basic.cc @@ -501,7 +501,50 @@ CNodePtr SessionBasic::CreateNewCNode(const CNodePtr &cnode, bool valid_input, K return new_cnode; } -static std::vector CreateSwitchOrPartialNode(const CNodePtr &cnode, KernelGraph *graph) { +CNodePtr SessionBasic::CreateSwitchInput(const AnfNodePtr &node_input, KernelGraph *graph) { + MS_EXCEPTION_IF_NULL(node_input); + MS_EXCEPTION_IF_NULL(graph); + // switch input generalizes partial + if (AnfAlgo::CheckPrimitiveType(node_input, prim::kPrimPartial) || + AnfAlgo::CheckPrimitiveType(node_input, prim::kPrimCall)) { + return node_input->cast(); + } + if (node_input->isa()) { + MS_LOG(EXCEPTION) << "If switch input is " << node_input->DebugString() << ", it mast be partial or call."; + } + std::vector partial_inputs = {NewValueNode(std::make_shared(prim::kPrimPartial->name()))}; + if (node_input->isa() && IsValueNode(node_input)) { + partial_inputs.emplace_back(node_input); + auto partial_node = graph->NewCNode(partial_inputs); + return partial_node; + } + KernelGraphPtr kernel_graph = NewKernelGraph(); + MS_EXCEPTION_IF_NULL(kernel_graph); + kernel_graph->set_output(graph->GetBackendAnfByFrontAnf(node_input)); + partial_inputs.emplace_back(std::make_shared(kernel_graph)); + auto partial_node = graph->NewCNode(partial_inputs); + return partial_node; +} + +CNodePtr SessionBasic::HandleSwitchInputs(const AnfNodePtr &anf_node, KernelGraph *graph) { + MS_EXCEPTION_IF_NULL(anf_node); + MS_EXCEPTION_IF_NULL(graph); + auto node = anf_node->cast(); + MS_EXCEPTION_IF_NULL(node); + if (node->inputs().size() < kSwitchInputSize) { + MS_LOG(EXCEPTION) << "Switch input size less than " << kSwitchInputSize; + } + auto primitive = NewValueNode(std::make_shared(prim::kPrimSwitch->name())); + std::vector switch_inputs = {primitive, node->input(1)}; + for (size_t index = 2; index < node->inputs().size(); index++) { + auto input = CreateSwitchInput(node->input(index), graph); + switch_inputs.emplace_back(input); + } + auto switch_node = graph->NewCNode(switch_inputs); + return switch_node; +} + +std::vector SessionBasic::CreateSwitchOrPartialNode(const CNodePtr &cnode, KernelGraph *graph) { MS_EXCEPTION_IF_NULL(cnode); MS_EXCEPTION_IF_NULL(graph); // create primitive of cnode:call(partial or switch) @@ -526,7 +569,8 @@ static std::vector CreateSwitchOrPartialNode(const CNodePtr &cnode, }); return cnode_inputs; } else if (AnfAlgo::CheckPrimitiveType(cnode_input, prim::kPrimSwitch)) { - cnode_inputs.emplace_back(cnode_input); + auto switch_node = HandleSwitchInputs(cnode_input, graph); + cnode_inputs.emplace_back(switch_node); return cnode_inputs; } MS_LOG(EXCEPTION) << "CNode input[0] must be partial or switch."; diff --git a/mindspore/ccsrc/session/session_basic.h b/mindspore/ccsrc/session/session_basic.h index cf85dd022..8f8f88e65 100755 --- a/mindspore/ccsrc/session/session_basic.h +++ b/mindspore/ccsrc/session/session_basic.h @@ -87,6 +87,10 @@ class SessionBasic { std::unordered_map *other_graph_cnode); CNodePtr CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph); + CNodePtr CreateSwitchInput(const AnfNodePtr &node_input, KernelGraph *graph); + CNodePtr HandleSwitchInputs(const AnfNodePtr &anf_node, KernelGraph *graph); + std::vector CreateSwitchOrPartialNode(const CNodePtr &cnode, KernelGraph *graph); + // set parameters of final graph virtual GraphId SetFinalGraphInput(const std::vector &) { return kInvalidGraphId; } // set output of final graph diff --git a/mindspore/ccsrc/utils/utils.h b/mindspore/ccsrc/utils/utils.h index d10d5830f..7442fa40e 100644 --- a/mindspore/ccsrc/utils/utils.h +++ b/mindspore/ccsrc/utils/utils.h @@ -246,6 +246,7 @@ constexpr auto kAnfPartialFuncGraphIndex = 1; constexpr auto kRealInputNodeIndexInTupleGetItem = 1; constexpr auto kInputNodeOutputIndexInTupleGetItem = 2; constexpr auto kTupleGetItemInputSize = 3; +constexpr auto kSwitchInputSize = 4; // index define of control depend constexpr auto kControlDependPriorIndex = 1; constexpr auto kControlDependBehindIndex = 2; -- GitLab