1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef MINDSPORE_CCSRC_DEBUG_RDR_STREAM_EXEC_ORDER_RECORDER_H_ 17 #define MINDSPORE_CCSRC_DEBUG_RDR_STREAM_EXEC_ORDER_RECORDER_H_ 18 #include <vector> 19 #include <string> 20 #include <memory> 21 #include <utility> 22 #include "nlohmann/json.hpp" 23 #include "backend/session/anf_runtime_algorithm.h" 24 #include "debug/rdr/base_recorder.h" 25 26 using json = nlohmann::json; 27 28 namespace mindspore { 29 class ExecNode { 30 public: ExecNode()31 ExecNode() {} ExecNode(const size_t index,const std::string & node_name,const uint32_t & logic_id,const uint32_t & stream_id,const std::string & node_info)32 ExecNode(const size_t index, const std::string &node_name, const uint32_t &logic_id, const uint32_t &stream_id, 33 const std::string &node_info) 34 : index_(index), node_name_(node_name), logic_id_(logic_id), stream_id_(stream_id), node_info_(node_info) {} ~ExecNode()35 ~ExecNode() {} SetEventId(const uint32_t & event_id)36 void SetEventId(const uint32_t &event_id) { event_id_ = event_id; } SetLabelId(const uint32_t & label_id)37 void SetLabelId(const uint32_t &label_id) { label_ids_.push_back(label_id); } SetActiveStreamId(const uint32_t & active_stream_id)38 void SetActiveStreamId(const uint32_t &active_stream_id) { active_stream_ids_.push_back(active_stream_id); } 39 json ExecNode2Json() const; 40 41 private: 42 size_t index_; 43 std::string node_name_; 44 uint32_t logic_id_; 45 uint32_t stream_id_; 46 std::string node_info_; 47 uint32_t event_id_{0}; 48 std::vector<uint32_t> label_ids_; 49 std::vector<uint32_t> active_stream_ids_; 50 }; 51 using ExecNodePtr = std::shared_ptr<ExecNode>; 52 53 class CNode; 54 using CNodePtr = std::shared_ptr<CNode>; 55 class StreamExecOrderRecorder : public BaseRecorder { 56 public: StreamExecOrderRecorder()57 StreamExecOrderRecorder() : BaseRecorder() {} StreamExecOrderRecorder(const std::string & module,const std::string & name,const std::vector<CNodePtr> & exec_order)58 StreamExecOrderRecorder(const std::string &module, const std::string &name, const std::vector<CNodePtr> &exec_order) 59 : BaseRecorder(module, name) { 60 // Extract information from execute order. 61 for (size_t i = 0; i < exec_order.size(); i++) { 62 CNodePtr cur_cnode_ptr = exec_order[i]; 63 MS_EXCEPTION_IF_NULL(cur_cnode_ptr); 64 65 ExecNode exec_node = 66 ExecNode(i, cur_cnode_ptr->fullname_with_scope(), AnfAlgo::GetStreamDistinctionLabel(cur_cnode_ptr.get()), 67 AnfAlgo::GetStreamId(cur_cnode_ptr), cur_cnode_ptr->DebugString()); 68 69 if (AnfAlgo::HasNodeAttr(kAttrEventId, cur_cnode_ptr)) { 70 exec_node.SetEventId(AnfAlgo::GetNodeAttr<uint32_t>(cur_cnode_ptr, kAttrEventId)); 71 } 72 73 if (AnfAlgo::HasNodeAttr(kAttrLabelIndex, cur_cnode_ptr)) { 74 exec_node.SetLabelId(AnfAlgo::GetNodeAttr<uint32_t>(cur_cnode_ptr, kAttrLabelIndex)); 75 } 76 77 if (AnfAlgo::HasNodeAttr(kAttrLabelSwitchList, cur_cnode_ptr)) { 78 auto label_list = AnfAlgo::GetNodeAttr<std::vector<uint32_t>>(cur_cnode_ptr, kAttrLabelSwitchList); 79 for (size_t j = 0; j < label_list.size(); ++j) { 80 exec_node.SetLabelId(label_list[j]); 81 } 82 } 83 84 std::string active_stream_str; 85 if (AnfAlgo::HasNodeAttr(kAttrActiveStreamList, cur_cnode_ptr)) { 86 auto stream_list = AnfAlgo::GetNodeAttr<std::vector<uint32_t>>(cur_cnode_ptr, kAttrActiveStreamList); 87 for (size_t j = 0; j < stream_list.size(); ++j) { 88 exec_node.SetActiveStreamId(stream_list[j]); 89 } 90 } 91 ExecNodePtr exec_node_ptr = std::make_shared<ExecNode>(exec_node); 92 exec_order_.push_back(std::move(exec_node_ptr)); 93 } 94 } ~StreamExecOrderRecorder()95 ~StreamExecOrderRecorder() {} 96 void Export() override; 97 98 private: 99 std::vector<ExecNodePtr> exec_order_; 100 }; 101 using StreamExecOrderRecorderPtr = std::shared_ptr<StreamExecOrderRecorder>; 102 } // namespace mindspore 103 #endif // MINDSPORE_CCSRC_DEBUG_RDR_STREAM_EXEC_ORDER_RECORDER_H_ 104