1 /** 2 * Copyright 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_CCSRC_PLUGIN_DEVICE_ASCEND_HAL_DEVICE_TENSORDUMP_UTILS_H_ 18 #define MINDSPORE_CCSRC_PLUGIN_DEVICE_ASCEND_HAL_DEVICE_TENSORDUMP_UTILS_H_ 19 20 #include <string> 21 #include <vector> 22 #include <queue> 23 #include <thread> 24 #include <mutex> 25 #include <utility> 26 #include <condition_variable> 27 #include "plugin/device/ascend/hal/device/mbuf_receive_manager.h" 28 29 namespace mindspore::device::ascend { 30 const std::pair<string, string> tensordump_mapping{"ms_tensor_dump", "TensorDump"}; 31 class AsyncFileWriter { 32 public: 33 explicit AsyncFileWriter(size_t thread_nums); 34 ~AsyncFileWriter(); 35 void Submit(std::function<void()> func); 36 37 private: 38 void WorkerThread(); 39 40 std::vector<std::thread> threads; 41 std::queue<std::function<void()>> tasks; 42 std::mutex queue_mutex; 43 std::condition_variable cv; 44 std::atomic_bool stop = false; 45 std::atomic_bool threads_started = false; 46 }; 47 48 class TensorDumpUtils { 49 public: 50 static TensorDumpUtils &GetInstance(); 51 52 TensorDumpUtils() = default; 53 TensorDumpUtils(const TensorDumpUtils &) = delete; 54 TensorDumpUtils &operator=(const TensorDumpUtils &) = delete; 55 void AsyncSaveDatasetToNpyFile(const ScopeAclTdtDataset &dataset); 56 57 private: 58 std::string TensorNameToArrayName(const std::string &tensor_name); 59 AsyncFileWriter file_writer{4}; 60 }; 61 62 } // namespace mindspore::device::ascend 63 #endif // MINDSPORE_CCSRC_PLUGIN_DEVICE_ASCEND_HAL_DEVICE_TENSORDUMP_UTILS_H_ 64