|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <ATen/ATen.h> |
| 10 | +#include <ATen/cuda/CUDAContext.h> |
| 11 | +#include <c10/core/ScalarType.h> |
| 12 | +#include <c10/cuda/CUDAGuard.h> |
| 13 | +#include <cuda_bf16.h> |
| 14 | + |
| 15 | +#include "include/fast_gemv.cuh" |
| 16 | + |
| 17 | +namespace fbgemm_gpu { |
| 18 | + |
| 19 | +// The heuristics are derived by sweeping over 4 different |
| 20 | +// problem sizes we care about and selected the best elapsed time/bw |
| 21 | +// combination. See more in |
| 22 | +// deeplearning/fbgemm/fbgemm_gpu/experimental/gen_ai/src/quantize/fast_gemv/sweep_utils.py |
| 23 | +dim3 get_best_block_dim(int m, int n, int k) { |
| 24 | + if (m == 1 && n == 1280 && k == 8192) { |
| 25 | + return dim3(128, 4); |
| 26 | + } else if (m == 1 && n == 8192 && k == 1024) { |
| 27 | + return dim3(32, 8); |
| 28 | + } else if (m == 1 && n == 7168 && k == 8192) { |
| 29 | + return dim3(256, 1); |
| 30 | + } else if (m == 1 && n == 8192 && k == 3584) { |
| 31 | + return dim3(64, 2); |
| 32 | + } else { |
| 33 | + // Default block dimensions |
| 34 | + return dim3(32, 4); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +at::Tensor bf16_fast_gemv(at::Tensor X, at::Tensor W) { |
| 39 | + // X: M x K |
| 40 | + // W: N x K |
| 41 | + auto m = X.size(0); |
| 42 | + auto n = W.size(0); |
| 43 | + auto k = W.size(1); |
| 44 | + |
| 45 | + TORCH_CHECK(X.is_cuda() && X.is_contiguous()); |
| 46 | + TORCH_CHECK(W.is_cuda() && W.is_contiguous()); |
| 47 | + |
| 48 | + dim3 block_dim = get_best_block_dim(m, n, k); |
| 49 | + |
| 50 | + TORCH_CHECK( |
| 51 | + n % block_dim.y == 0, |
| 52 | + "Invalid block dimensions: n (", |
| 53 | + n, |
| 54 | + ") must be divisible by block_dim.y (", |
| 55 | + block_dim.y, |
| 56 | + "). Received n: ", |
| 57 | + n, |
| 58 | + ", block_dim.y: ", |
| 59 | + block_dim.y, |
| 60 | + " Please either use a `n` which is divisible by `block_dim.y`, or update " |
| 61 | + "`get_best_block_dim()` heuristics to choose another `block_dim.y`. " |
| 62 | + " All current params - m: ", |
| 63 | + m, |
| 64 | + ", n: ", |
| 65 | + n, |
| 66 | + ", k: ", |
| 67 | + k, |
| 68 | + ", block_dim.x: ", |
| 69 | + block_dim.x, |
| 70 | + ", block_dim.y: ", |
| 71 | + block_dim.y, |
| 72 | + "."); |
| 73 | + TORCH_CHECK( |
| 74 | + k % block_dim.x == 0, |
| 75 | + "Invalid block dimensions: k (", |
| 76 | + k, |
| 77 | + ") must be divisible by block_dim.x (", |
| 78 | + block_dim.x, |
| 79 | + "). Received k: ", |
| 80 | + k, |
| 81 | + ", block_dim.x: ", |
| 82 | + block_dim.x, |
| 83 | + " Please either use a `k` which is divisible by `block_dim.x`, or update " |
| 84 | + "`get_best_block_dim()` heuristics to choose another `block_dim.x`." |
| 85 | + " All current params - m: ", |
| 86 | + m, |
| 87 | + ", n: ", |
| 88 | + n, |
| 89 | + ", k: ", |
| 90 | + k, |
| 91 | + ", block_dim.x: ", |
| 92 | + block_dim.x, |
| 93 | + ", block_dim.y: ", |
| 94 | + block_dim.y, |
| 95 | + "."); |
| 96 | + TORCH_CHECK( |
| 97 | + (k / block_dim.x) % 8 == 0, |
| 98 | + "Invalid num_per_thread: (", |
| 99 | + k / block_dim.x, |
| 100 | + ") must be divisible by 8.", |
| 101 | + " Received k: ", |
| 102 | + k, |
| 103 | + ", block_dim.x: ", |
| 104 | + block_dim.x, |
| 105 | + " Please either use a `k` that `k / block_dim.x` that is divisble by 8, or update " |
| 106 | + "`get_best_block_dim()` heuristics to choose another `block_dim.x`." |
| 107 | + " All current params - m: ", |
| 108 | + m, |
| 109 | + ", n: ", |
| 110 | + n, |
| 111 | + ", k: ", |
| 112 | + k, |
| 113 | + ", block_dim.x: ", |
| 114 | + block_dim.x, |
| 115 | + ", block_dim.y: ", |
| 116 | + block_dim.y, |
| 117 | + "."); |
| 118 | + |
| 119 | + dim3 grid_dim(1, n / block_dim.y); |
| 120 | + unsigned int num_per_thread = k / block_dim.x; |
| 121 | + |
| 122 | + auto stream = at::cuda::getCurrentCUDAStream(); |
| 123 | + |
| 124 | + auto Y = at::empty({m, n}, X.options().dtype(at::kBFloat16)); |
| 125 | + |
| 126 | + gemv_bf16<<<grid_dim, block_dim, 0, stream>>>( |
| 127 | + reinterpret_cast<__nv_bfloat16*>(W.data_ptr()), // mat |
| 128 | + reinterpret_cast<__nv_bfloat16*>(X.data_ptr()), // vec |
| 129 | + reinterpret_cast<__nv_bfloat16*>(Y.data_ptr()), // res |
| 130 | + k, |
| 131 | + num_per_thread); |
| 132 | + |
| 133 | + C10_CUDA_KERNEL_LAUNCH_CHECK(); |
| 134 | + |
| 135 | + return Y; |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace fbgemm_gpu |
0 commit comments