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_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H_ 18 #define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H_ 19 #include "src/extendrt/delegate/tensorrt/tensorrt_allocator.h" 20 #include <map> 21 #include <string> 22 #include <NvInfer.h> 23 #include "include/api/types.h" 24 #include "ir/tensor.h" 25 #include "src/extendrt/delegate/tensorrt/tensor_info.h" 26 27 namespace mindspore::lite { 28 struct CudaTensorParam { 29 void *data = nullptr; 30 bool is_valid_mem = false; 31 size_t size = 0; 32 }; 33 class TensorRTAllocator { 34 public: 35 TensorRTAllocator() = default; 36 37 ~TensorRTAllocator() = default; 38 39 void *MallocDeviceMem(const TensorInfo &host_tensor, size_t size); 40 41 void *MallocDeviceMem(const std::string &name, size_t size, nvinfer1::DataType data_type); 42 43 void *GetDevicePtr(const std::string &tensor_name); 44 SetCudaStream(cudaStream_t stream)45 void SetCudaStream(cudaStream_t stream) { stream_ = stream; } 46 47 std::map<std::string, CudaTensorParam> GetAllDevicePtr(); 48 49 int SyncMemInHostAndDevice(tensor::Tensor *host_tensor, const std::string &device_tensor_name, bool is_host2device, 50 bool sync = true); 51 52 int SyncMemInHostAndDevice(void *host_data, const std::string &device_tensor_name, size_t data_size, 53 bool is_host2device, bool sync = true); 54 int SyncMemHostToDevice(const tensor::Tensor &host_tensor, const std::string &device_tensor_name, bool sync = true, 55 size_t size = 0); 56 int SyncMemDeviceToHost(tensor::Tensor *host_tensor, const std::string &device_tensor_name, bool sync = true); 57 int SyncMemDeviceToHost(void *dst_data, size_t data_size, const std::string &device_tensor_name, bool sync = true); 58 59 int ClearDeviceMem(); 60 61 void MarkMemValid(const std::string &name, bool isValid); 62 63 bool GetMemIsValid(const std::string &name); 64 65 private: 66 std::map<std::string, CudaTensorParam> cuda_tensor_map_; 67 cudaStream_t stream_; 68 }; 69 } // namespace mindspore::lite 70 #endif // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H_ 71