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_dump_parser.cc 7.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "debug/data_dump_parser.h"
  17. #include <fstream>
  18. #include "utils/context/ms_context.h"
  19. #include "debug/common.h"
  20. static constexpr auto kDataDumpConfigPtah = "DATA_DUMP_CONFIG_PATH";
  21. static constexpr auto kEnableDataDump = "ENABLE_DATA_DUMP";
  22. static constexpr auto kDataDumpPath = "DATA_DUMP_PATH";
  23. static constexpr auto kConfigDumpMode = "dump_mode";
  24. static constexpr auto kConfigOpDebugMode = "op_debug_mode";
  25. static constexpr auto kConfigNetName = "net_name";
  26. static constexpr auto kConfigIteration = "iteration";
  27. static constexpr auto kConfigKernels = "kernels";
  28. namespace mindspore {
  29. void DataDumpParser::ResetParam() {
  30. enable_ = false;
  31. net_name_.clear();
  32. dump_mode_ = 0;
  33. dump_step_ = 0;
  34. kernel_map_.clear();
  35. }
  36. bool DataDumpParser::DumpEnabled() const {
  37. auto enable_dump = std::getenv(kEnableDataDump);
  38. if (enable_dump == nullptr) {
  39. MS_LOG(INFO) << "[DataDump] enable dump is null. Please export ENABLE_DATA_DUMP";
  40. return false;
  41. }
  42. auto enabled = std::atoi(enable_dump);
  43. if (enabled != 1) {
  44. MS_LOG(WARNING) << "[DataDump] Please export ENABLE_DATA_DUMP=1";
  45. return false;
  46. }
  47. auto context = MsContext::GetInstance();
  48. MS_EXCEPTION_IF_NULL(context);
  49. if (context->execution_mode() == kPynativeMode) {
  50. MS_LOG(EXCEPTION) << "[DataDump] PyNative mode not support data dump";
  51. }
  52. return true;
  53. }
  54. std::optional<std::string> DataDumpParser::GetDumpPath() const {
  55. auto dump_path = std::getenv(kDataDumpPath);
  56. if (dump_path == nullptr) {
  57. MS_LOG(ERROR) << "[DataDump] dump path is null. Please export DATA_DUMP_PATH";
  58. return {};
  59. }
  60. std::string dump_path_str(dump_path);
  61. if (!std::all_of(dump_path_str.begin(), dump_path_str.end(),
  62. [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) {
  63. MS_LOG(EXCEPTION) << "[DataDump] dump path only support alphabets, digit or {'-', '_', '/'}, but got:"
  64. << dump_path_str;
  65. }
  66. return dump_path_str;
  67. }
  68. std::string GetIfstreamString(const std::ifstream &ifstream) {
  69. std::stringstream buffer;
  70. buffer << ifstream.rdbuf();
  71. return buffer.str();
  72. }
  73. void DataDumpParser::ParseDumpConfig() {
  74. std::lock_guard<std::mutex> guard(lock_);
  75. MS_LOG(INFO) << "[DataDump] parse start";
  76. if (!DumpEnabled()) {
  77. MS_LOG(INFO) << "[DataDump] dump not enable";
  78. return;
  79. }
  80. ResetParam();
  81. auto dump_config_file = Common::GetConfigFile(kDataDumpConfigPtah);
  82. if (!dump_config_file.has_value()) {
  83. MS_LOG(EXCEPTION) << "[DataDump] Get config file failed";
  84. }
  85. std::ifstream json_file(dump_config_file.value());
  86. if (!json_file.is_open()) {
  87. MS_LOG(EXCEPTION) << "[DataDump] " << dump_config_file.value() << " open failed.";
  88. }
  89. nlohmann::json j;
  90. try {
  91. json_file >> j;
  92. } catch (nlohmann::json::parse_error &e) {
  93. MS_LOG(ERROR) << "[DataDump] json contents:" << GetIfstreamString(json_file);
  94. MS_LOG(EXCEPTION) << "[DataDump] parse json failed, error:" << e.what();
  95. }
  96. if (j.find("DumpSettings") == j.end()) {
  97. MS_LOG(EXCEPTION) << "[DataDump] DumpSettings is not exist.";
  98. }
  99. nlohmann::json dump_settings = j.at("DumpSettings");
  100. // convert json to string
  101. std::stringstream ss;
  102. ss << dump_settings;
  103. std::string cfg = ss.str();
  104. MS_LOG(INFO) << "[DataDump] Async dump settings Json: " << cfg;
  105. if (!IsConfigExist(dump_settings)) {
  106. MS_LOG(EXCEPTION) << "[DataDump] Async dump json invalid";
  107. }
  108. if (!ParseDumpSetting(dump_settings)) {
  109. MS_LOG(EXCEPTION) << "[DataDump] Parse dump json failed";
  110. }
  111. }
  112. bool DataDumpParser::NeedDump(const std::string &op_full_name) const {
  113. if (!DumpEnabled()) {
  114. return false;
  115. }
  116. if (dump_mode_ == 0) {
  117. return true;
  118. }
  119. auto iter = kernel_map_.find(op_full_name);
  120. return iter != kernel_map_.end();
  121. }
  122. bool DataDumpParser::IsConfigExist(const nlohmann::json &dump_settings) const {
  123. if (dump_settings.find(kConfigDumpMode) == dump_settings.end() ||
  124. dump_settings.find(kConfigNetName) == dump_settings.end() ||
  125. dump_settings.find(kConfigOpDebugMode) == dump_settings.end() ||
  126. dump_settings.find(kConfigIteration) == dump_settings.end() ||
  127. dump_settings.find(kConfigKernels) == dump_settings.end()) {
  128. MS_LOG(ERROR) << "[DataDump] DumpSettings keys are not exist.";
  129. return false;
  130. }
  131. return true;
  132. }
  133. bool DataDumpParser::ParseDumpSetting(const nlohmann::json &dump_settings) {
  134. auto mode = dump_settings.at(kConfigDumpMode);
  135. auto op_debug_mode = dump_settings.at(kConfigOpDebugMode);
  136. auto net_name = dump_settings.at(kConfigNetName);
  137. auto iteration = dump_settings.at(kConfigIteration);
  138. auto kernels = dump_settings.at(kConfigKernels);
  139. if (!(mode.is_number_unsigned() && op_debug_mode.is_number_unsigned() && net_name.is_string() &&
  140. iteration.is_number_unsigned() && kernels.is_array())) {
  141. MS_LOG(ERROR) << "[DataDump] Element's type in Dump config json is invalid.";
  142. enable_ = false;
  143. return false;
  144. }
  145. CheckDumpMode(mode);
  146. CheckOpDebugMode(op_debug_mode);
  147. enable_ = true;
  148. auto context_ptr = MsContext::GetInstance();
  149. MS_EXCEPTION_IF_NULL(context_ptr);
  150. dump_mode_ = mode;
  151. op_debug_mode_ = op_debug_mode;
  152. net_name_ = net_name;
  153. dump_step_ = iteration;
  154. for (const auto &kernel : kernels) {
  155. auto kernel_str = kernel.dump();
  156. kernel_str.erase(std::remove(kernel_str.begin(), kernel_str.end(), '\"'), kernel_str.end());
  157. MS_LOG(INFO) << "[DataDump] Need dump kernel:" << kernel_str;
  158. kernel_map_.insert({kernel_str, 0});
  159. }
  160. return true;
  161. }
  162. void DataDumpParser::MatchKernel(const std::string &kernel_name) {
  163. auto iter = kernel_map_.find(kernel_name);
  164. if (iter == kernel_map_.end()) {
  165. return;
  166. }
  167. iter->second = iter->second + 1;
  168. MS_LOG(INFO) << "Match dump kernel:" << iter->first << " match times:" << iter->second;
  169. }
  170. void DataDumpParser::PrintUnusedKernel() {
  171. for (const auto &iter : kernel_map_) {
  172. if (iter.second == 0) {
  173. MS_LOG(WARNING) << "[DataDump] Unused Kernel in json:" << iter.first;
  174. }
  175. }
  176. }
  177. void DataDumpParser::CheckDumpMode(uint32_t dump_mode) const {
  178. if (dump_mode != 0 && dump_mode != 1) {
  179. MS_LOG(EXCEPTION) << "[DataDump] dump_mode in config json should be 0 or 1";
  180. }
  181. }
  182. void DataDumpParser::CheckOpDebugMode(uint32_t op_debug_mode) const {
  183. if (op_debug_mode < 0 || op_debug_mode > 3) {
  184. MS_LOG(EXCEPTION) << "[DataDump] op_debug_mode in config json file should be [0-3]";
  185. }
  186. }
  187. } // namespace mindspore