| 1 | #include "marlin.cuh" |
| 2 | |
| 3 | #include <torch/csrc/stable/accelerator.h> |
| 4 | #include <torch/csrc/stable/library.h> |
| 5 | #include <torch/csrc/stable/ops.h> |
| 6 | #include <torch/csrc/stable/tensor.h> |
| 7 | #include <torch/headeronly/core/ScalarType.h> |
| 8 | #include <torch/headeronly/util/Exception.h> |
| 9 | |
| 10 | #include "libtorch_stable/torch_utils.h" |
| 11 | |
| 12 | namespace marlin { |
| 13 | |
| 14 | template <int const num_threads, int const num_bits, bool is_a_8bit> |
| 15 | __global__ void awq_marlin_repack_kernel( |
| 16 | uint32_t const* __restrict__ b_q_weight_ptr, uint32_t* __restrict__ out_ptr, |
| 17 | int size_k, int size_n) { |
| 18 | constexpr int pack_factor = 32 / num_bits; |
| 19 | |
| 20 | constexpr int target_tile_n_size = tile_n_size / (is_a_8bit ? 2 : 1); |
| 21 | constexpr int target_tile_k_size = tile_k_size * (is_a_8bit ? 2 : 1); |
| 22 | int k_tiles = size_k / target_tile_k_size; |
| 23 | int n_tiles = size_n / target_tile_n_size; |
| 24 | int block_k_tiles = div_ceil(k_tiles, gridDim.x); |
| 25 | |
| 26 | auto start_k_tile = blockIdx.x * block_k_tiles; |
| 27 | if (start_k_tile >= k_tiles) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | int finish_k_tile = min(start_k_tile + block_k_tiles, k_tiles); |
| 32 | |
| 33 | // Wait until the next thread tile has been loaded to shared memory. |
| 34 | auto wait_for_stage = [&]() { |
| 35 | // We only have `stages - 2` active fetches since we are double buffering |
| 36 | // and can only issue the next fetch when it is guaranteed that the previous |
| 37 | // shared memory load is fully complete (as it may otherwise be |
| 38 | // overwritten). |
| 39 | cp_async_wait<repack_stages - 2>(); |
| 40 | __syncthreads(); |
| 41 | }; |
| 42 | |
| 43 | extern __shared__ int4 sh[]; |
| 44 | |
| 45 | constexpr int tile_n_ints = target_tile_n_size / pack_factor; |
| 46 | |
| 47 | constexpr int stage_n_threads = tile_n_ints / 4; |
| 48 | constexpr int stage_k_threads = target_tile_k_size; |
| 49 | constexpr int stage_size = stage_k_threads * stage_n_threads; |
| 50 | |
| 51 | auto fetch_to_shared = [&](int pipe, int k_tile_id, int n_tile_id) { |
| 52 | if (n_tile_id >= n_tiles) { |
| 53 | cp_async_fence(); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | int first_n = n_tile_id * target_tile_n_size; |
| 58 | int first_n_packed = first_n / pack_factor; |
| 59 | |
| 60 | int4* sh_ptr = sh + stage_size * pipe; |
| 61 | |
| 62 | if (threadIdx.x < stage_size) { |
| 63 | auto k_id = threadIdx.x / stage_n_threads; |
| 64 | auto n_id = threadIdx.x % stage_n_threads; |
| 65 | |
| 66 | int first_k = k_tile_id * target_tile_k_size; |
| 67 | |
| 68 | cp_async4(&sh_ptr[k_id * stage_n_threads + n_id], |
| 69 | reinterpret_cast<int4 const*>( |
| 70 | &(b_q_weight_ptr[(first_k + k_id) * (size_n / pack_factor) + |
| 71 | first_n_packed + (n_id * 4)]))); |
| 72 | } |
| 73 | |
| 74 | cp_async_fence(); |
| 75 | }; |
| 76 | |
| 77 | auto repack_tile = [&](int pipe, int k_tile_id, int n_tile_id) { |
| 78 | if (n_tile_id >= n_tiles) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | auto warp_id = threadIdx.x / 32; |
| 83 | auto th_id = threadIdx.x % 32; |
| 84 | |
| 85 | if (warp_id >= 4) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | int tc_col = th_id / 4; |
| 90 | int tc_row = (th_id % 4) * (is_a_8bit ? 4 : 2); |
| 91 | |
| 92 | constexpr int tc_offsets[4] = {0, 1, 8, 9}; |
| 93 | |
| 94 | int cur_n = (warp_id / (is_a_8bit ? 2 : 1)) * 16 + tc_col; |
| 95 | int cur_n_packed = cur_n / pack_factor; |
| 96 | int cur_n_pos = cur_n % pack_factor; |
| 97 | |
| 98 | constexpr int sh_stride = tile_n_ints; |
| 99 | constexpr uint32_t mask = (1 << num_bits) - 1; |
| 100 | |
| 101 | int4* sh_stage_ptr = sh + stage_size * pipe; |
| 102 | uint32_t* sh_stage_int_ptr = reinterpret_cast<uint32_t*>(sh_stage_ptr); |
| 103 | |
| 104 | // Undo interleaving |
| 105 | int cur_n_pos_unpacked; |
| 106 | if constexpr (num_bits == 4) { |
| 107 | constexpr int undo_pack[8] = {0, 4, 1, 5, 2, 6, 3, 7}; |
| 108 | cur_n_pos_unpacked = undo_pack[cur_n_pos]; |
| 109 | } else { |
| 110 | constexpr int undo_pack[4] = {0, 2, 1, 3}; |
| 111 | cur_n_pos_unpacked = undo_pack[cur_n_pos]; |
| 112 | } |
| 113 | |
| 114 | uint32_t vals[8]; |
| 115 | #pragma unroll |
| 116 | for (int i = 0; i < 4; i++) { |
| 117 | if constexpr (is_a_8bit) { |
| 118 | int cur_elem = tc_row + i; |
| 119 | |
| 120 | int packed_src_0 = |
| 121 | sh_stage_int_ptr[cur_n_packed + (8 / pack_factor) * (warp_id % 2) + |
| 122 | sh_stride * cur_elem]; |
| 123 | int packed_src_1 = |
| 124 | sh_stage_int_ptr[cur_n_packed + (8 / pack_factor) * (warp_id % 2) + |
| 125 | sh_stride * (cur_elem + 16)]; |
| 126 | |
| 127 | vals[i] = (packed_src_0 >> (cur_n_pos_unpacked * num_bits)) & mask; |
| 128 | vals[4 + i] = (packed_src_1 >> (cur_n_pos_unpacked * num_bits)) & mask; |
| 129 | } else { |
| 130 | int cur_elem = tc_row + tc_offsets[i]; |
| 131 | |
| 132 | int packed_src_0 = |
| 133 | sh_stage_int_ptr[cur_n_packed + sh_stride * cur_elem]; |
| 134 | int packed_src_1 = sh_stage_int_ptr[cur_n_packed + (8 / pack_factor) + |
| 135 | sh_stride * cur_elem]; |
| 136 | |
| 137 | vals[i] = (packed_src_0 >> (cur_n_pos_unpacked * num_bits)) & mask; |
| 138 | vals[4 + i] = (packed_src_1 >> (cur_n_pos_unpacked * num_bits)) & mask; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | constexpr int tile_size = |
| 143 | target_tile_k_size * target_tile_n_size / pack_factor; |
| 144 | int out_offset = (k_tile_id * n_tiles + n_tile_id) * tile_size; |
| 145 | |
| 146 | // Result of: |
| 147 | // https://github.com/NVIDIA/FasterTransformer/blob/main/src/fastertransformer/cutlass_extensions/include/cutlass_extensions/interleaved_numeric_conversion.h |
| 148 | if constexpr (!is_a_8bit && num_bits == 4) { |
| 149 | int pack_idx[8] = {0, 2, 4, 6, 1, 3, 5, 7}; |
| 150 | |
| 151 | uint32_t res = 0; |
| 152 | #pragma unroll |
| 153 | for (int i = 0; i < 8; i++) { |
| 154 | res |= vals[pack_idx[i]] << (i * 4); |
| 155 | } |
| 156 | |
| 157 | out_ptr[out_offset + th_id * 4 + warp_id] = res; |
| 158 | |
| 159 | } else if constexpr (is_a_8bit && num_bits == 4) { |
| 160 | int pack_idx[8] = {0, 4, 1, 5, 2, 6, 3, 7}; |
| 161 | |
| 162 | uint32_t res = 0; |
| 163 | #pragma unroll |
| 164 | for (int i = 0; i < 8; i++) { |
| 165 | res |= vals[pack_idx[i]] << (i * 4); |
| 166 | } |
| 167 | |
| 168 | out_ptr[out_offset + th_id * 4 + warp_id] = res; |
| 169 | |
| 170 | } else { |
| 171 | constexpr int pack_idx[4] = {0, 2, 1, 3}; |
| 172 | |
| 173 | uint32_t res1 = 0; |
| 174 | uint32_t res2 = 0; |
| 175 | #pragma unroll |
| 176 | for (int i = 0; i < 4; i++) { |
| 177 | const int ii = is_a_8bit ? i : pack_idx[i]; |
| 178 | res1 |= vals[ii] << (i * 8); |
| 179 | res2 |= vals[4 + ii] << (i * 8); |
| 180 | } |
| 181 | |
| 182 | out_ptr[out_offset + th_id * 8 + (warp_id * 2) + 0] = res1; |
| 183 | out_ptr[out_offset + th_id * 8 + (warp_id * 2) + 1] = res2; |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | auto start_pipes = [&](int k_tile_id, int n_tile_id) { |
| 188 | #pragma unroll |
| 189 | for (int pipe = 0; pipe < repack_stages - 1; pipe++) { |
| 190 | fetch_to_shared(pipe, k_tile_id, n_tile_id + pipe); |
| 191 | } |
| 192 | |
| 193 | wait_for_stage(); |
| 194 | }; |
| 195 | #pragma unroll |
| 196 | for (int k_tile_id = start_k_tile; k_tile_id < finish_k_tile; k_tile_id++) { |
| 197 | int n_tile_id = 0; |
| 198 | |
| 199 | start_pipes(k_tile_id, n_tile_id); |
| 200 | |
| 201 | while (n_tile_id < n_tiles) { |
| 202 | #pragma unroll |
| 203 | for (int pipe = 0; pipe < repack_stages; pipe++) { |
| 204 | fetch_to_shared((pipe + repack_stages - 1) % repack_stages, k_tile_id, |
| 205 | n_tile_id + pipe + repack_stages - 1); |
| 206 | repack_tile(pipe, k_tile_id, n_tile_id + pipe); |
| 207 | wait_for_stage(); |
| 208 | } |
| 209 | n_tile_id += repack_stages; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } // namespace marlin |
| 215 | |
| 216 | #define CALL_IF(NUM_BITS, IS_A_8BIT) \ |
| 217 | else if (num_bits == NUM_BITS && is_a_8bit == IS_A_8BIT) { \ |
| 218 | cudaFuncSetAttribute( \ |
| 219 | marlin::awq_marlin_repack_kernel<marlin::repack_threads, NUM_BITS, \ |
| 220 | IS_A_8BIT>, \ |
| 221 | cudaFuncAttributeMaxDynamicSharedMemorySize, max_shared_mem); \ |
| 222 | marlin::awq_marlin_repack_kernel<marlin::repack_threads, NUM_BITS, \ |
| 223 | IS_A_8BIT> \ |
| 224 | <<<blocks, marlin::repack_threads, max_shared_mem, stream>>>( \ |
| 225 | b_q_weight_ptr, out_ptr, size_k, size_n); \ |
| 226 | } |
| 227 | |
| 228 | torch::stable::Tensor awq_marlin_repack(torch::stable::Tensor& b_q_weight, |
| 229 | int64_t size_k, int64_t size_n, |
| 230 | int64_t num_bits, bool is_a_8bit) { |
| 231 | // Verify compatibility with marlin tile of 16x64 |
| 232 | STD_TORCH_CHECK(size_k % marlin::tile_k_size == 0, "size_k = ", size_k, |
| 233 | " is not divisible by tile_k_size = ", marlin::tile_k_size); |
| 234 | STD_TORCH_CHECK(size_n % marlin::tile_n_size == 0, "size_n = ", size_n, |
| 235 | " is not divisible by tile_n_size = ", marlin::tile_n_size); |
| 236 | |
| 237 | STD_TORCH_CHECK(num_bits == 4 || num_bits == 8, |
| 238 | "num_bits must be 4 or 8. Got = ", num_bits); |
| 239 | int const pack_factor = 32 / num_bits; |
| 240 | |
| 241 | // Verify B |
| 242 | STD_TORCH_CHECK(b_q_weight.size(0) == size_k, |
| 243 | "b_q_weight.size(0) = ", b_q_weight.size(0), |
| 244 | " is not size_k = ", size_k); |
| 245 | STD_TORCH_CHECK((size_n / pack_factor) == b_q_weight.size(1), |
| 246 | "Shape mismatch: b_q_weight.size(1) = ", b_q_weight.size(1), |
| 247 | ", size_n = ", size_n, ", pack_factor = ", pack_factor); |
| 248 | |
| 249 | // Verify device and strides |
| 250 | STD_TORCH_CHECK(b_q_weight.is_cuda(), "b_q_weight is not on GPU"); |
| 251 | STD_TORCH_CHECK(b_q_weight.is_contiguous(), "b_q_weight is not contiguous"); |
| 252 | STD_TORCH_CHECK( |
| 253 | b_q_weight.scalar_type() == torch::headeronly::ScalarType::Int, |
| 254 | "b_q_weight type is not kInt"); |
| 255 | |
| 256 | const int32_t device_index = b_q_weight.get_device_index(); |
| 257 | torch::stable::accelerator::DeviceGuard device_guard(device_index); |
| 258 | const cudaStream_t stream = get_current_cuda_stream(device_index); |
| 259 | |
| 260 | // Alloc buffers |
| 261 | torch::stable::Tensor out = torch::stable::empty( |
| 262 | {size_k / marlin::tile_size, size_n * marlin::tile_size / pack_factor}, |
| 263 | b_q_weight.scalar_type(), std::nullopt, b_q_weight.device()); |
| 264 | |
| 265 | // Get ptrs |
| 266 | uint32_t const* b_q_weight_ptr = |
| 267 | reinterpret_cast<uint32_t const*>(b_q_weight.const_data_ptr()); |
| 268 | uint32_t* out_ptr = reinterpret_cast<uint32_t*>(out.mutable_data_ptr()); |
| 269 | |
| 270 | int blocks; |
| 271 | cudaDeviceGetAttribute(&blocks, cudaDevAttrMultiProcessorCount, device_index); |
| 272 | |
| 273 | int max_shared_mem = 0; |
| 274 | cudaDeviceGetAttribute(&max_shared_mem, |
| 275 | cudaDevAttrMaxSharedMemoryPerBlockOptin, device_index); |
| 276 | STD_TORCH_CHECK(max_shared_mem > 0); |
| 277 | |
| 278 | if (false) { |
| 279 | } |
| 280 | CALL_IF(4, false) |
| 281 | CALL_IF(8, false) |
| 282 | CALL_IF(4, true) |
| 283 | CALL_IF(8, true) |
| 284 | else { |
| 285 | STD_TORCH_CHECK(false, "Unsupported repack config: num_bits = ", num_bits, |
| 286 | ", is_a_8bit = ", is_a_8bit); |
| 287 | } |
| 288 | |
| 289 | return out; |
| 290 | } |
| 291 | |
| 292 | STABLE_TORCH_LIBRARY_IMPL(_C, CUDA, m) { |
| 293 | m.impl("awq_marlin_repack", TORCH_BOX(&awq_marlin_repack)); |
| 294 | } |