aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/kernels/internal/log_quantized_test.cc
blob: 8963abb9afd9d51473fe5a22d8e88d314b385ad9 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* Copyright 2018 The TensorFlow 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 <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <limits>
#include <random>
#include <sstream>
#include <string>
#include <vector>

#define GEMMLOWP_ENABLE_FIXEDPOINT_CONSTANTS_CHECKS

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/contrib/lite/kernels/internal/optimized/optimized_ops.h"
#include "tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h"
#include "tensorflow/contrib/lite/string.h"

namespace tflite {

class NumberGenerator {
 public:
  std::vector<int> RandomIntVector(int n, int min_val, int max_val) {
    std::vector<int> vec(n);
    double scale = static_cast<double>(max_val + 1 - min_val) / engine_.max();
    for (auto& it : vec) {
      it = min_val + std::floor(engine_() * scale);
    }
    return vec;
  }

  std::mt19937 engine_;
};

class LogQuantizedTest : public ::testing::Test {
 public:
  NumberGenerator generator_;
};

// input_integer_bits <= 30.  output_integer_bits > 0.
inline int32 LogPositiveValuesViaFloat(int32 input_val, int input_integer_bits,
                                       int output_integer_bits) {
  const double float_log_sum_of_exps = std::log(
      static_cast<double>(input_val) * 0.5 / (1 << (30 - input_integer_bits)));
  static constexpr double min_int =
      static_cast<double>(std::numeric_limits<int32>::min());
  static constexpr double max_int =
      static_cast<double>(std::numeric_limits<int32>::max());
  double double_result = tflite::TfLiteRound(float_log_sum_of_exps *
                                             (1 << (31 - output_integer_bits)));
  return static_cast<std::int32_t>(
      std::min(max_int, std::max(min_int, double_result)));
}

void CheckOutputData(const std::vector<int32>& test_output,
                     const std::vector<int32>& reference_output,
                     const std::vector<int32>& test_input,
                     const string& check_label, int input_integer_bits,
                     int output_integer_bits, int tolerance) {
  // In the special case of small input, specifically raw value of 5, a rounding
  // up leads to difference in the output.  We do not aim to be accurate for
  // very small input values, and there should be sufficient input fractional
  // bits that this is a small input.
  static constexpr double error_from_rounding_up = 0.0224585;
  const int n = test_output.size();
  ASSERT_EQ(n, reference_output.size());
  for (int i = 0; i < n; ++i) {
    // Adjust tolerance when input <= 5*2^-(31-input_integer_bits).
    const int adjusted_tolerance =
        test_input[i] > 5
            ? tolerance
            : std::max(tolerance, static_cast<int>(std::ceil(
                                      error_from_rounding_up *
                                      (1 << (31 - output_integer_bits)))));
    ASSERT_LE(std::abs(test_output[i] - reference_output[i]),
              adjusted_tolerance)
        << "Failure in \"" << check_label << "\" at i=" << i
        << ", test_input[i]=" << test_input[i] << "="
        << static_cast<double>(test_input[i]) / (1 << (31 - input_integer_bits))
        << ", test_output[i]=" << test_output[i] << "="
        << static_cast<double>(test_output[i]) /
               (1 << (31 - output_integer_bits))
        << ", reference_output[i]=" << reference_output[i] << "="
        << static_cast<double>(reference_output[i]) /
               (1 << (31 - output_integer_bits))
        << ", difference[i]=" << std::abs(reference_output[i] - test_output[i])
        << "="
        << static_cast<double>(std::abs(reference_output[i] - test_output[i])) /
               (1 << (31 - output_integer_bits))
        << "; tolerance=" << tolerance
        << ", adj tolerance=" << adjusted_tolerance;
  }
}

void RightShiftVector(const std::vector<int32>& shifts,
                      std::vector<int32>* vec) {
  const int n = vec->size();
  ASSERT_EQ(n, shifts.size());
  for (int i = 0; i < n; ++i) {
    vec->at(i) = std::max(1, vec->at(i) >> shifts[i]);
  }
}

template <int OutputIntegerBits, int InputIntegerBits>
void RunSingleTest(const std::vector<int32>& test_input,
                   const string& check_label, int tolerance) {
  const int n = test_input.size();
  std::vector<int32> float_gen_output(n, 0);
  std::vector<int32> reference_output(n, 0);
  std::vector<int32> optimized_output(n, 0);

  // Workaround the stupid things that intelligent humans do.
  // Consequence of __builtin_clz(0u) may equal 31 instead of 32.
  std::vector<int32> fudged_input(n, 0);
  for (int i = 0; i < n; ++i) {
    fudged_input[i] = std::max(test_input[i], 2);
  }

  for (int i = 0; i < n; ++i) {
    reference_output[i] =
        tflite::reference_ops::log_x_for_x_greater_than_or_equal_to_1_impl<
            OutputIntegerBits, InputIntegerBits>(
            gemmlowp::FixedPoint<int32, InputIntegerBits>::FromRaw(
                fudged_input[i]))
            .raw();
    optimized_output[i] =
        tflite::optimized_ops::log_x_for_x_greater_than_or_equal_to_1_impl<
            OutputIntegerBits, InputIntegerBits>(
            gemmlowp::FixedPoint<int32, InputIntegerBits>::FromRaw(
                fudged_input[i]))
            .raw();
    float_gen_output[i] = LogPositiveValuesViaFloat(
        fudged_input[i], InputIntegerBits, OutputIntegerBits);
  }
  // Note that first check is intolerant.
  {
    std::ostringstream label;
    label << check_label << " / optimized vs reference / InputIntegerBits="
          << InputIntegerBits << ", OutputIntegerBits=" << OutputIntegerBits;
    CheckOutputData(
        optimized_output, reference_output, test_input, label.str(),
        InputIntegerBits, OutputIntegerBits, 0);
  }
  {
    std::ostringstream label;
    label << check_label << " / reference vs float-gen / InputIntegerBits="
          << InputIntegerBits << ", OutputIntegerBits=" << OutputIntegerBits;
    CheckOutputData(
        reference_output, float_gen_output, test_input, label.str(),
        InputIntegerBits, OutputIntegerBits, tolerance);
  }
  {
    std::ostringstream label;
    label << check_label << " optimized vs float-gen / InputIntegerBits="
          << InputIntegerBits << ", OutputIntegerBits=" << OutputIntegerBits;
    CheckOutputData(
        optimized_output, float_gen_output, test_input, label.str(),
        InputIntegerBits, OutputIntegerBits, tolerance);
  }
}

template <int OutputIntegerBits>
void RunSingleTest(const std::vector<int32>& test_input, int input_integer_bits,
                   const string& check_label, int tolerance) {
#define INPUT_CASE(K)                                                   \
  case K:                                                               \
    return RunSingleTest<OutputIntegerBits, K>(test_input, check_label, \
                                               tolerance)
  switch (input_integer_bits) {
    INPUT_CASE(0);
    INPUT_CASE(1);
    INPUT_CASE(2);
    INPUT_CASE(3);
    INPUT_CASE(4);
    INPUT_CASE(5);
    INPUT_CASE(6);
    INPUT_CASE(7);
    INPUT_CASE(8);
    INPUT_CASE(9);
    INPUT_CASE(10);
    INPUT_CASE(11);
    INPUT_CASE(12);
    INPUT_CASE(13);
    INPUT_CASE(14);
    INPUT_CASE(15);
    INPUT_CASE(16);
    INPUT_CASE(17);
    INPUT_CASE(18);
    INPUT_CASE(19);
    INPUT_CASE(20);
    INPUT_CASE(21);
    INPUT_CASE(22);
    INPUT_CASE(23);
    INPUT_CASE(24);
    INPUT_CASE(25);
    INPUT_CASE(26);
    INPUT_CASE(27);
    INPUT_CASE(28);
    INPUT_CASE(29);
    default:
      ASSERT_LE(input_integer_bits, 30)
                << "Input integer bits not handled: " << input_integer_bits;
  }
#undef INPUT_CASE
}

void RunSingleTest(const std::vector<int32>& test_input, int input_integer_bits,
                   int output_integer_bits, const string& check_label,
                   int tolerance) {
#define OUTPUT_CASE(K)                                                   \
  case K:                                                                \
    return RunSingleTest<K>(test_input, input_integer_bits, check_label, \
                            tolerance)
  switch (output_integer_bits) {
    OUTPUT_CASE(0);
    OUTPUT_CASE(1);
    OUTPUT_CASE(2);
    OUTPUT_CASE(3);
    OUTPUT_CASE(4);
    OUTPUT_CASE(5);
    OUTPUT_CASE(6);
    OUTPUT_CASE(7);
    OUTPUT_CASE(8);
    OUTPUT_CASE(9);
    OUTPUT_CASE(10);
    OUTPUT_CASE(11);
    OUTPUT_CASE(12);
    OUTPUT_CASE(13);
    OUTPUT_CASE(14);
    OUTPUT_CASE(15);
    OUTPUT_CASE(16);
    OUTPUT_CASE(17);
    OUTPUT_CASE(18);
    OUTPUT_CASE(19);
    OUTPUT_CASE(20);
    OUTPUT_CASE(21);
    OUTPUT_CASE(22);
    OUTPUT_CASE(23);
    OUTPUT_CASE(24);
    OUTPUT_CASE(25);
    OUTPUT_CASE(26);
    OUTPUT_CASE(27);
    OUTPUT_CASE(28);
    OUTPUT_CASE(29);
    default:
      ASSERT_LE(input_integer_bits, 30)
                << "Input integer bits not handled: " << input_integer_bits;
  }
#undef OUTPUT_CASE
}

void RunUniformTest(int test_size, int input_integer_bits,
                    int output_integer_bits, const string& check_label,
                    int tolerance, NumberGenerator* generator) {
  std::vector<int> test_data = generator->RandomIntVector(
      test_size, 2, std::numeric_limits<int>::max() - 1);
  test_data[0] = 2;
  test_data[1] = 3;
  test_data[2] = 4;
  test_data[3] = std::numeric_limits<int32>::max() - 2;
  test_data[4] = std::numeric_limits<int32>::max() - 1;
  test_data[5] = std::numeric_limits<int32>::max();

  RunSingleTest(test_data, input_integer_bits, output_integer_bits,
                check_label + " / uniform test", tolerance);
}

void RunUniformShiftUniformTest(int test_size, int input_integer_bits,
                                int output_integer_bits,
                                const string& check_label, int tolerance,
                                NumberGenerator* generator) {
  std::vector<int> test_data = generator->RandomIntVector(
      test_size, 2, std::numeric_limits<int>::max() - 1);
  std::vector<int> shifts = generator->RandomIntVector(test_size, 0, 29);
  RightShiftVector(shifts, &test_data);

  RunSingleTest(test_data, input_integer_bits, output_integer_bits,
                check_label + " / shifted test", tolerance);
}

TEST_F(LogQuantizedTest, VariedIntegerBits) {
  static constexpr int kVariations = 250;
  static constexpr int kRunSize = 250;
  static constexpr int kIntegerTolerance = 8;
  static constexpr double kOutputFloatTolerance = 7.0e-7;

  std::vector<int> input_integer_bits =
      generator_.RandomIntVector(kVariations, 0, 24);
  std::vector<int> output_integer_bits =
      generator_.RandomIntVector(kVariations, 1, 10);

  for (int i = 0; i < kVariations; ++i) {
    int var_output_integer_bits = output_integer_bits[i];
    int tolerance =
        std::max(1.0 * kIntegerTolerance,
                 (1 << (31 - var_output_integer_bits)) * kOutputFloatTolerance);

    RunUniformTest(kRunSize, input_integer_bits[i], var_output_integer_bits,
                   "VariedIntegerBits", tolerance, &generator_);
    RunUniformShiftUniformTest(kRunSize, input_integer_bits[i],
                               var_output_integer_bits, "VariedIntegerBits",
                               tolerance, &generator_);
  }
}

TEST_F(LogQuantizedTest, SelectedIntegerBits) {
  static constexpr int kInputBits = 12;
  static constexpr int kOutputBits = 5;
  static constexpr int kRunSize = 100000;
  static constexpr int kIntegerTolerance = 4;

  RunUniformTest(kRunSize, kInputBits, kOutputBits, "SelectedIntegerBits",
                 kIntegerTolerance, &generator_);
  RunUniformShiftUniformTest(kRunSize, kInputBits, kOutputBits,
                             "SelectedIntegerBits", kIntegerTolerance,
                             &generator_);
}

}  // namespace tflite