1 /** 2 * Copyright 2022-2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "backend/common/optimizer/dynamic_shape/convert_custom_op.h" 18 19 #include <memory> 20 #include "include/backend/anf_runtime_algorithm.h" 21 #include "include/common/utils/anfalgo.h" 22 #include "include/backend/optimizer/helper.h" 23 #include "backend/common/optimizer/dynamic_shape/dynamic_shape_helper.h" 24 #include "utils/ms_context.h" 25 #include "utils/anf_utils.h" 26 27 namespace mindspore { 28 namespace opt::dynamic_shape { Run(const FuncGraphPtr & func_graph)29bool ConvertCustomOp::Run(const FuncGraphPtr &func_graph) { 30 MS_EXCEPTION_IF_NULL(func_graph); 31 const auto &node_list = TopoSort(func_graph->get_return()); 32 for (const auto &node : node_list) { 33 if (!IsRealCNode(node)) { 34 continue; 35 } 36 ConvertCustomOpForNode(node); 37 } 38 return true; 39 } 40 ConvertCustomOpForNode(const AnfNodePtr & node) const41void ConvertCustomOp::ConvertCustomOpForNode(const AnfNodePtr &node) const { 42 MS_EXCEPTION_IF_NULL(node); 43 bool is_dynamic_node = common::AnfAlgo::IsDynamicShape(node); 44 AnfNodePtr infer_node = nullptr; 45 AnfNodePtr init_node = nullptr; 46 if (is_dynamic_node) { 47 infer_node = GenInferNode(node); 48 init_node = GenInitNode(node); 49 AnfUtils::SetCustomInfoToBaseNode(node, infer_node, init_node); 50 } 51 52 RelatedCustomActorNode custom_nodes = {infer_node, init_node}; 53 CustomActorNodeManager::Instance().Register(node, custom_nodes); 54 } 55 } // namespace opt::dynamic_shape 56 } // namespace mindspore 57