c0b304c7489cfc646c5b24174d2964bb09a6b7c2
[platform/core/ml/nnfw.git] / runtime / libs / tflite / include / tflite / RandomTestRunner.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file  RandomTestRunner.h
19  * @brief This file contains class for random input testing
20  */
21
22 #ifndef __NNFW_TFLITE_RANDOM_TEST_RUNNER_H__
23 #define __NNFW_TFLITE_RANDOM_TEST_RUNNER_H__
24
25 #include "tflite/interp/Builder.h"
26
27 #include <misc/RandomGenerator.h>
28
29 namespace nnfw
30 {
31 namespace tflite
32 {
33
34 /**
35  * @brief Structure for NNAPI correctness test
36  */
37 struct RandomTestParam
38 {
39   int verbose;               //!< Verbosity of debug information
40   int tolerance;             //!< Torlerance of value difference
41   int tensor_logging = 0;    //!< Save logging to a file if not 0
42   std::string log_path = ""; //!< Path of log file, meaningful only when tensor_logging is 1
43 };
44
45 /**
46  * @brief Class to define Random test runner
47  */
48 class RandomTestRunner
49 {
50 public:
51   /**
52    * @brief     Construct a new RandomTestRunner object
53    * @param[in] seed          Random seed value
54    * @param[in] param         RandomTestParam object for test runner
55    * @param[in] quantization  TfLiteQuantizationParams type to represent quantization value
56    */
57   RandomTestRunner(uint32_t seed, const RandomTestParam &param)
58       : _randgen{seed, 0.0f, 2.0f}, _param{param}
59   {
60     // DO NOTHING
61   }
62
63 public:
64   /**
65    * @brief     Run the random test runner
66    * @param[in] running_count  Count to run tflite interpreter with NNAPI
67    * @return    0 if test succeeds, otherwise failure
68    */
69   int run(size_t running_count);
70
71 public:
72   /**
73    * @brief  Get RandomGenerator reference
74    * @return RandomGenerator reference
75    */
76   nnfw::misc::RandomGenerator &generator() { return _randgen; };
77
78 public:
79   /**
80    * @brief     Compile the random test runner
81    * @param[in] builder  Interpreter Builder used to run
82    */
83   void compile(const nnfw::tflite::Builder &builder);
84
85 private:
86   nnfw::misc::RandomGenerator _randgen;
87   const RandomTestParam _param;
88   std::unique_ptr<::tflite::Interpreter> _tfl_interp;
89   std::unique_ptr<::tflite::Interpreter> _nnapi;
90
91 public:
92   /**
93    * @brief     Create a RandomTestRunner object
94    * @param[in] seed  Random seed value
95    * @return    RandomGenerator object
96    */
97   static RandomTestRunner make(uint32_t seed);
98 };
99
100 } // namespace tflite
101 } // namespace nnfw
102
103 #endif // __NNFW_TFLITE_RANDOM_TEST_RUNNER_H__