Merge pull request !29940 from chenlei_autodiff/gk_litefeature/build-system-rewrite
| @@ -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. | |||
| @@ -13,8 +13,10 @@ | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include <vector> | |||
| #include "utils/anf_utils.h" | |||
| #include "utils/utils.h" | |||
| #include "utils/log_adapter.h" | |||
| #include "base/core_ops.h" | |||
| @@ -120,5 +122,54 @@ bool ConstInputToAttrInfoRegistry::GetRegisterByOpName(const std::string &op_nam | |||
| } | |||
| return false; | |||
| } | |||
| void ConstInputToAttr(const CNodePtr &cnode, const mindspore::HashSet<size_t> &input_attrs) { | |||
| MS_EXCEPTION_IF_NULL(cnode); | |||
| std::vector<AnfNodePtr> new_inputs; | |||
| auto primitive = GetCNodePrimitive(cnode); | |||
| MS_EXCEPTION_IF_NULL(primitive); | |||
| primitive = primitive->Clone(); | |||
| auto input_names = primitive->GetAttr(kAttrInputNames); | |||
| if (input_names == nullptr) { | |||
| MS_LOG(DEBUG) << "input_names are nullptr in cnode[" + cnode->DebugString() + "]"; | |||
| return; | |||
| } | |||
| auto input_names_vec = GetValue<std::vector<std::string>>(input_names); | |||
| auto inputs = cnode->inputs(); | |||
| new_inputs.push_back(inputs[0]); | |||
| bool need_update = false; | |||
| for (size_t i = 0; i < inputs.size() - 1; ++i) { | |||
| auto input_node = inputs[i + 1]; | |||
| MS_EXCEPTION_IF_NULL(input_node); | |||
| if (IsPrimitiveCNode(input_node, prim::kPrimDepend)) { | |||
| input_node = AnfUtils::VisitKernel(input_node, 0).first; | |||
| } | |||
| if (input_attrs.find(i) != input_attrs.end() && input_node->isa<ValueNode>() && !HasAbstractMonad(input_node)) { | |||
| auto value_node = input_node->cast<ValueNodePtr>(); | |||
| MS_EXCEPTION_IF_NULL(value_node); | |||
| MS_LOG(DEBUG) << "start erase input[" << i << "] of cnode[" + cnode->DebugString() + "]"; | |||
| if (i >= input_names_vec.size()) { | |||
| MS_LOG(EXCEPTION) << "Index " << i << " is larger than input names size [" << input_names_vec.size() << "]"; | |||
| } | |||
| auto value = value_node->value(); | |||
| if (value->isa<tensor::Tensor>()) { | |||
| auto tensor = value->cast<tensor::TensorPtr>(); | |||
| if (tensor->data().const_data() == nullptr) { | |||
| need_update = false; | |||
| break; | |||
| } | |||
| } | |||
| primitive->set_attr(input_names_vec[i], value); | |||
| need_update = true; | |||
| } else { | |||
| new_inputs.push_back(inputs[i + 1]); | |||
| } | |||
| } | |||
| if (need_update) { | |||
| // Update cnode's inputs | |||
| new_inputs[0] = NewValueNode(primitive); | |||
| cnode->set_inputs(new_inputs); | |||
| } | |||
| } | |||
| } // namespace opt | |||
| } // namespace mindspore | |||
| @@ -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. | |||
| @@ -13,10 +13,11 @@ | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_REGISTRY_H_ | |||
| #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_REGISTRY_H_ | |||
| #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ | |||
| #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ | |||
| #include <string> | |||
| #include "ir/anf.h" | |||
| #include "utils/hash_map.h" | |||
| #include "utils/hash_set.h" | |||
| #include "utils/ms_utils.h" | |||
| @@ -66,6 +67,8 @@ struct ConstInputToAttrInfoReceiver { | |||
| ConstInputToAttrInfoRegistry::Instance().Register(reg); | |||
| } | |||
| }; | |||
| void ConstInputToAttr(const CNodePtr &cnode, const mindspore::HashSet<size_t> &input_attrs); | |||
| } // namespace opt | |||
| #define REG_CONST_INPUT_TO_ATTR(op_name) REG_CONST_INPUT_TO_ATTR_UNIQ_HELPER(__COUNTER__, op_name) | |||
| @@ -75,4 +78,4 @@ struct ConstInputToAttrInfoReceiver { | |||
| ::mindspore::opt::ConstInputToAttrInfoRegister(op_name) | |||
| } // namespace mindspore | |||
| #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_REGISTRY_H_ | |||
| #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_CONST_INPUT_TO_ATTR_H_ | |||
| @@ -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. | |||
| @@ -33,7 +33,7 @@ | |||
| #include "runtime/device/kernel_info.h" | |||
| #include "utils/ms_context.h" | |||
| #include "utils/trace_base.h" | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "abstract/primitive_infer_map.h" | |||
| namespace mindspore { | |||
| @@ -591,55 +591,6 @@ ValueNodePtr CreateShapeValueNode(const FuncGraphPtr &func_graph, const std::vec | |||
| return shape_value_node; | |||
| } | |||
| void ConstInputToAttr(const CNodePtr &cnode, const mindspore::HashSet<size_t> &input_attrs) { | |||
| MS_EXCEPTION_IF_NULL(cnode); | |||
| std::vector<AnfNodePtr> new_inputs; | |||
| auto primitive = AnfAlgo::GetCNodePrimitive(cnode); | |||
| MS_EXCEPTION_IF_NULL(primitive); | |||
| primitive = primitive->Clone(); | |||
| auto input_names = primitive->GetAttr(kAttrInputNames); | |||
| if (input_names == nullptr) { | |||
| MS_LOG(DEBUG) << "input_names are nullptr in cnode[" + cnode->DebugString() + "]"; | |||
| return; | |||
| } | |||
| auto input_names_vec = GetValue<std::vector<std::string>>(input_names); | |||
| auto inputs = cnode->inputs(); | |||
| new_inputs.push_back(inputs[0]); | |||
| bool need_update = false; | |||
| for (size_t i = 0; i < inputs.size() - 1; ++i) { | |||
| auto input_node = inputs[i + 1]; | |||
| if (AnfAlgo::CheckPrimitiveType(input_node, prim::kPrimDepend)) { | |||
| input_node = AnfAlgo::VisitKernel(input_node, 0).first; | |||
| } | |||
| MS_EXCEPTION_IF_NULL(input_node); | |||
| if (input_attrs.find(i) != input_attrs.end() && input_node->isa<ValueNode>() && !HasAbstractMonad(input_node)) { | |||
| auto value_node = input_node->cast<ValueNodePtr>(); | |||
| MS_EXCEPTION_IF_NULL(value_node); | |||
| MS_LOG(DEBUG) << "start erase input[" << i << "] of cnode[" + cnode->DebugString() + "]"; | |||
| if (i >= input_names_vec.size()) { | |||
| MS_LOG(EXCEPTION) << "Index " << i << " is larger than input names size [" << input_names_vec.size() << "]"; | |||
| } | |||
| auto value = value_node->value(); | |||
| if (value->isa<tensor::Tensor>()) { | |||
| auto tensor = value->cast<tensor::TensorPtr>(); | |||
| if (tensor->data().const_data() == nullptr) { | |||
| need_update = false; | |||
| break; | |||
| } | |||
| } | |||
| primitive->set_attr(input_names_vec[i], value); | |||
| need_update = true; | |||
| } else { | |||
| new_inputs.push_back(inputs[i + 1]); | |||
| } | |||
| } | |||
| if (need_update) { | |||
| // Update cnode's inputs | |||
| new_inputs[0] = NewValueNode(primitive); | |||
| cnode->set_inputs(new_inputs); | |||
| } | |||
| } | |||
| bool AnfEqual(const BaseRef &a, const BaseRef &b) { | |||
| if (utils::isa<AnfNodePtr>(a) && utils::isa<AnfNodePtr>(b)) { | |||
| auto a_node = utils::cast<AnfNodePtr>(a); | |||
| @@ -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. | |||
| @@ -197,8 +197,6 @@ std::shared_ptr<std::vector<std::pair<AnfNodePtr, int>>> GetRealNodeUsedListByOu | |||
| size_t output_index); | |||
| bool IsNotRealUsedByOthers(const FuncGraphPtr &graph, const AnfNodePtr &node); | |||
| void ConstInputToAttr(const CNodePtr &cnode, const mindspore::HashSet<size_t> &input_attrs); | |||
| bool AnfEqual(const BaseRef &a, const BaseRef &b); | |||
| bool CNodeTypeEqual(const BaseRef &a, const BaseRef &b); | |||
| @@ -1,5 +1,5 @@ | |||
| /** | |||
| * Copyright 2020 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. | |||
| @@ -22,7 +22,7 @@ | |||
| #include "utils/ms_context.h" | |||
| #include "utils/utils.h" | |||
| #include "abstract/abstract_value.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| namespace mindspore { | |||
| namespace opt { | |||
| @@ -1,5 +1,5 @@ | |||
| /** | |||
| * Copyright 2020 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. | |||
| @@ -14,8 +14,7 @@ | |||
| * limitations under the License. | |||
| */ | |||
| #include "backend/common/pass/convert_const_input_to_attr.h" | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "utils/utils.h" | |||
| #include "utils/ms_context.h" | |||
| #include "base/core_ops.h" | |||
| @@ -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. | |||
| @@ -18,7 +18,7 @@ | |||
| #include <memory> | |||
| #include "utils/hash_set.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "backend/common/session/anf_runtime_algorithm.h" | |||
| namespace mindspore { | |||
| @@ -27,7 +27,6 @@ | |||
| #include "kernel/akg/akg_kernel_json_decoder.h" | |||
| #include "kernel/kernel.h" | |||
| #include "backend/common/session/anf_runtime_algorithm.h" | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "common/graph_kernel/adapter/fake_abstract_shape.h" | |||
| #include "common/graph_kernel/core/graph_builder.h" | |||
| #include "ir/func_graph_cloner.h" | |||
| @@ -0,0 +1,40 @@ | |||
| /** | |||
| * 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 "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "utils/anf_utils.h" | |||
| namespace mindspore::graphkernel { | |||
| bool ConvertConstInputToAttr::Run(const FuncGraphPtr &func_graph) { | |||
| bool changed = false; | |||
| auto nodes = TopoSort(func_graph->get_return()); | |||
| for (auto node : nodes) { | |||
| if (node == nullptr || !AnfUtils::IsRealCNodeKernel(node)) { | |||
| continue; | |||
| } | |||
| auto cnode = node->cast<CNodePtr>(); | |||
| MS_EXCEPTION_IF_NULL(cnode); | |||
| opt::ConstInputToAttrInfoRegister reg; | |||
| if (!opt::ConstInputToAttrInfoRegistry::Instance().GetRegisterByOpName(AnfUtils::GetCNodeName(cnode), ®)) { | |||
| continue; | |||
| } | |||
| changed = true; | |||
| opt::ConstInputToAttr(cnode, reg.GetConstInputAttrInfo()); | |||
| } | |||
| return changed; | |||
| } | |||
| } // namespace mindspore::graphkernel | |||
| @@ -0,0 +1,30 @@ | |||
| /** | |||
| * 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_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ | |||
| #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ | |||
| #include "ir/func_graph.h" | |||
| #include "backend/common/optimizer/pass.h" | |||
| namespace mindspore::graphkernel { | |||
| class ConvertConstInputToAttr : public opt::Pass { | |||
| public: | |||
| ConvertConstInputToAttr() : Pass("convert_const_input_to_attr") {} | |||
| ~ConvertConstInputToAttr() override = default; | |||
| bool Run(const FuncGraphPtr &func_graph) override; | |||
| }; | |||
| } // namespace mindspore::graphkernel | |||
| #endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ | |||
| @@ -28,12 +28,19 @@ | |||
| #include "common/graph_kernel/core/eliminate_redundant_output.h" | |||
| #include "common/graph_kernel/core/shape_ops_splitter.h" | |||
| #include "common/graph_kernel/core/update_state_formatter.h" | |||
| #include "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h" | |||
| #include "common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h" | |||
| namespace mindspore::graphkernel { | |||
| using opt::GetitemTuple; | |||
| using opt::GraphOptimizer; | |||
| PassManagerPtr GraphKernelOptimizer::PreProcess() const { | |||
| auto pm = std::make_shared<GraphKernelPassManager>(0, "preprocess"); | |||
| pm->AddPass(std::make_shared<ConvertConstInputToAttr>(), OptLevel_1); | |||
| return pm; | |||
| } | |||
| PassManagerPtr GraphKernelOptimizer::Cluster() const { | |||
| auto pm = std::make_shared<GraphKernelPassManager>(0, "cluster"); | |||
| // Expand complex basic kernels to composite kernels | |||
| @@ -71,6 +78,7 @@ PassManagerPtr GraphKernelOptimizer::Split() const { | |||
| void GraphKernelOptimizer::Run(const FuncGraphPtr &kernel_graph) { | |||
| auto optimizer = std::make_shared<GraphOptimizer>("graph_kernel_optimizer"); | |||
| optimizer->AddPassManager(PreProcess()); | |||
| optimizer->AddPassManager(Cluster()); | |||
| optimizer->AddPassManager(Split()); | |||
| @@ -28,6 +28,8 @@ class GraphKernelOptimizer { | |||
| void Run(const FuncGraphPtr &kernel_graph); | |||
| private: | |||
| // before graph_kernel | |||
| PassManagerPtr PreProcess() const; | |||
| // Cluster kernels | |||
| PassManagerPtr Cluster() const; | |||
| // Split kernels | |||
| @@ -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. | |||
| @@ -56,7 +56,7 @@ | |||
| #include "pipeline/jit/resource.h" | |||
| #include "pipeline/pynative/base.h" | |||
| #include "backend/common/session/session_factory.h" | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "runtime/hardware/device_context_manager.h" | |||
| #include "backend/graph_compiler/transform.h" | |||
| @@ -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. | |||
| @@ -19,7 +19,7 @@ | |||
| #include <memory> | |||
| #include <set> | |||
| #include "utils/hash_set.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "kernel/kernel_build_info.h" | |||
| #include "utils/utils.h" | |||
| #include "backend/common/session/kernel_graph.h" | |||
| @@ -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. | |||
| @@ -17,6 +17,7 @@ | |||
| #include "common/backend_common_test.h" | |||
| #include "common/py_func_graph_fetcher.h" | |||
| #include "runtime/device/kernel_info.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "backend/common/pass/convert_const_input_to_attr.h" | |||
| #include "debug/anf_ir_dump.h" | |||
| #include "backend/common/session/anf_runtime_algorithm.h" | |||
| @@ -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. | |||
| @@ -20,7 +20,7 @@ | |||
| #include "utils/utils.h" | |||
| #include "abstract/abstract_value.h" | |||
| #include "abstract/primitive_infer_map.h" | |||
| #include "backend/common/optimizer/const_input_to_attr_registry.h" | |||
| #include "backend/common/optimizer/const_input_to_attr.h" | |||
| #include "backend/common/optimizer/helper.h" | |||
| #include "common/common_test.h" | |||
| namespace mindspore { | |||