You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_saver.cc 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "profiler/device/data_saver.h"
  17. #include <fstream>
  18. #include <numeric>
  19. #include "sys/stat.h"
  20. #include "utils/log_adapter.h"
  21. #include "utils/ms_utils.h"
  22. #include "utils/ms_context.h"
  23. namespace mindspore {
  24. namespace profiler {
  25. OpDetailInfo::OpDetailInfo(std::shared_ptr<OpInfo> op_info, float proportion)
  26. : op_info_(op_info), proportion_(proportion) {
  27. // op_full_name is like 'xxx/xxx/{op_type}-op{node_id}'
  28. op_full_name_ = op_info->op_name;
  29. auto op_type_begin_iter = op_full_name_.rfind('/') + 1;
  30. auto op_type_end_iter = op_full_name_.rfind('-');
  31. op_type_ = op_full_name_.substr(op_type_begin_iter, op_type_end_iter - op_type_begin_iter);
  32. op_name_ = op_full_name_.substr(op_type_begin_iter);
  33. op_avg_time_ = op_info->op_host_cost_time / op_info->op_count;
  34. }
  35. void DataSaver::ParseOpInfo(const OpInfoMap &op_info_maps) {
  36. op_detail_infos_.reserve(op_info_maps.size());
  37. float total_time_sum = GetTotalOpTime(op_info_maps);
  38. for (auto item : op_info_maps) {
  39. op_timestamps_map_[item.first] = item.second.start_duration;
  40. float proportion = item.second.op_host_cost_time / total_time_sum;
  41. auto op_info = std::make_shared<OpInfo>(item.second);
  42. OpDetailInfo op_detail_info = OpDetailInfo(op_info, proportion);
  43. op_detail_infos_.emplace_back(op_detail_info);
  44. AddOpDetailInfoForType(op_detail_info);
  45. }
  46. // update average time of op type
  47. for (auto &op_type : op_type_infos_) {
  48. // device_infos: <type_name, op_type_info>
  49. op_type.second.avg_time_ = op_type.second.total_time_ / op_type.second.count_;
  50. }
  51. MS_LOG(DEBUG) << "Get " << op_detail_infos_.size() << " operation items.";
  52. MS_LOG(DEBUG) << "Get " << op_type_infos_.size() << " operation type items.";
  53. }
  54. void DataSaver::AddOpDetailInfoForType(const OpDetailInfo &op_detail_info) {
  55. // Construct OpType object according to op detail info
  56. OpType op_type = OpType{op_detail_info.op_type_,
  57. op_detail_info.op_info_->op_count,
  58. op_detail_info.op_info_->op_count,
  59. op_detail_info.op_info_->op_host_cost_time,
  60. 0,
  61. op_detail_info.proportion_};
  62. // Set the OpType into op_type_infos_ map
  63. std::string type_name = op_detail_info.op_type_;
  64. auto iter = op_type_infos_.find(type_name);
  65. if (iter == op_type_infos_.end()) {
  66. op_type_infos_.emplace(type_name, op_type);
  67. } else {
  68. iter->second += op_type;
  69. }
  70. }
  71. float DataSaver::GetTotalOpTime(const OpInfoMap &op_info_maps) {
  72. float sum = 0;
  73. sum = std::accumulate(op_info_maps.begin(), op_info_maps.end(), sum,
  74. [](float i, auto iter) { return i + iter.second.op_host_cost_time; });
  75. MS_LOG(DEBUG) << "The total op time is " << sum;
  76. return sum;
  77. }
  78. void DataSaver::WriteOpType(const std::string &saver_base_dir) {
  79. std::string file_path = saver_base_dir + "/" + op_side_ + "_op_type_info_" + device_id_ + ".csv";
  80. std::ofstream ofs(file_path);
  81. // check if the file is writable
  82. if (!ofs.is_open()) {
  83. MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
  84. return;
  85. }
  86. try {
  87. // write op type info into file
  88. if (op_side_ == "cpu") {
  89. ofs << OpType().GetCpuHeader() << std::endl;
  90. for (auto op_type_info : op_type_infos_) {
  91. op_type_info.second.OutputCpuOpTypeInfo(ofs);
  92. }
  93. }
  94. if (op_side_ == "gpu") {
  95. ofs << OpType().GetGpuHeader() << std::endl;
  96. for (auto op_type_info : op_type_infos_) {
  97. op_type_info.second.OutputGpuOpTypeInfo(ofs);
  98. }
  99. }
  100. } catch (const std::exception &e) {
  101. MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
  102. }
  103. ofs.close();
  104. ChangeFileMode(file_path);
  105. MS_LOG(INFO) << "Write " << op_type_infos_.size() << " op type infos into file: " << file_path;
  106. }
  107. void DataSaver::WriteOpDetail(const std::string &saver_base_dir) {
  108. std::string file_path = saver_base_dir + "/" + op_side_ + "_op_detail_info_" + device_id_ + ".csv";
  109. std::ofstream ofs(file_path);
  110. if (!ofs.is_open()) {
  111. MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
  112. return;
  113. }
  114. try {
  115. // write op detail info into file
  116. if (op_side_ == "cpu") {
  117. ofs << OpDetailInfo().GetCpuHeader() << std::endl;
  118. for (auto op_detail : op_detail_infos_) {
  119. op_detail.OutputCpuOpDetailInfo(ofs);
  120. }
  121. }
  122. if (op_side_ == "gpu") {
  123. ofs << OpDetailInfo().GetGpuHeader() << std::endl;
  124. for (auto op_detail : op_detail_infos_) {
  125. op_detail.OutputGpuOpDetailInfo(ofs);
  126. }
  127. }
  128. } catch (const std::exception &e) {
  129. MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
  130. }
  131. ofs.close();
  132. ChangeFileMode(file_path);
  133. MS_LOG(INFO) << "Write " << op_detail_infos_.size() << " op detail infos into file: " << file_path;
  134. }
  135. void DataSaver::WriteOpTimestamp(const std::string &saver_base_dir) {
  136. std::string file_path = saver_base_dir + "/" + op_side_ + "_op_execute_timestamp_" + device_id_ + ".txt";
  137. std::ofstream ofs(file_path);
  138. // check if the file is writable
  139. if (!ofs.is_open()) {
  140. MS_LOG(WARNING) << "Open file '" << file_path << "' failed!";
  141. return;
  142. }
  143. try {
  144. // write op timestamp info into file
  145. for (const auto &op_timestamp_info : op_timestamps_map_) {
  146. if (op_side_ == "cpu") {
  147. ofs << op_timestamp_info.first << ";HostCpuOps;";
  148. } else {
  149. ofs << op_timestamp_info.first << ";GpuOps;";
  150. }
  151. for (auto start_end : op_timestamp_info.second) {
  152. ofs << start_end.start_timestamp << "," << start_end.duration << " ";
  153. }
  154. ofs << std::endl;
  155. }
  156. } catch (const std::exception &e) {
  157. MS_LOG(ERROR) << "Write " << file_path << "failed: " << e.what();
  158. }
  159. ofs.close();
  160. ChangeFileMode(file_path);
  161. }
  162. void DataSaver::ChangeFileMode(const std::string &file_path) {
  163. if (chmod(common::SafeCStr(file_path), S_IRUSR | S_IWUSR) == -1) {
  164. MS_LOG(WARNING) << "Modify file: " << file_path << " to rw fail.";
  165. return;
  166. }
  167. }
  168. } // namespace profiler
  169. } // namespace mindspore