234 lines
8.1 KiB
C++
234 lines
8.1 KiB
C++
/**
|
|
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
|
*
|
|
* 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.
|
|
* 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 "include/common/utils/cse.h"
|
|
|
|
#include <vector>
|
|
#include <set>
|
|
|
|
#include "ir/anf.h"
|
|
#include "ir/scalar.h"
|
|
#include "utils/hash_map.h"
|
|
#include "abstract/abstract_function.h"
|
|
#include "utils/flags.h"
|
|
#include "include/common/utils/utils.h"
|
|
#include "utils/anf_utils.h"
|
|
|
|
namespace mindspore {
|
|
/* namespace to support opt */
|
|
namespace opt {
|
|
using mindspore::abstract::AbstractBase;
|
|
using mindspore::abstract::AbstractFunction;
|
|
using mindspore::abstract::AbstractFunctionPtr;
|
|
|
|
bool WithRecomputedScope(const AnfNodePtr &node) {
|
|
MS_EXCEPTION_IF_NULL(node);
|
|
if (!node->isa<CNode>()) {
|
|
return false;
|
|
}
|
|
auto full_name_with_scope = node->fullname_with_scope();
|
|
return full_name_with_scope.find(kAttrRecompute) == 0;
|
|
}
|
|
|
|
bool IsSetRecomputed(const CNodePtr &a, const CNodePtr &b) {
|
|
return (WithRecomputedScope(a) && !a->HasAttr(kAttrNeedCseAfterRecompute)) ||
|
|
(WithRecomputedScope(b) && !b->HasAttr(kAttrNeedCseAfterRecompute));
|
|
}
|
|
|
|
void UpdateDebugInfoAndDumpFlag(const AnfNodePtr &main, const AnfNodePtr &node) {
|
|
if (main == nullptr || !main->isa<CNode>()) {
|
|
return;
|
|
}
|
|
if (AnfUtils::GetDumpFlag(node) && !AnfUtils::GetDumpFlag(main)) {
|
|
AnfUtils::SetDumpFlag(main);
|
|
}
|
|
auto main_cnode = main->cast<CNodePtr>();
|
|
main_cnode->AddFusedDebugInfo(node);
|
|
}
|
|
|
|
BasePtr AbsOf(const AnfNodePtr &node, bool ignore_fg_abs_tracking_id) {
|
|
MS_EXCEPTION_IF_NULL(node);
|
|
auto node_abs = node->abstract();
|
|
// In testcase: TestOptOpt.CSE, node->abstract() is null.
|
|
if (node_abs == nullptr) {
|
|
return kAnyValue;
|
|
}
|
|
if (node_abs->isa<abstract::PrimitiveAbstractClosure>()) {
|
|
// Ignore the tracking_id and prim pointer hash.
|
|
auto prim_abs = node_abs->cast<abstract::PrimitiveAbstractClosurePtr>();
|
|
return prim_abs->prim();
|
|
} else if (ignore_fg_abs_tracking_id && node_abs->isa<abstract::FuncGraphAbstractClosure>()) {
|
|
// Ignore the tracking_id.
|
|
auto new_fg_abs = node_abs->cast<abstract::AbstractFunctionPtr>()->Copy();
|
|
new_fg_abs->set_tracking_id(nullptr);
|
|
return new_fg_abs;
|
|
}
|
|
return node_abs;
|
|
}
|
|
|
|
// For a single function graph (fg), this function groups nodes based on their computed hash values
|
|
// and then attempts to replace duplicate nodes within these groups.
|
|
// @param fg: The target function graph to process.
|
|
// @param manager: The manager for this function graph.
|
|
// @return: Whether the function graph was changed.
|
|
bool CSE::BuildOrderGroupAndDoReplaceForOneGraph(const FuncGraphPtr &fg, const FuncGraphManagerPtr &manager) const {
|
|
MS_EXCEPTION_IF_NULL(fg);
|
|
|
|
// Lists to store ordering of groups, groupings of nodes based on hash values, and hash values for each node.
|
|
std::vector<std::size_t> order_group;
|
|
mindspore::HashMap<std::size_t, std::vector<AnfNodePtr>> groups;
|
|
mindspore::HashMap<AnfNodePtr, std::size_t> hashes;
|
|
|
|
// Topologically sort the nodes in the function graph starting from the return node.
|
|
std::vector<AnfNodePtr> toposet = TopoSort(fg->get_return());
|
|
|
|
// Compute the hash value for each node and group nodes based on these values.
|
|
for (auto node : toposet) {
|
|
MS_EXCEPTION_IF_NULL(node);
|
|
|
|
// Skip nodes that have already been hashed.
|
|
if (hashes.find(node) != hashes.end()) {
|
|
continue;
|
|
}
|
|
|
|
std::size_t h = 0;
|
|
if (node->isa<ValueNode>()) {
|
|
ValueNodePtr value_node = node->cast<ValueNodePtr>();
|
|
auto value = value_node->value();
|
|
MS_EXCEPTION_IF_NULL(value);
|
|
// Combine the hash of the node's value with its abstract hash.
|
|
h = hash_combine(value->hash(), (AbsOf(value_node, true)->hash()));
|
|
} else if (node->isa<CNode>()) {
|
|
auto cnode = node->cast<CNodePtr>();
|
|
auto &inputs = cnode->inputs();
|
|
size_t init = 0;
|
|
// Combine the hash values of all inputs to the compute node.
|
|
h = std::accumulate(inputs.begin(), inputs.end(), init, [&hashes](std::size_t hash, const AnfNodePtr &node_in) {
|
|
return hash_combine(hash, hashes[node_in]);
|
|
});
|
|
} else if (node->isa<Parameter>()) {
|
|
// For parameter nodes, use the node's hash.
|
|
h = node->hash();
|
|
} else {
|
|
MS_LOG(ERROR) << "Unknown node type";
|
|
}
|
|
|
|
hashes[node] = h;
|
|
|
|
// Group the node based on its hash value.
|
|
if (groups.find(h) == groups.end()) {
|
|
std::vector<AnfNodePtr> innervec({node});
|
|
groups[h] = innervec;
|
|
order_group.emplace_back(h);
|
|
} else {
|
|
groups[h].push_back(node);
|
|
}
|
|
}
|
|
|
|
// Attempt to replace nodes within each group.
|
|
return DoReplace(manager, order_group, &groups);
|
|
}
|
|
|
|
// This function applies the BuildOrderGroupAndDoReplaceForOneGraph operation for all the function graphs
|
|
// managed by the given manager.
|
|
// @param manager: The manager for all function graphs to process.
|
|
// @return: Whether any of the function graphs were changed.
|
|
bool CSE::BuildOrderGroupAndDoReplace(const FuncGraphManagerPtr manager) const {
|
|
bool changed = false;
|
|
|
|
// Iterate over all function graphs managed by the manager.
|
|
for (FuncGraphPtr fg : manager->func_graphs()) {
|
|
// Attempt to replace nodes for the current function graph and update the 'changed' status.
|
|
changed = BuildOrderGroupAndDoReplaceForOneGraph(fg, manager) || changed;
|
|
}
|
|
|
|
return changed;
|
|
}
|
|
|
|
// Given a list of node groups, this function attempts to replace duplicate nodes within each group.
|
|
// Nodes are considered duplicates if they compute the same values.
|
|
// @param manager: The manager for the function graph.
|
|
// @param order_group: The ordered list of node groups to process.
|
|
// @param groups: The mapping of group hashes to lists of nodes.
|
|
// @return: Whether any nodes were replaced.
|
|
bool CSE::DoReplace(const FuncGraphManagerPtr manager, const std::vector<std::size_t> &order_group,
|
|
mindspore::HashMap<std::size_t, std::vector<AnfNodePtr>> *groups) const {
|
|
bool changes = false;
|
|
std::set<size_t> clear_set;
|
|
|
|
// Iterate over each group.
|
|
for (auto &h : order_group) {
|
|
std::vector<AnfNodePtr> &group = (*groups)[h];
|
|
|
|
// If there are more than 1 node in the group, they might represent the same computation.
|
|
if (group.size() > 1) {
|
|
// Check each node against every other node in the group.
|
|
for (size_t k = 0; k < group.size() - 1; k++) {
|
|
AnfNodePtr main = group[k];
|
|
MS_EXCEPTION_IF_NULL(main);
|
|
|
|
// Skip nodes that have already been replaced or are value nodes.
|
|
if ((k + 1 + clear_set.size() == group.size()) || (k > 0 && main->isa<ValueNode>())) {
|
|
break;
|
|
}
|
|
|
|
if (clear_set.find(k) != clear_set.end()) {
|
|
continue;
|
|
}
|
|
|
|
for (size_t i = k + 1; i < group.size(); i++) {
|
|
auto node = group[i];
|
|
MS_EXCEPTION_IF_NULL(node);
|
|
|
|
if (clear_set.find(i) != clear_set.end()) {
|
|
continue;
|
|
}
|
|
|
|
// Nodes must belong to the same function graph.
|
|
if (main->func_graph() != node->func_graph()) {
|
|
continue;
|
|
}
|
|
|
|
// Check if the nodes are equivalent.
|
|
if (CheckReplace(node, main)) {
|
|
changes = true;
|
|
// Optional: Update debug info or dump flags.
|
|
UpdateDebugInfoAndDumpFlag(main, node);
|
|
// Replace the node.
|
|
(void)manager->Replace(node, main);
|
|
(void)clear_set.insert(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
clear_set.clear();
|
|
}
|
|
}
|
|
|
|
return changes;
|
|
}
|
|
|
|
|
|
bool CSE::Cse(const FuncGraphPtr root, const FuncGraphManagerPtr manager) const {
|
|
MS_EXCEPTION_IF_NULL(manager);
|
|
manager->AddFuncGraph(root);
|
|
return BuildOrderGroupAndDoReplace(manager);
|
|
}
|
|
} // namespace opt
|
|
} // namespace mindspore
|