Home | History | Annotate | Download | only in benchmark
      1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_PARAMS_H_
     17 #define TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_PARAMS_H_
     18 #include <memory>
     19 #include <string>
     20 #include <unordered_map>
     21 #include <vector>
     22 
     23 #include "tensorflow/lite/tools/benchmark/logging.h"
     24 
     25 namespace tflite {
     26 namespace benchmark {
     27 
     28 template <typename T>
     29 class TypedBenchmarkParam;
     30 
     31 class BenchmarkParam {
     32  protected:
     33   enum class ParamType { TYPE_INT32, TYPE_FLOAT, TYPE_BOOL, TYPE_STRING };
     34   template <typename T>
     35   static ParamType GetValueType();
     36 
     37  public:
     38   template <typename T>
     39   static std::unique_ptr<BenchmarkParam> Create(const T& default_value) {
     40     return std::unique_ptr<BenchmarkParam>(
     41         new TypedBenchmarkParam<T>(default_value));
     42   }
     43 
     44   template <typename T>
     45   TypedBenchmarkParam<T>* AsTyped() {
     46     AssertHasSameType(GetValueType<T>(), type_);
     47     return static_cast<TypedBenchmarkParam<T>*>(this);
     48   }
     49   virtual ~BenchmarkParam() {}
     50   BenchmarkParam(ParamType type) : type_(type) {}
     51 
     52  private:
     53   static void AssertHasSameType(ParamType a, ParamType b);
     54 
     55   const ParamType type_;
     56 };
     57 
     58 template <typename T>
     59 class TypedBenchmarkParam : public BenchmarkParam {
     60  public:
     61   TypedBenchmarkParam(const T& value)
     62       : BenchmarkParam(GetValueType<T>()), value_(value) {}
     63   void Set(const T& value) { value_ = value; }
     64 
     65   T Get() { return value_; }
     66 
     67  private:
     68   T value_;
     69 };
     70 
     71 class BenchmarkParams {
     72  public:
     73   void AddParam(const std::string& name,
     74                 std::unique_ptr<BenchmarkParam> value) {
     75     params_[name] = std::move(value);
     76   }
     77 
     78   bool HasParam(const std::string& name) const {
     79     return params_.find(name) != params_.end();
     80   }
     81 
     82   template <typename T>
     83   void Set(const std::string& name, const T& value) {
     84     AssertParamExists(name);
     85     params_.at(name)->AsTyped<T>()->Set(value);
     86   }
     87 
     88   template <typename T>
     89   T Get(const std::string& name) const {
     90     AssertParamExists(name);
     91     return params_.at(name)->AsTyped<T>()->Get();
     92   }
     93 
     94  private:
     95   void AssertParamExists(const std::string& name) const;
     96   std::unordered_map<std::string, std::unique_ptr<BenchmarkParam>> params_;
     97 };
     98 
     99 }  // namespace benchmark
    100 }  // namespace tflite
    101 #endif  // TENSORFLOW_LITE_TOOLS_BENCHMARK_BENCHMARK_PARAMS_H_
    102