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.

e2e_dump.cc 5.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright 2019 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/e2e_dump.h"
  17. #include <limits.h>
  18. #include <fstream>
  19. #include <string>
  20. #include <optional>
  21. #include <nlohmann/json.hpp>
  22. #include "utils/log_adapter.h"
  23. #include "utils/system/file_system.h"
  24. #include "utils/system/env.h"
  25. #include "utils/convert_utils.h"
  26. #include "utils/context/ms_context.h"
  27. #include "debug/common.h"
  28. using json = nlohmann::json;
  29. namespace mindspore {
  30. Dump::Dump()
  31. : dump_enable_(false),
  32. trans_flag_(false),
  33. dump_path_("/tmp/"),
  34. dump_net_name_("net_name"),
  35. dump_mode_(0),
  36. dump_iter_(0),
  37. cur_iter_(0) {}
  38. bool Dump::IsKernelNeedDump(const std::string &kernel_name) {
  39. if (dump_mode_ == 0) {
  40. // Dump All Kernels mode
  41. return true;
  42. } else {
  43. auto iter = std::find(dump_kernels_.begin(), dump_kernels_.end(), kernel_name);
  44. if (iter != dump_kernels_.end()) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. bool Dump::ParseDumpConfig(const std::string &dump_config_file) {
  51. std::ifstream jsonFile(dump_config_file);
  52. if (!jsonFile.is_open()) {
  53. MS_LOG(ERROR) << dump_config_file << " open failed.";
  54. dump_enable_ = false;
  55. return false;
  56. }
  57. json j;
  58. jsonFile >> j;
  59. if (j.find("DumpSettings") == j.end()) {
  60. MS_LOG(ERROR) << "DumpSettings is not exist.";
  61. dump_enable_ = false;
  62. return false;
  63. } else {
  64. json dumpSettings = j.at("DumpSettings");
  65. // convert json to string
  66. std::stringstream ss;
  67. ss << dumpSettings;
  68. std::string cfg = ss.str();
  69. MS_LOG(INFO) << "E2E Dump Settings Json: " << cfg;
  70. if (!IsConfigExist(dumpSettings)) {
  71. return false;
  72. }
  73. if (!IsConfigValid(dumpSettings)) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. bool Dump::IsConfigExist(const nlohmann::json &dumpSettings) {
  80. if (dumpSettings.find("trans_flag") == dumpSettings.end() || dumpSettings.find("enable") == dumpSettings.end() ||
  81. dumpSettings.find("mode") == dumpSettings.end() || dumpSettings.find("path") == dumpSettings.end() ||
  82. dumpSettings.find("net_name") == dumpSettings.end() || dumpSettings.find("iteration") == dumpSettings.end() ||
  83. dumpSettings.find("kernels") == dumpSettings.end()) {
  84. MS_LOG(ERROR) << "DumpSettings keys is not exist.";
  85. dump_enable_ = false;
  86. return false;
  87. }
  88. return true;
  89. }
  90. bool Dump::IsConfigValid(const nlohmann::json &dumpSettings) {
  91. auto trans_flag = dumpSettings.at("trans_flag");
  92. auto enable = dumpSettings.at("enable");
  93. auto mode = dumpSettings.at("mode");
  94. auto path = dumpSettings.at("path");
  95. auto net_name = dumpSettings.at("net_name");
  96. auto iteration = dumpSettings.at("iteration");
  97. auto kernels = dumpSettings.at("kernels");
  98. if (!(enable.is_boolean() && trans_flag.is_boolean() && mode.is_number() && path.is_string() &&
  99. net_name.is_string() && iteration.is_number() && kernels.is_array())) {
  100. MS_LOG(ERROR) << "Element's type in Dump config json is invalid.";
  101. dump_enable_ = false;
  102. return false;
  103. }
  104. dump_enable_ = enable;
  105. auto context_ptr = MsContext::GetInstance();
  106. MS_EXCEPTION_IF_NULL(context_ptr);
  107. // dump_enable_ is true, close mem reuse
  108. context_ptr->set_enable_mem_reuse(!dump_enable_);
  109. trans_flag_ = trans_flag;
  110. dump_mode_ = mode;
  111. dump_path_ = path;
  112. dump_net_name_ = net_name;
  113. dump_iter_ = iteration;
  114. for (const auto &kernel : kernels) {
  115. dump_kernels_.push_back(kernel);
  116. }
  117. return true;
  118. }
  119. bool Dump::SetDumpConfFromJsonFile() {
  120. const char *config_path_str = std::getenv("MINDSPORE_CONFIG_PATH");
  121. if (config_path_str != nullptr) {
  122. MS_LOG(INFO) << "Getenv MINDSPORE_CONFIG_PATH :" << config_path_str;
  123. } else {
  124. MS_LOG(INFO) << "No need E2E Dump. please export MINDSPORE_CONFIG_PATH eg: MINDSPORE_CONFIG_PATH=/etc";
  125. dump_enable_ = false;
  126. return false;
  127. }
  128. auto context_ptr = MsContext::GetInstance();
  129. MS_EXCEPTION_IF_NULL(context_ptr);
  130. auto id = context_ptr->device_id();
  131. char real_path[PATH_MAX] = {0};
  132. if (nullptr == realpath(config_path_str, real_path)) {
  133. MS_LOG(ERROR) << "Env e2e dump path error, " << config_path_str;
  134. dump_enable_ = false;
  135. return false;
  136. }
  137. std::string dump_config_file = std::string(real_path) + "/e2e_dump_config_" + std::to_string(id) + ".json";
  138. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  139. MS_EXCEPTION_IF_NULL(fs);
  140. if (!fs->FileExist(dump_config_file)) {
  141. MS_LOG(ERROR) << dump_config_file << " not exist.";
  142. dump_enable_ = false;
  143. return false;
  144. }
  145. return ParseDumpConfig(dump_config_file);
  146. }
  147. bool Dump::DumpToFile(const std::string &filename, const void *data, size_t len) {
  148. if (filename.empty() || data == nullptr || len == 0) {
  149. MS_LOG(ERROR) << "Incorrect parameter.";
  150. return false;
  151. }
  152. auto realpath = Common::GetRealPath(filename);
  153. if (!realpath.has_value()) {
  154. MS_LOG(ERROR) << "Get real path failed.";
  155. return false;
  156. }
  157. std::ofstream fd;
  158. fd.open(realpath.value(), std::ios::binary | std::ios::out);
  159. if (!fd.is_open()) {
  160. MS_LOG(ERROR) << "Open file " << realpath.value() << " fail.";
  161. return false;
  162. }
  163. (void)fd.write(reinterpret_cast<const char *>(data), SizeToLong(len));
  164. fd.close();
  165. return true;
  166. }
  167. } // namespace mindspore