1 /** 2 * Copyright 2020-2023 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 17 #ifndef MINDSPORE_MINDSPORE_CCSRC_DEBUG_DUMP_JSON_PARSER_H_ 18 #define MINDSPORE_MINDSPORE_CCSRC_DEBUG_DUMP_JSON_PARSER_H_ 19 20 #include <string> 21 #include <map> 22 #include <set> 23 #include <mutex> 24 #include <vector> 25 #include <memory> 26 #include <regex> 27 #include "nlohmann/json.hpp" 28 #include "utils/ms_utils.h" 29 #include "include/backend/kernel_graph.h" 30 #include "include/backend/visible.h" 31 32 namespace mindspore { 33 void CheckJsonUnsignedType(const nlohmann::json &content, const std::string &key); 34 void CheckJsonStringType(const nlohmann::json &content, const std::string &key); 35 void CheckJsonArrayType(const nlohmann::json &content, const std::string &key); 36 37 class BACKEND_EXPORT DumpJsonParser { 38 public: GetInstance()39 static DumpJsonParser &GetInstance() { 40 std::call_once(instance_mutex_, []() { 41 if (instance_ == nullptr) { 42 instance_ = std::shared_ptr<DumpJsonParser>(new DumpJsonParser); 43 } 44 }); 45 return *instance_; 46 } Finalize()47 static void Finalize() { instance_ = nullptr; } 48 49 ~DumpJsonParser() = default; 50 void Parse(); 51 static bool DumpToFile(const std::string &filename, const void *data, size_t len, const ShapeVector &shape, 52 TypeId type); 53 void CopyDumpJsonToDir(uint32_t rank_id); 54 void CopyHcclJsonToDir(uint32_t rank_id); 55 void CopyMSCfgJsonToDir(uint32_t rank_id); 56 bool NeedDump(const std::string &op_full_name); 57 void MatchKernel(const std::string &kernel_name); 58 void PrintUnusedKernel(); 59 bool IsStatisticDump() const; 60 bool IsTensorDump() const; 61 bool IsFullDump() const; 62 bool IsNpyFormat() const; 63 bool IsDumpIter(uint32_t iteration) const; dump_layer()64 std::string dump_layer() const { return dump_layer_; } async_dump_enabled()65 bool async_dump_enabled() const { return async_dump_enabled_; } e2e_dump_enabled()66 bool e2e_dump_enabled() const { return e2e_dump_enabled_; } statistic_category()67 std::vector<std::string> statistic_category() const { return statistic_category_; } dump_mode()68 uint32_t dump_mode() const { return dump_mode_; } path()69 std::string path() const { return path_; } saved_data()70 std::string saved_data() const { return saved_data_; } iteration_string()71 std::string iteration_string() const { return iteration_; } net_name()72 std::string net_name() const { return net_name_; } op_debug_mode()73 uint32_t op_debug_mode() const { return op_debug_mode_; } trans_flag()74 bool trans_flag() const { return trans_flag_; } save_args_flag()75 bool save_args_flag() const { return save_args_flag_; } sample_mode()76 uint32_t sample_mode() const { return sample_mode_; } sample_num()77 uint32_t sample_num() const { return sample_num_; } cur_dump_iter()78 uint32_t cur_dump_iter() const { return cur_dump_iter_; } input_output()79 uint32_t input_output() const { return input_output_; } UpdateDumpIter()80 void UpdateDumpIter() { ++cur_dump_iter_; } UpdateDumpIter(int cur_step_count)81 void UpdateDumpIter(int cur_step_count) { cur_dump_iter_ = cur_step_count; } GetDatasetSink()82 bool GetDatasetSink() { return is_dataset_sink_; } SetDatasetSink(bool is_dataset_sink)83 void SetDatasetSink(bool is_dataset_sink) { is_dataset_sink_ = is_dataset_sink; } FileFormatIsNpy()84 bool FileFormatIsNpy() const { return file_format_ == JsonFileFormat::FORMAT_NPY; } 85 bool GetIterDumpFlag() const; 86 bool DumpEnabledForIter() const; 87 bool InputNeedDump() const; 88 bool OutputNeedDump() const; 89 std::string GetOpOverflowBinPath(uint32_t graph_id) const; 90 void GetCellDumpFlag(const session::KernelGraph &kernel_graph); 91 void UpdateNeedDumpKernels(const session::KernelGraph &kernel_graph); 92 bool IsDumpEnabled(); 93 bool IsDeviceCalcStats() const; 94 void PyNativeModeCheck(); 95 void CheckE2eSetting(); 96 bool IsHCCLKernelInput(const std::string &kernel_name) const; IsCallbackRegistered()97 bool IsCallbackRegistered() { return dumpdatacallback_registered_; } SetCallbackRegistered()98 void SetCallbackRegistered() { dumpdatacallback_registered_ = true; } 99 ClearGraph()100 void ClearGraph() { graphs_.clear(); } SaveGraph(session::KernelGraph * graph)101 void SaveGraph(session::KernelGraph *graph) { (void)graphs_.emplace_back(graph); } graphs()102 const std::vector<session::KernelGraph *> &graphs() const { return graphs_; } 103 enum JsonDumpMode { DUMP_ALL = 0, DUMP_KERNEL = 1, DUMP_KERNELS_WITH_FLAG = 2 }; 104 enum JsonFileFormat { FORMAT_NPY = 0, FORMAT_BIN = 1 }; 105 enum JsonInputOutput { DUMP_BOTH = 0, DUMP_INPUT = 1, DUMP_OUTPUT = 2 }; 106 enum JosonOpDebugMode { 107 DUMP_WHOLE = 0, 108 DUMP_AICORE_OVERFLOW = 1, 109 DUMP_ATOMIC_OVERFLOW = 2, 110 DUMP_BOTH_OVERFLOW = 3, 111 DUMP_LITE_EXCEPTION = 4 112 }; 113 enum JosonSampleMode { DUMP_NORMAL = 0, DUMP_HEAD_AND_TAIL = 1 }; 114 static bool IsAclDump(); GetKernelsJson()115 nlohmann::json GetKernelsJson() { return kernels_json_; } GetKernelRegs()116 std::map<std::string, std::regex> GetKernelRegs() { return kernel_regs_; } GetKernelStrs()117 std::map<std::string, uint32_t> GetKernelStrs() { return kernel_strings_; } 118 119 private: 120 DumpJsonParser() = default; 121 DISABLE_COPY_AND_ASSIGN(DumpJsonParser) 122 123 inline static std::shared_ptr<DumpJsonParser> instance_ = nullptr; 124 inline static std::once_flag instance_mutex_; 125 126 inline static std::mutex lock_; 127 bool async_dump_enabled_{false}; 128 bool e2e_dump_enabled_{false}; 129 bool is_dataset_sink_{false}; 130 bool dumpdatacallback_registered_{false}; 131 uint32_t dump_mode_{0}; 132 std::string path_; 133 std::string net_name_; 134 std::string saved_data_; 135 std::string iteration_; 136 uint32_t input_output_{0}; 137 std::map<std::string, uint32_t> kernels_; 138 std::map<std::string, uint32_t> kernel_types_; 139 std::map<std::string, std::regex> kernel_regs_; 140 std::map<std::string, uint32_t> kernel_strings_; 141 std::vector<std::string> cell_dump_kernels_; 142 std::set<std::string> hccl_input_kernels_; 143 std::set<uint32_t> support_devices_; 144 uint32_t op_debug_mode_{0}; 145 JsonFileFormat file_format_{FORMAT_BIN}; 146 bool trans_flag_{false}; 147 bool save_args_flag_{false}; 148 uint32_t sample_mode_{0}; 149 uint32_t sample_num_{100}; 150 uint32_t cur_dump_iter_{0}; 151 bool already_parsed_{false}; 152 std::string dump_layer_{""}; 153 std::string stat_calc_mode_{"host"}; 154 nlohmann::json kernels_json_ = nlohmann::json::array(); 155 std::vector<std::string> statistic_category_; 156 157 // Save graphs for dump. 158 std::vector<session::KernelGraph *> graphs_; 159 160 void ParseCommonDumpSetting(const nlohmann::json &content); 161 void ParseE2eDumpSetting(const nlohmann::json &content); 162 163 static auto CheckJsonKeyExist(const nlohmann::json &content, const std::string &key); 164 static bool CheckSelectableKeyExist(const nlohmann::json &content, const std::string &key); 165 166 void ParseDumpMode(const nlohmann::json &content); 167 void ParseDumpPath(const nlohmann::json &content); 168 void ParseNetName(const nlohmann::json &content); 169 void ParseSavedData(const nlohmann::json &content); 170 void ParseIteration(const nlohmann::json &content); 171 void ParseInputOutput(const nlohmann::json &content); 172 void ParseKernels(const nlohmann::json &content); 173 void ParseSupportDevice(const nlohmann::json &content); 174 bool ParseEnable(const nlohmann::json &content) const; 175 void ParseSampleMode(const nlohmann::json &content); 176 void ParseSampleNum(const nlohmann::json &content); 177 void ParseOpDebugMode(const nlohmann::json &content); 178 void ParseFileFormat(const nlohmann::json &content); 179 void ParseStatCalcMode(const nlohmann::json &content); 180 181 void JudgeDumpEnabled(); 182 void JsonConfigToString(); 183 void CheckStatCalcModeVaild(); 184 void ParseStatisticCategory(const nlohmann::json &content); 185 }; 186 } // namespace mindspore 187 #endif // MINDSPORE_MINDSPORE_CCSRC_DEBUG_DUMP_JSON_PARSER_H_ 188