activation_benchmark.cc 10.6 KB
Newer Older
L
Liangliang He 已提交
1
// Copyright 2018 Xiaomi, Inc.  All rights reserved.
L
Liangliang He 已提交
2
//
L
Liangliang He 已提交
3 4 5
// 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
L
Liangliang He 已提交
6
//
L
Liangliang He 已提交
7 8 9 10 11 12 13
//     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.
L
Liangliang He 已提交
14 15

#include <string>
L
liutuo 已提交
16

L
Liangliang He 已提交
17 18 19 20 21
#include "mace/core/operator.h"
#include "mace/core/testing/test_benchmark.h"
#include "mace/ops/ops_test_util.h"

namespace mace {
L
liutuo 已提交
22 23 24
namespace ops {
namespace test {

25
namespace {
L
Liangliang He 已提交
26
template <DeviceType D, typename T>
27
void ReluBenchmark(
L
Liangliang He 已提交
28 29 30 31 32 33 34 35 36
    int iters, int batch, int channels, int height, int width) {
  mace::testing::StopTiming();

  OpsTestNet net;

  // Add input data
  net.AddRandomInput<D, float>("Input", {batch, height, width, channels});

  if (D == DeviceType::OPENCL) {
Y
yejianwu 已提交
37
    BufferToImage<D, float>(&net, "Input", "InputImage",
38
                            kernels::BufferType::IN_OUT_CHANNEL);
L
Liangliang He 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    OpDefBuilder("Activation", "ReluBM")
        .Input("InputImage")
        .Output("Output")
        .AddStringArg("activation", "RELU")
        .Finalize(net.NewOperatorDef());
  } else {
    OpDefBuilder("Activation", "ReluBM")
        .Input("Input")
        .Output("Output")
        .AddStringArg("activation", "RELU")
        .Finalize(net.NewOperatorDef());
  }

  // Warm-up
  for (int i = 0; i < 5; ++i) {
    net.RunOp(D);
  }
  net.Sync();

  mace::testing::StartTiming();
  while (iters--) {
    net.RunOp(D);
  }
  net.Sync();
}
65
}  // namespace
L
Liangliang He 已提交
66 67 68 69

#define BM_RELU_MACRO(N, C, H, W, TYPE, DEVICE)                              \
  static void BM_RELU_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE(int iters) { \
    const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W;         \
L
Liangliang He 已提交
70
    mace::testing::MaccProcessed(tot);                                       \
L
Liangliang He 已提交
71 72 73 74 75
    mace::testing::BytesProcessed(tot *(sizeof(TYPE)));                      \
    ReluBenchmark<DEVICE, TYPE>(iters, N, C, H, W);                          \
  }                                                                          \
  BENCHMARK(BM_RELU_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE)

L
Liangliang He 已提交
76 77 78 79
#define BM_RELU(N, C, H, W)                 \
  BM_RELU_MACRO(N, C, H, W, float, CPU);    \
  BM_RELU_MACRO(N, C, H, W, float, OPENCL); \
  BM_RELU_MACRO(N, C, H, W, half, OPENCL);
L
Liangliang He 已提交
80

L
Liangliang He 已提交
81 82 83 84 85
BM_RELU(1, 1, 512, 512);
BM_RELU(1, 3, 128, 128);
BM_RELU(1, 3, 512, 512);
BM_RELU(1, 32, 112, 112);
BM_RELU(1, 64, 256, 256);
L
Liangliang He 已提交
86

87
namespace {
L
Liangliang He 已提交
88
template <DeviceType D, typename T>
89
void ReluxBenchmark(
L
Liangliang He 已提交
90 91 92 93 94 95 96 97 98
    int iters, int batch, int channels, int height, int width) {
  mace::testing::StopTiming();

  OpsTestNet net;

  // Add input data
  net.AddRandomInput<D, float>("Input", {batch, height, width, channels});

  if (D == DeviceType::OPENCL) {
Y
yejianwu 已提交
99
    BufferToImage<D, float>(&net, "Input", "InputImage",
100
                            kernels::BufferType::IN_OUT_CHANNEL);
L
Liangliang He 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    OpDefBuilder("Activation", "ReluxBM")
        .Input("InputImage")
        .Output("Output")
        .AddStringArg("activation", "RELUX")
        .AddFloatArg("max_limit", 6.0)
        .Finalize(net.NewOperatorDef());
  } else {
    OpDefBuilder("Activation", "ReluxBM")
        .Input("Input")
        .Output("Output")
        .AddStringArg("activation", "RELUX")
        .AddFloatArg("max_limit", 6.0)
        .Finalize(net.NewOperatorDef());
  }

  // Warm-up
  for (int i = 0; i < 5; ++i) {
    net.RunOp(D);
  }
  net.Sync();

  mace::testing::StartTiming();
  while (iters--) {
    net.RunOp(D);
  }
  net.Sync();
}
129
}  // namespace
L
Liangliang He 已提交
130 131 132 133

#define BM_RELUX_MACRO(N, C, H, W, TYPE, DEVICE)                              \
  static void BM_RELUX_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE(int iters) { \
    const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W;          \
L
Liangliang He 已提交
134
    mace::testing::MaccProcessed(tot);                                        \
L
Liangliang He 已提交
135 136 137 138 139
    mace::testing::BytesProcessed(tot *(sizeof(TYPE)));                       \
    ReluxBenchmark<DEVICE, TYPE>(iters, N, C, H, W);                          \
  }                                                                           \
  BENCHMARK(BM_RELUX_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE)

L
Liangliang He 已提交
140 141 142 143
#define BM_RELUX(N, C, H, W)                 \
  BM_RELUX_MACRO(N, C, H, W, float, CPU);    \
  BM_RELUX_MACRO(N, C, H, W, float, OPENCL); \
  BM_RELUX_MACRO(N, C, H, W, half, OPENCL);
L
Liangliang He 已提交
144

L
Liangliang He 已提交
145 146 147 148 149
BM_RELUX(1, 1, 512, 512);
BM_RELUX(1, 3, 128, 128);
BM_RELUX(1, 3, 512, 512);
BM_RELUX(1, 32, 112, 112);
BM_RELUX(1, 64, 256, 256);
L
Liangliang He 已提交
150

151
namespace {
L
Liangliang He 已提交
152
template <DeviceType D, typename T>
153
void PreluBenchmark(
L
Liangliang He 已提交
154 155 156 157 158 159 160
    int iters, int batch, int channels, int height, int width) {
  mace::testing::StopTiming();

  OpsTestNet net;

  // Add input data
  net.AddRandomInput<D, float>("Input", {batch, height, width, channels});
L
liuqi 已提交
161
  net.AddRandomInput<D, float>("Alpha", {channels});
L
Liangliang He 已提交
162 163

  if (D == DeviceType::OPENCL) {
Y
yejianwu 已提交
164
    BufferToImage<D, float>(&net, "Input", "InputImage",
165
                            kernels::BufferType::IN_OUT_CHANNEL);
Y
yejianwu 已提交
166
    BufferToImage<D, float>(&net, "Alpha", "AlphaImage",
L
liuqi 已提交
167
                            kernels::BufferType::ARGUMENT);
L
Liangliang He 已提交
168 169 170

    OpDefBuilder("Activation", "PreluBM")
        .Input("InputImage")
L
liuqi 已提交
171
        .Input("AlphaImage")
L
Liangliang He 已提交
172 173 174 175 176 177
        .Output("Output")
        .AddStringArg("activation", "PRELU")
        .Finalize(net.NewOperatorDef());
  } else {
    OpDefBuilder("Activation", "PreluBM")
        .Input("Input")
L
liuqi 已提交
178
        .Input("Alpha")
L
Liangliang He 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        .Output("Output")
        .AddStringArg("activation", "PRELU")
        .Finalize(net.NewOperatorDef());
  }

  // Warm-up
  for (int i = 0; i < 5; ++i) {
    net.RunOp(D);
  }
  net.Sync();

  mace::testing::StartTiming();
  while (iters--) {
    net.RunOp(D);
  }
  net.Sync();
}
196
}  // namespace
L
Liangliang He 已提交
197 198 199 200

#define BM_PRELU_MACRO(N, C, H, W, TYPE, DEVICE)                              \
  static void BM_PRELU_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE(int iters) { \
    const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W;          \
L
Liangliang He 已提交
201
    mace::testing::MaccProcessed(tot);                                        \
L
Liangliang He 已提交
202 203 204 205 206
    mace::testing::BytesProcessed(tot *(sizeof(TYPE)));                       \
    PreluBenchmark<DEVICE, TYPE>(iters, N, C, H, W);                          \
  }                                                                           \
  BENCHMARK(BM_PRELU_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE)

L
Liangliang He 已提交
207 208 209 210
#define BM_PRELU(N, C, H, W)                 \
  BM_PRELU_MACRO(N, C, H, W, float, CPU);    \
  BM_PRELU_MACRO(N, C, H, W, float, OPENCL); \
  BM_PRELU_MACRO(N, C, H, W, half, OPENCL);
L
Liangliang He 已提交
211

L
Liangliang He 已提交
212 213 214 215 216
BM_PRELU(1, 1, 512, 512);
BM_PRELU(1, 3, 128, 128);
BM_PRELU(1, 3, 512, 512);
BM_PRELU(1, 32, 112, 112);
BM_PRELU(1, 64, 256, 256);
L
Liangliang He 已提交
217

218
namespace {
L
Liangliang He 已提交
219
template <DeviceType D, typename T>
220
void TanhBenchmark(
L
Liangliang He 已提交
221 222 223 224 225 226 227 228 229
    int iters, int batch, int channels, int height, int width) {
  mace::testing::StopTiming();

  OpsTestNet net;

  // Add input data
  net.AddRandomInput<D, float>("Input", {batch, height, width, channels});

  if (D == DeviceType::OPENCL) {
Y
yejianwu 已提交
230
    BufferToImage<D, float>(&net, "Input", "InputImage",
231
                            kernels::BufferType::IN_OUT_CHANNEL);
L
Liangliang He 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

    OpDefBuilder("Activation", "TanhBM")
        .Input("InputImage")
        .Output("Output")
        .AddStringArg("activation", "TANH")
        .Finalize(net.NewOperatorDef());
  } else {
    OpDefBuilder("Activation", "TanhBM")
        .Input("Input")
        .Output("Output")
        .AddStringArg("activation", "TANH")
        .Finalize(net.NewOperatorDef());
  }

  // Warm-up
  for (int i = 0; i < 5; ++i) {
    net.RunOp(D);
  }
  net.Sync();

  mace::testing::StartTiming();
  while (iters--) {
    net.RunOp(D);
  }
  net.Sync();
}
258
}  // namespace
L
Liangliang He 已提交
259 260 261 262

#define BM_TANH_MACRO(N, C, H, W, TYPE, DEVICE)                              \
  static void BM_TANH_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE(int iters) { \
    const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W;         \
L
Liangliang He 已提交
263
    mace::testing::MaccProcessed(tot);                                       \
L
Liangliang He 已提交
264 265 266 267 268
    mace::testing::BytesProcessed(tot *(sizeof(TYPE)));                      \
    TanhBenchmark<DEVICE, TYPE>(iters, N, C, H, W);                          \
  }                                                                          \
  BENCHMARK(BM_TANH_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE)

L
Liangliang He 已提交
269 270 271 272
#define BM_TANH(N, C, H, W)                 \
  BM_TANH_MACRO(N, C, H, W, float, CPU);    \
  BM_TANH_MACRO(N, C, H, W, float, OPENCL); \
  BM_TANH_MACRO(N, C, H, W, half, OPENCL);
L
Liangliang He 已提交
273

L
Liangliang He 已提交
274 275 276 277 278
BM_TANH(1, 1, 512, 512);
BM_TANH(1, 3, 128, 128);
BM_TANH(1, 3, 512, 512);
BM_TANH(1, 32, 112, 112);
BM_TANH(1, 64, 256, 256);
L
Liangliang He 已提交
279

280
namespace {
L
Liangliang He 已提交
281
template <DeviceType D, typename T>
282
void SigmoidBenchmark(
L
Liangliang He 已提交
283 284 285 286 287 288 289 290 291
    int iters, int batch, int channels, int height, int width) {
  mace::testing::StopTiming();

  OpsTestNet net;

  // Add input data
  net.AddRandomInput<D, float>("Input", {batch, height, width, channels});

  if (D == DeviceType::OPENCL) {
Y
yejianwu 已提交
292
    BufferToImage<D, float>(&net, "Input", "InputImage",
293
                            kernels::BufferType::IN_OUT_CHANNEL);
L
Liangliang He 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

    OpDefBuilder("Activation", "SigmoidBM")
        .Input("InputImage")
        .Output("Output")
        .AddStringArg("activation", "SIGMOID")
        .Finalize(net.NewOperatorDef());
  } else {
    OpDefBuilder("Activation", "SigmoidBM")
        .Input("Input")
        .Output("Output")
        .AddStringArg("activation", "SIGMOID")
        .Finalize(net.NewOperatorDef());
  }

  // Warm-up
  for (int i = 0; i < 5; ++i) {
    net.RunOp(D);
  }
  net.Sync();

  mace::testing::StartTiming();
  while (iters--) {
    net.RunOp(D);
  }
  net.Sync();
}
320
}  // namespace
L
Liangliang He 已提交
321 322 323 324 325

#define BM_SIGMOID_MACRO(N, C, H, W, TYPE, DEVICE)                   \
  static void BM_SIGMOID_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE(  \
      int iters) {                                                   \
    const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W; \
L
Liangliang He 已提交
326
    mace::testing::MaccProcessed(tot);                               \
L
Liangliang He 已提交
327 328 329 330 331
    mace::testing::BytesProcessed(tot *(sizeof(TYPE)));              \
    SigmoidBenchmark<DEVICE, TYPE>(iters, N, C, H, W);               \
  }                                                                  \
  BENCHMARK(BM_SIGMOID_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE)

332 333 334 335 336 337 338 339 340 341
#define BM_SIGMOID(N, C, H, W)                 \
  BM_SIGMOID_MACRO(N, C, H, W, float, CPU);    \
  BM_SIGMOID_MACRO(N, C, H, W, float, OPENCL); \
  BM_SIGMOID_MACRO(N, C, H, W, half, OPENCL);

BM_SIGMOID(1, 1, 512, 512);
BM_SIGMOID(1, 3, 128, 128);
BM_SIGMOID(1, 3, 512, 512);
BM_SIGMOID(1, 32, 112, 112);
BM_SIGMOID(1, 64, 256, 256);
L
Liangliang He 已提交
342

L
liutuo 已提交
343 344
}  // namespace test
}  // namespace ops
L
Liangliang He 已提交
345
}  // namespace mace