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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. constexpr auto kDataDumpConfigPtah = "DATA_DUMP_CONFIG_PATH";
  21. constexpr auto kEnableDataDump = "ENABLE_DATA_DUMP";
  22. constexpr auto kDataDumpPath = "DATA_DUMP_PATH";
  23. namespace mindspore {
  24. void DataDumpParser::ResetParam() {
  25. enable_ = false;
  26. net_name_.clear();
  27. dump_mode_ = 0;
  28. dump_step_ = 0;
  29. kernel_set_.clear();
  30. }
  31. bool DataDumpParser::DumpEnabled() const {
  32. auto enable_dump = std::getenv(kEnableDataDump);
  33. if (!enable_dump) {
  34. MS_LOG(WARNING) << "[DataDump] enable dump is null. Please export ENABLE_DATA_DUMP";
  35. return false;
  36. }
  37. auto enabled = std::atoi(enable_dump);
  38. if (enabled != 1) {
  39. MS_LOG(WARNING) << "[DataDump] Please export ENABLE_DATA_DUMP=1";
  40. return false;
  41. }
  42. auto context = MsContext::GetInstance();
  43. MS_EXCEPTION_IF_NULL(context);
  44. if (context->execution_mode() == kPynativeMode) {
  45. MS_LOG(EXCEPTION) << "[DataDump] PyNative mode not support data dump";
  46. }
  47. return true;
  48. }
  49. std::optional<std::string> DataDumpParser::GetDumpPath() const {
  50. auto dump_path = std::getenv(kDataDumpPath);
  51. if (!dump_path) {
  52. MS_LOG(ERROR) << "[DataDump] dump path is null. Please export DATA_DUMP_PATH";
  53. return {};
  54. }
  55. std::string dump_path_str(dump_path);
  56. return dump_path_str;
  57. }
  58. void DataDumpParser::ParseDumpConfig() {
  59. std::lock_guard<std::mutex> guard(lock_);
  60. MS_LOG(INFO) << "[DataDump] parse start";
  61. if (!DumpEnabled()) {
  62. MS_LOG(INFO) << "[DataDump] dump not enable";
  63. return;
  64. }
  65. ResetParam();
  66. auto dump_config_file = Common::GetConfigFile(kDataDumpConfigPtah);
  67. if (!dump_config_file.has_value()) {
  68. MS_LOG(EXCEPTION) << "[DataDump] Get config file failed";
  69. }
  70. std::ifstream json_file(dump_config_file.value());
  71. if (!json_file.is_open()) {
  72. MS_LOG(EXCEPTION) << "[DataDump] " << dump_config_file.value() << " open failed.";
  73. }
  74. nlohmann::json j;
  75. json_file >> j;
  76. if (j.find("DumpSettings") == j.end()) {
  77. MS_LOG(EXCEPTION) << "[DataDump] DumpSettings is not exist.";
  78. }
  79. nlohmann::json dump_settings = j.at("DumpSettings");
  80. // convert json to string
  81. std::stringstream ss;
  82. ss << dump_settings;
  83. std::string cfg = ss.str();
  84. MS_LOG(INFO) << "[DataDump] Async dump settings Json: " << cfg;
  85. if (!IsConfigExist(dump_settings)) {
  86. MS_LOG(EXCEPTION) << "[DataDump] Async dump json invalid";
  87. }
  88. if (!ParseDumpSetting(dump_settings)) {
  89. MS_LOG(EXCEPTION) << "[DataDump] Parse dump json failed";
  90. }
  91. }
  92. bool DataDumpParser::NeedDump(const std::string &op_full_name) const {
  93. if (!DumpEnabled()) {
  94. return false;
  95. }
  96. if (dump_mode_ == 0) {
  97. return true;
  98. }
  99. auto iter = kernel_set_.find(op_full_name);
  100. return iter != kernel_set_.end();
  101. }
  102. bool DataDumpParser::IsConfigExist(const nlohmann::json &dump_settings) const {
  103. if (dump_settings.find("mode") == dump_settings.end() || dump_settings.find("net_name") == dump_settings.end() ||
  104. dump_settings.find("iteration") == dump_settings.end() || dump_settings.find("kernels") == dump_settings.end()) {
  105. MS_LOG(ERROR) << "[DataDump] DumpSettings keys are not exist.";
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool DataDumpParser::ParseDumpSetting(const nlohmann::json &dump_settings) {
  111. auto mode = dump_settings.at("mode");
  112. auto net_name = dump_settings.at("net_name");
  113. auto iteration = dump_settings.at("iteration");
  114. auto kernels = dump_settings.at("kernels");
  115. if (!(mode.is_number() && net_name.is_string() && iteration.is_number() && kernels.is_array())) {
  116. MS_LOG(ERROR) << "[DataDump] Element's type in Dump config json is invalid.";
  117. enable_ = false;
  118. return false;
  119. }
  120. enable_ = true;
  121. auto context_ptr = MsContext::GetInstance();
  122. MS_EXCEPTION_IF_NULL(context_ptr);
  123. dump_mode_ = mode;
  124. net_name_ = net_name;
  125. dump_step_ = iteration;
  126. for (const auto &kernel : kernels) {
  127. auto kernel_str = kernel.dump();
  128. kernel_str.erase(std::remove(kernel_str.begin(), kernel_str.end(), '\"'), kernel_str.end());
  129. MS_LOG(INFO) << "[DataDump] Need dump kernel:" << kernel_str;
  130. kernel_set_.insert(kernel_str);
  131. }
  132. return true;
  133. }
  134. } // namespace mindspore