3 * Copyright (C) 2019 Samsung Electronics
4 * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
9 * @brief Host handler for NPU Engine (NE).
10 * @see https://code.sec.samsung.net/confluence/display/ODLC/2020+Overall+Software+Stack
11 * @author Dongju Chae <dongju.chae@samsung.com>
12 * @bug No known bugs except for NYI items
15 #ifndef __NPU_ENGINE_HANDLER_H__
16 #define __NPU_ENGINE_HANDLER_H__
19 #include <CommPlugin.h>
22 #include "ne-scheduler.h"
25 #include "ne-profiler.h"
28 /** @brief class def. of host handler */
35 int registerModel (generic_buffer *model_buf, uint32_t *modelid);
36 int unregisterModel (uint32_t modelid);
37 int unregisterModels ();
39 int getTensorSize (uint32_t modelid, bool input, uint32_t index,
42 int getProfile (int req_id, const npu_profile_opt &opt, npu_profile *profile);
43 int setProfileModel (int req_id, uint32_t model_id);
44 int getAPILevel (uint32_t *level);
45 int getTops (uint32_t *tops);
46 int getDspmSize (uint32_t *dspm);
48 int setDataInfo (uint32_t modelid, tensors_data_info *in,
49 tensors_data_info *out);
50 int setConstraint (uint32_t modelid, npuConstraint constraint);
52 Model *getModel (uint32_t modelid);
54 int allocGenericBuffer (generic_buffer *buffer);
55 int allocGenericBuffer (generic_buffers *buffers);
57 int deallocGenericBuffer (generic_buffer *buffer);
58 int deallocGenericBuffer (generic_buffers *buffers);
60 int runModel (uint32_t modelid, npu_infer_mode mode,
61 const input_buffers *input, output_buffers *output = nullptr,
62 npuOutputNotify cb = nullptr, void *data = nullptr);
64 int createRequest (uint32_t model_id, int *req_id);
65 int removeRequest (int req_id);
66 int getRequestModel (int req_id, uint32_t *model_id);
68 int setRequestData (int req_id, input_buffers *input,
69 tensors_data_info *in_info, output_buffers *output,
70 tensors_data_info *out_info);
71 int setRequestCallback (int req_id, npuOutputNotify cb, void *data);
72 int setRequestMode (int req_id, npu_infer_mode mode);
73 int setRequestConstraint (int req_id, npu_constraint constraint);
74 int setRequestScheduler (int req_id, npu_scheduler sched,
75 npu_scheduler_param sched_param);
77 int submitRequest (int req_id);
78 int submitRequestKernel (int req_id);
80 /** @brief get statistics */
81 int getMemoryStatus (size_t *alloc_total, size_t *free_total);
82 int getDeviceStatus (npu_status *status, uint32_t *num_requests);
84 int getStatApps (npu_stat_apps *stat);
85 int getStatReqs (int appid, npu_stat_reqs *stat);
87 static int getNumDevices (dev_type type);
88 static int getDevice (npudev_h *dev, dev_type type, uint32_t id);
91 /** only Device can create a HostHandler instance */
92 HostHandler (Device *device);
94 Device *device_; /**< dedicated device instance */
95 ModelProfiler *profiler_;
97 ThreadSafeMap<uint32_t, Model> models_;
98 /**< registerd models */
99 npu_async_mode async_mode_;
100 /**< async mode of runAsync */
101 static npudev_h latest_dev_;
102 /**< latest device; just for backward-compatability */
106 * @brief device class. it contains all related instances (i.e., mem, handler, scheduler)
107 * @note Each getNPUdevice() call creates the corresponding instance of Device.
108 * Also, the caller has a responsibility to release this instance using putNPUdevice()
112 /** @brief Factory method to create a trinity device dependong on dev type */
113 static Device *createInstance (dev_type device_type, int device_id);
114 /** @brief destructor of device */
115 virtual ~Device () {}
118 dev_type getType () { return type_; }
119 int getID () { return id_; }
120 bool needModel () { return need_model_; }
121 void setNeedModel (bool need_model) { need_model_ = need_model; }
123 void setAsyncMode (npu_async_mode mode) { mode_ = mode; }
124 HostHandler *getHostHandler () { return handler_.get (); }
125 bool initialized () { return initialized_; }
126 const DriverAPI *getDriverAPI () { return api_.get (); }
128 /** the below requires initialized variables */
130 /** @brief stops all requests from this device (choose wait or force) */
131 int stop (bool force_stop);
132 int allocMemory (size_t size, void **addr);
133 int deallocMemory (int dmabuf_fd, size_t size, void *addr);
135 /** virtual methods to implement each device's behaviors */
136 virtual int setModel (const generic_buffer *model, Model **model_ptr) {
139 virtual int unsetModel (Model *model) { return -EPERM; }
141 virtual int getTensorSize (const Model *model, bool input, uint32_t index,
146 virtual int run (npu_input_opmode opmode, const Model *model,
147 const input_buffers *input, output_buffers *output = nullptr,
148 npuOutputNotify cb = nullptr, void *cb_data = nullptr) = 0;
150 virtual int createRequest (const Model *model, int *req_id) = 0;
151 virtual int removeRequest (int req_id) = 0;
152 virtual int getRequestModel (int req_id, uint32_t *model_id) = 0;
154 virtual int setRequestData (int req_id, input_buffers *input,
155 tensors_data_info *in_info,
156 output_buffers *output,
157 tensors_data_info *out_info) = 0;
158 virtual int setRequestCallback (int req_id, npuOutputNotify cb,
160 virtual int setRequestMode (int req_id, npu_infer_mode mode) = 0;
161 virtual int setRequestConstraint (int req_id, npu_constraint constraint) = 0;
162 virtual int setRequestScheduler (int req_id, npu_scheduler sched,
163 npu_scheduler_param param) = 0;
164 virtual int submitRequest (int req_id) = 0;
165 virtual int submitRequestKernel (int req_id) = 0;
168 /** the device instance has ownership of all related components */
169 std::unique_ptr<DriverAPI> api_; /**< device api */
170 std::unique_ptr<MemAllocator> mem_; /**< memory allocator */
171 std::unique_ptr<HostHandler> handler_; /**< host handler */
172 std::unique_ptr<Scheduler> scheduler_; /**< scheduler */
174 CommPlugin &comm_; /**< plugin communicator */
176 dev_type type_; /**< device type */
177 int id_; /**< device id */
178 bool need_model_; /**< indicates whether the device needs model */
179 npu_async_mode mode_; /**< async run mode */
181 /** @brief constructor of device */
182 Device (dev_type type, int id, bool need_model = true);
185 /** @brief initialization */
189 std::atomic_flag atomic_flag_; /**< atomic flag to check initilization */
192 /** @brief Trinity Vision2 (TRIV2) class */
193 class TrinityVision2 : public Device {
195 TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
196 ~TrinityVision2 () {}
198 static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
199 void *dst, void *src, size_t size);
201 SegmentTable *prepareSegmentTable (const Model *model,
202 const input_buffers *input,
203 const output_buffers *output = nullptr);
205 int setModel (const generic_buffer *model, Model **model_ptr);
206 int unsetModel (Model *model);
208 int getTensorSize (const Model *model, bool input, uint32_t index,
210 int checkDspmSize (const Model *model);
212 int run (npu_input_opmode opmode, const Model *model,
213 const input_buffers *input, output_buffers *output = nullptr,
214 npuOutputNotify cb = nullptr, void *cb_data = nullptr);
216 int createRequest (const Model *model, int *req_id);
217 int removeRequest (int req_id);
218 int getRequestModel (int req_id, uint32_t *model_id);
220 int setRequestData (int req_id, input_buffers *input,
221 tensors_data_info *in_info, output_buffers *output,
222 tensors_data_info *out_info);
223 int setRequestCallback (int req_id, npuOutputNotify cb, void *data);
224 int setRequestMode (int req_id, npu_infer_mode mode);
225 int setRequestConstraint (int req_id, npu_constraint constraint);
226 int setRequestScheduler (int req_id, npu_scheduler sched,
227 npu_scheduler_param sched_param);
228 int submitRequest (int req_id);
229 int submitRequestKernel (int req_id);
232 void callback (Request *req, npuOutputNotify cb, void *cb_data);
234 #endif /* __NPU_ENGINE_HANDLER_H__ */