benchmark.cc 8.5 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include <iostream>
T
tensor-tang 已提交
16
#include <random>
T
tensor-tang 已提交
17 18 19 20 21
#include <string>
#include <vector>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "paddle/fluid/operators/jit/kernels.h"
22
#include "paddle/fluid/platform/device_tracer.h"
T
tensor-tang 已提交
23 24 25 26 27 28 29 30 31
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/port.h"

DEFINE_int32(burning, 10, "Burning times.");
DEFINE_int32(repeat, 3000, "Repeat times.");
DEFINE_int32(max_size, 1000, "The Max size would be tested.");

template <typename T>
void RandomVec(const int n, T* a, const T lower = static_cast<T>(-20.f),
32 33
               const T upper = static_cast<T>(20.f), unsigned int seed = 100) {
  std::mt19937 rng(seed);
T
tensor-tang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  std::uniform_real_distribution<double> uniform_dist(0, 1);
  for (int i = 0; i < n; ++i) {
    a[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
  }
}

std::vector<int> TestSizes() {
  std::vector<int> s;
  for (int i = 1; i <= FLAGS_max_size; ++i) {
    s.push_back(i);
  }
  return s;
}

T
tensor-tang 已提交
48 49 50 51 52 53 54
template <typename KernelTuples, typename... Args>
struct BenchFunc {
  // return this function avg time
  double operator()(const typename KernelTuples::func_type tgt, Args... args) {
    for (int i = 0; i < FLAGS_burning; ++i) {
      tgt(args...);
    }
T
tensor-tang 已提交
55
    auto start = paddle::platform::PosixInNsec() * 1e-3;
T
tensor-tang 已提交
56 57 58
    for (int i = 0; i < FLAGS_repeat; ++i) {
      tgt(args...);
    }
T
tensor-tang 已提交
59
    auto end = paddle::platform::PosixInNsec() * 1e-3;
60
    return static_cast<double>(end - start) / FLAGS_repeat;
T
tensor-tang 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }
};

namespace jit = paddle::operators::jit;

template <jit::KernelType KT, typename KernelTuples, typename PlaceType,
          typename... Args>
void BenchAllImpls(const typename KernelTuples::attr_type& attr, Args... args) {
  BenchFunc<KernelTuples, Args...> benchmark;
  std::vector<std::pair<std::string, double>> infos;
  // test refer
  auto refer = jit::GetRefer<KT, KernelTuples>();
  if (!refer) {
    LOG(FATAL) << "Refer can not be empty!";
  }
  infos.push_back(std::make_pair("Refer", benchmark(refer, args...)));

  // test jitcode
  auto jitcode = jit::GetJitCode<KT, KernelTuples, PlaceType>(attr);
  if (jitcode) {
    infos.push_back(std::make_pair("JitCode", benchmark(jitcode, args...)));
  }
  // test all impls in more
  jit::KernelKey kkey(KT, PlaceType());
  auto& pool = jit::KernelPool().Instance().AllKernels();
  auto iter = pool.find(kkey);
  if (iter != pool.end()) {
    auto& impls = iter->second;
    for (auto& impl : impls) {
T
tensor-tang 已提交
90
      auto i = dynamic_cast<const jit::KernelMore<KernelTuples>*>(impl.get());
T
tensor-tang 已提交
91 92
      if (i && i->UseMe(attr)) {
        auto more = i->GetFunc();
T
tensor-tang 已提交
93 94
        infos.push_back(
            std::make_pair(i->ImplType(), benchmark(more, args...)));
T
tensor-tang 已提交
95 96
      }
    }
T
tensor-tang 已提交
97
  }
T
tensor-tang 已提交
98 99 100 101
  // Test result from Get function
  auto tgt = jit::Get<KT, KernelTuples, PlaceType>(attr);
  if (!tgt) {
    LOG(FATAL) << "Target can not be empty!";
T
tensor-tang 已提交
102
  }
T
tensor-tang 已提交
103 104 105 106 107 108 109 110 111
  infos.push_back(std::make_pair("Target", benchmark(tgt, args...)));

  // print
  std::ostringstream loginfos;
  loginfos << "Kernel Type " << jit::to_string(KT) << ": " << attr << ": ";
  for (auto pair : infos) {
    loginfos << pair.first << " takes " << pair.second << " us; ";
  }
  LOG(INFO) << loginfos.str();
T
tensor-tang 已提交
112 113
}

114 115
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchXYZNKernel() {
T
tensor-tang 已提交
116 117 118 119
  for (int d : TestSizes()) {
    std::vector<T> x(d), y(d), z(d);
    RandomVec<T>(d, x.data());
    RandomVec<T>(d, y.data());
T
tensor-tang 已提交
120 121
    BenchAllImpls<KT, jit::XYZNTuples<T>, PlaceType>(d, x.data(), y.data(),
                                                     z.data(), d);
T
tensor-tang 已提交
122 123
  }
}
124

125 126 127 128 129 130
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchAXYNKernel() {
  for (int d : TestSizes()) {
    const T a = static_cast<T>(3);
    std::vector<T> x(d), y(d);
    RandomVec<T>(d, x.data());
T
tensor-tang 已提交
131 132
    BenchAllImpls<KT, jit::AXYNTuples<T>, PlaceType>(d, &a, x.data(), y.data(),
                                                     d);
133 134 135 136 137 138 139 140
  }
}

template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchXYNKernel() {
  for (int d : TestSizes()) {
    std::vector<T> x(d), y(d);
    RandomVec<T>(d, x.data());
T
tensor-tang 已提交
141
    BenchAllImpls<KT, jit::XYNTuples<T>, PlaceType>(d, x.data(), y.data(), d);
142 143 144
  }
}

T
tensor-tang 已提交
145 146 147 148
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchLSTMKernel() {
  for (bool use_peephole : {true, false}) {
    for (int d : TestSizes()) {
T
tensor-tang 已提交
149
      const jit::lstm_attr_t attr(d, jit::kVSigmoid, jit::kVTanh, jit::kVTanh,
T
tensor-tang 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                                  use_peephole);
      std::vector<T> x(4 * d), ct_1(d), ct(d), ht(d), wp(3 * d), checked(2 * d);
      RandomVec<T>(4 * d, x.data(), -2.f, 2.f);
      RandomVec<T>(3 * d, wp.data(), -2.f, 2.f);
      RandomVec<T>(d, ct_1.data(), -2.f, 2.f);
      const T* ct_1_data = ct_1.data();
      const T* wp_data = wp.data();
      T* x_data = x.data();
      T* checked_data = checked.data();
      T* ct_data = ct.data();
      T* ht_data = ht.data();
      jit::lstm_t step;
      step.gates = x_data;
      step.ct_1 = ct_1_data;
      step.ct = ct_data;
      step.ht = ht_data;
      if (use_peephole) {
        step.wp = wp_data;
        step.checked = checked_data;
      }
T
tensor-tang 已提交
170
      BenchAllImpls<KT, jit::LSTMTuples<T>, PlaceType>(attr, &step, &attr);
T
tensor-tang 已提交
171 172 173 174
    }
  }
}

175 176 177
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchGRUKernel() {
  for (int d : TestSizes()) {
T
tensor-tang 已提交
178
    const jit::gru_attr_t attr(d, jit::kVSigmoid, jit::kVTanh);
179 180 181 182 183 184 185 186 187 188
    std::vector<T> x(3 * d), ht_1(d), ht(d);
    RandomVec<T>(3 * d, x.data(), -2.f, 2.f);
    RandomVec<T>(d, ht_1.data(), -2.f, 2.f);
    const T* ht_1_data = ht_1.data();
    T* x_data = x.data();
    T* ht_data = ht.data();
    jit::gru_t step;
    step.gates = x_data;
    step.ht_1 = ht_1_data;
    step.ht = ht_data;
T
tensor-tang 已提交
189
    BenchAllImpls<KT, jit::GRUTuples<T>, PlaceType>(attr, &step, &attr);
190 191 192
  }
}

193 194
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void BenchSeqPoolKernel() {
195 196
  std::vector<jit::SeqPoolType> pool_types = {
      jit::SeqPoolType::kSum, jit::SeqPoolType::kAvg, jit::SeqPoolType::kSqrt};
197
  for (auto type : pool_types) {
T
tensor-tang 已提交
198
    for (int w : TestSizes()) {
T
tensor-tang 已提交
199
      jit::seq_pool_attr_t attr(w, type);
T
tensor-tang 已提交
200
      for (int h : TestSizes()) {
T
tensor-tang 已提交
201
        attr.h = h;
202 203 204 205 206 207 208 209 210 211 212
        std::vector<T> x(h * w), y(w);
        RandomVec<T>(h * w, x.data(), -2.f, 2.f);
        const T* x_data = x.data();
        T* y_data = y.data();
        BenchAllImpls<KT, jit::SeqPoolTuples<T>, PlaceType>(attr, x_data,
                                                            y_data, &attr);
      }
    }
  }
}

213 214 215 216 217 218 219 220 221 222 223 224 225
// Benchmark all jit kernels including jitcode, mkl and refer.
// To use this tool, run command: ./benchmark [options...]
// Options:
//     --burning: the burning time before count
//     --repeat: the repeat times
//     --max_size: the max size would be tested
int main(int argc, char* argv[]) {
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  google::InitGoogleLogging(argv[0]);
  LOG(INFO) << "Burning " << FLAGS_burning << " times, Repeat " << FLAGS_repeat
            << " times.";
  using T = float;
  using PlaceType = paddle::platform::CPUPlace;
T
tensor-tang 已提交
226
  // xyzn
T
tensor-tang 已提交
227 228 229 230
  BenchXYZNKernel<jit::kVMul, T, PlaceType>();
  BenchXYZNKernel<jit::kVAdd, T, PlaceType>();
  BenchXYZNKernel<jit::kVAddRelu, T, PlaceType>();
  BenchXYZNKernel<jit::kVSub, T, PlaceType>();
231

T
tensor-tang 已提交
232
  // axyn
T
tensor-tang 已提交
233 234
  BenchAXYNKernel<jit::kVScal, T, PlaceType>();
  BenchAXYNKernel<jit::kVAddBias, T, PlaceType>();
235

T
tensor-tang 已提交
236
  // xyn
T
tensor-tang 已提交
237 238 239 240 241
  BenchXYNKernel<jit::kVRelu, T, PlaceType>();
  BenchXYNKernel<jit::kVIdentity, T, PlaceType>();
  BenchXYNKernel<jit::kVExp, T, PlaceType>();
  BenchXYNKernel<jit::kVSigmoid, T, PlaceType>();
  BenchXYNKernel<jit::kVTanh, T, PlaceType>();
T
tensor-tang 已提交
242 243

  // lstm and peephole
T
tensor-tang 已提交
244 245
  BenchLSTMKernel<jit::kLSTMCtHt, T, PlaceType>();
  BenchLSTMKernel<jit::kLSTMC1H1, T, PlaceType>();
246 247

  // gru functions
T
tensor-tang 已提交
248 249 250
  BenchGRUKernel<jit::kGRUH1, T, PlaceType>();
  BenchGRUKernel<jit::kGRUHtPart1, T, PlaceType>();
  BenchGRUKernel<jit::kGRUHtPart2, T, PlaceType>();
251 252 253

  // seq pool function
  BenchSeqPoolKernel<jit::kSeqPool, T, PlaceType>();
254
}