| 1 | #pragma once |
| 2 | |
| 3 | #include <hip/hip_runtime.h> |
| 4 | #include "base.h" |
| 5 | #include <hip/hip_fp16.h> |
| 6 | |
| 7 | namespace quickreduce { |
| 8 | |
| 9 | struct CodecBase { |
| 10 | const int thread; |
| 11 | const int rank; |
| 12 | const int group_leader; |
| 13 | __quickreduce_device_inline__ CodecBase(int thread, int rank) |
| 14 | : thread(thread), |
| 15 | rank(rank), |
| 16 | group_leader((threadIdx.x / kThreadGroupSize) * kThreadGroupSize) { |
| 17 | set_fp16_ovfl(true); |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | // Default full precision codec. |
| 22 | template <typename T, int world_size> |
| 23 | struct CodecFP : public CodecBase { |
| 24 | static constexpr int kWorldSize = world_size; |
| 25 | static constexpr int kRankAtoms = kAtoms / kWorldSize; |
| 26 | |
| 27 | // Codec tile size process by this workgroup. |
| 28 | // Each thread processes atoms of f16x8_t (16B). |
| 29 | static constexpr int kRankTransmittedTileSize = |
| 30 | kBlockSize * kRankAtoms * sizeof(int32x4_t); |
| 31 | static_assert(kRankTransmittedTileSize % 16 == 0, |
| 32 | "kRankTransmittedTileSize must be 16B aligned."); |
| 33 | |
| 34 | // Total tile size for the collective communication. |
| 35 | static constexpr int kTransmittedTileSize = |
| 36 | kRankTransmittedTileSize * kWorldSize; |
| 37 | |
| 38 | __quickreduce_device_inline__ CodecFP(int thread, int rank) |
| 39 | : CodecBase(thread, rank) {} |
| 40 | |
| 41 | __quickreduce_device_inline__ void send(int32x4_t* __restrict__ send_buffer, |
| 42 | const int32x4_t* __restrict__ data) { |
| 43 | for (int i = 0; i < kRankAtoms; i++) { |
| 44 | __builtin_nontemporal_store(data[i], send_buffer + thread); |
| 45 | send_buffer += kAtomStride; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | __quickreduce_device_inline__ void recv(int32x4_t** __restrict__ recv_buffer, |
| 50 | int32x4_t* __restrict__ data) { |
| 51 | for (int i = 0; i < kRankAtoms; i++) { |
| 52 | data[i] = __builtin_nontemporal_load(*recv_buffer + thread); |
| 53 | *recv_buffer += kAtomStride; |
| 54 | } |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // Int4 symmetric quantization codec. |
| 59 | // We quantize the FP16 data to block-scaled Int4 in blocks of 4 * |
| 60 | // kThreadGroupSize. |
| 61 | template <typename T, int world_size> |
| 62 | struct CodecQ4 : public CodecBase { |
| 63 | static constexpr int kWorldSize = world_size; |
| 64 | |
| 65 | // Codec tile size process by this workgroup. |
| 66 | // Each threads processes a fragment of fp16x8_t (16B), |
| 67 | // into a int4x8_t (4B) and a fp16 scale shared among 32 values. |
| 68 | static constexpr int kRankAtoms = kAtoms / kWorldSize; |
| 69 | static constexpr int kRankTileStride = 1152; |
| 70 | static constexpr int kRankTileScaleOffset = 1024; |
| 71 | static constexpr int kRankTransmittedTileSize = kRankTileStride * kRankAtoms; |
| 72 | static_assert(kRankTransmittedTileSize % 16 == 0, |
| 73 | "kRankTransmittedTileSize must be 16B aligned."); |
| 74 | |
| 75 | static constexpr int kRankBufferTileStride = |
| 76 | kRankTileStride / sizeof(int32x4_t); |
| 77 | |
| 78 | // Total tile size for the collective communication. |
| 79 | static constexpr int kTransmittedTileSize = |
| 80 | kRankTransmittedTileSize * kWorldSize; |
| 81 | |
| 82 | // Constants configuration |
| 83 | |
| 84 | // {-1/8.0h, -1/8.0h}, f16x2_t |
| 85 | static constexpr int kScaleFactor = |
| 86 | std::is_same<T, half>::value ? 0xB000B000 : 0xBE00BE00; |
| 87 | |
| 88 | // {1e-7, 1e-7}, f16x2_t |
| 89 | static constexpr int kScaleEpsilon = |
| 90 | std::is_same<T, half>::value ? 0x00010001 : 0x33D733D7; |
| 91 | |
| 92 | // {-8, -8}, f16x2_t |
| 93 | static constexpr int kRangeMin = |
| 94 | std::is_same<T, half>::value ? 0xC800C800 : 0xC100C100; |
| 95 | |
| 96 | // {+7, +7}, f16x2_t |
| 97 | static constexpr int kRangeMax = |
| 98 | std::is_same<T, half>::value ? 0x47004700 : 0x40E040E0; |
| 99 | |
| 100 | // {+8, +8}, int16x2_t |
| 101 | static constexpr int kRangeBias = 0x00080008; |
| 102 | |
| 103 | __quickreduce_device_inline__ CodecQ4(int thread, int rank) |
| 104 | : CodecBase(thread, rank) {} |
| 105 | |
| 106 | __quickreduce_device_inline__ void send(int32x4_t* __restrict__ send_buffer, |
| 107 | const int32x4_t* __restrict__ data) { |
| 108 | for (int k = 0; k < kRankAtoms; k++) { |
| 109 | int32x4_t const atom = data[k]; |
| 110 | |
| 111 | // Compute the absolute maximum of the atom in the thread group |
| 112 | // In 2 blocks of values, upper/lower halves of the f16x2_t |
| 113 | int wblockmax = group_abs_max<T>(atom); |
| 114 | |
| 115 | // Derive scales |
| 116 | int decoding_scale; |
| 117 | int encoding_scale; |
| 118 | decoding_scale = packed_mul<T>(wblockmax, kScaleFactor); |
| 119 | encoding_scale = packed_add<T>(decoding_scale, kScaleEpsilon); |
| 120 | encoding_scale = packed_rcp<T>(encoding_scale); |
| 121 | |
| 122 | // Apply scales to get quantized values |
| 123 | int32x4_t w; |
| 124 | for (int i = 0; i < 4; i++) { |
| 125 | w[i] = packed_mul<T>(atom[i], encoding_scale); |
| 126 | w[i] = packed_max<T>(w[i], kRangeMin); |
| 127 | w[i] = packed_min<T>(w[i], kRangeMax); |
| 128 | } |
| 129 | |
| 130 | // Convert from f16x2_t to uint16x2_t |
| 131 | int32x4_t q; |
| 132 | { |
| 133 | int16_t* qi = reinterpret_cast<int16_t*>(&q); |
| 134 | T* wh = reinterpret_cast<T*>(&w); |
| 135 | for (int i = 0; i < 8; i++) qi[i] = (int16_t)rintf(T2float_cast(wh[i])); |
| 136 | |
| 137 | for (int i = 0; i < 4; i++) { |
| 138 | q[i] = packed_add<int16_t>(q[i], kRangeBias); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Pack 8 x q4 into int32_t |
| 143 | int qw = q[0] | (q[1] << 4) | (q[2] << 8) | (q[3] << 12); |
| 144 | |
| 145 | // Write quantized atom to send_buffer |
| 146 | // note: only the group leader stores the scale |
| 147 | uint8_t* atom_ptr = |
| 148 | reinterpret_cast<uint8_t*>(send_buffer + k * kRankBufferTileStride); |
| 149 | int32_t* qw_ptr = reinterpret_cast<int32_t*>(atom_ptr) + thread; |
| 150 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 151 | (thread / 8); |
| 152 | |
| 153 | __builtin_nontemporal_store(qw, qw_ptr); |
| 154 | if (threadIdx.x == group_leader) { |
| 155 | __builtin_nontemporal_store(decoding_scale, qs_ptr); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | __quickreduce_device_inline__ void recv(int32x4_t** __restrict__ recv_buffer, |
| 161 | int32x4_t* __restrict__ data) { |
| 162 | for (int k = 0; k < kRankAtoms; k++) { |
| 163 | // Directly read quantized atom from recv_buffer |
| 164 | uint8_t* atom_ptr = reinterpret_cast<uint8_t*>(*recv_buffer); |
| 165 | int32_t* qw_ptr = reinterpret_cast<int32_t*>(atom_ptr) + thread; |
| 166 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 167 | (thread / 8); |
| 168 | |
| 169 | int32_t qw = __builtin_nontemporal_load(qw_ptr); |
| 170 | int qs = __builtin_nontemporal_load(qs_ptr); |
| 171 | |
| 172 | *recv_buffer += kRankBufferTileStride; |
| 173 | |
| 174 | // Unpack q4 into f16x8_t |
| 175 | int32x4_t w; |
| 176 | { |
| 177 | static constexpr uint kMask000F = 0x000F000F; |
| 178 | static constexpr uint kHalf2_1024 = |
| 179 | 0x64006400; // {1024.0, 1024.0}, fp16x2_t |
| 180 | static uint constexpr kHalf2_1032 = |
| 181 | 0xE408E408; // {-1032.0, -1032.0}, fp16x2_t |
| 182 | |
| 183 | for (int i = 0; i < 4; i++) { |
| 184 | if constexpr (std::is_same<T, half>::value) { |
| 185 | int32_t q4 = ((qw >> (i * 4)) & kMask000F) | kHalf2_1024; |
| 186 | w[i] = packed_add<half>(q4, kHalf2_1032); |
| 187 | } else { |
| 188 | int32_t int16_2 = (qw >> (i * 4)) & kMask000F; |
| 189 | int16_t low = static_cast<int16_t>(int16_2 & 0xFFFF); |
| 190 | int16_t high = static_cast<int16_t>((int16_2 >> 16) & 0xFFFF); |
| 191 | nv_bfloat16 bf_low = __float2bfloat16(static_cast<float>(low)); |
| 192 | nv_bfloat16 bf_high = __float2bfloat16(static_cast<float>(high)); |
| 193 | nv_bfloat162 bf2 = __halves2bfloat162(bf_low, bf_high); |
| 194 | int32_t packed_bf16 = *reinterpret_cast<int32_t*>(&bf2); |
| 195 | w[i] = packed_add<nv_bfloat16>(packed_bf16, kRangeMin); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Apply decoding scales |
| 201 | for (int i = 0; i < 4; i++) { |
| 202 | w[i] = packed_mul<T>(w[i], qs); |
| 203 | } |
| 204 | |
| 205 | data[k] = w; |
| 206 | } |
| 207 | } |
| 208 | }; |
| 209 | |
| 210 | // Int3 symmetric quantization codec. |
| 211 | // We quantize the FP16 data to block-scaled Int3 in blocks of 4 * |
| 212 | // kThreadGroupSize. Uniform symmetric quantization (round-to-int + clip), |
| 213 | // matching the structure of CodecQ4. Signed range is [-4, +3]. |
| 214 | template <typename T, int world_size> |
| 215 | struct CodecQ3 : public CodecBase { |
| 216 | static constexpr int kWorldSize = world_size; |
| 217 | |
| 218 | // Layout per quantization block (32 values = 8 threads * 4 fp16x2 lanes): |
| 219 | // - each thread owns 8 values and writes: |
| 220 | // * q2 payload : 8 * 2 bits -> uint16 (2 bytes) |
| 221 | // * q1 payload : 8 * 1 bit -> uint8 (1 byte) |
| 222 | // - one scale is shared per 32 values and written by group leader. |
| 223 | // |
| 224 | // kRankTileStride is split as: |
| 225 | // [0 .. 511] : q2 payload region (256 threads * 2 bytes) |
| 226 | // [512 .. 767] : q1 payload region (256 threads * 1 byte) |
| 227 | // [768 .. 895] : scale region (32 groups * 4 bytes) |
| 228 | static constexpr int kRankAtoms = kAtoms / kWorldSize; |
| 229 | static constexpr int kRankTileStride = 896; |
| 230 | static constexpr int kRankTileQ1Offset = 512; |
| 231 | static constexpr int kRankTileScaleOffset = 768; |
| 232 | static constexpr int kRankTransmittedTileSize = kRankTileStride * kRankAtoms; |
| 233 | static_assert(kRankTransmittedTileSize % 16 == 0, |
| 234 | "kRankTransmittedTileSize must be 16B aligned."); |
| 235 | |
| 236 | static constexpr int kRankBufferTileStride = |
| 237 | kRankTileStride / sizeof(int32x4_t); |
| 238 | |
| 239 | static constexpr int kTransmittedTileSize = |
| 240 | kRankTransmittedTileSize * kWorldSize; |
| 241 | |
| 242 | // {-1/4.0h, -1/4.0h}, f16x2_t / bf16x2_t. Sign-flipped so absmax maps |
| 243 | // to -4; the sign cancels with decoding_scale on the recv side. |
| 244 | static constexpr int kScaleFactor = |
| 245 | std::is_same<T, half>::value ? 0xB400B400 : 0xBE80BE80; |
| 246 | |
| 247 | // {1e-7, 1e-7}, f16x2_t |
| 248 | static constexpr int kScaleEpsilon = |
| 249 | std::is_same<T, half>::value ? 0x00010001 : 0x33D733D7; |
| 250 | |
| 251 | // {-4, -4}, f16x2_t / bf16x2_t |
| 252 | static constexpr int kRangeMin = |
| 253 | std::is_same<T, half>::value ? 0xC400C400 : 0xC080C080; |
| 254 | |
| 255 | // {+3, +3}, f16x2_t / bf16x2_t |
| 256 | static constexpr int kRangeMax = |
| 257 | std::is_same<T, half>::value ? 0x42004200 : 0x40404040; |
| 258 | |
| 259 | // {+4, +4}, int16x2_t -- shifts signed [-4, +3] to unsigned [0, 7]. |
| 260 | static constexpr int kRangeBias = 0x00040004; |
| 261 | |
| 262 | __quickreduce_device_inline__ CodecQ3(int thread, int rank) |
| 263 | : CodecBase(thread, rank) {} |
| 264 | |
| 265 | __quickreduce_device_inline__ void send(int32x4_t* __restrict__ send_buffer, |
| 266 | const int32x4_t* __restrict__ data) { |
| 267 | for (int k = 0; k < kRankAtoms; k++) { |
| 268 | int32x4_t const atom = data[k]; |
| 269 | |
| 270 | // 1) Per-group dynamic scale (shared across 32 values). |
| 271 | int wblockmax = group_abs_max<T>(atom); |
| 272 | int decoding_scale = packed_mul<T>(wblockmax, kScaleFactor); |
| 273 | int encoding_scale = packed_add<T>(decoding_scale, kScaleEpsilon); |
| 274 | encoding_scale = packed_rcp<T>(encoding_scale); |
| 275 | |
| 276 | // 2) Scale + clip to signed int3 range [-4, +3]. |
| 277 | int32x4_t w; |
| 278 | for (int i = 0; i < 4; i++) { |
| 279 | w[i] = packed_mul<T>(atom[i], encoding_scale); |
| 280 | w[i] = packed_max<T>(w[i], kRangeMin); |
| 281 | w[i] = packed_min<T>(w[i], kRangeMax); |
| 282 | } |
| 283 | |
| 284 | // 3) Round to integer and bias to unsigned domain [0, 7]. |
| 285 | int32x4_t q; |
| 286 | { |
| 287 | int16_t* qi = reinterpret_cast<int16_t*>(&q); |
| 288 | T* wh = reinterpret_cast<T*>(&w); |
| 289 | for (int i = 0; i < 8; i++) qi[i] = (int16_t)rintf(T2float_cast(wh[i])); |
| 290 | |
| 291 | for (int i = 0; i < 4; i++) { |
| 292 | q[i] = packed_add<int16_t>(q[i], kRangeBias); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // 4) Split each 3-bit unsigned value into low-2-bit and high-1-bit |
| 297 | // halves, packed into one uint16 (low 2 bits per value) plus one |
| 298 | // uint8 (high 1 bit per value). |
| 299 | uint16_t q2w = 0; |
| 300 | uint8_t q1w = 0; |
| 301 | { |
| 302 | int16_t* tw = reinterpret_cast<int16_t*>(&q); |
| 303 | #pragma unroll |
| 304 | for (int i = 0; i < 8; i++) { |
| 305 | uint32_t v = static_cast<uint32_t>(tw[i]) & 0x7u; |
| 306 | q2w |= static_cast<uint16_t>((v & 0x3u) << (i * 2)); |
| 307 | q1w |= static_cast<uint8_t>(((v >> 2) & 0x1u) << i); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | uint8_t* atom_ptr = |
| 312 | reinterpret_cast<uint8_t*>(send_buffer + k * kRankBufferTileStride); |
| 313 | uint16_t* q2w_ptr = reinterpret_cast<uint16_t*>(atom_ptr) + thread; |
| 314 | uint8_t* q1w_ptr = |
| 315 | reinterpret_cast<uint8_t*>(atom_ptr + kRankTileQ1Offset) + thread; |
| 316 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 317 | (thread / 8); |
| 318 | |
| 319 | __builtin_nontemporal_store(q2w, q2w_ptr); |
| 320 | *q1w_ptr = q1w; |
| 321 | if (threadIdx.x == group_leader) { |
| 322 | __builtin_nontemporal_store(decoding_scale, qs_ptr); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | __quickreduce_device_inline__ void recv(int32x4_t** __restrict__ recv_buffer, |
| 328 | int32x4_t* __restrict__ data) { |
| 329 | for (int k = 0; k < kRankAtoms; k++) { |
| 330 | uint8_t* atom_ptr = reinterpret_cast<uint8_t*>(*recv_buffer); |
| 331 | uint16_t* q2w_ptr = reinterpret_cast<uint16_t*>(atom_ptr) + thread; |
| 332 | uint8_t* q1w_ptr = |
| 333 | reinterpret_cast<uint8_t*>(atom_ptr + kRankTileQ1Offset) + thread; |
| 334 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 335 | (thread / 8); |
| 336 | |
| 337 | uint16_t q2w = __builtin_nontemporal_load(q2w_ptr); |
| 338 | uint8_t q1w = *q1w_ptr; |
| 339 | int qs = __builtin_nontemporal_load(qs_ptr); |
| 340 | |
| 341 | *recv_buffer += kRankBufferTileStride; |
| 342 | |
| 343 | // Unpack unsigned values [0, 7] then shift back to signed domain |
| 344 | // [-4, +3] by adding kRangeMin. |
| 345 | int32x4_t w; |
| 346 | { |
| 347 | int16_t qv[8]; |
| 348 | #pragma unroll |
| 349 | for (int i = 0; i < 8; i++) { |
| 350 | uint32_t low2 = (q2w >> (2 * i)) & 0x3u; |
| 351 | uint32_t high1 = (q1w >> i) & 0x1u; |
| 352 | qv[i] = static_cast<int16_t>(low2 | (high1 << 2)); |
| 353 | } |
| 354 | |
| 355 | #pragma unroll |
| 356 | for (int i = 0; i < 4; i++) { |
| 357 | int qpack = packed_from_int16_pair<T>(qv[2 * i], qv[2 * i + 1]); |
| 358 | w[i] = packed_add<T>(qpack, kRangeMin); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Apply decode scale to reconstruct fp16/bf16 lanes. |
| 363 | for (int i = 0; i < 4; i++) { |
| 364 | w[i] = packed_mul<T>(w[i], qs); |
| 365 | } |
| 366 | |
| 367 | data[k] = w; |
| 368 | } |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | // Int6 symmetric quantization codec. |
| 373 | // We quantize the FP16 data to block-scaled Int6 in blocks of 4 * |
| 374 | // kThreadGroupSize. |
| 375 | template <typename T, int world_size> |
| 376 | struct CodecQ6 : public CodecBase { |
| 377 | static constexpr int kWorldSize = world_size; |
| 378 | |
| 379 | // Codec tile size process by this workgroup. |
| 380 | // Each threads processes a fragment of fp16x8_t (16B), |
| 381 | // into a int6x8_t (4B + 2B) and a fp16 scale shared among 32 values. |
| 382 | static constexpr int kRankAtoms = kAtoms / kWorldSize; |
| 383 | static constexpr int kRankTileStride = 1664; |
| 384 | static constexpr int kRankTileQ2Offset = 1024; |
| 385 | static constexpr int kRankTileScaleOffset = 1536; |
| 386 | static constexpr int kRankTransmittedTileSize = kRankTileStride * kRankAtoms; |
| 387 | static_assert(kRankTransmittedTileSize % 16 == 0, |
| 388 | "kRankTransmittedTileSize must be 16B aligned."); |
| 389 | |
| 390 | static constexpr int kRankBufferTileStride = |
| 391 | kRankTileStride / sizeof(int32x4_t); |
| 392 | |
| 393 | // Total tile size for the collective communication. |
| 394 | static constexpr int kTransmittedTileSize = |
| 395 | kRankTransmittedTileSize * kWorldSize; |
| 396 | |
| 397 | // Constants configuration |
| 398 | |
| 399 | // {-1/32.0h, -1/32.0h}, fp16x2_t |
| 400 | static constexpr int kScaleFactor = |
| 401 | std::is_same<T, half>::value ? 0xA800A800 : 0xBD00BD00; |
| 402 | |
| 403 | // {1e-7, 1e-7}, fp16x2_t |
| 404 | static constexpr int kScaleEpsilon = |
| 405 | std::is_same<T, half>::value ? 0x00010001 : 0x33D733D7; |
| 406 | |
| 407 | // {-32, -32}, fp16x2_t |
| 408 | static constexpr int kRangeMin = |
| 409 | std::is_same<T, half>::value ? 0xD000D000 : 0xC200C200; |
| 410 | |
| 411 | // {+31, +31}, fp16x2_t |
| 412 | static constexpr int kRangeMax = |
| 413 | std::is_same<T, half>::value ? 0x4FC04FC0 : 0x41F841F8; |
| 414 | |
| 415 | // {+32, +32}, int16x2_t |
| 416 | static constexpr int kRangeBias = 0x00200020; |
| 417 | |
| 418 | __quickreduce_device_inline__ CodecQ6(int thread, int rank) |
| 419 | : CodecBase(thread, rank) {} |
| 420 | |
| 421 | __quickreduce_device_inline__ void send(int32x4_t* __restrict__ send_buffer, |
| 422 | const int32x4_t* __restrict__ data) { |
| 423 | for (int k = 0; k < kRankAtoms; k++) { |
| 424 | int32x4_t const atom = data[k]; |
| 425 | |
| 426 | // Compute the absolute maximum of the atom in the thread group |
| 427 | // In 2 blocks of values, upper/lower halves of the f16x2_t |
| 428 | int wblockmax = group_abs_max<T>(atom); |
| 429 | |
| 430 | // Derive scales |
| 431 | int decoding_scale; |
| 432 | int encoding_scale; |
| 433 | decoding_scale = packed_mul<T>(wblockmax, kScaleFactor); |
| 434 | encoding_scale = packed_add<T>(decoding_scale, kScaleEpsilon); |
| 435 | encoding_scale = packed_rcp<T>(encoding_scale); |
| 436 | |
| 437 | // Apply scales to get quantized values |
| 438 | int32x4_t w; |
| 439 | for (int i = 0; i < 4; i++) { |
| 440 | w[i] = packed_mul<T>(atom[i], encoding_scale); |
| 441 | w[i] = packed_max<T>(w[i], kRangeMin); |
| 442 | w[i] = packed_min<T>(w[i], kRangeMax); |
| 443 | } |
| 444 | |
| 445 | // Convert from f16x2_t to uint16x2_t |
| 446 | int32x4_t q; |
| 447 | { |
| 448 | int16_t* qi = reinterpret_cast<int16_t*>(&q); |
| 449 | T* wh = reinterpret_cast<T*>(&w); |
| 450 | for (int i = 0; i < 8; i++) qi[i] = (int16_t)rintf(T2float_cast(wh[i])); |
| 451 | |
| 452 | for (int i = 0; i < 4; i++) { |
| 453 | q[i] = packed_add<int16_t>(q[i], kRangeBias); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Pack 8 x q6 into int32_t + int16_t |
| 458 | uint32_t q4w; |
| 459 | uint16_t q2w = 0; |
| 460 | q4w = (q[0] & 0x000F000F) | ((q[1] & 0x000F000F) << 4) | |
| 461 | ((q[2] & 0x000F000F) << 8) | ((q[3] & 0x000F000F) << 12); |
| 462 | { |
| 463 | int16_t* tw = reinterpret_cast<int16_t*>(&q); |
| 464 | #pragma unroll |
| 465 | for (int i = 0; i < 8; i++) { |
| 466 | q2w |= (tw[i] >> 4) << (i * 2); |
| 467 | } |
| 468 | } |
| 469 | // Write quantized atom to send_buffer |
| 470 | // note: only the group leader stores the scale |
| 471 | uint8_t* atom_ptr = |
| 472 | reinterpret_cast<uint8_t*>(send_buffer + k * kRankBufferTileStride); |
| 473 | uint32_t* q4w_ptr = reinterpret_cast<uint32_t*>(atom_ptr) + thread; |
| 474 | uint16_t* q2w_ptr = |
| 475 | reinterpret_cast<uint16_t*>(atom_ptr + kRankTileQ2Offset) + thread; |
| 476 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 477 | (thread / 8); |
| 478 | |
| 479 | __builtin_nontemporal_store(q4w, q4w_ptr); |
| 480 | __builtin_nontemporal_store(q2w, q2w_ptr); |
| 481 | if (threadIdx.x == group_leader) { |
| 482 | __builtin_nontemporal_store(decoding_scale, qs_ptr); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | __quickreduce_device_inline__ void recv(int32x4_t** __restrict__ recv_buffer, |
| 488 | int32x4_t* __restrict__ data) { |
| 489 | for (int k = 0; k < kRankAtoms; k++) { |
| 490 | // Directly read quantized atom from recv_buffer |
| 491 | uint8_t* atom_ptr = reinterpret_cast<uint8_t*>(*recv_buffer); |
| 492 | uint32_t* q4w_ptr = reinterpret_cast<uint32_t*>(atom_ptr) + thread; |
| 493 | uint16_t* q2w_ptr = |
| 494 | reinterpret_cast<uint16_t*>(atom_ptr + kRankTileQ2Offset) + thread; |
| 495 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 496 | (thread / 8); |
| 497 | |
| 498 | uint32_t q4w = __builtin_nontemporal_load(q4w_ptr); |
| 499 | uint16_t q2w = __builtin_nontemporal_load(q2w_ptr); |
| 500 | int qs = __builtin_nontemporal_load(qs_ptr); |
| 501 | |
| 502 | *recv_buffer += kRankBufferTileStride; |
| 503 | |
| 504 | // Unpack q6 into fp16x8_t |
| 505 | int32x4_t w; |
| 506 | { |
| 507 | static uint constexpr kMask000F = 0x000F000F; |
| 508 | static uint constexpr kHalf2_1024 = |
| 509 | 0x64006400; // {1024.0, 1024.0}, fp16x2_t |
| 510 | static uint constexpr kHalf2_1056 = |
| 511 | 0xE420E420; // {-1056.0, -1056.0}, fp16x2_t |
| 512 | |
| 513 | #pragma unroll |
| 514 | for (int i = 0; i < 4; i++) { |
| 515 | int32_t q4 = q4w & kMask000F; |
| 516 | int32_t q2 = (q2w & 0x3) | ((q2w & 0xC) << 14); |
| 517 | q4w >>= 4; |
| 518 | q2w >>= 4; |
| 519 | if constexpr (std::is_same<T, half>::value) { |
| 520 | int32_t q6 = q4 | (q2 << 4) | kHalf2_1024; |
| 521 | asm volatile("v_pk_add_f16 %0, %1, %2" |
| 522 | : "=v"(w[i]) |
| 523 | : "v"(q6), "v"(kHalf2_1056)); |
| 524 | } else { |
| 525 | int32_t int16_2 = q4 | (q2 << 4); |
| 526 | int16_t low = static_cast<int16_t>(int16_2 & 0xFFFF); |
| 527 | int16_t high = static_cast<int16_t>((int16_2 >> 16) & 0xFFFF); |
| 528 | |
| 529 | nv_bfloat16 bf_low = __float2bfloat16(static_cast<float>(low)); |
| 530 | nv_bfloat16 bf_high = __float2bfloat16(static_cast<float>(high)); |
| 531 | nv_bfloat162 bf2 = __halves2bfloat162(bf_low, bf_high); |
| 532 | int32_t packed_bf16 = *reinterpret_cast<int32_t*>(&bf2); |
| 533 | w[i] = packed_add<nv_bfloat16>(packed_bf16, kRangeMin); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Apply decoding scales |
| 539 | for (int i = 0; i < 4; i++) { |
| 540 | w[i] = packed_mul<T>(w[i], qs); |
| 541 | } |
| 542 | |
| 543 | data[k] = w; |
| 544 | } |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | // Int8 symmetric quantization codec. |
| 549 | // We quantize the FP16 data to block-scaled Int8 in blocks of 4 * |
| 550 | // kThreadGroupSize. |
| 551 | template <typename T, int world_size> |
| 552 | struct CodecQ8 : public CodecBase { |
| 553 | static constexpr int kWorldSize = world_size; |
| 554 | |
| 555 | // Codec tile size process by this workgroup. |
| 556 | // Each threads processes a fragment of f16x8_t (16B), |
| 557 | // into a int8x8_t (8B) and a f16 scale shared among 32 values. |
| 558 | static constexpr int kRankAtoms = kAtoms / kWorldSize; |
| 559 | static constexpr int kRankTileStride = 2176; |
| 560 | static constexpr int kRankTileScaleOffset = 2048; |
| 561 | static constexpr int kRankTransmittedTileSize = kRankTileStride * kRankAtoms; |
| 562 | static_assert(kRankTransmittedTileSize % 16 == 0, |
| 563 | "kRankTileSize must be 16B aligned."); |
| 564 | |
| 565 | static constexpr int kRankBufferTileStride = |
| 566 | kRankTileStride / sizeof(int32x4_t); |
| 567 | |
| 568 | // Total tile size for the collective communication. |
| 569 | static constexpr int kTransmittedTileSize = |
| 570 | kRankTransmittedTileSize * kWorldSize; |
| 571 | |
| 572 | // Constants configuration |
| 573 | |
| 574 | // {-1/128.0h, -1/128.0h}, f16x2_t |
| 575 | static constexpr int kScaleFactor = |
| 576 | std::is_same<T, half>::value ? 0xA000A000 : 0xBC00BC00; |
| 577 | |
| 578 | // {1e-7, 1e-7}, f16x2_t |
| 579 | static constexpr int kScaleEpsilon = |
| 580 | std::is_same<T, half>::value ? 0x00010001 : 0x33D733D7; |
| 581 | |
| 582 | // {-128, -128}, f16x2_t |
| 583 | static constexpr int kRangeMin = |
| 584 | std::is_same<T, half>::value ? 0xD800D800 : 0xC300C300; |
| 585 | // {+127, +127}, f16x2_t |
| 586 | static constexpr int kRangeMax = |
| 587 | std::is_same<T, half>::value ? 0x57F057F0 : 0x42FE42FE; |
| 588 | |
| 589 | // {+128, +128}, int16x2_t |
| 590 | static constexpr int kRangeBias = 0x00800080; |
| 591 | |
| 592 | __quickreduce_device_inline__ CodecQ8(int thread, int rank) |
| 593 | : CodecBase(thread, rank) {} |
| 594 | |
| 595 | __quickreduce_device_inline__ void send(int32x4_t* __restrict__ send_buffer, |
| 596 | int32x4_t const* __restrict__ data) { |
| 597 | for (int k = 0; k < kRankAtoms; k++) { |
| 598 | int32x4_t const atom = data[k]; |
| 599 | // Compute the absolute maximum of the atom in the thread group |
| 600 | // In 2 blocks of values, upper/lower halves of the f16x2_t |
| 601 | int wblockmax = group_abs_max<T>(atom); |
| 602 | |
| 603 | // Derive scales |
| 604 | int decoding_scale; |
| 605 | int encoding_scale; |
| 606 | decoding_scale = packed_mul<T>(wblockmax, kScaleFactor); |
| 607 | encoding_scale = packed_add<T>(decoding_scale, kScaleEpsilon); |
| 608 | encoding_scale = packed_rcp<T>(encoding_scale); |
| 609 | |
| 610 | // Apply scales to get quantized values |
| 611 | int32x4_t w; |
| 612 | for (int i = 0; i < 4; i++) { |
| 613 | w[i] = packed_mul<T>(atom[i], encoding_scale); |
| 614 | w[i] = packed_max<T>(w[i], kRangeMin); |
| 615 | w[i] = packed_min<T>(w[i], kRangeMax); |
| 616 | } |
| 617 | |
| 618 | // Convert from f16x2_t to uint16x2_t |
| 619 | int32x4_t q; |
| 620 | { |
| 621 | int16_t* qi = reinterpret_cast<int16_t*>(&q); |
| 622 | T* wh = reinterpret_cast<T*>(&w); |
| 623 | for (int i = 0; i < 8; i++) qi[i] = (int16_t)rintf(T2float_cast(wh[i])); |
| 624 | |
| 625 | for (int i = 0; i < 4; i++) { |
| 626 | q[i] = packed_add<int16_t>(q[i], kRangeBias); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Pack 8 x q8 into int32x2_t |
| 631 | int32x2_t qw; |
| 632 | qw[0] = q[0] | (q[1] << 8); |
| 633 | qw[1] = q[2] | (q[3] << 8); |
| 634 | |
| 635 | // Write quantized atom to send_buffer |
| 636 | // note: only the group leader stores the scale |
| 637 | uint8_t* atom_ptr = |
| 638 | reinterpret_cast<uint8_t*>(send_buffer + k * kRankBufferTileStride); |
| 639 | int32x2_t* qw_ptr = reinterpret_cast<int32x2_t*>(atom_ptr) + thread; |
| 640 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 641 | (thread / 8); |
| 642 | |
| 643 | __builtin_nontemporal_store(qw, qw_ptr); |
| 644 | if (threadIdx.x == group_leader) { |
| 645 | __builtin_nontemporal_store(decoding_scale, qs_ptr); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | __quickreduce_device_inline__ void recv(int32x4_t** __restrict__ recv_buffer, |
| 651 | int32x4_t* __restrict__ data) { |
| 652 | for (int k = 0; k < kRankAtoms; k++) { |
| 653 | // Directly read quantized atom from recv_buffer |
| 654 | uint8_t* atom_ptr = reinterpret_cast<uint8_t*>(*recv_buffer); |
| 655 | int32x2_t* qw_ptr = reinterpret_cast<int32x2_t*>(atom_ptr) + thread; |
| 656 | int* qs_ptr = reinterpret_cast<int*>(atom_ptr + kRankTileScaleOffset) + |
| 657 | (thread / 8); |
| 658 | |
| 659 | int32x2_t qw = __builtin_nontemporal_load(qw_ptr); |
| 660 | int qs = __builtin_nontemporal_load(qs_ptr); |
| 661 | |
| 662 | *recv_buffer += kRankBufferTileStride; |
| 663 | |
| 664 | // Unpack q8 into fp16x8_t |
| 665 | int32x4_t w; |
| 666 | { |
| 667 | static uint constexpr kMask00FF = 0x00FF00FF; |
| 668 | |
| 669 | // {1024.0, 1024.0}, fp16x2_t |
| 670 | static uint constexpr kHalf2_1024 = 0x64006400; |
| 671 | |
| 672 | // {-1152.0, -1152.0}, fp16x2_t |
| 673 | static uint constexpr kHalf2_1152 = 0xE480E480; |
| 674 | |
| 675 | #pragma unroll |
| 676 | for (int i = 0; i < 4; i++) { |
| 677 | if constexpr (std::is_same<T, half>::value) { |
| 678 | int32_t q8 = |
| 679 | ((qw[i / 2] >> ((i % 2) * 8)) & kMask00FF) | kHalf2_1024; |
| 680 | w[i] = packed_add<half>(q8, kHalf2_1152); |
| 681 | } else { |
| 682 | int32_t int16_2 = (qw[i / 2] >> ((i % 2) * 8)) & kMask00FF; |
| 683 | int16_t low = static_cast<int16_t>(int16_2 & 0xFFFF); |
| 684 | int16_t high = static_cast<int16_t>((int16_2 >> 16) & 0xFFFF); |
| 685 | nv_bfloat16 bf_low = __float2bfloat16(static_cast<float>(low)); |
| 686 | nv_bfloat16 bf_high = __float2bfloat16(static_cast<float>(high)); |
| 687 | nv_bfloat162 bf2 = __halves2bfloat162(bf_low, bf_high); |
| 688 | int32_t packed_bf16 = *reinterpret_cast<int32_t*>(&bf2); |
| 689 | w[i] = packed_add<nv_bfloat16>(packed_bf16, kRangeMin); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // Apply decoding scales |
| 695 | for (int i = 0; i < 4; i++) { |
| 696 | w[i] = packed_mul<T>(w[i], qs); |
| 697 | } |
| 698 | |
| 699 | data[k] = w; |
| 700 | } |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | // Twoshot All Reduce |
| 705 | template <typename T, class Codec, bool cast_bf2half> |
| 706 | struct AllReduceTwoshot { |
| 707 | static_assert(sizeof(T) == 2); |
| 708 | |
| 709 | static constexpr int kWorldSize = Codec::kWorldSize; |
| 710 | |
| 711 | __device__ static void run( |
| 712 | T const* __restrict__ input, T* __restrict__ output, |
| 713 | uint32_t const N, // number of elements |
| 714 | int const block, // block index |
| 715 | int const rank, // rank index |
| 716 | uint8_t** __restrict__ buffer_list, // communication buffers |
| 717 | uint32_t const data_offset, // offset to start of the data buffer |
| 718 | uint32_t flag_color, int64_t data_size_per_phase) { |
| 719 | // Topology |
| 720 | int thread = threadIdx.x + threadIdx.y * kWavefront; |
| 721 | uint8_t* rank_buffer = buffer_list[rank]; |
| 722 | Codec codec(thread, rank); |
| 723 | int block_id = blockIdx.x; |
| 724 | // -------------------------------------------------------- |
| 725 | // Read input into registers |
| 726 | int32x4_t tA[kAtoms]; |
| 727 | |
| 728 | BufferResource src_buffer(const_cast<T*>(input), N * sizeof(T)); |
| 729 | uint32_t src_offset = block * kTileSize + thread * sizeof(int32x4_t); |
| 730 | |
| 731 | for (int i = 0; i < kAtoms; i++) { |
| 732 | tA[i] = buffer_load_dwordx4(src_buffer.descriptor, src_offset, 0, 0); |
| 733 | src_offset += kAtomStride * sizeof(int32x4_t); |
| 734 | if constexpr (cast_bf2half) { |
| 735 | const nv_bfloat162* bf_buf = |
| 736 | reinterpret_cast<const nv_bfloat162*>(&tA[i]); |
| 737 | half2 half_buf[4]; |
| 738 | #pragma unroll |
| 739 | for (int j = 0; j < 4; ++j) { |
| 740 | float2 f = __bfloat1622float2(bf_buf[j]); |
| 741 | half_buf[j] = __float22half2_rn(f); |
| 742 | } |
| 743 | tA[i] = *reinterpret_cast<const int32x4_t*>(half_buf); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | // -------------------------------------------------------- |
| 748 | // Phase-1A: Write segment data into the communication buffer of the target |
| 749 | // rank responsible for this segment. |
| 750 | uint32_t comm_data0_offset = |
| 751 | data_offset + block_id * Codec::kTransmittedTileSize; |
| 752 | uint32_t comm_data1_offset = data_size_per_phase + comm_data0_offset; |
| 753 | |
| 754 | uint32_t comm_flags0_offset = block_id * (kWorldSize * sizeof(uint32_t)); |
| 755 | uint32_t comm_flags1_offset = (data_offset / 2) + comm_flags0_offset; |
| 756 | |
| 757 | for (int r = 0; r < kWorldSize; r++) { |
| 758 | int32x4_t* send_buffer = |
| 759 | reinterpret_cast<int32x4_t*>(buffer_list[r] + comm_data0_offset + |
| 760 | rank * Codec::kRankTransmittedTileSize); |
| 761 | codec.send(send_buffer, &tA[r * Codec::kRankAtoms]); |
| 762 | } |
| 763 | |
| 764 | __syncthreads(); |
| 765 | if (thread < kWorldSize) { |
| 766 | int r = thread; |
| 767 | uint32_t* flag_ptr = reinterpret_cast<uint32_t*>( |
| 768 | buffer_list[r] + comm_flags0_offset + rank * sizeof(uint32_t)); |
| 769 | set_sync_flag(flag_ptr, flag_color); |
| 770 | } |
| 771 | // -------------------------------------------------------- |
| 772 | // Phase-1B: Reduce the segment data from the communication buffers. |
| 773 | int32x4_t tR[Codec::kRankAtoms] = {}; |
| 774 | { |
| 775 | // Read the data from the communication buffer. |
| 776 | int32x4_t* recv_buffer = |
| 777 | reinterpret_cast<int32x4_t*>(rank_buffer + comm_data0_offset); |
| 778 | uint32_t* flag_ptr = |
| 779 | reinterpret_cast<uint32_t*>(rank_buffer + comm_flags0_offset); |
| 780 | |
| 781 | for (int r = 0; r < kWorldSize; r++) { |
| 782 | // Wait for the flags to be set. |
| 783 | if (thread == 0) { |
| 784 | wait_sync_flag(&flag_ptr[r], flag_color); |
| 785 | } |
| 786 | __syncthreads(); |
| 787 | |
| 788 | // note: we reuse tA as temp buffer here |
| 789 | codec.recv(&recv_buffer, tA); |
| 790 | |
| 791 | for (int i = 0; i < Codec::kRankAtoms; i++) { |
| 792 | packed_assign_add<T>(&tR[i], &tA[i]); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Phase-2: Write the reduced segment to every other rank |
| 798 | for (int r = 0; r < kWorldSize; r++) { |
| 799 | int32x4_t* send_buffer = |
| 800 | reinterpret_cast<int32x4_t*>(buffer_list[r] + comm_data1_offset + |
| 801 | rank * Codec::kRankTransmittedTileSize); |
| 802 | codec.send(send_buffer, tR); |
| 803 | } |
| 804 | |
| 805 | __syncthreads(); |
| 806 | if (thread < kWorldSize) { |
| 807 | int r = thread; |
| 808 | uint32_t* flag_ptr = reinterpret_cast<uint32_t*>( |
| 809 | buffer_list[r] + comm_flags1_offset + rank * sizeof(uint32_t)); |
| 810 | set_sync_flag(flag_ptr, flag_color); |
| 811 | } |
| 812 | |
| 813 | // Phase-2: Read the gather segments from the rank's communication buffer. |
| 814 | { |
| 815 | // Read the data from the communication buffer. |
| 816 | int32x4_t* recv_buffer = |
| 817 | reinterpret_cast<int32x4_t*>(rank_buffer + comm_data1_offset); |
| 818 | uint32_t* flag_ptr = |
| 819 | reinterpret_cast<uint32_t*>(rank_buffer + comm_flags1_offset); |
| 820 | |
| 821 | for (int r = 0; r < kWorldSize; r++) { |
| 822 | // Wait for the flags to be set. |
| 823 | if (thread == 0) { |
| 824 | wait_sync_flag(&flag_ptr[r], flag_color); |
| 825 | } |
| 826 | __syncthreads(); |
| 827 | |
| 828 | // Gather all reduced and final rank segments into tA. |
| 829 | codec.recv(&recv_buffer, &tA[r * Codec::kRankAtoms]); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // -------------------------------------------------------- |
| 834 | // Write the result to output. |
| 835 | BufferResource dst_buffer(output, N * sizeof(T)); |
| 836 | uint32_t dst_offset = block * kTileSize + thread * sizeof(int32x4_t); |
| 837 | |
| 838 | for (int i = 0; i < kAtoms; i++) { |
| 839 | if constexpr (cast_bf2half) { |
| 840 | const half2* half_buf = reinterpret_cast<const half2*>(&tA[i]); |
| 841 | nv_bfloat162 bf16_buf[4]; |
| 842 | #pragma unroll |
| 843 | for (int j = 0; j < 4; ++j) { |
| 844 | float2 f = __half22float2(half_buf[j]); |
| 845 | bf16_buf[j] = __float22bfloat162_rn(f); |
| 846 | } |
| 847 | buffer_store_dwordx4(*reinterpret_cast<const int32x4_t*>(bf16_buf), |
| 848 | dst_buffer.descriptor, dst_offset, 0, 0); |
| 849 | } else { |
| 850 | buffer_store_dwordx4(tA[i], dst_buffer.descriptor, dst_offset, 0, 0); |
| 851 | } |
| 852 | dst_offset += kAtomStride * sizeof(int32x4_t); |
| 853 | } |
| 854 | } |
| 855 | }; |
| 856 | |
| 857 | } // namespace quickreduce |