• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RUNTIME_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H
18 #define MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H
19 #include "src/delegate/tensorrt/tensorrt_allocator.h"
20 #include <map>
21 #include <string>
22 #include <NvInfer.h>
23 #include "include/api/types.h"
24 #include "include/ms_tensor.h"
25 
26 namespace mindspore::lite {
27 struct CudaTensorParam {
28   void *data = nullptr;
29   bool is_valid_mem = false;
30   size_t size = 0;
31 };
32 class TensorRTAllocator {
33  public:
34   TensorRTAllocator() = default;
35 
36   ~TensorRTAllocator() = default;
37 
38   void *MallocDeviceMem(const mindspore::MSTensor &host_tensor, size_t size);
39 
40   void *MallocDeviceMem(const std::string &name, size_t size, nvinfer1::DataType data_type);
41 
42   void *GetDevicePtr(const std::string &tensor_name);
43 
44   int SyncMemInHostAndDevice(mindspore::MSTensor host_tensor, const std::string &device_tensor_name,
45                              bool is_host2device, bool sync = true);
46 
47   int SyncMemInHostAndDevice(void *host_data, const std::string &device_tensor_name, size_t data_size,
48                              bool is_host2device, bool sync = true);
49 
50   int ClearDeviceMem();
51 
52   void MarkMemValid(const std::string &name, bool isValid);
53 
54   bool GetMemIsValid(const std::string &name);
55 
56  private:
57   std::map<std::string, CudaTensorParam> cuda_tensor_map_;
58 };
59 }  // namespace mindspore::lite
60 #endif  // MINDSPORE_LITE_SRC_RUNTIME_DELEGATE_TENSORRT_TENSORRT_ALLOCATOR_H
61