7349c504ac480375cd04b4b87dc78f72df76a19a
[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] =
60             dirpath_ + "/input_fmap_" + std::to_string (idx) + ".bin";
61     }
62   }
63   void prepareOutputPath (uint32_t num) {
64     if (NPUBIN_VERSION (meta_->magiccode) <= 1) {
65       outpath_.resize (1);
66       outpath_[0] = dirpath_ + "/output_fmap.bin";
67     } else {
68       outpath_.resize (num);
69       for (uint32_t idx = 0; idx < num; idx++)
70         outpath_[idx] =
71             dirpath_ + "/output_fmap_" + std::to_string (idx) + ".bin";
72     }
73   }
74   const char *getInputPath (uint32_t idx) { return inpath_[idx].c_str (); }
75   const char *getOutputPath (uint32_t idx) { return outpath_[idx].c_str (); }
76
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_; }
82
83  private:
84   npudev_h dev_;
85
86   npubin_meta *meta_;
87   uint32_t model_id_;
88
89   std::string dirpath_;
90
91   input_buffers input_;
92   output_buffers output_;
93
94   std::vector<std::string> inpath_;
95   std::vector<std::string> outpath_;
96 };
97
98 /** @brief utility to access trinity device */
99 class UtilTrinity {
100  public:
101   UtilTrinity (dev_type type, bool need_model, bool verify);
102   virtual ~UtilTrinity ();
103
104   npudev_h getDeviceHandle () { return dev_; }
105
106   static void setDump () { dump_ = true; }
107   void setTrinityFormat () { trinity_format_ = true; }
108   void setSync () { sync_ = true; }
109   void setMute ();
110   void setNotiMode (const std::string mode);
111   void setNodePath (const std::string node);
112   void setProfLevel (const std::string level);
113
114   int init (uint32_t tops = default_tops);
115   void clear ();
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);
123
124   int createRequest (uint32_t model_id);
125   int submitRequest (int req_id);
126   int removeRequest (int req_id);
127
128   int run (uint32_t model_id);
129   int run (uint32_t model_id, bool sync);
130   int runAll ();
131   int runAll (bool sync);
132
133   int getProfile (int req_id, npu_profile *profile);
134   int getMemoryStatus (size_t *alloc_total, size_t *free_total);
135
136   UtilModel *findModel (uint32_t model_id);
137
138   static void callbackVerify (output_buffers *output, int req_id, void *data);
139   static void callback (output_buffers *output, int req_id, void *data);
140
141   uint32_t wait ();
142
143  protected:
144   npudev_h dev_;
145   std::vector<std::unique_ptr<UtilModel>> models_;
146   npu_notimode noti_mode_;
147   profile_level_type prof_level_;
148   bool need_model_;
149   bool trinity_format_;
150   bool mute_;
151   bool sync_;
152   bool verify_;
153
154   std::string node_;
155
156  private:
157   int set_constraint (uint32_t model_id, uint32_t timeout,
158                       npu_priority priority);
159   int run_each (UtilModel *model, bool sync);
160
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) {
164     return -EPERM;
165   }
166   virtual bool extract_node_id (uint32_t *id) { return false; }
167
168   dev_type type_;
169
170   static std::mutex m_;
171   static std::condition_variable cv_;
172
173   static uint32_t success_;
174   static uint32_t done_;
175   static uint32_t total_;
176
177   static bool dump_;
178 };
179
180 class UtilTRIV2 : public UtilTrinity {
181  public:
182   UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV2_CONN_SOCIP, true, true) {}
183
184  private:
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);
189 };
190
191 extern "C" {
192 #endif
193
194 enum test_ret {
195   test_ret_failure = -1,
196   test_ret_success = 0,
197   test_ret_skipped,
198   test_ret_skipped_wrong_num_args,
199   test_ret_skipped_not_compatible,
200   test_ret_none,
201 };
202
203 const char msg_skipped_reason[][255] = {
204     "",
205     "",
206     "Wrong number of arguments are provided.",
207     "The given model is not compatible with this test.",
208 };
209
210 typedef int (*FUNC_APPTEST) (npudev_h, const char *, const char *);
211
212 /**
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
219  */
220 int run_apptest (npudev_h dev, char **argv, FUNC_APPTEST func);
221
222 /**
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
227  */
228 void fill_input_data (generic_buffer *buffer, size_t offset, size_t size);
229
230 /**
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
235  */
236 generic_buffer *make_model (npudev_h dev, npubin_meta *meta,
237                             buffer_types buf_type);
238
239 /**
240  * @brief Destroy generic buffer with model data
241  * @param[in] model the generic buffer instance
242  */
243 void destroy_model (npudev_h dev, generic_buffer *model);
244
245 /**
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
249  */
250 int check_memory_leak (npudev_h dev);
251
252 #if defined(__cplusplus)
253 }
254 #endif
255
256 #endif /* _NE_TEST_UTILS_H_ */