• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #pragma once
7 
8 #include <gtest/gtest.h>
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <cmath>
13 #include <cstddef>
14 #include <cstdlib>
15 #include <functional>
16 #include <random>
17 #include <vector>
18 
19 #include <fp16.h>
20 
21 #include <xnnpack.h>
22 #include <xnnpack/AlignedAllocator.h>
23 #include <xnnpack/params-init.h>
24 #include <xnnpack/params.h>
25 
26 
is_fp16_zero(uint16_t x)27 static inline bool is_fp16_zero(uint16_t x) {
28   const uint16_t two_x = x + x;
29   return two_x == 0;
30 }
31 
32 class SpMMMicrokernelTester {
33  public:
mr(size_t mr)34   inline SpMMMicrokernelTester& mr(size_t mr) {
35     this->mr_ = mr;
36     return *this;
37   }
38 
mr()39   inline size_t mr() const {
40     return this->mr_;
41   }
42 
nr(size_t nr)43   inline SpMMMicrokernelTester& nr(size_t nr) {
44     this->nr_ = nr;
45     return *this;
46   }
47 
nr()48   inline size_t nr() const {
49     return this->nr_;
50   }
51 
m(size_t m)52   inline SpMMMicrokernelTester& m(size_t m) {
53     this->m_ = m;
54     return *this;
55   }
56 
m()57   inline size_t m() const {
58     return this->m_;
59   }
60 
n(size_t n)61   inline SpMMMicrokernelTester& n(size_t n) {
62     this->n_ = n;
63     return *this;
64   }
65 
n()66   inline size_t n() const {
67     return this->n_;
68   }
69 
k(size_t k)70   inline SpMMMicrokernelTester& k(size_t k) {
71     this->k_ = k;
72     return *this;
73   }
74 
k()75   inline size_t k() const {
76     return this->k_;
77   }
78 
output_stride(size_t output_stride)79   inline SpMMMicrokernelTester& output_stride(size_t output_stride) {
80     assert(output_stride != 0);
81     this->output_stride_ = output_stride;
82     return *this;
83   }
84 
output_stride()85   inline size_t output_stride() const {
86     if (this->output_stride_ == 0) {
87       return m();
88     } else {
89       assert(this->output_stride_ >= m());
90       return this->output_stride_;
91     }
92   }
93 
sparsity(float sparsity)94   inline SpMMMicrokernelTester& sparsity(float sparsity) {
95     this->sparsity_ = sparsity;
96     return *this;
97   }
98 
sparsity()99   inline float sparsity() const {
100     return this->sparsity_;
101   }
102 
qmin(uint8_t qmin)103   inline SpMMMicrokernelTester& qmin(uint8_t qmin) {
104     this->qmin_ = qmin;
105     return *this;
106   }
107 
qmin()108   inline uint8_t qmin() const {
109     return this->qmin_;
110   }
111 
qmax(uint8_t qmax)112   inline SpMMMicrokernelTester& qmax(uint8_t qmax) {
113     this->qmax_ = qmax;
114     return *this;
115   }
116 
qmax()117   inline uint8_t qmax() const {
118     return this->qmax_;
119   }
120 
iterations(size_t iterations)121   inline SpMMMicrokernelTester& iterations(size_t iterations) {
122     this->iterations_ = iterations;
123     return *this;
124   }
125 
iterations()126   inline size_t iterations() const {
127     return this->iterations_;
128   }
129 
Test(xnn_f32_spmm_minmax_ukernel_function spmm,xnn_init_f32_minmax_params_fn init_params)130   void Test(xnn_f32_spmm_minmax_ukernel_function spmm, xnn_init_f32_minmax_params_fn init_params) const {
131     ASSERT_GE(m(), 1);
132     ASSERT_GE(n(), 1);
133     ASSERT_GE(k(), 1);
134 
135     std::random_device random_device;
136     auto rng = std::mt19937(random_device());
137     auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
138     auto prng = std::bind(std::uniform_real_distribution<float>(), rng);
139 
140     std::vector<float, AlignedAllocator<float, 64>> input(k() * m());
141     // Think of b as (n/nr + n % nr) x k, expansion happens later.
142     const size_t ncols = n() / nr() + n() % nr();
143     std::vector<float> b(ncols * k());
144     std::vector<float> bias(n());
145     // Number of non-zero weights per N (output channel).
146     std::vector<uint32_t> nmap(n());
147     // Mapping from index of non-zero weight to increment of K (input channel) following this index.
148     std::vector<int32_t> dmap(n() * k());
149     std::vector<float> w(n() * k() + n());
150     std::vector<float> output((n() - 1) * output_stride() + m());
151     std::vector<float> output_ref(n() * m());
152 
153     for (size_t iteration = 0; iteration < iterations(); iteration++) {
154       std::generate(input.begin(), input.end(), std::ref(f32rng));
155       std::generate(b.begin(), b.end(), std::ref(f32rng));
156       std::generate(bias.begin(), bias.end(), std::ref(f32rng));
157       std::fill(output.begin(), output.end(), nanf(""));
158       std::fill(output_ref.begin(), output_ref.end(), 0.0f);
159       std::fill(nmap.begin(), nmap.end(), 0);
160       std::fill(dmap.begin(), dmap.end(), 0);
161       std::fill(w.begin(), w.end(), 0.0f);
162 
163       for (float& b_value : b) {
164         if (prng() <= sparsity()) {
165           b_value = 0.0f;
166         }
167       }
168 
169       uint32_t nnz = 0;
170       uint32_t wcnt = 0;
171       size_t last_kk = 0;
172       bool first_nzz = true;
173       size_t first_kk = 0;
174       for (size_t nn = 0; nn < n() / nr(); nn++) {
175         for (size_t i = 0; i < nr(); ++i)
176           w[wcnt++] = bias[nr() * nn + i];
177         for (size_t kk = 0; kk < k(); kk++) {
178           if (b[nn * k() + kk] != 0.0f) {
179             // Every non-zero actually corresponds to nr adjacent non-zeros.
180             for (size_t i = 0; i < nr(); ++i)
181               w[wcnt++] = b[nn * k() + kk] + static_cast<float>(i);
182             // Skip the very first non-zero weight as we record only the difference.
183             if (first_nzz) {
184               first_kk = kk;
185             } else {
186               const int32_t increment = int32_t(kk - last_kk) * int32_t(m() * sizeof(float));
187               dmap[nnz++] = increment;
188             }
189             last_kk = kk;
190             first_nzz = false;
191             nmap[nn] += 1;
192           }
193         }
194       }
195 
196       // now we've constructed the matrix for the blocked part and switch to the
197       // leftovers, which we do as nr=1 always.
198       for (size_t nn = n() / nr(); nn < ncols; nn++) {
199         w[wcnt++] = bias[(n() / nr()) * nr() + (nn - n() / nr())];
200         for (size_t kk = 0; kk < k(); kk++) {
201           if (b[nn * k() + kk] != 0.0f) {
202             // Every non-zero actually corresponds to nr adjacent non-zeros.
203             w[wcnt++] = b[nn * k() + kk];
204             // Skip the very first non-zero weight as we record only the difference.
205             if (first_nzz) {
206               first_kk = kk;
207             } else {
208               const int32_t increment = int32_t(kk - last_kk) * int32_t(m() * sizeof(float));
209               dmap[nnz++] = increment;
210             }
211             last_kk = kk;
212             first_nzz = false;
213             nmap[nn] += 1;
214           }
215         }
216       }
217       // In the end, we must return input pointer to the initial value.
218       const int64_t increment = int32_t(first_kk - last_kk) * int32_t(m() * sizeof(float));
219       dmap[nnz++] = increment;
220 
221       // Generate expanded b which will be used in reference calculation.
222       // Everywhere there is input non-zero in the original we copy it and add an
223       // adjacent non-zero with incremented weight value.
224       std::vector<float> b_full(n() * k());
225       if (nr() == 1) {
226          b_full = b;
227       }
228       else {
229         for (size_t nn = 0; nn < n() / nr(); nn++) {
230           for (size_t kk = 0; kk < k(); kk++) {
231             if (b[nn * k() + kk] != 0.0f) {
232               for (size_t i = 0; i < nr(); ++i)
233                 b_full[nr() * nn * k() + i * k() + kk] = b[nn * k() + kk] + static_cast<float>(i);
234             }
235           }
236         }
237         for (size_t nn = n() / nr(); nn < ncols; nn++) {
238           for (size_t kk = 0; kk < k(); kk++) {
239             if (b[nn * k() + kk] != 0.0f) {
240               b_full[nr() * (n() / nr()) * k() + (nn - n() / nr()) * k() + kk] = b[nn * k() + kk];
241             }
242           }
243         }
244       }
245 
246       for (size_t oc = 0; oc < n(); oc++) {
247         for (size_t pxb = 0; pxb < m(); pxb++) {
248           output_ref[oc * m() + pxb] = bias[oc];
249           for (size_t ic = 0; ic < k(); ic++) {
250             output_ref[oc * m() + pxb] += input[ic * m() + pxb] * b_full[oc * k() + ic];
251           }
252         }
253       }
254 
255       // Micro-kernel can access one element beyond w and dmap for software pipelining.
256       w.resize(wcnt + 1);
257       dmap.resize(nnz + 1);
258 
259       // Compute clamping parameters.
260       const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
261       const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
262       const float output_min = accumulated_min + (accumulated_max - accumulated_min) / 255.0f * float(qmin());
263       const float output_max = accumulated_max - (accumulated_max - accumulated_min) / 255.0f * float(255 - qmax());
264 
265       // Clamp reference results.
266       for (float& output_value : output_ref) {
267         output_value = std::min(std::max(output_value, output_min), output_max);
268       }
269 
270       // Prepare parameters.
271       xnn_f32_minmax_params params;
272       init_params(&params, output_min, output_max);
273 
274       spmm(m() * sizeof(float), n(),
275         input.data() + first_kk * m(),
276         w.data(), dmap.data(), nmap.data(),
277         output.data(), output_stride() * sizeof(float),
278         &params);
279 
280       // Validate micro-kernel outputs.
281       for (size_t i = 0; i < m(); i++) {
282         for (size_t j = 0; j < n(); j++) {
283           ASSERT_NEAR(
284               output[j * output_stride() + i],
285               output_ref[j * m() + i],
286               std::abs(output_ref[j * m() + i]) * 1.0e-6f)
287             << "at M index " << i << " / " << m() << " (tile " << mr() << ")"
288             << ", N index " << j << " / " << n() << " (tile " << nr() << ")"
289             << ", K = " << k();
290         }
291       }
292     }
293   }
294 
Test(xnn_f16_spmm_minmax_ukernel_function spmm,xnn_init_f16_scaleminmax_params_fn init_params)295   void Test(xnn_f16_spmm_minmax_ukernel_function spmm, xnn_init_f16_scaleminmax_params_fn init_params) const {
296     ASSERT_GE(m(), 1);
297     ASSERT_GE(n(), 1);
298     ASSERT_GE(k(), 1);
299 
300     std::random_device random_device;
301     auto rng = std::mt19937(random_device());
302     auto f32rng = std::bind(std::uniform_real_distribution<float>(), rng);
303     auto f16rng = std::bind(fp16_ieee_from_fp32_value, f32rng);
304     auto prng = std::bind(std::uniform_real_distribution<float>(), rng);
305 
306     std::vector<uint16_t, AlignedAllocator<uint16_t, 64>> input(k() * m());
307     // Think of b as (n/nr + n % nr) x k, expansion happens later.
308     const size_t ncols = n() / nr() + n() % nr();
309     std::vector<uint16_t> b(ncols * k());
310     std::vector<uint16_t> bias(n());
311     // Number of non-zero weights per N (output channel).
312     std::vector<uint32_t> nmap(n());
313     // Mapping from index of non-zero weight to increment of K (input channel) following this index.
314     std::vector<int32_t> dmap(n() * k());
315     std::vector<uint16_t> w(n() * k() + n());
316     std::vector<uint16_t> output((n() - 1) * output_stride() + m());
317     std::vector<float> output_ref(n() * m());
318 
319     for (size_t iteration = 0; iteration < iterations(); iteration++) {
320       std::generate(input.begin(), input.end(), std::ref(f16rng));
321       std::generate(b.begin(), b.end(), std::ref(f16rng));
322       std::generate(bias.begin(), bias.end(), std::ref(f16rng));
323       std::fill(output.begin(), output.end(), 0xC000);
324       std::fill(output_ref.begin(), output_ref.end(), 0.0f);
325       std::fill(nmap.begin(), nmap.end(), 0);
326       std::fill(dmap.begin(), dmap.end(), 0);
327       std::fill(w.begin(), w.end(), 0);
328 
329       for (uint16_t& b_value : b) {
330         if (prng() <= sparsity()) {
331           b_value = 0;
332         }
333       }
334 
335       uint32_t nnz = 0;
336       uint32_t wcnt = 0;
337       size_t last_kk = 0;
338       bool first_nzz = true;
339       size_t first_kk = 0;
340       for (size_t nn = 0; nn < n() / nr(); nn++) {
341         for (size_t i = 0; i < nr(); ++i)
342           w[wcnt++] = bias[nr() * nn + i];
343         for (size_t kk = 0; kk < k(); kk++) {
344           if (!is_fp16_zero(b[nn * k() + kk])) {
345             // Every non-zero actually corresponds to nr adjacent non-zeros.
346             for (size_t i = 0; i < nr(); ++i)
347               w[wcnt++] = fp16_ieee_from_fp32_value(fp16_ieee_to_fp32_value(b[nn * k() + kk]) + static_cast<float>(i));
348             // Skip the very first non-zero weight as we record only the difference.
349             if (first_nzz) {
350               first_kk = kk;
351             } else {
352               const int32_t increment = int32_t(kk - last_kk) * int32_t(m() * sizeof(uint16_t));
353               dmap[nnz++] = increment;
354             }
355             last_kk = kk;
356             first_nzz = false;
357             nmap[nn] += 1;
358           }
359         }
360       }
361 
362       // now we've constructed the matrix for the blocked part and switch to the
363       // leftovers, which we do as nr=1 always.
364       for (size_t nn = n() / nr(); nn < ncols; nn++) {
365         w[wcnt++] = bias[(n() / nr()) * nr() + (nn - n() / nr())];
366         for (size_t kk = 0; kk < k(); kk++) {
367           if (!is_fp16_zero(b[nn * k() + kk])) {
368             // Every non-zero actually corresponds to nr adjacent non-zeros.
369             w[wcnt++] = b[nn * k() + kk];
370             // Skip the very first non-zero weight as we record only the difference.
371             if (first_nzz) {
372               first_kk = kk;
373             } else {
374               const int32_t increment = int32_t(kk - last_kk) * int32_t(m() * sizeof(uint16_t));
375               dmap[nnz++] = increment;
376             }
377             last_kk = kk;
378             first_nzz = false;
379             nmap[nn] += 1;
380           }
381         }
382       }
383       // In the end, we must return input pointer to the initial value.
384       const int64_t increment = int32_t(first_kk - last_kk) * int32_t(m() * sizeof(uint16_t));
385       dmap[nnz++] = increment;
386 
387       // Generate expanded b which will be used in reference calculation.
388       // Everywhere there is input non-zero in the original we copy it and add an
389       // adjacent non-zero with incremented weight value.
390       std::vector<uint16_t> b_full(n() * k());
391       if (nr() == 1) {
392          b_full = b;
393       }
394       else {
395         for (size_t nn = 0; nn < n() / nr(); nn++) {
396           for (size_t kk = 0; kk < k(); kk++) {
397             if (b[nn * k() + kk] != 0.0f) {
398               for (size_t i = 0; i < nr(); ++i)
399                 b_full[nr() * nn * k() + i * k() + kk] = fp16_ieee_from_fp32_value(
400                   fp16_ieee_to_fp32_value(b[nn * k() + kk]) + static_cast<float>(i));
401             }
402           }
403         }
404         for (size_t nn = n() / nr(); nn < ncols; nn++) {
405           for (size_t kk = 0; kk < k(); kk++) {
406             if (b[nn * k() + kk] != 0.0f) {
407               b_full[nr() * (n() / nr()) * k() + (nn - n() / nr()) * k() + kk] = b[nn * k() + kk];
408             }
409           }
410         }
411       }
412 
413       for (size_t oc = 0; oc < n(); oc++) {
414         for (size_t pxb = 0; pxb < m(); pxb++) {
415           output_ref[oc * m() + pxb] = fp16_ieee_to_fp32_value(bias[oc]);
416           for (size_t ic = 0; ic < k(); ic++) {
417             output_ref[oc * m() + pxb] += fp16_ieee_to_fp32_value(input[ic * m() + pxb]) * fp16_ieee_to_fp32_value(b_full[oc * k() + ic]);
418           }
419         }
420       }
421 
422       // Micro-kernel can access one element beyond w and dmap for software pipelining.
423       w.resize(wcnt + 1);
424       dmap.resize(nnz + 1);
425 
426       // Compute clamping parameters.
427       const float accumulated_min = *std::min_element(output_ref.cbegin(), output_ref.cend());
428       const float accumulated_max = *std::max_element(output_ref.cbegin(), output_ref.cend());
429       const float output_min = accumulated_min + (accumulated_max - accumulated_min) / 255.0f * float(qmin());
430       const float output_max = accumulated_max - (accumulated_max - accumulated_min) / 255.0f * float(255 - qmax());
431 
432       // Clamp reference results.
433       for (float& output_value : output_ref) {
434         output_value = std::min(std::max(output_value, output_min), output_max);
435       }
436 
437       // Prepare parameters.
438       xnn_f16_scaleminmax_params params;
439       init_params(&params,
440         UINT16_C(0x3C00) /* 1.0 */, fp16_ieee_from_fp32_value(output_min), fp16_ieee_from_fp32_value(output_max));
441 
442       spmm(m() * sizeof(uint16_t), n(),
443         input.data() + first_kk * m(),
444         w.data(), dmap.data(), nmap.data(),
445         output.data(), output_stride() * sizeof(uint16_t),
446         &params);
447 
448       // Validate micro-kernel outputs.
449       for (size_t i = 0; i < m(); i++) {
450         for (size_t j = 0; j < n(); j++) {
451           ASSERT_NEAR(
452               fp16_ieee_to_fp32_value(output[j * output_stride() + i]),
453               output_ref[j * m() + i],
454               std::max(1.0e-4f, std::abs(output_ref[j * m() + i]) * 1.0e-2f))
455             << "at M index " << i << " / " << m() << " (tile " << mr() << ")"
456             << ", N index " << j << " / " << n() << " (tile " << nr() << ")"
457             << ", K = " << k();
458         }
459       }
460     }
461   }
462 
463  private:
464   size_t mr_{1};
465   size_t nr_{1};
466   size_t m_{1};
467   size_t n_{1};
468   size_t k_{1};
469   size_t output_stride_{0};
470   float sparsity_{0.5f};
471   uint8_t qmin_{0};
472   uint8_t qmax_{255};
473   size_t iterations_{1};
474 };
475