3 * Copyright (C) 2019 Samsung Electronics
4 * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
5 * Copyright (C) 2019 Wook Song <wook16.song@samsung.com>
8 * @file ne_test_utils.h
10 * @brief Declarations of utility functions to use for testing
11 * @author Dongju Chae <dongju.chae@samsung.com>
12 * Wook Song <wook16.song@samsung.com>
13 * @bug No known bugs except for NYI items
16 #ifndef _NE_TEST_UTILS_H_
17 #define _NE_TEST_UTILS_H_
19 #include "ne_test_utils_common.h"
21 #include <libnpuhost.h>
22 #include <npubinfmt.h>
27 #if defined(__cplusplus)
31 #include <condition_variable>
34 /** @brief utility to access test model */
37 UtilModel (npudev_h dev) : dev_ (dev), meta_ (nullptr), model_id_ (0) {
38 memset (&input_, '\x00', sizeof (input_buffers));
39 memset (&output_, '\x00', sizeof (output_buffers));
42 if (output_.num_buffers > 0)
43 cleanNPU_genericBuffers (dev_, &output_);
49 void setMetadata (npubin_meta *meta) { meta_ = meta; }
50 void setDirpath (std::string dirpath) { dirpath_ = dirpath; }
51 void setModelID (uint32_t model_id) { model_id_ = model_id; }
52 void prepareInputPath (uint32_t num) {
53 if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
55 inpath_[0] = dirpath_ + "/input_fmap.bin";
58 for (uint32_t idx = 0; idx < num; idx++)
59 inpath_[idx] = dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
62 void prepareOutputPath (uint32_t num) {
63 if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
65 outpath_[0] = dirpath_ + "/output_fmap.bin";
67 outpath_.resize (num);
68 for (uint32_t idx = 0; idx < num; idx++)
69 outpath_[idx] = dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
72 const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
73 const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
75 npubin_meta *getMetadata () { return meta_; }
76 std::string &getDirpath () { return dirpath_; }
77 uint32_t getModelID () { return model_id_; }
78 input_buffers *getInput () { return &input_; }
79 output_buffers *getOutput () { return &output_; }
90 output_buffers output_;
92 std::vector<std::string> inpath_;
93 std::vector<std::string> outpath_;
96 /** @brief utility to access trinity device */
99 UtilTrinity (dev_type type, bool need_model, bool verify);
100 virtual ~UtilTrinity ();
102 npudev_h getDeviceHandle () { return dev_; }
104 static void setDump () { dump_ = true; }
105 void setTrinityFormat () { trinity_format_ = true; }
106 void setSync () { sync_ = true; }
108 void setNotiMode (const std::string mode);
109 void setNodePath (const std::string node);
110 void setProfLevel (const std::string level);
112 int init (uint32_t tops = default_tops);
114 void printUsage (const char *prog_name, const char *param_str = nullptr);
115 int parseArgs (int argc, char **argv, const char *param_str = nullptr, int *index = nullptr);
116 int loadModel (std::string dirpath, uint32_t *model_id, npu_priority priority = NPU_PRIORITY_MID,
117 uint32_t timeout = 5000);
118 int unloadModel (uint32_t model_id);
120 int createRequest (uint32_t model_id);
121 int submitRequest (int req_id);
122 int removeRequest (int req_id);
124 int run (uint32_t model_id);
125 int run (uint32_t model_id, bool sync);
127 int runAll (bool sync);
129 int getProfile (int req_id, npu_profile *profile);
130 int getMemoryStatus (size_t *alloc_total, size_t *free_total);
132 UtilModel *findModel (uint32_t model_id);
134 static void callbackVerify (output_buffers *output, int req_id, void *data);
135 static void callback (output_buffers *output, int req_id, void *data);
141 std::vector<std::unique_ptr<UtilModel>> models_;
142 npu_notimode noti_mode_;
143 profile_level_type prof_level_;
145 bool trinity_format_;
153 int set_constraint (uint32_t model_id, uint32_t timeout, npu_priority priority);
154 int run_each (UtilModel *model, bool sync);
156 virtual bool check_version (npubin_meta *meta) { return false; }
157 virtual int prepare_model (UtilModel *model) { return -EPERM; }
158 virtual int set_data_info (npubin_meta *meta, uint32_t model_id) { return -EPERM; }
159 virtual bool extract_node_id (uint32_t *id) { return false; }
163 static std::mutex m_;
164 static std::condition_variable cv_;
166 static uint32_t success_;
167 static uint32_t done_;
168 static uint32_t total_;
173 class UtilTRIV2 : public UtilTrinity {
175 UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
178 bool check_version (npubin_meta *meta);
179 int set_data_info (npubin_meta *meta, uint32_t model_id);
180 int prepare_model (UtilModel *model);
181 bool extract_node_id (uint32_t *id);
188 test_ret_failure = -1,
189 test_ret_success = 0,
191 test_ret_skipped_wrong_num_args,
192 test_ret_skipped_not_compatible,
196 const char msg_skipped_reason[][255] = {
199 "Wrong number of arguments are provided.",
200 "The given model is not compatible with this test.",
203 typedef int (*FUNC_APPTEST) (npudev_h, const char *, const char *);
206 * @brief entry function to run apptest
207 * @param[in] dev npu device
208 * @param[in] argv arguments from main function
209 * @param[in] func inference function ptr for each sub-directory
210 * @note it traverse sub-directory and call 'func'
211 * @return 0 if no error, otherwise a negative errno
213 int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func);
216 * @brief Fill input buffer with 'x00'
217 * @param[in] buffer The buffer used for input data
218 * @param[in] offset The offset indicating the point where the actual data starts in the buffer
219 * @param[in] size The size of input data
221 void fill_input_data (generic_buffer *buffer, size_t offset, size_t size);
224 * @brief Make a model based on the parameter of the npubin_meta type, meta
225 * @param[in] meta Meta data required to make a model
226 * @param[in] buf_type The memory type of the buffer included in the model
227 * @return A generic buffer including dummy model data, NULL if error
229 generic_buffer *make_model (npudev_h dev, npubin_meta *meta, buffer_types buf_type);
232 * @brief Destroy generic buffer with model data
233 * @param[in] model the generic buffer instance
235 void destroy_model (npudev_h dev, generic_buffer *model);
238 * @brief check memory leak by calling memory status API
239 * @param[in] dev npu device handle
240 * @return 0 if no error, otherwise a negative errno
242 int check_memory_leak (npudev_h dev);
244 #if defined(__cplusplus)
248 #endif /* _NE_TEST_UTILS_H_ */