pytorch

Форк
0
/
load_benchmark_torch.cc 
93 строки · 2.7 Кб
1
/**
2
 * Copyright (c) 2016-present, Facebook, Inc.
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
#include <string>
18
#include <vector>
19

20
#include <ATen/ATen.h>
21
#include "caffe2/core/timer.h"
22
#include "caffe2/utils/string_utils.h"
23
#include <torch/csrc/autograd/grad_mode.h>
24
#include <torch/csrc/jit/mobile/module.h>
25
#include <torch/csrc/jit/mobile/import.h>
26
#include <torch/csrc/jit/serialization/import.h>
27
#include <torch/script.h>
28

29
#include <c10/mobile/CPUCachingAllocator.h>
30

31
#include <chrono>
32
using namespace std::chrono;
33

34
C10_DEFINE_string(model, "", "The given torch script model to benchmark.");
35
C10_DEFINE_int(iter, 10, "The number of iterations to run.");
36
C10_DEFINE_bool(
37
  report_pep,
38
  true,
39
  "Whether to print performance stats for AI-PEP.");
40

41
int main(int argc, char** argv) {
42
  c10::SetUsageMessage(
43
    "Run model load time benchmark for pytorch model.\n"
44
    "Example usage:\n"
45
    "./load_benchmark_torch"
46
    " --model=<model_file>"
47
    " --iter=20");
48
  if (!c10::ParseCommandLineFlags(&argc, &argv)) {
49
    std::cerr << "Failed to parse command line flags!" << std::endl;
50
    return 1;
51
  }
52

53
  std::cout << "Starting benchmark." << std::endl;
54
  CAFFE_ENFORCE(
55
      FLAGS_iter >= 0,
56
      "Number of main runs should be non negative, provided ",
57
      FLAGS_iter,
58
      ".");
59

60
  caffe2::Timer timer;
61
  std::vector<long> times;
62

63
  for (int i = 0; i < FLAGS_iter; ++i) {
64
    auto start = high_resolution_clock::now();
65

66
#if BUILD_LITE_INTERPRETER
67
    auto module = torch::jit::_load_for_mobile(FLAGS_model);
68
#else
69
    auto module = torch::jit::load(FLAGS_model);
70
#endif
71

72
    auto stop = high_resolution_clock::now();
73
    auto duration = duration_cast<microseconds>(stop - start);
74
    times.push_back(duration.count());
75
  }
76

77
  const double micros = static_cast<double>(timer.MicroSeconds());
78
  if (FLAGS_report_pep) {
79
    for (auto t : times) {
80
      std::cout << R"(PyTorchObserver {"type": "NET", "unit": "us", )"
81
                << R"("metric": "latency", "value": ")"
82
                << t << R"("})" << std::endl;
83
    }
84
  }
85

86
  const double iters = static_cast<double>(FLAGS_iter);
87
  std::cout << "Main run finished. Microseconds per iter: "
88
            << micros / iters
89
            << ". Iters per second: " << 1000.0 * 1000 * iters / micros
90
            << std::endl;
91

92
  return 0;
93
}
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.