|
| 1 | +#define TORCH_ASSERT_ONLY_METHOD_OPERATORS |
| 2 | +#include <ATen/core/Tensor.h> |
| 3 | + |
| 4 | +#include <ATen/Dispatch.h> |
| 5 | +#include <ATen/Parallel.h> |
| 6 | +#include <ATen/cpu/vec/functional.h> |
| 7 | +#include <ATen/cpu/vec/vec.h> |
| 8 | +#include <ATen/native/cpu/utils.h> |
| 9 | +#include <ATen/native/LinearAlgebra.h> |
| 10 | +#include <c10/util/irange.h> |
| 11 | + |
| 12 | +#if (defined(_WIN32) || defined(_WIN64)) |
| 13 | +#define RESTRICT __restrict |
| 14 | +#else |
| 15 | +#define RESTRICT __restrict__ |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace at::native { |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +#if defined(CPU_CAPABILITY_AVX512) && !defined(_MSC_VER) |
| 23 | + |
| 24 | +// A block : {BLOCK_M, BLOCK_K}, lda = K |
| 25 | +// B block : {BLOCK_K, BLOCK_N}, ldb = K |
| 26 | +// C block : {BLOCK_M, BLOCK_N}, ldc = N |
| 27 | +// |
| 28 | +// scales block: {BLOCK_N} |
| 29 | +// |
| 30 | +template <int BLOCK_M, int BLOCK_N> |
| 31 | +inline void tinygemm_kernel( |
| 32 | + const BFloat16* RESTRICT A, |
| 33 | + const int8_t* RESTRICT B, |
| 34 | + const BFloat16* RESTRICT scales, |
| 35 | + BFloat16* RESTRICT C, |
| 36 | + int lda, |
| 37 | + int ldb, |
| 38 | + int ldc, |
| 39 | + int K) { |
| 40 | + |
| 41 | + constexpr int ROWS = BLOCK_M; |
| 42 | + constexpr int COLS = BLOCK_N; |
| 43 | + |
| 44 | + const int PREFETCH_SIZE_K = 16 * 4; |
| 45 | + |
| 46 | + __m512 va; |
| 47 | + __m512 vb[COLS]; |
| 48 | + __m512 vc[ROWS * COLS]; |
| 49 | + __m512 scale[COLS]; |
| 50 | + |
| 51 | + auto load_scale = [&](int i) { |
| 52 | + float ss = static_cast<float>(scales[i]); |
| 53 | + scale[i] = _mm512_set1_ps(ss); |
| 54 | + }; |
| 55 | + compile_time_for<COLS>::op(load_scale); |
| 56 | + |
| 57 | + auto loadc = [&](auto i) { |
| 58 | + vc[i] = _mm512_setzero_ps(); |
| 59 | + }; |
| 60 | + compile_time_for<ROWS * COLS>::op(loadc); |
| 61 | + |
| 62 | + auto compute = [&](auto i, int k) { |
| 63 | + constexpr int row = i / COLS; |
| 64 | + constexpr int col = i % COLS; |
| 65 | + |
| 66 | + if constexpr (col == 0) { |
| 67 | + __m256i a16 = _mm256_load_si256((__m256i*)(A + row * lda + k)); |
| 68 | + if (k + PREFETCH_SIZE_K < K) { |
| 69 | + _mm_prefetch(A + row * lda + k + PREFETCH_SIZE_K, _MM_HINT_T0); |
| 70 | + } |
| 71 | + vec::cvtbf16_fp32(a16, va); |
| 72 | + } |
| 73 | + |
| 74 | + if constexpr (row == 0) { |
| 75 | + __m128i b8 = _mm_load_si128((__m128i*)(B + col * ldb + k)); |
| 76 | + if (k + PREFETCH_SIZE_K < K) { |
| 77 | + _mm_prefetch(B + col * ldb + k + PREFETCH_SIZE_K, _MM_HINT_T0); |
| 78 | + } |
| 79 | + __m512i b32 = _mm512_cvtepi8_epi32(b8); |
| 80 | + vb[col] = _mm512_cvtepi32_ps(b32); |
| 81 | + vb[col] = _mm512_mul_ps(vb[col], scale[col]); |
| 82 | + } |
| 83 | + |
| 84 | + constexpr int idx = row * COLS + col; |
| 85 | + vc[idx] = _mm512_fmadd_ps(va, vb[col], vc[idx]); |
| 86 | + }; |
| 87 | + |
| 88 | + for (int k = 0; k < K; k += 16) { |
| 89 | + compile_time_for<ROWS * COLS>::op(compute, k); |
| 90 | + } |
| 91 | + |
| 92 | + auto storec = [&](auto i) { |
| 93 | + constexpr int row = i / COLS; |
| 94 | + constexpr int col = i % COLS; |
| 95 | + C[row * ldc + col] = static_cast<BFloat16>(_mm512_reduce_add_ps(vc[i])); |
| 96 | + }; |
| 97 | + compile_time_for<ROWS * COLS>::op(storec); |
| 98 | +} |
| 99 | + |
| 100 | +#elif defined(CPU_CAPABILITY_AVX2) && !defined(_MSC_VER) |
| 101 | + |
| 102 | +static inline float _mm256_reduce_add_ps(__m256& v) { |
| 103 | + __m256 v1 = _mm256_permute2f128_ps(v, v, 0x1); |
| 104 | + v = _mm256_add_ps(v, v1); |
| 105 | + v1 = _mm256_shuffle_ps(v, v, 0x4E); |
| 106 | + v = _mm256_add_ps(v, v1); |
| 107 | + v1 = _mm256_shuffle_ps(v, v, 0xB1); |
| 108 | + v = _mm256_add_ps(v, v1); |
| 109 | + return _mm256_cvtss_f32(v); |
| 110 | +} |
| 111 | + |
| 112 | +template <int BLOCK_M, int BLOCK_N> |
| 113 | +inline void tinygemm_kernel( |
| 114 | + const BFloat16* RESTRICT A, |
| 115 | + const int8_t* RESTRICT B, |
| 116 | + const BFloat16* RESTRICT scales, |
| 117 | + BFloat16* RESTRICT C, |
| 118 | + int lda, |
| 119 | + int ldb, |
| 120 | + int ldc, |
| 121 | + int K) { |
| 122 | + |
| 123 | + constexpr int ROWS = BLOCK_M; |
| 124 | + constexpr int COLS = BLOCK_N; |
| 125 | + |
| 126 | + const int PREFETCH_SIZE_K = 16 * 4; |
| 127 | + |
| 128 | + __m256 va; |
| 129 | + __m256 vb[COLS]; |
| 130 | + __m256 vc[ROWS * COLS]; |
| 131 | + __m256 scale[COLS]; |
| 132 | + |
| 133 | + auto load_scale = [&](int i) { |
| 134 | + float ss = static_cast<float>(scales[i]); |
| 135 | + scale[i] = _mm256_set1_ps(ss); |
| 136 | + }; |
| 137 | + compile_time_for<COLS>::op(load_scale); |
| 138 | + |
| 139 | + auto loadc = [&](auto i) { |
| 140 | + vc[i] = _mm256_setzero_ps(); |
| 141 | + }; |
| 142 | + compile_time_for<ROWS * COLS>::op(loadc); |
| 143 | + |
| 144 | + auto compute = [&](auto i, int k) { |
| 145 | + constexpr int row = i / COLS; |
| 146 | + constexpr int col = i % COLS; |
| 147 | + |
| 148 | + if constexpr (col == 0) { |
| 149 | + __m128i a16 = _mm_load_si128((__m128i*)(A + row * lda + k)); |
| 150 | + if (k + PREFETCH_SIZE_K < K) { |
| 151 | + _mm_prefetch(A + row * lda + k + PREFETCH_SIZE_K, _MM_HINT_T0); |
| 152 | + } |
| 153 | + vec::cvtbf16_fp32(a16, va); |
| 154 | + } |
| 155 | + |
| 156 | + if constexpr (row == 0) { |
| 157 | + __m128i b8 = _mm_loadu_si64((__m128i*)(B + col * ldb + k)); |
| 158 | + if (k + PREFETCH_SIZE_K < K) { |
| 159 | + _mm_prefetch(B + col * ldb + k + PREFETCH_SIZE_K, _MM_HINT_T0); |
| 160 | + } |
| 161 | + __m256i b32 = _mm256_cvtepi8_epi32(b8); |
| 162 | + vb[col] = _mm256_cvtepi32_ps(b32); |
| 163 | + vb[col] = _mm256_mul_ps(vb[col], scale[col]); |
| 164 | + } |
| 165 | + |
| 166 | + constexpr int idx = row * COLS + col; |
| 167 | + vc[idx] = _mm256_fmadd_ps(va, vb[col], vc[idx]); |
| 168 | + }; |
| 169 | + |
| 170 | + for (int k = 0; k < K; k += 8) { |
| 171 | + compile_time_for<ROWS * COLS>::op(compute, k); |
| 172 | + } |
| 173 | + |
| 174 | + auto storec = [&](auto i) { |
| 175 | + constexpr int row = i / COLS; |
| 176 | + constexpr int col = i % COLS; |
| 177 | + C[row * ldc + col] = static_cast<BFloat16>(_mm256_reduce_add_ps(vc[i])); |
| 178 | + }; |
| 179 | + compile_time_for<ROWS * COLS>::op(storec); |
| 180 | +} |
| 181 | + |
| 182 | +#else |
| 183 | + |
| 184 | +// non-vectorized version |
| 185 | +template <int BLOCK_M, int BLOCK_N> |
| 186 | +inline void tinygemm_kernel( |
| 187 | + const BFloat16* RESTRICT A, |
| 188 | + const int8_t* RESTRICT B, |
| 189 | + const BFloat16* RESTRICT scales, |
| 190 | + BFloat16* RESTRICT C, |
| 191 | + int lda, |
| 192 | + int ldb, |
| 193 | + int ldc, |
| 194 | + int K) { |
| 195 | + |
| 196 | + for (const auto m : c10::irange(BLOCK_M)) { |
| 197 | + for (const auto n : c10::irange(BLOCK_N)) { |
| 198 | + float c_val = 0; |
| 199 | + float scale_val = static_cast<float>(scales[n]); |
| 200 | + for (const auto k : c10::irange(K)) { |
| 201 | + float a_val = static_cast<float>(A[m * lda + k]); |
| 202 | + float b_val = static_cast<float>(B[n * ldb + k]); |
| 203 | + c_val += a_val * (b_val * scale_val); |
| 204 | + } |
| 205 | + C[m * ldc + n] = c_val; |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +#endif |
| 211 | + |
| 212 | +#define LAUNCH_TINYGEMM_KERNEL(MB_SIZE, NB_SIZE) \ |
| 213 | + tinygemm_kernel<MB_SIZE, NB_SIZE>( \ |
| 214 | + A_ptr, B_ptr, S_ptr, C_ptr, \ |
| 215 | + K, K, N, K); |
| 216 | + |
| 217 | +#define LAUNCH_TINYGEMM_NB_SIZE(MB_SIZE) \ |
| 218 | + switch (nb_size) { \ |
| 219 | + case 1: \ |
| 220 | + LAUNCH_TINYGEMM_KERNEL(MB_SIZE, 1); \ |
| 221 | + break; \ |
| 222 | + case 2: \ |
| 223 | + LAUNCH_TINYGEMM_KERNEL(MB_SIZE, 2); \ |
| 224 | + break; \ |
| 225 | + case 3: \ |
| 226 | + LAUNCH_TINYGEMM_KERNEL(MB_SIZE, 3); \ |
| 227 | + break; \ |
| 228 | + case 4: \ |
| 229 | + LAUNCH_TINYGEMM_KERNEL(MB_SIZE, 4); \ |
| 230 | + break; \ |
| 231 | + default: \ |
| 232 | + TORCH_CHECK(false, "Unsupported n block size: ", nb_size); \ |
| 233 | + break; \ |
| 234 | + } |
| 235 | + |
| 236 | +void int8pack_mm_kernel( |
| 237 | + const Tensor& C, |
| 238 | + const Tensor& A, |
| 239 | + const Tensor& B, |
| 240 | + const Tensor& scales) { |
| 241 | + |
| 242 | + const BFloat16* A_data = A.data_ptr<BFloat16>(); |
| 243 | + const int8_t* B_data = B.data_ptr<int8_t>(); |
| 244 | + BFloat16* C_data = C.data_ptr<BFloat16>(); |
| 245 | + const BFloat16* S_data = scales.data_ptr<BFloat16>(); |
| 246 | + |
| 247 | + int M = A.size(0); |
| 248 | + int N = B.size(0); |
| 249 | + int K = A.size(1); |
| 250 | + |
| 251 | + constexpr int BLOCK_M = 4; |
| 252 | + constexpr int BLOCK_N = 4; |
| 253 | + |
| 254 | + const int MB = (M + BLOCK_M - 1) / BLOCK_M; |
| 255 | + const int NB = (N + BLOCK_N - 1) / BLOCK_N; |
| 256 | + |
| 257 | + at::parallel_for(0, MB * NB, 0, [&](int begin, int end) { |
| 258 | + int mb{0}, nb{0}; |
| 259 | + data_index_init(begin, mb, MB, nb, NB); |
| 260 | + |
| 261 | + for (const auto i : c10::irange(begin, end)) { |
| 262 | + (void)i; |
| 263 | + |
| 264 | + int mb_start = mb * BLOCK_M; |
| 265 | + int mb_size = std::min(BLOCK_M, M - mb_start); |
| 266 | + int nb_start = nb * BLOCK_N; |
| 267 | + int nb_size = std::min(BLOCK_N, N - nb_start); |
| 268 | + |
| 269 | + const BFloat16* A_ptr = A_data + mb_start * K; |
| 270 | + const int8_t* B_ptr = B_data + nb_start * K; |
| 271 | + const BFloat16* S_ptr = S_data + nb_start; |
| 272 | + BFloat16* C_ptr = C_data + mb_start * N + nb_start; |
| 273 | + |
| 274 | + switch (mb_size) { |
| 275 | + case 1: |
| 276 | + LAUNCH_TINYGEMM_NB_SIZE(1); |
| 277 | + break; |
| 278 | + case 2: |
| 279 | + LAUNCH_TINYGEMM_NB_SIZE(2); |
| 280 | + break; |
| 281 | + case 3: |
| 282 | + LAUNCH_TINYGEMM_NB_SIZE(3); |
| 283 | + break; |
| 284 | + case 4: |
| 285 | + LAUNCH_TINYGEMM_NB_SIZE(4); |
| 286 | + break; |
| 287 | + default: |
| 288 | + TORCH_CHECK(false, "Unsupported m block size: ", mb_size); |
| 289 | + } |
| 290 | + |
| 291 | + // move to the next index |
| 292 | + data_index_step(mb, MB, nb, NB); |
| 293 | + } |
| 294 | + }); |
| 295 | +} |
| 296 | + |
| 297 | +} // anonymous namespace |
| 298 | + |
| 299 | +ALSO_REGISTER_AVX512_DISPATCH(int8pack_mm_stub, &int8pack_mm_kernel); |
| 300 | + |
| 301 | +} // at::native |
0 commit comments