File: f32-exp.cc

package info (click to toggle)
xnnpack 0.0~git20201031.beca652%2Breally.git20200323.1b35463-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,260 kB
  • sloc: cpp: 287,277; ansic: 197,749; asm: 25,609; python: 7,794; sh: 1,286; makefile: 14
file content (123 lines) | stat: -rw-r--r-- 4,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <functional>
#include <random>
#include <vector>

#include <benchmark/benchmark.h>
#include <fp16/fp16.h>

#include <xnnpack/AlignedAllocator.h>
#include <xnnpack/common.h>
#include <xnnpack/math-stubs.h>


static void ExpError(benchmark::State& state,
  xnn_f32_unary_math_function exp,
  size_t tile_size)
{
  // The smallest x for which expf(x) is non-zero (-0x1.9FE368p+6f).
  const uint32_t min_input = 0xC2CFF1B4;
  // The largest x for which expf(x) is finite (0x1.62E42Ep6f).
  const uint32_t max_input = 0x42B17217;
  // Number of tiles in one block of inputs/outputs. Combining multiple tiles in a block reduce function call overhead.
  const size_t num_tiles = 100;

  double max_ulp_error = 0.0;
  std::vector<float, AlignedAllocator<float, 64>> x(tile_size * num_tiles);
  std::vector<float, AlignedAllocator<float, 64>> y(tile_size * num_tiles);
  for (auto _ : state) {
    for (uint32_t n = min_input; int32_t(n) < 0; n -= tile_size * num_tiles) {
      for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
        x[i] = fp32_from_bits(std::max<uint32_t>(n - i, 0x80000000));
      }
      std::fill(y.begin(), y.end(), std::nanf(""));

      exp(tile_size * num_tiles * sizeof(float), x.data(), y.data());

      for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
        const double y_ref = std::exp(double(x[i]));
        const double abs_error = std::abs(y_ref - double(y[i]));
        const float y_abs = std::abs(y_ref);
        const float y_ulp = fp32_from_bits(fp32_to_bits(y_abs) + 1) - y_abs;
        max_ulp_error = std::max<double>(max_ulp_error, abs_error / y_ulp);
      }
    }
    for (uint32_t n = 0; n < max_input; n += tile_size * num_tiles) {
      for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
        x[i] = fp32_from_bits(std::min<uint32_t>(n + i, max_input));
      }
      std::fill(y.begin(), y.end(), std::nanf(""));

      exp(tile_size * num_tiles * sizeof(float), x.data(), y.data());

      for (uint32_t i = 0; i < tile_size * num_tiles; i++) {
        const double y_ref = std::exp(double(x[i]));
        const double abs_error = std::abs(y_ref - double(y[i]));
        const float y_abs = std::abs(y_ref);
        const float y_ulp = fp32_from_bits(fp32_to_bits(y_abs) + 1) - y_abs;
        max_ulp_error = std::max<double>(max_ulp_error, abs_error / y_ulp);
      }
    }
  }

  state.counters["ULPERROR"] = benchmark::Counter(max_ulp_error);
}

#if XNN_ARCH_X86 || XNN_ARCH_X86_64
  static void f32_exp__sse2_p5(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__sse2_p5, 4);
  }
  static void f32_exp__avx2_perm_p3(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx2_perm_p3, 8);
  }
  static void f32_exp__avx2_perm_p4(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx2_perm_p4, 8);
  }
  static void f32_exp__avx2_p5(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx2_p5, 8);
  }
  static void f32_exp__avx512f_perm2_p2(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx512f_perm2_p2, 16);
  }
  static void f32_exp__avx512f_perm_p3(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx512f_perm_p3, 16);
  }
  static void f32_exp__avx512f_p5_scalef(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx512f_p5_scalef, 16);
  }
  static void f32_exp__avx512f_p5(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__avx512f_p5, 16);
  }

  BENCHMARK(f32_exp__sse2_p5)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx2_perm_p4)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx2_perm_p3)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx2_p5)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx512f_perm2_p2)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx512f_perm_p3)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx512f_p5_scalef)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__avx512f_p5)->Unit(benchmark::kMillisecond)->Iterations(1);
#endif  // XNN_ARCH_X86 || XNN_ARCH_X86_64

#if XNN_ARCH_ARM || XNN_ARCH_ARM64
  static void f32_exp__neonfma_lut64_p2(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__neonfma_lut64_p2, 4);
  }
  static void f32_exp__neonfma_p5(benchmark::State& state) {
    ExpError(state, xnn_math_f32_exp__neonfma_p5, 4);
  }

  BENCHMARK(f32_exp__neonfma_lut64_p2)->Unit(benchmark::kMillisecond)->Iterations(1);
  BENCHMARK(f32_exp__neonfma_p5)->Unit(benchmark::kMillisecond)->Iterations(1);
#endif  // XNN_ARCH_ARM || XNN_ARCH_ARM64

#ifndef XNNPACK_BENCHMARK_NO_MAIN
BENCHMARK_MAIN();
#endif