1 /** 2 * Copyright 2021 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_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_ADAPTER_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_ADAPTER_H_ 19 20 #include <algorithm> 21 #include <mutex> 22 #include <string> 23 #include <memory> 24 #include <vector> 25 #include <limits> 26 27 #include "utils/ms_context.h" 28 #include "ir/anf.h" 29 30 namespace mindspore { 31 namespace device { 32 namespace ascend { 33 class AscendMemAdapter { 34 public: GetInstance()35 static AscendMemAdapter &GetInstance() { 36 static AscendMemAdapter instance{}; 37 return instance; 38 } 39 40 bool Initialize(); 41 bool DeInitialize(); 42 43 uint8_t *MallocStaticDevMem(size_t size, const std::string &tag = ""); 44 uint8_t *MallocDynamicDevMem(size_t size, const std::string &tag = ""); 45 uint8_t *MallocOverflowMem(); 46 uint8_t *GetBaseAddr() const; FreeStaticDevMem(void *)47 bool FreeStaticDevMem(void *) const { return true; } 48 void ResetDynamicMemory(); GetActualPeakMemory()49 int64_t GetActualPeakMemory() const { return actual_peak_memory_; } GetUsedPeakMemory()50 int64_t GetUsedPeakMemory() const { return used_peak_memory_; } UpdateActualPeakMemory(int64_t memory)51 void UpdateActualPeakMemory(int64_t memory) { actual_peak_memory_ = std::max(actual_peak_memory_, memory); } UpdateUsedPeakMemory(int64_t memory)52 void UpdateUsedPeakMemory(int64_t memory) { used_peak_memory_ = std::max(used_peak_memory_, memory); } 53 54 static size_t GetRoundUpAlignSize(size_t input_size); 55 FreeDevMemSize()56 [[nodiscard]] uint64_t FreeDevMemSize() const { return std::max(static_mem_offset_ - max_dynamic_mem_offset_, 0L); } MaxHbmSizeForMs()57 [[nodiscard]] uint64_t MaxHbmSizeForMs() const { return max_available_ms_hbm_size_; } GetMsUsedHbmSize()58 [[nodiscard]] int64_t GetMsUsedHbmSize() const { return ms_used_hbm_size_; } 59 std::string DevMemStatistics() const; 60 std::string DevMemDetailInfo() const; 61 62 private: 63 AscendMemAdapter() = default; 64 65 struct MemoryBlock { MemoryBlockMemoryBlock66 MemoryBlock(void *ptr, const size_t size, const std::string &tag) { 67 mem_ptr = ptr; 68 mem_size = size; 69 mem_tag = tag; 70 } 71 72 void *mem_ptr{nullptr}; 73 size_t mem_size{0}; 74 std::string mem_tag; 75 }; 76 77 uint8_t *MallocFromRts(size_t size) const; 78 bool FreeToRts(void *devPtr, const size_t size) const; 79 size_t GetDeviceMemSizeFromContext() const; 80 bool initialized_{false}; 81 82 // Support multi-thread. 83 std::mutex mutex_; 84 85 // rts Memory INFO 86 size_t device_hbm_total_size_{0}; 87 size_t device_hbm_free_size_{0}; 88 int64_t max_available_ms_hbm_size_{0}; 89 uint8_t *device_mem_base_addr_{nullptr}; 90 int64_t ms_used_hbm_size_{0}; 91 92 // dynamic memory info, from a low address to a high address 93 int64_t cur_dynamic_mem_offset_{0}; 94 // Maximum dynamic memory have already allocated, dynamically updated 95 int64_t max_dynamic_mem_offset_{0}; 96 // History maximum dynamic memory (used in memory pool recycle mode) 97 int64_t history_max_dynamic_mem_offset_{0}; 98 std::vector<std::shared_ptr<MemoryBlock>> dynamic_memory_block_list_; 99 100 // static memory info, from a high address to a low address 101 int64_t static_mem_offset_{0}; 102 // Actual peak memory usage (with fragments) 103 int64_t actual_peak_memory_{0}; 104 // Used peak memory usage (without fragments) 105 int64_t used_peak_memory_{0}; 106 std::vector<std::shared_ptr<MemoryBlock>> static_memory_block_list_; 107 static size_t GetRoundDownAlignSize(size_t input_size); 108 }; 109 } // namespace ascend 110 } // namespace device 111 } // namespace mindspore 112 113 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_ASCEND_ASCEND_MEMORY_ADAPTER_H_ 114