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++)
60 dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
63 void prepareOutputPath (uint32_t num) {
64 if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
66 outpath_[0] = dirpath_ + "/output_fmap.bin";
68 outpath_.resize (num);
69 for (uint32_t idx = 0; idx < num; idx++)
71 dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
74 const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
75 const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
77 npubin_meta *getMetadata () { return meta_; }
78 std::string &getDirpath () { return dirpath_; }
79 uint32_t getModelID () { return model_id_; }
80 input_buffers *getInput () { return &input_; }
81 output_buffers *getOutput () { return &output_; }
92 output_buffers output_;
94 std::vector<std::string> inpath_;
95 std::vector<std::string> outpath_;
98 /** @brief utility to access trinity device */
101 UtilTrinity (dev_type type, bool need_model, bool verify);
102 virtual ~UtilTrinity ();
104 npudev_h getDeviceHandle () { return dev_; }
106 static void setDump () { dump_ = true; }
107 void setTrinityFormat () { trinity_format_ = true; }
108 void setSync () { sync_ = true; }
110 void setNotiMode (const std::string mode);
111 void setNodePath (const std::string node);
112 void setProfLevel (const std::string level);
114 int init (uint32_t tops = default_tops);
116 void printUsage (const char *prog_name, const char *param_str = nullptr);
117 int parseArgs (int argc, char **argv, const char *param_str = nullptr,
118 int *index = nullptr);
119 int loadModel (std::string dirpath, uint32_t *model_id,
120 npu_priority priority = NPU_PRIORITY_MID,
121 uint32_t timeout = 5000);
122 int unloadModel (uint32_t model_id);
124 int createRequest (uint32_t model_id);
125 int submitRequest (int req_id);
126 int removeRequest (int req_id);
128 int run (uint32_t model_id);
129 int run (uint32_t model_id, bool sync);
131 int runAll (bool sync);
133 int getProfile (int req_id, npu_profile *profile);
134 int getMemoryStatus (size_t *alloc_total, size_t *free_total);
136 UtilModel *findModel (uint32_t model_id);
138 static void callbackVerify (output_buffers *output, int req_id, void *data);
139 static void callback (output_buffers *output, int req_id, void *data);
145 std::vector<std::unique_ptr<UtilModel>> models_;
146 npu_notimode noti_mode_;
147 profile_level_type prof_level_;
149 bool trinity_format_;
157 int set_constraint (uint32_t model_id, uint32_t timeout,
158 npu_priority priority);
159 int run_each (UtilModel *model, bool sync);
161 virtual bool check_version (npubin_meta *meta) { return false; }
162 virtual int prepare_model (UtilModel *model) { return -EPERM; }
163 virtual int set_data_info (npubin_meta *meta, uint32_t model_id) {
166 virtual bool extract_node_id (uint32_t *id) { return false; }
170 static std::mutex m_;
171 static std::condition_variable cv_;
173 static uint32_t success_;
174 static uint32_t done_;
175 static uint32_t total_;
180 class UtilTRIV2 : public UtilTrinity {
182 UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
185 bool check_version (npubin_meta *meta);
186 int set_data_info (npubin_meta *meta, uint32_t model_id);
187 int prepare_model (UtilModel *model);
188 bool extract_node_id (uint32_t *id);
195 test_ret_failure = -1,
196 test_ret_success = 0,
198 test_ret_skipped_wrong_num_args,
199 test_ret_skipped_not_compatible,
203 const char msg_skipped_reason[][255] = {
206 "Wrong number of arguments are provided.",
207 "The given model is not compatible with this test.",
210 typedef int (*FUNC_APPTEST) (npudev_h, const char *, const char *);
213 * @brief entry function to run apptest
214 * @param[in] dev npu device
215 * @param[in] argv arguments from main function
216 * @param[in] func inference function ptr for each sub-directory
217 * @note it traverse sub-directory and call 'func'
218 * @return 0 if no error, otherwise a negative errno
220 int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func);
223 * @brief Fill input buffer with 'x00'
224 * @param[in] buffer The buffer used for input data
225 * @param[in] offset The offset indicating the point where the actual data starts in the buffer
226 * @param[in] size The size of input data
228 void fill_input_data (generic_buffer *buffer, size_t offset, size_t size);
231 * @brief Make a model based on the parameter of the npubin_meta type, meta
232 * @param[in] meta Meta data required to make a model
233 * @param[in] buf_type The memory type of the buffer included in the model
234 * @return A generic buffer including dummy model data, NULL if error
236 generic_buffer *make_model (npudev_h dev, npubin_meta *meta,
237 buffer_types buf_type);
240 * @brief Destroy generic buffer with model data
241 * @param[in] model the generic buffer instance
243 void destroy_model (npudev_h dev, generic_buffer *model);
246 * @brief check memory leak by calling memory status API
247 * @param[in] dev npu device handle
248 * @return 0 if no error, otherwise a negative errno
250 int check_memory_leak (npudev_h dev);
252 #if defined(__cplusplus)
256 #endif /* _NE_TEST_UTILS_H_ */