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 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright 2020 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_DATA_SAVER_H
  17. #define MINDSPORE_DATA_SAVER_H
  18. #include <iostream>
  19. #include <unordered_map>
  20. #include <vector>
  21. #include <string>
  22. #include <memory>
  23. #include "profiler/device/gpu/gpu_profiling.h"
  24. namespace mindspore {
  25. namespace profiler {
  26. namespace gpu {
  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 GetHeader() const {
  37. return "op_side,op_type,op_name,op_full_name,op_occurrences,op_total_time(us),op_avg_time(us),total_proportion,"
  38. "cuda_activity_cost_time(us),cuda_activity_call_count";
  39. }
  40. friend std::ostream &operator<<(std::ostream &os, const OpDetailInfo &event) {
  41. os << "Device," << event.op_type_ << ',' << event.op_name_ << ',' << event.op_full_name_ << ','
  42. << event.op_info_->op_count << ',' << event.op_info_->op_host_cost_time << ',' << event.op_avg_time_ << ','
  43. << event.proportion_ << ',' << event.op_info_->cupti_activity_time << ',' << event.op_info_->op_kernel_count;
  44. return os;
  45. }
  46. };
  47. struct OpType {
  48. std::string op_type_;
  49. int count_{0};
  50. float total_time_{0};
  51. float avg_time_{0};
  52. float proportion_{0};
  53. std::string GetHeader() const { return "op_type,type_occurrences,total_time(us),total_proportion,avg_time(us)"; }
  54. friend std::ostream &operator<<(std::ostream &os, const OpType &event) {
  55. os << event.op_type_ << ',' << event.count_ << ',' << event.total_time_ << ',' << event.proportion_ << ','
  56. << event.avg_time_;
  57. return os;
  58. }
  59. OpType &operator+=(const OpType &other) {
  60. this->count_ += other.count_;
  61. this->total_time_ += other.total_time_;
  62. this->proportion_ += other.proportion_;
  63. return *this;
  64. }
  65. };
  66. struct ActivityData {
  67. std::shared_ptr<Event> basic_info_{nullptr};
  68. std::string block_dim_;
  69. std::string grid_dim_;
  70. int count_{0};
  71. float total_duration_{0};
  72. float avg_duration_{0};
  73. float max_duration_{0};
  74. float min_duration_{0};
  75. ActivityData() = default;
  76. explicit ActivityData(std::shared_ptr<Event> data);
  77. std::string GetHeader() const {
  78. return "name,type,op_full_name,stream_id,block_dim,grid_dim,occurrences,"
  79. "total_duration(us),avg_duration(us),max_duration(us),min_duration(us)";
  80. }
  81. friend std::ostream &operator<<(std::ostream &os, const ActivityData &event) {
  82. os << "\"" << event.basic_info_->kernel_name << "\"," << event.basic_info_->kernel_type << ','
  83. << event.basic_info_->op_name << ',' << event.basic_info_->stream_id << ',' << event.block_dim_ << ','
  84. << event.grid_dim_ << ',' << event.count_ << ',' << event.total_duration_ << ',' << event.avg_duration_ << ','
  85. << event.max_duration_ << ',' << event.min_duration_;
  86. return os;
  87. }
  88. ActivityData &operator+=(const ActivityData &other);
  89. };
  90. using OpInfoMap = std::unordered_map<std::string, OpInfo>;
  91. using DeviceActivityInfos = std::unordered_map<std::string, ActivityData>; // <device_id, ActivityData>
  92. using AllActivityInfos = std::unordered_map<uint32_t, DeviceActivityInfos>; // <device_id, ActivityData>
  93. using OpTypeInfos = std::unordered_map<std::string, OpType>; // <op_full_name, Optype>
  94. using OpDetailInfos = std::vector<OpDetailInfo>;
  95. class DataSaver {
  96. public:
  97. DataSaver() = default;
  98. ~DataSaver() = default;
  99. DataSaver(const DataSaver &) = delete;
  100. DataSaver &operator=(const DataSaver &) = delete;
  101. void ParseOpInfo(const OpInfoMap &op_info_maps);
  102. void ParseEvent(const std::vector<Event> &events);
  103. void WriteFile(std::string out_path);
  104. private:
  105. void AddOpDetailInfoForType(const OpDetailInfo &op_detail_info);
  106. float GetTotalOpTime(const OpInfoMap &op_info_maps);
  107. void AddKernelEvent(const Event &event);
  108. void AddKernelEventToDevice(const Event &event, DeviceActivityInfos *device_activity_infos);
  109. void WriteOpType(const std::string &saver_base_dir);
  110. void WriteOpDetail(const std::string &saver_base_dir);
  111. void WriteActivity(const std::string &saver_base_dir);
  112. std::string device_id_;
  113. AllActivityInfos activity_infos_;
  114. OpTypeInfos op_type_infos_;
  115. OpDetailInfos op_detail_infos_;
  116. };
  117. } // namespace gpu
  118. } // namespace profiler
  119. } // namespace mindspore
  120. #endif // MINDSPORE_DATA_SAVER_H