Skip to content

Commit f5790a2

Browse files
authored
[PAL] Implement proper build-time detection and filtering of NVCC gencodes to enable NVIDIA multicast primitive support (#387)
1 parent 776c0f6 commit f5790a2

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ ifeq ($(USE_NVIDIA), 1)
144144
CCL_INCLUDE = $(CCL_HOME)/include
145145
CCL_LINK = -lnccl
146146
ADAPTOR_FLAG = -DUSE_NVIDIA_ADAPTOR
147+
ifeq ($(NVCC_GENCODE_MULTICAST_UNSUPPORTED), 1)
148+
ADAPTOR_FLAG += -DNVCC_GENCODE_MULTICAST_UNSUPPORTED
149+
endif
147150
else ifeq ($(USE_ASCEND), 1)
148151
DEVICE_LIB = $(DEVICE_HOME)/lib64
149152
DEVICE_INCLUDE = $(DEVICE_HOME)/include

flagcx/adaptor/ccl/nccl_adaptor.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ flagcxResult_t ncclAdaptorAllReduce(const void *sendbuff, void *recvbuff,
332332
flagcxRedOp_t op, flagcxInnerComm_t comm,
333333
flagcxStream_t stream) {
334334
#if defined(COMPILE_KERNEL_HOST) && \
335-
(NCCL_VERSION_CODE > NCCL_VERSION(2, 28, 0)) && (__CUDA_ARCH__ >= 900)
335+
(NCCL_VERSION_CODE > NCCL_VERSION(2, 28, 0)) && \
336+
!defined(NVCC_GENCODE_MULTICAST_UNSUPPORTED)
336337
size_t size = count * getFlagcxDataTypeSize(datatype);
337338
int nranks;
338339
FLAGCXCHECK((flagcxResult_t)ncclCommCount(comm->base, &nranks));
@@ -362,8 +363,7 @@ flagcxResult_t ncclAdaptorAllReduce(const void *sendbuff, void *recvbuff,
362363
FLAGCXCHECK((flagcxResult_t)ncclAllReduce(
363364
sendbuff, recvbuff, count, (ncclDataType_t)datatype, (ncclRedOp_t)op,
364365
comm->base, stream->base));
365-
#endif // defined(COMPILE_KERNEL_HOST) && (NCCL_VERSION_CODE > NCCL_VERSION(2,
366-
// 28, 0) && (__CUDA_ARCH__ >= 900))
366+
#endif
367367
return flagcxSuccess;
368368
}
369369

flagcx/kernels/custom_allreduce.cu

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ FLAGCX_DEVICE_INLINE_DECORATOR typename packed_t<T, ByteSize>::array_type
148148
multimem_sum(T* addr) {
149149
using P = packed_t<T, ByteSize>;
150150
typename P::array_type ret;
151+
#if __CUDA_ARCH__ >= 900
151152
if constexpr (std::is_same<T, nv_bfloat16>::value) {
152153
typename P::storage_t h;
153154
asm volatile(
@@ -177,6 +178,7 @@ multimem_sum(T* addr) {
177178
: "l"(addr)
178179
: "memory");
179180
}
181+
#endif
180182
return ret;
181183
}
182184

@@ -188,6 +190,7 @@ multimem_min(T* addr) {
188190
"multimem min only supports half and bfloat16");
189191
using P = packed_t<T, ByteSize>;
190192
typename P::array_type ret;
193+
#if __CUDA_ARCH__ >= 900
191194
if constexpr (std::is_same<T, nv_bfloat16>::value) {
192195
typename P::storage_t h;
193196
asm volatile(
@@ -205,6 +208,7 @@ multimem_min(T* addr) {
205208
: "memory");
206209
unpack<T, ByteSize>(h, ret.data);
207210
}
211+
#endif
208212
return ret;
209213
}
210214

@@ -216,6 +220,7 @@ multimem_max(T* addr) {
216220
"multimem max only supports half and bfloat16");
217221
using P = packed_t<T, ByteSize>;
218222
typename P::array_type ret;
223+
#if __CUDA_ARCH__ >= 900
219224
if constexpr (std::is_same<T, nv_bfloat16>::value) {
220225
typename P::storage_t h;
221226
asm volatile(
@@ -233,6 +238,7 @@ multimem_max(T* addr) {
233238
: "memory");
234239
unpack<T, ByteSize>(h, ret.data);
235240
}
241+
#endif
236242
return ret;
237243
}
238244

@@ -264,6 +270,7 @@ multimem_reduce(T* addr, ncclRedOp_t op) {
264270
template <typename T, int ByteSize = (sizeof(T) <= 4 ? 4 : 8)>
265271
FLAGCX_DEVICE_INLINE_DECORATOR void
266272
multimem_st(T* addr, typename packed_t<T, ByteSize>::array_type val) {
273+
#if __CUDA_ARCH__ >= 900
267274
using P = packed_t<T, ByteSize>;
268275
if constexpr (std::is_same<T, nv_bfloat16>::value) {
269276
typename P::storage_t h = pack<T, ByteSize>(val.data);
@@ -292,6 +299,7 @@ multimem_st(T* addr, typename packed_t<T, ByteSize>::array_type val) {
292299
: "l"(addr), "d"(val.data[0])
293300
: "memory");
294301
}
302+
#endif
295303
}
296304

297305
// Store to local/shared memory using vectorized store

makefiles/nvidia_gencode.mk

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,32 @@ else
8888
$(CUDA8_GENCODE) \
8989
$(CUDA8_PTX)
9090
endif
91-
# $(info DEVICE_COMPILER_GENCODE is ${DEVICE_COMPILER_GENCODE})
91+
92+
# ----------------
93+
# Multicast primitive support check (requires sm_90+)
94+
# If sm_90+ gencode exists: filter out < sm_90 gencodes
95+
# If no sm_90+ gencode: keep all gencodes and define NVCC_GENCODE_MULTICAST_UNSUPPORTED
96+
# ----------------
97+
98+
# Define all sm_90+ gencodes for detection
99+
SM90_PLUS_GENCODES = $(CUDA12_GENCODE) $(CUDA12_8_GENCODE) $(CUDA13_GENCODE) $(CUDA12_PTX) $(CUDA13_PTX)
100+
101+
# Check if any sm_90+ gencode is present in DEVICE_COMPILER_GENCODE
102+
HAS_SM90_PLUS := $(strip $(foreach gc,$(SM90_PLUS_GENCODES),$(findstring $(gc),$(DEVICE_COMPILER_GENCODE))))
103+
104+
ifneq ($(HAS_SM90_PLUS),)
105+
# Has sm_90+ support: filter out older gencodes for multicast kernel compilation
106+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA8_GENCODE),$(DEVICE_COMPILER_GENCODE))
107+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA9_GENCODE),$(DEVICE_COMPILER_GENCODE))
108+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA10_GENCODE),$(DEVICE_COMPILER_GENCODE))
109+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA11_GENCODE),$(DEVICE_COMPILER_GENCODE))
110+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA8_PTX),$(DEVICE_COMPILER_GENCODE))
111+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA9_PTX),$(DEVICE_COMPILER_GENCODE))
112+
DEVICE_COMPILER_GENCODE := $(filter-out $(CUDA11_PTX),$(DEVICE_COMPILER_GENCODE))
113+
$(info Multicast support enabled. Using DEVICE_COMPILER_GENCODE: ${DEVICE_COMPILER_GENCODE})
114+
else
115+
# No sm_90+ support: keep all gencodes and disable multicast
116+
NVCC_GENCODE_MULTICAST_UNSUPPORTED := 1
117+
$(info WARNING: No sm_90+ architecture detected. Multicast primitives disabled. Custom AllReduce will fallback to ncclAllReduce.)
118+
$(info Using DEVICE_COMPILER_GENCODE: ${DEVICE_COMPILER_GENCODE})
119+
endif

0 commit comments

Comments
 (0)