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 17 #ifndef MINDSPORE_CCSRC_PROFILER_DEVICE_DATA_SAVER_H 18 #define MINDSPORE_CCSRC_PROFILER_DEVICE_DATA_SAVER_H 19 #include <iostream> 20 #include <algorithm> 21 #include <unordered_map> 22 #include <vector> 23 #include <string> 24 #include <memory> 25 #include "profiler/device/profiling.h" 26 #include "utils/log_adapter.h" 27 namespace mindspore { 28 namespace profiler { 29 struct OpDetailInfo { 30 std::string op_type_; 31 std::string op_name_; 32 std::string op_full_name_; 33 std::shared_ptr<OpInfo> op_info_{nullptr}; 34 float op_avg_time_{0}; 35 float proportion_{0}; 36 37 OpDetailInfo() = default; 38 OpDetailInfo(const std::shared_ptr<OpInfo> op_info, float proportion); 39 GetCpuHeaderOpDetailInfo40 std::string GetCpuHeader() const { 41 return "op_side,op_type,op_name,full_op_name,op_occurrences,op_total_time(ms)," 42 "op_avg_time(ms),total_proportion,subgraph,pid"; 43 } GetGpuHeaderOpDetailInfo44 std::string GetGpuHeader() const { 45 return "op_side,op_type,op_name,op_full_name,op_occurrences,op_total_time(us),op_avg_time(us),total_proportion," 46 "cuda_activity_cost_time(us),cuda_activity_call_count"; 47 } 48 OutputCpuOpDetailInfoOpDetailInfo49 void OutputCpuOpDetailInfo(std::ostream &os) const { 50 os << "Host," << op_type_ << ',' << op_name_ << ',' << op_full_name_ << ',' << op_info_->op_count << ',' 51 << op_info_->op_host_cost_time << ',' << op_avg_time_ << ',' << proportion_ << ",Default," << op_info_->pid 52 << std::endl; 53 } 54 OutputGpuOpDetailInfoOpDetailInfo55 void OutputGpuOpDetailInfo(std::ostream &os) const { 56 os << "Device," << op_type_ << ',' << op_name_ << ',' << op_full_name_ << ',' << op_info_->op_count << ',' 57 << op_info_->op_host_cost_time << ',' << op_avg_time_ << ',' << proportion_ << ',' 58 << op_info_->cupti_activity_time << ',' << op_info_->op_kernel_count << std::endl; 59 } 60 }; 61 62 struct OpType { 63 std::string op_type_; 64 int count_{0}; 65 int step_{0}; 66 float total_time_{0}; 67 float avg_time_{0}; 68 float proportion_{0}; 69 GetCpuHeaderOpType70 std::string GetCpuHeader() const { 71 return "op_type,type_occurrences,execution_frequency(per-step)," 72 "total_compute_time,avg_time(ms),percent"; 73 } GetGpuHeaderOpType74 std::string GetGpuHeader() const { return "op_type,type_occurrences,total_time(us),total_proportion,avg_time(us)"; } 75 OutputCpuOpTypeInfoOpType76 void OutputCpuOpTypeInfo(std::ostream &os) const { 77 if (step_ == 0) { 78 MS_LOG(ERROR) << "The run step can not be 0."; 79 return; 80 } 81 if (count_ == 0) { 82 MS_LOG(ERROR) << "The num of operation type can not be 0."; 83 return; 84 } 85 os << op_type_ << ',' << count_ << ',' << count_ / step_ << ',' << total_time_ << ',' << total_time_ / count_ << ',' 86 << proportion_ << std::endl; 87 } 88 OutputGpuOpTypeInfoOpType89 void OutputGpuOpTypeInfo(std::ostream &os) const { 90 os << op_type_ << ',' << count_ << ',' << total_time_ << ',' << proportion_ << ',' << avg_time_ << std::endl; 91 } 92 93 OpType &operator+=(const OpType &other) { 94 this->count_ += other.count_; 95 this->total_time_ += other.total_time_; 96 this->proportion_ += other.proportion_; 97 return *this; 98 } 99 }; 100 101 using OpTimestampInfo = std::unordered_map<std::string, std::vector<StartDuration>>; // <op_full_name, StartDuration> 102 using OpInfoMap = std::unordered_map<std::string, OpInfo>; 103 using OpTypeInfos = std::unordered_map<std::string, OpType>; // <op_full_name, Optype> 104 using OpDetailInfos = std::vector<OpDetailInfo>; 105 106 class DataSaver { 107 public: 108 DataSaver() = default; 109 110 virtual ~DataSaver() = default; 111 112 void ParseOpInfo(const OpInfoMap &op_info_maps); 113 114 OpTimestampInfo op_timestamps_map_; 115 116 protected: 117 void AddOpDetailInfoForType(const OpDetailInfo &op_detail_info); 118 119 float GetTotalOpTime(const OpInfoMap &op_info_maps) const; 120 121 void WriteOpType(const std::string &saver_base_dir) const; 122 123 void WriteOpDetail(const std::string &saver_base_dir) const; 124 125 void WriteOpTimestamp(const std::string &saver_base_dir) const; 126 127 void ChangeFileMode(const std::string &file_path) const; 128 129 OpTypeInfos op_type_infos_; 130 OpDetailInfos op_detail_infos_; 131 std::string op_side_; 132 std::string device_id_; 133 }; 134 } // namespace profiler 135 } // namespace mindspore 136 137 #endif // MINDSPORE_CCSRC_PROFILER_DEVICE_DATA_SAVER_H 138