!20837 adjust and update irpass

Merge pull request !20837 from huangbingjian/modify_pass
This commit is contained in:
i-robot 2021-07-27 11:50:44 +00:00 committed by Gitee
commit cc0e1cd6d2
10 changed files with 44 additions and 156 deletions

View File

@ -49,7 +49,6 @@
#include "frontend/optimizer/irpass/sparse_tensor_eliminate.h"
#include "frontend/optimizer/irpass/switch_or_switch_layer_defer_inline.h"
#include "frontend/optimizer/irpass/call_graph_tuple_transform.h"
#include "frontend/optimizer/irpass/bool_scalar_eliminate.h"
#include "frontend/optimizer/irpass/recompute_prepare.h"
namespace mindspore {
@ -256,8 +255,6 @@ OptimizeIRPassLib::OptimizeIRPassLib() {
switch_layer_defer_inline_ =
MakeSubstitution(std::make_shared<SwitchLayerDeferInline>(), "switch_layer_defer_inline", prim::kPrimSwitchLayer);
bool_scalar_eliminate_ = MakeSubstitution(std::make_shared<BoolScalarEliminate>(), "bool_scalar_eliminate_", IsCNode);
// recompute
set_cell_output_no_recompute_ = MakeSubstitution(std::make_shared<SetCellOutputNoRecompute>(),
"set_cell_output_no_recompute", IsValueNode<FuncGraph>);

View File

@ -157,9 +157,6 @@ class OptimizeIRPassLib {
// Pynative Eliminate
SubstitutionPtr pynative_eliminate_;
// Eliminate getattr bool scalar
SubstitutionPtr bool_scalar_eliminate_;
// Recompute
SubstitutionPtr set_cell_output_no_recompute_;
};

View File

@ -1,58 +0,0 @@
/**
* Copyright 2021 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 "frontend/optimizer/irpass/bool_scalar_eliminate.h"
#include "frontend/optimizer/optimizer.h"
namespace mindspore {
namespace opt {
namespace irpass {
AnfNodePtr BoolScalarEliminate::operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) {
auto cnode = node->cast<CNodePtr>();
if (cnode == nullptr) {
return nullptr;
}
if (!cnode->IsApply(prim::kPrimGetAttr)) {
return nullptr;
}
auto vnode = cnode->input(1)->cast<ValueNodePtr>();
if (vnode == nullptr) {
return nullptr;
}
if (!vnode->value()->isa<BoolImm>()) {
return nullptr;
}
auto res = optimizer->resource();
auto manager = res->manager();
auto &node_users = manager->node_users();
auto iter = node_users.find(node);
if (iter == node_users.end()) {
return nullptr;
}
AnfNodeIndexSet node_idx_set = iter->second;
for (auto &item : node_idx_set) {
(void)manager->Replace(item.first, vnode);
}
return nullptr;
}
} // namespace irpass
} // namespace opt
} // namespace mindspore

View File

@ -1,36 +0,0 @@
/**
* Copyright 2021 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_FRONTEND_OPTIMIZER_IRPASS_BOOL_SCALAR_ELIMINATE_H
#define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_BOOL_SCALAR_ELIMINATE_H
#include "ir/func_graph.h"
#include "frontend/optimizer/optimizer_caller.h"
#include "ir/pattern_matcher.h"
namespace mindspore {
namespace opt {
namespace irpass {
class BoolScalarEliminate : public OptimizerCaller {
public:
AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override;
};
} // namespace irpass
} // namespace opt
} // namespace mindspore
#endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_BOOL_SCALAR_ELIMINATE_H

View File

@ -65,36 +65,27 @@ AnfNodePtr ExpandJ(const ValueNodePtr &vnode, const pipeline::ResourceBasePtr &r
}
} // namespace internal
bool ExpandJPrim::operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer) {
auto manager = optimizer->manager();
// Search all j nodes.
GetJPrim(manager);
// Get j nodes that don't have embed j nodes.
std::vector<CNodePtr> todo;
// If graph also contains J(FuncGraph) or J(Primitive), then ignore this graph.
// ExpandJ innermost graph or primitive first.
std::copy_if(j_nodes_.begin(), j_nodes_.end(), std::back_inserter(todo),
[](const CNodePtr &j_node) { return !internal::CheckIfEmbedJ(j_node); });
// Expand j nodes that don't have embed j nodes.
bool change = false;
for (auto &j_node : todo) {
auto expanded_j = internal::ExpandJ(j_node->input(1)->cast<ValueNodePtr>(), optimizer->resource());
manager->Replace(j_node, expanded_j);
change = true;
}
return change;
}
bool ExpandJPrim::operator()(const FuncGraphPtr &root, const OptimizerPtr &optimizer) {
AnfNodePtr ret = root->get_return();
MS_EXCEPTION_IF_NULL(ret);
std::vector<AnfNodePtr> all_nodes = DeepScopedGraphSearch(ret);
void ExpandJPrim::GetJPrim(const FuncGraphManagerPtr &manager) {
j_nodes_.clear();
for (auto &fg : manager->func_graphs()) {
std::vector<AnfNodePtr> toposet = TopoSort(fg->get_return());
for (const auto &node : toposet) {
if (IsPrimitiveCNode(node, prim::kPrimJ)) {
j_nodes_.push_back(node->cast<CNodePtr>());
bool change = false;
auto manager = optimizer->manager();
for (auto &node : all_nodes) {
if (IsPrimitiveCNode(node, prim::kPrimJ)) {
auto j_node = node->cast<CNodePtr>();
// If graph also contains J(FuncGraph) or J(Primitive), then ignore this graph.
// ExpandJ innermost graph or primitive first.
if (internal::CheckIfEmbedJ(j_node)) {
continue;
}
auto expanded_j = internal::ExpandJ(j_node->input(1)->cast<ValueNodePtr>(), optimizer->resource());
manager->Replace(j_node, expanded_j);
change = true;
}
}
return change;
}
} // namespace irpass
} // namespace opt

View File

@ -36,11 +36,7 @@ class ExpandJPrim {
public:
ExpandJPrim() = default;
virtual ~ExpandJPrim() = default;
bool operator()(const FuncGraphPtr &func_graph, const OptimizerPtr &optimizer);
void GetJPrim(const FuncGraphManagerPtr &manager);
private:
std::vector<CNodePtr> j_nodes_;
bool operator()(const FuncGraphPtr &root, const OptimizerPtr &optimizer);
};
} // namespace irpass
} // namespace opt

View File

@ -1,5 +1,5 @@
/**
* Copyright 2020 Huawei Technologies Co., Ltd
* Copyright 2020-2021 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.
@ -40,7 +40,7 @@ class GetitemTransform {
GetitemTransform() : cache_() {}
~GetitemTransform() = default;
FuncGraphPtr operator()(const FuncGraphPtr &fg, int64_t idx) {
FuncGraphPtr operator()(const AnfNodePtr &node, const FuncGraphPtr &fg, int64_t idx) {
if (cache_.find(fg) == cache_.end()) {
cache_[fg] = {};
}
@ -61,7 +61,11 @@ class GetitemTransform {
}
new_fg->set_output(cnode->input(ids));
} else {
new_fg->set_output(new_fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), output, NewValueNode(idx)}));
auto idx_node = NewValueNode(idx);
idx_node->set_abstract(std::make_shared<abstract::AbstractScalar>(idx));
auto output_node = new_fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), output, idx_node});
output_node->set_abstract(node->abstract());
new_fg->set_output(output_node);
}
cache[idx] = new_fg;
@ -78,7 +82,7 @@ class GetItemTransformACrossGraph {
GetItemTransformACrossGraph() : cache_() {}
~GetItemTransformACrossGraph() = default;
FuncGraphPtr operator()(const FuncGraphPtr &fg, int64_t idx) {
FuncGraphPtr operator()(const AnfNodePtr &node, const FuncGraphPtr &fg, int64_t idx) {
if (cache_.find(fg) == cache_.end()) {
cache_[fg] = {};
}
@ -109,7 +113,11 @@ class GetItemTransformACrossGraph {
}
new_fg->set_output(cnode->input(ids));
} else {
new_fg->set_output(new_fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), output, NewValueNode(idx)}));
auto idx_node = NewValueNode(idx);
idx_node->set_abstract(std::make_shared<abstract::AbstractScalar>(idx));
auto output_node = new_fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), output, idx_node});
output_node->set_abstract(node->abstract());
new_fg->set_output(output_node);
}
cache[idx] = new_fg_outer;
@ -136,7 +144,7 @@ class IncorporateGetitem : public AnfVisitor {
return nullptr;
}
auto new_fg = getitem_transform_(fg_, idx_);
auto new_fg = getitem_transform_(node, fg_, idx_);
(void)args_.insert(args_.begin(), NewValueNode(new_fg));
auto new_node = node->func_graph()->NewCNode(args_);
// Check if the another only usage of {G, Xs} is UpdateState{s, {G, Xs}}, if yes, replace
@ -213,7 +221,7 @@ class IncorporateGetitemDepend : public AnfVisitor {
return nullptr;
}
auto new_fg = getitem_transform_(fg_, idx_);
auto new_fg = getitem_transform_(node, fg_, idx_);
(void)args_.insert(args_.begin(), NewValueNode(new_fg));
auto new_fg_cnode = node->func_graph()->NewCNode(args_);
AnfNodePtr new_depend_cnode;
@ -333,8 +341,8 @@ class IncorporateGetitemSwitch : public AnfVisitor {
!ExistEnvNodeInTupleItem(g2_) && !has_env_type) {
return nullptr;
}
auto new_g1 = getitem_transform_(g1_, idx_);
auto new_g2 = getitem_transform_(g2_, idx_);
auto new_g1 = getitem_transform_(node, g1_, idx_);
auto new_g2 = getitem_transform_(node, g2_, idx_);
auto sw_node = fg->NewCNode({NewValueNode(prim::kPrimSwitch), x_, NewValueNode(new_g1), NewValueNode(new_g2)});
(void)args_.insert(args_.begin(), sw_node);
@ -463,7 +471,7 @@ class IncorporateGetitemSwitchLayerA : public AnfVisitor {
std::vector<AnfNodePtr> layers;
for (auto &graph : graphs_) {
auto fg_transform = getitem_transform_(graph, idx_);
auto fg_transform = getitem_transform_(node, graph, idx_);
if (fg_transform == nullptr) {
return nullptr;
}
@ -554,7 +562,7 @@ class IncorporateGetitemSwitchLayerB : public AnfVisitor {
std::vector<AnfNodePtr> layers;
for (auto &graph : graphs_) {
auto fg_transform = getitem_transform_(graph, idx_);
auto fg_transform = getitem_transform_(node, graph, idx_);
if (fg_transform == nullptr) {
return nullptr;
}

View File

@ -159,10 +159,10 @@ class ChoicePartialEliminater : public AnfVisitor {
if (fg->func_graph_cnodes_index().size() != 1) {
// If a graph is used by 2 or more partial nodes at the same time, clone the graph.
auto new_fg = BasicClone(fg);
auto new_fg_node = NewValueNode(new_fg);
MS_EXCEPTION_IF_NULL(fg->manager());
fg->manager()->Replace(fg_node, new_fg_node);
fg_list_[i] = new_fg_node;
auto manager = fg->manager();
MS_EXCEPTION_IF_NULL(manager);
manager->AddFuncGraph(new_fg);
fg_node->cast<ValueNodePtr>()->set_value(new_fg);
}
}
return true;

View File

@ -60,7 +60,7 @@ class SpecializeTransform {
new_params.push_back(params[i]);
continue;
}
// replace the parameter with arg.
// replace the parameter with arg in new_fg without changing origin func_graph.
mng->Replace(params[i], NewReplaceValueNode(need_eliminate_args[i]));
}
mng->SetParameters(new_fg, new_params);

View File

@ -125,14 +125,8 @@ FuncGraphPtr PrimBpOptPassStep1(const opt::irpass::OptimizeIRPassLib &irpass, co
irpass.inline_,
});
opt::OptPassConfig bool_scalar_eliminate = opt::OptPassConfig({
irpass.bool_scalar_eliminate_,
});
OptPassGroupMap map({{"ad_eliminate", pynative_eliminate},
{"ad_inline", inline_opt},
{"bool_scalar_eliminate", bool_scalar_eliminate},
{"ad_switch_simplify", switch_simplify}});
OptPassGroupMap map(
{{"ad_eliminate", pynative_eliminate}, {"ad_inline", inline_opt}, {"ad_switch_simplify", switch_simplify}});
auto prim_bprop_opt_step_1 = opt::Optimizer::MakeOptimizer("prim_bprop_opt_step_1", res, map);
FuncGraphPtr func_graph = res->func_graph();
@ -320,7 +314,6 @@ OptPassGroupMap GetOptPassesA(const opt::irpass::OptimizeIRPassLib &irpass) {
false, true);
opt::OptPassConfig accelerated_algorithm = opt::OptPassConfig({irpass.less_batch_normalization_});
opt::OptPassConfig virtual_dataset = opt::OptPassConfig({irpass.virtual_dataset_eliminate_});
opt::OptPassConfig after_resolve_pass =
opt::OptPassConfig({irpass.get_make_ref_eliminate_, irpass.replace_old_param_});