3 * Copyright (C) 2020 Samsung Electronics
4 * Copyright (C) 2020 Dongju Chae <dongju.chae@samsung.com>
9 * @brief Impelemetation of NPU Engine entrypoint that handles APIs 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"
18 #include <npubinfmt.h>
19 #include <NPUdrvAPI.h>
20 #include <CommPlugin.h>
25 #include <condition_variable>
32 /** @brief host handler constructor */
33 HostHandler::HostHandler (Device *device)
35 /* ignored as we don't use double buffering anymore, but for backward-compatibility */
36 async_mode_ (NPUASYNC_WAIT)
40 /** @brief host handler destructor */
41 HostHandler::~HostHandler ()
46 * @brief register model from generic buffer
47 * @param[in] model_buf model buffer
48 * @param[out] modelid model id
49 * @return 0 if no error. otherwise a negative errno
52 HostHandler::registerModel (generic_buffer *model_buf, uint32_t *modelid)
54 if (model_buf == nullptr || modelid == nullptr) {
55 logerr (TAG, "Invalid arguments given\n");
59 Model *model = nullptr;
60 int status = device_->setModel (model_buf, &model);
62 logerr (TAG, "Failed to set model: %d\n", status);
66 assert (model != nullptr);
68 status = models_.insert (model->getID(), model);
70 logerr (TAG, "Failed to insert model id\n");
75 *modelid = model->getID();
80 * @brief remove the registered model
81 * @param[in] modelid model id
82 * @return 0 if no error. otherwise a negative errno
85 HostHandler::unregisterModel (uint32_t modelid)
87 Model *model = models_.find (modelid);
91 int status = device_->unsetModel (model);
93 logerr (TAG, "Failed to unset model: %d\n", status);
97 return models_.remove (modelid);
101 * @brief remove all registered models
105 HostHandler::unregisterModels ()
107 std::function <bool (Model *)> functor =
108 [&] (Model *m) -> bool {
109 bool can_remove = true;
110 int status = device_->unsetModel (m);
112 logwarn (TAG, "Failed to unset model: %d\n", status);
118 models_.for_each (functor);
123 * @brief Get the profile information from NPU
124 * @param[in] task_id The identifier for each inference
125 * @param[out] profile The profile instance
126 * @return 0 if no error, otherwise a negative errno.
129 HostHandler::getProfile (int task_id, npu_profile *profile)
131 if (task_id < 0 || profile == nullptr) {
132 logerr (TAG, "Invalid parameter provided\n");
136 const DriverAPI * api = device_->getDriverAPI ();
137 assert (api != nullptr);
139 profile->num_layers = 0;
140 profile->layers = nullptr;
142 int status = api->getProfile (task_id, profile);
144 logerr (TAG, "Failed to get profile information: %d\n", status);
152 * @brief get the stats for the latest apps of the target device
153 * @param[out] stat The list of app stat
154 * @note The caller has the responsibility to free the resources.
155 * This API is not working on the emulated envionment.
158 HostHandler::getStatApps (npu_stat_apps *stat)
160 const DriverAPI * api = device_->getDriverAPI ();
161 assert (api != nullptr);
163 return api->getStatApps (stat);
167 * @brief get the stats for the latest tasks of the target app
168 * @param[in] appid The identifier of target app
169 * @param[out] stat The list of task stat
170 * @note The caller has the responsibility to free the resources.
171 * This API is not working on the emulated envionment.
174 HostHandler::getStatTasks (int appid, npu_stat_tasks *stat)
176 const DriverAPI * api = device_->getDriverAPI ();
177 assert (api != nullptr);
179 return api->getStatTasks (appid, stat);
183 * @brief Get the driver API level of opened NPU device
184 * @param[out] level driver API level
185 * @return 0 if no error, otherwise a negative errno
188 HostHandler::getAPILevel (uint32_t *level)
190 const DriverAPI * api = device_->getDriverAPI ();
191 assert (api != nullptr);
193 return api->getAPILevel (level);
197 * @brief Get the TOPS of the opened NPU device
198 * @param[in] dev the NPU device handle
199 * @param[out] tops npu tops
200 * @return 0 if no error, otherwise a negative errno
201 * @note this does not support for emulated devices
204 HostHandler::getTops (uint32_t *tops)
206 const DriverAPI * api = device_->getDriverAPI ();
207 assert (api != nullptr);
209 return api->getTops (tops);
213 * @brief Get the DSP DSPM size of the opened NPU device
214 * @param[in] dev the NPU device handle
215 * @param[out] dspm dspm size
216 * @return 0 if no error, otherwise a negative errno
217 * @note this does not support for emulated devices
220 HostHandler::getDspmSize (uint32_t *dspm)
222 const DriverAPI * api = device_->getDriverAPI ();
223 assert (api != nullptr);
225 return api->getDspmSize (dspm);
228 * @brief Set the data layout for input/output tensors
229 * @param[in] modelid The ID of model whose layouts are set
230 * @param[in] in the layout/type info for input tensors
231 * @param[in] out the layout/type info for output tensors
232 * @return @c 0 if no error. otherwise a negative error value
233 * @note if this function is not called, default layout/type will be used.
236 HostHandler::setDataInfo (uint32_t modelid, tensors_data_info *in,
237 tensors_data_info *out)
239 Model *model = models_.find (modelid);
240 if (model == nullptr)
243 return model->setDataInfo (in, out);
247 * @brief Set the inference constraint for next NPU inferences
248 * @param[in] modelid The target model id
249 * @param[in] constraint inference constraint (e.g., timeout, priority)
250 * @return @c 0 if no error. otherwise a negative error value
251 * @note If this function is not called, default values are used.
254 HostHandler::setConstraint (uint32_t modelid, npuConstraint constraint)
256 Model *model = models_.find (modelid);
257 if (model == nullptr)
260 model->setConstraint (constraint);
266 * @brief find and return model instance
267 * @param[in] modelid model id
268 * @return model instance if found. otherwise nullptr
271 HostHandler::getModel (uint32_t modelid)
273 return models_.find (modelid);
276 /** @brief dummay callback for runSync. */
279 callbackSync (output_buffers *output) : output_(output), done_(false) {}
281 static void callback (output_buffers *output, uint64_t sequence, void *data) {
282 callbackSync *sync = static_cast<callbackSync *>(data);
283 sync->callback (output, sequence);
286 void callback (output_buffers *output, uint64_t sequence) {
287 if (output_ != nullptr) {
288 /** just copy internal variables of output buffers */
289 memcpy (output_, output, sizeof (output_buffers));
296 std::unique_lock<std::mutex> lock (m_);
297 cv_.wait (lock, [this]() { return done_; });
302 std::condition_variable cv_;
303 output_buffers *output_;
308 * @brief Execute inference. Wait (block) until the output is available.
309 * @param[in] modelid The model to be inferred.
310 * @param[in] input The input data to be inferred.
311 * @param[out] output The output result.
312 * @return @c 0 if no error. otherwise a negative error value
315 HostHandler::runSync (uint32_t modelid, const input_buffers *input,
316 output_buffers *output)
318 callbackSync sync (output);
319 int status = runAsync (modelid, input, callbackSync::callback,
320 static_cast <void*> (&sync), NPUASYNC_DROP_OLD, nullptr);
322 /** sync needs to wait callback */
329 * @brief Invoke NPU inference. Unblocking call.
330 * @param[in] modelid The model to be inferred.
331 * @param[in] input The input data to be inferred.
332 * @param[in] cb The output buffer handler.
333 * @param[in] cb_data The data given as a parameter to the runNPU_async call.
334 * @param[in] mode Configures how this operation works.
335 * @param[out] sequence The sequence number returned with runNPU_async.
336 * @return @c 0 if no error. otherwise a negative error value
339 HostHandler::runAsync (uint32_t modelid, const input_buffers *input,
340 npuOutputNotify cb, void *cb_data, npu_async_mode mode, uint64_t *sequence)
342 Model *model = nullptr;
344 if (device_->needModel()) {
345 model = getModel (modelid);
346 if (model == nullptr)
350 /* check the given model before running */
351 if (model != nullptr && !model->finalize ()) {
352 logerr (TAG, "Failed to finalize the model. Please see the log messages\n");
356 device_->setAsyncMode (mode);
357 return device_->run (NPUINPUT_HOST, model, input, cb, cb_data, sequence);
361 * @brief Let NPU accept input frames from its internal source continuously
362 * @param[in] modelid The model to be inferred.
363 * @param[in] opmode NPU has different opmode with auto-inputs. Choose one.
364 * @param[in] hw_dev The target device feeding input data
365 * @return @c 0 if no error. otherwise a negative error value
368 HostHandler::runInternal (uint32_t modelid, npu_input_opmode opmode,
371 Model *model = nullptr;
373 if (device_->needModel()) {
374 model = getModel (modelid);
375 if (model == nullptr)
379 /* check the given model before running */
380 if (model != nullptr && !model->finalize ()) {
381 logerr (TAG, "Failed to finalize the model. Please see the log messages\n");
385 return device_->runInternal (opmode, model, hw_dev);
389 * @brief Stop the request with the given id
390 * @param[in] dev The NPU device handle
391 * @param[in] id The request id
392 * @return @c 0 if no error. otherwise a negative error value
395 HostHandler::stopInternal (int id)
398 logerr (TAG, "Unable to stop this request with id (%d)\n", id);
402 const DriverAPI * api = device_->getDriverAPI ();
403 assert (api != nullptr);
405 return api->stop_target (id);
409 * @brief get number of available devices
410 * @param[in] type device type
411 * @return number of devices
414 HostHandler::getNumDevices (dev_type type)
416 return DriverAPI::getNumDevices (type);
420 * @brief get device instance
421 * @param[out] dev device instance
422 * @param[in] type device type
423 * @param[in] id device id
424 * @return 0 if no error. otherwise a negative errno
427 HostHandler::getDevice (npudev_h *dev, dev_type type, uint32_t id)
429 int num_devices = getNumDevices (type);
431 /** check the validity of device id */
432 if (!(num_devices > 0 && id < static_cast<uint32_t>(num_devices))) {
433 logerr (TAG, "Invalid arguments provided\n");
437 Device *device = Device::createInstance (type, id);
438 if (device == nullptr) {
439 logerr (TAG, "Failed to create a device with the given type\n");
449 * @brief allocate generic buffer (just for users)
450 * @param[out] buffer buffer instance
451 * @return 0 if no error. otherwise a negative errno
454 HostHandler::allocGenericBuffer (generic_buffer *buffer)
459 if (buffer->size == 0) {
460 logerr (TAG, "Invalid size\n");
464 if (buffer->size > UINT32_MAX) {
465 logerr (TAG, "Don't support such a large size");
469 switch (buffer->type) {
472 if (buffer->filepath == nullptr)
477 /* now, npu-engine always provides dmabuf-based allocation */
478 void *addr = nullptr;
479 int dmabuf = device_->allocMemory (buffer->size, &addr);
483 buffer->dmabuf = dmabuf;
495 * @brief deallocate generic buffer (just for users)
496 * @param[in] buffer buffer instance
497 * @return 0 if no error. otherwise a negative errno
500 HostHandler::deallocGenericBuffer (generic_buffer *buffer)
505 switch (buffer->type) {
507 /** always true cuz nothing to do */
510 return device_->deallocMemory (buffer->dmabuf, buffer->size, buffer->addr);
519 * @brief allocate multiple generic buffers (just for users)
520 * @param[out] buffers multi-buffer instance
521 * @return 0 if no error. otherwise a negative errno
524 HostHandler::allocGenericBuffer (generic_buffers *buffers)
529 if (buffers == NULL || buffers->num_buffers < 1)
532 for (idx = 0; idx < buffers->num_buffers; idx++) {
533 status = allocGenericBuffer (&buffers->bufs[idx]);
542 deallocGenericBuffer (&buffers->bufs[--idx]);
549 * @brief deallocate multiple generic buffers (just for users)
550 * @param[in] buffers multi-buffer instance
551 * @return 0 if no error. otherwise a negative errno
554 HostHandler::deallocGenericBuffer (generic_buffers *buffers)
556 if (buffers == NULL || buffers->num_buffers < 1)
559 for (uint32_t idx = 0; idx < buffers->num_buffers; idx++)
560 deallocGenericBuffer (&buffers->bufs[idx]);
561 buffers->num_buffers = 0;
567 * @brief get the current memory status
568 * @param[out] alloc_total The size of allocated memory until now
569 * @param[out] free_total The size of freed memory until now
570 * @return 0 if no error. otherwise a negatice error value
573 HostHandler::getMemoryStatus (size_t *alloc_total, size_t *free_total)
575 /** API is always set in initialize () */
576 const DriverAPI * api = device_->getDriverAPI ();
577 assert (api != nullptr);
579 return api->getMemoryStatus (alloc_total, free_total);
583 * @brief Get the current device status to be used
584 * @param[out] status the device status
585 * @param[out] num_requests the number of running requests (or pending)
586 * @return 0 if no error, otherwise a negative errno.
589 HostHandler::getDeviceStatus (npu_status *status, uint32_t *num_requests)
591 /** API is always set in initialize () */
592 const DriverAPI * api = device_->getDriverAPI ();
597 device_state_t state = api->isReady ();
598 if (state == device_state_t::STATE_READY) {
599 *num_requests = api->numRequests ();
600 if (*num_requests > 0)
612 /** implement methods of Device class */
614 /** @brief constructor of device */
615 Device::Device (dev_type type, int id, bool need_model)
616 : comm_ (CommPlugin::getCommPlugin()), type_ (type), id_ (id), need_model_ (true),
617 mode_ (NPUASYNC_WAIT), initialized_ (false), atomic_flag_ (ATOMIC_FLAG_INIT)
622 * @brief create device instance depending on device type and id
623 * @param[in] type device type
624 * @param[in] id device id
625 * @return device instance
628 Device::createInstance (dev_type type, int id)
630 Device *device = nullptr;
632 switch (type & DEVICETYPE_MASK) {
633 case DEVICETYPE_TRIV2:
634 device = new TrinityVision2 (id);
636 case DEVICETYPE_DEPR:
637 logwarn (TAG, "You're trying to open deprecated devices..\n");
643 if (device != nullptr && device->init () != 0) {
652 * @brief device initialization
653 * @return 0 if no error, otherwise a negative errno
654 * @note Init failures come from createDriverAPI() only.
659 /** should be initilizaed only once */
660 if (!atomic_flag_.test_and_set()) {
661 /** create the corresponding driver API */
662 api_ = DriverAPI::createDriverAPI (type_, id_);
663 if (api_.get() == nullptr) {
664 atomic_flag_.clear();
665 logerr (TAG, "Failed to create driver API\n");
669 handler_.reset (new HostHandler (this));
670 scheduler_.reset (new Scheduler (api_.get()));
671 mem_ = MemAllocator::createInstance (api_.get());
673 initialized_ = true; /** c++11 does not provide test() of atomic flag */
680 * @brief stop all requests from this device
681 * @param[in] force_stop indicate the schedduler waits until to handle previous requests
682 * @return 0 if no error, otherwise a negative errno
685 Device::stop (bool force_stop)
687 if (!initialized ()) {
688 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
692 Request *req = new Request (NPUINPUT_STOP);
693 req->setForceStop (force_stop);
694 return scheduler_->submitRequest (req);
698 * @brief allocate generic memory buffer
699 * @param[in] size the size to allocate
700 * @param[out] addr the mapped address
701 * @return dmabuf fd if no error, otherwise a negative errno
704 Device::allocMemory (size_t size, void **addr)
706 if (!initialized ()) {
707 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
711 if (size == 0 || addr == nullptr) {
712 logerr (TAG, "Invalid arguments\n");
716 return mem_->allocMemory (size, addr);
720 * @brief deallocate generic memory buffer
721 * @param[in] dmabuf_fd dmabuf file descriptor
722 * @param[in] size buffer size
723 * @param[in] addr mapped addr
724 * @return 0 if no error, otherwise a negative errno
727 Device::deallocMemory (int dmabuf_fd, size_t size, void * addr)
729 if (!initialized ()) {
730 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
734 if (dmabuf_fd < 0 || size == 0 || addr == nullptr) {
735 logerr (TAG, "Invalid arguments\n");
739 return mem_->deallocMemory (dmabuf_fd, size, addr);
743 * @brief extract the segment table instance from input generic buffers
744 * @param[in] model the model instance
745 * @param[in] input the input generic buffers
746 * @param[in] output the output generic buffers
747 * @return the segment table instance
750 TrinityVision2::prepareSegmentTable (const Model *model, const input_buffers *input,
751 const output_buffers *output)
753 const Metadata *meta = model->getMetadata ();
754 if (meta == nullptr || (input != nullptr &&
755 meta->getInputNum() != input->num_buffers)) {
756 logerr (TAG, "Invalid metadata info provided\n");
760 SegmentTable * segt = mem_->allocSegmentTable (new HWmemDevice);
761 int status = segt->alloc ();
763 logerr (TAG, "Failed to allocate segment table: %d\n", status);
767 status = segt->createSegments (model, input, output);
769 logerr (TAG, "Failed to create segments: %d\n", status);
781 * @brief implementation of TRIV2's setModel ()
782 * @param[in] model_buf the model generic buffer
783 * @param[out] model the model instance
784 * @return 0 if no error, otherwise a negative errno
787 TrinityVision2::setModel (const generic_buffer *model_buf, Model ** model_ptr)
789 if (!initialized ()) {
790 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
794 if (model_buf == nullptr || model_ptr == nullptr)
800 switch (model_buf->type) {
803 model = mem_->allocModel (new HWmemDevice);
804 if (model == nullptr) {
805 logerr (TAG, "Failed to allocate model\n");
809 status = model->alloc (NPUBIN_META_SIZE);
811 logerr (TAG, "Failed to allocate model: %d\n", status);
815 status = comm_.extractGenericBuffer (model_buf, model->getData(), nullptr,
816 0, NPUBIN_META_SIZE);
818 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
826 status = model->setMetadata (model->getData());
830 /** allocate program (optional; NOP) */
831 if (model->getMetadata()->getProgramSize() > 0) {
832 HWmem * hwmem_prog = new HWmem (new HWmemDevice);
833 hwmem_prog->setDriverAPI (api_.get());
834 hwmem_prog->setContiguous (true);
836 model->setProgramData (hwmem_prog);
838 status = hwmem_prog->alloc (model->getMetadata()->getProgramSize());
840 logerr (TAG, "Failed to allocate program\n");
844 status = comm_.extractGenericBuffer (model_buf, hwmem_prog->getData(), nullptr,
845 model->getMetadata()->getMetaSize(),
846 model->getMetadata()->getProgramSize());
848 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
852 /** register this model to the driver */
853 model_config_t config;
854 config.version = model->getMetadata()->getVersion ();
855 config.dbuf_fd = hwmem_prog->getDmabuf ();
856 config.program_size = hwmem_prog->getSize ();
857 config.program_offset_addr = 0;
858 config.metadata_dbuf_fd = model->getDmabuf ();
860 /** for metadata extended section */
861 size_t extended_size = model->getMetadata()->getMetaExtendedSize();
862 if (extended_size > 0) {
863 HWmem * hwmem_extended = new HWmem (new HWmemDevice);
864 hwmem_extended->setDriverAPI (api_.get ());
866 model->setExtendedMetadata (hwmem_extended);
868 status = hwmem_extended->alloc (extended_size);
870 logerr (TAG, "Failed to allocate extended metadata: %d\n", status);
874 config.metadata_ext_dbuf_fd = hwmem_extended->getDmabuf ();
875 config.metadata_ext_size = extended_size;
877 status = comm_.extractGenericBuffer (model_buf, hwmem_extended->getData (),
878 nullptr, NPUBIN_META_SIZE, extended_size);
880 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
885 status = api_->registerModel (&config, model->getMetadata()->getNPUVersion());
889 model->setInternalID(config.id);
892 /** allocate weight (optional) */
893 if (model->getMetadata()->getWeightSize() > 0) {
894 HWmem * hwmem_weight = new HWmem (new HWmemDevice);
895 hwmem_weight->setDriverAPI (api_.get());
897 model->setWeightData (hwmem_weight);
899 status = hwmem_weight->alloc (model->getMetadata()->getWeightSize());
901 logerr (TAG, "Failed to allocate program\n");
905 status = comm_.extractGenericBuffer (model_buf, hwmem_weight->getData(), nullptr,
906 model->getMetadata()->getMetaSize() + model->getMetadata()->getProgramSize(),
907 model->getMetadata()->getWeightSize());
909 logerr (TAG, "Failed to extract generic buffer: %d\n", status);
923 * @brief implementation of TRIV2's unsetModel ()
924 * @param[in] model the model instance
925 * @return 0 if no error, otherwise a negative errno
928 TrinityVision2::unsetModel (Model * model)
930 if (!initialized ()) {
931 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
935 if (model == nullptr) {
936 logerr (TAG, "Invalid model instance\n");
940 if (model->getMetadata()->getProgramSize() > 0)
941 return api_->deregisterModel (model->getInternalID ());
946 /** @brief implementation of TRIV2's run() */
948 TrinityVision2::run (npu_input_opmode opmode, const Model *model,
949 const input_buffers *input, npuOutputNotify cb, void *cb_data,
952 if (!initialized ()) {
953 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
957 if (opmode != NPUINPUT_HOST)
960 if (input == nullptr || input->num_buffers == 0 || model == nullptr)
963 const_cast<Model *>(model)->updateDataInfo ();
965 /** this device uses segment table */
966 SegmentTable * segt = prepareSegmentTable (model, input);
967 if (segt == nullptr) {
968 logerr (TAG, "Failed to create segment table instance\n");
972 /** extract input data */
973 for (uint32_t idx = 0; idx < input->num_buffers; idx++) {
974 if (!segt->getInputSegment(idx)->isExternal ()) {
975 uint32_t seg_offset = segt->getInputSegmentOffset(idx);
976 auto func = std::bind (TrinityVision2::manipulateData, model, idx, true,
977 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
978 int status = comm_.extractGenericBuffer (
980 segt->getInputSegment(idx)->getData() + seg_offset,
983 logerr (TAG, "Failed to feed input segment: %d\n", status);
989 Request *req = new Request (opmode);
990 req->setModel (model);
991 req->setSegmentTable (segt);
992 req->setCallback (std::bind (&TrinityVision2::callback, this, req, cb, cb_data));
995 *sequence = req->getID();
997 return scheduler_->submitRequest (req);
1000 /** @brief implementation of TRIV2's runInternal() */
1002 TrinityVision2::runInternal (npu_input_opmode opmode, const Model *model,
1005 if (!initialized ()) {
1006 logerr (TAG, "Uninitialized device; should use libnpuhost APIs\n");
1010 if (opmode != NPUINPUT_HW_RECURRING)
1013 /** this device uses segment table */
1014 SegmentTable * segt = prepareSegmentTable (model, nullptr, nullptr);
1015 if (segt == nullptr) {
1016 logerr (TAG, "Failed to create segment table instance\n");
1020 Request *req = new Request (opmode);
1021 req->setModel (model);
1022 req->setSegmentTable (segt);
1023 req->setHwDevice (hw_dev);
1025 return scheduler_->submitRequest (req);
1028 /** @brief callback of TRIV2 request */
1030 TrinityVision2::callback (Request *req, npuOutputNotify cb, void *cb_data)
1035 const Model *model = req->getModel ();
1036 SegmentTable *segt = req->getSegmentTable ();
1037 output_buffers output = {
1038 .num_buffers = segt->getNumOutputSegments ()
1041 for (uint32_t idx = 0; idx < output.num_buffers; idx++) {
1042 uint32_t output_tensor_size = model->getOutputTensorSize (idx);
1044 output.bufs[idx].type = BUFFER_MAPPED;
1045 output.bufs[idx].size = output_tensor_size;
1046 /** user needs to free this */
1047 output.bufs[idx].addr = calloc (1, output_tensor_size);
1049 #if defined(ENABLE_FPGA_WORKAROUND)
1051 segt->getOutputSegment(idx)->getDmabuf(),
1052 segt->getOutputSegmentOffset(idx),
1053 output.bufs[idx].addr,
1054 output.bufs[idx].size);
1056 auto func = std::bind (TrinityVision2::manipulateData, model, idx, false,
1057 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
1058 int status = comm_.insertGenericBuffer (
1059 segt->getOutputSegment(idx)->getData() + segt->getOutputSegmentOffset(idx),
1060 &output.bufs[idx], func);
1063 logerr (TAG, "Failed to return output buffer: %d\n", status);
1068 cb (&output, req->getID(), cb_data);
1073 /** Implement data manipulation (each device may have different impl.) */
1077 * @brief perform data manipulation
1078 * @param[in] model model instance
1079 * @param[in] idx tensor index
1080 * @param[in] is_input indicate it's input manipulation
1081 * @param[out] dst destination buffer
1082 * @param[in] src source buffer (feature map)
1083 * @param[in] size size to be copied
1084 * @return size of memory copy if no error, otherwise zero
1086 * @note the input data format should be NHWC
1088 * @detail Feature map data in TRIV2, (x, y, z) = (width, height, depth)
1090 * 1) Image input (depth == 1 or depth == 3)
1091 * Addr(x,y,z) = Addr(0,0,0) + z + depth * x + ymod * y
1094 * Addr(x,y,z) = Addr(0,0,0) + (z % 64) + (64 * x) + ymod * y + zmod * (z / 64)
1097 TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
1098 void *dst, void *src, size_t size)
1100 const Metadata *meta = model->getMetadata ();
1101 DataConverter converter (is_input);
1103 converter.setData (src, dst, size);
1104 converter.setTops (meta->getTops ());
1106 const tensor_data_info* info = model->getInputDataInfo (idx);
1107 if (info == nullptr)
1110 converter.setDataLayout (info->layout, DATA_LAYOUT_TRIV2);
1111 converter.setDataType (info->type, meta->getInputQuantType (idx));
1112 converter.setDataDims (meta->getInputDims (idx));
1113 converter.setQuantZero (meta->getInputQuantZero (idx));
1114 converter.setQuantScale (meta->getInputQuantScale (idx));
1116 const tensor_data_info* info = model->getOutputDataInfo (idx);
1117 if (info == nullptr)
1120 converter.setDataLayout (DATA_LAYOUT_TRIV2, info->layout);
1121 converter.setDataType (meta->getOutputQuantType (idx), info->type);
1122 converter.setDataDims (meta->getOutputDims (idx));
1123 converter.setQuantZero (meta->getOutputQuantZero (idx));
1124 converter.setQuantScale (meta->getOutputQuantScale (idx));
1127 return converter.perform ();
1133 TrinityVision2::manipulateData (const Model *model, uint32_t idx, bool is_input,
1134 void *dst, void *src, size_t size)
1136 memcpy (dst, src, size);