Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RequantizeOnlyTest.cc « test - github.com/marian-nmt/FBGEMM.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 94e8e7d5733a6b196497de71f40e853e611c5fa4 (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
/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 * All rights reserved.
 * 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 <functional>
#include <iostream>
#include <random>
#include <stdexcept>
#include <string>

#include <gtest/gtest.h>

#include "TestUtils.h"
#include "bench/BenchUtils.h"
#include "fbgemm/Fbgemm.h"

using namespace std;
using namespace fbgemm;

vector<QuantizationGranularity> qGranularityValsLocal{
    QuantizationGranularity::TENSOR,
    QuantizationGranularity::OUT_CHANNEL};

namespace {

// tuple represents #rows, #cols, fuse_relu, quantization_granularity, bias_type
class FloatRequantizeTest
    : public testing::TestWithParam<
          tuple<int, int, bool, QuantizationGranularity>> {};

}; // namespace

INSTANTIATE_TEST_CASE_P(
    InstantiationName,
    FloatRequantizeTest,
    ::testing::Combine(
        ::testing::ValuesIn({1, 2, 3, 4}), // number of rows
        ::testing::ValuesIn(
            {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 20, 32}), // number of
                                                                  // cols
        ::testing::Bool(), // fuse relu
        ::testing::ValuesIn(qGranularityValsLocal))); // requantization granularity

/**
 * Test for float bias
 */
TEST_P(FloatRequantizeTest, floatBiasTest) {
  int rows, cols;
  bool fuse_relu;
  QuantizationGranularity q_gran;
  tie(rows, cols, fuse_relu, q_gran) = GetParam();

  int numElements = rows * cols;

  aligned_vector<float> act_times_w_scale(cols);
  randFill<float>(act_times_w_scale, -8, 8);

  float out_scale = 2.0f;

  aligned_vector<float> C_multiplier(cols);
  transform(
      act_times_w_scale.begin(),
      act_times_w_scale.end(),
      C_multiplier.begin(),
      [&out_scale](float i) { return i / out_scale; });

  aligned_vector<int32_t> Bint8_zero_point(cols);
  randFill<int32_t>(Bint8_zero_point, -8, 8);

  aligned_vector<int32_t> row_offset_buf(rows);
  randFill<int32_t>(row_offset_buf, -8, 8);

  aligned_vector<int32_t> col_offsets(cols);
  randFill<int32_t>(col_offsets, -8, 8);

  // quantized bias
  aligned_vector<int32_t> bias_q(cols);
  randFill<int32_t>(bias_q, -8, 8);

  // floating point bias
  aligned_vector<float> bias_f(cols);
  if (q_gran == QuantizationGranularity::TENSOR) {
    transform(
        bias_q.begin(),
        bias_q.end(),
        bias_f.begin(),
        [&act_times_w_scale](float i) { return i * act_times_w_scale[0]; });
  } else if (q_gran == QuantizationGranularity::OUT_CHANNEL) {
    transform(
        act_times_w_scale.begin(),
        act_times_w_scale.end(),
        bias_q.begin(),
        bias_f.begin(),
        multiplies<float>());

  } else {
    FAIL();
  }

  aligned_vector<int32_t> input(numElements);
  randFill<int32_t>(input, -8, 8);

  aligned_vector<uint8_t> output_q_bias(numElements);
  aligned_vector<uint8_t> output_f_bias(numElements);

  int32_t C_zero_point = 3;
  int32_t Aint8_zero_point = 3;

  block_type_t block{0, rows, 0, cols};

  DoNothing<> doNothingObj{};

#define TESTCODE(FUSE_RELU, Q_GRAN)                           \
  ReQuantizeOutput<FUSE_RELU, Q_GRAN> reqObj_q(               \
      doNothingObj,                                           \
      C_multiplier.data(),                                    \
      C_zero_point,                                           \
      Aint8_zero_point,                                       \
      Bint8_zero_point.data(),                                \
      row_offset_buf.data(),                                  \
      col_offsets.data(),                                     \
      bias_q.data(),                                          \
      cols);                                                  \
  ReQuantizeOutput<FUSE_RELU, Q_GRAN, float> reqObj_f(        \
      doNothingObj,                                           \
      C_multiplier.data(),                                    \
      C_zero_point,                                           \
      Aint8_zero_point,                                       \
      Bint8_zero_point.data(),                                \
      row_offset_buf.data(),                                  \
      col_offsets.data(),                                     \
      bias_f.data(),                                          \
      cols,                                                   \
      1,                                                      \
      act_times_w_scale.data());                              \
  reqObj_q.f<inst_set_t::avx2>(                               \
      output_q_bias.data(), input.data(), block, cols, cols); \
  reqObj_f.f<inst_set_t::avx2>(                               \
      output_f_bias.data(), input.data(), block, cols, cols);

  if (fuse_relu) {
    if (q_gran == QuantizationGranularity::TENSOR) {
      TESTCODE(true, QuantizationGranularity::TENSOR)

    } else if (q_gran == QuantizationGranularity::OUT_CHANNEL) {
      TESTCODE(true, QuantizationGranularity::OUT_CHANNEL)

    } else {
      FAIL();
    }

  } else {
    if (q_gran == QuantizationGranularity::TENSOR) {
      TESTCODE(false, QuantizationGranularity::TENSOR)

    } else if (q_gran == QuantizationGranularity::OUT_CHANNEL) {
      TESTCODE(false, QuantizationGranularity::OUT_CHANNEL)

    } else {
      FAIL();
    }
  }
#undef TESTCODE
  ASSERT_EQ(output_q_bias, output_f_bias)
      << "Requantization with quantized bias and float bias differs";
}