Browse Source

!29940 [GraphKernel] decouple convert_const_input_to_attr.

Merge pull request !29940 from chenlei_autodiff/gk_lite
feature/build-system-rewrite
i-robot Gitee 4 years ago
parent
commit
3b84ba1f1d
No known key found for this signature in database GPG Key ID: 173E9B9CA92EEF8F
16 changed files with 157 additions and 75 deletions
  1. +53
    -2
      mindspore/ccsrc/backend/common/optimizer/const_input_to_attr.cc
  2. +7
    -4
      mindspore/ccsrc/backend/common/optimizer/const_input_to_attr.h
  3. +2
    -51
      mindspore/ccsrc/backend/common/optimizer/helper.cc
  4. +1
    -3
      mindspore/ccsrc/backend/common/optimizer/helper.h
  5. +2
    -2
      mindspore/ccsrc/backend/common/pass/const_to_attr_strided_slice_grad.cc
  6. +2
    -3
      mindspore/ccsrc/backend/common/pass/convert_const_input_to_attr.cc
  7. +2
    -2
      mindspore/ccsrc/backend/common/pass/custom_op_const_input_to_attr.cc
  8. +0
    -1
      mindspore/ccsrc/common/graph_kernel/graph_kernel_helper.cc
  9. +40
    -0
      mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.cc
  10. +30
    -0
      mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.h
  11. +8
    -0
      mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc
  12. +2
    -0
      mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.h
  13. +2
    -2
      mindspore/ccsrc/pipeline/pynative/pynative_execute.cc
  14. +2
    -2
      mindspore/ccsrc/plugin/device/ascend/optimizer/ir_fission/topk_split.cc
  15. +2
    -1
      tests/ut/cpp/pre_activate/ascend/ir_fission/topk_split_test.cc
  16. +2
    -2
      tests/ut/cpp/pre_activate/common/restore_abs_input_in_backed_infer_test.cc

mindspore/ccsrc/backend/common/optimizer/const_input_to_attr_registry.cc → mindspore/ccsrc/backend/common/optimizer/const_input_to_attr.cc View File

@@ -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

mindspore/ccsrc/backend/common/optimizer/const_input_to_attr_registry.h → mindspore/ccsrc/backend/common/optimizer/const_input_to_attr.h View File

@@ -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_

+ 2
- 51
mindspore/ccsrc/backend/common/optimizer/helper.cc View File

@@ -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
- 3
mindspore/ccsrc/backend/common/optimizer/helper.h View File

@@ -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);


+ 2
- 2
mindspore/ccsrc/backend/common/pass/const_to_attr_strided_slice_grad.cc View File

@@ -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 {


+ 2
- 3
mindspore/ccsrc/backend/common/pass/convert_const_input_to_attr.cc View File

@@ -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"


+ 2
- 2
mindspore/ccsrc/backend/common/pass/custom_op_const_input_to_attr.cc View File

@@ -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 {


+ 0
- 1
mindspore/ccsrc/common/graph_kernel/graph_kernel_helper.cc View File

@@ -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"


+ 40
- 0
mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.cc View File

@@ -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), &reg)) {
continue;
}
changed = true;
opt::ConstInputToAttr(cnode, reg.GetConstInputAttrInfo());
}
return changed;
}
} // namespace mindspore::graphkernel

+ 30
- 0
mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.h View File

@@ -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_

+ 8
- 0
mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc View File

@@ -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());



+ 2
- 0
mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.h View File

@@ -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


+ 2
- 2
mindspore/ccsrc/pipeline/pynative/pynative_execute.cc View File

@@ -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"


+ 2
- 2
mindspore/ccsrc/plugin/device/ascend/optimizer/ir_fission/topk_split.cc View File

@@ -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"


+ 2
- 1
tests/ut/cpp/pre_activate/ascend/ir_fission/topk_split_test.cc View File

@@ -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"


+ 2
- 2
tests/ut/cpp/pre_activate/common/restore_abs_input_in_backed_infer_test.cc View File

@@ -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 {


Loading…
Cancel
Save