|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <cuda.h> |
| 6 | +#include <cuda_bf16.h> |
| 7 | +#include <cuda_fp16.h> |
| 8 | + |
| 9 | +namespace meta::comms { |
| 10 | + |
| 11 | +template <typename T> |
| 12 | +concept SupportedTypes = |
| 13 | + (std::same_as<T, half> || std::same_as<T, __nv_bfloat16>); |
| 14 | + |
| 15 | +template <SupportedTypes T> |
| 16 | +static inline __device__ uint32_t |
| 17 | +vecElementAdd(const uint32_t& a, const uint32_t& b) { |
| 18 | + if constexpr (std::is_same<T, half>::value) { |
| 19 | + const __half* x = reinterpret_cast<const __half*>(&a); |
| 20 | + const __half* y = reinterpret_cast<const __half*>(&b); |
| 21 | + __half2 p = __halves2half2(x[0], x[1]); |
| 22 | + __half2 q = __halves2half2(y[0], y[1]); |
| 23 | + __half2 z = __hadd2(p, q); |
| 24 | + return (reinterpret_cast<uint32_t*>(&z))[0]; |
| 25 | + } else if constexpr (std::is_same<T, __nv_bfloat16>::value) { |
| 26 | + const __nv_bfloat16* x = reinterpret_cast<const __nv_bfloat16*>(&a); |
| 27 | + const __nv_bfloat16* y = reinterpret_cast<const __nv_bfloat16*>(&b); |
| 28 | + __nv_bfloat162 p = {x[0], x[1]}; |
| 29 | + __nv_bfloat162 q = {y[0], y[1]}; |
| 30 | + __nv_bfloat162 z = __hadd2(p, q); |
| 31 | + return (reinterpret_cast<uint32_t*>(&z))[0]; |
| 32 | + } |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +template <SupportedTypes T> |
| 37 | +static inline __device__ uint4 vecElementAdd(const uint4& a, const uint4& b) { |
| 38 | + uint4 res{0, 0, 0, 0}; |
| 39 | + res.x = vecElementAdd<T>(a.x, b.x); |
| 40 | + res.y = vecElementAdd<T>(a.y, b.y); |
| 41 | + res.z = vecElementAdd<T>(a.z, b.z); |
| 42 | + res.w = vecElementAdd<T>(a.w, b.w); |
| 43 | + return res; |
| 44 | +} |
| 45 | + |
| 46 | +template <SupportedTypes T> |
| 47 | +static inline __device__ void copyFromSrcToDest( |
| 48 | + const T* __restrict__ srcbuff, |
| 49 | + T* __restrict__ destbuff, |
| 50 | + const size_t idxStart, |
| 51 | + const size_t idxEnd, |
| 52 | + const size_t idxStride) { |
| 53 | + for (size_t idx = idxStart; idx < idxEnd; idx += idxStride) { |
| 54 | + *reinterpret_cast<uint4*>(&destbuff[idx]) = |
| 55 | + reinterpret_cast<const uint4*>(&srcbuff[idx])[0]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +template <SupportedTypes T, int NRANKS, bool hasAcc> |
| 60 | +static inline __device__ void reduceScatter( |
| 61 | + T* const* __restrict__ ipcbuffs, |
| 62 | + T* __restrict__ destbuff, |
| 63 | + const T* __restrict__ acc, |
| 64 | + int selfRank, |
| 65 | + const size_t idxStart, |
| 66 | + const size_t idxEnd, |
| 67 | + const size_t idxStride, |
| 68 | + int pattern) { |
| 69 | + /* |
| 70 | + This reduceScatter func handles three different patterns: |
| 71 | + - enable_offset is used to pick between the two patterns |
| 72 | +
|
| 73 | + 1st pattern: ReduceScatter itself |
| 74 | + Rank 0: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 75 | + Rank 1: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 76 | + Rank 2: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 77 | + Rank 3: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 78 | + ---> reduceScatter --> |
| 79 | + Rank 0: sum 0 |
| 80 | + Rank 1: sum 1 |
| 81 | + Rank 2: sum 2 |
| 82 | + Rank 3: sum 3 |
| 83 | +
|
| 84 | + 2nd pattern: ReduceScatter as the 2nd step inside AllReduce-Tree (RS + AG) |
| 85 | + Rank 0: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 86 | + Rank 1: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 87 | + Rank 2: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 88 | + Rank 3: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 89 | + ---> reduceScatter --> |
| 90 | + Rank 0: sum 0 | - | - | - |
| 91 | + Rank 1: - | sum 1 | - | - |
| 92 | + Rank 2: - | - | sum 2 | - |
| 93 | + Rank 3: - | - | - | sum 3 |
| 94 | +
|
| 95 | + 3rd pattern: reduce for AllReduce-Flat |
| 96 | + Rank 0: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 97 | + Rank 1: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 98 | + Rank 2: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 99 | + Rank 3: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 100 | + ---> reduce --> |
| 101 | + Rank 0: sum 0 | sum 1 | sum 2 | sum 3 |
| 102 | + Rank 1: sum 0 | sum 1 | sum 2 | sum 3 |
| 103 | + Rank 2: sum 0 | sum 1 | sum 2 | sum 3 |
| 104 | + Rank 3: sum 0 | sum 1 | sum 2 | sum 3 |
| 105 | + */ |
| 106 | + |
| 107 | + for (size_t idx = idxStart; idx < idxEnd; idx += idxStride) { |
| 108 | + size_t srcIdx = (pattern == 2) ? idx : (idx + selfRank * idxEnd); |
| 109 | + size_t destIdx = (pattern == 1) ? (idx + selfRank * idxEnd) : idx; |
| 110 | + |
| 111 | + uint4 sum{0, 0, 0, 0}; |
| 112 | + // TODO: The bias accumulation needs to be moved to stage 2 if the bias |
| 113 | + // vector can be different on each rank. Currently we assume the bias vector |
| 114 | + // is the same across ranks. |
| 115 | + if constexpr (hasAcc) { |
| 116 | + sum = reinterpret_cast<const uint4*>(&acc[srcIdx])[0]; |
| 117 | + } |
| 118 | + |
| 119 | + // Pipelining read val from other ranks and accumulation |
| 120 | + uint4 srcVals[2]; |
| 121 | + // Prologue: read data from first rank |
| 122 | + *reinterpret_cast<uint4*>(&srcVals[0]) = |
| 123 | + reinterpret_cast<const uint4*>(&ipcbuffs[0][srcIdx])[0]; |
| 124 | +#pragma unroll NRANKS - 1 |
| 125 | + for (int r = 0; r < NRANKS - 1; ++r) { |
| 126 | + // Kick-off reading data from next rank |
| 127 | + *reinterpret_cast<uint4*>(&srcVals[(r + 1) & 1]) = |
| 128 | + reinterpret_cast<const uint4*>( |
| 129 | + &ipcbuffs[(r + 1) % NRANKS][srcIdx])[0]; |
| 130 | + // Do accumulation for current rank |
| 131 | + sum = vecElementAdd<T>(sum, srcVals[r & 1]); |
| 132 | + } |
| 133 | + // Epilogue: accumulation for last rank |
| 134 | + sum = vecElementAdd<T>(sum, srcVals[(NRANKS - 1) & 1]); |
| 135 | + |
| 136 | + // Store to the destination buffer |
| 137 | + *reinterpret_cast<uint4*>(&destbuff[destIdx]) = |
| 138 | + *reinterpret_cast<const uint4*>(&sum); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +template <SupportedTypes T, int NRANKS> |
| 143 | +static inline __device__ void allGather( |
| 144 | + T* const* __restrict__ ipcbuffs, |
| 145 | + T* __restrict__ destbuff, |
| 146 | + int selfRank, |
| 147 | + const size_t idxStart, |
| 148 | + const size_t idxEnd, |
| 149 | + const size_t idxStride, |
| 150 | + bool enable_offset) { |
| 151 | + /* |
| 152 | + This allGather func handles two different patterns: |
| 153 | + - enable_offset is used to pick between the two patterns |
| 154 | +
|
| 155 | + 1st pattern: AllGather itself |
| 156 | + Rank 0: chunk 0 |
| 157 | + Rank 1: chunk 1 |
| 158 | + Rank 2: chunk 2 |
| 159 | + Rank 3: chunk 3 |
| 160 | + ---> AllGather --> |
| 161 | + Rank 0: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 162 | + Rank 1: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 163 | + Rank 2: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 164 | + Rank 3: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 165 | +
|
| 166 | + 2nd pattern: AllGather as the 2nd step inside AllReduce (RS + AG) |
| 167 | + Rank 0: chunk 0 | - | - | - |
| 168 | + Rank 1: - | chunk 1 | - | - |
| 169 | + Rank 2: - | - | chunk 2 | - |
| 170 | + Rank 3: - | - | - | chunk 3 |
| 171 | + ---> AllGather --> |
| 172 | + Rank 0: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 173 | + Rank 1: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 174 | + Rank 2: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 175 | + Rank 3: chunk 0 | chunk 1 | chunk 2 | chunk 3 |
| 176 | + */ |
| 177 | + |
| 178 | + for (size_t idx = idxStart; idx < idxEnd; idx += idxStride) { |
| 179 | +#pragma unroll NRANKS |
| 180 | + for (int r = 0; r < NRANKS; ++r) { |
| 181 | + int srcRank = (selfRank + r) % NRANKS; |
| 182 | + int destIdx = idx + srcRank * idxEnd; |
| 183 | + int srcIdx; |
| 184 | + if (enable_offset) { |
| 185 | + srcIdx = destIdx; |
| 186 | + } else { |
| 187 | + srcIdx = idx; |
| 188 | + } |
| 189 | + *reinterpret_cast<uint4*>(&destbuff[destIdx]) = |
| 190 | + reinterpret_cast<const uint4*>(&ipcbuffs[srcRank][srcIdx])[0]; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +} // namespace meta::comms |
0 commit comments