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.h 4.6 kB

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