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.3 kB

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