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

avx512vnni_gemm.h - github.com/marian-nmt/intgemm/intgemm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9db526b6a260633599a360c3fd2db81be01a37d (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
#pragma once

#include "intgemm_config.h"

#ifdef INTGEMM_COMPILER_SUPPORTS_AVX512VNNI
#include "avx512_gemm.h"
#include "types.h"

namespace intgemm {

// Workaround extra vmovdqa64 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94663
INTGEMM_AVX512VNNI static inline void VNNI8(__m512i &c, __m512i a, __m512i b) {
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
    asm ("vpdpbusds %2, %1, %0" : "+x"(c) : "x"(a), "mx"(b));
#else
    c = _mm512_dpbusds_epi32(c, a, b);
#endif
}

// Rewrite that loads of struct to template labdas as soon as c++14 is used
struct AVX512VNNI_Multiply_InitALivesLoop {
  template <typename Iterator, typename Type>
  INTGEMM_AVX512VNNI static void body(const Type* A, Index A_rowidx, Index width, const __m512i* A_lives[Iterator::total_iterations]) {
    A_lives[Iterator::template I<0>()] = reinterpret_cast<const __m512i*>(A + (A_rowidx + Iterator::template I<0>()) * width);
  }
};

struct AVX512VNNI_Multiply_InitSumsLoop {
  template <typename Iterator>
  INTGEMM_AVX512VNNI static void body(__m512i sums[Iterator::template N<0>()][Iterator::template N<1>()]) {
    static constexpr auto Row = Iterator::template I<0>();
    static constexpr auto Column = Iterator::template I<1>();
    sums[Row][Column] = setzero_si<__m512i>();
  }
};

struct AVX512VNNI_Multiply_TileLoop {
  template <typename Iterator>
  INTGEMM_AVX512VNNI static void body(const __m512i* A_lives[Iterator::template N<0>()],
                          const __m512i* B_live,
                          __m512i sums[Iterator::template N<0>()][Iterator::template N<1>()]) {
    static constexpr auto Row = Iterator::template I<0>();
    static constexpr auto Column = Iterator::template I<1>();
    auto neg_mask = _mm512_test_epi8_mask(*A_lives[Row], _mm512_set1_epi8(-128));
    VNNI8(sums[Row][Column], _mm512_abs_epi8(*A_lives[Row]), _mm512_mask_sub_epi8(B_live[Column], neg_mask, setzero_si<__m512i>(), B_live[Column]));
  }
};

struct AVX512VNNI_Multiply_IncreaseALivesLoop {
  template <typename Iterator>
  INTGEMM_AVX512VNNI static void body(const __m512i* A_lives[Iterator::total_iterations]) {
    ++A_lives[Iterator::template I<0>()];
  }
};

struct AVX512VNNI_Multiply_MakeFinalOutputAndRunCallback {
  template <typename Iterator, typename CallbackImpl>
  INTGEMM_AVX512VNNI static void body(__m512i sums[Iterator::template N<0>()][8 * Iterator::template N<1>()], CallbackImpl callback_impl, Index A_rowidx, Index B_colidx, Index A_rows, Index B_cols) {
    static constexpr auto Row = Iterator::template I<0>();
    static constexpr auto Column8 = Iterator::template I<1>();
    auto pack0123 = Pack0123(sums[Row][8 * Column8 + 0], sums[Row][8 * Column8 + 1], sums[Row][8 * Column8 + 2], sums[Row][8 * Column8 + 3]);
    auto pack4567 = Pack0123(sums[Row][8 * Column8 + 4], sums[Row][8 * Column8 + 5], sums[Row][8 * Column8 + 6], sums[Row][8 * Column8 + 7]);
    auto total = PermuteSummer(pack0123, pack4567);
    RunCallback(callback_impl, total, A_rowidx + Iterator::template I<0>(), B_colidx + 8 * Column8, A_rows, B_cols);
  }
};

struct AVX512VNNI_Multiply8Shift_TileLoop {
  template <typename Iterator>
  INTGEMM_AVX512VNNI static void body(const __m512i* A_lives[Iterator::template N<0>()],
                          const __m512i* B_live,
                          __m512i sums[Iterator::template N<0>()][Iterator::template N<1>()]) {
    static constexpr auto Row = Iterator::template I<0>();
    static constexpr auto Column = Iterator::template I<1>();
    VNNI8(sums[Row][Column], *A_lives[Row], B_live[Column]);
  }
};

struct AVX512VNNI_8bit : public AVX512_8bit {
  template <Index TileRows, Index TileColumnsMultiplier, typename Callback>
  INTGEMM_AVX512VNNI static void Multiply(const int8_t *A, const int8_t *B, Index A_rows, Index width, Index B_cols, Callback callback) {
    static constexpr Index TileColumns = 8 * TileColumnsMultiplier;
    assert(A_rows % TileRows == 0);
    assert(width % sizeof(__m512i) == 0);
    assert(B_cols % TileColumns == 0);
    assert(reinterpret_cast<uintptr_t>(A) % sizeof(__m512i) == 0);
    assert(reinterpret_cast<uintptr_t>(B) % sizeof(__m512i) == 0);

    const Index simd_width = width / sizeof(__m512i);
    auto callback_impl = callbacks::CallbackImpl<CPUType::AVX2, Callback>(callback);
    const __m512i *A_lives[TileRows];
    __m512i sums[TileRows][TileColumns];

    /* Process with tile = (TileRows, TileColumns). */
    auto *B0_col = reinterpret_cast<const __m512i*>(B);
#pragma omp for
    for (Index B0_colidx = 0; B0_colidx != B_cols; B0_col += TileColumns * simd_width, B0_colidx += TileColumns) {
      for (Index A_rowidx = 0; A_rowidx < A_rows; A_rowidx += TileRows) {
        StaticLoop<AVX512VNNI_Multiply_InitALivesLoop, MakeStaticLoopIterator<TileRows>>(A, A_rowidx, width, A_lives);
        StaticLoop<AVX512VNNI_Multiply_InitSumsLoop, MakeStaticLoopIterator<TileRows, TileColumns>>(sums);
        /* Process a tile (use A as the loop variable so the add can be done where gcc likes it for branch prediction. */
        auto* B_live = B0_col;
        for (Index i = 0; i < simd_width; ++i, B_live += TileColumns) {
          StaticLoop<AVX512VNNI_Multiply_TileLoop, MakeStaticLoopIterator<TileRows, TileColumns>>(A_lives, B_live, sums);
          StaticLoop<AVX512VNNI_Multiply_IncreaseALivesLoop, MakeStaticLoopIterator<TileRows>>(A_lives);
        }
        StaticLoop<AVX512VNNI_Multiply_MakeFinalOutputAndRunCallback, MakeStaticLoopIterator<TileRows, TileColumnsMultiplier>>(sums, callback_impl, A_rowidx, B0_colidx, A_rows, B_cols);
      }
    }
  }

  template <Index TileRows, Index TileColumnsMultiplier, typename Callback>
  INTGEMM_AVX512VNNI static void Multiply8Shift(const uint8_t *A, const int8_t *B, Index A_rows, Index width, Index B_cols, Callback callback) {
    static constexpr Index TileColumns = 8 * TileColumnsMultiplier;
    assert(A_rows % TileRows == 0);
    assert(width % sizeof(__m512i) == 0);
    assert(B_cols % TileColumns == 0);
    assert(reinterpret_cast<uintptr_t>(A) % sizeof(__m512i) == 0);
    assert(reinterpret_cast<uintptr_t>(B) % sizeof(__m512i) == 0);

    const Index simd_width = width / sizeof(__m512i);
    auto callback_impl = callbacks::CallbackImpl<CPUType::AVX2, Callback>(callback);
    const __m512i *A_lives[TileRows];
    __m512i sums[TileRows][TileColumns];

    /* Process with tile = (TileRows, TileColumns). */
    auto *B0_col = reinterpret_cast<const __m512i*>(B);
#pragma omp for
    for (Index B0_colidx = 0; B0_colidx != B_cols; B0_col += TileColumns * simd_width, B0_colidx += TileColumns) {
      for (Index A_rowidx = 0; A_rowidx < A_rows; A_rowidx += TileRows) {
        StaticLoop<AVX512VNNI_Multiply_InitALivesLoop, MakeStaticLoopIterator<TileRows>>(A, A_rowidx, width, A_lives);
        StaticLoop<AVX512VNNI_Multiply_InitSumsLoop, MakeStaticLoopIterator<TileRows, TileColumns>>(sums);
        /* Process a tile (use A as the loop variable so the add can be done where gcc likes it for branch prediction. */
        auto* B_live = B0_col;
        for (Index i = 0; i < simd_width; ++i, B_live += TileColumns) {
          StaticLoop<AVX512VNNI_Multiply8Shift_TileLoop, MakeStaticLoopIterator<TileRows, TileColumns>>(A_lives, B_live, sums);
          StaticLoop<AVX512VNNI_Multiply_IncreaseALivesLoop, MakeStaticLoopIterator<TileRows>>(A_lives);
        }
        StaticLoop<AVX512VNNI_Multiply_MakeFinalOutputAndRunCallback, MakeStaticLoopIterator<TileRows, TileColumnsMultiplier>>(sums, callback_impl, A_rowidx, B0_colidx, A_rows, B_cols);
      }
    }
  }

  template <typename Callback>
  INTGEMM_AVX512VNNI static void PrepareBias(const int8_t *B, Index width, Index B_cols, Callback callback) {
    assert(width % sizeof(__m512i) == 0);
    assert(B_cols % 8 == 0);
    assert(reinterpret_cast<uintptr_t>(B) % sizeof(__m512i) == 0);
    auto callback_impl = callbacks::CallbackImpl<CPUType::AVX2, Callback>(callback);
    const Index simd_width = width / sizeof(__m512i);
    __m512i zeros = setzero_si<__m512i>();
    const __m512i a = set1_epi8<__m512i>(1);
    // Go over 8 columns of B at a time.
#pragma omp for
    for (Index B0_colidx = 0; B0_colidx < B_cols; B0_colidx += 8) {
      const __m512i *B0_col = reinterpret_cast<const __m512i*>(B) + B0_colidx * simd_width;
      const __m512i *B_live = B0_col; //In order to make the code look as much as possible as the above function
      const __m512i *B_end = B_live + simd_width*8;

      // TODO: separate first step.
      __m512i sum0 = zeros, sum1 = zeros, sum2 = zeros, sum3 = zeros, sum4 = zeros, sum5 = zeros, sum6 = zeros, sum7 = zeros;
      for (; B_live != B_end; B_live += 8) {
        // Retrieve the conveniently consecutive values of B.
        VNNI8(sum0, a, *B_live);
        VNNI8(sum1, a, *(B_live + 1));
        VNNI8(sum2, a, *(B_live + 2));
        VNNI8(sum3, a, *(B_live + 3));
        VNNI8(sum4, a, *(B_live + 4));
        VNNI8(sum5, a, *(B_live + 5));
        VNNI8(sum6, a, *(B_live + 6));
        VNNI8(sum7, a, *(B_live + 7));
      }
      __m512i pack0123 = Pack0123(sum0, sum1, sum2, sum3);
      __m512i pack4567 = Pack0123(sum4, sum5, sum6, sum7);
      auto total = PermuteSummer(pack0123, pack4567);
      callback_impl(total, callbacks::OutputBufferInfo(0, B0_colidx, 1, B_cols));
    }
  }

  constexpr static const char *const kName = "8-bit AVX512VNNI";

  static const CPUType kUses = CPUType::AVX512VNNI;
};

} // namespace intgemm

#endif