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"
31 #define NPU_TENSOR_MAX (16)
34 * @brief Npu model ID.
37 using ModelID = uint32_t;
40 * @brief Npu request ID.
43 using RequestID = uint32_t;
46 * @brief Npu buffer type
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 */
57 * @brief Various kinds of buffer supported for input/output/model.
63 { /** NPU_BUFFER_MAPPED/DMABUF */
64 void *addr; /**< Mapped address of the buffer */
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 */
71 uint64_t size; /**< The size of the buffer in bytes */
72 BufferTypes type; /**< Type of memory in this buffer */
76 * @brief Npu generic buffer array.
82 GenericBuffer buffers[NPU_TENSOR_MAX];
86 * @brief Npu input/output buffers are compotible with GenericBuffers.
89 typedef GenericBuffers InputBuffers;
90 typedef GenericBuffers OutputBuffers;
93 * @brief Npu tensor data info description.
103 * @brief Npu tensor data info array.
106 struct TensorDataInfos
109 TensorDataInfo infos[NPU_TENSOR_MAX];
113 * @brief Npu error status.
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,
127 * @brief Npu context definition
129 * @param models The model lists.
130 * @param requests The request lists.
131 * @param defaultCore The core number to be used by default.
135 std::vector<ModelID> models;
136 std::vector<RequestID> requests;
141 * @brief Npu backend interface
143 * Backend module should implement this Backend interface.
144 * Npu daemon will load this class symbol at runtime.
149 virtual ~Backend() = default;
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;
168 typedef Backend *(*NpuAlloc)();
169 typedef void (*NpuDealloc)(Backend *);
174 #endif // __ONE_SERVICE_NPUD_CORE_BACKEND_H__