3 * Copyright (C) 2020 Samsung Electronics
4 * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
7 * @file ne-host-handler.cc
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
15 #include <npubinfmt.h>
16 #include <NPUdrvAPI.h>
17 #include <CommPlugin.h>
21 #include "ne-scheduler.h"
22 #include "ne-handler.h"
27 #include <condition_variable>
34 #define INIT_HOST_HANDLER(handler, dev) \
35 Device *tdev = static_cast <Device *> (dev); \
36 if (tdev == nullptr) return -EINVAL; \
37 HostHandler *handler = tdev->getHostHandler (); \
38 if (handler == nullptr) return -EINVAL;
40 /** @brief device class. it contains all related instances */
43 /** @brief Factory method to create a trinity device dependong on dev type */
44 static Device *createInstance (dev_type device_type, int device_id);
46 /** @brief constructor of device */
47 Device (dev_type type, int id, bool need_model = true)
48 : comm_(CommPlugin::getCommPlugin()), type_ (type), id_ (id),
49 need_model_ (true), mode_ (NPUASYNC_WAIT), initialized_ (ATOMIC_FLAG_INIT) {}
51 /** @brief destructor of device */
54 /** @brief initialization */
56 if (!initialized_.test_and_set()) {
57 /** create the corresponding driver API */
58 api_ = DriverAPI::createDriverAPI (type_, id_);
59 if (api_.get() == nullptr) {
61 logerr (TAG, "Failed to create driver API\n");
65 handler_.reset (new HostHandler (this));
66 scheduler_.reset (new Scheduler (api_.get()));
67 mem_ = MemAllocator::createInstance (api_.get());
73 HostHandler *getHostHandler () { return handler_.get(); }
74 dev_type getType () { return type_; }
75 int getID () { return id_; }
76 bool needModel () { return need_model_; }
77 void setAsyncMode (npu_async_mode mode) { mode_ = mode; }
79 HWmem * allocMemory () { return mem_->allocMemory (); }
80 void deallocMemory (int dmabuf_fd) { mem_->deallocMemory (dmabuf_fd); }
82 /** it stops all requests in this device (choose wait or force) */
83 int stop (bool force_stop) {
84 Request *req = new Request (NPUINPUT_STOP);
85 req->setForceStop (force_stop);
86 return scheduler_->submitRequest (req);
89 virtual Model * registerModel (const generic_buffer *model) = 0;
90 virtual int run (npu_input_opmode opmode, const Model *model,
91 const input_buffers *input, npuOutputNotify cb, void *cb_data,
92 uint64_t *sequence) = 0;
95 /** the device instance has ownership of all related components */
96 std::unique_ptr<DriverAPI> api_; /**< device api */
97 std::unique_ptr<MemAllocator> mem_; /**< memory allocator */
98 std::unique_ptr<HostHandler> handler_; /**< host handler */
99 std::unique_ptr<Scheduler> scheduler_; /**< scheduler */
101 CommPlugin& comm_; /**< plugin communicator */
103 dev_type type_; /**< device type */
104 int id_; /**< device id */
105 bool need_model_; /**< indicates whether the device needs model */
106 npu_async_mode mode_; /**< async run mode */
109 std::atomic_flag initialized_;
112 /** @brief Trinity Vision (TRIV) classs */
113 class TrinityVision : public Device {
115 TrinityVision (int id) : Device (NPUCOND_TRIV_CONN_SOCIP, id) {}
118 static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
119 void *dst, void *src, size_t size);
121 Model * registerModel (const generic_buffer *model_buf) {
122 Model *model = mem_->allocModel ();
123 if (model == nullptr) {
124 logerr (TAG, "Failed to allocate model\n");
129 if (model_buf->type == BUFFER_DMABUF) {
130 model->setDmabuf (model_buf->dmabuf);
131 model->setOffset (model_buf->offset);
132 model->setSize (model_buf->size);
134 status = model->alloc (model_buf->size);
136 logerr (TAG, "Failed to allocate model: %d\n", status);
140 status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr);
142 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
147 status = model->setMetadata (model->getData());
151 model_config_t config;
152 config.dmabuf_id = model->getDmabuf();
153 config.program_size = model->getMetadata()->getProgramSize();
154 config.program_offset_addr = model->getOffset() + model->getMetadata()->getMetaSize();
155 config.weight_offset_addr = config.program_offset_addr + config.program_size;
157 status = api_->setModel (&config);
168 Buffer * prepareInputBuffers (const Model *model, const input_buffers *input) {
169 const Metadata *meta = model->getMetadata();
170 const generic_buffer *first = &input->bufs[0];
172 if (meta->getInputNum() != input->num_buffers)
175 Buffer * buffer = mem_->allocBuffer ();
176 if (buffer != nullptr) {
177 if (first->type == BUFFER_DMABUF) {
178 buffer->setDmabuf (first->dmabuf);
179 buffer->setOffset (first->offset);
180 buffer->setSize (meta->getBufferSize());
182 int status = buffer->alloc (meta->getBufferSize ());
184 logerr (TAG, "Failed to allocate buffer: %d\n", status);
191 buffer->createTensors (meta);
195 int run (npu_input_opmode opmode, const Model *model,
196 const input_buffers *input, npuOutputNotify cb, void *cb_data,
197 uint64_t *sequence) {
198 if (opmode != NPUINPUT_HOST)
201 Buffer *buffer = prepareInputBuffers (model, input);
202 if (buffer == nullptr)
205 for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
206 auto func = std::bind (TrinityVision::manipulateData, model, idx, true,
207 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
208 int status = comm_.extractGenericBuffer (&input->bufs[idx],
209 buffer->getInputTensor(idx)->getData(), func);
211 logerr (TAG, "Failed to feed input buffer: %d\n", status);
216 /** this device uses CMA buffer */
218 Request *req = new Request (opmode);
219 req->setModel (model);
220 req->setBuffer (buffer);
221 req->setCallback (std::bind (&TrinityVision::callback, this, req, cb, cb_data));
224 *sequence = req->getID();
226 return scheduler_->submitRequest (req);
229 void callback (Request *req, npuOutputNotify cb, void *cb_data) {
230 const Model *model = req->getModel ();
231 Buffer *buffer = req->getBuffer ();
232 output_buffers output = {
233 .num_buffers = buffer->getOutputNum ()
236 for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
237 uint32_t output_tensor_size = model->getOutputTensorSize (idx);
239 output.bufs[idx].type = BUFFER_MAPPED;
240 output.bufs[idx].size = output_tensor_size;
241 /** user needs to free this */
242 output.bufs[idx].addr = malloc (output_tensor_size);
244 auto func = std::bind (TrinityVision::manipulateData, model, idx, false,
245 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
246 int status = comm_.insertGenericBuffer (buffer->getOutputTensor(idx)->getData(),
247 &output.bufs[idx], func);
249 logerr (TAG, "Failed to return output buffer: %d\n", status);
253 cb (&output, req->getID(), cb_data);
257 /** @brief Trinity Vision2 (TRIV2) classs */
258 class TrinityVision2 : public Device {
260 TrinityVision2 (int id) : Device (NPUCOND_TRIV2_CONN_SOCIP, id) {}
261 ~TrinityVision2 () {}
263 static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
264 void *dst, void *src, size_t size) {
265 memcpy (dst, src, size);
269 Model * registerModel (const generic_buffer *model_buf) {
270 /** TODO: model's weight values are stored in segments */
274 int run (npu_input_opmode opmode, const Model *model,
275 const input_buffers *input, npuOutputNotify cb, void *cb_data,
276 uint64_t *sequence) {
277 if (opmode != NPUINPUT_HOST && opmode != NPUINPUT_HW_RECURRING)
280 /** this device uses segment table */
282 Request *req = new Request (opmode);
283 req->setModel (model);
285 req->setSegmentTable (segt);
287 req->setCallback (std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
290 *sequence = req->getID();
292 return scheduler_->submitRequest (req);
295 void callback (Request *req, npuOutputNotify cb, void *cb_data) {
299 /** @brief Trinity Asr (TRIA) classs */
300 class TrinityAsr : public Device {
302 TrinityAsr (int id) : Device (NPUCOND_TRIA_CONN_SOCIP, id, false) {}
305 static size_t manipulateData (const Model *model, uint32_t idx, bool is_input,
306 void *dst, void *src, size_t size) {
307 memcpy (dst, src, size);
311 Model * registerModel (const generic_buffer *model_buf) { return nullptr; }
313 int run (npu_input_opmode opmode, const Model *model,
314 const input_buffers *input, npuOutputNotify cb, void *cb_data,
315 uint64_t *sequence) {
316 if (opmode != NPUINPUT_HOST)
319 /** ASR does not require model and support only a single tensor */
320 const generic_buffer *first_buf = &input->bufs[0];
321 Buffer * buffer = mem_->allocBuffer ();
323 if (first_buf->type == BUFFER_DMABUF) {
324 buffer->setDmabuf (first_buf->dmabuf);
325 buffer->setOffset (first_buf->offset);
326 buffer->setSize (first_buf->size);
328 status = buffer->alloc (first_buf->size);
334 buffer->createTensors ();
336 status = comm_.extractGenericBuffer (first_buf,
337 buffer->getInputTensor(0)->getData(), nullptr);
341 Request *req = new Request (opmode);
342 req->setBuffer (buffer);
343 req->setCallback (std::bind (&TrinityAsr::callback, this, req, cb, cb_data));
346 *sequence = req->getID();
348 return scheduler_->submitRequest (req);
351 void callback (Request *req, npuOutputNotify cb, void *cb_data) {
357 #define do_quantized_memcpy(type) do {\
360 while (idx < num_elems) {\
361 val = ((type *) src)[idx];\
364 val = (val > 255.0) ? 255.0 : 0.0;\
365 ((uint8_t *) dst)[idx++] = (uint8_t) val;\
368 while (idx < num_elems) {\
369 val = *(uint8_t *) src;\
372 ((type *) dst)[idx++] = (type) val;\
373 dst = (void*)(((uint8_t *) dst) + data_size);\
374 src = (void*)(((uint8_t *) src) + 1);\
380 * @brief memcpy during quantization
382 static void memcpy_with_quant (bool quant, data_type type, float scale, uint32_t zero_point,
383 void *dst, const void *src, uint32_t num_elems)
385 double _scale = (double) scale;
386 double _zero_point = (double) zero_point;
388 uint32_t data_size = get_data_size (type);
393 do_quantized_memcpy (int8_t);
395 case DATA_TYPE_UINT8:
396 do_quantized_memcpy (uint8_t);
398 case DATA_TYPE_INT16:
399 do_quantized_memcpy (int16_t);
401 case DATA_TYPE_UINT16:
402 do_quantized_memcpy (uint16_t);
404 case DATA_TYPE_INT32:
405 do_quantized_memcpy (int32_t);
407 case DATA_TYPE_UINT32:
408 do_quantized_memcpy (uint32_t);
410 case DATA_TYPE_INT64:
411 do_quantized_memcpy (int64_t);
413 case DATA_TYPE_UINT64:
414 do_quantized_memcpy (uint64_t);
416 case DATA_TYPE_FLOAT32:
417 do_quantized_memcpy (float);
419 case DATA_TYPE_FLOAT64:
420 do_quantized_memcpy (double);
423 logerr (TAG, "Unsupported datatype %d\n", type);
428 * @brief perform data manipulation
429 * @param[in] model model instance
430 * @param[in] idx tensor index
431 * @param[in] is_input indicate it's input manipulation
432 * @param[out] dst destination buffer
433 * @param[in] src source buffer (feature map)
434 * @param[in] size size to be copied
435 * @return size of memory copy if no error, otherwise zero
437 * @note the input data format should be NHWC
438 * @detail rules for the memory address of activations in NPU HW.
439 * (https://code.sec.samsung.net/confluence/pages/viewpage.action?pageId=146491864)
441 * 1) Special case (depth == 3)
442 * - addr(x,y,z) = addr(0,0,0) + (z) + 3 * (x + width * y)
445 * - addr(x,y,z) = addr(0,0,0) + (z % MPA_L) + MPA_L * (x + width * (y + height * (z / MPA_L)))
447 * Thus, if depth is not a multiple of MPA_L (i.e., 64), zero padding is required
450 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
451 void *dst, void *src, size_t size)
453 const Metadata *meta = model->getMetadata();
454 const tensor_data_info* info;
455 const uint32_t *dims;
459 /** extract required information from the metadata */
461 if (idx >= meta->getInputNum()) {
462 logerr (TAG, "Wrong information for input tensors in metadata\n");
466 info = model->getInputDataInfo (idx);
467 dims = meta->getInputDims (idx);
468 zero_point = meta->getInputQuantZero (idx);
469 scale = meta->getInputQuantScale (idx);
471 if (idx >= meta->getOutputNum()) {
472 logerr (TAG, "Wrong information for output tensors in metadata\n");
476 info = model->getOutputDataInfo (idx);
477 dims = meta->getOutputDims (idx);
478 zero_point = meta->getOutputQuantZero (idx);
479 scale = meta->getOutputQuantScale (idx);
482 if (info == nullptr) {
483 logerr (TAG, "Unmatched tensors info\n");
487 uint32_t batch = dims[0];
488 uint32_t height = dims[1];
489 uint32_t width = dims[2];
490 uint32_t depth = dims[3];
492 uint32_t data_size = get_data_size (info->type);
493 if (data_size == 0) {
494 logerr (TAG, "Invalid data size\n");
498 bool need_quantization = false;
500 * note that we assume DATA_TYPE_SRNPU is the smallest data type that we consider.
501 * Also, DATA_TYPE_SRNPU and uint8_t may be regarded as the same in the view of apps.
503 if (info->type != DATA_TYPE_SRNPU) {
504 assert (data_size >= get_data_size (DATA_TYPE_SRNPU));
506 if (data_size > get_data_size (DATA_TYPE_SRNPU) ||
507 !(zero_point == DEFAULT_ZERO_POINT && scale == DEFAULT_SCALE))
508 need_quantization = true;
511 /** check data manipulation is required */
512 if (depth != 3 && depth != 64 && info->layout != DATA_LAYOUT_SRNPU) {
513 uint32_t MPA_L = DATA_GRANULARITY;
515 uint32_t std_offset; /* standard offset in NHWC data format */
516 uint32_t npu_offset; /* npu offset in NPU HW data format*/
521 /* @todo we currently support only NHWC */
522 if (info->layout != DATA_LAYOUT_NHWC) {
523 logerr (TAG, "data manipulation is supported for NHWC only\n");
527 for (n = 0; n < batch; n++) {
528 for (h = 0; h < height; h++) {
529 for (w = 0; w < width; w++) {
530 for (d = 0; d < depth; d += MPA_L) {
531 std_offset = d + depth * (w + width * (h + n * height));
532 npu_offset = MPA_L * (w + width * (h + (n + d / MPA_L) * height));
533 slice_size = (depth - d >= MPA_L) ? MPA_L : depth - d;
536 src_offset = std_offset * data_size;
537 dst_offset = npu_offset;
539 src_offset = npu_offset;
540 dst_offset = std_offset * data_size;
543 /* if depth is not a multiple of MPA_L, add zero paddings (not exact values) */
544 if (need_quantization) {
545 memcpy_with_quant (is_input, info->type, scale, zero_point,
546 static_cast<char*>(dst) + dst_offset,
547 static_cast<char*>(src) + src_offset,
551 static_cast<char*>(dst) + dst_offset,
552 static_cast<char*>(src) + src_offset,
559 } else if (need_quantization) {
560 /** depth == 3 || depth == 64; special cases which can directly copy input tensor data */
562 size = size / data_size;
564 memcpy_with_quant (is_input, info->type, scale, zero_point,
567 memcpy (dst, src, size);
576 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
577 void *dst, void *src, size_t size)
579 memcpy (dst, src, size);
586 * @brief create device instance depending on device type and id
587 * @param[in] type device type
588 * @param[in] id device id
589 * @return device instance
592 Device::createInstance (dev_type type, int id)
594 Device *device = nullptr;
596 switch (type & DEVICETYPE_MASK) {
597 case DEVICETYPE_TRIV:
598 device = new TrinityVision (id);
600 case DEVICETYPE_TRIV2:
601 device = new TrinityVision2 (id);
603 case DEVICETYPE_TRIA:
604 device = new TrinityAsr (id);
610 if (device != nullptr && device->init () != 0) {
618 /** @brief host handler constructor */
619 HostHandler::HostHandler (Device *device)
621 /* ignored as we don't use double buffering anymore, but for backward-compatibility */
622 async_mode_ (NPUASYNC_WAIT)
626 /** @brief host handler destructor */
627 HostHandler::~HostHandler ()
632 * @brief register model from generic buffer
633 * @param[in] model_buf model buffer
634 * @param[out] modelid model id
635 * @return 0 if no error. otherwise a negative errno
638 HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid)
640 Model *model = device_->registerModel (model_buf);
641 if (model == nullptr) {
642 logerr (TAG, "Failed to register model\n");
646 int status = models_.insert (model->getID(), model);
648 logerr (TAG, "Failed to insert model id\n");
653 *modelid = model->getID();
658 * @brief remove the registered model
659 * @param[in] modelid model id
660 * @return 0 if no error. otherwise a negative errno
663 HostHandler::unregisterModel (uint32_t modelid)
665 return models_.remove (modelid);
669 * @brief remove all registered models
673 HostHandler::unregisterModels ()
680 * @brief Set the data layout for input/output tensors
681 * @param[in] modelid The ID of model whose layouts are set
682 * @param[in] in the layout/type info for input tensors
683 * @param[in] out the layout/type info for output tensors
684 * @return @c 0 if no error. otherwise a negative error value
685 * @note if this function is not called, default layout/type will be used.
688 HostHandler::setDataInfo (uint32_t modelid, tensors_data_info *in,
689 tensors_data_info *out)
691 Model *model = models_.find (modelid);
692 if (model == nullptr)
695 model->setDataInfo (in, out);
701 * @brief Set the inference constraint for next NPU inferences
702 * @param[in] modelid The target model id
703 * @param[in] constraint inference constraint (e.g., timeout, priority)
704 * @return @c 0 if no error. otherwise a negative error value
705 * @note If this function is not called, default values are used.
708 HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint)
710 Model *model = models_.find (modelid);
711 if (model == nullptr)
714 model->setConstraint (constraint);
720 * @brief find and return model instance
721 * @param[in] modelid model id
722 * @return model instance if found. otherwise nullptr
725 HostHandler::getModel (uint32_t modelid)
727 return models_.find (modelid);
730 /** @brief dummay callback for runSync. */
733 callbackSync (output_buffers *output) : output_(output), done_(false) {}
735 static void callback (output_buffers *output, uint64_t sequence, void *data) {
736 callbackSync *sync = static_cast<callbackSync *>(data);
737 sync->callback (output, sequence);
740 void callback (output_buffers *output, uint64_t sequence) {
741 /** just copy internal variables of output buffers */
742 memcpy (output_, output, sizeof (output_buffers));
748 std::unique_lock<std::mutex> lock (m_);
749 cv_.wait (lock, [this]() { return done_; });
754 std::condition_variable cv_;
755 output_buffers *output_;
760 * @brief Execute inference. Wait (block) until the output is available.
761 * @param[in] modelid The model to be inferred.
762 * @param[in] input The input data to be inferred.
763 * @param[out] output The output result.
764 * @return @c 0 if no error. otherwise a negative error value
767 HostHandler::runSync (uint32_t modelid, const input_buffers *input,
768 output_buffers *output)
770 callbackSync sync (output);
771 int status = runAsync (modelid, input, callbackSync::callback,
772 static_cast <void*> (&sync), NPUASYNC_DROP_OLD, nullptr);
774 /** sync needs to wait callback */
781 * @brief Invoke NPU inference. Unblocking call.
782 * @param[in] modelid The model to be inferred.
783 * @param[in] input The input data to be inferred.
784 * @param[in] cb The output buffer handler.
785 * @param[in] cb_data The data given as a parameter to the runNPU_async call.
786 * @param[in] mode Configures how this operation works.
787 * @param[out] sequence The sequence number returned with runNPU_async.
788 * @return @c 0 if no error. otherwise a negative error value
791 HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
792 npuOutputNotify cb, void *cb_data, npu_async_mode mode, uint64_t *sequence)
794 Model *model = nullptr;
796 if (device_->needModel()) {
797 model = getModel (modelid);
798 if (model == nullptr)
802 device_->setAsyncMode (mode);
803 return device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
807 * @brief get number of available devices
808 * @param[in] type device type
809 * @return number of devices
812 HostHandler::getNumDevices (dev_type type)
814 return DriverAPI::getNumDevices (type);
818 * @brief get device instance
819 * @param[out] dev device instance
820 * @param[in] type device type
821 * @param[in] id device id
822 * @return 0 if no error. otherwise a negative errno
825 HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
827 int num_devices = getNumDevices (type);
829 /** check the validity of device id */
830 if (!(num_devices > 0 && id < static_cast<uint32_t>(num_devices))) {
831 logerr (TAG, "Invalid arguments provided\n");
835 Device *device = Device::createInstance (type, id);
836 if (device == nullptr) {
837 logerr (TAG, "Failed to create a device with the given type\n");
842 /** This is just for backward-compatility; we don't guarantee its corresness */
849 * @brief allocate generic buffer (just for users)
850 * @param[out] buffer buffer instance
851 * @return 0 if no error. otherwise a negative errno
854 HostHandler::allocGenericBuffer (generic_buffer *buffer)
859 if (buffer->size > UINT32_MAX) {
860 logerr (TAG, "Don't support such a large size");
864 if (buffer->type == BUFFER_FILE) {
866 if (buffer->filepath == nullptr)
869 /* now, npu-engine always provides dmabuf-based allocation */
870 HWmem *hwmem = device_->allocMemory ();
871 if (hwmem == nullptr || hwmem->alloc (buffer->size) < 0)
874 buffer->dmabuf = hwmem->getDmabuf();
875 buffer->offset = hwmem->getOffset();
876 buffer->addr = hwmem->getData();
882 * @brief deallocate generic buffer (just for users)
883 * @param[in] buffer buffer instance
884 * @return 0 if no error. otherwise a negative errno
887 HostHandler::deallocGenericBuffer (generic_buffer *buffer)
892 if (buffer->type != BUFFER_FILE)
893 device_->deallocMemory (buffer->dmabuf);
899 * @brief allocate multiple generic buffers (just for users)
900 * @param[out] buffers multi-buffer instance
901 * @return 0 if no error. otherwise a negative errno
904 HostHandler::allocGenericBuffer (generic_buffers *buffers)
906 if (buffers == NULL || buffers->num_buffers < 1)
909 buffer_types type = buffers->bufs[0].type;
910 if (type == BUFFER_FILE)
913 uint64_t total_size = 0;
914 for (uint32_t idx = 0; idx < buffers->num_buffers; idx++)
915 total_size += buffers->bufs[idx].size;
917 uint64_t first_size = buffers->bufs[0].size;
918 buffers->bufs[0].size = total_size;
919 int status = allocGenericBuffer (&buffers->bufs[0]);
923 uint64_t offset = first_size;
924 for (uint32_t idx = 1; idx < buffers->num_buffers; idx++) {
925 buffers->bufs[idx].dmabuf = buffers->bufs[0].dmabuf;
926 buffers->bufs[idx].offset = buffers->bufs[0].offset + offset;
927 buffers->bufs[idx].type = type;
929 offset += buffers->bufs[idx].size;
936 * @brief deallocate multiple generic buffers (just for users)
937 * @param[in] buffers multi-buffer instance
938 * @return 0 if no error. otherwise a negative errno
941 HostHandler::deallocGenericBuffer (generic_buffers *buffers)
943 if (buffers == NULL || buffers->num_buffers < 1)
946 return deallocGenericBuffer (&buffers->bufs[0]);
949 /** just for backward-compatability */
950 npudev_h HostHandler::latest_dev_ = nullptr;
952 /** implementation of libnpuhost APIs */
955 * @brief Returns the number of available NPU devices.
956 * @return @c The number of NPU devices.
957 * @retval 0 if no NPU devices available. if positive (number of NPUs) if NPU devices available. otherwise, a negative error value.
958 * @note the caller should call putNPUdevice() to release the device handle
960 int getnumNPUdeviceByType (dev_type type)
962 return HostHandler::getNumDevices (type);
966 * @brief Returns the handle of the chosen NPU devices.
967 * @param[out] dev The NPU device handle
968 * @param[in] id The NPU id to get the handle. 0 <= id < getnumNPUdeviceByType().
969 * @return @c 0 if no error. otherwise a negative error value
970 * @note the caller should call putNPUdevice() to release the device handle
972 int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
974 return HostHandler::getDevice (dev, type, id);
978 * @brief Returns the handle of an NPU device meeting the condition
979 * @param[out] dev The NPU device handle
980 * @param[in] cond The condition for device search.
981 * @return @c 0 if no error. otherwise a negative error value
982 * @note the caller should call putNPUdevice() to release the device handle
983 * @note it's not supported yet
985 int getNPUdeviceByCondition(npudev_h *dev, const npucondition *cond)
987 /** not implmeneted yet */
988 return getNPUdeviceByType (dev, NPUCOND_TRIV_CONN_SOCIP, 0);
992 * @brief release the NPU device instance obtained by getDevice ()
993 * @param[in] dev the NPU device handle
995 void putNPUdevice (npudev_h dev)
998 delete static_cast<Device *> (dev);
1002 * @brief Send the NN model to NPU.
1003 * @param[in] dev The NPU device handle
1004 * @param[in] modelfile The filepath to the compiled NPU NN model in any buffer_type
1005 * @param[out] modelid The modelid allocated for this instance of NN model.
1006 * @return @c 0 if no error. otherwise a negative error value
1008 * @detail For ASR devices, which do not accept models, but have models
1009 * embedded in devices, you do not need to call register and
1010 * register calls for ASR are ignored.
1012 * @todo Add a variation: in-memory model register.
1014 int registerNPUmodel (npudev_h dev, generic_buffer *modelfile, uint32_t *modelid)
1016 INIT_HOST_HANDLER (host_handler, dev);
1018 return host_handler->registerModel (modelfile, modelid);
1022 * @brief Remove the NN model from NPU
1023 * @param[in] dev The NPU device handle
1024 * @param[in] modelid The model to be removed from the NPU.
1025 * @return @c 0 if no error. otherwise a negative error value
1026 * @detail This may incur some latency with memory compatcion.
1028 int unregisterNPUmodel(npudev_h dev, uint32_t modelid)
1030 INIT_HOST_HANDLER (host_handler, dev);
1032 return host_handler->unregisterModel (modelid);
1036 * @brief Remove all NN models from NPU
1037 * @param[in] dev The NPU device handle
1038 * @return @c 0 if no error. otherwise a negative error value
1040 int unregisterNPUmodel_all(npudev_h dev)
1042 INIT_HOST_HANDLER (host_handler, dev);
1044 return host_handler->unregisterModels ();
1048 * @brief [OPTIONAL] Set the data layout for input/output tensors
1049 * @param[in] dev The NPU device handle
1050 * @param[in] modelid The ID of model whose layouts are set
1051 * @param[in] info_in the layout/type info for input tensors
1052 * @param[in] info_out the layout/type info for output tensors
1053 * @return @c 0 if no error. otherwise a negative error value
1054 * @note if this function is not called, default layout/type will be used.
1056 int setNPU_dataInfo(npudev_h dev, uint32_t modelid,
1057 tensors_data_info *info_in, tensors_data_info *info_out)
1059 INIT_HOST_HANDLER (host_handler, dev);
1061 return host_handler->setDataInfo (modelid, info_in, info_out);
1065 * @brief [OPTIONAL] Set the inference constraint for next NPU inferences
1066 * @param[in] dev The NPU device handle
1067 * @param[in] modelid The target model id
1068 * @param[in] constraint inference constraint (e.g., timeout, priority)
1069 * @return @c 0 if no error. otherwise a negative error value
1070 * @note If this function is not called, default values are used.
1072 int setNPU_constraint(npudev_h dev, uint32_t modelid, npuConstraint constraint)
1074 INIT_HOST_HANDLER (host_handler, dev);
1076 return host_handler->setConstraint (modelid, constraint);
1080 * @brief Execute inference. Wait (block) until the output is available.
1081 * @param[in] dev The NPU device handle
1082 * @param[in] modelid The model to be inferred.
1083 * @param[in] input The input data to be inferred.
1084 * @param[out] output The output result. The caller MUST allocate appropriately before calling this.
1085 * @return @c 0 if no error. otherwise a negative error value
1087 * @detail This is a syntactic sugar of runNPU_async().
1088 * CAUTION: There is a memcpy for the output buffer.
1090 int runNPU_sync(npudev_h dev, uint32_t modelid, const input_buffers *input,
1091 output_buffers *output)
1093 INIT_HOST_HANDLER (host_handler, dev);
1095 return host_handler->runSync (modelid, input, output);
1099 * @brief Invoke NPU inference. Unblocking call.
1100 * @param[in] dev The NPU device handle
1101 * @param[in] modelid The model to be inferred.
1102 * @param[in] input The input data to be inferred.
1103 * @param[in] cb The output buffer handler.
1104 * @param[out] sequence The sequence number returned with runNPU_async.
1105 * @param[in] data The data given as a parameter to the runNPU_async call.
1106 * @param[in] mode Configures how this operation works.
1107 * @return @c 0 if no error. otherwise a negative error value
1109 int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
1110 npuOutputNotify cb, uint64_t *sequence, void *data,
1111 npu_async_mode mode)
1113 INIT_HOST_HANDLER (host_handler, dev);
1115 return host_handler->runAsync (modelid, input, cb, data, mode, sequence);
1119 * @brief Allocate a buffer for NPU model with the requested buffer type.
1120 * @param[in] dev The NPU device handle
1121 * @param[in/out] Buffer the buffer pointer where memory is allocated.
1122 * @return 0 if no error, otherwise a negative errno.
1124 int allocNPU_modelBuffer (npudev_h dev, generic_buffer *buffer)
1126 INIT_HOST_HANDLER (host_handler, dev);
1128 return host_handler->allocGenericBuffer (buffer);
1132 * @brief Free the buffer and remove the address mapping.
1133 * @param[in] dev The NPU device handle
1134 * @param[in] buffer the model buffer
1135 * @return 0 if no error, otherwise a negative errno.
1137 int cleanNPU_modelBuffer (npudev_h dev, generic_buffer *buffer)
1139 INIT_HOST_HANDLER (host_handler, dev);
1141 return host_handler->deallocGenericBuffer (buffer);
1145 * @brief Allocate a buffer for NPU input with the requested buffer type.
1146 * @param[in] dev The NPU device handle
1147 * @param[in/out] Buffer the buffer pointer where memory is allocated.
1148 * @return 0 if no error, otherwise a negative errno.
1149 * @note please utilize allocInputBuffers() for multiple input tensors because subsequent
1150 * calls of allocInputBuffer() don't gurantee contiguous allocations between them.
1152 int allocNPU_inputBuffer (npudev_h dev, generic_buffer *buffer)
1154 INIT_HOST_HANDLER (host_handler, dev);
1156 return host_handler->allocGenericBuffer (buffer);
1160 * @brief Free the buffer and remove the address mapping.
1161 * @param[in] dev The NPU device handle
1162 * @param[in] buffer the input buffer
1163 * @return 0 if no error, otherwise a negative errno.
1165 int cleanNPU_inputBuffer (npudev_h dev, generic_buffer *buffer)
1167 INIT_HOST_HANDLER (host_handler, dev);
1169 return host_handler->deallocGenericBuffer (buffer);
1173 * @brief Allocate input buffers, which have multiple instances of generic_buffer
1174 * @param[in] dev The NPU device handle
1175 * @param[in/out] input input buffers.
1176 * @return 0 if no error, otherwise a negative errno.
1177 * @note it reuses allocInputBuffer().
1178 * @details in case of BUFFER_DMABUF, this function can be used to gurantee physically-contiguous
1179 * memory mapping for multiple tensors (in a single inference, not batch size).
1181 int allocNPU_inputBuffers (npudev_h dev, input_buffers * input)
1183 INIT_HOST_HANDLER (host_handler, dev);
1185 return host_handler->allocGenericBuffer (input);
1189 * @brief Free input buffers allocated by allocInputBuffers().
1190 * @param[in] dev The NPU device handle
1191 * @param[in/out] input input buffers.
1192 * @note it reuses cleanInputbuffer().
1193 * @return 0 if no error, otherwise a negative errno.
1195 int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input)
1197 INIT_HOST_HANDLER (host_handler, dev);
1199 return host_handler->deallocGenericBuffer (input);
1203 * @brief Get metadata for NPU model
1204 * @param[in] model The path of model binary file
1205 * @param[in] need_extra whether you want to extract the extra data in metadata
1206 * @return the metadata structure to be filled if no error, otherwise nullptr
1208 * @note For most npu-engine users, the extra data is not useful because it will be
1209 * used for second-party users (e.g., compiler, simulator).
1210 * Also, the caller needs to free the metadata.
1212 * @note the caller needs to free the metadata
1214 npubin_meta * getNPUmodel_metadata (const char *model, bool need_extra)
1223 fp = fopen (model, "rb");
1225 logerr (TAG, "Failed to open the model binary: %d\n", -errno);
1229 meta = (npubin_meta *) malloc (NPUBIN_META_SIZE);
1231 logerr (TAG, "Failed to allocate metadata\n");
1235 ret = fread (meta, 1, NPUBIN_META_SIZE, fp);
1236 if (ret != NPUBIN_META_SIZE) {
1237 logerr (TAG, "Failed to read the metadata\n");
1241 if (!CHECK_NPUBIN (meta->magiccode)) {
1242 logerr (TAG, "Invalid metadata provided\n");
1246 if (need_extra && NPUBIN_META_EXTRA (meta->magiccode) > 0) {
1247 npubin_meta *new_meta;
1249 new_meta = (npubin_meta *) realloc (meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
1251 logerr (TAG, "Failed to allocate extra metadata\n");
1255 ret = fread (new_meta->reserved_extra, 1, NPUBIN_META_EXTRA_SIZE (meta->magiccode), fp);
1256 if (ret != NPUBIN_META_EXTRA_SIZE (meta->magiccode)) {
1257 logerr (TAG, "Invalid extra metadata provided\n");
1277 /** deprecated buffer APIs; please use the above APIs */
1280 * @brief Returns the number of NPU devices (TRIV).
1282 int getnumNPUdevice (void)
1284 logwarn (TAG, "deprecated. Please use getnumNPUdeviceByType ()\n");
1285 return getnumNPUdeviceByType (NPUCOND_TRIV_CONN_SOCIP);
1289 * @brief Returns the list of ASR devices (TRIA)
1291 int getnumASRdevice (void)
1293 logwarn (TAG, "deprecated. Please use getnumNPUdeviceByType ()\n");
1294 return getnumNPUdeviceByType (NPUCOND_TRIA_CONN_SOCIP);
1298 * @brief Returns the handle of the chosen TRIV device.
1300 int getNPUdevice (npudev_h *dev, uint32_t id)
1302 logwarn (TAG, "deprecated. Please use getNPUdeviceByType ()\n");
1303 return getNPUdeviceByType (dev, NPUCOND_TRIV_CONN_SOCIP, id);
1307 * @brief Returns the handle of the chosen TRIA device.
1309 int getASRdevice (npudev_h *dev, uint32_t id)
1311 logwarn (TAG, "deprecated. Please use getNPUdeviceByType ()\n");
1312 return getNPUdeviceByType (dev, NPUCOND_TRIA_CONN_SOCIP, id);
1315 /** @brief deprecated */
1316 int allocModelBuffer (generic_buffer *buffer)
1318 logwarn (TAG, "deprecated. Please use allocNPU_modelBuffer\n");
1319 return allocNPU_modelBuffer (HostHandler::getLatestDevice(), buffer);
1322 /** @brief deprecated */
1323 int cleanModelBuffer (generic_buffer *buffer)
1325 logwarn (TAG, "deprecated. Please use cleanNPU_modelBuffer\n");
1326 return allocNPU_modelBuffer (HostHandler::getLatestDevice(), buffer);
1329 /** @brief deprecated */
1330 int allocInputBuffer (generic_buffer *buffer)
1332 logwarn (TAG, "deprecated. Please use allocNPU_inputBuffer\n");
1333 return allocNPU_inputBuffer (HostHandler::getLatestDevice(), buffer);
1336 /** @brief deprecated */
1337 int cleanInputBuffer (generic_buffer *buffer)
1339 logwarn (TAG, "deprecated. Please use cleanNPU_inputBuffer\n");
1340 return cleanNPU_inputBuffer (HostHandler::getLatestDevice(), buffer);
1343 /** @brief deprecated */
1344 int allocInputBuffers (input_buffers * input)
1346 logwarn (TAG, "deprecated. Please use allocNPU_inputBuffers\n");
1347 return allocNPU_inputBuffers (HostHandler::getLatestDevice(), input);
1350 /** @brief deprecated */
1351 int cleanInputBuffers (input_buffers * input)
1353 logwarn (TAG, "deprecated. Please use cleanNPU_inputBuffers\n");
1354 return cleanNPU_inputBuffers (HostHandler::getLatestDevice(), input);
1357 /** @brief deprecated */
1358 int allocNPUBuffer (uint64_t size, buffer_types type,
1359 const char * filepath, generic_buffer *buffer)
1362 buffer->size = size;
1363 buffer->type = type;
1364 buffer->filepath = filepath;
1367 logwarn (TAG, "deprecated. Please use allocNPU_* APIs\n");
1368 return allocModelBuffer (buffer);
1371 /** @brief deprecated */
1372 int cleanNPUBuffer (generic_buffer * buffer)
1374 logwarn (TAG, "deprecated. Please use cleanNPU_* APIs\n");
1375 return cleanModelBuffer (buffer);