Files
rocm-systems/src/device/reduce_scatter.h
T

168 líneas
7.2 KiB
C++
Original Vista normal Histórico

2018-09-24 16:06:59 -07:00
/*************************************************************************
2022-01-07 06:39:55 -08:00
* Copyright (c) 2015-2022, NVIDIA CORPORATION. All rights reserved.
2018-09-24 16:06:59 -07:00
*
* See LICENSE.txt for license information
************************************************************************/
2023-09-26 05:47:28 -07:00
#include "device.h"
2018-09-24 16:06:59 -07:00
#include "collectives.h"
2021-07-08 14:12:04 -07:00
#include "primitives.h"
2018-09-24 16:06:59 -07:00
2021-07-08 14:12:04 -07:00
namespace {
template<typename T, typename RedOp, typename Proto>
2021-09-08 13:56:25 -07:00
__device__ __forceinline__ void runRing(ncclWorkElem *args) {
2021-07-08 14:12:04 -07:00
const int tid = threadIdx.x;
2022-05-24 02:02:31 -07:00
const int nthreads = args->nWarps*WARP_SIZE;
2022-01-07 06:39:55 -08:00
const int bid = args->bid;
const int nChannels = args->nChannels;
2021-07-08 14:12:04 -07:00
ncclRing *ring = &ncclShmem.channel.ring;
2022-05-24 02:02:31 -07:00
int const *ringRanks = ring->userRanks;
2021-07-08 14:12:04 -07:00
const ssize_t chunkSize = int(Proto::calcBytePerStep()/sizeof(T) * (Proto::Id == NCCL_PROTO_SIMPLE ? REDUCESCATTER_CHUNKSTEPS : 1));
// We should not need the final /2 but it makes performance much, much smoother. Might be a bug somewhere.
const ssize_t minChunkSizeLL128 = int(nthreads*(Proto::calcBytePerGrain()/sizeof(T))/2);
const int nranks = ncclShmem.comm.nRanks;
const ssize_t loopSize = nChannels*chunkSize;
2022-01-07 06:39:55 -08:00
const ssize_t size = args->count;
2021-07-08 14:12:04 -07:00
2022-01-07 06:39:55 -08:00
Primitives<T, RedOp, FanSymmetric<1>, 0, Proto, 0>
prims(tid, nthreads, &ring->prev, &ring->next, args->sendbuff, args->recvbuff, args->redOpArg);
2021-07-08 14:12:04 -07:00
for (ssize_t gridOffset = 0; gridOffset < size; gridOffset += loopSize) {
ssize_t realChunkSize;
if (Proto::Id == NCCL_PROTO_SIMPLE) {
realChunkSize = min(chunkSize, divUp(size-gridOffset, nChannels));
realChunkSize = roundUp(realChunkSize, (nthreads-WARP_SIZE)*sizeof(uint64_t)/sizeof(T));
2020-09-04 14:35:05 -07:00
}
2021-07-08 14:12:04 -07:00
else if (Proto::Id == NCCL_PROTO_LL)
2022-01-07 06:39:55 -08:00
realChunkSize = size-gridOffset < loopSize ? args->lastChunkSize : chunkSize;
2021-07-08 14:12:04 -07:00
else if (Proto::Id == NCCL_PROTO_LL128)
realChunkSize = min(divUp(size-gridOffset, nChannels*minChunkSizeLL128)*minChunkSizeLL128, chunkSize);
realChunkSize = int(realChunkSize);
ssize_t chunkOffset = gridOffset + bid*int(realChunkSize);
/////////////// begin ReduceScatter steps ///////////////
ssize_t offset;
int nelem = min(realChunkSize, size-chunkOffset);
int rankDest;
// step 0: push data to next GPU
rankDest = ringRanks[nranks-1];
offset = chunkOffset + rankDest * size;
prims.send(offset, nelem);
// k-2 steps: reduce and copy to next GPU
for (int j=2; j<nranks; ++j) {
rankDest = ringRanks[nranks-j];
2020-09-04 14:35:05 -07:00
offset = chunkOffset + rankDest * size;
2021-07-08 14:12:04 -07:00
prims.recvReduceSend(offset, nelem);
2020-09-04 14:35:05 -07:00
}
2021-07-08 14:12:04 -07:00
// step k-1: reduce this buffer and data, which will produce the final result
rankDest = ringRanks[0];
offset = chunkOffset + rankDest * size;
prims.recvReduceCopy(offset, chunkOffset, nelem, /*postOp=*/true);
2019-11-19 14:57:39 -08:00
}
2021-07-08 14:12:04 -07:00
}
}
template<typename T, typename RedOp>
struct RunWorkElement<ncclFuncReduceScatter, T, RedOp, NCCL_ALGO_RING, NCCL_PROTO_SIMPLE> {
2021-09-08 13:56:25 -07:00
__device__ __forceinline__ void run(ncclWorkElem *args) {
2021-07-08 14:12:04 -07:00
using Proto = ProtoSimple<REDUCESCATTER_CHUNKSTEPS/REDUCESCATTER_SLICESTEPS, REDUCESCATTER_SLICESTEPS>;
runRing<T, RedOp, Proto>(args);
}
2020-09-04 14:35:05 -07:00
};
2021-07-08 14:12:04 -07:00
template<typename T, typename RedOp>
struct RunWorkElement<ncclFuncReduceScatter, T, RedOp, NCCL_ALGO_RING, NCCL_PROTO_LL> {
2021-09-08 13:56:25 -07:00
__device__ __forceinline__ void run(ncclWorkElem *args) {
2021-07-08 14:12:04 -07:00
runRing<T, RedOp, ProtoLL>(args);
}
2020-09-04 14:35:05 -07:00
};
2021-07-08 14:12:04 -07:00
template<typename T, typename RedOp>
struct RunWorkElement<ncclFuncReduceScatter, T, RedOp, NCCL_ALGO_RING, NCCL_PROTO_LL128> {
2021-09-08 13:56:25 -07:00
__device__ __forceinline__ void run(ncclWorkElem *args) {
2021-07-08 14:12:04 -07:00
runRing<T, RedOp, ProtoLL128>(args);
}
2020-09-04 14:35:05 -07:00
};
2023-02-27 02:48:21 -08:00
template<typename T, typename RedOp>
struct RunWorkElement<ncclFuncReduceScatter, T, RedOp, NCCL_ALGO_NVLS, NCCL_PROTO_SIMPLE> {
__device__ __forceinline__ void run(ncclWorkElem *args) {
const int tid = threadIdx.x;
const int bid = args->bid;
const int nChannels = args->nChannels;
struct ncclNvls* nvls = &ncclShmem.channel.nvls;
const ssize_t chunkSize = int(args->lastChunkSize);
const ssize_t size = args->count;
const ssize_t loopSize = nChannels*chunkSize;
2023-09-26 05:47:28 -07:00
const int rank = ncclShmem.comm.rank;
const int nranks = ncclShmem.comm.nRanks;
2023-02-27 02:48:21 -08:00
2023-09-26 05:47:28 -07:00
/* if we are direct NVLS, we only need to allocate 1 warp to scatter for sync;
* if not, based on #ranks, we allocate 7 or 5 warps to reduce to saturate bandwidth
* and the rest are allocated to scatter. */
const int nThreadsReduce = args->regUsed ? (NCCL_MAX_NTHREADS - WARP_SIZE) : (nranks <= 6 ? 7 * WARP_SIZE : 5 * WARP_SIZE);
const int nThreadsScatter = args->regUsed ? WARP_SIZE : (NCCL_MAX_NTHREADS - nThreadsReduce);
2023-02-27 02:48:21 -08:00
const int tidEndScatter = nThreadsScatter;
const int tidEndReduce = tidEndScatter + nThreadsReduce;
2023-09-26 05:47:28 -07:00
if (!args->regUsed) {
if (tid < tidEndScatter) {
// Scatter
using Proto = ProtoSimple<1, 1, COLL_UNROLL>;
Primitives<T, RedOp, FanAsymmetric<0, NCCL_MAX_NVLS_ARITY>, /*Direct=*/0, Proto, 0>
prims(tid, nThreadsScatter, NULL, nvls->up, args->sendbuff, NULL,
args->redOpArg, 0 * Proto::MaxGroupWidth, 1, 1);
for (ssize_t gridOffset = 0; gridOffset < size; gridOffset += loopSize) {
ssize_t offset = gridOffset + bid * chunkSize;
int nelem = min(chunkSize, size - offset);
prims.scatter(offset, nvls->nHeads * size, nelem, size, -1, 0);
}
} else if (tid < tidEndReduce) {
// Reduce through NVLS
using Proto = ProtoSimple<1, 1, COLL_UNROLL, 1, 0>;
Primitives<T, RedOp, FanAsymmetric<1, 0>, /*Direct=*/0, Proto, 0>
prims(tid - tidEndScatter, nThreadsReduce, &nvls->down, NULL, NULL, args->recvbuff,
args->redOpArg, 3 * Proto::MaxGroupWidth, 0, 0);
for (ssize_t gridOffset = 0; gridOffset < size; gridOffset += loopSize) {
ssize_t offset = gridOffset + bid * chunkSize;
int nelem = min(chunkSize, size - offset);
prims.recv(offset, nelem);
}
2023-02-27 02:48:21 -08:00
}
2023-09-26 05:47:28 -07:00
} else {
if (tid < tidEndScatter) {
// Scatter
using Proto = ProtoSimple<1, 1, COLL_UNROLL>;
Primitives<T, RedOp, FanSymmetric<NCCL_MAX_NVLS_ARITY>, /*Direct=*/0, Proto, 0>
prims(tid, nThreadsScatter, nvls->up, nvls->up, NULL, NULL,
args->redOpArg, 0 * Proto::MaxGroupWidth, 1, 1);
for (ssize_t gridOffset = 0; gridOffset < size; gridOffset += loopSize) {
prims.scatter(0, 0, 0, 0, -1, 0);
}
/* gather used as sync */
prims.gather(0, 0, 0, 0, -1, 0);
} else if (tid < tidEndReduce) {
// Reduce through NVLS
using Proto = ProtoSimple<1, 1, COLL_UNROLL, 1, 0>;
Primitives<T, RedOp, FanSymmetric<1>, /*Direct=*/1, Proto, 0>
prims(tid - tidEndScatter, nThreadsReduce, &nvls->down, &nvls->down, NULL, args->recvbuff,
args->redOpArg, 3 * Proto::MaxGroupWidth, 0, 0, args);
for (ssize_t gridOffset = 0; gridOffset < size; gridOffset += loopSize) {
ssize_t outOffset = gridOffset + bid * chunkSize;
ssize_t inpOffset = outOffset + rank * size;
int nelem = min(chunkSize, size - outOffset);
prims.directRecvCopy(inpOffset, outOffset, nelem);
}
/* send for sync */
prims.send(0, 0);
2023-02-27 02:48:21 -08:00
}
}
}
};