From 630ddbde6019f35ab88c23775f70f5d7af55d021 Mon Sep 17 00:00:00 2001 From: Dongju Chae Date: Fri, 28 Jun 2019 17:28:48 +0900 Subject: [PATCH] [N3] The prototype of interence scheduler This commit adds the implementation of inference scheduler (N3). Signed-off-by: Dongju Chae --- core/npu-engine/src/ne-scheduler.c | 149 +++++++++++++++++++++++++++++++++++++ core/npu-engine/src/ne-scheduler.h | 12 +-- 2 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 core/npu-engine/src/ne-scheduler.c diff --git a/core/npu-engine/src/ne-scheduler.c b/core/npu-engine/src/ne-scheduler.c new file mode 100644 index 0000000..849809d --- /dev/null +++ b/core/npu-engine/src/ne-scheduler.c @@ -0,0 +1,149 @@ +/** + * Proprietary + * Copyright (C) 2019 Samsung Electronics + * Copyright (C) 2019 Dongju Chae + * Copyright (C) 2019 MyungJoo Ham + */ +/** + * @file ne-scheduler.c + * @date 28 Jun 2019 + * @brief Internal API to access N3. + * @see http://suprem.sec.samsung.net/confluence/display/ODLC/Software+Stack + * @author Dongju Chae + * @author MyungJoo Ham + * @bug No known bugs except for NYI items + */ + +#include "ne-scheduler.h" +#include "ne-inf.h" + +#include +#include + +#define TAG _N3 + +/** + * @brief current opmode + * @note it's the latest opmode started. + */ +static n4_opmode cur_opmode = N4_OPS_END; + +/** + * @brief convert npu_input_opmode to n4_opmode + * @param[in] op npu_input_opmode + * @return n4_opmode + */ +static n4_opmode +opmode_npu_to_n4 (npu_input_opmode op) +{ + switch (op) { + case NPUINPUT_HOST: + return N4_OPS_HOST_INPUT; + case NPUINPUT_I2S_MIC: + return N4_OPS_INTERNAL_MIC; + case NPUINPUT_INTERNAL_CAM: + return N4_OPS_INTERNAL_CAMERA; + default: + return N4_OPS_END; + } +} + +/** + * @brief request configure() for all submodels to N4 + * @param[in] op n4_opmode + * @param[in] model model + * @param[in] cb output ready callback + * @param[in] cb_data callback data + * @return 0 if ok, otherwise a negative errrno + */ +static int +config_all_submodels (n4_opmode op, model *model, output_ready cb, void *cb_data) +{ + submodel *smodel; + int err; + + assert (model); + + list_for_each_entry (smodel, model->submodels, list) { + if ((err = n4_configure (op, smodel, cb, cb_data, + NULL /* TODO let's think about how to set this */)) < 0) + return err; + } + + return 0; +} + +/** + * @brief Configure inference mode or stop inference + * @param[in] op The op-mode related with inputs for inference + * @param[in] force if non-zero, try to stop/start with preemption. (NOT SUPPORTED WITH THIS VERSION) + * @param[in] model The model used for inference. + * @param[in] cb The callback to be called when the output is ready + * @param[in] cb_data The private data to be given to the callback. + * @return @c 0 if success. otherwise, -ERRNO. + */ +int setOpMode(npu_input_opmode op, int force, model *model, output_ready cb, void *cb_data) +{ + n4_opmode n4_op; + int err; + + if (!model) { + logerr (TAG, "A valid model was not provided\n"); + return -EINVAL; + } + + if (op == NPUINPUT_STOP) { + if (cur_opmode == N4_OPS_END) { + logerr (TAG, "No input service was working\n"); + return -EINVAL; + } + + if ((err = n4_stop (cur_opmode, force ? STOP_PREEMPT : STOP_TRY)) == 0) + cur_opmode = N4_OPS_END; + + return err; + } + + /* start */ + n4_op = opmode_npu_to_n4 (op); + if (n4_op == N4_OPS_END) { + logerr (TAG, "Unknown npu input opmode (%d)\n", op); + return -EINVAL; + } + + if (cur_opmode != N4_OPS_END) { + /* wait or preempt the previous one */ + if ((err = n4_stop (cur_opmode, force ? STOP_PREEMPT : STOP_WAIT)) != 0) + return err; + } + + if ((err = config_all_submodels (n4_op, model, cb, cb_data)) < 0) + return err; + + if ((err = n4_start (n4_op)) < 0) + return err; + + cur_opmode = n4_op; + + return 0; +} + +/** + * @brief Mark the buffer ready for inference. + * @param[in] The input buffer that is ready. + * @return @c 0 if success. otherwise, -ERRNO. + */ +int dataReady(buffer *buf) +{ + if (cur_opmode == N4_OPS_END) { + logerr (TAG, "No input service yet\n"); + return -EINVAL; + } + + if (!buf) { + logerr (TAG, "Empty buffer\n"); + return -EINVAL; + } + + return n4_dataReady (buf); +} diff --git a/core/npu-engine/src/ne-scheduler.h b/core/npu-engine/src/ne-scheduler.h index b248f5d..930380d 100644 --- a/core/npu-engine/src/ne-scheduler.h +++ b/core/npu-engine/src/ne-scheduler.h @@ -21,18 +21,20 @@ #ifndef NRC_SCHEDULER_H__ #define NRC_SCHEDULER_H__ -#include "../../../common/typedef.h" +#include +#include "ne-common.h" +#include "ne-model.h" /** * @brief Configure inference mode or stop inference * @param[in] op The op-mode related with inputs for inference * @param[in] force if non-zero, try to stop/start with preemption. (NOT SUPPORTED WITH THIS VERSION) * @param[in] model The model used for inference. - * @param[in] nb notifier-block to be called with output buffer when an instance of inference is completed. + * @param[in] cb The callback to be called when the output is ready + * @param[in] cb_data The private data to be given to the callback. * @return @c 0 if success. otherwise, -ERRNO. */ -int setOpMode(npu_input_opmode op, int force, model *model, - notifier_block *nb); +int setOpMode(npu_input_opmode op, int force, model *model, output_ready cb, void *cb_data); /** * @brief Mark the buffer ready for inference. @@ -40,6 +42,6 @@ int setOpMode(npu_input_opmode op, int force, model *model, * @return @c 0 if success. otherwise, -ERRNO. */ int dataReady(buffer *buf); -/** @todo Update N7 to support npu_buffer */ +/** @todo Update N7 to support buffer */ #endif /* NRC_SCHEDULER_H__ */ -- 2.7.4