Each apptest has its own Tester class so a single change is made to multiple classes simultaneously.
This patch moves methods to remove such code smell.
Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
using namespace std;
-/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2 {
- public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- if (model_dir == "")
- return -EINVAL;
-
- std::string model_path = model_dir + "/model.tvn";
- npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
- if (meta == nullptr)
- return -EINVAL;
-
- uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
- free (meta);
-
- int status = UtilTRIV2::init (tops);
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
- }
-
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
-
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
-
- return wait () == 1 ? test_ret_success : test_ret_failure;
- }
-
- private:
- uint32_t model_id_;
-};
-
/** @brief apptest main */
int
main (int argc, char **argv) {
- Tester tester;
+ UtilTRIV2 tester;
char *dir;
- int status;
- int index;
+ int task_id, status, index;
+ uint32_t model_id;
status = tester.parseArgs (argc, argv, "model_path", &index);
if (status == test_ret_skipped || index < 0)
dir = argv[index];
/** initialize triv2 device */
- status = tester.init (dir);
+ status = tester.prepare (dir, &model_id);
if (status < test_ret_success)
goto err;
/** run the inference with the device */
- status = tester.run ();
+ status = tester.run (model_id, &task_id);
if (status < test_ret_success)
goto err;
public:
Tester () : tops_ (0), num_iter_ (0), mode_ (AGING_TEST_NONE) {}
- /** @brief initilize the device handle */
- int init (string model_dir) {
- if (model_dir == "")
- return -EINVAL;
-
- model_dir_ = model_dir;
- string model_path = model_dir + "/model.tvn";
- npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
- if (meta == nullptr)
- return -EINVAL;
-
- tops_ = NPU_VERSION_TOPS (meta->npu_version);
- free (meta);
-
- return 0;
- }
-
void check_memleak () {
size_t alloc_total = 0;
size_t free_total = 0;
uint32_t model_id;
int req_id, status;
- status = UtilTRIV2::init (tops_);
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
if (mode_ == AGING_TEST_MODEL) {
/* repeat model_register/unregister */
for (uint32_t i = 0; i < num_iter_; i++) {
return status;
}
+ void set_model (string model_dir) { model_dir_ = model_dir; }
+
private:
string model_dir_;
uint32_t tops_;
main (int argc, char **argv) {
const char *help = "model_dir test_mode num_iterations";
Tester tester;
- int status;
- int index;
+ int status, index;
+ uint32_t model_id;
+ char *dir;
status = tester.parseArgs (argc, argv, help, &index);
if (status == test_ret_skipped || index < 0)
goto skip;
}
+ dir = argv[index];
/** initialize triv2 device */
- status = tester.init (argv[index]);
+ status = tester.prepare (dir, &model_id);
if (status < test_ret_success)
goto err;
+ tester.set_model (dir);
+
/** configure testing */
status = tester.config (argv[index + 1], argv[index + 2]);
if (status < test_ret_success)
using namespace std;
-/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2 {
- public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- std::string model_path = model_dir + "/model.tvn";
- npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
- if (meta == nullptr) {
- cerr << "Failed to get the model metadata\n";
- return -EINVAL;
- }
-
- uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
- free (meta);
-
- int status = UtilTRIV2::init (tops);
- if (status != test_ret_success) {
- cerr << "Failed to initialize the device\n";
- return status;
- }
-
- return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
- }
-
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
-
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
-
- return wait () == 1 ? test_ret_success : test_ret_failure;
- }
-
- private:
- uint32_t model_id_;
-};
-
/** @brief apptest main */
int
main (int argc, char **argv) {
- Tester tester;
+ UtilTRIV2 tester;
char *dir_str;
- int status;
- int index;
+ int task_id, status, index;
std::ios_base::fmtflags f (cerr.flags ());
return -errno;
}
- uint32_t total_succs, total_fails;
+ uint32_t total_succs, total_fails, model_id;
struct dirent *dir;
total_succs = total_fails = 0;
/** initialize triv2 device */
string path (dir_str);
- status = tester.init (path + "/" + dir->d_name);
+ status = tester.prepare (path + "/" + dir->d_name, &model_id);
if (status == test_ret_success) {
/** run the inference with the device */
- status = tester.run ();
+ status = tester.run (model_id, &task_id);
if (status == test_ret_success) {
cerr << "PASSED\n";
total_succs++;
if (mid_model_id_ == 0 || high_model_id_ == 0)
return test_ret_failure;
- int mid_task_id = UtilTRIV2::run (mid_model_id_);
+ int mid_task_id = UtilTrinity::run (mid_model_id_);
if (mid_task_id < 0)
return mid_task_id;
- int high_task_id = UtilTRIV2::run (high_model_id_);
+ int high_task_id = UtilTrinity::run (high_model_id_);
if (high_task_id < 0)
return high_task_id;
/** @brief C++ class to describe how to use npu-engine library */
class Tester : public UtilTRIV2 {
public:
- Tester () : model_id_ (0) {}
-
- /** @brief initilize the device handle */
- int init (std::string model_dir) {
- int status = UtilTRIV2::init ();
- if (status != test_ret_success) {
- cerr << "Failed to initialize\n";
- return status;
- }
-
- return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
- }
-
- /** @brief run the inference */
- int run () {
- if (model_id_ == 0)
- return test_ret_failure;
-
- int task_id = UtilTRIV2::run (model_id_);
- if (task_id < 0)
- return task_id;
-
- int result = wait () == 1 ? test_ret_success : test_ret_failure;
-
- print_profile (task_id);
-
- return result;
- }
-
/** @brief print profile */
void print_profile (int task_id) {
npu_profile profile;
cerr << "Failed to get profile: " << status << "\n";
}
}
-
- private:
- uint32_t model_id_;
};
/** @brief apptest main */
main (int argc, char **argv) {
Tester tester;
char *dir;
- int status;
- int index;
+ int task_id, status, index;
+ uint32_t model_id;
status = tester.parseArgs (argc, argv, "model_path", &index);
if (status == test_ret_skipped || index < 0)
goto err;
dir = argv[index];
+
/** initialize triv2 device */
- status = tester.init (dir);
+ status = tester.prepare (dir, &model_id);
if (status < test_ret_success)
goto err;
/** run the inference with the device */
- status = tester.run ();
+ status = tester.run (model_id, &task_id);
if (status < test_ret_success)
goto err;
+ tester.print_profile (task_id);
+
cerr << "[APPTEST] " << argv[0] << ": PASSED\n";
return 0;
* @brief test APIs to run model with input data (TRIV2)
*/
TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3) {
- UtilTRIV2 *tester = new UtilTRIV2 (testenv.get_devices ()[1]);
+ UtilTrinity *tester = new UtilTrinity (testenv.get_devices ()[1]);
ASSERT_EQ (tester->init (testenv.get_tops ()), 0);
* @brief test APIs to run model with input data (TRIV2) (negative cases)
*/
TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3_n) {
- UtilTRIV2 *tester = new UtilTRIV2 (testenv.get_devices ()[1]);
+ UtilTrinity *tester = new UtilTrinity (testenv.get_devices ()[1]);
/* without init */
EXPECT_NE (tester->run (0, true), 0);
* @brief test profile APIs (legacy)
*/
TEST (ne_libnpuhost_test, profile_apis_legacy) {
- UtilTRIV2 tester;
+ UtilTrinity tester;
ASSERT_EQ (tester.init (testenv.get_tops ()), 0);
return setNPU_constraint (dev_, model_id, constraint);
}
+/**
+ * @brief Get model's tops
+ * @param[in] model_dir The directory of tvn model to get tops
+ */
+int
+UtilTRIV2::get_tops (std::string model_dir) {
+ if (model_dir == "")
+ return -EINVAL;
+
+ std::string model_path = model_dir + "/model.tvn";
+ npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
+ if (meta == nullptr)
+ return -EINVAL;
+
+ uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
+ free (meta);
+
+ return tops;
+}
+
+/**
+ * @brief Init and load model
+ * @param[in] model_dir The directory of tvn model to test
+ * @param[out] model_id The model id of given model
+ */
+int
+UtilTRIV2::prepare (std::string model_dir, uint32_t *model_id) {
+ int status, tops = get_tops (model_dir);
+ status = UtilTrinity::init (tops);
+
+ if (status != test_ret_success)
+ return status;
+
+ return UtilTRIV2::loadModel (model_dir, model_id, NPU_PRIORITY_MID, 10);
+}
+
+/**
+ * @brief Run initialized model
+ * @param[in] model_id The model id to run
+ * @param[out] task_id The task id of model
+ */
+int
+UtilTRIV2::run (uint32_t model_id, int *task_id) {
+ if (model_id == 0)
+ return test_ret_failure;
+
+ *task_id = UtilTrinity::run (model_id);
+ if (*task_id < 0)
+ return *task_id;
+
+ return wait () == 1 ? test_ret_success : test_ret_failure;
+}
+
/** @brief implementation of extract_node_id for TRIV2 */
bool
UtilTRIV2::extract_node_id (uint32_t *id) {
/** @brief utility to access trinity device */
class UtilTrinity {
public:
+ UtilTrinity () : UtilTrinity (NPUCOND_TRIV24_CONN_SOCIP, true, true) {}
+ UtilTrinity (dev_type type) : UtilTrinity (type, true, true) {}
UtilTrinity (dev_type type, bool need_model, bool verify);
virtual ~UtilTrinity ();
UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV24_CONN_SOCIP, true, true) {}
UtilTRIV2 (dev_type type) : UtilTrinity (type, true, true) {}
+ int get_tops (std::string model_dir);
+ int prepare (std::string model_dir, uint32_t *model_id);
+ int run (uint32_t model_id, int *task_id);
+
private:
bool check_version (npubin_meta *meta);
int set_data_info (npubin_meta *meta, uint32_t model_id);