239 lines
8.9 KiB
C++
239 lines
8.9 KiB
C++
/**
|
|
* Copyright 2019 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 "backend/common/optimizer/optimizer.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <utility>
|
|
|
|
#include "backend/common/optimizer/pass_manager.h"
|
|
#include "backend/common/session/anf_runtime_algorithm.h"
|
|
#include "include/common/utils/anfalgo.h"
|
|
#include "ir/manager.h"
|
|
|
|
namespace mindspore {
|
|
namespace opt {
|
|
PatternProcessPass::PatternProcessPass(const std::string &name, bool multigraph)
|
|
: NodePass(name),
|
|
multigraph_(multigraph),
|
|
pattern_engine_(PatternEngine(std::make_shared<Visitor>())),
|
|
primitive_vars_(std::make_shared<PrimitiveVarMap>()),
|
|
equiv_(std::make_shared<Equiv>()) {}
|
|
|
|
const BaseRef PatternProcessPass::DefinePattern() const {
|
|
VarPtr X = std::make_shared<Var>();
|
|
return BaseRef({X});
|
|
}
|
|
|
|
void PatternProcessPass::Build() {
|
|
VarPtr fg = std::make_shared<Var>("RootG");
|
|
pattern_ = SexpToNode(DefinePattern(), fg, primitive_vars_.get(), multigraph_);
|
|
}
|
|
|
|
/**
|
|
* @brief Matches a node in the computation graph against a predefined pattern and processes it accordingly.
|
|
*
|
|
* This method attempts to match a given node against a specific pattern. If a match is found, it processes
|
|
* the node and returns the result.
|
|
*
|
|
* @param func_graph The computation graph containing the node.
|
|
* @param node The node in the computation graph to match against the pattern.
|
|
* @return Processed node if a match is found; otherwise, nullptr.
|
|
*/
|
|
AnfNodePtr PatternProcessPass::Run(const FuncGraphPtr &func_graph, const AnfNodePtr &node) {
|
|
// Ensure the pattern is built before matching.
|
|
if (pattern_ == nullptr) {
|
|
Build();
|
|
}
|
|
|
|
auto primitive = GetCNodePrimitive(pattern_);
|
|
// Check if the node matches the primitive pattern.
|
|
if (IsPrimitiveCNode(node, primitive)) {
|
|
MS_EXCEPTION_IF_NULL(primitive_vars_);
|
|
MS_EXCEPTION_IF_NULL(equiv_);
|
|
equiv_->clear();
|
|
EquivPtr equiv = pattern_engine_.Match(pattern_, node, *primitive_vars_, equiv_);
|
|
if (equiv != nullptr && !equiv->empty()) {
|
|
return Process(func_graph, node, equiv);
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
/**
|
|
* @brief Retrieves the original nodes that match the primitive variables.
|
|
*
|
|
* @return A vector containing the original nodes matched against the primitive variables.
|
|
*/
|
|
std::vector<AnfNodePtr> PatternProcessPass::GetOrigNodes() const {
|
|
std::vector<AnfNodePtr> orig_nodes;
|
|
for (auto &prim_var : *primitive_vars_) {
|
|
if (equiv_->find(prim_var.second) == equiv_->end()) {
|
|
continue;
|
|
}
|
|
auto baseref = (*equiv_)[prim_var.second];
|
|
if (!utils::isa<CNode>(baseref)) {
|
|
continue;
|
|
}
|
|
auto node = utils::cast<AnfNodePtr>(baseref);
|
|
orig_nodes.push_back(node);
|
|
}
|
|
return orig_nodes;
|
|
}
|
|
|
|
/**
|
|
* @brief Creates a new CNode with the given inputs and attaches it to the provided computation graph.
|
|
*
|
|
* @param inputs The input nodes for the new CNode.
|
|
* @param fg The computation graph to which the new CNode should be added.
|
|
* @return The newly created CNode.
|
|
*/
|
|
CNodePtr PatternProcessPass::NewCNode(const std::vector<AnfNodePtr> &inputs, const FuncGraphPtr &fg) const {
|
|
MS_EXCEPTION_IF_NULL(fg);
|
|
auto orig_nodes = GetOrigNodes();
|
|
return opt::NewCNode(inputs, fg, orig_nodes);
|
|
}
|
|
|
|
/**
|
|
* @brief Creates a new CNode by copying an existing one and attaches it to the provided kernel graph.
|
|
*
|
|
* @param cnode The CNode to copy.
|
|
* @param fg The kernel graph to which the new CNode should be added.
|
|
* @return The newly created CNode.
|
|
*/
|
|
CNodePtr PatternProcessPass::NewCNode(const CNodePtr &cnode, const KernelGraphPtr &fg) const {
|
|
MS_EXCEPTION_IF_NULL(fg);
|
|
auto orig_nodes = GetOrigNodes();
|
|
return opt::NewCNode(cnode, fg, orig_nodes);
|
|
}
|
|
|
|
/**
|
|
* @brief Matches a node against another predefined pattern.
|
|
*
|
|
* This method checks if a node matches another specific pattern, distinct from the primary pattern.
|
|
*
|
|
* @param node The node in the computation graph to match against the secondary pattern.
|
|
* @param equiv The equivalence mapping of nodes to variables in the primary pattern.
|
|
* @return True if the node matches the secondary pattern; otherwise, false.
|
|
*/
|
|
bool MultipleOutputPatternProcessPass::MatchAnotherPattern(const AnfNodePtr &node, const EquivPtr &equiv) const {
|
|
MS_EXCEPTION_IF_NULL(node);
|
|
MS_EXCEPTION_IF_NULL(equiv);
|
|
VarPtr fg = std::make_shared<Var>("RootG");
|
|
MS_EXCEPTION_IF_NULL(child_primitive_vars_);
|
|
MS_EXCEPTION_IF_NULL(child_equiv_);
|
|
EquivPtr another_equiv =
|
|
child_pattern_engine_.Match(SexpToNode(DefineAnotherPattern(), fg, child_primitive_vars_.get(), true), node,
|
|
*child_primitive_vars_, child_equiv_);
|
|
if (another_equiv != nullptr && !another_equiv->empty()) {
|
|
return IsShareNodes(equiv, another_equiv);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @brief Retrieves the original nodes that match both the primary and secondary primitive variables.
|
|
*
|
|
* @return A vector containing the original nodes matched against both sets of primitive variables.
|
|
*/
|
|
std::vector<AnfNodePtr> MultipleOutputPatternProcessPass::GetOrigNodes() const {
|
|
std::vector<AnfNodePtr> orig_nodes = PatternProcessPass::GetOrigNodes();
|
|
for (auto &prim_var : *child_primitive_vars_) {
|
|
auto baseref = (*child_equiv_)[prim_var.second];
|
|
if (!utils::isa<CNode>(baseref)) {
|
|
continue;
|
|
}
|
|
auto node = utils::cast<AnfNodePtr>(baseref);
|
|
orig_nodes.push_back(node);
|
|
}
|
|
return orig_nodes;
|
|
}
|
|
|
|
/**
|
|
* @brief Adds a pass manager to the graph optimizer's list of pass managers.
|
|
*
|
|
* A pass manager contains a set of transformation passes to be applied to the computation graph.
|
|
*
|
|
* @param pass_manager The pass manager to be added.
|
|
*/
|
|
void GraphOptimizer::AddPassManager(const PassManagerPtr &pass_manager) {
|
|
if (pass_manager != nullptr) {
|
|
pass_managers_.push_back(pass_manager);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Optimizes a given computation graph by applying various transformation passes.
|
|
*
|
|
* This method applies a set of transformation passes to the given computation graph, seeking to
|
|
* optimize its structure and operations. These transformations can include tasks like constant
|
|
* folding, operation fusion, or simplification. The goal is to generate a more efficient graph
|
|
* without changing the original semantics. This function can either apply all the transformation
|
|
* passes once or multiple times based on the `run_only_once` parameter.
|
|
*
|
|
* @param func_graph The computation graph that needs to be optimized. Represented as a pointer
|
|
* to a FuncGraph.
|
|
* @param run_only_once If true, each transformation pass is applied only once. Otherwise, the
|
|
* passes may be applied multiple times until no further changes are observed
|
|
* in the graph.
|
|
* @return Returns the optimized computation graph.
|
|
*/
|
|
FuncGraphPtr GraphOptimizer::Optimize(const FuncGraphPtr &func_graph, bool run_only_once) {
|
|
// Check if the input computation graph is null. If so, raise an exception.
|
|
MS_EXCEPTION_IF_NULL(func_graph);
|
|
|
|
// Determine if we should run the optimization only once based on the number of pass managers.
|
|
run_only_once_ = (pass_managers_.size() == 1) ? true : run_only_once;
|
|
|
|
// Create or retrieve a manager for the computation graph.
|
|
// The manager is responsible for handling various tasks like graph traversal, node replacement,
|
|
// and overall graph management.
|
|
auto manager = Manage(func_graph, true);
|
|
|
|
bool changed = true;
|
|
// Loop until no further changes are observed in the graph.
|
|
while (changed) {
|
|
changed = false;
|
|
// Iterate through all the pass managers and run their associated transformation passes
|
|
// on the computation graph.
|
|
for (size_t i = 0; i < pass_managers_.size(); ++i) {
|
|
const PassManagerPtr &pm = pass_managers_[i];
|
|
// If a transformation results in a change to the graph, set the `changed` flag to true.
|
|
if (pm != nullptr && pm->Run(func_graph)) {
|
|
changed = true;
|
|
}
|
|
}
|
|
// If we're only supposed to run the optimization once, break out of the loop.
|
|
if (run_only_once_) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// List to hold the collection of computation graphs.
|
|
std::vector<FuncGraphPtr> func_graphs;
|
|
func_graphs.push_back(func_graph);
|
|
|
|
// Perform a topological sort on the computation graph nodes starting from the return node.
|
|
// This ensures that the graph nodes are arranged in a specific order based on their dependencies.
|
|
(void)TopoSort(func_graph->get_return());
|
|
|
|
// Return the optimized computation graph.
|
|
return func_graph;
|
|
}
|
|
} // namespace opt
|
|
} // namespace mindspore
|