File: correctness_meta_gemm.cc

package info (click to toggle)
gemmlowp 0.0~git20211220.e844ffd-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,752 kB
  • sloc: cpp: 113,898; ansic: 9,221; python: 3,251; sh: 79; objc: 55; makefile: 16
file content (344 lines) | stat: -rw-r--r-- 12,064 bytes parent folder | download | duplicates (12)
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
335
336
337
338
339
340
341
342
343
344
// Copyright 2015 The Gemmlowp 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 <unistd.h>
#ifdef __APPLE__
#include <sys/time.h>
#endif

#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <vector>

#include "../meta/legacy_multi_thread_gemm.h"
#include "../public/gemmlowp.h"
#include "test.h"
// lets include these so we make sure they always compile
#include "../meta/multi_thread_gemm.h"
#include "../meta/multi_thread_transform.h"
#include "../meta/legacy_multi_thread_common.h"

#if defined(__arm__) && !defined(GEMMLOWP_NEON)
#warning "Building without NEON support on ARM, check your compiler setup!"
#endif

double time() {
#ifdef __APPLE__
  timeval t;
  gettimeofday(&t, nullptr);
  return t.tv_sec + 1e-6 * t.tv_usec;
#else
  timespec t;
  clock_gettime(CLOCK_REALTIME, &t);
  return t.tv_sec + 1e-9 * t.tv_nsec;
#endif
}

void prepare_test_data(std::uint8_t* data, std::int32_t rows, std::int32_t cols,
                       std::int32_t seed, std::int32_t seed_2) {
  std::int32_t value = seed;
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      data[i * cols + j] = static_cast<std::uint8_t>(value);
      value = ((value * seed_2) + seed) % 256;
    }
  }
}

void check_result(std::uint8_t* left, std::uint8_t* right, std::uint8_t* result,
                  std::int32_t rows, std::int32_t cols, std::int32_t depth,
                  std::int32_t lhs_offset, std::int32_t rhs_offset,
                  std::int32_t sum_offset, std::int32_t mul_offset,
                  std::int32_t shift) {
  std::int32_t rounding = (1 << (shift - 1));
  std::int32_t wrong = 0;
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      std::int32_t expected = 0;
      for (int k = 0; k < depth; ++k) {
        expected +=
            (static_cast<std::int32_t>(left[depth * i + k]) + lhs_offset) *
            (static_cast<std::int32_t>(right[depth * j + k]) + rhs_offset);
      }
      expected += sum_offset;
      expected *= mul_offset;
      expected += rounding;
      expected = (expected >> shift);
      if (expected < 0) {
        expected = 0;
      } else if (expected > 255) {
        expected = 255;
      }
      expected = static_cast<std::int32_t>(static_cast<std::uint8_t>(expected));
      std::int32_t actual = static_cast<std::int32_t>(result[i * cols + j]);
      if (actual != expected) {
        std::cout << "(" << i << ", " << j << "): " << expected << "!="
                  << actual << std::endl;
        wrong++;
      }
    }
  }
  if (wrong > 0) {
    std::cout << "Wrong: " << rows << "x" << cols << "x" << depth << " : "
              << wrong << "/" << (rows * cols) << std::endl
              << std::flush;
    std::exit(1);
  } else {
    std::cout << "." << std::flush;
  }
}

void check_result_f(std::uint8_t* left, std::uint8_t* right, float* result,
                    std::int32_t rows, std::int32_t cols, std::int32_t depth,
                    std::int32_t lhs_offset, std::int32_t rhs_offset,
                    float result_offset) {
  std::int32_t wrong = 0;
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      std::int32_t expected = 0;
      for (int k = 0; k < depth; ++k) {
        expected +=
            (static_cast<std::int32_t>(left[depth * i + k]) + lhs_offset) *
            (static_cast<std::int32_t>(right[depth * j + k]) + rhs_offset);
      }
      float expected_float = static_cast<float>(expected) * result_offset;
      float actual_float = result[i * cols + j];
      if (actual_float != expected_float) {
        std::cout << "(" << i << ", " << j << "): " << expected_float << "!="
                  << actual_float << std::endl;
        wrong++;
      }
    }
  }
  if (wrong > 0) {
    std::cout << "Wrong: " << rows << "x" << cols << "x" << depth << " : "
              << wrong << "/" << (rows * cols) << std::endl
              << std::flush;
    std::exit(1);
  } else {
    std::cout << "." << std::flush;
  }
}


