File size: 9,298 Bytes
8b7c501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2023 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 <gtest/gtest.h>

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <limits>
#include <utility>
#include <vector>

#include <fp16/fp16.h>

#include "math-evaluation-tester.h"

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


void MathEvaluationTester::TestOutputMatchReference(xnn_f16_unary_math_fn math_fn, float output_value) const {
  ASSERT_FALSE(std::isnan(output_value));
  ASSERT_FALSE(std::isnan(input_min()));
  ASSERT_FALSE(std::isnan(input_max()));
  ASSERT_LE(input_min(), input_max());
  ASSERT_EQ(std::signbit(input_min()), std::signbit(input_max()));
  ASSERT_EQ(fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(output_value)), output_value);
  ASSERT_EQ(fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(input_min())), input_min());
  ASSERT_EQ(fp16_ieee_to_fp32_value(fp16_ieee_from_fp32_value(input_max())), input_max());

  uint16_t range_min = fp16_ieee_from_fp32_value(std::fabs(input_min()));
  uint16_t range_max = fp16_ieee_from_fp32_value(std::fabs(input_max()));
  uint16_t range_sign = std::signbit(input_min()) ? UINT16_C(0x8000) : 0;
  if (range_min > range_max) {
    std::swap(range_min, range_max);
  }

  std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> inputs(kBlockSize);
  std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> outputs(kBlockSize);
  for (uint16_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint16_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] = range_sign | std::min<uint16_t>(block_start + block_offset, range_max);
    }
    math_fn(kBlockSize * sizeof(uint16_t), inputs.data(), outputs.data());
    for (uint16_t i = 0; i < kBlockSize; i++) {
      const uint16_t reference_output = fp16_ieee_from_fp32_value(output_value);
      ASSERT_EQ(reference_output, outputs[i])
        << "input = 0x" << std::hex << std::setw(4) << std::setfill('0') << inputs[i]
        << " (" << fp16_ieee_to_fp32_value(inputs[i]) << ")"
        << ", reference = 0x" << std::hex << std::setw(4) << std::setfill('0') << reference_output
        << " (" << output_value << ")"
        << ", optimized = 0x" << std::hex << std::setw(4) << std::setfill('0') << outputs[i]
        << " (" << fp16_ieee_to_fp32_value(outputs[i]) << ")";
    }
  }
}

void MathEvaluationTester::TestOutputMatchReference(xnn_f32_unary_math_fn math_fn, float output_value) const {
  ASSERT_FALSE(std::isnan(output_value));
  ASSERT_FALSE(std::isnan(input_min()));
  ASSERT_FALSE(std::isnan(input_max()));
  ASSERT_LE(input_min(), input_max());
  ASSERT_EQ(std::signbit(input_min()), std::signbit(input_max()));

  uint32_t range_min = float_as_uint32(std::fabs(input_min()));
  uint32_t range_max = float_as_uint32(std::fabs(input_max()));
  uint32_t range_sign = std::signbit(input_min()) ? UINT32_C(0x80000000) : 0;
  if (range_min > range_max) {
    std::swap(range_min, range_max);
  }

  std::vector<float, AlignedAllocator<float, 64>> inputs(kBlockSize);
  std::vector<float, AlignedAllocator<float, 64>> outputs(kBlockSize);
  for (uint32_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint32_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] = uint32_as_float(range_sign | std::min<uint32_t>(block_start + block_offset, range_max));
    }
    math_fn(kBlockSize * sizeof(float), inputs.data(), outputs.data());
    for (uint32_t i = 0; i < kBlockSize; i++) {
      const uint32_t reference_output = float_as_uint32(output_value);
      ASSERT_EQ(reference_output, float_as_uint32(outputs[i]))
        << "input = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(inputs[i])
        << " (" << inputs[i] << ")"
        << ", reference = 0x" << std::hex << std::setw(8) << std::setfill('0') << reference_output
        << " (" << output_value << ")"
        << ", optimized = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(outputs[i]);
    }
  }
}

