|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486 |
- /**
- * Copyright 2019-2020 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 "graph/build/stream_allocator.h"
- #include <memory>
- #include "common/ge/ge_util.h"
- #include "framework/common/debug/ge_log.h"
- #include "framework/common/fmk_error_codes.h"
- #include "framework/common/types.h"
- #include "graph/build/logical_stream_allocator.h"
- #include "graph/common/omg_util.h"
- #include "graph/debug/ge_attr_define.h"
- #include "graph/ge_context.h"
- #include "graph/utils/graph_utils.h"
- #include "init/gelib.h"
-
- using std::map;
- using std::set;
- using std::string;
- using std::vector;
-
- namespace {
- const uint32_t kMaxSwitchStreamNum = 1;
- const int64_t kTaskNumPerNormalNode = 3;
- const int64_t kTaskNumPerHcclNode = 200;
- const char *const kTrueStr = "true";
- const char *const kFalseStr = "false";
-
- inline bool HasContinuousStreamLabel(const ge::OpDescPtr &op_desc, std::string &continuous_stream_label) {
- if (ge::AttrUtils::GetStr(op_desc, ge::ATTR_NAME_CONTINUOUS_STREAM_LABEL, continuous_stream_label)) {
- GELOGD("node[%s] get continuous_stream_label %s", op_desc->GetName().c_str(), continuous_stream_label.c_str());
- return true;
- }
- return false;
- }
-
- bool IsHcclOp(const string &op_type) {
- const set<string> hccl_op_types({ge::HCOMBROADCAST, ge::HCOMALLGATHER, ge::HCOMALLREDUCE, ge::HCOMREDUCESCATTER});
- return hccl_op_types.find(op_type) != hccl_op_types.end();
- }
- } // namespace
-
- namespace ge {
- StreamAllocator::StreamAllocator(ComputeGraphPtr whole_graph, const Graph2SubGraphInfoList &subgraphs)
- : whole_graph_(std::move(whole_graph)), subgraphs_(subgraphs) {
- string single_stream_str;
- (void)GetContext().GetOption(ENABLE_SINGLE_STREAM, single_stream_str);
-
- const set<string> stream_options = {"", kTrueStr, kFalseStr};
- if (stream_options.find(single_stream_str) == stream_options.end()) {
- GELOGW("The value %s of the %s option is invalid, it should be true or false.", single_stream_str.c_str(),
- ENABLE_SINGLE_STREAM);
- }
-
- enable_single_stream_ = (single_stream_str == kTrueStr) ? true : false;
- GELOGI("Enable single stream: %s.", enable_single_stream_ ? kTrueStr : kFalseStr);
- }
-
- Status StreamAllocator::AssignLogicalStreams(const std::map<std::string, int> &max_parallel_num, bool hcom_parallel) {
- GELOGI("Assign logical streams start.");
- GE_CHECK_NOTNULL(whole_graph_);
- GE_DUMP(whole_graph_, "BeforeAssignedLogicalStreams");
-
- auto gelib = GELib::GetInstance();
- if (gelib == nullptr) {
- GELOGE(FAILED, "Get GELib instance failed.");
- return FAILED;
- }
-
- const map<string, SchedulerConf> &scheduler_confs = gelib->DNNEngineManagerObj().GetSchedulers();
- LogicalStreamAllocator logical_allocator(scheduler_confs, max_parallel_num);
- logical_allocator.EnableSingleStream(enable_single_stream_);
- logical_allocator.EnableHcomParallel(hcom_parallel);
-
- Status status = logical_allocator.Assign(whole_graph_, subgraphs_, stream_num_);
- if (status != SUCCESS) {
- GELOGE(status, "Assign logical streams failed.");
- return status;
- }
- GE_DUMP(whole_graph_, "AfterAssignedLogicalStreams");
- GELOGI("Assign logical streams success.");
-
- return SUCCESS;
- }
-
- // After allocating the logical stream in the graph, refresh the stream in the
- // graph and insert the synchronization node.
- Status StreamAllocator::RefreshRealStream(int64_t &stream_num, int64_t &event_num) {
- GELOGI("RefreshRealStream start.");
- GE_CHECK_NOTNULL(whole_graph_);
- GE_DUMP(whole_graph_, "BeforeRefreshRealStream");
-
- Status status = AssignSingleStream();
- if (status != SUCCESS) {
- GELOGE(status, "AssignSingleStream failed!");
- return status;
- }
-
- status = SetActiveStreamsByLabel();
- if (status != SUCCESS) {
- GELOGE(status, "SetActiveStreamsByLabel failed!");
- return status;
- }
-
- status = SetActiveStreamsForSubgraphs();
- if (status != SUCCESS) {
- GELOGE(status, "SetActiveStreamsForSubgraphs failed.");
- return status;
- }
-
- status = InsertSyncEvents();
- if (status != SUCCESS) {
- GELOGE(status, "InsertSyncEventId failed!");
- return status;
- }
-
- status = OptimizeSyncEvents();
- if (status != SUCCESS) {
- GELOGE(status, "OptimizeSyncEventId failed!");
- return status;
- }
-
- vector<set<int64_t>> split_streams(stream_num_);
- status = SplitStreams(split_streams);
- if (status != SUCCESS) {
- GELOGE(status, "SplitStreams failed!");
- return status;
- }
-
- status = UpdateActiveStreams(split_streams);
- if (status != SUCCESS) {
- GELOGE(status, "UpdateActiveStreams failed!");
- return status;
- }
-
- status = AddActiveEntryStream();
- if (status != SUCCESS) {
- GELOGE(status, "AddActiveEntryStream failed!");
- return status;
- }
-
- status = RefreshContinuousEvents();
- if (status != SUCCESS) {
- GELOGE(status, "RefreshContinuousEvents failed!");
- return status;
- }
-
- status = InsertSyncEventNodes();
- if (status != SUCCESS) {
- GELOGE(status, "InsertSyncEventNode failed!");
- return status;
- }
-
- DumpEvents();
- GE_DUMP(whole_graph_, "AfterRefreshRealStream");
-
- for (const NodePtr &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- auto stream_id = node->GetOpDesc()->GetStreamId();
- if (stream_id == kInvalidStream) {
- node->GetOpDesc()->SetStreamId(0);
- }
- }
-
- if (stream_num_ == 0) {
- GELOGI("None of nodes need to assign stream, stream num is 0, it will cause error, so change it to 1");
- stream_num_ = 1;
- }
- GELOGI("stream num: %ld, event num: %u.", stream_num_, event_num_);
- GELOGI("RefreshRealStream successfully.");
-
- stream_num = stream_num_;
- event_num = static_cast<int64_t>(event_num_);
-
- return SUCCESS;
- }
-
- Status StreamAllocator::AssignSingleStream() {
- if (!enable_single_stream_) {
- return SUCCESS;
- }
-
- if (stream_num_ > 1) {
- GELOGE(FAILED, "The number of ts streams is %ld, only one is supported.", stream_num_);
- return FAILED;
- }
-
- int64_t task_count = 0;
- for (const NodePtr &node : whole_graph_->GetAllNodes()) {
- string op_type = node->GetType();
- if (IsHcclOp(op_type)) {
- task_count += kTaskNumPerHcclNode;
- } else {
- task_count += kTaskNumPerNormalNode;
- }
- }
-
- uint32_t max_normal_stream_count = 0;
- uint32_t max_normal_task_count = 0;
- Status status = GetMaxStreamAndTask(false, max_normal_stream_count, max_normal_task_count);
- if (status != SUCCESS) {
- GELOGE(status, "Get max task count of normal stream failed.");
- return status;
- }
-
- if (task_count > static_cast<int64_t>(max_normal_task_count)) {
- uint32_t max_huge_stream_count = 0;
- uint32_t max_huge_task_count = 0;
- Status status = GetMaxStreamAndTask(true, max_huge_stream_count, max_huge_task_count);
- if (status == SUCCESS) {
- int64_t huge_stream = 0;
- GELOGI("Use huge stream %ld.", huge_stream);
- huge_streams_.emplace_back(huge_stream);
- } else {
- GELOGW(
- "The estimated task count %ld is greater than the max count of normal stream,"
- " but the huge stream is not supported.",
- task_count);
- }
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::SetActiveStreamsByLabel() {
- for (const auto &node : whole_graph_->GetAllNodes()) {
- OpDescPtr op_desc = node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- string stream_label;
- if (AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
- int64_t stream_id = op_desc->GetStreamId();
- if (stream_id != kInvalidStream) {
- labeled_streams_[stream_label].emplace(stream_id);
- }
- }
- }
-
- for (const auto &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- vector<string> activated_label_list;
- if (!AttrUtils::GetListStr(node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, activated_label_list) ||
- activated_label_list.empty()) {
- continue;
- }
-
- vector<uint32_t> activated_stream_list;
- for (string &activated_label : activated_label_list) {
- specific_activated_labels_[activated_label].emplace(node);
- for (int64_t activated_stream : labeled_streams_[activated_label]) {
- activated_stream_list.push_back(static_cast<uint32_t>(activated_stream));
- specific_activated_streams_.emplace(activated_stream);
- specific_activated_streams_nodes_map_[activated_stream].emplace(node);
- GELOGI("Node %s active stream %ld by %s.", node->GetName().c_str(), activated_stream, activated_label.c_str());
- }
- }
- GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, activated_stream_list),
- GELOGE(FAILED, "SetListInt failed.");
- return FAILED);
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::SetActiveStreamsForSubgraphs() {
- for (auto &subgraph : whole_graph_->GetAllSubgraphs()) {
- GE_CHECK_NOTNULL(subgraph);
- NodePtr first_active_node = nullptr;
-
- // Get all streams in subgraph.
- set<int64_t> subgraph_streams;
- for (auto &node : subgraph->GetDirectNode()) {
- OpDescPtr op_desc = node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- // Skip streams with label
- string stream_label;
- if (AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
- continue;
- }
- int64_t stream_id = op_desc->GetStreamId();
- if (stream_id != kInvalidStream) {
- subgraph_streams.emplace(stream_id);
- GELOGI("Add stream %ld to active_stream_list of node %s of graph %s", stream_id, node->GetName().c_str(),
- subgraph->GetName().c_str());
- }
- bool is_first_active = false;
- if (AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, is_first_active) && is_first_active) {
- first_active_node = node;
- }
- }
-
- if (first_active_node == nullptr) {
- continue;
- }
-
- subgraph_first_active_node_map_[subgraph] = first_active_node;
-
- // Set active streams for StreamActive.
- subgraph_streams.erase(first_active_node->GetOpDesc()->GetStreamId());
-
- vector<uint32_t> active_streams;
- for (int64_t active_stream : subgraph_streams) {
- active_streams.emplace_back(static_cast<uint32_t>(active_stream));
- specific_activated_streams_.emplace(active_stream);
- }
-
- if (!AttrUtils::SetListInt(first_active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
- GELOGE(FAILED, "Set active streams for node %s failed.", first_active_node->GetName().c_str());
- return FAILED;
- }
- }
- return SUCCESS;
- }
-
- // Insert the send/recv event id to the graph
- Status StreamAllocator::InsertSyncEvents() {
- for (const auto &cur_node : whole_graph_->GetAllNodes()) {
- // Take the adjacent points, then judge whether need to insert the event
- for (const OutDataAnchorPtr &anchor : cur_node->GetAllOutDataAnchors()) {
- for (const InDataAnchorPtr &peer_in_anchor : anchor->GetPeerInDataAnchors()) {
- NodePtr next_node = peer_in_anchor->GetOwnerNode();
- Status status = InsertOneEventInTwoNodes(cur_node, next_node);
- if (status != SUCCESS) {
- GELOGE(status, "InsertOneEventInTwoNodes failed!");
- return status;
- }
- }
- }
-
- /// If the two nodes of the control side belong to two streams,
- /// you also need to add the send/recv event.
- if (cur_node->GetOutControlAnchor() != nullptr) {
- for (const AnchorPtr &peer_in_anchor : cur_node->GetOutControlAnchor()->GetPeerAnchors()) {
- NodePtr next_node = peer_in_anchor->GetOwnerNode();
- Status status = InsertOneEventInTwoNodes(cur_node, next_node);
- if (status != SUCCESS) {
- GELOGE(status, "InsertOneEventInTwoNodes failed!");
- return status;
- }
- }
- }
- }
-
- Status status = InsertEventsForSubgraph();
- if (status != SUCCESS) {
- GELOGE(status, "InsertEventsBetweenSubAndParentGraphNodes failed!");
- return status;
- }
-
- return SUCCESS;
- }
-
- // Insert one send/recv event in two nodes
- Status StreamAllocator::InsertOneEventInTwoNodes(const NodePtr &cur_node, const NodePtr &next_node) {
- GE_CHECK_NOTNULL(cur_node->GetOpDesc());
- GE_CHECK_NOTNULL(next_node->GetOpDesc());
-
- // No need to insert events after node that do not assign streams.
- int64_t cur_stream_id = cur_node->GetOpDesc()->GetStreamId();
- if (cur_stream_id == kInvalidStream) {
- GELOGD("No need to insert event after node %s.", cur_node->GetName().c_str());
- return SUCCESS;
- }
-
- // No need to insert events between nodes in the same stream.
- int64_t next_stream_id = next_node->GetOpDesc()->GetStreamId();
- if (cur_stream_id == next_stream_id) {
- return SUCCESS;
- }
-
- if (next_stream_id == kInvalidStream) {
- GELOGE(FAILED, "Stream id of next_node %s should not be %ld", next_node->GetName().c_str(), kInvalidStream);
- return FAILED;
- }
-
- // No event needs to be inserted between the active node and the activated stream.
- string next_node_label;
- if (AttrUtils::GetStr(next_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, next_node_label) && !next_node_label.empty()) {
- auto iter = specific_activated_labels_.find(next_node_label);
- if (iter != specific_activated_labels_.end()) {
- for (const auto &active_node : iter->second) {
- OpDescPtr active_op = active_node->GetOpDesc();
- GE_CHECK_NOTNULL(active_op);
- if ((cur_stream_id == active_op->GetStreamId()) && (cur_node->GetOpDesc()->GetId() <= active_op->GetId())) {
- GELOGI("No need to insert event between node %s and %s.", cur_node->GetName().c_str(),
- next_node->GetName().c_str());
- return SUCCESS;
- }
- }
- }
- }
-
- // Add send and receive events.
- AddSendEventId(cur_node, event_num_);
- AddRecvEventId(next_node, event_num_);
- GELOGD("Insert event %u between node %s(stream %ld) and %s(stream %ld)", event_num_, cur_node->GetName().c_str(),
- cur_stream_id, next_node->GetName().c_str(), next_stream_id);
-
- ++event_num_;
-
- return SUCCESS;
- }
-
- Status StreamAllocator::InsertEventsForSubgraph() {
- for (const auto &subgraph : whole_graph_->GetAllSubgraphs()) {
- GE_CHECK_NOTNULL(subgraph);
- for (const auto &node : subgraph->GetDirectNode()) {
- auto op_desc = node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- bool is_subgraph_end_node = false;
- if (!AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_END_NODE, is_subgraph_end_node) || !is_subgraph_end_node) {
- continue;
- }
- const auto parent_node = subgraph->GetParentNode();
- GE_CHECK_NOTNULL(parent_node);
-
- // Insert events between subgraph end node and parent node's out nodes
- for (const auto &next_node : parent_node->GetOutAllNodes()) {
- Status status = InsertOneEventInTwoNodes(node, next_node);
- if (status != SUCCESS) {
- GELOGE(status, "InsertOneEventInTwoNodes failed!");
- return status;
- }
- }
-
- break;
- }
- }
-
- return SUCCESS;
- }
-
- // Optimize the event in the graph, delete the redundant sync event according to the stream information
- Status StreamAllocator::OptimizeSyncEvents() {
- map<int64_t, vector<NodePtr>> stream_nodes;
-
- for (const auto &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- int64_t stream_id = node->GetOpDesc()->GetStreamId();
- stream_nodes[stream_id].emplace_back(node);
- }
-
- Status status = OptimizeBySendEvents(stream_nodes);
- if (status != SUCCESS) {
- GELOGE(status, "OptimizeBySendEvents failed!");
- return status;
- }
-
- status = OptimizeByRecvEvents(stream_nodes);
- if (status != SUCCESS) {
- GELOGE(status, "OptimizeByRecvEvents failed!");
- return status;
- }
-
- status = OptimizeByStreamActivate();
- if (status != SUCCESS) {
- GELOGE(status, "OptimizeByStreamActivate failed!");
- return status;
- }
- for (auto pair : node_to_send_events_) {
- if (pair.first->GetType() == STREAMSWITCH) {
- for (auto event_id : pair.second) {
- GELOGI("Curren switch node is %s, remove send event_id %d.", pair.first->GetName().c_str(), event_id);
- RmvSendEventId(pair.first, event_id);
- auto recv_node = GetNodeFromRecvEventId(event_id);
- GELOGI("Curren recv_node is %s, remove recv event_id %d.", recv_node->GetName().c_str(), event_id);
- RmvRecvEventId(recv_node, event_id);
- }
- }
- }
- return SUCCESS;
- }
-
- /// Optimization scenario: one stream has multiple send events in one node,
- /// and multiple nodes for recv events on another stream
- /// Example:
- /// Stream0 Stream1
- /// N1 - - - event - > N1
- /// \ |
- /// \ v
- /// - - event - > N2
- Status StreamAllocator::OptimizeBySendEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
- for (const auto &one_pair : stream_nodes) {
- // The nodes on a stream in order
- const vector<NodePtr> &nodes = one_pair.second;
-
- map<NodePtr, uint32_t> send_node_to_event_id;
-
- for (const auto &recv_node_ptr : nodes) {
- GE_CHECK_NOTNULL(recv_node_ptr);
- // Get all recv events of the current node, then traverse the event
- vector<uint32_t> recv_events;
- GetRecvEventIdList(recv_node_ptr, recv_events);
-
- for (const auto &event_id : recv_events) {
- NodePtr send_node_ptr = GetNodeFromSendEventId(event_id);
- GE_CHECK_NOTNULL(send_node_ptr);
-
- /// If the record to the stream is found in the map,
- /// and the recv node is the node, then remove sync event
- if (send_node_to_event_id.find(send_node_ptr) != send_node_to_event_id.end()) {
- RmvSendEventId(send_node_ptr, event_id);
- RmvRecvEventId(recv_node_ptr, event_id);
- GELOGI("Remove event %u between node %s and node %s", event_id, send_node_ptr->GetName().c_str(),
- recv_node_ptr->GetName().c_str());
- } else {
- send_node_to_event_id[send_node_ptr] = event_id;
- }
- }
- }
- }
-
- return SUCCESS;
- }
-
- /// Scenario: multiple send nodes on a stream sent to a single recv node on the destination stream
- /// Example:
- /// Stream0 Stream1
- /// N1 - -
- /// | |
- /// | - - event - - -
- /// | |
- /// V V
- /// N2 - - - event - > N2
- Status StreamAllocator::OptimizeByRecvEvents(const map<int64_t, vector<NodePtr>> &stream_nodes) {
- for (const auto &one_pair : stream_nodes) {
- // The nodes on a stream in order
- const vector<NodePtr> &nodes = one_pair.second;
-
- map<NodePtr, uint32_t> recv_node_to_event_id;
-
- for (const auto &send_node_ptr : nodes) {
- GE_CHECK_NOTNULL(send_node_ptr);
- // Get all send events of the current node, then traverse the event
- vector<uint32_t> send_id_list;
- GetSendEventIdList(send_node_ptr, send_id_list);
-
- for (const auto &event_id : send_id_list) {
- NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
- GE_CHECK_NOTNULL(recv_node_ptr);
-
- /// If the record to the stream is found in the map,
- /// and the send node is the node, then remove sync event
- auto it = recv_node_to_event_id.find(recv_node_ptr);
- if (it != recv_node_to_event_id.end()) {
- uint32_t pre_event_id = it->second;
- NodePtr pre_send_node_ptr = GetNodeFromSendEventId(pre_event_id);
- GE_CHECK_NOTNULL(pre_send_node_ptr);
-
- RmvSendEventId(pre_send_node_ptr, pre_event_id);
- RmvRecvEventId(recv_node_ptr, pre_event_id);
- GELOGI("Remove event %u between node %s and node %s.", event_id, pre_send_node_ptr->GetName().c_str(),
- recv_node_ptr->GetName().c_str());
- }
- recv_node_to_event_id[recv_node_ptr] = event_id;
- }
- }
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::OptimizeByStreamActivate() {
- auto node_to_send_events_temp = node_to_send_events_;
- for (const auto &node_event_id_pair : node_to_send_events_temp) {
- const NodePtr &send_node_ptr = node_event_id_pair.first;
- for (const auto &event_id : node_event_id_pair.second) {
- NodePtr recv_node_ptr = GetNodeFromRecvEventId(event_id);
- GE_CHECK_NOTNULL(recv_node_ptr);
- if (IsRecvNodeActivatedBySendNode(send_node_ptr, recv_node_ptr)) {
- RmvSendEventId(send_node_ptr, event_id);
- RmvRecvEventId(recv_node_ptr, event_id);
- GELOGI("Remove event %u between node %s and node %s.", event_id, send_node_ptr->GetName().c_str(),
- recv_node_ptr->GetName().c_str());
- }
- }
- }
- return SUCCESS;
- }
-
- // In situation : stream(normal) -> stream(streamActivate)->
- // -> stream(streamSwitch) -> stream(streamActivate) -> stream(stream true or false)
- // No need to insert an event between node in stream(normal) and node in stream(stream true or false)
- bool StreamAllocator::IsRecvNodeActivatedBySendNode(const NodePtr &send_node_ptr, const NodePtr &recv_node_ptr) const {
- GE_CHECK_NOTNULL_EXEC(send_node_ptr->GetOpDesc(), GELOGE(FAILED, "op desc is nullptr"); return false);
- GE_CHECK_NOTNULL_EXEC(recv_node_ptr->GetOpDesc(), GELOGE(FAILED, "op desc is nullptr"); return false);
- auto cur_stream_id = send_node_ptr->GetOpDesc()->GetStreamId();
- if (AttrUtils::HasAttr(recv_node_ptr->GetOpDesc(), ATTR_NAME_STREAM_LABEL)) {
- // find streamActivate node
- auto iter = specific_activated_streams_nodes_map_.find(recv_node_ptr->GetOpDesc()->GetStreamId());
- set<NodePtr> activate_stream_nodes;
- if (iter != specific_activated_streams_nodes_map_.end()) {
- activate_stream_nodes = iter->second;
- }
- set<NodePtr> visited_nodes{recv_node_ptr};
- while (!activate_stream_nodes.empty()) {
- set<NodePtr> activate_stream_nodes_temp;
- for (const auto &activate_stream_node : activate_stream_nodes) {
- GE_IF_BOOL_EXEC(activate_stream_node->GetOpDesc() == nullptr, continue);
- if (visited_nodes.find(activate_stream_node) != visited_nodes.end() ||
- AttrUtils::HasAttr(activate_stream_node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE)) {
- return false;
- }
- visited_nodes.insert(activate_stream_node);
- // nodes in stream link to streamActivate no need to add event before activated node
- for (const auto &pre_activate_stream_node : activate_stream_node->GetInNodes()) {
- GE_IF_BOOL_EXEC(pre_activate_stream_node->GetOpDesc() == nullptr, continue);
- if (pre_activate_stream_node->GetOpDesc()->GetStreamId() == cur_stream_id &&
- pre_activate_stream_node->GetOpDesc()->GetId() >= send_node_ptr->GetOpDesc()->GetId()) {
- return true;
- }
- auto in_nodes_of_pre = pre_activate_stream_node->GetInNodes();
- if (std::find(in_nodes_of_pre.begin(), in_nodes_of_pre.end(), send_node_ptr) != in_nodes_of_pre.end()) {
- return true;
- }
- }
- auto iterator = specific_activated_streams_nodes_map_.find(activate_stream_node->GetOpDesc()->GetStreamId());
- if (iterator != specific_activated_streams_nodes_map_.end()) {
- auto active_nodes = iterator->second;
- for (const auto &active_node : active_nodes) {
- activate_stream_nodes_temp.emplace(active_node);
- }
- }
- }
- activate_stream_nodes = activate_stream_nodes_temp;
- }
- }
- return false;
- }
-
- // Split the stream according to the maximum number of nodes in the stream.
- Status StreamAllocator::SplitStreams(vector<set<int64_t>> &split_streams) {
- if (enable_single_stream_ || stream_num_ == 0) {
- GELOGI("The single stream option is enabled or the number of streams is 0, no need to split streams.");
- return SUCCESS;
- }
-
- // stream_node_num_vec records the number of all nodes on each stream
- // added_stream_num_vec records the number of streams that each stream needs to increase
- // new_stream_id_vec records the new physical stream id for each stream
- vector<int64_t> stream_node_num_vec(stream_num_);
- vector<int64_t> added_stream_num_vec(stream_num_);
- vector<int64_t> new_stream_id_vec(stream_num_);
- map<string, int64_t> stream_continuous_2_node_num_map;
- map<string, vector<NodePtr>> stream_continuous_2_nodes_map;
- map<int64_t, vector<NodePtr>> stream_2_nodes_map;
- vector<NodePtr> pre_node_vec(stream_num_);
-
- int64_t last_stream_id = stream_num_ - 1;
- for (auto i = 0; i <= last_stream_id; i++) {
- stream_node_num_vec[i] = 0;
- added_stream_num_vec[i] = 0;
- new_stream_id_vec[i] = i;
- pre_node_vec[i] = nullptr;
- }
-
- uint32_t max_stream_count = 0;
- uint32_t max_task_count = 0;
- GE_CHK_STATUS_RET(GetMaxStreamAndTask(false, max_stream_count, max_task_count),
- "Get max stream and task count failed.");
-
- for (const auto &cur_node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(cur_node);
- auto op_desc = cur_node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- int64_t stream_id = op_desc->GetStreamId();
- if (stream_id == kInvalidStream) {
- continue;
- }
- if (stream_id > last_stream_id) {
- GELOGE(FAILED, "SplitStreams:streamid(%ld) > last_stream_id(%ld)", stream_id, last_stream_id);
- return FAILED;
- }
- AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
- stream_2_nodes_map[stream_id].push_back(cur_node);
- // The maximum number of tasks per stream.
- int64_t max_node_num_one_stream = GetMaxNodeNumPerStream(cur_node, max_task_count);
- std::string continuous_stream_label;
- if (HasContinuousStreamLabel(op_desc, continuous_stream_label)) {
- stream_continuous_2_node_num_map[continuous_stream_label]++;
- // return error
- if (stream_continuous_2_node_num_map[continuous_stream_label] > max_node_num_one_stream) {
- GELOGE(FAILED, "SplitStreams:node[%s] stream_id[%ld] continuous stream label[%s] unsatisfied ",
- op_desc->GetName().c_str(), stream_id, continuous_stream_label.c_str());
- return FAILED;
- }
- stream_continuous_2_nodes_map[continuous_stream_label].push_back(cur_node);
- }
- // Split the stream if it exceeds the maximum number of nodes in the stream.
- if (NeedSpiltNewStream(stream_node_num_vec[stream_id], max_node_num_one_stream, op_desc)) {
- last_stream_id++;
- GELOGI(
- "stream_node_num_vec[%ld]= %ld > max_node_num_one_stream : %ld, "
- "It's time to split the stream, split newly-added stream id is %ld",
- stream_id, stream_node_num_vec[stream_id], max_node_num_one_stream, last_stream_id);
- NodePtr pre_node = pre_node_vec[stream_id];
- stream_node_num_vec[stream_id] = 0;
- AddNodeNum(cur_node, stream_node_num_vec[stream_id]);
- // try spilt a new stream and move same continuous stream label nodes from this stream
- bool not_use_cur = false;
- NodePtr not_cur = nullptr;
- std::string cur_continuous_stream_label;
- if (HasContinuousStreamLabel(op_desc, cur_continuous_stream_label)) {
- // get stored nodes
- auto nodes = stream_continuous_2_nodes_map[cur_continuous_stream_label];
- GE_RETURN_WITH_LOG_IF_FALSE(!nodes.empty(), "split stream with continuous stream label %s failed",
- cur_continuous_stream_label.c_str());
- for (const auto &node : nodes) {
- auto stored_op_desc = node->GetOpDesc();
- GE_CHECK_NOTNULL(stored_op_desc);
- stored_op_desc->SetStreamId(last_stream_id);
- AddNodeNum(node, stream_node_num_vec[stream_id]);
- }
- not_use_cur = true;
- not_cur = nodes.front();
- GE_CHECK_NOTNULL(not_cur);
- GELOGI("split from first node %s with continuous stream label %s", not_cur->GetName().c_str(),
- cur_continuous_stream_label.c_str());
- auto iter = std::find(stream_2_nodes_map[stream_id].begin(), stream_2_nodes_map[stream_id].end(), not_cur);
- GE_RETURN_WITH_LOG_IF_FALSE(
- (iter != stream_2_nodes_map[stream_id].end()) && (iter != stream_2_nodes_map[stream_id].begin()),
- "split stream with continuous stream label %s failed", cur_continuous_stream_label.c_str());
- iter--;
- pre_node = *iter;
- }
-
- added_stream_num_vec[stream_id]++;
- new_stream_id_vec[stream_id] = last_stream_id;
- split_streams[stream_id].emplace(last_stream_id);
- node_split_stream_map_[cur_node] = last_stream_id;
-
- // Add the send/recv event to the first and last nodes of the split stream.
- if (pre_node != nullptr) {
- GE_CHK_STATUS_RET(AddEventId(pre_node, not_cur, cur_node, not_use_cur), "AddEventId failed.");
- }
- }
-
- /// If the split stream num is greater than 1, the node behind the same
- /// stream must reset the new stream id.
- if (added_stream_num_vec[stream_id] >= 1) {
- op_desc->SetStreamId(new_stream_id_vec[stream_id]);
- }
-
- pre_node_vec[stream_id] = cur_node;
- }
-
- if (last_stream_id >= 0) {
- stream_num_ = last_stream_id + 1;
- }
- return SUCCESS;
- }
-
- bool StreamAllocator::NeedSpiltNewStream(int64_t stream_node_num, int64_t max_node_num_one_stream,
- const OpDescPtr &op_desc) const {
- const set<string> label_op_types({LABELSET, LABELGOTO, LABELGOTOEX, LABELSWITCH, LABELSWITCHBYINDEX});
- bool is_first_active_node = false;
- (void)AttrUtils::GetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, is_first_active_node);
- return (stream_node_num > max_node_num_one_stream && op_desc->GetSubgraphInstanceNames().empty() &&
- !is_first_active_node && label_op_types.count(op_desc->GetType()) == 0);
- }
-
- Status StreamAllocator::UpdateActiveStreams(const vector<set<int64_t>> &split_streams) {
- UpdateLabelStreams(split_streams);
-
- for (auto &node : whole_graph_->GetAllNodes()) {
- if ((node->GetType() == STREAMSWITCH) || (node->GetType() == STREAMSWITCHN)) {
- if (InsertActiveNodesAfterSwitch(node) != SUCCESS) {
- GELOGE(FAILED, "Insert active nodes after switch node failed.");
- return FAILED;
- }
- } else {
- vector<uint32_t> active_streams;
- GE_CHECK_NOTNULL(node->GetOpDesc());
- if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
- vector<uint32_t> new_active_streams = active_streams;
- for (const uint32_t logical_stream : active_streams) {
- if (static_cast<size_t>(logical_stream) >= split_streams.size()) {
- GELOGE(FAILED, "logical stream is out of range.");
- return FAILED;
- }
- const set<int64_t> &new_split_streams = split_streams[logical_stream];
- if (!new_split_streams.empty()) {
- for (int64_t split_stream : new_split_streams) {
- new_active_streams.emplace_back(static_cast<uint32_t>(split_stream));
- GELOGI("Add stream %ld to active_stream_list of node %s of graph %s", split_stream,
- node->GetName().c_str(), node->GetOwnerComputeGraph()->GetName().c_str());
- }
- }
- }
- if (!AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, new_active_streams)) {
- GELOGE(FAILED, "Set active streams for node %s failed.", node->GetName().c_str());
- return FAILED;
- }
- }
- }
- }
-
- Status status = UpdateActiveStreamsForSubgraphs();
- if (status != SUCCESS) {
- GELOGE(status, "SetActiveStreamsForSubgraph failed!");
- return status;
- }
-
- status = SetActiveStreamsForLoop();
- if (status != SUCCESS) {
- GELOGE(status, "SetActiveStreamsForLoop failed!");
- return status;
- }
-
- return SUCCESS;
- }
-
- void StreamAllocator::UpdateLabelStreams(const vector<set<int64_t>> &split_streams) {
- for (size_t i = 0; i < split_streams.size(); i++) {
- auto &streams = split_streams[i];
- if (streams.empty()) {
- continue;
- }
- if (specific_activated_streams_.count(static_cast<int64_t>(i)) > 0) {
- specific_activated_streams_.insert(streams.begin(), streams.end());
- }
- for (auto &labeled_stream : labeled_streams_) {
- if (labeled_stream.second.count(static_cast<int64_t>(i)) > 0) {
- labeled_stream.second.insert(streams.begin(), streams.end());
- break;
- }
- }
- }
- }
-
- Status StreamAllocator::InsertActiveNodesAfterSwitch(NodePtr &switch_node) {
- vector<NodePtr> active_nodes;
- if (InsertActiveNodesAfterSwitch(switch_node, active_nodes) != SUCCESS) {
- GELOGE(FAILED, "Insert active nodes after node %s failed.", switch_node->GetName().c_str());
- return FAILED;
- }
- if (active_nodes.empty()) {
- return SUCCESS;
- }
- vector<int64_t> stream_ids;
- for (auto &active_node : active_nodes) {
- GE_CHECK_NOTNULL(active_node->GetOpDesc());
- active_node->GetOpDesc()->SetStreamId(stream_num_);
- stream_ids.emplace_back(stream_num_);
- specific_activated_streams_.emplace(stream_num_);
- stream_num_++;
- }
- auto op_desc = switch_node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
-
- if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, stream_ids)) {
- GELOGE(FAILED, "SetListInt failed.");
- return FAILED;
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::InsertActiveNodesAfterSwitch(NodePtr &switch_node, vector<NodePtr> &active_nodes) {
- GE_CHECK_NOTNULL(switch_node);
- OpDescPtr switch_desc = switch_node->GetOpDesc();
- GE_CHECK_NOTNULL(switch_desc);
- vector<string> ori_active_label_list;
- if (!AttrUtils::GetListStr(switch_desc, ATTR_NAME_ACTIVE_LABEL_LIST, ori_active_label_list) ||
- ori_active_label_list.empty()) {
- GELOGE(INTERNAL_ERROR, "Get active label list of switch %s failed.", switch_node->GetName().c_str());
- return INTERNAL_ERROR;
- }
-
- vector<string> active_label_list;
- vector<NodePtr> added_active_nodes;
- if (AddActiveNodes(switch_node, ori_active_label_list, active_label_list, added_active_nodes) != SUCCESS) {
- GELOGE(FAILED, "Add active nodes after node %s failed.", switch_node->GetName().c_str());
- return FAILED;
- }
-
- if (SetActiveLabelList(switch_node, active_label_list) != SUCCESS) {
- GELOGE(FAILED, "set active label list failed");
- return FAILED;
- }
-
- if (added_active_nodes.empty()) {
- return SUCCESS;
- }
-
- for (auto &active_node : added_active_nodes) {
- GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
- if (switch_node->GetOutControlAnchor()->LinkTo(active_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
- GELOGE(FAILED, "Link %s to %s failed.", switch_node->GetName().c_str(), active_node->GetName().c_str());
- return FAILED;
- }
- active_nodes.emplace_back(active_node);
- }
- return SUCCESS;
- }
-
- Status StreamAllocator::UpdateActiveStreamsForSubgraphs() const {
- // Update active stream list for active nodes
- for (auto &node_stream_pair : node_split_stream_map_) {
- auto node = node_stream_pair.first;
- auto subgraph = node->GetOwnerComputeGraph();
- if (subgraph->GetParentNode() == nullptr) {
- continue;
- }
- // Skip streams with label
- GE_CHECK_NOTNULL(node->GetOpDesc());
- string stream_label;
- if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label) && !stream_label.empty()) {
- continue;
- }
- auto it = subgraph_first_active_node_map_.find(subgraph);
- if (it == subgraph_first_active_node_map_.end()) {
- continue;
- }
- const auto &active_node = it->second;
- GE_CHECK_NOTNULL(active_node);
- auto op_desc = active_node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- vector<uint32_t> active_streams;
- (void)AttrUtils::GetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams);
- set<uint32_t> new_active_streams(active_streams.begin(), active_streams.end());
- new_active_streams.emplace(static_cast<uint32_t>(node_stream_pair.second));
- active_streams.assign(new_active_streams.begin(), new_active_streams.end());
- if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
- GELOGE(FAILED, "Set active streams for node %s failed.", active_node->GetName().c_str());
- return FAILED;
- }
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::SetActiveStreamsForLoop() {
- vector<uint32_t> loop_active_streams;
- for (int64_t stream_id = 0; stream_id < stream_num_; stream_id++) {
- if (specific_activated_streams_.count(stream_id) == 0) {
- loop_active_streams.emplace_back(static_cast<uint32_t>(stream_id));
- }
- }
- // Set the stream that needs to be activated
- for (const auto &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- bool is_loop_active = false;
- if (AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_IS_LOOP_ACTIVE, is_loop_active) && is_loop_active) {
- vector<string> activated_label_list;
- if (!AttrUtils::GetListStr(node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, activated_label_list) ||
- activated_label_list.empty()) {
- GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, loop_active_streams),
- GELOGE(FAILED, "SetListInt failed.");
- return FAILED);
- for (const auto &stream_id : loop_active_streams) {
- GELOGI("Active stream %u for node: %s", stream_id, node->GetName().c_str());
- }
-
- break;
- }
- }
- }
-
- return CheckStreamActived();
- }
-
- Status StreamAllocator::CheckStreamActived() const {
- for (const auto &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- vector<uint32_t> active_streams;
- if (AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
- uint32_t stream_id = static_cast<uint32_t>(node->GetOpDesc()->GetStreamId());
- auto iter = find(active_streams.begin(), active_streams.end(), stream_id);
- if (iter != active_streams.end()) {
- GELOGE(FAILED, "Node %s cannot active its own stream %u.", node->GetName().c_str(), stream_id);
- return FAILED;
- }
- }
- }
-
- return SUCCESS;
- }
-
- // Add active entry stream for special env.
- Status StreamAllocator::AddActiveEntryStream() {
- auto gelib = GELib::GetInstance();
- bool head_stream = (gelib == nullptr) ? false : gelib->HeadStream();
- GELOGI("Configured head stream: %u", head_stream);
- if (!head_stream) {
- return SUCCESS;
- }
-
- // Collect streams active by StreamSwitch/StreamActive node.
- std::set<uint32_t> deactive_stream;
- for (ge::NodePtr &node : whole_graph_->GetAllNodes()) {
- GE_CHECK_NOTNULL(node->GetOpDesc());
- Status ret = CollectDeactiveStream(node->GetOpDesc(), deactive_stream);
- if (ret != SUCCESS) {
- return ret;
- }
- }
-
- // Collect default active stream, Add to active entry stream.
- std::vector<uint32_t> active_stream_list;
- for (int64_t stream_id = 0; stream_id < stream_num_; ++stream_id) {
- if (deactive_stream.count(stream_id) == 0) {
- active_stream_list.push_back(stream_id);
- }
- }
-
- int64_t new_stream_id = stream_num_;
- stream_num_++;
- return InsertActiveEntryStream(active_stream_list, new_stream_id);
- }
-
- // Collect deactive stream from flowctrl op.
- Status StreamAllocator::CollectDeactiveStream(const OpDescPtr &op_desc, std::set<uint32_t> &deactive_streams) const {
- GE_CHECK_NOTNULL(op_desc);
- std::string op_type = op_desc->GetType();
- if (op_type == STREAMSWITCH) {
- std::vector<uint32_t> active_stream_list;
- // If GetListInt fail, active_stream_list is empty.
- (void)ge::AttrUtils::GetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_stream_list);
- if (active_stream_list.size() != kMaxSwitchStreamNum) {
- GELOGE(INTERNAL_ERROR, "Stream num of switch true branch must be %u.", kMaxSwitchStreamNum);
- return INTERNAL_ERROR;
- }
-
- deactive_streams.insert(active_stream_list[0]);
- GELOGI("Flowctrl_op node:%s, flowctrl stream id:%u.", op_desc->GetName().c_str(), active_stream_list[0]);
- } else if (op_type == STREAMACTIVE) {
- if (op_desc->HasAttr(ATTR_NAME_SWITCH_BRANCH_NODE_LABEL)) {
- std::vector<uint32_t> active_stream_list;
- if (!AttrUtils::GetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_stream_list)) {
- GELOGE(INTERNAL_ERROR, "StreamActiveOp get attr ACTIVE_STREAM fail.");
- return INTERNAL_ERROR;
- }
-
- for (uint32_t deactive_stream : active_stream_list) {
- deactive_streams.insert(deactive_stream);
- GELOGI("Flowctrl_op node:%s, flowctrl stream id:%u.", op_desc->GetName().c_str(), deactive_stream);
- }
- }
- }
-
- return SUCCESS;
- }
-
- // Insert StreamActive Op for Entry Stream.
- Status StreamAllocator::InsertActiveEntryStream(const std::vector<uint32_t> &active_streams, int64_t stream_id) {
- string node_name = whole_graph_->GetName() + "_ActiveEntryStream_" + string(STREAMACTIVE);
- OpDescPtr op_desc = ge::MakeShared<OpDesc>(node_name, STREAMACTIVE);
- if (op_desc == nullptr) {
- GELOGE(FAILED, "Failed to new opdesc.");
- return FAILED;
- }
- GELOGI("Create StreamActive op:%s.", op_desc->GetName().c_str());
-
- GE_CHK_BOOL_EXEC(
- AttrUtils::SetListStr(op_desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, std::move(std::vector<std::string>())),
- GELOGE(FAILED, "SetListStr failed.");
- return FAILED);
-
- NodePtr active_node = whole_graph_->AddNodeFront(op_desc);
- GE_IF_BOOL_EXEC(active_node == nullptr,
- GELOGE(FAILED, "Create StreamActive op: %s failed.", op_desc->GetName().c_str());
- return INTERNAL_ERROR);
- GE_CHECK_NOTNULL(active_node->GetOpDesc());
- // Add one stream for ActiveEntryStream Task.
- active_node->GetOpDesc()->SetStreamId(stream_id);
-
- GE_CHK_BOOL_EXEC(AttrUtils::SetBool(op_desc, "is_aicpu_stream", true), GELOGE(FAILED, "SetBool failed.");
- return FAILED);
- GE_CHK_BOOL_EXEC(AttrUtils::SetListInt(active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams),
- GELOGE(FAILED, "SetListInt failed.");
- return FAILED);
-
- std::vector<std::string> group_names;
- GE_CHK_BOOL_EXEC(AttrUtils::SetListStr(active_node->GetOpDesc(), ATTR_NAME_SWITCH_BRANCH_NODE_LABEL, group_names),
- GELOGE(FAILED, "SetLisStr failed.");
- return FAILED);
-
- return SUCCESS;
- }
-
- // Refresh events to continuous events
- Status StreamAllocator::RefreshContinuousEvents() {
- // Establish a mapping relationship from old to new event id
- map<uint32_t, uint32_t> old_to_new_events;
- uint32_t new_event_id = 0;
- for (const auto &one_pair : node_to_send_events_) {
- for (const auto &event_id : one_pair.second) {
- old_to_new_events[event_id] = new_event_id;
- new_event_id++;
- }
- }
-
- // Refresh send event id
- for (auto &one_pair : node_to_send_events_) {
- vector<uint32_t> &send_events = one_pair.second;
- for (size_t i = 0; i < send_events.size(); i++) {
- auto find_it = old_to_new_events.find(send_events[i]);
- if (find_it == old_to_new_events.end()) {
- GELOGE(FAILED, "RefreshContinuousEvents: invalid send event %u", send_events[i]);
- return FAILED;
- }
- send_events[i] = find_it->second;
- }
- }
-
- // Refresh recv event id
- for (auto &one_pair : node_to_recv_events_) {
- vector<uint32_t> &recv_events = one_pair.second;
- for (size_t i = 0; i < recv_events.size(); i++) {
- auto find_it = old_to_new_events.find(recv_events[i]);
- if (find_it == old_to_new_events.end()) {
- GELOGE(FAILED, "RefreshContinuousEvents: invalid recv event %u", recv_events[i]);
- return FAILED;
- }
- recv_events[i] = find_it->second;
- }
- }
-
- event_num_ = static_cast<uint32_t>(old_to_new_events.size());
-
- return SUCCESS;
- }
-
- // Insert the real send/recv node in the graph
- Status StreamAllocator::InsertSyncEventNodes() {
- for (const auto &node : whole_graph_->GetAllNodes()) {
- // Add the node corresponding to the recv event
- vector<uint32_t> recv_event_id_list;
- GetRecvEventIdList(node, recv_event_id_list);
- GE_CHECK_NOTNULL(node->GetOpDesc());
- GE_CHECK_NOTNULL(node->GetInControlAnchor());
- GE_CHECK_NOTNULL(node->GetOutControlAnchor());
- for (auto &event_id : recv_event_id_list) {
- string recv_node_name = whole_graph_->GetName() + "_Recv_" + to_string(event_id);
- OpDescPtr op_desc_ptr = MakeShared<OpDesc>(recv_node_name, RECV);
- GE_CHECK_NOTNULL(op_desc_ptr);
-
- int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
- op_desc_ptr->SetStreamId(temp_stream_id);
- GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, RECV_ATTR_EVENT_ID, event_id), GELOGE(FAILED, "SetInt failed.");
- return FAILED);
- (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
- std::move(std::vector<std::string>()));
- NodePtr recv_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
- GE_CHECK_NOTNULL(recv_node);
- GE_CHECK_NOTNULL(recv_node->GetOutControlAnchor());
- Status status = GraphUtils::AddEdge(recv_node->GetOutControlAnchor(), node->GetInControlAnchor());
- if (status != SUCCESS) {
- GELOGE(status, "Add edge for node %s and node %s failed.", recv_node->GetName().c_str(),
- node->GetName().c_str());
- return status;
- }
-
- GELOGI("Insert recv event %u before node: %s", event_id, node->GetName().c_str());
- }
-
- // Add the node corresponding to the send event
- vector<uint32_t> send_event_id_list;
- GetSendEventIdList(node, send_event_id_list);
-
- for (auto &event_id : send_event_id_list) {
- string send_node_name = whole_graph_->GetName() + "_Send_" + to_string(event_id);
- OpDescPtr op_desc_ptr = MakeShared<OpDesc>(send_node_name, SEND);
- GE_CHECK_NOTNULL(op_desc_ptr);
-
- int64_t temp_stream_id = node->GetOpDesc()->GetStreamId();
- op_desc_ptr->SetStreamId(temp_stream_id);
- GE_CHK_BOOL_EXEC(AttrUtils::SetInt(op_desc_ptr, SEND_ATTR_EVENT_ID, event_id), GELOGE(FAILED, "SetInt failed.");
- return FAILED);
- (void)AttrUtils::SetListStr(op_desc_ptr, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES,
- std::move(std::vector<std::string>()));
- NodePtr send_node = node->GetOwnerComputeGraph()->AddNode(op_desc_ptr);
- GE_CHECK_NOTNULL(send_node);
- GE_CHECK_NOTNULL(send_node->GetInControlAnchor());
- Status status = GraphUtils::AddEdge(node->GetOutControlAnchor(), send_node->GetInControlAnchor());
- if (status != SUCCESS) {
- GELOGE(status, "Add edge for node %s and node %s failed.", node->GetName().c_str(),
- send_node->GetName().c_str());
- return status;
- }
-
- GELOGI("Insert send event %u after node: %s", event_id, node->GetName().c_str());
- }
- }
-
- Status status = ReorderEventNodes();
- if (status != SUCCESS) {
- GELOGE(status, "Graph ReorderEventNodes failed");
- return status;
- }
-
- return SUCCESS;
- }
-
- Status StreamAllocator::ReorderEventNodes() const {
- Status status = whole_graph_->InsertEventNodes();
- if (status != SUCCESS) {
- GELOGE(status, "Whole graph InsertEventNodes failed");
- return status;
- }
- for (const auto &subgraph : whole_graph_->GetAllSubgraphs()) {
- status = subgraph->InsertEventNodes();
- if (status != SUCCESS) {
- GELOGE(status, "Subgraph %s InsertEventNodes failed", subgraph->GetName().c_str());
- return status;
- }
- }
- return SUCCESS;
- }
-
- void StreamAllocator::DumpEvents() {
- map<int64_t, vector<NodePtr>> after_refresh_stream_nodes;
- for (const auto &node : whole_graph_->GetAllNodes()) {
- GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, continue);
- int64_t stream_id = node->GetOpDesc()->GetStreamId();
- after_refresh_stream_nodes[stream_id].emplace_back(node);
- }
-
- for (const auto &one_pair : after_refresh_stream_nodes) {
- int64_t stream_id = one_pair.first;
- GELOGI("After RefreshRealStream: stream %ld.", stream_id);
-
- for (const auto &node : one_pair.second) {
- string send_event_str;
- for (const auto &send_event_id : node_to_send_events_[node]) {
- send_event_str += " " + to_string(send_event_id);
- }
- if (!send_event_str.empty()) {
- GELOGI("node: %s, send events: %s", node->GetName().c_str(), send_event_str.c_str());
- }
-
- string recv_event_str;
- for (const auto &recv_event_id : node_to_recv_events_[node]) {
- recv_event_str += " " + to_string(recv_event_id);
- }
- if (!recv_event_str.empty()) {
- GELOGI("node: %s, recv events: %s", node->GetName().c_str(), recv_event_str.c_str());
- }
- }
- }
- }
-
- Status StreamAllocator::GetMaxStreamAndTask(bool huge_stream, uint32_t &max_stream_count, uint32_t &max_task_count) {
- const char *buffer_optimize_on = std::getenv("BUFFER_OPTIMIZE_ON");
- if (buffer_optimize_on != nullptr) {
- rtError_t ret = rtSetPlatformType(PLATFORM_MINI_V1);
- if (ret != RT_ERROR_NONE) {
- GELOGE(FAILED, "Get max stream and task count by rts failed.");
- return FAILED;
- }
- }
-
- uint32_t stream_type = RT_NORMAL_STREAM;
- if (huge_stream) {
- stream_type = RT_HUGE_STREAM;
- }
- rtError_t ret = rtGetMaxStreamAndTask(stream_type, &max_stream_count, &max_task_count);
- if (ret != RT_ERROR_NONE) {
- GELOGE(FAILED, "Get max stream and task count by rts failed.");
- return FAILED;
- }
- GELOGI("Allowed max stream count: %u, max task count per stream: %u.", max_stream_count, max_task_count);
-
- return SUCCESS;
- }
-
- int64_t StreamAllocator::GetMaxNodeNumPerStream(const NodePtr &node, uint32_t max_task_count) {
- int64_t max_node_num_one_stream = static_cast<int64_t>(max_task_count);
- string op_type = node->GetType();
- if (IsHcclOp(op_type)) {
- max_node_num_one_stream /= kTaskNumPerHcclNode;
- } else {
- max_node_num_one_stream /= kTaskNumPerNormalNode;
- }
- if (max_node_num_one_stream == 0) {
- max_node_num_one_stream = 1;
- }
-
- return max_node_num_one_stream;
- }
-
- void StreamAllocator::AddNodeNum(const NodePtr &node, int64_t &node_num) {
- node_num++;
- vector<uint32_t> events;
- GetSendEventIdList(node, events);
- node_num += static_cast<int64_t>(events.size());
- GetRecvEventIdList(node, events);
- node_num += static_cast<int64_t>(events.size());
- }
-
- // Insert send event id on a node
- void StreamAllocator::AddSendEventId(const NodePtr &node, uint32_t event_id) {
- node_to_send_events_[node].emplace_back(event_id);
- }
-
- // Insert recv event id on a node
- void StreamAllocator::AddRecvEventId(const NodePtr &node, uint32_t event_id) {
- node_to_recv_events_[node].emplace_back(event_id);
- }
-
- // Remove send event id from a node
- void StreamAllocator::RmvSendEventId(const NodePtr &node, uint32_t event_id) {
- auto find_it = node_to_send_events_.find(node);
- if (find_it == node_to_send_events_.end()) {
- return;
- }
-
- vector<uint32_t> &send_events = find_it->second;
- for (auto it = send_events.begin(); it != send_events.end(); ++it) {
- if (*it == event_id) {
- send_events.erase(it);
- return;
- }
- }
- }
-
- // Remove recv event id from a node
- void StreamAllocator::RmvRecvEventId(const NodePtr &node, uint32_t event_id) {
- auto find_it = node_to_recv_events_.find(node);
- if (find_it == node_to_recv_events_.end()) {
- return;
- }
-
- vector<uint32_t> &recv_events = find_it->second;
- for (auto it = recv_events.begin(); it != recv_events.end(); ++it) {
- if (*it == event_id) {
- recv_events.erase(it);
- return;
- }
- }
- }
-
- // Get send event id list from a node
- void StreamAllocator::GetSendEventIdList(const NodePtr &node, vector<uint32_t> &send_list) const {
- send_list.clear();
- auto find_it = node_to_send_events_.find(node);
- if (find_it != node_to_send_events_.end()) {
- send_list = find_it->second;
- }
- }
-
- // Get recv event id list from a node
- void StreamAllocator::GetRecvEventIdList(const NodePtr &node, vector<uint32_t> &recv_list) const {
- recv_list.clear();
- auto find_it = node_to_recv_events_.find(node);
- if (find_it != node_to_recv_events_.end()) {
- recv_list = find_it->second;
- }
- }
-
- // Get a specific send node according to the recv event
- NodePtr StreamAllocator::GetNodeFromSendEventId(uint32_t send_event_id) const {
- for (const auto &one_pair : node_to_send_events_) {
- const vector<uint32_t> &events = one_pair.second;
- for (const auto &event_id : events) {
- if (event_id == send_event_id) {
- return one_pair.first;
- }
- }
- }
-
- return nullptr;
- }
-
- // Get a specific recv node according to the recv event
- NodePtr StreamAllocator::GetNodeFromRecvEventId(uint32_t recv_event_id) const {
- for (const auto &one_pair : node_to_recv_events_) {
- const vector<uint32_t> &events = one_pair.second;
- for (const auto &event_id : events) {
- if (event_id == recv_event_id) {
- return one_pair.first;
- }
- }
- }
-
- return nullptr;
- }
-
- Status StreamAllocator::AddEventId(const NodePtr &pre_node, const NodePtr ¬_cur, const NodePtr &cur_node,
- bool not_use_cur) {
- GELOGI("Add send event %u for node %s", event_num_, pre_node->GetName().c_str());
- AddSendEventId(pre_node, event_num_);
- if (not_use_cur) {
- GE_CHECK_NOTNULL(not_cur);
- GELOGI("Add recv event %u for node %s", event_num_, not_cur->GetName().c_str());
- AddRecvEventId(not_cur, event_num_);
- } else {
- GELOGI("Add recv event %u for node %s", event_num_, cur_node->GetName().c_str());
- AddRecvEventId(cur_node, event_num_);
- }
- ++event_num_;
- return SUCCESS;
- }
-
- Status StreamAllocator::AddActiveNodes(NodePtr &switch_node, const vector<string> &ori_active_label_list,
- vector<string> &active_label_list, vector<NodePtr> &added_active_nodes) {
- size_t label_num = ori_active_label_list.size();
- for (size_t i = 0; i < label_num; i++) {
- const string &active_label = ori_active_label_list[i];
- if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
- GELOGE(FAILED, "can not find stream label %s", active_label.c_str());
- return FAILED;
- }
- if (labeled_streams_[active_label].size() <= 1) {
- active_label_list.emplace_back(active_label);
- continue;
- }
-
- string name = switch_node->GetName() + "_" + STREAMACTIVE + "_" + std::to_string(i);
- GELOGI("Create StreamActive op %s after node %s.", name.c_str(), switch_node->GetName().c_str());
- OpDescPtr active_op_desc = MakeShared<OpDesc>(name, STREAMACTIVE);
- GE_CHECK_NOTNULL(active_op_desc);
- NodePtr active_node = whole_graph_->AddNode(active_op_desc);
- GE_CHECK_NOTNULL(active_node);
-
- for (NodePtr &node : switch_node->GetOutControlNodes()) {
- OpDescPtr op_desc = node->GetOpDesc();
- GE_CHECK_NOTNULL(op_desc);
- string stream_label;
- // If GetStr failed, stream_label is empty.
- (void)AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label);
- if (stream_label != active_label) {
- continue;
- }
- GE_CHECK_NOTNULL(switch_node->GetOutControlAnchor());
- if (switch_node->GetOutControlAnchor()->Unlink(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
- GELOGE(FAILED, "Unlink %s to %s failed.", switch_node->GetName().c_str(), node->GetName().c_str());
- return FAILED;
- }
- GE_CHECK_NOTNULL(active_node->GetOutControlAnchor());
- if (active_node->GetOutControlAnchor()->LinkTo(node->GetInControlAnchor()) != GRAPH_SUCCESS) {
- GELOGE(FAILED, "Link %s to %s failed.", active_node->GetName().c_str(), node->GetName().c_str());
- return FAILED;
- }
- }
-
- if (SetSwitchBranchNodeLabel(active_node, name) != SUCCESS) {
- GELOGE(FAILED, "Set switch branch node label failed.");
- return FAILED;
- }
- if (SetStreamLabel(active_node, name) != SUCCESS) {
- GELOGE(FAILED, "Set stream label failed.");
- return FAILED;
- }
- if (SetActiveLabelList(active_node, {active_label}) != SUCCESS) {
- GELOGE(FAILED, "Set active label list failed.");
- return FAILED;
- }
- if (SetActiveStreamList(active_node, active_label) != SUCCESS) {
- GELOGE(FAILED, "Set active stream list failed.");
- return FAILED;
- }
-
- added_active_nodes.emplace_back(active_node);
- active_label_list.emplace_back(name);
- }
- return SUCCESS;
- }
-
- Status StreamAllocator::SetActiveStreamList(NodePtr &active_node, const string &active_label) {
- if (labeled_streams_.find(active_label) == labeled_streams_.end()) {
- GELOGE(FAILED, "Can not find stream label %s.", active_label.c_str());
- return FAILED;
- }
- set<int64_t> &streams = labeled_streams_[active_label];
- vector<int64_t> active_streams(streams.begin(), streams.end());
- if (!AttrUtils::SetListInt(active_node->GetOpDesc(), ATTR_NAME_ACTIVE_STREAM_LIST, active_streams)) {
- GELOGE(FAILED, "SetListInt of %s failed.", ATTR_NAME_ACTIVE_STREAM_LIST.c_str());
- return FAILED;
- }
- return SUCCESS;
- }
- } // namespace ge
|