void check_result_i32(std::uint8_t* left, std::uint8_t* right,
                      std::int32_t* result, std::int32_t rows,
                      std::int32_t cols, std::int32_t depth,
                      std::int32_t lhs_offset, std::int32_t rhs_offset) {
  std::int32_t wrong = 0;
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
      std::int32_t expected = 0;
      for (int k = 0; k < depth; ++k) {
        expected +=
            (static_cast<std::int32_t>(left[depth * i + k]) + lhs_offset) *
            (static_cast<std::int32_t>(right[depth * j + k]) + rhs_offset);
      }
      std::int32_t actual = result[i * cols + j];
      if (actual != expected) {
        std::cout << "(" << i << ", " << j << "): " << expected << "!="
                  << actual << std::endl;
        wrong++;
      }
    }
  }
  if (wrong > 0) {
    std::cout << "Wrong: " << rows << "x" << cols << "x" << depth << " : "
              << wrong << "/" << (rows * cols) << std::endl
              << std::flush;
    std::exit(1);
  } else {
    std::cout << "." << std::flush;
  }
}

template <typename T>
void clear(T* result, std::int32_t rows, std::int32_t cols) {
  for (int i = 0; i < rows * cols; ++i) {
    result[i] = static_cast<T>(0);
  }
}

void test(std::uint8_t* scratch, std::uint8_t* lhs, std::uint8_t* rhs,
          std::int32_t m, std::int32_t n, std::int32_t k, std::uint8_t* result,
          gemmlowp::WorkersPool* pool, std::int32_t pool_size) {
  prepare_test_data(lhs, m, k, 11, 13);
  prepare_test_data(rhs, n, k, 177, 19);

  clear(result, m, n);
  gemmlowp::meta::multi_thread_gemm_q8(pool, pool_size, scratch, lhs, rhs, m, n,
                                       k, -127, -127, 127 * k, 1, 7, result);
  check_result(lhs, rhs, result, m, n, k, -127, -127, 127 * k, 1, 7);
}

void test_f(std::uint8_t* scratch, std::uint8_t* lhs, std::uint8_t* rhs,
            std::int32_t m, std::int32_t n, std::int32_t k, float* result,
            gemmlowp::WorkersPool* pool, std::int32_t pool_size) {
  prepare_test_data(lhs, m, k, 11, 13);
  prepare_test_data(rhs, n, k, 177, 19);

  clear(result, m, n);
  float scale = 1.0f / 1234567.8f;
  gemmlowp::meta::multi_thread_gemm_f(pool, pool_size, scratch, lhs, rhs, m, n,
                                      k, -127, -127, scale, result);
  check_result_f(lhs, rhs, result, m, n, k, -127, -127, scale);
}

void test_i32(std::uint8_t* scratch, std::uint8_t* lhs, std::uint8_t* rhs,
              std::int32_t m, std::int32_t n, std::int32_t k,
              std::int32_t* result, gemmlowp::WorkersPool* pool,
              std::int32_t pool_size) {
  prepare_test_data(lhs, m, k, 11, 13);
  prepare_test_data(rhs, n, k, 177, 19);

  clear(result, m, n);
  gemmlowp::meta::multi_thread_gemm_i32(pool, pool_size, scratch, lhs, rhs, m,
                                        n, k, -127, -127, result);
  check_result_i32(lhs, rhs, result, m, n, k, -127, -127);
}

void q_suite(int mi, int ni, int ki, int mx, int nx, int kx, int md, int nd,
             int kd, std::uint8_t* scratch, std::uint8_t* left,
             std::uint8_t* right, std::uint8_t* result,
             gemmlowp::WorkersPool* pool, int t) {
  for (int m = mi; m < mx; m += md) {
    for (int n = ni; n < nx; n += nd) {
      for (int k = ki; k < kx; k += kd) {
        test(scratch, left, right, m, n, k, result, pool, t);
      }
    }
  }
  std::cout << std::endl;
}

void f_suite(int mi, int ni, int ki, int mx, int nx, int kx, int md, int nd,
             int kd, std::uint8_t* scratch, std::uint8_t* left,
             std::uint8_t* right, float* result, gemmlowp::WorkersPool* pool,
             int t) {
  for (int m = mi; m < mx; m += md) {
    for (int n = ni; n < nx; n += nd) {
      for (int k = ki; k < kx; k += kd) {
        test_f(scratch, left, right, m, n, k, result, pool, t);
      }
    }
  }
  std::cout << std::endl;
}

