mindspore2022/mindspore/ccsrc/frontend/optimizer/comm_op_attrs.cc

128 lines
5.0 KiB
C++

/**
* Copyright 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 the header file "frontend/optimizer/comm_op_attrs.h" which contains the declarations for communication operator attributes
#include "frontend/optimizer/comm_op_attrs.h"
// Include the <memory> header for smart pointers
#include <memory>
// Include the <vector> header for using vectors
#include <vector>
// Include the <string> header for using strings
#include <string>
// Include the <algorithm> header for using algorithms like sorting
#include <algorithm>
// Include the header file "frontend/parallel/ops_info/ops_utils.h" which contains utility functions for parallel operations
#include "frontend/parallel/ops_info/ops_utils.h"
// Include the header file "frontend/parallel/device_manager.h" which contains the declarations for device management
#include "frontend/parallel/device_manager.h"
// Include the header file "include/common/utils/anfalgo.h" which contains utility functions for ANF (Abstract Neural Network) algorithms
// Start of the `mindspore` namespace
namespace mindspore {
// Start of the `opt` namespace
namespace opt {
// Function to set communication operation attributes in the given function graph
void CommOpAttrs(const FuncGraphPtr &graph) {
// Check if the device manager is initialized
if (parallel::g_device_manager == nullptr) {
// If not initialized, return from the function
return;
}
// Check if the graph is null
MS_EXCEPTION_IF_NULL(graph);
// Get the return node of the graph
AnfNodePtr return_node = graph->get_return();
// Check if the return node is null
MS_EXCEPTION_IF_NULL(return_node);
// Perform topological sorting on all nodes in the graph
std::vector<AnfNodePtr> all_nodes = TopoSort(return_node);
// Iterate over all nodes in the graph
for (auto &node : all_nodes) {
// Check if the node is null
MS_EXCEPTION_IF_NULL(node);
// Check if the node is a CNode
if (!node->isa<CNode>()) {
// If not a CNode, continue to the next node
continue;
}
// Get the primitive of the CNode
auto primitive = GetCNodePrimitive(node);
// Check if the primitive is null
if (primitive == nullptr) {
// If primitive is null, continue to the next node
continue;
}
// Check if the node is a communication operation
if (!common::AnfAlgo::IsCommunicationOp(node)) {
// If not a communication operation, continue to the next node
continue;
}
// Rest of the code for handling communication operation attributes
// ...
}
}
} // End of the `opt` namespace
} // End of the `mindspore` namespace
continue;
}
// Get the primitive of the CNode
auto comm_prim = common::AnfAlgo::GetCNodePrimitive(node);
// Initialize an empty string for the group name
std::string group_name = "";
// Check if the primitive has the attribute "group"
if (comm_prim->HasAttr(parallel::GROUP)) {
// If it does, get the value of the "group" attribute and assign it to the group_name variable
group_name = GetValue<std::string>(comm_prim->GetAttr(parallel::GROUP));
}
// Initialize an empty vector to store the rank list
std::vector<unsigned int> rank_list = {};
// Find the rank list by hash name using the device manager
auto long_rank_list = parallel::g_device_manager->FindRankListByHashName(group_name);
// Convert the long_rank_list to unsigned int and store it in the rank_list vector
(void)std::transform(long_rank_list.begin(), long_rank_list.end(), std::back_inserter(rank_list),
[](int64_t d) -> unsigned int { return IntToUint(LongToInt(d)); });
// Add the rank_list as an attribute to the primitive
(void)comm_prim->AddAttr(kAttrGroupRankIds, MakeValue<std::vector<unsigned int>>(rank_list));
}
}
} // namespace opt
} // namespace mindspore