[UnitTest] Add some negative cases for libnpuhost unittest
[platform/adaptation/npu/trix-engine.git] / src / core / ne-handler.cc
1 /**
2  * Proprietary
3  * Copyright (C) 2020 Samsung Electronics
4  * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
5  */
6 /**
7  * @file ne-host-handler.cc
8  * @date 03 Apr 2020
9  * @brief Implementation of APIs to access NPU from Host
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 #include "ne-handler.h"
16
17 #include <libnpuhost.h>
18 #include <npubinfmt.h>
19 #include <NPUdrvAPI.h>
20 #include <CommPlugin.h>
21
22 #include <string.h>
23 #include <assert.h>
24
25 #include <condition_variable>
26 #include <functional>
27 #include <atomic>
28 #include <map>
29
30 #define TAG _N2
31
32 #define INIT_HOST_HANDLER(handler, dev) \
33   Device *tdev = static_cast <Device *> (dev); \
34   if (tdev == nullptr) return -EINVAL; \
35   HostHandler *handler = tdev->getHostHandler (); \
36   if (handler == nullptr) return -EINVAL;
37
38 /** just for backward-compatability */
39 npudev_h HostHandler::latest_dev_ = nullptr;
40
41 /** implement libnpuhost APIs */
42
43 /**
44  * @brief Returns the number of available NPU devices.
45  * @return @c The number of NPU devices.
46  * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
47  * @note the caller should call putNPUdevice() to release the device handle
48  */
49 int getnumNPUdeviceByType (dev_type type)
50 {
51   return HostHandler::getNumDevices (type);
52 }
53
54 /**
55  * @brief Returns the handle of the chosen NPU devices.
56  * @param[out] dev The NPU device handle
57  * @param[in] id The NPU id to get the handle. 0 <= id < getnumNPUdeviceByType().
58  * @return @c 0 if no error. otherwise a negative error value
59  * @note the caller should call putNPUdevice() to release the device handle
60  */
61 int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
62 {
63   return HostHandler::getDevice (dev, type, id);
64 }
65
66 /**
67  * @brief release the NPU device instance obtained by getDevice ()
68  * @param[in] dev the NPU device handle
69  */
70 void putNPUdevice (npudev_h dev)
71 {
72   if (dev != nullptr)
73     delete static_cast<Device *> (dev);
74 }
75
76 /**
77  * @brief Send the NN model to NPU.
78  * @param[in] dev The NPU device handle
79  * @param[in] modelfile The filepath to the compiled NPU NN model in any buffer_type
80  * @param[out] modelid The modelid allocated for this instance of NN model.
81  * @return @c 0 if no error. otherwise a negative error value
82  *
83  * @detail For ASR devices, which do not accept models, but have models
84  *         embedded in devices, you do not need to call register and
85  *         register calls for ASR are ignored.
86  *
87  * @todo Add a variation: in-memory model register.
88  */
89 int registerNPUmodel (npudev_h dev, generic_buffer *modelfile, uint32_t *modelid)
90 {
91   INIT_HOST_HANDLER (host_handler, dev);
92
93   return host_handler->registerModel (modelfile, modelid);
94 }
95
96 /**
97  * @brief Remove the NN model from NPU
98  * @param[in] dev The NPU device handle
99  * @param[in] modelid The model to be removed from the NPU.
100  * @return @c 0 if no error. otherwise a negative error value
101  * @detail This may incur some latency with memory compatcion.
102  */
103 int unregisterNPUmodel(npudev_h dev, uint32_t modelid)
104 {
105   INIT_HOST_HANDLER (host_handler, dev);
106
107   return host_handler->unregisterModel (modelid);
108 }
109
110 /**
111  * @brief Remove all NN models from NPU
112  * @param[in] dev The NPU device handle
113  * @return @c 0 if no error. otherwise a negative error value
114  */
115 int unregisterNPUmodel_all(npudev_h dev)
116 {
117   INIT_HOST_HANDLER (host_handler, dev);
118
119   return host_handler->unregisterModels ();
120 }
121
122 /**
123  * @brief [OPTIONAL] Set the data layout for input/output tensors
124  * @param[in] dev The NPU device handle
125  * @param[in] modelid The ID of model whose layouts are set
126  * @param[in] info_in the layout/type info for input tensors
127  * @param[in] info_out the layout/type info for output tensors
128  * @return @c 0 if no error. otherwise a negative error value
129  * @note if this function is not called, default layout/type will be used.
130  */
131 int setNPU_dataInfo(npudev_h dev, uint32_t modelid,
132     tensors_data_info *info_in, tensors_data_info *info_out)
133 {
134   INIT_HOST_HANDLER (host_handler, dev);
135
136   return host_handler->setDataInfo (modelid, info_in, info_out);
137 }
138
139 /**
140  * @brief [OPTIONAL] Set the inference constraint for next NPU inferences
141  * @param[in] dev The NPU device handle
142  * @param[in] modelid The target model id
143  * @param[in] constraint inference constraint (e.g., timeout, priority)
144  * @return @c 0 if no error. otherwise a negative error value
145  * @note If this function is not called, default values are used.
146  */
147 int setNPU_constraint(npudev_h dev, uint32_t modelid, npuConstraint constraint)
148 {
149   INIT_HOST_HANDLER (host_handler, dev);
150
151   return host_handler->setConstraint (modelid, constraint);
152 }
153
154 /**
155  * @brief Execute inference. Wait (block) until the output is available.
156  * @param[in] dev The NPU device handle
157  * @param[in] modelid The model to be inferred.
158  * @param[in] input The input data to be inferred.
159  * @param[out] output The output result. The caller MUST allocate appropriately before calling this.
160  * @return @c 0 if no error. otherwise a negative error value
161  *
162  * @detail This is a syntactic sugar of runNPU_async().
163  *         CAUTION: There is a memcpy for the output buffer.
164  */
165 int runNPU_sync(npudev_h dev, uint32_t modelid, const input_buffers *input,
166     output_buffers *output)
167 {
168   INIT_HOST_HANDLER (host_handler, dev);
169
170   return host_handler->runSync (modelid, input, output);
171 }
172
173 /**
174  * @brief Invoke NPU inference. Unblocking call.
175  * @param[in] dev The NPU device handle
176  * @param[in] modelid The model to be inferred.
177  * @param[in] input The input data to be inferred.
178  * @param[in] cb The output buffer handler.
179  * @param[out] sequence The sequence number returned with runNPU_async.
180  * @param[in] data The data given as a parameter to the runNPU_async call.
181  * @param[in] mode Configures how this operation works.
182  * @return @c 0 if no error. otherwise a negative error value
183  */
184 int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
185     npuOutputNotify cb, uint64_t *sequence, void *data,
186     npu_async_mode mode)
187 {
188   INIT_HOST_HANDLER (host_handler, dev);
189
190   return host_handler->runAsync (modelid, input, cb, data, mode, sequence);
191 }
192
193 /**
194  * @brief Let NPU accept input frames from its internal source continuously
195  * @param[in] dev The NPU device handle
196  * @param[in] modelid The model to be inferred.
197  * @param[in] opmode NPU has different opmode with auto-inputs. Choose one.
198  * @param[in] hw_dev The target device feeding input data
199  * @return @c 0 if no error. otherwise a negative error value
200  */
201 int runNPU_internalInput(npudev_h dev, uint32_t modelid, npu_input_opmode opmode,
202     const char * hw_dev)
203 {
204   INIT_HOST_HANDLER (host_handler, dev);
205
206   return host_handler->runInternal (modelid, opmode, hw_dev);
207 }
208
209 /**
210  * @brief Stop the request with the given id
211  * @param[in] dev The NPU device handle
212  * @param[in] id The request id
213  * @return @c 0 if no error. otherwise a negative error value
214  */
215 int stopNPU_internalInput(npudev_h dev, int id)
216 {
217   INIT_HOST_HANDLER (host_handler, dev);
218
219   return host_handler->stopInternal (id);
220 }
221
222 /**
223  * @brief Allocate a generic buffer with the requested buffer type.
224  * @param[in] dev The NPU device handle
225  * @param[in/out] Buffer the buffer pointer where memory is allocated.
226  * @return 0 if no error, otherwise a negative errno.
227  */
228 int allocNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
229 {
230   INIT_HOST_HANDLER (host_handler, dev);
231
232   return host_handler->allocGenericBuffer (buffer);
233 }
234
235 /**
236  * @brief Free the generic buffer and remove the address mapping
237  * @param[in] dev The NPU device handle
238  * @param[in] buffer the model buffer
239  * @return 0 if no error, otherwise a negative errno.
240  */
241 int cleanNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
242 {
243   INIT_HOST_HANDLER (host_handler, dev);
244
245   return host_handler->deallocGenericBuffer (buffer);
246 }
247
248 /**
249  * @brief Allocate generic buffers, which have multiple instances of generic_buffer
250  * @param[in] dev The NPU device handle
251  * @param[in/out] buffers generic buffers.
252  * @return 0 if no error, otherwise a negative errno.
253  * @note it reuses allocGenericBuffer().
254  */
255 int allocNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
256 {
257   INIT_HOST_HANDLER (host_handler, dev);
258
259   return host_handler->allocGenericBuffer (buffers);
260 }
261
262 /**
263  * @brief Free generic buffers allocated by allocGenericBuffers().
264  * @param[in] dev The NPU device handle
265  * @param[in/out] buffers generic buffers.
266  * @note it reuses cleanGenericbuffer().
267  * @return 0 if no error, otherwise a negative errno.
268  */
269 int cleanNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
270 {
271   INIT_HOST_HANDLER (host_handler, dev);
272
273   return host_handler->deallocGenericBuffer (buffers);
274 }
275
276 /**
277  * @brief alias of allocNPU_genericBuffer for model buffer
278  */
279 int allocNPU_modelBuffer (npudev_h dev, generic_buffer * model)
280 {
281   return allocNPU_genericBuffer (dev, model);
282 }
283
284 /**
285  * @brief alias of cleanNPU_genericBuffer for model buffer
286  */
287 int cleanNPU_modelBuffer (npudev_h dev, generic_buffer * model)
288 {
289   return cleanNPU_genericBuffer (dev, model);
290 }
291
292 /**
293  * @brief alias of allocNPU_genericBuffer for input buffer
294  */
295 int allocNPU_inputBuffer (npudev_h dev, generic_buffer * input)
296 {
297   return allocNPU_genericBuffer (dev, input);
298 }
299
300 /**
301  * @brief alias of cleanNPU_genericBuffer for input buffer
302  */
303 int cleanNPU_inputBuffer (npudev_h dev, generic_buffer * input)
304 {
305   return cleanNPU_genericBuffer (dev, input);
306 }
307
308 /**
309  * @brief alias of allocNPU_genericBuffers for input buffers
310  */
311 int allocNPU_inputBuffers (npudev_h dev, input_buffers * input)
312 {
313   return allocNPU_genericBuffers (dev, input);
314 }
315
316 /**
317  * @brief alias of cleanNPU_genericBuffers for input buffers
318  */
319 int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input)
320 {
321   return cleanNPU_genericBuffers (dev, input);
322 }
323
324 /**
325  * @brief get the current memory status for the given device
326  * @param[in] dev The NPU device handle
327  * @param[out] alloc_total The size of allocated memory until now
328  * @param[out] free_total The size of freed memory until now
329  * @return @c 0 if no error. otherwise a negatice error value
330  */
331 int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total)
332 {
333   INIT_HOST_HANDLER (host_handler, dev);
334
335   return host_handler->getMemoryStatus (alloc_total, free_total);
336 }
337
338 /**
339  * @brief Get the current device status to be used
340  * @param[in] dev The NPU device handle
341  * @param[out] status the device status
342  * @param[out] num_requests the number of running requests (or pending)
343  * @return 0 if no error, otherwise a negative errno.
344  */
345 int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests)
346 {
347   INIT_HOST_HANDLER (host_handler, dev);
348
349   return host_handler->getDeviceStatus (status, num_requests);
350 }
351
352 /**
353  * @brief Get metadata for NPU model
354  * @param[in] model The path of model binary file
355  * @param[in] need_extra whether you want to extract the extra data in metadata
356  * @return the metadata structure to be filled if no error, otherwise nullptr
357  *
358  * @note For most npu-engine users, the extra data is not useful because it will be
359  *       used for second-party users (e.g., compiler, simulator).
360  *       Also, the caller needs to free the metadata.
361  *
362  * @note the caller needs to free the metadata
363  */
364 npubin_meta * getNPUmodel_metadata (const char *model, bool need_extra)
365 {
366   npubin_meta *meta;
367   FILE *fp;
368   size_t ret;
369
370   if (!model)
371     return nullptr;
372
373   fp = fopen (model, "rb");
374   if (!fp) {
375     logerr (TAG, "Failed to open the model binary: %d\n", -errno);
376     return nullptr;
377   }
378
379   meta = (npubin_meta *) malloc (NPUBIN_META_SIZE);
380   if (!meta) {
381     logerr (TAG, "Failed to allocate metadata\n");
382     goto exit_err;
383   }
384
385   ret = fread (meta, 1, NPUBIN_META_SIZE, fp);
386   if (ret != NPUBIN_META_SIZE) {
387     logerr (TAG, "Failed to read the metadata\n");
388     goto exit_free;
389   }
390
391   if (!CHECK_NPUBIN (meta->magiccode)) {
392     logerr (TAG, "Invalid metadata provided\n");
393     goto exit_free;
394   }
395
396   if (need_extra && NPUBIN_META_EXTRA (meta->magiccode) > 0) {
397     npubin_meta *new_meta;
398
399     new_meta = (npubin_meta *) malloc (NPUBIN_META_TOTAL_SIZE(meta->magiccode));
400     if (!new_meta) {
401       logerr (TAG, "Failed to allocate extra metadata\n");
402       goto exit_free;
403     }
404
405     memcpy (new_meta, meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
406     free (meta);
407     meta = nullptr;
408     ret = fread (new_meta->reserved_extra, 1, NPUBIN_META_EXTRA_SIZE (new_meta->magiccode), fp);
409     if (ret != NPUBIN_META_EXTRA_SIZE (new_meta->magiccode)) {
410       logerr (TAG, "Invalid extra metadata provided\n");
411       free (new_meta);
412       goto exit_err;
413     }
414
415     meta = new_meta;
416   }
417
418   fclose (fp);
419
420   return meta;
421
422 exit_free:
423   free (meta);
424 exit_err:
425   fclose (fp);
426
427   return nullptr;
428 }
429
430 /** implement methods of HostHandler class */
431
432 /** @brief host handler constructor */
433 HostHandler::HostHandler (Device *device)
434   : device_(device),
435     /* ignored as we don't use double buffering anymore, but for backward-compatibility */
436     async_mode_ (NPUASYNC_WAIT)
437 {
438 }
439
440 /** @brief host handler destructor */
441 HostHandler::~HostHandler ()
442 {
443 }
444
445 /**
446  * @brief register model from generic buffer
447  * @param[in] model_buf model buffer
448  * @param[out] modelid model id
449  * @return 0 if no error. otherwise a negative errno
450  */
451 int
452 HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid)
453 {
454   if (model_buf == nullptr || modelid == nullptr) {
455     logerr (TAG, "Invalid arguments given\n");
456     return -EINVAL;
457   }
458
459   Model *model = nullptr;
460   int status = device_->setModel (model_buf, &model);
461   if (status != 0) {
462     logerr (TAG, "Failed to set model: %d\n", status);
463     return status;
464   }
465
466   assert (model != nullptr);
467
468   status = models_.insert (model->getID(), model);
469   if (status != 0) {
470     logerr (TAG, "Failed to insert model id\n");
471     delete model;
472     return status;
473   }
474
475   *modelid = model->getID();
476   return 0;
477 }
478
479 /**
480  * @brief remove the registered model
481  * @param[in] modelid model id
482  * @return 0 if no error. otherwise a negative errno
483  */
484 int
485 HostHandler::unregisterModel (uint32_t modelid)
486 {
487   Model *model = models_.find (modelid);
488   if (model == nullptr)
489     return -ENOENT;
490
491   int status = device_->unsetModel (model);
492   if (status != 0) {
493     logerr (TAG, "Failed to unset model: %d\n", status);
494     return status;
495   }
496
497   return models_.remove (modelid);
498 }
499
500 /**
501  * @brief remove all registered models
502  * @return 0
503  */
504 int
505 HostHandler::unregisterModels ()
506 {
507   models_.clear ();
508   return 0;
509 }
510
511 /**
512  * @brief Set the data layout for input/output tensors
513  * @param[in] modelid The ID of model whose layouts are set
514  * @param[in] in the layout/type info for input tensors
515  * @param[in] out the layout/type info for output tensors
516  * @return @c 0 if no error. otherwise a negative error value
517  * @note if this function is not called, default layout/type will be used.
518  */
519 int
520 HostHandler::setDataInfo (uint32_t modelid, tensors_data_info *in,
521     tensors_data_info *out)
522 {
523   Model *model = models_.find (modelid);
524   if (model == nullptr)
525     return -ENOENT;
526
527   return model->setDataInfo (in, out);
528 }
529
530 /**
531  * @brief Set the inference constraint for next NPU inferences
532  * @param[in] modelid The target model id
533  * @param[in] constraint inference constraint (e.g., timeout, priority)
534  * @return @c 0 if no error. otherwise a negative error value
535  * @note If this function is not called, default values are used.
536  */
537 int
538 HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint)
539 {
540   Model *model = models_.find (modelid);
541   if (model == nullptr)
542     return -ENOENT;
543
544   model->setConstraint (constraint);
545
546   return 0;
547 }
548
549 /**
550  * @brief find and return model instance
551  * @param[in] modelid model id
552  * @return model instance if found. otherwise nullptr
553  */
554 Model *
555 HostHandler::getModel (uint32_t modelid)
556 {
557   return models_.find (modelid);
558 }
559
560 /** @brief dummay callback for runSync. */
561 class callbackSync {
562   public:
563     callbackSync (output_buffers *output) : output_(output), done_(false) {}
564
565     static void callback (output_buffers *output, uint64_t sequence, void *data) {
566       callbackSync *sync = static_cast<callbackSync *>(data);
567       sync->callback (output, sequence);
568     }
569
570     void callback (output_buffers *output, uint64_t sequence) {
571       if (output_ != nullptr) {
572         /** just copy internal variables of output buffers */
573         memcpy (output_, output, sizeof (output_buffers));
574       }
575       done_ = true;
576       cv_.notify_one ();
577     }
578
579     void wait () {
580       std::unique_lock<std::mutex> lock (m_);
581       cv_.wait (lock, [this]() { return done_; });
582     }
583
584   private:
585     std::mutex m_;
586     std::condition_variable cv_;
587     output_buffers *output_;
588     bool done_;
589 };
590
591 /**
592  * @brief Execute inference. Wait (block) until the output is available.
593  * @param[in] modelid The model to be inferred.
594  * @param[in] input The input data to be inferred.
595  * @param[out] output The output result.
596  * @return @c 0 if no error. otherwise a negative error value
597  */
598 int
599 HostHandler::runSync (uint32_t modelid, const input_buffers *input,
600     output_buffers *output)
601 {
602   callbackSync sync (output);
603   int status = runAsync (modelid, input, callbackSync::callback,
604       static_cast <void*> (&sync), NPUASYNC_DROP_OLD, nullptr);
605   if (status == 0) {
606     /** sync needs to wait callback */
607     sync.wait ();
608   }
609   return status;
610 }
611
612 /**
613  * @brief Invoke NPU inference. Unblocking call.
614  * @param[in] modelid The model to be inferred.
615  * @param[in] input The input data to be inferred.
616  * @param[in] cb The output buffer handler.
617  * @param[in] cb_data The data given as a parameter to the runNPU_async call.
618  * @param[in] mode Configures how this operation works.
619  * @param[out] sequence The sequence number returned with runNPU_async.
620  * @return @c 0 if no error. otherwise a negative error value
621  */
622 int
623 HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
624     npuOutputNotify cb, void *cb_data, npu_async_mode mode, uint64_t *sequence)
625 {
626   Model *model = nullptr;
627
628   if (device_->needModel()) {
629     model = getModel (modelid);
630     if (model == nullptr)
631       return -ENOENT;
632   }
633
634   /* check the given model before running */
635   if (model != nullptr && !model->finalize ()) {
636     logerr (TAG, "Failed to finalize the model. Please see the log messages\n");
637     return -EINVAL;
638   }
639
640   device_->setAsyncMode (mode);
641   return device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
642 }
643
644 /**
645  * @brief Let NPU accept input frames from its internal source continuously
646  * @param[in] modelid The model to be inferred.
647  * @param[in] opmode NPU has different opmode with auto-inputs. Choose one.
648  * @param[in] hw_dev The target device feeding input data
649  * @return @c 0 if no error. otherwise a negative error value
650  */
651 int
652 HostHandler::runInternal (uint32_t modelid, npu_input_opmode opmode,
653     std::string hw_dev)
654 {
655   Model *model = nullptr;
656
657   if (device_->needModel()) {
658     model = getModel (modelid);
659     if (model == nullptr)
660       return -ENOENT;
661   }
662
663   /* check the given model before running */
664   if (model != nullptr && !model->finalize ()) {
665     logerr (TAG, "Failed to finalize the model. Please see the log messages\n");
666     return -EINVAL;
667   }
668
669   return device_->runInternal (opmode, model, hw_dev);
670 }
671
672 /**
673  * @brief Stop the request with the given id
674  * @param[in] dev The NPU device handle
675  * @param[in] id The request id
676  * @return @c 0 if no error. otherwise a negative error value
677  */
678 int
679 HostHandler::stopInternal (int id)
680 {
681   if (id <= 0) {
682     logerr (TAG, "Unable to stop this request with id (%d)\n", id);
683     return -EINVAL;
684   }
685
686   const DriverAPI * api = device_->getDriverAPI ();
687   assert (api != nullptr);
688
689   return api->stop_target (id);
690 }
691
692 /**
693  * @brief get number of available devices
694  * @param[in] type device type
695  * @return number of devices
696  */
697 int
698 HostHandler::getNumDevices (dev_type type)
699 {
700   return DriverAPI::getNumDevices (type);
701 }
702
703 /**
704  * @brief get device instance
705  * @param[out] dev device instance
706  * @param[in] type device type
707  * @param[in] id device id
708  * @return 0 if no error. otherwise a negative errno
709  */
710 int
711 HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
712 {
713   int num_devices = getNumDevices (type);
714
715   /** check the validity of device id */
716   if (!(num_devices > 0 && id < static_cast<uint32_t>(num_devices))) {
717     logerr (TAG, "Invalid arguments provided\n");
718     return -ENODEV;
719   }
720
721   Device *device = Device::createInstance (type, id);
722   if (device == nullptr) {
723     logerr (TAG, "Failed to create a device with the given type\n");
724     return -EINVAL;
725   }
726
727   *dev = device;
728   /** This is just for backward-compatility; we don't guarantee its corresness */
729   latest_dev_ = *dev;
730
731   return 0;
732 }
733
734 /**
735  * @brief allocate generic buffer (just for users)
736  * @param[out] buffer buffer instance
737  * @return 0 if no error. otherwise a negative errno
738  */
739 int
740 HostHandler::allocGenericBuffer (generic_buffer *buffer)
741 {
742   if (buffer == NULL)
743     return -EINVAL;
744
745   if (buffer->size == 0) {
746     logerr (TAG, "Invalid size\n");
747     return -EINVAL;
748   }
749
750   if (buffer->size > UINT32_MAX) {
751     logerr (TAG, "Don't support such a large size");
752     return -ENOMEM;
753   }
754
755   switch (buffer->type) {
756     case BUFFER_FILE:
757       /* nothing to do */
758       if (buffer->filepath == nullptr)
759         return -EINVAL;
760       break;
761     case BUFFER_MAPPED:
762     {
763       /* now, npu-engine always provides dmabuf-based allocation */
764       void *addr = nullptr;
765       int dmabuf = device_->allocMemory (buffer->size, &addr);
766       if (dmabuf < 0)
767         return dmabuf;
768
769       buffer->dmabuf = dmabuf;
770       buffer->offset = 0;
771       buffer->addr = addr;
772     } break;
773     default:
774       return -EINVAL;
775   }
776
777   return 0;
778 }
779
780 /**
781  * @brief deallocate generic buffer (just for users)
782  * @param[in] buffer buffer instance
783  * @return 0 if no error. otherwise a negative errno
784  */
785 int
786 HostHandler::deallocGenericBuffer (generic_buffer *buffer)
787 {
788   if (buffer == NULL)
789     return -EINVAL;
790
791   switch (buffer->type) {
792     case BUFFER_FILE:
793       /** always true cuz nothing to do */
794       break;
795     case BUFFER_MAPPED:
796       return device_->deallocMemory (buffer->dmabuf, buffer->size, buffer->addr);
797     default:
798       return -EINVAL;
799   }
800
801   return 0;
802 }
803
804 /**
805  * @brief allocate multiple generic buffers (just for users)
806  * @param[out] buffers multi-buffer instance
807  * @return 0 if no error. otherwise a negative errno
808  */
809 int
810 HostHandler::allocGenericBuffer (generic_buffers *buffers)
811 {
812   uint32_t idx;
813   int status = 0;
814
815   if (buffers == NULL || buffers->num_buffers < 1)
816     return -EINVAL;
817
818   for (idx = 0; idx < buffers->num_buffers; idx++) {
819     status = allocGenericBuffer (&buffers->bufs[idx]);
820     if (status != 0)
821       goto free_buffer;
822   }
823
824   return 0;
825
826 free_buffer:
827   while (idx != 0) {
828     deallocGenericBuffer (&buffers->bufs[--idx]);
829   }
830
831   return status;
832 }
833
834 /**
835  * @brief deallocate multiple generic buffers (just for users)
836  * @param[in] buffers multi-buffer instance
837  * @return 0 if no error. otherwise a negative errno
838  */
839 int
840 HostHandler::deallocGenericBuffer (generic_buffers *buffers)
841 {
842   if (buffers == NULL || buffers->num_buffers < 1)
843     return -EINVAL;
844
845   for (uint32_t idx = 0; idx < buffers->num_buffers; idx++)
846     deallocGenericBuffer (&buffers->bufs[idx]);
847   buffers->num_buffers = 0;
848
849   return 0;
850 }
851
852 /**
853  * @brief get the current memory status
854  * @param[out] alloc_total The size of allocated memory until now
855  * @param[out] free_total The size of freed memory until now
856  * @return 0 if no error. otherwise a negatice error value
857  */
858 int
859 HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total)
860 {
861   /** API is always set in initialize () */
862   const DriverAPI * api = device_->getDriverAPI ();
863   assert (api != nullptr);
864
865   return api->getMemoryStatus (alloc_total, free_total);
866 }
867
868 /**
869  * @brief Get the current device status to be used
870  * @param[out] status the device status
871  * @param[out] num_requests the number of running requests (or pending)
872  * @return 0 if no error, otherwise a negative errno.
873  */
874 int
875 HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests)
876 {
877   /** API is always set in initialize () */
878   const DriverAPI * api = device_->getDriverAPI ();
879
880   if (!api)
881     return -EINVAL;
882
883   device_state_t state = api->isReady ();
884   if (state == device_state_t::STATE_READY) {
885     *num_requests = api->numRequests ();
886     if (*num_requests > 0)
887       *status = NPU_READY;
888     else
889       *status = NPU_IDLE;
890   } else {
891     *num_requests = 0;
892     *status = NPU_ERROR;
893   }
894
895   return 0;
896 }
897
898 /** implement methods of Device class */
899
900 /** @brief constructor of device */
901 Device::Device (dev_type type, int id, bool need_model)
902   : comm_ (CommPlugin::getCommPlugin()), type_ (type), id_ (id), need_model_ (true),
903     mode_ (NPUASYNC_WAIT), initialized_ (false), atomic_flag_ (ATOMIC_FLAG_INIT)
904 {
905 }
906
907 /**
908  * @brief create device instance depending on device type and id
909  * @param[in] type device type
910  * @param[in] id device id
911  * @return device instance
912  */
913 Device *
914 Device::createInstance (dev_type type, int id)
915 {
916   Device *device = nullptr;
917
918   switch (type & DEVICETYPE_MASK) {
919     case DEVICETYPE_TRIV:
920       device = new TrinityVision (id);
921       break;
922     case DEVICETYPE_TRIV2:
923       device = new TrinityVision2 (id);
924       break;
925     case DEVICETYPE_TRIA:
926       device = new TrinityAsr (id);
927       device->setNeedModel (false);
928       break;
929     default:
930       break;
931   }
932
933   if (device != nullptr && device->init () != 0) {
934     delete device;
935     device = nullptr;
936   }
937
938   return device;
939 }
940
941 /**
942  * @brief device initialization
943  * @return 0 if no error, otherwise a negative errno
944  * @note Init failures come from createDriverAPI() only.
945  */
946 int
947 Device::init ()
948 {
949   /** should be initilizaed only once */
950   if (!atomic_flag_.test_and_set()) {
951     /** create the corresponding driver API */
952     api_ = DriverAPI::createDriverAPI (type_, id_);
953     if (api_.get() == nullptr) {
954       atomic_flag_.clear();
955       logerr (TAG, "Failed to create driver API\n");
956       return -EINVAL;
957     }
958
959     handler_.reset (new HostHandler (this));
960     scheduler_.reset (new Scheduler (api_.get()));
961     mem_ = MemAllocator::createInstance (api_.get());
962
963     initialized_ = true;  /** c++11 does not provide test() of atomic flag */
964   }
965
966   return 0;
967 }
968
969 /**
970  * @brief stop all requests from this device
971  * @param[in] force_stop indicate the schedduler waits until to handle previous requests
972  * @return 0 if no error, otherwise a negative errno
973  */
974 int
975 Device::stop (bool force_stop)
976 {
977   if (!initialized ()) {
978     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
979     return -EPERM;
980   }
981
982   Request *req = new Request (NPUINPUT_STOP);
983   req->setForceStop (force_stop);
984   return scheduler_->submitRequest (req);
985 }
986
987 /**
988  * @brief allocate generic memory buffer
989  * @param[in] size the size to allocate
990  * @param[out] addr the mapped address
991  * @return dmabuf fd if no error, otherwise a negative errno
992  */
993 int
994 Device::allocMemory (size_t size, void **addr)
995 {
996   if (!initialized ()) {
997     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
998     return -EPERM;
999   }
1000
1001   if (size == 0 || addr == nullptr) {
1002     logerr (TAG, "Invalid arguments\n");
1003     return -EINVAL;
1004   }
1005
1006   return mem_->allocMemory (size, addr);
1007 }
1008
1009 /**
1010  * @brief deallocate generic memory buffer
1011  * @param[in] dmabuf_fd dmabuf file descriptor
1012  * @param[in] size buffer size
1013  * @param[in] addr mapped addr
1014  * @return 0 if no error, otherwise a negative errno
1015  */
1016 int
1017 Device::deallocMemory (int dmabuf_fd, size_t size, void * addr)
1018 {
1019   if (!initialized ()) {
1020     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1021     return -EPERM;
1022   }
1023
1024   if (dmabuf_fd < 0 || size == 0 || addr == nullptr) {
1025     logerr (TAG, "Invalid arguments\n");
1026     return -EINVAL;
1027   }
1028
1029   return mem_->deallocMemory (dmabuf_fd, size, addr);
1030 }
1031
1032 /**
1033  * @brief extract the buffer instance from input generic buffers
1034  * @param[in] meta the model metadata
1035  * @param[in] input the input generic buffers
1036  * @return the buffer instance
1037  */
1038 Buffer *
1039 TrinityVision::prepareInputBuffers (const Metadata *meta, const input_buffers *input)
1040 {
1041   if (meta == nullptr || input == nullptr ||
1042       meta->getInputNum() != input->num_buffers) {
1043     logerr (TAG, "Invalid metadata info provided\n");
1044     return nullptr;
1045   }
1046
1047   Buffer * buffer;
1048   const generic_buffer *first = &input->bufs[0];
1049   if (first->type == BUFFER_DMABUF) {
1050     buffer = mem_->allocBuffer (new HWmemExternal);
1051     if (buffer == nullptr)
1052       return nullptr;
1053
1054     buffer->setDmabuf (first->dmabuf);
1055     buffer->setOffset (first->offset);
1056     buffer->setSize (meta->getBufferSize());
1057   } else {
1058     buffer = mem_->allocBuffer (new HWmemDevice);
1059     if (buffer == nullptr)
1060       return nullptr;
1061
1062     int status = buffer->alloc (meta->getBufferSize ());
1063     if (status != 0) {
1064       logerr (TAG, "Failed to allocate buffer: %d\n", status);
1065       delete buffer;
1066       return nullptr;
1067     }
1068   }
1069
1070   int status = buffer->createTensors (meta);
1071   if (status != 0) {
1072     logerr (TAG, "Failed to create tensors: %d\n", status);
1073     delete buffer;
1074     buffer = nullptr;
1075   }
1076
1077   return buffer;
1078 }
1079
1080 /**
1081  * @brief implementation of TRIV's setModel ()
1082  * @param[in] model_buf the model generic buffer
1083  * @param[out] model the model instance
1084  * @return 0 if no error, otherwise a negative errno
1085  */
1086 int
1087 TrinityVision::setModel (const generic_buffer *model_buf, Model ** model_ptr)
1088 {
1089   if (!initialized ()) {
1090     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1091     return -EPERM;
1092   }
1093
1094   if (model_buf == nullptr || model_ptr == nullptr)
1095     return -EINVAL;
1096
1097   Model *model = nullptr;
1098   HWmem * hwmem_prog = nullptr;
1099   HWmem * hwmem_weight = nullptr;
1100   int status;
1101
1102   /** In TRIV1, model data (including program/weight) should be contiguous */
1103
1104   switch (model_buf->type) {
1105   case BUFFER_FILE:
1106   case BUFFER_MAPPED:
1107     model = mem_->allocModel (new HWmemDevice);
1108     if (model == nullptr) {
1109       logerr (TAG, "Failed to allocate model\n");
1110       return -ENOMEM;
1111     }
1112
1113     status = model->alloc (model_buf->size);
1114     if (status != 0) {
1115       logerr (TAG, "Failed to allocate model: %d\n", status);
1116       goto delete_exit;
1117     }
1118
1119     /** extract the whole model data */
1120     status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr);
1121     if (status != 0) {
1122       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1123       goto delete_exit;
1124     }
1125     break;
1126   default:
1127     return -EINVAL;
1128   }
1129
1130   status = model->setMetadata (model->getData());
1131   if (status != 0)
1132     goto delete_exit;
1133
1134   /** allocate program (optional; NOP) */
1135   if (model->getMetadata()->getProgramSize() > 0) {
1136     hwmem_prog = new HWmem (new HWmemChunk);
1137     model->setProgramData (hwmem_prog);
1138
1139     hwmem_prog->setParent (model);
1140     hwmem_prog->setOffset (model->getMetadata()->getMetaSize());
1141     status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
1142     if (status != 0) {
1143       logerr (TAG, "Failed to allocate program\n");
1144       goto delete_exit;
1145     }
1146   }
1147
1148   /** allocate weight (optional) */
1149   if (model->getMetadata()->getWeightSize() > 0) {
1150     hwmem_weight = new HWmem (new HWmemChunk);
1151     model->setWeightData (hwmem_weight);
1152
1153     hwmem_weight->setParent (model);
1154     hwmem_weight->setOffset (model->getMetadata()->getMetaSize() +
1155         model->getMetadata()->getProgramSize());
1156     status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
1157     if (status != 0) {
1158       logerr (TAG, "Failed to allocate program\n");
1159       goto delete_exit;
1160     }
1161   }
1162
1163   if (hwmem_prog != nullptr) {
1164     /** register this model to the driver */
1165     model_config_t config;
1166     config.dbuf_fd = hwmem_prog->getDmabuf ();
1167     config.program_size = hwmem_prog->getSize ();
1168     config.program_offset_addr = hwmem_prog->getOffset ();
1169     if (hwmem_weight != nullptr)
1170       config.weight_offset_addr = hwmem_weight->getOffset ();
1171
1172     status = api_->registerModel (&config);
1173     if (status != 0)
1174       goto delete_exit;
1175
1176     model->setInternalID(config.id);
1177   }
1178
1179   *model_ptr = model;
1180   return status;
1181
1182 delete_exit:
1183   delete model;
1184   return status;
1185 }
1186
1187 /**
1188  * @brief implementation of TRIV's unsetModel ()
1189  * @param[in] model the model instance
1190  * @return 0 if no error, otherwise a negative errno
1191  */
1192 int
1193 TrinityVision::unsetModel (Model * model)
1194 {
1195   if (!initialized ()) {
1196     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1197     return -EPERM;
1198   }
1199
1200   if (model == nullptr) {
1201     logerr (TAG, "Invalid model instance\n");
1202     return -EINVAL;
1203   }
1204
1205   if (model->getMetadata()->getProgramSize() > 0)
1206     return api_->deregisterModel (model->getInternalID ());
1207
1208   return 0;
1209 }
1210
1211 /**
1212  * @brief implementation of TRIV's run()
1213  * @param[in] opmode input opmode
1214  * @param[in] model the model instance
1215  * @param[in] input generic buffers of input data
1216  * @param[in] cb the output callback
1217  * @param[in] cb_data the output callback data
1218  * @param[out] sequence The sequence number returned with runNPU_async.
1219  */
1220 int
1221 TrinityVision::run (npu_input_opmode opmode, const Model *model,
1222     const input_buffers *input, npuOutputNotify cb, void *cb_data,
1223     uint64_t *sequence)
1224 {
1225   if (!initialized ()) {
1226     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1227     return -EPERM;
1228   }
1229
1230   if (opmode != NPUINPUT_HOST) {
1231     logerr (TAG, "TRIV supports only host inputservice\n");
1232     return -EINVAL;
1233   }
1234
1235   if (model == nullptr || input == nullptr) {
1236     logerr (TAG, "TRIV requires both model and input buffers\n");
1237     return -EINVAL;
1238   }
1239
1240   Buffer *buffer = prepareInputBuffers (model->getMetadata(), input);
1241   if (buffer == nullptr) {
1242     logerr (TAG, "Failed to extract buffer instance\n");
1243     return -EINVAL;
1244   }
1245
1246   if (!buffer->isExternal ()) {
1247     for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
1248       auto func = std::bind (TrinityVision::manipulateData, model, idx, true,
1249           std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1250       int status = comm_.extractGenericBuffer (&input->bufs[idx],
1251           buffer->getInputTensor(idx)->getData(), func);
1252       if (status != 0) {
1253         logerr (TAG, "Failed to feed input buffer: %d\n", status);
1254         return status;
1255       }
1256     }
1257   }
1258
1259   /** this device uses CMA buffer */
1260
1261   Request *req = new Request (opmode);
1262   req->setModel (model);
1263   req->setBuffer (buffer);
1264
1265   if (cb != nullptr)
1266     req->setCallback (std::bind (&TrinityVision::callback, this, req, cb, cb_data));
1267
1268   if (sequence != nullptr)
1269     *sequence = req->getID();
1270
1271   return scheduler_->submitRequest (req);
1272 }
1273
1274 /**
1275  * @brief callback of TRIV2 request
1276  * @param[in] req the request instance
1277  * @param[in] cb callback for completion
1278  * @param[in] cb_data callback data
1279  * @note The callback invoke does not gurantee the request was successful
1280  * @todo Check the request failures
1281  */
1282 void
1283 TrinityVision::callback (Request *req, npuOutputNotify cb, void *cb_data)
1284 {
1285   const Model *model = req->getModel ();
1286   Buffer *buffer = req->getBuffer ();
1287   output_buffers output = {
1288     .num_buffers = buffer->getOutputNum ()
1289   };
1290
1291   for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
1292     uint32_t output_tensor_size = model->getOutputTensorSize (idx);
1293
1294     if (buffer->isExternal ()) {
1295       output.bufs[idx].type = BUFFER_DMABUF;
1296       output.bufs[idx].size = output_tensor_size;
1297       output.bufs[idx].addr = buffer->getOutputTensor(idx)->getData();
1298     } else {
1299       output.bufs[idx].type = BUFFER_MAPPED;
1300       output.bufs[idx].size = output_tensor_size;
1301       /** user needs to free this */
1302       output.bufs[idx].addr = malloc (output_tensor_size);
1303
1304       auto func = std::bind (TrinityVision::manipulateData, model, idx, false,
1305           std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1306       int status = comm_.insertGenericBuffer (buffer->getOutputTensor(idx)->getData(),
1307           &output.bufs[idx], func);
1308       if (status != 0) {
1309         logerr (TAG, "Failed to return output buffer: %d\n", status);
1310       }
1311     }
1312   }
1313
1314   cb (&output, req->getID(), cb_data);
1315
1316   delete buffer;
1317 }
1318
1319 /**
1320  * @brief extract the segment table instance from input generic buffers
1321  * @param[in] model the model instance
1322  * @param[in] input the input generic buffers
1323  * @param[in] output the output generic buffers
1324  * @return the segment table instance
1325  */
1326 SegmentTable *
1327 TrinityVision2::prepareSegmentTable (const Model *model, const input_buffers *input,
1328     const output_buffers *output)
1329 {
1330   if (model == nullptr) {
1331     logerr (TAG, "Invalid arguments provided\n");
1332     return nullptr;
1333   }
1334
1335   const Metadata *meta = model->getMetadata ();
1336   if (meta == nullptr || (input != nullptr &&
1337         meta->getInputNum() != input->num_buffers)) {
1338     logerr (TAG, "Invalid metadata info provided\n");
1339     return nullptr;
1340   }
1341
1342   SegmentTable * segt = mem_->allocSegmentTable (new HWmemDevice);
1343   int status = segt->alloc ();
1344   if (status != 0) {
1345     logerr (TAG, "Failed to allocate segment table: %d\n", status);
1346     goto delete_segt;
1347   }
1348
1349   status = segt->createSegments (model, input, output);
1350   if (status != 0) {
1351     logerr (TAG, "Failed to create segments: %d\n", status);
1352     goto delete_segt;
1353   }
1354
1355   return segt;
1356
1357 delete_segt:
1358   delete segt;
1359   return nullptr;
1360 }
1361
1362 /**
1363  * @brief implementation of TRIV2's setModel ()
1364  * @param[in] model_buf the model generic buffer
1365  * @param[out] model the model instance
1366  * @return 0 if no error, otherwise a negative errno
1367  */
1368 int
1369 TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
1370 {
1371   if (!initialized ()) {
1372     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1373     return -EPERM;
1374   }
1375
1376   if (model_buf == nullptr || model_ptr == nullptr)
1377     return -EINVAL;
1378
1379   Model *model;
1380   int status;
1381
1382   switch (model_buf->type) {
1383   case BUFFER_FILE:
1384   case BUFFER_MAPPED:
1385     model = mem_->allocModel (new HWmemDevice);
1386     if (model == nullptr) {
1387       logerr (TAG, "Failed to allocate model\n");
1388       return -ENOMEM;
1389     }
1390
1391     status = model->alloc (NPUBIN_META_SIZE);
1392     if (status != 0) {
1393       logerr (TAG, "Failed to allocate model: %d\n", status);
1394       goto delete_exit;
1395     }
1396
1397     status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr,
1398         0, NPUBIN_META_SIZE);
1399     if (status != 0) {
1400       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1401       goto delete_exit;
1402     }
1403     break;
1404   default:
1405     return -EINVAL;
1406   }
1407
1408   status = model->setMetadata (model->getData());
1409   if (status != 0)
1410     goto delete_exit;
1411
1412   /** allocate program (optional; NOP) */
1413   if (model->getMetadata()->getProgramSize() > 0) {
1414     HWmem * hwmem_prog = new HWmem (new HWmemDevice);
1415     hwmem_prog->setDriverAPI (api_.get());
1416
1417     model->setProgramData (hwmem_prog);
1418
1419     status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
1420     if (status != 0) {
1421       logerr (TAG, "Failed to allocate program\n");
1422       goto delete_exit;
1423     }
1424
1425     status = comm_.extractGenericBuffer (model_buf, hwmem_prog->getData(), nullptr,
1426         model->getMetadata()->getMetaSize(),
1427         model->getMetadata()->getProgramSize());
1428     if (status != 0) {
1429       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1430       goto delete_exit;
1431     }
1432
1433     /** register this model to the driver */
1434     model_config_t config;
1435     config.dbuf_fd = hwmem_prog->getDmabuf ();
1436     config.program_size = hwmem_prog->getSize ();
1437     config.program_offset_addr = 0;
1438
1439     status = api_->registerModel (&config);
1440     if (status != 0)
1441       goto delete_exit;
1442
1443     model->setInternalID(config.id);
1444   }
1445
1446   /** allocate weight (optional) */
1447   if (model->getMetadata()->getWeightSize() > 0) {
1448     HWmem * hwmem_weight = new HWmem (new HWmemDevice);
1449     hwmem_weight->setDriverAPI (api_.get());
1450
1451     model->setWeightData (hwmem_weight);
1452
1453     status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
1454     if (status != 0) {
1455       logerr (TAG, "Failed to allocate program\n");
1456       goto delete_exit;
1457     }
1458
1459     status = comm_.extractGenericBuffer (model_buf, hwmem_weight->getData(), nullptr,
1460         model->getMetadata()->getMetaSize() + model->getMetadata()->getProgramSize(),
1461         model->getMetadata()->getWeightSize());
1462     if (status != 0) {
1463       logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1464       goto delete_exit;
1465     }
1466   }
1467
1468   *model_ptr = model;
1469   return status;
1470
1471 delete_exit:
1472   delete model;
1473   return status;
1474 }
1475
1476 /**
1477  * @brief implementation of TRIV2's unsetModel ()
1478  * @param[in] model the model instance
1479  * @return 0 if no error, otherwise a negative errno
1480  */
1481 int
1482 TrinityVision2::unsetModel (Model * model)
1483 {
1484   if (!initialized ()) {
1485     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1486     return -EPERM;
1487   }
1488
1489   if (model == nullptr) {
1490     logerr (TAG, "Invalid model instance\n");
1491     return -EINVAL;
1492   }
1493
1494   if (model->getMetadata()->getProgramSize() > 0)
1495     return api_->deregisterModel (model->getInternalID ());
1496
1497   return 0;
1498 }
1499
1500 /** @brief implementation of TRIV2's run() */
1501 int
1502 TrinityVision2::run (npu_input_opmode opmode, const Model *model,
1503     const input_buffers *input, npuOutputNotify cb, void *cb_data,
1504     uint64_t *sequence)
1505 {
1506   if (!initialized ()) {
1507     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1508     return -EPERM;
1509   }
1510
1511   if (opmode != NPUINPUT_HOST)
1512     return -EINVAL;
1513
1514   if (input == nullptr || input->num_buffers == 0)
1515     return -EINVAL;
1516
1517   /** this device uses segment table */
1518   SegmentTable * segt = prepareSegmentTable (model, input);
1519   if (segt == nullptr) {
1520     logerr (TAG, "Failed to create segment table instance\n");
1521     return -EINVAL;
1522   }
1523
1524   /** extract input data */
1525   for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
1526     size_t max_seg_size = segt->getInputSegment(idx)->getSize();
1527     uint32_t seg_offset = segt->getInputSegmentOffset(idx);
1528
1529     if (input->bufs[idx].size + seg_offset > max_seg_size) {
1530       logerr (TAG, "Too large input data provided: max segment size (%zu)\n",
1531           max_seg_size);
1532       return -ERANGE;
1533     }
1534
1535     if (!segt->getInputSegment(idx)->isExternal ()) {
1536       auto func = std::bind (TrinityVision2::manipulateData, model, idx, true,
1537           std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1538       int status = comm_.extractGenericBuffer (
1539           &input->bufs[idx],
1540           segt->getInputSegment(idx)->getData() + seg_offset,
1541           func);
1542       if (status != 0) {
1543         logerr (TAG, "Failed to feed input segment: %d\n", status);
1544         return status;
1545       }
1546     }
1547   }
1548
1549   Request *req = new Request (opmode);
1550   req->setModel (model);
1551   req->setSegmentTable (segt);
1552   req->setCallback (std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
1553
1554   if (sequence)
1555     *sequence = req->getID();
1556
1557   return scheduler_->submitRequest (req);
1558 }
1559
1560 /** @brief implementation of TRIV2's runInternal() */
1561 int
1562 TrinityVision2::runInternal (npu_input_opmode opmode, const Model *model,
1563     std::string hw_dev)
1564 {
1565   if (!initialized ()) {
1566     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1567     return -EPERM;
1568   }
1569
1570   if (opmode != NPUINPUT_HW_RECURRING)
1571     return -EINVAL;
1572
1573   /** this device uses segment table */
1574   SegmentTable * segt = prepareSegmentTable (model, nullptr, nullptr);
1575   if (segt == nullptr) {
1576     logerr (TAG, "Failed to create segment table instance\n");
1577     return -EINVAL;
1578   }
1579
1580   Request *req = new Request (opmode);
1581   req->setModel (model);
1582   req->setSegmentTable (segt);
1583   req->setHwDevice (hw_dev);
1584
1585   return scheduler_->submitRequest (req);
1586 }
1587
1588 /** @brief callback of TRIV2 request */
1589 void
1590 TrinityVision2::callback (Request *req, npuOutputNotify cb, void *cb_data)
1591 {
1592   const Model *model = req->getModel ();
1593   SegmentTable *segt = req->getSegmentTable ();
1594   output_buffers output = {
1595     .num_buffers = segt->getNumOutputSegments ()
1596   };
1597
1598   for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
1599     uint32_t output_tensor_size = model->getOutputTensorSize (idx);
1600
1601     output.bufs[idx].type = BUFFER_MAPPED;
1602     output.bufs[idx].size = output_tensor_size;
1603     /** user needs to free this */
1604     output.bufs[idx].addr = calloc (1, output_tensor_size);
1605
1606     auto func = std::bind (TrinityVision2::manipulateData, model, idx, false,
1607         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1608     int status = comm_.insertGenericBuffer (
1609         segt->getOutputSegment(idx)->getData() + segt->getOutputSegmentOffset(idx),
1610         &output.bufs[idx], func);
1611
1612     if (status != 0) {
1613       logerr (TAG, "Failed to return output buffer: %d\n", status);
1614     }
1615   }
1616
1617   cb (&output, req->getID(), cb_data);
1618
1619   delete segt;
1620 }
1621
1622 /** @brief implementation of TRIA's run(): WIP */
1623 int
1624 TrinityAsr::run (npu_input_opmode opmode, const Model *model,
1625     const input_buffers *input, npuOutputNotify cb, void *cb_data,
1626     uint64_t *sequence)
1627 {
1628   if (!initialized ()) {
1629     logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1630     return -EPERM;
1631   }
1632
1633   if (opmode != NPUINPUT_HOST)
1634     return -EINVAL;
1635
1636   if (input == nullptr || input->num_buffers != 1)
1637     return -EINVAL;
1638
1639   Buffer * buffer;
1640   int status;
1641   /** ASR does not require model and support only a single tensor */
1642   const generic_buffer *first_buf = &input->bufs[0];
1643   if (first_buf->type == BUFFER_DMABUF) {
1644     buffer = mem_->allocBuffer (new HWmemExternal);
1645     if (buffer == nullptr)
1646       return -ENOMEM;
1647
1648     buffer->setDmabuf (first_buf->dmabuf);
1649     buffer->setOffset (first_buf->offset);
1650     buffer->setSize (first_buf->size);
1651   } else {
1652     buffer = mem_->allocBuffer (new HWmemDevice);
1653     if (buffer == nullptr)
1654       return -ENOMEM;
1655
1656     status = buffer->alloc (first_buf->size);
1657     if (status != 0) {
1658       delete buffer;
1659       return status;
1660     }
1661   }
1662
1663   status = buffer->createTensors ();
1664   if (status != 0) {
1665     logerr (TAG, "Failed to create tensors: %d\n", status);
1666     delete buffer;
1667     return status;
1668   }
1669
1670   if (!buffer->isExternal ()) {
1671     status = comm_.extractGenericBuffer (first_buf,
1672         buffer->getInputTensor(0)->getData(), nullptr);
1673     if (status != 0)
1674       return status;
1675   }
1676
1677   Request *req = new Request (opmode);
1678   req->setBuffer (buffer);
1679   req->setCallback (std::bind (&TrinityAsr::callback, this, req, cb, cb_data));
1680
1681   if (sequence)
1682     *sequence = req->getID();
1683
1684   return scheduler_->submitRequest (req);
1685 }
1686
1687 /** @brief callback of TRIA request: WIP */
1688 void
1689 TrinityAsr::callback (Request *req, npuOutputNotify cb, void *cb_data)
1690 {
1691   Buffer *buffer = req->getBuffer ();
1692   output_buffers output = {
1693     .num_buffers = 1
1694   };
1695
1696   /** TODO: finalize this impl. when the ASR's working scenario is determined */
1697   cb (&output, req->getID(), cb_data);
1698
1699   delete buffer;
1700 }
1701
1702 /** Implement data manipulation (each device may have different impl.) */
1703
1704 #ifdef ENABLE_MANIP
1705
1706 #define do_quantized_memcpy(type) do {\
1707     idx = 0;\
1708     if (quant) {\
1709       while (idx < num_elems) {\
1710           val = ((type *) src)[idx];\
1711           val = val / _scale;\
1712           val += _zero_point;\
1713           val = (val > 255.0) ? 255.0 : 0.0;\
1714           ((uint8_t *) dst)[idx++] = (uint8_t) val;\
1715       }\
1716     } else {\
1717       while (idx < num_elems) {\
1718           val = *(uint8_t *) src;\
1719           val -= _zero_point;\
1720           val *= _scale;\
1721           ((type *) dst)[idx++] = (type) val;\
1722           dst = (void*)(((uint8_t *) dst) + data_size);\
1723           src = (void*)(((uint8_t *) src) + 1);\
1724       }\
1725     }\
1726   } while (0)
1727
1728 /**
1729  * @brief memcpy during quantization
1730  */
1731 static void memcpy_with_quant (bool quant, data_type type, float scale, uint32_t zero_point,
1732     void *dst, const void *src, uint32_t num_elems)
1733 {
1734   double _scale = (double) scale;
1735   double _zero_point = (double) zero_point;
1736   double val;
1737   uint32_t data_size = get_data_size (type);
1738   uint32_t idx;
1739
1740   switch (type) {
1741     case DATA_TYPE_INT8:
1742       do_quantized_memcpy (int8_t);
1743       break;
1744     case DATA_TYPE_UINT8:
1745       do_quantized_memcpy (uint8_t);
1746       break;
1747     case DATA_TYPE_INT16:
1748       do_quantized_memcpy (int16_t);
1749       break;
1750     case DATA_TYPE_UINT16:
1751       do_quantized_memcpy (uint16_t);
1752       break;
1753     case DATA_TYPE_INT32:
1754       do_quantized_memcpy (int32_t);
1755       break;
1756     case DATA_TYPE_UINT32:
1757       do_quantized_memcpy (uint32_t);
1758       break;
1759     case DATA_TYPE_INT64:
1760       do_quantized_memcpy (int64_t);
1761       break;
1762     case DATA_TYPE_UINT64:
1763       do_quantized_memcpy (uint64_t);
1764       break;
1765     case DATA_TYPE_FLOAT32:
1766       do_quantized_memcpy (float);
1767       break;
1768     case DATA_TYPE_FLOAT64:
1769       do_quantized_memcpy (double);
1770       break;
1771     default:
1772       logerr (TAG, "Unsupported datatype %d\n", type);
1773   }
1774 }
1775
1776 /**
1777  * @brief perform data manipulation
1778  * @param[in] model model instance
1779  * @param[in] idx tensor index
1780  * @param[in] is_input indicate it's input manipulation
1781  * @param[out] dst destination buffer
1782  * @param[in] src source buffer (feature map)
1783  * @param[in] size size to be copied
1784  * @return size of memory copy if no error, otherwise zero
1785  *
1786  * @note the input data format should be NHWC
1787  * @detail rules for the memory address of activations in NPU HW.
1788  *         (https://code.sec.samsung.net/confluence/pages/viewpage.action?pageId=146491864)
1789  *
1790  * 1) Special case (depth == 3)
1791  * - addr(x,y,z) = addr(0,0,0) + (z) + 3 * (x + width * y)
1792  *
1793  * 2) Common case
1794  * - addr(x,y,z) = addr(0,0,0) + (z % MPA_L) + MPA_L * (x + width * (y + height * (z / MPA_L)))
1795  *
1796  * Thus, if depth is not a multiple of MPA_L (i.e., 64), zero padding is required
1797  */
1798 size_t
1799 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
1800     void *dst, void *src, size_t size)
1801 {
1802   const Metadata *meta = model->getMetadata();
1803   const tensor_data_info* info;
1804   const uint32_t *dims;
1805   uint32_t zero_point;
1806   float scale;
1807
1808   /** extract required information from the metadata */
1809   if (is_input) {
1810     if (idx >= meta->getInputNum()) {
1811       logerr (TAG, "Wrong information for input tensors in metadata\n");
1812       return 0;
1813     }
1814
1815     info = model->getInputDataInfo (idx);
1816     dims = meta->getInputDims (idx);
1817     zero_point = meta->getInputQuantZero (idx);
1818     scale = meta->getInputQuantScale (idx);
1819   } else {
1820     if (idx >= meta->getOutputNum()) {
1821       logerr (TAG, "Wrong information for output tensors in metadata\n");
1822       return 0;
1823     }
1824
1825     info = model->getOutputDataInfo (idx);
1826     dims = meta->getOutputDims (idx);
1827     zero_point = meta->getOutputQuantZero (idx);
1828     scale = meta->getOutputQuantScale (idx);
1829   }
1830
1831   if (info == nullptr) {
1832     logerr (TAG, "Unmatched tensors info\n");
1833     return 0;
1834   }
1835
1836   uint32_t batch = dims[0];
1837   uint32_t height = dims[1];
1838   uint32_t width = dims[2];
1839   uint32_t depth = dims[3];
1840
1841   uint32_t data_size = get_data_size (info->type);
1842   if (data_size == 0) {
1843     logerr (TAG, "Invalid data size\n");
1844     return 0;
1845   }
1846
1847   bool need_quantization = false;
1848   /**
1849    * note that we assume DATA_TYPE_SRNPU is the smallest data type that we consider.
1850    * Also, DATA_TYPE_SRNPU and uint8_t may be regarded as the same in the view of apps.
1851    */
1852   if (info->type != DATA_TYPE_SRNPU) {
1853     assert (data_size >= get_data_size (DATA_TYPE_SRNPU));
1854
1855     if (data_size > get_data_size (DATA_TYPE_SRNPU) ||
1856         !(zero_point == default_quant_zero && scale == default_quant_scale))
1857       need_quantization = true;
1858   }
1859
1860   /** check data manipulation is required */
1861   if (depth != 3 && depth != 64 && info->layout != DATA_LAYOUT_SRNPU) {
1862     uint32_t MPA_L = DATA_GRANULARITY;
1863     uint32_t n, h, w, d;
1864     uint32_t std_offset;  /* standard offset in NHWC data format */
1865     uint32_t npu_offset;  /* npu offset in NPU HW data format*/
1866     uint32_t src_offset;
1867     uint32_t dst_offset;
1868     uint32_t slice_size;
1869
1870     /* @todo we currently support only NHWC */
1871     if (info->layout != DATA_LAYOUT_NHWC) {
1872       logerr (TAG, "data manipulation is supported for NHWC only\n");
1873       return 0;
1874     }
1875
1876     for (n = 0; n < batch; n++) {
1877       for (h = 0; h < height; h++) {
1878         for (w = 0; w < width; w++) {
1879           for (d = 0; d < depth; d += MPA_L) {
1880             std_offset = d + depth * (w + width * (h + n * height));
1881             npu_offset = MPA_L * (w + width * (h + (n + d / MPA_L) * height));
1882             slice_size = (depth - d >= MPA_L) ? MPA_L : depth - d;
1883
1884             if (is_input) {
1885               src_offset = std_offset * data_size;
1886               dst_offset = npu_offset;
1887             } else {
1888               src_offset = npu_offset;
1889               dst_offset = std_offset * data_size;
1890             }
1891
1892             /* if depth is not a multiple of MPA_L, add zero paddings (not exact values) */
1893             if (need_quantization) {
1894               memcpy_with_quant (is_input, info->type, scale, zero_point,
1895                   static_cast<char*>(dst) + dst_offset,
1896                   static_cast<char*>(src) + src_offset,
1897                   slice_size);
1898             } else {
1899               memcpy (
1900                   static_cast<char*>(dst) + dst_offset,
1901                   static_cast<char*>(src) + src_offset,
1902                   slice_size);
1903             }
1904           }
1905         }
1906       }
1907     }
1908   } else if (need_quantization) {
1909     /** depth == 3 || depth == 64; special cases which can directly copy input tensor data */
1910     memcpy_with_quant (is_input, info->type, scale, zero_point,
1911         dst, src, is_input ? size / data_size : size);
1912   } else {
1913     memcpy (dst, src, size);
1914   }
1915
1916   return size;
1917 }
1918
1919 #else
1920
1921 size_t
1922 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
1923     void *dst, void *src, size_t size)
1924 {
1925   memcpy (dst, src, size);
1926   return size;
1927 }
1928
1929 #endif
1930
1931 /** other device types don't have data manip impl. yet */
1932
1933 size_t
1934 TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
1935     void *dst, void *src, size_t size)
1936 {
1937   memcpy (dst, src, size);
1938   return size;
1939 }
1940
1941 size_t
1942 TrinityAsr::manipulateData (const Model *model, uint32_t idx, bool is_input,
1943     void *dst, void *src, size_t size)
1944 {
1945   memcpy (dst, src, size);
1946   return size;
1947 }