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

I8SpmdmBenchmark.cc « bench - github.com/marian-nmt/FBGEMM.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdec2d0cc8720d136b053253af6658c185c1f988 (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
/*
 * 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 <array>
#include <cassert>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <random>

#ifdef _OPENMP
#include <omp.h>
#endif

#include "BenchUtils.h"
#include "fbgemm/FbgemmI8Spmdm.h"
#include "src/RefImplementations.h"

using namespace std;
using namespace fbgemm;

int main() {
#ifdef _OPENMP
  // Use 1 thread unless OMP_NUM_THREADS is explicit set.
  const char* val = getenv("OMP_NUM_THREADS");
  if (val == nullptr || !*val) {
    omp_set_num_threads(1);
  }
#endif

  const vector<array<int, 3>> shapes = {
      //   M,    N,    K
      {1024, 1024, 1024},
      {511, 512, 512},
  };

  // SpMDM is often memory BW bound so we want to flush LLC.
  bool flush = true;
  std::vector<char> llc;
  if (flush) {
    llc.resize(128 * 1024 * 1024, 1.0);
  }

  constexpr int NWARMUP = 4;
  constexpr int NITER = 16;

#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
  cout << "WARNING: the timer may be inaccurate when used by multiple threads."
       << endl;
  cout << "M, "
       << "N, "
       << "K, "
       << "Density, "
       << "Accumulation, "
       << "Initialize (ms), "
       << "Transpose uint8 (ms), "
       << "Transpose 32xN (ms), "
       << "Compute (ms), "
       << "Transpose 32xN (ms), "
       << "Total (ms), "
       << "GB/s, "
       << "GOPs" << endl;
#else
  cout << "M, "
       << "N, "
       << "K, "
       << "Density, "
       << "Accumulation, "
       << "GB/s, "
       << "GOPs" << endl;
#endif

  for (const auto& shape : shapes) {
    for (float density : {0.0001f, 0.001f, 0.01f, 0.1f, 1.0f}) {
      for (bool accumulation : {false, true}) {
        int M = shape[0];
        int N = shape[1];
        int K = shape[2];

        cout << M << ", " << N << ", " << K << ", ";

        aligned_vector<uint8_t> A(M * K);
        randFill<uint8_t>(A, 0, 255);

        fbgemm::CompressedSparseColumn B_csc(K, N);
        vector<int32_t> C(M * N);
        vector<int32_t> C_ref(C.size());

        for (int i = 0; i < M; ++i) {
          for (int j = 0; j < N; ++j) {
            C_ref[i * N + j] = i + j;
          }
        }

        // deterministic random number
        std::default_random_engine eng;
        binomial_distribution<> per_col_nnz_dist(K, density);
        uniform_int_distribution<> value_dist(
            numeric_limits<int8_t>::min() / 2,
            numeric_limits<int8_t>::max() / 2);

        vector<int> row_indices(K);

        int total_nnz = 0;
        for (int j = 0; j < N; ++j) {
          B_csc.ColPtr()[j] = total_nnz;

          int nnz_of_j = per_col_nnz_dist(eng);
          total_nnz += nnz_of_j;

          iota(row_indices.begin(), row_indices.end(), 0);
          shuffle(row_indices.begin(), row_indices.end(), eng);
          sort(row_indices.begin(), row_indices.begin() + nnz_of_j);

          for (int k = 0; k < nnz_of_j; ++k) {
            B_csc.RowIdx().push_back(row_indices[k]);
            B_csc.Values().push_back(value_dist(eng));
          }
        }
        B_csc.ColPtr()[N] = total_nnz;

        double ttot = 0;
#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
        double total_initial_time = 0.0;
        double total_transpose_uint8_time = 0.0;
        double total_transpose_32xN_time = 0.0;
        double total_compute_time = 0.0;
        double total_transpose_Nx32_time = 0.0;
        double total_run_time = 0.0;
#endif
        double ops = double(NITER) * B_csc.NumOfNonZeros() * M * 2;
        double bytes = double(NITER) *
            (M * N * sizeof(int32_t) + M * K +
             B_csc.NumOfNonZeros() * (sizeof(int16_t) + sizeof(int8_t)) +
             B_csc.ColPtr().size() * sizeof(int32_t));

        spmdm_ref(M, A.data(), K, B_csc, accumulation, C_ref.data(), N);

        chrono::time_point<chrono::system_clock> t_begin, t_end;
        for (int iter = 0; iter < NWARMUP + NITER; ++iter) {
          for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
              C[i * N + j] = i + j;
            }
          }
          llc_flush(llc);

          t_begin = chrono::system_clock::now();
#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
          spmdm_initial_time = 0.0;
          spmdm_transpose_uint8_time = 0.0;
          spmdm_transpose_32xN_time = 0.0;
          spmdm_compute_time = 0.0;
          spmdm_transpose_Nx32_time = 0.0;
          spmdm_run_time = 0.0;
#endif

#ifndef FBGEMM_MEASURE_TIME_BREAKDOWN
#pragma omp parallel
#endif
          {
            int num_threads = fbgemm_get_num_threads();
            int tid = fbgemm_get_thread_num();
            int i_per_thread =
                ((M + 31) / 32 + num_threads - 1) / num_threads * 32;
            int i_begin = std::min(tid * i_per_thread, M);
            int i_end = std::min(i_begin + i_per_thread, M);

            block_type_t block = {i_begin, i_end - i_begin, 0, N};
            B_csc.SpMDM(
                block, A.data(), K, accumulation, C.data() + i_begin * N, N);
          }
          t_end = chrono::system_clock::now();
          if (iter >= NWARMUP) {
            double dt = chrono::duration<double>(t_end - t_begin).count();
            // double dt = chrono::duration_cast<chrono::nanoseconds>(t_end -
            // t_begin).count();
            ttot += dt;
#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
            total_initial_time += spmdm_initial_time;
            total_transpose_uint8_time += spmdm_transpose_uint8_time;
            total_transpose_32xN_time += spmdm_transpose_32xN_time;
            total_compute_time += spmdm_compute_time;
            total_transpose_Nx32_time += spmdm_transpose_Nx32_time;
            total_run_time += spmdm_run_time;
#endif
          }
        }

        compare_buffers(C_ref.data(), C.data(), M, N, N, 5, 0);

        cout << fixed << B_csc.Density() << ", " << accumulation << ", ";

#ifdef FBGEMM_MEASURE_TIME_BREAKDOWN
        cout << fixed << total_initial_time / (double)NITER / 1e6 << ", "
             << total_transpose_uint8_time / (double)NITER / 1e6 << ", "
             << total_transpose_32xN_time / (double)NITER / 1e6 << ", "
             << total_compute_time / (double)NITER / 1e6 << ", "
             << total_transpose_Nx32_time / (double)NITER / 1e6 << ", "
             << total_run_time / (double)NITER / 1e6 << ", ";
#endif
        // Report performance
        cout << fixed << bytes / ttot / 1e9 << ", " << ops / ttot / 1e9 << endl;

      } // accumulation
    } // for each density
  } // for each shape

  return 0;
}