1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 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 #ifndef MINDSPORE_BENCHMARK_BENCHMARK_UNIFIED_API_H_ 18 #define MINDSPORE_BENCHMARK_BENCHMARK_UNIFIED_API_H_ 19 20 #include <signal.h> 21 #include <random> 22 #include <unordered_map> 23 #include <fstream> 24 #include <iostream> 25 #include <map> 26 #include <cmath> 27 #include <string> 28 #include <vector> 29 #include <memory> 30 #include <cfloat> 31 #include <utility> 32 #ifndef BENCHMARK_CLIP_JSON 33 #include <nlohmann/json.hpp> 34 #endif 35 #include "tools/benchmark/benchmark_base.h" 36 #include "include/model.h" 37 #include "tools/common/flag_parser.h" 38 #include "src/common/file_utils.h" 39 #include "src/common/utils.h" 40 #include "include/api/types.h" 41 #include "include/api/model.h" 42 43 namespace mindspore::lite { 44 45 class MS_API BenchmarkUnifiedApi : public BenchmarkBase { 46 public: BenchmarkUnifiedApi(BenchmarkFlags * flags)47 explicit BenchmarkUnifiedApi(BenchmarkFlags *flags) : BenchmarkBase(flags) {} 48 49 virtual ~BenchmarkUnifiedApi(); 50 51 int RunBenchmark() override; 52 53 protected: 54 int CompareDataGetTotalBiasAndSize(const std::string &name, mindspore::MSTensor *tensor, float *total_bias, 55 int *total_size); 56 void InitContext(const std::shared_ptr<mindspore::Context> &context); 57 58 // call GenerateRandomData to fill inputTensors 59 int GenerateInputData() override; 60 61 int ReadInputFile() override; 62 63 void InitMSContext(const std::shared_ptr<Context> &context); 64 65 int GetDataTypeByTensorName(const std::string &tensor_name) override; 66 67 int CompareOutput() override; 68 69 int InitTimeProfilingCallbackParameter() override; 70 71 int InitPerfProfilingCallbackParameter() override; 72 73 int InitDumpTensorDataCallbackParameter() override; 74 75 int InitPrintTensorDataCallbackParameter() override; 76 77 int PrintInputData(); 78 79 template <typename T> ConverterToInt64Vector(const std::vector<T> & srcDims)80 std::vector<int64_t> ConverterToInt64Vector(const std::vector<T> &srcDims) { 81 std::vector<int64_t> dims; 82 for (auto shape : srcDims) { 83 dims.push_back(static_cast<int64_t>(shape)); 84 } 85 return dims; 86 } 87 88 int MarkPerformance(); 89 90 int MarkAccuracy(); 91 92 private: 93 mindspore::Model ms_model_; 94 std::vector<mindspore::MSTensor> ms_inputs_for_api_; 95 96 MSKernelCallBack ms_before_call_back_ = nullptr; 97 MSKernelCallBack ms_after_call_back_ = nullptr; 98 }; 99 100 } // namespace mindspore::lite 101 #endif // MINNIE_BENCHMARK_BENCHMARK_H_ 102