You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

benchmark.h 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Copyright 2019 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. #ifndef PREDICT_BENCHMARK_BENCHMARK_H_
  17. #define PREDICT_BENCHMARK_BENCHMARK_H_
  18. #include <getopt.h>
  19. #include <signal.h>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <map>
  23. #include <string>
  24. #include <vector>
  25. #include <memory>
  26. #include <unordered_map>
  27. #include "common/flag_parser.h"
  28. #include "common/file_utils.h"
  29. #include "common/func_utils.h"
  30. #include "common/mslog.h"
  31. #include "common/utils.h"
  32. #include "include/errorcode.h"
  33. #include "include/session.h"
  34. #include "include/tensor.h"
  35. #include "schema/inner/ms_generated.h"
  36. #include "src/graph.h"
  37. #include "src/graph_execution.h"
  38. #include "src/op.h"
  39. namespace mindspore {
  40. namespace predict {
  41. enum InDataType { kImage = 0, kBinary = 1 };
  42. struct CheckTensor {
  43. CheckTensor(const std::vector<size_t> &shape, const std::vector<float> &data) {
  44. this->shape = shape;
  45. this->data = data;
  46. }
  47. std::vector<size_t> shape;
  48. std::vector<float> data;
  49. };
  50. class BenchmarkFlags : public virtual FlagParser {
  51. public:
  52. BenchmarkFlags() {
  53. // common
  54. AddFlag(&BenchmarkFlags::modelPath, "modelPath", "Input model path", "");
  55. AddFlag(&BenchmarkFlags::tensorDataTypeIn, "tensorDataType", "Data type of input Tensor. float", "float");
  56. AddFlag(&BenchmarkFlags::inDataPath, "inDataPath", "Input data path, if not set, use random input", "");
  57. // MarkPerformance
  58. AddFlag(&BenchmarkFlags::loopCount, "loopCount", "Run loop count", 10);
  59. AddFlag(&BenchmarkFlags::numThreads, "numThreads", "Run threads number", 2);
  60. AddFlag(&BenchmarkFlags::warmUpLoopCount, "warmUpLoopCount", "Run warm up loop", 3);
  61. // MarkAccuracy
  62. AddFlag(&BenchmarkFlags::calibDataPath, "calibDataPath", "Calibration data file path", "");
  63. }
  64. ~BenchmarkFlags() override = default;
  65. public:
  66. // common
  67. std::string modelPath;
  68. std::string inDataPath;
  69. InDataType inDataType;
  70. std::string inDataTypeIn;
  71. DataType tensorDataType;
  72. std::string tensorDataTypeIn;
  73. // MarkPerformance
  74. int loopCount;
  75. int numThreads;
  76. int warmUpLoopCount;
  77. // MarkAccuracy
  78. std::string calibDataPath;
  79. };
  80. class Benchmark {
  81. public:
  82. explicit Benchmark(BenchmarkFlags *flags) : _flags(flags) {}
  83. virtual ~Benchmark() = default;
  84. STATUS Init();
  85. STATUS RunBenchmark();
  86. private:
  87. // call GenerateInputData or ReadInputFile to init inputTensors
  88. STATUS LoadInput();
  89. // call GenerateRandomData to fill inputTensors
  90. STATUS GenerateInputData();
  91. STATUS GenerateRandomData(size_t size, void *data);
  92. STATUS ReadInputFile();
  93. STATUS ReadCalibData();
  94. STATUS CleanData();
  95. STATUS CompareOutput(const std::map<NODE_ID, std::vector<Tensor *>> &msOutputs);
  96. float CompareData(const std::string &nodeName, std::vector<int64_t> msShape, float *msTensorData);
  97. STATUS MarkPerformance();
  98. STATUS MarkAccuracy();
  99. private:
  100. BenchmarkFlags *_flags;
  101. std::shared_ptr<Session> session;
  102. Context ctx;
  103. std::vector<Tensor *> msInputs;
  104. std::map<std::string, std::vector<Tensor *>> msOutputs;
  105. std::unordered_map<std::string, CheckTensor *> calibData;
  106. std::string modelName = "";
  107. bool cleanData = true;
  108. const float US2MS = 1000.0f;
  109. const float percentage = 100.0f;
  110. const int printNum = 50;
  111. const float minFloatThr = 0.0000001f;
  112. const uint64_t maxTimeThr = 1000000;
  113. };
  114. int RunBenchmark(int argc, const char **argv);
  115. } // namespace predict
  116. } // namespace mindspore
  117. #endif // PREDICT_BENCHMARK_BENCHMARK_H_