blob: c0eb36248117c1cfe48b705b79d6464ad671fd4e [file] [log] [blame]
XNNPACK Teamb455b122019-09-27 18:10:33 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// This source code is licensed under the BSD-style license found in the
5// LICENSE file in the root directory of this source tree.
6
7#include <algorithm>
8#include <cmath>
9#include <functional>
10#include <random>
11#include <vector>
12
13#include <xnnpack.h>
14
15#include <benchmark/benchmark.h>
Frank Barchardbb4c18b2019-09-30 11:05:52 -070016#include "bench/utils.h"
XNNPACK Teamb455b122019-09-27 18:10:33 -070017
18
Marat Dukhanfd8e6892020-01-27 15:25:25 -080019static void softmax_q8(benchmark::State& state) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070020 const size_t batch_size = static_cast<size_t>(state.range(0));
21 const size_t channels = static_cast<size_t>(state.range(1));
22
23 std::random_device random_device;
24 auto rng = std::mt19937(random_device());
25 auto u8rng = std::bind(std::uniform_int_distribution<uint8_t>(), rng);
26
27 std::vector<uint8_t> input(batch_size * channels);
28 std::vector<uint8_t> output(batch_size * channels);
29 std::generate(input.begin(), input.end(), std::ref(u8rng));
30 std::fill(output.begin(), output.end(), 0xA5);
31
Marat Dukhan04f03be2019-11-19 12:36:47 -080032 xnn_status status = xnn_initialize(nullptr /* allocator */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070033 if (status != xnn_status_success) {
34 state.SkipWithError("failed to initialize XNNPACK");
35 return;
36 }
37
Marat Dukhanfd8e6892020-01-27 15:25:25 -080038 xnn_operator_t softmax_op = nullptr;
39 status = xnn_create_softmax_nc_q8(
XNNPACK Teamb455b122019-09-27 18:10:33 -070040 channels, channels /* input stride */, channels /* output stride */,
41 1.0f /* input scale */,
42 0 /* output zero point */, 1.0f / 256.0f /* output scale */,
Marat Dukhanfd8e6892020-01-27 15:25:25 -080043 0 /* flags */, &softmax_op);
44 if (status != xnn_status_success || softmax_op == nullptr) {
45 state.SkipWithError("failed to create SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070046 return;
47 }
48
Marat Dukhanfd8e6892020-01-27 15:25:25 -080049 status = xnn_setup_softmax_nc_q8(
50 softmax_op,
XNNPACK Teamb455b122019-09-27 18:10:33 -070051 batch_size,
52 input.data(), output.data(),
53 nullptr /* thread pool */);
54 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080055 state.SkipWithError("failed to setup SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070056 return;
57 }
58
59 for (auto _ : state) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080060 status = xnn_run_operator(softmax_op, nullptr /* thread pool */);
XNNPACK Teamb455b122019-09-27 18:10:33 -070061 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080062 state.SkipWithError("failed to run SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070063 return;
64 }
65 }
66
Marat Dukhanfd8e6892020-01-27 15:25:25 -080067 status = xnn_delete_operator(softmax_op);
XNNPACK Teamb455b122019-09-27 18:10:33 -070068 if (status != xnn_status_success) {
Marat Dukhanfd8e6892020-01-27 15:25:25 -080069 state.SkipWithError("failed to delete SoftMax operator");
XNNPACK Teamb455b122019-09-27 18:10:33 -070070 return;
71 }
72
Frank Barchardbb4c18b2019-09-30 11:05:52 -070073 state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency();
74
XNNPACK Teamb455b122019-09-27 18:10:33 -070075 const size_t elements_per_iteration = batch_size * channels;
76 state.counters["elements"] =
77 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
78
79 const size_t bytes_per_iteration = 2 * elements_per_iteration * sizeof(uint8_t);
80 state.counters["bytes"] =
81 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
82}
83
84static void CharacteristicArguments(benchmark::internal::Benchmark* b)
85{
86 b->ArgNames({"N", "C"});
87
88 // CIFAR-10
89 b->Args({1, 10});
90 // CIFAR-100 */
91 b->Args({1, 100});
92 // ImageNet-1K
93 b->Args({1, 1000});
94 // ImageNet-1K+1
95 b->Args({1, 1001});
96 // ImageNet-22K
97 b->Args({1, 21841});
98}
99
Marat Dukhanfd8e6892020-01-27 15:25:25 -0800100BENCHMARK(softmax_q8)->Apply(CharacteristicArguments)->UseRealTime();
XNNPACK Teamb455b122019-09-27 18:10:33 -0700101
102#ifndef XNNPACK_BENCHMARK_NO_MAIN
103BENCHMARK_MAIN();
104#endif