[Format] Revise the format of whole source codes
[platform/adaptation/npu/trix-engine.git] / tests / utils / ne_test_utils.h
1 /**
2  * Proprietary
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>
6  */
7 /**
8  * @file ne_test_utils.h
9  * @date 20 Aug 2019
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
14  */
15
16 #ifndef _NE_TEST_UTILS_H_
17 #define _NE_TEST_UTILS_H_
18
19 #include "ne_test_utils_common.h"
20
21 #include <libnpuhost.h>
22 #include <npubinfmt.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #if defined(__cplusplus)
28 #include <iostream>
29 #include <string>
30 #include <mutex>
31 #include <condition_variable>
32 #include <vector>
33
34 /** @brief utility to access test model */
35 class UtilModel {
36  public:
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));
40   }
41   ~UtilModel () {
42     if (output_.num_buffers > 0)
43       cleanNPU_genericBuffers (dev_, &output_);
44
45     if (meta_)
46       free (meta_);
47   }
48
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) {
54       inpath_.resize (1);
55       inpath_[0] = dirpath_ + "/input_fmap.bin";
56     } else {
57       inpath_.resize (num);
58       for (uint32_t idx = 0; idx < num; idx++)
59         inpath_[idx] = dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
60     }
61   }
62   void prepareOutputPath (uint32_t num) {
63     if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
64       outpath_.resize (1);
65       outpath_[0] = dirpath_ + "/output_fmap.bin";
66     } else {
67       outpath_.resize (num);
68       for (uint32_t idx = 0; idx < num; idx++)
69         outpath_[idx] = dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
70     }
71   }
72   const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
73   const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
74
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_; }
80
81  private:
82   npudev_h dev_;
83
84   npubin_meta *meta_;
85   uint32_t model_id_;
86
87   std::string dirpath_;
88
89   input_buffers input_;
90   output_buffers output_;
91
92   std::vector<std::string> inpath_;
93   std::vector<std::string> outpath_;
94 };
95
96 /** @brief utility to access trinity device */
97 class UtilTrinity {
98  public:
99   UtilTrinity (dev_type type, bool need_model, bool verify);
100   virtual ~UtilTrinity ();
101
102   npudev_h getDeviceHandle () { return dev_; }
103
104   static void setDump () { dump_ = true; }
105   void setTrinityFormat () { trinity_format_ = true; }
106   void setSync () { sync_ = true; }
107   void setMute ();
108   void setNotiMode (const std::string mode);
109   void setNodePath (const std::string node);
110   void setProfLevel (const std::string level);
111
112   int init (uint32_t tops = default_tops);
113   void clear ();
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);
119
120   int createRequest (uint32_t model_id);
121   int submitRequest (int req_id);
122   int removeRequest (int req_id);
123
124   int run (uint32_t model_id);
125   int run (uint32_t model_id, bool sync);
126   int runAll ();
127   int runAll (bool sync);
128
129   int getProfile (int req_id, npu_profile *profile);
130   int getMemoryStatus (size_t *alloc_total, size_t *free_total);
131
132   UtilModel *findModel (uint32_t model_id);
133
134   static void callbackVerify (output_buffers *output, int req_id, void *data);
135   static void callback (output_buffers *output, int req_id, void *data);
136
137   uint32_t wait ();
138
139  protected:
140   npudev_h dev_;
141   std::vector<std::unique_ptr<UtilModel>> models_;
142   npu_notimode noti_mode_;
143   profile_level_type prof_level_;
144   bool need_model_;
145   bool trinity_format_;
146   bool mute_;
147   bool sync_;
148   bool verify_;
149
150   std::string node_;
151
152  private:
153   int set_constraint (uint32_t model_id, uint32_t timeout, npu_priority priority);
154   int run_each (UtilModel *model, bool sync);
155
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; }
160
161   dev_type type_;
162
163   static std::mutex m_;
164   static std::condition_variable cv_;
165
166   static uint32_t success_;
167   static uint32_t done_;
168   static uint32_t total_;
169
170   static bool dump_;
171 };
172
173 class UtilTRIV2 : public UtilTrinity {
174  public:
175   UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
176
177  private:
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);
182 };
183
184 extern "C" {
185 #endif
186
187 enum test_ret {
188   test_ret_failure = -1,
189   test_ret_success = 0,
190   test_ret_skipped,
191   test_ret_skipped_wrong_num_args,
192   test_ret_skipped_not_compatible,
193   test_ret_none,
194 };
195
196 const char msg_skipped_reason[][255] = {
197     "",
198     "",
199     "Wrong number of arguments are provided.",
200     "The given model is not compatible with this test.",
201 };
202
203 typedef int (*FUNC_APPTEST) (npudev_h, const char *, const char *);
204
205 /**
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
212  */
213 int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func);
214
215 /**
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
220  */
221 void fill_input_data (generic_buffer *buffer, size_t offset, size_t size);
222
223 /**
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
228  */
229 generic_buffer *make_model (npudev_h dev, npubin_meta *meta, buffer_types buf_type);
230
231 /**
232  * @brief Destroy generic buffer with model data
233  * @param[in] model the generic buffer instance
234  */
235 void destroy_model (npudev_h dev, generic_buffer *model);
236
237 /**
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
241  */
242 int check_memory_leak (npudev_h dev);
243
244 #if defined(__cplusplus)
245 }
246 #endif
247
248 #endif /* _NE_TEST_UTILS_H_ */