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