2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONE_SERVICE_NPUD_CORE_BACKEND_H__
18 #define __ONE_SERVICE_NPUD_CORE_BACKEND_H__
20 #include "ir/Layout.h"
21 #include "ir/DataType.h"
32 #define NPU_TENSOR_MAX (16)
35 * @brief Npu model ID.
38 using ModelID = uint32_t;
41 * @brief Npu request ID.
44 using RequestID = uint32_t;
47 * @brief Npu buffer type
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 */
58 * @brief Various kinds of buffer supported for input/output/model.
64 { /** NPU_BUFFER_MAPPED/DMABUF */
65 void *addr; /**< Mapped address of the buffer */
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 */
72 uint64_t size; /**< The size of the buffer in bytes */
73 BufferTypes type; /**< Type of memory in this buffer */
77 * @brief Npu generic buffer array.
83 GenericBuffer buffers[NPU_TENSOR_MAX];
87 * @brief Npu input/output buffers are compotible with GenericBuffers.
90 typedef GenericBuffers InputBuffers;
91 typedef GenericBuffers OutputBuffers;
94 * @brief Npu tensor data info description.
104 * @brief Npu tensor data info array.
107 struct TensorDataInfos
110 TensorDataInfo infos[NPU_TENSOR_MAX];
114 * @brief Npu error status.
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,
128 * @brief Npu context definition
130 * @param models The model lists.
131 * @param requests The request lists.
132 * @param defaultCore The core number to be used by default.
136 std::vector<ModelID> models;
137 std::vector<RequestID> requests;
142 * @brief Npu backend interface
144 * Backend module should implement this Backend interface.
145 * Npu daemon will load this class symbol at runtime.
150 virtual ~Backend() = default;
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;
169 typedef Backend *(*NpuAlloc)();
170 typedef void (*NpuDealloc)(Backend *);
175 #endif // __ONE_SERVICE_NPUD_CORE_BACKEND_H__