From c58580e04a5142a2173e0fb6c87b75a72d497046 Mon Sep 17 00:00:00 2001 From: Dongju Chae Date: Wed, 11 Aug 2021 18:02:17 +0900 Subject: [PATCH] [Coverage] Increase code coverage This patch increases code coverage. - Line Coverage: 89.1% - Func Coverage: 92.2% Signed-off-by: Dongju Chae --- src/core/ne-handler.cc | 33 ++++++-------- src/core/ne-handler.h | 9 ++-- tests/unittests/ne_libnpuhost_test.cc | 84 +++++++++++++++++++++++++++++++++++ utils/coverage/coverage.sh | 2 + 4 files changed, 102 insertions(+), 26 deletions(-) diff --git a/src/core/ne-handler.cc b/src/core/ne-handler.cc index e985418..71bd999 100644 --- a/src/core/ne-handler.cc +++ b/src/core/ne-handler.cc @@ -1298,17 +1298,15 @@ TrinityVision2::setRequestData (int req_id, input_buffers *input, tensors_data_i return -ENOENT; } - const Model *model = req->getModel (); - if (model == nullptr) { - logerr (TAG, "Unable to find the target model\n"); - return -EINVAL; - } - if (input == nullptr || output == nullptr) { logerr (TAG, "Invalid arguments detected\n"); return -EINVAL; } + const Model *model = req->getModel (); + /* internal logic errors */ + assert (model != nullptr); + if (in_info != nullptr && out_info != nullptr) { /* FIXME: should be per request, not per model */ const_cast (model)->setDataInfo (in_info, out_info); @@ -1379,10 +1377,8 @@ TrinityVision2::setRequestConstraint (int req_id, npu_constraint constraint) { } const Model *model = req->getModel (); - if (model == nullptr) { - logerr (TAG, "Unable to find the target model\n"); - return -EINVAL; - } + /* internal logic errors */ + assert (model != nullptr); /* FIXME: should be per request, not per model */ const_cast (model)->setConstraint (constraint); @@ -1462,16 +1458,8 @@ TrinityVision2::submitRequestKernel (int req_id) { } const Model *model = req->getModel (); - if (model == nullptr) { - logerr (TAG, "Unable to find the model associated to this request\n"); - return -ENOENT; - } - - SegmentTable *segt = dynamic_cast (req->getInferData ()); - if (segt != nullptr) { - logwarn (TAG, "Segment table with user-provided data is destroyed\n"); - delete segt; - } + /* internal logic errors */ + assert (model != nullptr); /* dummy input/ouput */ input_buffers input = {0}; @@ -1485,6 +1473,11 @@ TrinityVision2::submitRequestKernel (int req_id) { output.bufs[0].type = BUFFER_DMABUF; output.bufs[0].dmabuf = KERNEL_OUTPUT_SEG; /* indicator of kernel output */ + SegmentTable *segt = dynamic_cast (req->getInferData ()); + if (segt != nullptr) { + logwarn (TAG, "Segment table with user-provided data is destroyed\n"); + delete segt; + } segt = prepareSegmentTable (model, &input, &output); if (req->getScheduler () != NPU_SCHEDULER_VD) diff --git a/src/core/ne-handler.h b/src/core/ne-handler.h index 1042759..b9ad809 100644 --- a/src/core/ne-handler.h +++ b/src/core/ne-handler.h @@ -129,12 +129,9 @@ class Device { int deallocMemory (int dmabuf_fd, size_t size, void *addr); /** virtual methods to implement each device's behaviors */ - virtual int setModel (const generic_buffer *model, Model **model_ptr) { return -EPERM; } - virtual int unsetModel (Model *model) { return -EPERM; } - - virtual int getTensorSize (const Model *model, bool input, uint32_t index, uint32_t *size) { - return -EPERM; - } + virtual int setModel (const generic_buffer *model, Model **model_ptr) = 0; + virtual int unsetModel (Model *model) = 0; + virtual int getTensorSize (const Model *model, bool input, uint32_t index, uint32_t *size) = 0; virtual int run (npu_input_opmode opmode, const Model *model, const input_buffers *input, output_buffers *output = nullptr, npuOutputNotify cb = nullptr, diff --git a/tests/unittests/ne_libnpuhost_test.cc b/tests/unittests/ne_libnpuhost_test.cc index 8adf60d..1c1bb78 100644 --- a/tests/unittests/ne_libnpuhost_test.cc +++ b/tests/unittests/ne_libnpuhost_test.cc @@ -823,6 +823,47 @@ TEST (ne_libnpuhost_test, request_decoupled_apis) { } /** + * @brief test request decoupled APIs with error handling + */ +TEST (ne_libnpuhost_test, request_decoupled_apis_invalid_n) { + npudev_h dev; + uint32_t modelid; + generic_buffer model; + std::string model_dir (BASIC_TEST_MODEL); + std::string model_path = model_dir + "/model.tvn"; + npu_constraint constraint; + int req_id; + + /* skip if the test model is not available */ + if (!create_test_model (model_path.c_str (), &model)) + return; + + ASSERT_EQ (getNPUdeviceByTypeAny (&dev, NPUCOND_TRIV2_CONN_SOCIP, 2), 0); + ASSERT_EQ (registerNPUmodel (dev, &model, &modelid), 0); + + /* invalid model id */ + EXPECT_NE (createNPU_request (dev, 0xffff, &req_id), 0); + /* invalid arguments */ + EXPECT_NE (createNPU_request (dev, modelid, nullptr), 0); + /* invalid request id */ + EXPECT_NE (setNPU_requestData (dev, req_id, nullptr, nullptr, nullptr, nullptr), 0); + EXPECT_NE (setNPU_requestCallback (dev, req_id, nullptr, nullptr), 0); + EXPECT_NE (setNPU_requestMode (dev, req_id, NPU_INFER_BLOCKING), 0); + EXPECT_NE (setNPU_requestConstraint (dev, req_id, constraint), 0); + EXPECT_NE (setNPU_requestScheduler (dev, req_id, NPU_SCHEDULER_UNKNOWN, nullptr), 0); + EXPECT_NE (submitNPU_request (dev, req_id), 0); + EXPECT_NE (submitNPU_requestKernel (dev, req_id), 0); + EXPECT_NE (removeNPU_request (dev, req_id), 0); + + ASSERT_EQ (createNPU_request (dev, modelid, &req_id), 0); + EXPECT_NE (setNPU_requestData (dev, req_id, nullptr, nullptr, nullptr, nullptr), 0); + ASSERT_EQ (removeNPU_request (dev, req_id), 0); + + ASSERT_EQ (unregisterNPUmodel (dev, modelid), 0); + putNPUdevice (dev); +} + +/** * @brief test getNPU_requestModel */ TEST (ne_libnpuhost_test, request_get_model) { @@ -1050,6 +1091,49 @@ TEST (ne_libnpuhost_test, profile_apis_decoupled_n) { } /** + * @brief test getNPUmodel_tensorSize() + */ +TEST (ne_libnpuhost_test, get_model_tensor_size) { + npudev_h dev; + uint32_t modelid, size; + generic_buffer model; + std::string model_dir (BASIC_TEST_MODEL); + std::string model_path = model_dir + "/model.tvn"; + + /* skip if the test model is not available */ + if (!create_test_model (model_path.c_str (), &model)) + return; + + ASSERT_EQ (getNPUdeviceByTypeAny (&dev, NPUCOND_TRIV2_CONN_SOCIP, 2), 0); + ASSERT_EQ (registerNPUmodel (dev, &model, &modelid), 0); + + EXPECT_EQ (getNPUmodel_tensorSize (dev, modelid, true, 0, &size), 0); + EXPECT_GT (size, 0); + EXPECT_EQ (getNPUmodel_tensorSize (dev, modelid, false, 0, &size), 0); + EXPECT_GT (size, 0); + + ASSERT_EQ (unregisterNPUmodel (dev, modelid), 0); + + putNPUdevice (dev); +} + +/** + * @brief test getNPUmodel_tensorSize() with error handling + */ +TEST (ne_libnpuhost_test, get_model_tensor_size_n) { + npudev_h dev; + uint32_t size; + + ASSERT_EQ (getNPUdeviceByTypeAny (&dev, NPUCOND_TRIV2_CONN_SOCIP, 2), 0); + + /* invalid model id */ + EXPECT_NE (getNPUmodel_tensorSize (dev, 0xffff, true, 0, &size), 0); + EXPECT_NE (getNPUmodel_tensorSize (dev, 0xffff, false, 0, &size), 0); + + putNPUdevice (dev); +} + +/** * @brief main function for unit test */ int diff --git a/utils/coverage/coverage.sh b/utils/coverage/coverage.sh index 21ceea9..04fc0d9 100755 --- a/utils/coverage/coverage.sh +++ b/utils/coverage/coverage.sh @@ -55,11 +55,13 @@ popd mkdir -p sample_models cp -rf ${INSTALL_PREFIX}/share/npu-engine/testdata/TRIV235_2TOPS/CONV* sample_models/ +cp -rf ${INSTALL_PREFIX}/share/npu-engine/testdata/TRIV235_2TOPS/TANH_300 sample_models/ ./tests/apptests/apptest_tvn_triv2_bulk sample_models > /dev/null ./tests/apptests/apptest_tvn_triv2_dmabuf sample_models/CONV_2D_300 > /dev/null ./tests/apptests/apptest_tvn_triv2_profile sample_models/CONV_2D_301 -p visa > /dev/null ./tests/apptests/apptest_tvn_triv2_profile sample_models/CONV_2D_301 -p layer > /dev/null +NE_MUTE_STDOUT=1 ./tests/apptests/apptest_tvn_triv2_profile sample_models/TANH_300 -p layer > /dev/null rm -rf sample_models -- 2.7.4