void MathEvaluationTester::TestOutputMatchZero(xnn_f32_unary_math_fn math_fn) const {
  ASSERT_FALSE(std::isnan(input_min()));
  ASSERT_FALSE(std::isnan(input_max()));
  ASSERT_LE(input_min(), input_max());
  ASSERT_EQ(std::signbit(input_min()), std::signbit(input_max()));

  uint32_t range_min = float_as_uint32(std::fabs(input_min()));
  uint32_t range_max = float_as_uint32(std::fabs(input_max()));
  uint32_t range_sign = std::signbit(input_min()) ? UINT32_C(0x80000000) : 0;
  if (range_min > range_max) {
    std::swap(range_min, range_max);
  }

  std::vector<float, AlignedAllocator<float, 64>> inputs(kBlockSize);
  std::vector<float, AlignedAllocator<float, 64>> outputs(kBlockSize);
  for (uint32_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint32_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] = uint32_as_float(range_sign | std::min<uint32_t>(block_start + block_offset, range_max));
    }
    math_fn(kBlockSize * sizeof(float), inputs.data(), outputs.data());
    for (uint32_t i = 0; i < kBlockSize; i++) {
      ASSERT_EQ(0.0f, outputs[i])
        << "input = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(inputs[i])
        << " (" << inputs[i] << ")"
        << ", optimized = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(outputs[i]);
    }
  }
}

void MathEvaluationTester::TestNaN(xnn_f16_unary_math_fn math_fn) const {
  ASSERT_TRUE(std::isnan(input_min()));
  ASSERT_TRUE(std::isnan(input_max()));

  const uint16_t range_min = UINT16_C(0x7C01);
  const uint16_t range_max = UINT16_C(0x7FFF);

  std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> inputs(kBlockSize);
  std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> outputs(kBlockSize);

  // Positive NaN inputs
  for (uint16_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint16_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] = std::min<uint16_t>(block_start + block_offset, range_max);
    }
    math_fn(kBlockSize * sizeof(uint16_t), inputs.data(), outputs.data());
    for (uint16_t i = 0; i < kBlockSize; i++) {
      ASSERT_TRUE(std::isnan(fp16_ieee_to_fp32_value(outputs[i])))
        << "input = 0x" << std::hex << std::setw(4) << std::setfill('0') << inputs[i]
        << ", output = 0x" << std::hex << std::setw(4) << std::setfill('0') << outputs[i];
    }
  }

  // Negative NaN inputs
  for (uint16_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint16_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] =
        UINT16_C(0x8000) | std::min<uint16_t>(block_start + block_offset, range_max);
    }
    math_fn(kBlockSize * sizeof(uint16_t), inputs.data(), outputs.data());
    for (uint16_t i = 0; i < kBlockSize; i++) {
      ASSERT_TRUE(std::isnan(fp16_ieee_to_fp32_value(outputs[i])))
        << "input = 0x" << std::hex << std::setw(4) << std::setfill('0') << inputs[i]
        << ", output = 0x" << std::hex << std::setw(4) << std::setfill('0') << outputs[i];
    }
  }
}

void MathEvaluationTester::TestNaN(xnn_f32_unary_math_fn math_fn) const {
  ASSERT_TRUE(std::isnan(input_min()));
  ASSERT_TRUE(std::isnan(input_max()));

  const uint32_t range_min = UINT32_C(0x7F800001);
  const uint32_t range_max = UINT32_C(0x7FFFFFFF);

  std::vector<float, AlignedAllocator<float, 64>> inputs(kBlockSize);
  std::vector<float, AlignedAllocator<float, 64>> outputs(kBlockSize);

  // Positive NaN inputs
  for (uint32_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint32_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] = uint32_as_float(std::min<uint32_t>(block_start + block_offset, range_max));
    }
    math_fn(kBlockSize * sizeof(float), inputs.data(), outputs.data());
    for (uint32_t i = 0; i < kBlockSize; i++) {
      ASSERT_TRUE(std::isnan(outputs[i]))
        << "input = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(inputs[i])
        << ", output = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(outputs[i]);
    }
  }

  // Negative NaN inputs
  for (uint32_t block_start = range_min; block_start <= range_max; block_start += kBlockSize) {
    for (uint32_t block_offset = 0; block_offset < kBlockSize; block_offset++) {
      inputs[block_offset] =
        uint32_as_float(UINT32_C(0x80000000) | std::min<uint32_t>(block_start + block_offset, range_max));
    }
    math_fn(kBlockSize * sizeof(float), inputs.data(), outputs.data());
    for (uint32_t i = 0; i < kBlockSize; i++) {
      ASSERT_TRUE(std::isnan(outputs[i]))
        << "input = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(inputs[i])
        << ", output = 0x" << std::hex << std::setw(8) << std::setfill('0') << float_as_uint32(outputs[i]);
    }
  }
}