void i32_suite(int mi, int ni, int ki, int mx, int nx, int kx, int md, int nd,
               int kd, std::uint8_t* scratch, std::uint8_t* left,
               std::uint8_t* right, std::int32_t* result,
               gemmlowp::WorkersPool* pool, int t) {
  for (int m = mi; m < mx; m += md) {
    for (int n = ni; n < nx; n += nd) {
      for (int k = ki; k < kx; k += kd) {
        test_i32(scratch, left, right, m, n, k, result, pool, t);
      }
    }
  }
  std::cout << std::endl;
}

int main(int argc, char* argv[]) {
  bool run_long_test = false;

  if (argc > 1 && strcmp(argv[1], "long")) {
    run_long_test = true;
  }

  const std::int32_t min_n = 1;
  const std::int32_t min_m = 1;
  const std::int32_t min_k = 8;

  const std::int32_t max_n = 1024;
  const std::int32_t max_m = 1024;
  const std::int32_t max_k = 2048;

  std::uint8_t* left = new std::uint8_t[max_m * max_k];
  std::uint8_t* right = new std::uint8_t[max_n * max_k];
  std::uint8_t* result = new std::uint8_t[max_m * max_n];
  float* result_float = new float[max_m * max_n];
  std::int32_t* result_i32 = new std::int32_t[max_m * max_n];
  std::uint8_t* scratch = new std::uint8_t[1024 * 1024 * 64];

  gemmlowp::WorkersPool pool;

  int max_repetitions = run_long_test ? 10 : 1;

  for (int repetitions = 0; repetitions < max_repetitions; ++repetitions) {
    int t = std::min(repetitions + 1, 4);
    std::cout << "Threads: " << t << std::endl << std::flush;

    std::cout << "Quantized 8 bit." << std::endl << std::flush;

    std::cout << "Small." << std::endl << std::flush;
    q_suite(1, 1, 1, 16, 16, 32, 1, 1, 1, scratch, left, right, result, &pool,
            t);

    if (run_long_test) {
      std::cout << "Big." << std::endl << std::flush;
      q_suite(1, 1, 1, 512, 512, 2048, 111, 111, 111, scratch, left, right,
              result, &pool, t);
    }

    std::cout << "Gemv." << std::endl << std::flush;
    q_suite(1, 1, 1, 2, 512, 2048, 1, 111, 111, scratch, left, right, result,
            &pool, t);
    q_suite(1, 1, 1, 512, 2, 2048, 111, 1, 111, scratch, left, right, result,
            &pool, t);

    std::cout << std::endl << "Floats." << std::endl << std::flush;

    std::cout << "Small." << std::endl << std::flush;
    f_suite(1, 1, 1, 16, 16, 32, 1, 1, 1, scratch, left, right, result_float,
            &pool, t);

    if (run_long_test) {
      std::cout << "Big." << std::endl << std::flush;
      f_suite(1, 1, 1, 512, 512, 2048, 111, 111, 111, scratch, left, right,
              result_float, &pool, t);
    }

    std::cout << "Gemv." << std::endl << std::flush;
    f_suite(1, 1, 1, 2, 512, 2048, 1, 111, 111, scratch, left, right,
            result_float, &pool, t);
    f_suite(1, 1, 1, 512, 2, 2048, 111, 1, 111, scratch, left, right,
            result_float, &pool, t);

    std::cout << std::endl << "Int32." << std::endl << std::flush;

    std::cout << "Small." << std::endl << std::flush;
    i32_suite(1, 1, 1, 16, 16, 32, 1, 1, 1, scratch, left, right, result_i32,
              &pool, t);

    if (run_long_test) {
      std::cout << "Big." << std::endl << std::flush;
      i32_suite(1, 1, 1, 512, 512, 2048, 111, 111, 111, scratch, left, right,
                result_i32, &pool, t);
    }

    std::cout << "Gemv." << std::endl << std::flush;
    i32_suite(1, 1, 1, 2, 512, 2048, 1, 111, 111, scratch, left, right,
              result_i32, &pool, t);
    i32_suite(1, 1, 1, 512, 2, 2048, 111, 1, 111, scratch, left, right,
              result_i32, &pool, t);

    std::cout << std::endl << std::flush;
  }

  std::cout << "Done." << std::endl << std::flush;
}