Skip to content

Commit cd0f7aa

Browse files
jerrymanniljithunnair-amdjeffdaily
authored
[ROCm] cpp_extension allow user to override default flags (pytorch#152432) (#2374)
cherry-pick of pytorch@e4adf5d We need -fgpu-rdc for projects such as DeepEP + rocSHMEM. The default of -no-gpu-rdc doesn't work for such cases. As per pytorch#152432 (comment): "rocshmem shares the same global variable in different files, as deepEP uses CUDAExtention to build the project https://github.com/deepseek-ai/DeepEP/blob/65e2a700f0330f3fb1c26f49a0250d1f9d0ac1e3/setup.py#L51 and depends on rocshmem, this -fgpu-rdc is needed. The current logic in Pytorch prevents users from overriding this flag." Pull Request resolved: pytorch#152432 Approved by: https://github.com/jeffdaily Co-authored-by: Jithun Nair <[email protected]> Co-authored-by: Jeff Daily <[email protected]>
1 parent 22c98ea commit cd0f7aa

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

torch/utils/cpp_extension.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,11 +2407,18 @@ def _get_cuda_arch_flags(cflags: Optional[list[str]] = None) -> list[str]:
24072407

24082408
def _get_rocm_arch_flags(cflags: Optional[list[str]] = None) -> list[str]:
24092409
# If cflags is given, there may already be user-provided arch flags in it
2410-
# (from `extra_compile_args`)
2410+
# (from `extra_compile_args`). If user also specified -fgpu-rdc or -fno-gpu-rdc, we
2411+
# assume they know what they're doing. Otherwise, we force -fno-gpu-rdc default.
2412+
has_gpu_rdc_flag = False
24112413
if cflags is not None:
2414+
has_custom_flags = False
24122415
for flag in cflags:
24132416
if 'amdgpu-target' in flag or 'offload-arch' in flag:
2414-
return ['-fno-gpu-rdc']
2417+
has_custom_flags = True
2418+
elif 'gpu-rdc' in flag:
2419+
has_gpu_rdc_flag = True
2420+
if has_custom_flags:
2421+
return [] if has_gpu_rdc_flag else ['-fno-gpu-rdc']
24152422
# Use same defaults as used for building PyTorch
24162423
# Allow env var to override, just like during initial cmake build.
24172424
_archs = os.environ.get('PYTORCH_ROCM_ARCH', None)
@@ -2424,7 +2431,7 @@ def _get_rocm_arch_flags(cflags: Optional[list[str]] = None) -> list[str]:
24242431
else:
24252432
archs = _archs.replace(' ', ';').split(';')
24262433
flags = [f'--offload-arch={arch}' for arch in archs]
2427-
flags += ['-fno-gpu-rdc']
2434+
flags += [] if has_gpu_rdc_flag else ['-fno-gpu-rdc']
24282435
return flags
24292436

24302437
def _get_build_directory(name: str, verbose: bool) -> str:
@@ -2612,8 +2619,8 @@ def _write_ninja_file_to_build_library(path,
26122619

26132620
if with_cuda and IS_HIP_EXTENSION:
26142621
cuda_flags = ['-DWITH_HIP'] + cflags + COMMON_HIP_FLAGS + COMMON_HIPCC_FLAGS
2615-
cuda_flags += extra_cuda_cflags
26162622
cuda_flags += _get_rocm_arch_flags(cuda_flags)
2623+
cuda_flags += extra_cuda_cflags
26172624
elif with_cuda:
26182625
cuda_flags = common_cflags + COMMON_NVCC_FLAGS + _get_cuda_arch_flags()
26192626
if IS_WINDOWS:

0 commit comments

Comments
 (0)