fix switch_layer's abstrct is null

This commit is contained in:
huanghui 2022-01-13 16:50:33 +08:00
parent 786c06fe20
commit 2a67bf7492
6 changed files with 48 additions and 33 deletions

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.
@ -31,7 +31,7 @@ bool CNodeHasTupleInput(const CNodePtr &cnode) {
continue;
}
if (IsValueNode<Primitive>(inputs[i])) {
// unexpected high order primitvie as cnode input when transform graph
// unexpected high order primitive as cnode input when transform graph
MS_LOG(WARNING) << "CheckTupleInput, got unexpected primitive as input" << cnode->DebugString();
return false;
}
@ -62,7 +62,10 @@ std::vector<AnfNodePtr> TransformTupleArgument(const FuncGraphPtr &fg, const Anf
auto &elements = abs->elements();
std::vector<AnfNodePtr> tuple_node_expanded;
for (size_t i = 0; i < elements.size(); i++) {
auto elem_node = fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, NewValueNode(SizeToLong(i))});
auto idx = NewValueNode(SizeToLong(i));
auto abstract_scalar = std::make_shared<abstract::AbstractScalar>(std::make_shared<Int64Imm>(SizeToLong(i)));
idx->set_abstract(abstract_scalar);
auto elem_node = fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, idx});
elem_node->set_abstract(elements[i]);
if (elements[i]->isa<abstract::AbstractTuple>()) {
auto nodes = TransformTupleArgument(fg, elem_node, elements[i]->cast<abstract::AbstractTuplePtr>());
@ -119,11 +122,11 @@ AnfNodePtr TransformPartial(const FuncGraphPtr &trans_fg, const CNodePtr &cnode)
return new_node;
}
AnfNodePtr TransformSwitchCall(const AnfNodePtr &swtich_node, const CNodePtr &cnode) {
AnfNodePtr TransformSwitchCall(const AnfNodePtr &switch_node, const CNodePtr &cnode) {
auto &cinputs = cnode->inputs();
auto fg = cnode->func_graph();
std::vector<AnfNodePtr> inputs;
inputs.push_back(swtich_node);
inputs.push_back(switch_node);
for (size_t i = 1; i < cinputs.size(); i++) {
auto abs = cinputs[i]->abstract();
if (abs == nullptr) {

View File

@ -33,7 +33,7 @@ std::vector<AnfNodePtr> TransformTupleArgument(const FuncGraphPtr &fg, const Anf
const abstract::AbstractTuplePtr &abs);
AnfNodePtr TransformCallGraph(const FuncGraphPtr &trans_fg, const CNodePtr &cnode);
AnfNodePtr TransformPartial(const FuncGraphPtr &trans_fg, const CNodePtr &cnode);
AnfNodePtr TransformSwitchCall(const AnfNodePtr &swtich_node, const CNodePtr &cnode);
AnfNodePtr TransformSwitchCall(const AnfNodePtr &switch_node, const CNodePtr &cnode);
class GraphTupleParamTransform {
public:

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.
@ -123,7 +123,7 @@ OptimizeIRPassLib::OptimizeIRPassLib() {
environ_get_eliminate_ =
MakeSubstitution(std::make_shared<EnvironGetEliminater>(), "environ_get_eliminate", prim::kPrimEnvironGet);
environ_get_add_eliminate_ =
MakeSubstitution(std::make_shared<EnvironGetAddEliminater>(), "environ_get_add_eliminate_", prim::kPrimEnvironGet);
MakeSubstitution(std::make_shared<EnvironGetAddEliminater>(), "environ_get_add_eliminate", prim::kPrimEnvironGet);
environ_get_set_eliminate_ =
MakeSubstitution(std::make_shared<EnvironGetSetEliminater>(), "environ_get_set_eliminate", prim::kPrimEnvironGet);
environ_get_depend_swap_ =

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.
@ -35,6 +35,10 @@
namespace mindspore {
namespace opt {
namespace irpass {
constexpr int kInputZero = 0;
constexpr int kInputOne = 1;
constexpr int kInputTwo = 2;
constexpr int kInputThree = 3;
// {G, Xs}-->transform graph call tuple inputs to flat inputs.
class GraphCallTupleTransform : public AnfVisitor {
public:
@ -48,7 +52,7 @@ class GraphCallTupleTransform : public AnfVisitor {
auto cnode = node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(cnode);
auto &inputs = cnode->inputs();
auto fg = GetValueNode<FuncGraphPtr>(inputs[0]);
auto fg = GetValueNode<FuncGraphPtr>(inputs[kInputZero]);
if (fg == nullptr) {
return nullptr;
}
@ -78,29 +82,29 @@ class SwitchCallTupleTransform : public AnfVisitor {
if (call_inputs.size() < 1) {
return nullptr;
}
if (!IsPrimitiveCNode(call_inputs[0], prim::kPrimSwitch)) {
if (!IsPrimitiveCNode(call_inputs[kInputZero], prim::kPrimSwitch)) {
return nullptr;
}
auto swich_cnode = call_inputs[0]->cast<CNodePtr>();
auto switch_inputs = swich_cnode->inputs();
auto switch_cnode = call_inputs[kInputZero]->cast<CNodePtr>();
auto switch_inputs = switch_cnode->inputs();
if (switch_inputs.size() != 4) {
return nullptr;
}
AnfNodePtr transformed = nullptr;
bool true_br_changed = TransformBranchNode(switch_inputs[2], optimizer->manager(), &transformed);
bool true_br_changed = TransformBranchNode(switch_inputs[kInputTwo], optimizer->manager(), &transformed);
if (true_br_changed) {
switch_inputs[2] = transformed;
switch_inputs[kInputTwo] = transformed;
}
bool false_br_changed = TransformBranchNode(switch_inputs[3], optimizer->manager(), &transformed);
bool false_br_changed = TransformBranchNode(switch_inputs[kInputThree], optimizer->manager(), &transformed);
if (false_br_changed) {
switch_inputs[3] = transformed;
switch_inputs[kInputThree] = transformed;
}
if (true_br_changed || false_br_changed) {
call_inputs[0] = swich_cnode->func_graph()->NewCNode(switch_inputs);
call_inputs[kInputZero] = switch_cnode->func_graph()->NewCNode(switch_inputs);
}
if (CNodeHasTupleInput(switch_call_cnode)) {
return TransformSwitchCall(call_inputs[0], switch_call_cnode);
return TransformSwitchCall(call_inputs[kInputZero], switch_call_cnode);
}
if (true_br_changed || false_br_changed) {
return switch_call_cnode->func_graph()->NewCNode(call_inputs);
@ -120,8 +124,8 @@ class SwitchCallTupleTransform : public AnfVisitor {
}
if (IsPrimitiveCNode(node, prim::kPrimPartial)) {
auto partial_inputs = node->cast<CNodePtr>()->inputs();
if (IsValueNode<FuncGraph>(partial_inputs[1])) {
FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(partial_inputs[1]);
if (IsValueNode<FuncGraph>(partial_inputs[kInputOne])) {
FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(partial_inputs[kInputOne]);
if (FuncGraphHasTupleInput(fg)) {
fg = graph_transform_(fg, mng);
}
@ -156,23 +160,26 @@ class SwitchLayerCallTupleTransform : public AnfVisitor {
if (call_inputs.size() < 1) {
return nullptr;
}
if (!IsPrimitiveCNode(call_inputs[0], prim::kPrimSwitchLayer)) {
if (!IsPrimitiveCNode(call_inputs[kInputZero], prim::kPrimSwitchLayer)) {
return nullptr;
}
auto swich_layer_cnode = call_inputs[0]->cast<CNodePtr>();
auto switch_layer_inputs = swich_layer_cnode->inputs();
auto switch_layer_cnode = call_inputs[kInputZero]->cast<CNodePtr>();
auto switch_layer_inputs = switch_layer_cnode->inputs();
if (switch_layer_inputs.size() != 3) {
return nullptr;
}
AnfNodePtr transformed = nullptr;
bool layer_changed = TransformLayerNode(switch_layer_inputs[2], optimizer->manager(), &transformed);
bool layer_changed = TransformLayerNode(switch_layer_inputs[kInputTwo], optimizer->manager(), &transformed);
if (layer_changed) {
switch_layer_inputs[2] = transformed;
call_inputs[0] = switch_layer_call_cnode->func_graph()->NewCNode(switch_layer_inputs);
transformed->set_abstract(switch_layer_inputs[kInputTwo]->abstract());
switch_layer_inputs[kInputTwo] = transformed;
auto new_switch_layer = switch_layer_call_cnode->func_graph()->NewCNode(switch_layer_inputs);
new_switch_layer->set_abstract(switch_layer_cnode->abstract());
call_inputs[kInputZero] = new_switch_layer;
}
if (CNodeHasTupleInput(switch_layer_call_cnode)) {
return TransformSwitchCall(call_inputs[0], switch_layer_call_cnode);
return TransformSwitchCall(call_inputs[kInputZero], switch_layer_call_cnode);
}
if (layer_changed) {
return switch_layer_call_cnode->func_graph()->NewCNode(call_inputs);
@ -195,7 +202,9 @@ class SwitchLayerCallTupleTransform : public AnfVisitor {
FuncGraphPtr fg = GetValueNode<FuncGraphPtr>(tuple_inputs[i]);
if (FuncGraphHasTupleInput(fg)) {
FuncGraphPtr transformed_fg = graph_transform_(fg, mng);
tuple_inputs[i] = NewValueNode(transformed_fg);
auto new_value_node = NewValueNode(transformed_fg);
new_value_node->set_abstract(tuple_inputs[i]->abstract());
tuple_inputs[i] = new_value_node;
changed = true;
}
}

View File

@ -538,7 +538,7 @@ void InitOpt(const ResourcePtr &res) {
g_pass_opts["opt_a"] = Optimizer::MakeOptimizer("opt_a", res, GetOptPassesA(irpass));
g_pass_opts["opt_b"] = Optimizer::MakeOptimizer("opt_b", res, GetOptPassesB(irpass), false, true);
g_pass_opts["opt_after_cconv"] =
Optimizer::MakeOptimizer("opt_after_cconv", res, GetOptPassesAfterCconv(irpass), false, false);
Optimizer::MakeOptimizer("opt_after_cconv", res, GetOptPassesAfterCconv(irpass), false, true);
g_pass_opts["opt_trans_graph"] =
Optimizer::MakeOptimizer("opt_trans_graph", res, GetOptPassesTransformGraph(irpass), true, true);
g_pass_opts["renormal"] = Optimizer::MakeOptimizer("renormal", res, GetOptPassesC(irpass));

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.
@ -424,8 +424,11 @@ void Cloner::AddInputs(const FuncGraphPtr &func_graph_user, const FuncGraphPtr &
auto &repl_func_graph = repl_map_func_graph_[func_graph_user];
auto [iter, inserted] = repl_func_graph.emplace(func_graph, nullptr);
if (inserted) {
AnfNodePtrList cnode_inputs{BuildPrimitiveValueNode(prim::kPrimPartial), BuildFuncGraphValueNode(func_graph)};
iter->second = func_graph_user->NewCNode(std::move(cnode_inputs));
auto value_node = BuildPrimitiveValueNode(prim::kPrimPartial);
AnfNodePtrList cnode_inputs{value_node, BuildFuncGraphValueNode(func_graph)};
auto partial_node = func_graph_user->NewCNode(std::move(cnode_inputs));
partial_node->set_abstract(value_node->abstract());
iter->second = partial_node;
}
auto cnode = dyn_cast<CNode>(iter->second);
if (cnode == nullptr) {