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 "ne-handler.h"
17 #include <libnpuhost.h>
18 #include <npubinfmt.h>
19 #include <NPUdrvAPI.h>
20 #include <CommPlugin.h>
25 #include <condition_variable>
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;
38 /** just for backward-compatability */
39 npudev_h HostHandler::latest_dev_ = nullptr;
41 /** implement libnpuhost APIs */
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
49 int getnumNPUdeviceByType (dev_type type)
51 return HostHandler::getNumDevices (type);
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
61 int getNPUdeviceByType (npudev_h *dev, dev_type type, uint32_t id)
63 return HostHandler::getDevice (dev, type, id);
67 * @brief release the NPU device instance obtained by getDevice ()
68 * @param[in] dev the NPU device handle
70 void putNPUdevice (npudev_h dev)
73 delete static_cast<Device *> (dev);
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
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.
87 * @todo Add a variation: in-memory model register.
89 int registerNPUmodel (npudev_h dev, generic_buffer *modelfile, uint32_t *modelid)
91 INIT_HOST_HANDLER (host_handler, dev);
93 return host_handler->registerModel (modelfile, modelid);
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.
103 int unregisterNPUmodel(npudev_h dev, uint32_t modelid)
105 INIT_HOST_HANDLER (host_handler, dev);
107 return host_handler->unregisterModel (modelid);
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
115 int unregisterNPUmodel_all(npudev_h dev)
117 INIT_HOST_HANDLER (host_handler, dev);
119 return host_handler->unregisterModels ();
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.
131 int setNPU_dataInfo(npudev_h dev, uint32_t modelid,
132 tensors_data_info *info_in, tensors_data_info *info_out)
134 INIT_HOST_HANDLER (host_handler, dev);
136 return host_handler->setDataInfo (modelid, info_in, info_out);
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.
147 int setNPU_constraint(npudev_h dev, uint32_t modelid, npuConstraint constraint)
149 INIT_HOST_HANDLER (host_handler, dev);
151 return host_handler->setConstraint (modelid, constraint);
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
162 * @detail This is a syntactic sugar of runNPU_async().
163 * CAUTION: There is a memcpy for the output buffer.
165 int runNPU_sync(npudev_h dev, uint32_t modelid, const input_buffers *input,
166 output_buffers *output)
168 INIT_HOST_HANDLER (host_handler, dev);
170 return host_handler->runSync (modelid, input, output);
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
184 int runNPU_async(npudev_h dev, uint32_t modelid, const input_buffers *input,
185 npuOutputNotify cb, uint64_t *sequence, void *data,
188 INIT_HOST_HANDLER (host_handler, dev);
190 return host_handler->runAsync (modelid, input, cb, data, mode, sequence);
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
201 int runNPU_internalInput(npudev_h dev, uint32_t modelid, npu_input_opmode opmode,
204 INIT_HOST_HANDLER (host_handler, dev);
206 return host_handler->runInternal (modelid, opmode, hw_dev);
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
215 int stopNPU_internalInput(npudev_h dev, int id)
217 INIT_HOST_HANDLER (host_handler, dev);
219 return host_handler->stopInternal (id);
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.
228 int allocNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
230 INIT_HOST_HANDLER (host_handler, dev);
232 return host_handler->allocGenericBuffer (buffer);
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.
241 int cleanNPU_genericBuffer (npudev_h dev, generic_buffer * buffer)
243 INIT_HOST_HANDLER (host_handler, dev);
245 return host_handler->deallocGenericBuffer (buffer);
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().
255 int allocNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
257 INIT_HOST_HANDLER (host_handler, dev);
259 return host_handler->allocGenericBuffer (buffers);
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.
269 int cleanNPU_genericBuffers (npudev_h dev, generic_buffers * buffers)
271 INIT_HOST_HANDLER (host_handler, dev);
273 return host_handler->deallocGenericBuffer (buffers);
277 * @brief alias of allocNPU_genericBuffer for model buffer
279 int allocNPU_modelBuffer (npudev_h dev, generic_buffer * model)
281 return allocNPU_genericBuffer (dev, model);
285 * @brief alias of cleanNPU_genericBuffer for model buffer
287 int cleanNPU_modelBuffer (npudev_h dev, generic_buffer * model)
289 return cleanNPU_genericBuffer (dev, model);
293 * @brief alias of allocNPU_genericBuffer for input buffer
295 int allocNPU_inputBuffer (npudev_h dev, generic_buffer * input)
297 return allocNPU_genericBuffer (dev, input);
301 * @brief alias of cleanNPU_genericBuffer for input buffer
303 int cleanNPU_inputBuffer (npudev_h dev, generic_buffer * input)
305 return cleanNPU_genericBuffer (dev, input);
309 * @brief alias of allocNPU_genericBuffers for input buffers
311 int allocNPU_inputBuffers (npudev_h dev, input_buffers * input)
313 return allocNPU_genericBuffers (dev, input);
317 * @brief alias of cleanNPU_genericBuffers for input buffers
319 int cleanNPU_inputBuffers (npudev_h dev, input_buffers * input)
321 return cleanNPU_genericBuffers (dev, input);
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
331 int getNPU_memoryStatus(npudev_h dev, size_t *alloc_total, size_t *free_total)
333 INIT_HOST_HANDLER (host_handler, dev);
335 return host_handler->getMemoryStatus (alloc_total, free_total);
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.
345 int getNPU_deviceStatus(npudev_h dev, npu_status *status, uint32_t *num_requests)
347 INIT_HOST_HANDLER (host_handler, dev);
349 return host_handler->getDeviceStatus (status, num_requests);
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
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.
362 * @note the caller needs to free the metadata
364 npubin_meta * getNPUmodel_metadata (const char *model, bool need_extra)
373 fp = fopen (model, "rb");
375 logerr (TAG, "Failed to open the model binary: %d\n", -errno);
379 meta = (npubin_meta *) malloc (NPUBIN_META_SIZE);
381 logerr (TAG, "Failed to allocate metadata\n");
385 ret = fread (meta, 1, NPUBIN_META_SIZE, fp);
386 if (ret != NPUBIN_META_SIZE) {
387 logerr (TAG, "Failed to read the metadata\n");
391 if (!CHECK_NPUBIN (meta->magiccode)) {
392 logerr (TAG, "Invalid metadata provided\n");
396 if (need_extra && NPUBIN_META_EXTRA (meta->magiccode) > 0) {
397 npubin_meta *new_meta;
399 new_meta = (npubin_meta *) malloc (NPUBIN_META_TOTAL_SIZE(meta->magiccode));
401 logerr (TAG, "Failed to allocate extra metadata\n");
405 memcpy (new_meta, meta, NPUBIN_META_TOTAL_SIZE(meta->magiccode));
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");
430 /** implement methods of HostHandler class */
432 /** @brief host handler constructor */
433 HostHandler::HostHandler (Device *device)
435 /* ignored as we don't use double buffering anymore, but for backward-compatibility */
436 async_mode_ (NPUASYNC_WAIT)
440 /** @brief host handler destructor */
441 HostHandler::~HostHandler ()
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
452 HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid)
454 if (model_buf == nullptr || modelid == nullptr) {
455 logerr (TAG, "Invalid arguments given\n");
459 Model *model = nullptr;
460 int status = device_->setModel (model_buf, &model);
462 logerr (TAG, "Failed to set model: %d\n", status);
466 assert (model != nullptr);
468 status = models_.insert (model->getID(), model);
470 logerr (TAG, "Failed to insert model id\n");
475 *modelid = model->getID();
480 * @brief remove the registered model
481 * @param[in] modelid model id
482 * @return 0 if no error. otherwise a negative errno
485 HostHandler::unregisterModel (uint32_t modelid)
487 Model *model = models_.find (modelid);
488 if (model == nullptr)
491 int status = device_->unsetModel (model);
493 logerr (TAG, "Failed to unset model: %d\n", status);
497 return models_.remove (modelid);
501 * @brief remove all registered models
505 HostHandler::unregisterModels ()
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.
520 HostHandler::setDataInfo (uint32_t modelid, tensors_data_info *in,
521 tensors_data_info *out)
523 Model *model = models_.find (modelid);
524 if (model == nullptr)
527 return model->setDataInfo (in, out);
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.
538 HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint)
540 Model *model = models_.find (modelid);
541 if (model == nullptr)
544 model->setConstraint (constraint);
550 * @brief find and return model instance
551 * @param[in] modelid model id
552 * @return model instance if found. otherwise nullptr
555 HostHandler::getModel (uint32_t modelid)
557 return models_.find (modelid);
560 /** @brief dummay callback for runSync. */
563 callbackSync (output_buffers *output) : output_(output), done_(false) {}
565 static void callback (output_buffers *output, uint64_t sequence, void *data) {
566 callbackSync *sync = static_cast<callbackSync *>(data);
567 sync->callback (output, sequence);
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));
580 std::unique_lock<std::mutex> lock (m_);
581 cv_.wait (lock, [this]() { return done_; });
586 std::condition_variable cv_;
587 output_buffers *output_;
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
599 HostHandler::runSync (uint32_t modelid, const input_buffers *input,
600 output_buffers *output)
602 callbackSync sync (output);
603 int status = runAsync (modelid, input, callbackSync::callback,
604 static_cast <void*> (&sync), NPUASYNC_DROP_OLD, nullptr);
606 /** sync needs to wait callback */
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
623 HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
624 npuOutputNotify cb, void *cb_data, npu_async_mode mode, uint64_t *sequence)
626 Model *model = nullptr;
628 if (device_->needModel()) {
629 model = getModel (modelid);
630 if (model == nullptr)
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");
640 device_->setAsyncMode (mode);
641 return device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
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
652 HostHandler::runInternal (uint32_t modelid, npu_input_opmode opmode,
655 Model *model = nullptr;
657 if (device_->needModel()) {
658 model = getModel (modelid);
659 if (model == nullptr)
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");
669 return device_->runInternal (opmode, model, hw_dev);
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
679 HostHandler::stopInternal (int id)
682 logerr (TAG, "Unable to stop this request with id (%d)\n", id);
686 const DriverAPI * api = device_->getDriverAPI ();
687 assert (api != nullptr);
689 return api->stop_target (id);
693 * @brief get number of available devices
694 * @param[in] type device type
695 * @return number of devices
698 HostHandler::getNumDevices (dev_type type)
700 return DriverAPI::getNumDevices (type);
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
711 HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
713 int num_devices = getNumDevices (type);
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");
721 Device *device = Device::createInstance (type, id);
722 if (device == nullptr) {
723 logerr (TAG, "Failed to create a device with the given type\n");
728 /** This is just for backward-compatility; we don't guarantee its corresness */
735 * @brief allocate generic buffer (just for users)
736 * @param[out] buffer buffer instance
737 * @return 0 if no error. otherwise a negative errno
740 HostHandler::allocGenericBuffer (generic_buffer *buffer)
745 if (buffer->size == 0) {
746 logerr (TAG, "Invalid size\n");
750 if (buffer->size > UINT32_MAX) {
751 logerr (TAG, "Don't support such a large size");
755 switch (buffer->type) {
758 if (buffer->filepath == nullptr)
763 /* now, npu-engine always provides dmabuf-based allocation */
764 void *addr = nullptr;
765 int dmabuf = device_->allocMemory (buffer->size, &addr);
769 buffer->dmabuf = dmabuf;
781 * @brief deallocate generic buffer (just for users)
782 * @param[in] buffer buffer instance
783 * @return 0 if no error. otherwise a negative errno
786 HostHandler::deallocGenericBuffer (generic_buffer *buffer)
791 switch (buffer->type) {
793 /** always true cuz nothing to do */
796 return device_->deallocMemory (buffer->dmabuf, buffer->size, buffer->addr);
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
810 HostHandler::allocGenericBuffer (generic_buffers *buffers)
815 if (buffers == NULL || buffers->num_buffers < 1)
818 for (idx = 0; idx < buffers->num_buffers; idx++) {
819 status = allocGenericBuffer (&buffers->bufs[idx]);
828 deallocGenericBuffer (&buffers->bufs[--idx]);
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
840 HostHandler::deallocGenericBuffer (generic_buffers *buffers)
842 if (buffers == NULL || buffers->num_buffers < 1)
845 for (uint32_t idx = 0; idx < buffers->num_buffers; idx++)
846 deallocGenericBuffer (&buffers->bufs[idx]);
847 buffers->num_buffers = 0;
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
859 HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total)
861 /** API is always set in initialize () */
862 const DriverAPI * api = device_->getDriverAPI ();
863 assert (api != nullptr);
865 return api->getMemoryStatus (alloc_total, free_total);
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.
875 HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests)
877 /** API is always set in initialize () */
878 const DriverAPI * api = device_->getDriverAPI ();
879 assert (api != nullptr);
881 device_state_t state = api->isReady ();
882 if (state == device_state_t::STATE_READY) {
883 *num_requests = api->numRequests ();
884 if (*num_requests > 0)
896 /** implement methods of Device class */
898 /** @brief constructor of device */
899 Device::Device (dev_type type, int id, bool need_model)
900 : comm_ (CommPlugin::getCommPlugin()), type_ (type), id_ (id), need_model_ (true),
901 mode_ (NPUASYNC_WAIT), initialized_ (false), atomic_flag_ (ATOMIC_FLAG_INIT)
906 * @brief create device instance depending on device type and id
907 * @param[in] type device type
908 * @param[in] id device id
909 * @return device instance
912 Device::createInstance (dev_type type, int id)
914 Device *device = nullptr;
916 switch (type & DEVICETYPE_MASK) {
917 case DEVICETYPE_TRIV:
918 device = new TrinityVision (id);
920 case DEVICETYPE_TRIV2:
921 device = new TrinityVision2 (id);
923 case DEVICETYPE_TRIA:
924 device = new TrinityAsr (id);
925 device->setNeedModel (false);
931 if (device != nullptr && device->init () != 0) {
940 * @brief device initialization
941 * @return 0 if no error, otherwise a negative errno
942 * @note Init failures come from createDriverAPI() only.
947 /** should be initilizaed only once */
948 if (!atomic_flag_.test_and_set()) {
949 /** create the corresponding driver API */
950 api_ = DriverAPI::createDriverAPI (type_, id_);
951 if (api_.get() == nullptr) {
952 atomic_flag_.clear();
953 logerr (TAG, "Failed to create driver API\n");
957 handler_.reset (new HostHandler (this));
958 scheduler_.reset (new Scheduler (api_.get()));
959 mem_ = MemAllocator::createInstance (api_.get());
961 initialized_ = true; /** c++11 does not provide test() of atomic flag */
968 * @brief stop all requests from this device
969 * @param[in] force_stop indicate the schedduler waits until to handle previous requests
970 * @return 0 if no error, otherwise a negative errno
973 Device::stop (bool force_stop)
975 if (!initialized ()) {
976 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
980 Request *req = new Request (NPUINPUT_STOP);
981 req->setForceStop (force_stop);
982 return scheduler_->submitRequest (req);
986 * @brief allocate generic memory buffer
987 * @param[in] size the size to allocate
988 * @param[out] addr the mapped address
989 * @return dmabuf fd if no error, otherwise a negative errno
992 Device::allocMemory (size_t size, void **addr)
994 if (!initialized ()) {
995 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
999 if (size == 0 || addr == nullptr) {
1000 logerr (TAG, "Invalid arguments\n");
1004 return mem_->allocMemory (size, addr);
1008 * @brief deallocate generic memory buffer
1009 * @param[in] dmabuf_fd dmabuf file descriptor
1010 * @param[in] size buffer size
1011 * @param[in] addr mapped addr
1012 * @return 0 if no error, otherwise a negative errno
1015 Device::deallocMemory (int dmabuf_fd, size_t size, void * addr)
1017 if (!initialized ()) {
1018 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1022 if (dmabuf_fd < 0 || size == 0 || addr == nullptr) {
1023 logerr (TAG, "Invalid arguments\n");
1027 return mem_->deallocMemory (dmabuf_fd, size, addr);
1031 * @brief extract the buffer instance from input generic buffers
1032 * @param[in] meta the model metadata
1033 * @param[in] input the input generic buffers
1034 * @return the buffer instance
1037 TrinityVision::prepareInputBuffers (const Metadata *meta, const input_buffers *input)
1039 if (meta == nullptr || input == nullptr ||
1040 meta->getInputNum() != input->num_buffers) {
1041 logerr (TAG, "Invalid metadata info provided\n");
1046 const generic_buffer *first = &input->bufs[0];
1047 if (first->type == BUFFER_DMABUF) {
1048 buffer = mem_->allocBuffer (new HWmemExternal);
1049 if (buffer == nullptr)
1052 buffer->setDmabuf (first->dmabuf);
1053 buffer->setOffset (first->offset);
1054 buffer->setSize (meta->getBufferSize());
1056 buffer = mem_->allocBuffer (new HWmemDevice);
1057 if (buffer == nullptr)
1060 int status = buffer->alloc (meta->getBufferSize ());
1062 logerr (TAG, "Failed to allocate buffer: %d\n", status);
1068 int status = buffer->createTensors (meta);
1070 logerr (TAG, "Failed to create tensors: %d\n", status);
1079 * @brief implementation of TRIV's setModel ()
1080 * @param[in] model_buf the model generic buffer
1081 * @param[out] model the model instance
1082 * @return 0 if no error, otherwise a negative errno
1085 TrinityVision::setModel (const generic_buffer *model_buf, Model ** model_ptr)
1087 if (!initialized ()) {
1088 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1092 if (model_buf == nullptr || model_ptr == nullptr)
1095 Model *model = nullptr;
1096 HWmem * hwmem_prog = nullptr;
1097 HWmem * hwmem_weight = nullptr;
1100 /** In TRIV1, model data (including program/weight) should be contiguous */
1102 switch (model_buf->type) {
1105 model = mem_->allocModel (new HWmemDevice);
1106 if (model == nullptr) {
1107 logerr (TAG, "Failed to allocate model\n");
1111 status = model->alloc (model_buf->size);
1113 logerr (TAG, "Failed to allocate model: %d\n", status);
1117 /** extract the whole model data */
1118 status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr);
1120 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1128 status = model->setMetadata (model->getData());
1132 /** allocate program (optional; NOP) */
1133 if (model->getMetadata()->getProgramSize() > 0) {
1134 hwmem_prog = new HWmem (new HWmemChunk);
1135 model->setProgramData (hwmem_prog);
1137 hwmem_prog->setParent (model);
1138 hwmem_prog->setOffset (model->getMetadata()->getMetaSize());
1139 status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
1141 logerr (TAG, "Failed to allocate program\n");
1146 /** allocate weight (optional) */
1147 if (model->getMetadata()->getWeightSize() > 0) {
1148 hwmem_weight = new HWmem (new HWmemChunk);
1149 model->setWeightData (hwmem_weight);
1151 hwmem_weight->setParent (model);
1152 hwmem_weight->setOffset (model->getMetadata()->getMetaSize() +
1153 model->getMetadata()->getProgramSize());
1154 status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
1156 logerr (TAG, "Failed to allocate program\n");
1161 if (hwmem_prog != nullptr) {
1162 /** register this model to the driver */
1163 model_config_t config;
1164 config.dbuf_fd = hwmem_prog->getDmabuf ();
1165 config.program_size = hwmem_prog->getSize ();
1166 config.program_offset_addr = hwmem_prog->getOffset ();
1167 if (hwmem_weight != nullptr)
1168 config.weight_offset_addr = hwmem_weight->getOffset ();
1170 status = api_->registerModel (&config);
1174 model->setInternalID(config.id);
1186 * @brief implementation of TRIV's unsetModel ()
1187 * @param[in] model the model instance
1188 * @return 0 if no error, otherwise a negative errno
1191 TrinityVision::unsetModel (Model * model)
1193 if (!initialized ()) {
1194 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1198 if (model == nullptr) {
1199 logerr (TAG, "Invalid model instance\n");
1203 if (model->getMetadata()->getProgramSize() > 0)
1204 return api_->deregisterModel (model->getInternalID ());
1210 * @brief implementation of TRIV's run()
1211 * @param[in] opmode input opmode
1212 * @param[in] model the model instance
1213 * @param[in] input generic buffers of input data
1214 * @param[in] cb the output callback
1215 * @param[in] cb_data the output callback data
1216 * @param[out] sequence The sequence number returned with runNPU_async.
1219 TrinityVision::run (npu_input_opmode opmode, const Model *model,
1220 const input_buffers *input, npuOutputNotify cb, void *cb_data,
1223 if (!initialized ()) {
1224 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1228 if (opmode != NPUINPUT_HOST) {
1229 logerr (TAG, "TRIV supports only host inputservice\n");
1233 if (model == nullptr || input == nullptr) {
1234 logerr (TAG, "TRIV requires both model and input buffers\n");
1238 Buffer *buffer = prepareInputBuffers (model->getMetadata(), input);
1239 if (buffer == nullptr) {
1240 logerr (TAG, "Failed to extract buffer instance\n");
1244 if (!buffer->isExternal ()) {
1245 for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
1246 auto func = std::bind (TrinityVision::manipulateData, model, idx, true,
1247 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1248 int status = comm_.extractGenericBuffer (&input->bufs[idx],
1249 buffer->getInputTensor(idx)->getData(), func);
1251 logerr (TAG, "Failed to feed input buffer: %d\n", status);
1257 /** this device uses CMA buffer */
1259 Request *req = new Request (opmode);
1260 req->setModel (model);
1261 req->setBuffer (buffer);
1264 req->setCallback (std::bind (&TrinityVision::callback, this, req, cb, cb_data));
1266 if (sequence != nullptr)
1267 *sequence = req->getID();
1269 return scheduler_->submitRequest (req);
1273 * @brief callback of TRIV2 request
1274 * @param[in] req the request instance
1275 * @param[in] cb callback for completion
1276 * @param[in] cb_data callback data
1277 * @note The callback invoke does not gurantee the request was successful
1278 * @todo Check the request failures
1281 TrinityVision::callback (Request *req, npuOutputNotify cb, void *cb_data)
1283 const Model *model = req->getModel ();
1284 Buffer *buffer = req->getBuffer ();
1285 output_buffers output = {
1286 .num_buffers = buffer->getOutputNum ()
1289 for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
1290 uint32_t output_tensor_size = model->getOutputTensorSize (idx);
1292 if (buffer->isExternal ()) {
1293 output.bufs[idx].type = BUFFER_DMABUF;
1294 output.bufs[idx].size = output_tensor_size;
1295 output.bufs[idx].addr = buffer->getOutputTensor(idx)->getData();
1297 output.bufs[idx].type = BUFFER_MAPPED;
1298 output.bufs[idx].size = output_tensor_size;
1299 /** user needs to free this */
1300 output.bufs[idx].addr = malloc (output_tensor_size);
1302 auto func = std::bind (TrinityVision::manipulateData, model, idx, false,
1303 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1304 int status = comm_.insertGenericBuffer (buffer->getOutputTensor(idx)->getData(),
1305 &output.bufs[idx], func);
1307 logerr (TAG, "Failed to return output buffer: %d\n", status);
1312 cb (&output, req->getID(), cb_data);
1318 * @brief extract the segment table instance from input generic buffers
1319 * @param[in] model the model instance
1320 * @param[in] input the input generic buffers
1321 * @param[in] output the output generic buffers
1322 * @return the segment table instance
1325 TrinityVision2::prepareSegmentTable (const Model *model, const input_buffers *input,
1326 const output_buffers *output)
1328 if (model == nullptr) {
1329 logerr (TAG, "Invalid arguments provided\n");
1333 const Metadata *meta = model->getMetadata ();
1334 if (meta == nullptr || (input != nullptr &&
1335 meta->getInputNum() != input->num_buffers)) {
1336 logerr (TAG, "Invalid metadata info provided\n");
1340 SegmentTable * segt = mem_->allocSegmentTable (new HWmemDevice);
1341 int status = segt->alloc ();
1343 logerr (TAG, "Failed to allocate segment table: %d\n", status);
1347 status = segt->createSegments (model, input, output);
1349 logerr (TAG, "Failed to create segments: %d\n", status);
1361 * @brief implementation of TRIV2's setModel ()
1362 * @param[in] model_buf the model generic buffer
1363 * @param[out] model the model instance
1364 * @return 0 if no error, otherwise a negative errno
1367 TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
1369 if (!initialized ()) {
1370 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1374 if (model_buf == nullptr || model_ptr == nullptr)
1380 switch (model_buf->type) {
1383 model = mem_->allocModel (new HWmemDevice);
1384 if (model == nullptr) {
1385 logerr (TAG, "Failed to allocate model\n");
1389 status = model->alloc (NPUBIN_META_SIZE);
1391 logerr (TAG, "Failed to allocate model: %d\n", status);
1395 status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr,
1396 0, NPUBIN_META_SIZE);
1398 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1406 status = model->setMetadata (model->getData());
1410 /** allocate program (optional; NOP) */
1411 if (model->getMetadata()->getProgramSize() > 0) {
1412 HWmem * hwmem_prog = new HWmem (new HWmemDevice);
1413 hwmem_prog->setDriverAPI (api_.get());
1415 model->setProgramData (hwmem_prog);
1417 status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
1419 logerr (TAG, "Failed to allocate program\n");
1423 status = comm_.extractGenericBuffer (model_buf, hwmem_prog->getData(), nullptr,
1424 model->getMetadata()->getMetaSize(),
1425 model->getMetadata()->getProgramSize());
1427 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1431 /** register this model to the driver */
1432 model_config_t config;
1433 config.dbuf_fd = hwmem_prog->getDmabuf ();
1434 config.program_size = hwmem_prog->getSize ();
1435 config.program_offset_addr = 0;
1437 status = api_->registerModel (&config);
1441 model->setInternalID(config.id);
1444 /** allocate weight (optional) */
1445 if (model->getMetadata()->getWeightSize() > 0) {
1446 HWmem * hwmem_weight = new HWmem (new HWmemDevice);
1447 hwmem_weight->setDriverAPI (api_.get());
1449 model->setWeightData (hwmem_weight);
1451 status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
1453 logerr (TAG, "Failed to allocate program\n");
1457 status = comm_.extractGenericBuffer (model_buf, hwmem_weight->getData(), nullptr,
1458 model->getMetadata()->getMetaSize() + model->getMetadata()->getProgramSize(),
1459 model->getMetadata()->getWeightSize());
1461 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
1475 * @brief implementation of TRIV2's unsetModel ()
1476 * @param[in] model the model instance
1477 * @return 0 if no error, otherwise a negative errno
1480 TrinityVision2::unsetModel (Model * model)
1482 if (!initialized ()) {
1483 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1487 if (model == nullptr) {
1488 logerr (TAG, "Invalid model instance\n");
1492 if (model->getMetadata()->getProgramSize() > 0)
1493 return api_->deregisterModel (model->getInternalID ());
1498 /** @brief implementation of TRIV2's run() */
1500 TrinityVision2::run (npu_input_opmode opmode, const Model *model,
1501 const input_buffers *input, npuOutputNotify cb, void *cb_data,
1504 if (!initialized ()) {
1505 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1509 if (opmode != NPUINPUT_HOST)
1512 /** this device uses segment table */
1513 SegmentTable * segt = prepareSegmentTable (model, input);
1514 if (segt == nullptr) {
1515 logerr (TAG, "Failed to create segment table instance\n");
1519 /** extract input data */
1520 for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
1521 size_t max_seg_size = segt->getInputSegment(idx)->getSize();
1522 uint32_t seg_offset = segt->getInputSegmentOffset(idx);
1524 if (input->bufs[idx].size + seg_offset > max_seg_size) {
1525 logerr (TAG, "Too large input data provided: max segment size (%zu)\n",
1530 if (!segt->getInputSegment(idx)->isExternal ()) {
1531 auto func = std::bind (TrinityVision2::manipulateData, model, idx, true,
1532 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1533 int status = comm_.extractGenericBuffer (
1535 segt->getInputSegment(idx)->getData() + seg_offset,
1538 logerr (TAG, "Failed to feed input segment: %d\n", status);
1544 Request *req = new Request (opmode);
1545 req->setModel (model);
1546 req->setSegmentTable (segt);
1547 req->setCallback (std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
1550 *sequence = req->getID();
1552 return scheduler_->submitRequest (req);
1555 /** @brief implementation of TRIV2's runInternal() */
1557 TrinityVision2::runInternal (npu_input_opmode opmode, const Model *model,
1560 if (!initialized ()) {
1561 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1565 if (opmode != NPUINPUT_HW_RECURRING)
1568 /** this device uses segment table */
1569 SegmentTable * segt = prepareSegmentTable (model, nullptr, nullptr);
1570 if (segt == nullptr) {
1571 logerr (TAG, "Failed to create segment table instance\n");
1575 Request *req = new Request (opmode);
1576 req->setModel (model);
1577 req->setSegmentTable (segt);
1578 req->setHwDevice (hw_dev);
1580 return scheduler_->submitRequest (req);
1583 /** @brief callback of TRIV2 request */
1585 TrinityVision2::callback (Request *req, npuOutputNotify cb, void *cb_data)
1587 const Model *model = req->getModel ();
1588 SegmentTable *segt = req->getSegmentTable ();
1589 output_buffers output = {
1590 .num_buffers = segt->getNumOutputSegments ()
1593 for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
1594 uint32_t output_tensor_size = model->getOutputTensorSize (idx);
1596 output.bufs[idx].type = BUFFER_MAPPED;
1597 output.bufs[idx].size = output_tensor_size;
1598 /** user needs to free this */
1599 output.bufs[idx].addr = calloc (1, output_tensor_size);
1601 auto func = std::bind (TrinityVision2::manipulateData, model, idx, false,
1602 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1603 int status = comm_.insertGenericBuffer (
1604 segt->getOutputSegment(idx)->getData() + segt->getOutputSegmentOffset(idx),
1605 &output.bufs[idx], func);
1608 logerr (TAG, "Failed to return output buffer: %d\n", status);
1612 cb (&output, req->getID(), cb_data);
1617 /** @brief implementation of TRIA's run(): WIP */
1619 TrinityAsr::run (npu_input_opmode opmode, const Model *model,
1620 const input_buffers *input, npuOutputNotify cb, void *cb_data,
1623 if (!initialized ()) {
1624 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1628 if (opmode != NPUINPUT_HOST)
1633 /** ASR does not require model and support only a single tensor */
1634 const generic_buffer *first_buf = &input->bufs[0];
1635 if (first_buf->type == BUFFER_DMABUF) {
1636 buffer = mem_->allocBuffer (new HWmemExternal);
1637 if (buffer == nullptr)
1640 buffer->setDmabuf (first_buf->dmabuf);
1641 buffer->setOffset (first_buf->offset);
1642 buffer->setSize (first_buf->size);
1644 buffer = mem_->allocBuffer (new HWmemDevice);
1645 if (buffer == nullptr)
1648 status = buffer->alloc (first_buf->size);
1655 status = buffer->createTensors ();
1657 logerr (TAG, "Failed to create tensors: %d\n", status);
1662 if (!buffer->isExternal ()) {
1663 status = comm_.extractGenericBuffer (first_buf,
1664 buffer->getInputTensor(0)->getData(), nullptr);
1669 Request *req = new Request (opmode);
1670 req->setBuffer (buffer);
1671 req->setCallback (std::bind (&TrinityAsr::callback, this, req, cb, cb_data));
1674 *sequence = req->getID();
1676 return scheduler_->submitRequest (req);
1679 /** @brief callback of TRIA request: WIP */
1681 TrinityAsr::callback (Request *req, npuOutputNotify cb, void *cb_data)
1683 Buffer *buffer = req->getBuffer ();
1684 output_buffers output = {
1688 /** TODO: finalize this impl. when the ASR's working scenario is determined */
1689 cb (&output, req->getID(), cb_data);
1694 /** Implement data manipulation (each device may have different impl.) */
1698 #define do_quantized_memcpy(type) do {\
1701 while (idx < num_elems) {\
1702 val = ((type *) src)[idx];\
1703 val = val / _scale;\
1704 val += _zero_point;\
1705 val = (val > 255.0) ? 255.0 : 0.0;\
1706 ((uint8_t *) dst)[idx++] = (uint8_t) val;\
1709 while (idx < num_elems) {\
1710 val = *(uint8_t *) src;\
1711 val -= _zero_point;\
1713 ((type *) dst)[idx++] = (type) val;\
1714 dst = (void*)(((uint8_t *) dst) + data_size);\
1715 src = (void*)(((uint8_t *) src) + 1);\
1721 * @brief memcpy during quantization
1723 static void memcpy_with_quant (bool quant, data_type type, float scale, uint32_t zero_point,
1724 void *dst, const void *src, uint32_t num_elems)
1726 double _scale = (double) scale;
1727 double _zero_point = (double) zero_point;
1729 uint32_t data_size = get_data_size (type);
1733 case DATA_TYPE_INT8:
1734 do_quantized_memcpy (int8_t);
1736 case DATA_TYPE_UINT8:
1737 do_quantized_memcpy (uint8_t);
1739 case DATA_TYPE_INT16:
1740 do_quantized_memcpy (int16_t);
1742 case DATA_TYPE_UINT16:
1743 do_quantized_memcpy (uint16_t);
1745 case DATA_TYPE_INT32:
1746 do_quantized_memcpy (int32_t);
1748 case DATA_TYPE_UINT32:
1749 do_quantized_memcpy (uint32_t);
1751 case DATA_TYPE_INT64:
1752 do_quantized_memcpy (int64_t);
1754 case DATA_TYPE_UINT64:
1755 do_quantized_memcpy (uint64_t);
1757 case DATA_TYPE_FLOAT32:
1758 do_quantized_memcpy (float);
1760 case DATA_TYPE_FLOAT64:
1761 do_quantized_memcpy (double);
1764 logerr (TAG, "Unsupported datatype %d\n", type);
1769 * @brief perform data manipulation
1770 * @param[in] model model instance
1771 * @param[in] idx tensor index
1772 * @param[in] is_input indicate it's input manipulation
1773 * @param[out] dst destination buffer
1774 * @param[in] src source buffer (feature map)
1775 * @param[in] size size to be copied
1776 * @return size of memory copy if no error, otherwise zero
1778 * @note the input data format should be NHWC
1779 * @detail rules for the memory address of activations in NPU HW.
1780 * (https://code.sec.samsung.net/confluence/pages/viewpage.action?pageId=146491864)
1782 * 1) Special case (depth == 3)
1783 * - addr(x,y,z) = addr(0,0,0) + (z) + 3 * (x + width * y)
1786 * - addr(x,y,z) = addr(0,0,0) + (z % MPA_L) + MPA_L * (x + width * (y + height * (z / MPA_L)))
1788 * Thus, if depth is not a multiple of MPA_L (i.e., 64), zero padding is required
1791 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
1792 void *dst, void *src, size_t size)
1794 const Metadata *meta = model->getMetadata();
1795 const tensor_data_info* info;
1796 const uint32_t *dims;
1797 uint32_t zero_point;
1800 /** extract required information from the metadata */
1802 if (idx >= meta->getInputNum()) {
1803 logerr (TAG, "Wrong information for input tensors in metadata\n");
1807 info = model->getInputDataInfo (idx);
1808 dims = meta->getInputDims (idx);
1809 zero_point = meta->getInputQuantZero (idx);
1810 scale = meta->getInputQuantScale (idx);
1812 if (idx >= meta->getOutputNum()) {
1813 logerr (TAG, "Wrong information for output tensors in metadata\n");
1817 info = model->getOutputDataInfo (idx);
1818 dims = meta->getOutputDims (idx);
1819 zero_point = meta->getOutputQuantZero (idx);
1820 scale = meta->getOutputQuantScale (idx);
1823 if (info == nullptr) {
1824 logerr (TAG, "Unmatched tensors info\n");
1828 uint32_t batch = dims[0];
1829 uint32_t height = dims[1];
1830 uint32_t width = dims[2];
1831 uint32_t depth = dims[3];
1833 uint32_t data_size = get_data_size (info->type);
1834 if (data_size == 0) {
1835 logerr (TAG, "Invalid data size\n");
1839 bool need_quantization = false;
1841 * note that we assume DATA_TYPE_SRNPU is the smallest data type that we consider.
1842 * Also, DATA_TYPE_SRNPU and uint8_t may be regarded as the same in the view of apps.
1844 if (info->type != DATA_TYPE_SRNPU) {
1845 assert (data_size >= get_data_size (DATA_TYPE_SRNPU));
1847 if (data_size > get_data_size (DATA_TYPE_SRNPU) ||
1848 !(zero_point == default_quant_zero && scale == default_quant_scale))
1849 need_quantization = true;
1852 /** check data manipulation is required */
1853 if (depth != 3 && depth != 64 && info->layout != DATA_LAYOUT_SRNPU) {
1854 uint32_t MPA_L = DATA_GRANULARITY;
1855 uint32_t n, h, w, d;
1856 uint32_t std_offset; /* standard offset in NHWC data format */
1857 uint32_t npu_offset; /* npu offset in NPU HW data format*/
1858 uint32_t src_offset;
1859 uint32_t dst_offset;
1860 uint32_t slice_size;
1862 /* @todo we currently support only NHWC */
1863 if (info->layout != DATA_LAYOUT_NHWC) {
1864 logerr (TAG, "data manipulation is supported for NHWC only\n");
1868 for (n = 0; n < batch; n++) {
1869 for (h = 0; h < height; h++) {
1870 for (w = 0; w < width; w++) {
1871 for (d = 0; d < depth; d += MPA_L) {
1872 std_offset = d + depth * (w + width * (h + n * height));
1873 npu_offset = MPA_L * (w + width * (h + (n + d / MPA_L) * height));
1874 slice_size = (depth - d >= MPA_L) ? MPA_L : depth - d;
1877 src_offset = std_offset * data_size;
1878 dst_offset = npu_offset;
1880 src_offset = npu_offset;
1881 dst_offset = std_offset * data_size;
1884 /* if depth is not a multiple of MPA_L, add zero paddings (not exact values) */
1885 if (need_quantization) {
1886 memcpy_with_quant (is_input, info->type, scale, zero_point,
1887 static_cast<char*>(dst) + dst_offset,
1888 static_cast<char*>(src) + src_offset,
1892 static_cast<char*>(dst) + dst_offset,
1893 static_cast<char*>(src) + src_offset,
1900 } else if (need_quantization) {
1901 /** depth == 3 || depth == 64; special cases which can directly copy input tensor data */
1902 memcpy_with_quant (is_input, info->type, scale, zero_point,
1903 dst, src, is_input ? size / data_size : size);
1905 memcpy (dst, src, size);
1914 TrinityVision::manipulateData (const Model *model, uint32_t idx, bool is_input,
1915 void *dst, void *src, size_t size)
1917 memcpy (dst, src, size);
1923 /** other device types don't have data manip impl. yet */
1926 TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
1927 void *dst, void *src, size_t size)
1929 memcpy (dst, src, size);
1934 TrinityAsr::manipulateData (const Model *model, uint32_t idx, bool is_input,
1935 void *dst, void *src, size_t size)
1937 memcpy (dst, src, size);