39c8f7eea7f57b9b2dbafdcf0434ef3a69623757
[platform/adaptation/npu/trix-engine.git] / src / core / ne-handler.h
1 /**
2  * Proprietary
3  * Copyright (C) 2019 Samsung Electronics
4  * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
5  */
6 /**
7  * @file NE-handler.h
8  * @date 25 Jun 2019
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
13  */
14
15 #ifndef __NPU_ENGINE_HANDLER_H__
16 #define __NPU_ENGINE_HANDLER_H__
17
18 #include <typedef.h>
19 #include <CommPlugin.h>
20
21 #include "ne-mem.h"
22 #include "ne-scheduler.h"
23 #include "ne-model.h"
24 #include "ne-utils.h"
25 #include "ne-profiler.h"
26
27 class Device;
28 /** @brief class def. of host handler */
29 class HostHandler {
30   friend class Device;
31
32  public:
33   ~HostHandler ();
34
35   int registerModel (generic_buffer *model_buf, uint32_t *modelid);
36   int unregisterModel (uint32_t modelid);
37   int unregisterModels ();
38
39   int getTensorSize (uint32_t modelid, bool input, uint32_t index,
40                      uint32_t *size);
41
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);
47
48   int setDataInfo (uint32_t modelid, tensors_data_info *in,
49                    tensors_data_info *out);
50   int setConstraint (uint32_t modelid, npuConstraint constraint);
51
52   Model *getModel (uint32_t modelid);
53
54   int allocGenericBuffer (generic_buffer *buffer);
55   int allocGenericBuffer (generic_buffers *buffers);
56
57   int deallocGenericBuffer (generic_buffer *buffer);
58   int deallocGenericBuffer (generic_buffers *buffers);
59
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);
63
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);
67
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);
76
77   int submitRequest (int req_id);
78   int submitRequestKernel (int req_id);
79
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);
83
84   int getStatApps (npu_stat_apps *stat);
85   int getStatReqs (int appid, npu_stat_reqs *stat);
86
87   static int getNumDevices (dev_type type);
88   static int getDevice (npudev_h *dev, dev_type type, uint32_t id);
89
90  private:
91   /** only Device can create a HostHandler instance */
92   HostHandler (Device *device);
93
94   Device *device_; /**< dedicated device instance */
95   ModelProfiler *profiler_;
96
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 */
103 };
104
105 /**
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()
109  */
110 class Device {
111  public:
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 () {}
116
117   /** primitives */
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; }
122
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 (); }
127
128   /** the below requires initialized variables */
129
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);
134
135   /** virtual methods to implement each device's behaviors */
136   virtual int setModel (const generic_buffer *model, Model **model_ptr) {
137     return -EPERM;
138   }
139   virtual int unsetModel (Model *model) { return -EPERM; }
140
141   virtual int getTensorSize (const Model *model, bool input, uint32_t index,
142                              uint32_t *size) {
143     return -EPERM;
144   }
145
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;
149
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;
153
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,
159                                   void *data) = 0;
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;
166
167  protected:
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 */
173
174   CommPlugin &comm_; /**< plugin communicator */
175
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 */
180
181   /** @brief constructor of device */
182   Device (dev_type type, int id, bool need_model = true);
183
184  private:
185   /** @brief initialization */
186   int init ();
187
188   bool initialized_;
189   std::atomic_flag atomic_flag_; /**< atomic flag to check initilization */
190 };
191
192 /** @brief Trinity Vision2 (TRIV2) class */
193 class TrinityVision2 : public Device {
194  public:
195   TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
196   ~TrinityVision2 () {}
197
198   static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
199                                 void *dst, void *src, size_t size);
200
201   SegmentTable *prepareSegmentTable (const Model *model,
202                                      const input_buffers *input,
203                                      const output_buffers *output = nullptr);
204
205   int setModel (const generic_buffer *model, Model **model_ptr);
206   int unsetModel (Model *model);
207
208   int getTensorSize (const Model *model, bool input, uint32_t index,
209                      uint32_t *size);
210   int checkDspmSize (const Model *model);
211
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);
215
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);
219
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);
230
231  private:
232   void callback (Request *req, npuOutputNotify cb, void *cb_data);
233 };
234 #endif /* __NPU_ENGINE_HANDLER_H__ */