clean code

This commit is contained in:
r1chardf1d0 2022-06-07 17:15:38 +08:00
parent c3bfbf2c3c
commit 3b57154300
14 changed files with 93 additions and 59 deletions

View File

@ -24,9 +24,9 @@
#include "include/common/utils/anfalgo.h"
#include "kernel/common_utils.h"
#include "common/graph_kernel/adapter/fake_abstract_shape.h"
#if ENABLE_D
#ifdef ENABLE_D
#include "plugin/device/ascend/hal/device/kernel_select_ascend.h"
#elif ENABLE_GPU
#elif defined(ENABLE_GPU)
#include "plugin/device/gpu/hal/device/kernel_info_setter.h"
#endif
#include "plugin/device/cpu/hal/device/kernel_select_cpu.h"
@ -210,21 +210,21 @@ void CallbackImpl::SetEmptyKernelInfo(const AnfNodePtr &node) {
void CallbackImpl::ResetKernelInfo(const AnfNodePtr &node) {
auto cnode = node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(cnode);
#if ENABLE_D
#ifdef ENABLE_D
if (GetTargetFromContext() == kCPUDevice) {
cnode->set_kernel_info(std::make_shared<device::KernelInfo>());
device::cpu::SetKernelInfo(cnode);
} else {
device::ascend::SetKernelInfo(cnode, KernelType::UNKNOWN_KERNEL_TYPE);
}
#elif ENABLE_GPU
#elif defined(ENABLE_GPU)
cnode->set_kernel_info(std::make_shared<device::KernelInfo>());
if (GetTargetFromContext() == kCPUDevice) {
device::cpu::SetKernelInfo(cnode);
} else {
device::gpu::SetKernelInfo(cnode);
}
#elif ENABLE_CPU
#elif defined(ENABLE_CPU)
cnode->set_kernel_info(std::make_shared<device::KernelInfo>());
device::cpu::SetKernelInfo(cnode);
#endif
@ -246,21 +246,23 @@ TypeId CallbackImplWithInferShape::GetOutputType(const AnfNodePtr &node, size_t
return CallbackImpl::GetOutputInferType(node, i);
}
std::string CallbackImplWithInferShape::GetInputFormat(const AnfNodePtr &node, size_t i) { return kOpFormat_DEFAULT; }
std::string CallbackImplWithInferShape::GetInputFormat(const AnfNodePtr &, size_t) { return kOpFormat_DEFAULT; }
std::string CallbackImplWithInferShape::GetOutputFormat(const AnfNodePtr &node, size_t i) { return kOpFormat_DEFAULT; }
std::string CallbackImplWithInferShape::GetOutputFormat(const AnfNodePtr &, size_t) { return kOpFormat_DEFAULT; }
void CallbackImplWithInferShape::SetBasicNodeKernelInfo(const AnfNodePtr &node,
const std::vector<inner::NodeBase> &outputs_info) {
node->set_kernel_info(std::make_shared<device::KernelInfo>());
if (node->cast<CNodePtr>() != nullptr) return;
std::vector<std::string> output_formats;
std::vector<TypeId> output_types;
AbstractBasePtrList abs_list;
if (node->cast<CNodePtr>() != nullptr) {
return;
}
bool has_fake_abstract = false;
std::vector<TypeId> output_types;
std::vector<std::string> output_formats;
AbstractBasePtrList abs_list;
for (size_t i = 0; i < outputs_info.size(); ++i) {
output_formats.push_back(outputs_info[i].format);
output_types.push_back(outputs_info[i].type);
output_formats.push_back(outputs_info[i].format);
ShapeVector abs_shape;
if (outputs_info[i].format != kOpFormat_DEFAULT) {
abs_shape = GetFakeAbstractShape(outputs_info[i].shape, outputs_info[i].format);
@ -268,8 +270,7 @@ void CallbackImplWithInferShape::SetBasicNodeKernelInfo(const AnfNodePtr &node,
} else {
abs_shape = outputs_info[i].shape;
}
auto abs_tensor = std::make_shared<abstract::AbstractTensor>(TypeIdToType(outputs_info[i].type), abs_shape);
abs_list.push_back(abs_tensor);
abs_list.push_back(std::make_shared<abstract::AbstractTensor>(TypeIdToType(outputs_info[i].type), abs_shape));
}
if (has_fake_abstract) {
if (abs_list.size() == 1) {
@ -285,7 +286,7 @@ void CallbackImplWithInferShape::SetBasicNodeKernelInfo(const AnfNodePtr &node,
AnfAlgo::SetSelectKernelBuildInfo(info_builder.Build(), node.get());
}
std::string CallbackImplWithInferShape::GetProcessor(const AnfNodePtr &node) {
std::string CallbackImplWithInferShape::GetProcessor(const AnfNodePtr &) {
return kernel::GetStrProcessorFromContext();
}
} // namespace mindspore::graphkernel

View File

@ -47,9 +47,9 @@ class COMMON_EXPORT CallbackImplWithInferShape : public CallbackImpl {
ShapeVector GetOutputShape(const AnfNodePtr &node, size_t i) override;
TypeId GetInputType(const AnfNodePtr &node, size_t i) override;
TypeId GetOutputType(const AnfNodePtr &node, size_t i) override;
std::string GetInputFormat(const AnfNodePtr &node, size_t i) override;
std::string GetOutputFormat(const AnfNodePtr &node, size_t i) override;
std::string GetProcessor(const AnfNodePtr &node) override;
std::string GetInputFormat(const AnfNodePtr &, size_t) override;
std::string GetOutputFormat(const AnfNodePtr &, size_t) override;
std::string GetProcessor(const AnfNodePtr &) override;
void SetBasicNodeKernelInfo(const AnfNodePtr &node, const std::vector<inner::NodeBase> &outputs_info) override;
};
} // namespace mindspore::graphkernel

View File

@ -18,6 +18,7 @@
#include <map>
#include <set>
#include <vector>
#include <string>
#include <memory>
#include "include/common/utils/python_adapter.h"
@ -37,7 +38,9 @@ ExpanderPtr GetExpander(const AnfNodePtr &node, bool abstract) {
abstract
? std::make_shared<PyExpander>(std::static_pointer_cast<Callback>(std::make_shared<CallbackImplWithInferShape>()))
: std::make_shared<PyExpander>(Callback::Instance());
if (IsComplexOp(node)) return ComplexOpDecorator::Creator(expander);
if (IsComplexOp(node)) {
return ComplexOpDecorator::Creator(expander);
}
constexpr size_t kAssignInputIdx = 1;
constexpr size_t kLambOptimizerInputIdx = 12;
@ -54,7 +57,7 @@ ExpanderPtr GetExpander(const AnfNodePtr &node, bool abstract) {
{prim::kPrimAdamWeightDecay->name(), {OpUMonadExpanderDeco::GetCreator(kAdamWeightDecayInputIdx)}},
{prim::kPrimDropout->name(), {DropoutExpanderDeco::Creator}},
};
auto iter = creators.find(GetCNodePrimitive(node)->name());
const auto iter = creators.find(GetCNodePrimitive(node)->name());
if (iter != creators.end()) {
return WrapExpander(expander, iter->second);
}
@ -62,10 +65,16 @@ ExpanderPtr GetExpander(const AnfNodePtr &node, bool abstract) {
}
bool CanExpandFallback(const AnfNodePtr &node) {
if (!node->isa<CNode>()) return false;
if (common::AnfAlgo::IsDynamicShape(node) && common::GetEnv("MS_DEV_EXPANDER_FALLBACK_DYNAMIC") != "on") return false;
if (common::GetEnv("MS_DEV_EXPANDER_FALLBACK") == "off") return false;
static std::vector<OpWithLevel> expander_fallback_ops_with_level = {
if (!node->isa<CNode>()) {
return false;
}
if (common::AnfAlgo::IsDynamicShape(node) && common::GetEnv("MS_DEV_EXPANDER_FALLBACK_DYNAMIC") != "on") {
return false;
}
if (common::GetEnv("MS_DEV_EXPANDER_FALLBACK") == "off") {
return false;
}
static const std::vector<OpWithLevel> expander_fallback_ops_with_level = {
{kAllTarget, OpLevel_0, prim::kPrimEqualCount},
{kAllTarget, OpLevel_0, prim::kPrimSoftsign},
{kAllTarget, OpLevel_0, prim::kPrimSquare},
@ -122,20 +131,24 @@ bool CanExpandFallback(const AnfNodePtr &node) {
{kAllTarget, OpLevel_1, prim::kPrimMinimumGrad},
{kAllTarget, OpLevel_1, prim::kPrimTanhGrad},
};
auto op_level = (common::GetEnv("MS_DEV_EXPANDER_FALLBACK") == "1") ? 1 : 0;
unsigned int op_level = (common::GetEnv("MS_DEV_EXPANDER_FALLBACK") == "1") ? 1 : 0;
auto ops = GkUtils::GetValidOps(expander_fallback_ops_with_level, op_level, {}, {}, {});
return std::any_of(ops.begin(), ops.end(),
[&node](const PrimitivePtr &prim) { return IsPrimitiveCNode(node, prim); });
}
FuncGraphPtr TryExpandCNode(const AnfNodePtr &node, const std::function<bool(const CNodePtr &kernel_node)> &func) {
if (!CanExpandFallback(node)) return nullptr;
if (!CanExpandFallback(node)) {
return nullptr;
}
auto expand_fg = GetCNodeFuncGraph(GetExpander(node)->Run(node));
if (expand_fg != nullptr) {
auto todos = TopoSort(expand_fg->get_return());
for (const auto &n : todos) {
auto cnode = n->cast<CNodePtr>();
if (cnode == nullptr || !AnfUtils::IsRealKernel(cnode)) continue;
if (cnode == nullptr || !AnfUtils::IsRealKernel(cnode)) {
continue;
}
auto suc = func(cnode);
if (!suc) {
MS_LOG(DEBUG) << "Expanding core ops [" << cnode->fullname_with_scope() << "] failed.";
@ -163,7 +176,7 @@ void ConvertAttrToInput(const FuncGraphPtr &graph) {
{prim::kPrimReduceMax->name(), {1}},
{prim::kPrimReduceSum->name(), {1}},
{prim::kPrimTranspose->name(), {1}}};
if (attr2input_map.count(primitive->name())) {
if (attr2input_map.count(primitive->name()) != 0) {
auto input_names = primitive->GetAttr(kAttrInputNames);
auto cnode = dyn_cast<CNode>(node);
AnfNodePtrList inputs = cnode->inputs();
@ -172,7 +185,7 @@ void ConvertAttrToInput(const FuncGraphPtr &graph) {
auto attrs_map = attr2input_map[primitive->name()];
size_t j = 1;
for (size_t i = 0; i < input_names_vec.size(); ++i) {
if (attrs_map.count(i)) {
if (attrs_map.count(i) != 0) {
auto value = primitive->GetAttr(input_names_vec[i]);
auto value_node = std::make_shared<ValueNode>(value);
value_node->set_abstract(value->ToAbstract());
@ -193,7 +206,9 @@ void ConvertAttrToInput(const FuncGraphPtr &graph) {
AnfNodePtr AttrToInputDeco::Run(const AnfNodePtr &node) {
auto new_node = decorated_->Run(node);
if (new_node == nullptr) return nullptr;
if (new_node == nullptr) {
return nullptr;
}
auto new_cnode = dyn_cast<CNode>(new_node);
auto expand_fg = GetCNodeFuncGraph(new_cnode);
ConvertAttrToInput(expand_fg);

View File

@ -15,9 +15,6 @@
*/
#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_ADAPTER_EXPANDER_H_
#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_ADAPTER_EXPANDER_H_
#include <vector>
#include <string>
#include <unordered_set>
#include <memory>
#include "common/graph_kernel/core/expander.h"
#include "ir/func_graph.h"
@ -59,7 +56,7 @@ class COMMON_EXPORT AttrToInputDeco : public ExpanderDecorator {
/**
* Get the Expander which is used to expand a cnode to a funcgraph which composite same function with core ops.
*/
COMMON_EXPORT ExpanderPtr GetExpander(const AnfNodePtr &, bool abstract = true);
COMMON_EXPORT ExpanderPtr GetExpander(const AnfNodePtr &node, bool abstract = true);
/**
* Inline the expanded func graph to main graph.

View File

@ -17,7 +17,6 @@
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_FAKE_ABSTRACT_SHAPE_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_FAKE_ABSTRACT_SHAPE_H_
#include <string>
#include <vector>
#include <map>
#include "utils/shape_utils.h"

View File

@ -34,7 +34,9 @@ class GraphKernelExpanderWithPy : public GraphKernelExpander {
std::vector<PrimitivePtr> InitOpList() override;
ExpanderPtr InitExpander(const AnfNodePtr &node) override;
bool CanExpand(const CNodePtr &node) const override {
if (IsComplexOp(node)) return true;
if (IsComplexOp(node)) {
return true;
}
return GraphKernelExpander::CanExpand(node);
}
};

View File

@ -16,8 +16,6 @@
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_
#include "ir/anf.h"
#include "ir/func_graph.h"
#include "backend/common/session/kernel_graph.h"
#include "backend/common/optimizer/optimizer.h"
#include "backend/common/optimizer/pass_manager.h"

View File

@ -144,8 +144,12 @@ class CostModelSplitSchemer : public SplitSchemer {
}
virtual bool IsValidKernelNode(const AnfNodePtr &node) const {
if (!node->isa<CNode>()) return false;
if (AnfUtils::IsRealKernel(node)) return true;
if (!node->isa<CNode>()) {
return false;
}
if (AnfUtils::IsRealKernel(node)) {
return true;
}
return false;
}
@ -193,9 +197,13 @@ class CostModelSplitSchemer : public SplitSchemer {
// assign virtual node to the same group of its input.
virtual void GroupVirtualNodes() {
for (const auto &node : topo_all_nodes_) {
if (node_group_.count(node)) continue;
if (node_group_.count(node) != 0) {
continue;
}
auto cnode = node->cast<CNodePtr>();
if (cnode == nullptr) continue;
if (cnode == nullptr) {
continue;
}
bool found = false;
for (const auto &input : cnode->inputs()) {
auto iter = node_group_.find(input);

View File

@ -207,7 +207,7 @@ bool AtomicAddCheckerAscend::SuitableForAtomicAdd(const AnfNodePtr &node) {
for (size_t i = 0; i < src_shape_vec.size(); ++i) {
auto dim = src_shape_vec[i];
if (dim != 1) {
if (reduce_axis_set.count(i)) {
if (reduce_axis_set.count(i) != 0) {
start_with_reduce = true;
}
break;
@ -222,7 +222,7 @@ bool AtomicAddCheckerAscend::SuitableForAtomicAdd(const AnfNodePtr &node) {
auto start_non_reduce_dim = 1LL;
for (size_t i = 0; i < src_shape_vec.size(); ++i) {
auto dim = src_shape_vec[i];
if (reduce_axis_set.count(i)) {
if (reduce_axis_set.count(i) != 0) {
break;
}
start_non_reduce_dim = start_non_reduce_dim * dim;

View File

@ -151,7 +151,9 @@ std::vector<std::pair<AnfNodePtr, int>> StitchAtomicCleanInserter::FindInnerCNod
}
std::pair<bool, InplaceAssignerInfo> StitchAtomicCleanInserter::IsStitchWithAtomic(const AnfNodePtr &anf_node) {
if (!common::AnfAlgo::IsGraphKernel(anf_node)) return {false, InplaceAssignerInfo()};
if (!common::AnfAlgo::IsGraphKernel(anf_node)) {
return {false, InplaceAssignerInfo()};
}
auto node = anf_node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(node);
auto sub_graph = common::AnfAlgo::GetCNodeFuncGraphPtr(node);

View File

@ -164,7 +164,7 @@ class COMMON_EXPORT Callback {
class CallbackImplRegister {
public:
explicit CallbackImplRegister(const std::function<CallbackPtr()> &fn) { Callback::RegImpl(fn()); }
explicit CallbackImplRegister(const std::function<CallbackPtr()> &fn) noexcept { Callback::RegImpl(fn()); }
~CallbackImplRegister() = default;
protected:

View File

@ -246,9 +246,11 @@ std::vector<InplaceAssignUserInfo> InplaceAssignBuilder::FindOriginCNodeUsers(
for (const auto &node_index : users) {
// 1. First, find TupleGetItem nodes.
const auto &user_node = node_index.first;
if (!IsPrimitiveCNode(user_node, prim::kPrimTupleGetItem)) continue;
if (!IsPrimitiveCNode(user_node, prim::kPrimTupleGetItem)) {
continue;
}
auto item_idx = GetItemIdx(user_node);
auto iter = real_indices_and_input_node.find(item_idx);
const auto iter = real_indices_and_input_node.find(item_idx);
if (iter != real_indices_and_input_node.end()) {
(void)getitem_user_nodes.emplace_back(user_node, iter->second);
}
@ -271,7 +273,7 @@ std::vector<InplaceAssignUserInfo> InplaceAssignBuilder::FindOriginCNodeUsers(
void InplaceAssignBuilder::ProcessOriginCNodeUser(
const FuncGraphPtr &main_graph, const AnfNodePtr &composite_node,
const std::vector<std::pair<InplaceAssignerInfo, AnfNodePtr>> &info_and_inplace_assignee_addr,
const FuncGraphManagerPtr &mng) {
const FuncGraphManagerPtr &mng) const {
// 1. Find users.
auto user_nodes = FindOriginCNodeUsers(main_graph, composite_node, info_and_inplace_assignee_addr, mng);
for (const auto &iter : user_nodes) {

View File

@ -60,7 +60,7 @@ class InplaceAssignBuilder : public opt::Pass {
virtual void ProcessOriginCNodeUser(
const FuncGraphPtr &main_graph, const AnfNodePtr &composite_node,
const std::vector<std::pair<InplaceAssignerInfo, AnfNodePtr>> &info_and_inplace_assignee_addr,
const FuncGraphManagerPtr &mng);
const FuncGraphManagerPtr &mng) const;
virtual void SetTargetAttrs(const CNodePtr &) {}
private:

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.
@ -62,7 +62,7 @@ std::map<size_t, AnfNodePtr> FindAssignAndOutputVal(const CNodePtr &fg_cnode) {
auto assign_parameter = out->cast<CNodePtr>()->input(1);
auto iter = std::find(inputs.begin() + 1, inputs.end(), assign_val);
if (iter != inputs.end()) {
size_t assign_val_index = iter - inputs.begin();
size_t assign_val_index = static_cast<size_t>(iter - inputs.begin());
auto assign_to = ParameterToInput(assign_parameter);
if (assign_to != nullptr && assign_val_index > 0) {
output_replace_map[assign_val_index - 1] = assign_to;
@ -122,7 +122,9 @@ void UpdateUsersOfGraphKernel(const FuncGraphPtr &func_graph, const AnfNodePtr &
MS_EXCEPTION_IF_NULL(mng);
for (const auto &getitem_iter : mng->node_users()[cnode]) {
auto getitem = getitem_iter.first;
if (GetitemIndex(getitem) != removed_index) continue;
if (GetitemIndex(getitem) != removed_index) {
continue;
}
auto getitem_users = mng->node_users()[getitem]; // get a copy of getitem's users before replacing
for (const auto &getitem_user_iter : getitem_users) {
@ -146,13 +148,17 @@ bool RepalceOutputByParameter(const FuncGraphPtr &func_graph) {
bool changed = false;
for (const auto &n : todos) {
if (!common::AnfAlgo::IsGraphKernel(n)) continue;
if (!common::AnfAlgo::IsGraphKernel(n)) {
continue;
}
auto cnode = n->cast<CNodePtr>();
auto replaceable_nodes = FindAssignAndOutputVal(cnode);
if (replaceable_nodes.empty()) continue;
if (replaceable_nodes.empty()) {
continue;
}
changed = true;
for (const auto &iter : replaceable_nodes) {
UpdateUsersOfGraphKernel(func_graph, cnode, iter.second, static_cast<int64_t>(iter.first));
for (const auto &[index, node] : replaceable_nodes) {
UpdateUsersOfGraphKernel(func_graph, cnode, node, static_cast<int64_t>(index));
}
}
return changed;
@ -164,7 +170,9 @@ bool ReplaceAssignByInplaceAssignInGraphkernel(const FuncGraphPtr &func_graph) {
auto todos = TopoSort(func_graph->get_return());
bool changed = false;
for (const auto &n : todos) {
if (!common::AnfAlgo::CheckPrimitiveType(n, prim::kPrimAssign)) continue;
if (!common::AnfAlgo::CheckPrimitiveType(n, prim::kPrimAssign)) {
continue;
}
changed = true;
auto cnode = n->cast<CNodePtr>();
AnfNodePtrList inputs = {NewValueNode(prim::kPrimInplaceAssign), cnode->input(1), cnode->input(2), cnode->input(2)};
@ -194,7 +202,9 @@ bool RepalceAssignByInplaceAssign(const FuncGraphPtr &func_graph) {
auto changed = false;
for (const auto &n : todos) {
if (!common::AnfAlgo::IsGraphKernel(n)) continue;
if (!common::AnfAlgo::IsGraphKernel(n)) {
continue;
}
auto graph_kernel_fg = common::AnfAlgo::GetCNodeFuncGraphPtr(n);
MS_EXCEPTION_IF_NULL(graph_kernel_fg);
changed = ReplaceAssignByInplaceAssignInGraphkernel(graph_kernel_fg) || changed;