5bc13b66cdabb39a68c51f11bfd2cdd2ba84d5ea
[platform/core/ml/nnfw.git] / runtime / service / npud / core / Backend.h
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
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 __ONE_SERVICE_NPUD_CORE_BACKEND_H__
18 #define __ONE_SERVICE_NPUD_CORE_BACKEND_H__
19
20 #include "ir/Layout.h"
21 #include "ir/DataType.h"
22
23 #include <string>
24 #include <vector>
25
26 namespace npud
27 {
28 namespace core
29 {
30
31 #define NPU_TENSOR_MAX (16)
32
33 /**
34  * @brief Npu model ID.
35  *
36  */
37 using ModelID = uint32_t;
38
39 /**
40  * @brief Npu request ID.
41  *
42  */
43 using RequestID = uint32_t;
44
45 /**
46  * @brief Npu buffer type
47  *
48  */
49 enum BufferTypes
50 {
51   NPU_BUFFER_MAPPED,   /**< buffer is a memory-mapped ptr */
52   NPU_BUFFER_DMABUF,   /**< buffer is a dmabuf fd, representing contiguous memory */
53   NPU_BUFFER_UNDEFINED /**< buffer type is undefined */
54 };
55
56 /**
57  * @brief Various kinds of buffer supported for input/output/model.
58  *
59  */
60 struct GenericBuffer
61 {
62   struct
63   {             /** NPU_BUFFER_MAPPED/DMABUF */
64     void *addr; /**< Mapped address of the buffer */
65     struct
66     {                  /** NPU_BUFFER_DMABUF only */
67       int dmabuf;      /**< The dma-buf fd handle of the memory allocated */
68       uint64_t offset; /**< Offset to be applied to the base memory address */
69     };
70   };
71   uint64_t size;    /**< The size of the buffer in bytes */
72   BufferTypes type; /**< Type of memory in this buffer */
73 };
74
75 /**
76  * @brief Npu generic buffer array.
77  *
78  */
79 struct GenericBuffers
80 {
81   uint32_t numBuffers;
82   GenericBuffer buffers[NPU_TENSOR_MAX];
83 };
84
85 /**
86  * @brief Npu input/output buffers are compotible with GenericBuffers.
87  *
88  */
89 typedef GenericBuffers InputBuffers;
90 typedef GenericBuffers OutputBuffers;
91
92 /**
93  * @brief Npu tensor data info description.
94  *
95  */
96 struct TensorDataInfo
97 {
98   ir::Layout layout;
99   ir::DataType type;
100 };
101
102 /**
103  * @brief Npu tensor data info array.
104  *
105  */
106 struct TensorDataInfos
107 {
108   uint32_t numInfos;
109   TensorDataInfo infos[NPU_TENSOR_MAX];
110 };
111
112 /**
113  * @brief Npu error status.
114  *
115  */
116 enum NpuStatus
117 {
118   NPU_STATUS_SUCCESS = 0,
119   NPU_STATUS_ERROR_OPERATION_FAILED,
120   NPU_STATUS_ERROR_NOT_SUPPORTED,
121   NPU_STATUS_ERROR_INVALID_ARGUMENT,
122   NPU_STATUS_ERROR_INVALID_MODEL,
123   NPU_STATUS_ERROR_INVALID_DATA,
124 };
125
126 /**
127  * @brief Npu context definition
128  *
129  * @param models The model lists.
130  * @param requests The request lists.
131  * @param defaultCore The core number to be used by default.
132  */
133 struct NpuContext
134 {
135   std::vector<ModelID> models;
136   std::vector<RequestID> requests;
137   int defaultCore;
138 };
139
140 /**
141  * @brief Npu backend interface
142  *
143  * Backend module should implement this Backend interface.
144  * Npu daemon will load this class symbol at runtime.
145  */
146 class Backend
147 {
148 public:
149   virtual ~Backend() = default;
150
151   virtual NpuStatus getVersion(std::string &version) = 0;
152   virtual NpuStatus createContext(int deviceId, int priority, NpuContext **ctx) = 0;
153   virtual NpuStatus destroyContext(NpuContext *ctx) = 0;
154   virtual NpuStatus createBuffer(NpuContext *ctx, GenericBuffer *buffer) = 0;
155   virtual NpuStatus destroyBuffer(NpuContext *ctx, GenericBuffer *buffer) = 0;
156   // TODO Support to register model from buffer
157   virtual NpuStatus registerModel(NpuContext *ctx, const std::string &modelPath,
158                                   ModelID *modelId) = 0;
159   virtual NpuStatus unregisterModel(NpuContext *ctx, ModelID modelId) = 0;
160   virtual NpuStatus createRequest(NpuContext *ctx, ModelID modelId, RequestID *requestId) = 0;
161   virtual NpuStatus destroyRequest(NpuContext *ctx, RequestID requestId) = 0;
162   virtual NpuStatus setRequestData(NpuContext *ctx, RequestID requestId, InputBuffers *inputBufs,
163                                    TensorDataInfos *inputInfos, OutputBuffers *outputBufs,
164                                    TensorDataInfos *outputInfos) = 0;
165   virtual NpuStatus submitRequest(NpuContext *ctx, RequestID requestId) = 0;
166 };
167
168 typedef Backend *(*NpuAlloc)();
169 typedef void (*NpuDealloc)(Backend *);
170
171 } // namespace core
172 } // namespace npud
173
174 #endif // __ONE_SERVICE_NPUD_CORE_BACKEND_H__