From 4c5b5ab032f2a4f3c7807925ea92fffa457423c2 Mon Sep 17 00:00:00 2001 From: Z Liu Date: Sat, 8 Aug 2026 18:55:04 +0000 Subject: [PATCH] [ROCm] Enable hipFile (#191069) This PR enables hipFile in PyTorch by adding mappings to hipify and updating cmake files, adds more tests for GDS, and also adds a helper variable and a function to facilitate tests' skip condition. hipFile only ships with ROCm on ROCm 7.14 and later, therefore hipFile is only required after that version. Authored with Claude. Pull Request resolved: https://github.com/pytorch/pytorch/pull/191069 Approved by: https://github.com/jeffdaily --- .ci/manywheel/repair_wheel.py | 6 ++ CMakeLists.txt | 3 +- cmake/Dependencies.cmake | 5 + cmake/Summary.cmake | 1 + cmake/public/LoadHIP.cmake | 3 + docs/source/cuda.md | 1 + test/test_cuda.py | 106 ++++++++++++++++++--- torch/CMakeLists.txt | 8 +- torch/_C/__init__.pyi.in | 1 + torch/csrc/Module.cpp | 7 ++ torch/cuda/gds.py | 12 ++- torch/utils/hipify/cuda_to_hip_mappings.py | 16 ++++ 12 files changed, 149 insertions(+), 20 deletions(-) diff --git a/.ci/manywheel/repair_wheel.py b/.ci/manywheel/repair_wheel.py index 1ea97950e9fb..d69e91aefb44 100755 --- a/.ci/manywheel/repair_wheel.py +++ b/.ci/manywheel/repair_wheel.py @@ -176,6 +176,12 @@ ROCM_SO_FILES: list[str] = [ "librocroller.so", ] +# hipFile only ships with ROCm 7.14 and later, where it is required. +_version_file = Path(os.environ.get("ROCM_HOME", "/opt/rocm")) / ".info" / "version" +_rocm_version = _version_file.read_text().strip() if _version_file.is_file() else "" +if tuple(int(x) for x in _rocm_version.split(".")[:2] if x.isdigit()) >= (7, 14): + ROCM_SO_FILES.append("libhipfile.so") + def rocm_os_deps() -> list[Path]: """OS-side runtime deps that must travel with ROCm wheels.""" diff --git a/CMakeLists.txt b/CMakeLists.txt index 8750d13eea7e..294545e7f9de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,8 +317,7 @@ cmake_dependent_option(USE_STATIC_CUDNN "Use cuDNN static libraries" OFF cmake_dependent_option(USE_CUSPARSELT "Use cuSPARSELt" ON "USE_CUDA" OFF) cmake_dependent_option(USE_HIPSPARSELT "Use hipSPARSELt" ON "USE_ROCM" OFF) cmake_dependent_option(USE_CUDSS "Use cuDSS" ON "USE_CUDA" OFF) -# USE_ROCM is guarded against in Dependencies.cmake because USE_ROCM is not properly defined here -cmake_dependent_option(USE_CUFILE "Use cuFile" ON "USE_CUDA AND NOT WIN32" OFF) +cmake_dependent_option(USE_CUFILE "Use cuFile" ON "USE_CUDA OR USE_ROCM;NOT WIN32" OFF) option(USE_FBGEMM "Use FBGEMM (quantized 8-bit server operators)" ON) option(USE_KINETO "Use Kineto profiling library" ON) option(USE_CUPTI_SO "Use CUPTI as a shared library" ON) diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index c4ec1a4e0742..0184ab323c74 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -1117,6 +1117,11 @@ if(USE_ROCM) caffe2_update_option(USE_HIPSPARSELT OFF) endif() + # hipfile only ships with ROCm 7.14 and above, disable the option if not found + if(USE_CUFILE AND NOT hipfile_FOUND) + caffe2_update_option(USE_CUFILE OFF) + endif() + # ---[ Kernel asserts # Kernel asserts is disabled for ROCm by default. # It can be turned on by turning on the env USE_ROCM_KERNEL_ASSERT to the build system. diff --git a/cmake/Summary.cmake b/cmake/Summary.cmake index a526ba481f04..d953dfefab9e 100644 --- a/cmake/Summary.cmake +++ b/cmake/Summary.cmake @@ -131,6 +131,7 @@ function(caffe2_print_configuration_summary) message(STATUS " USE_ROCM_CK_GEMM : ${USE_ROCM_CK_GEMM}") message(STATUS " USE_LAYERNORM_FAST_RECIPROCAL : ${USE_LAYERNORM_FAST_RECIPROCAL}") message(STATUS " USE_HIPSPARSELT : ${USE_HIPSPARSELT}") + message(STATUS " USE_CUFILE : ${USE_CUFILE}") endif() message(STATUS " USE_EIGEN_FOR_BLAS : ${CAFFE2_USE_EIGEN_FOR_BLAS}") message(STATUS " USE_EIGEN_FOR_SPARSE : ${USE_EIGEN_SPARSE}") diff --git a/cmake/public/LoadHIP.cmake b/cmake/public/LoadHIP.cmake index 2937716d74eb..21bf5916365e 100644 --- a/cmake/public/LoadHIP.cmake +++ b/cmake/public/LoadHIP.cmake @@ -350,6 +350,9 @@ if(PYTORCH_FOUND_HIP) # Optional components. find_package_and_print_version(hipsparselt) # Will be required when ready. + if(ROCM_VERSION_DEV VERSION_GREATER_EQUAL "7.14.0") + find_package_and_print_version(hipfile REQUIRED) + endif() # ROCm 8.0 and later requires libhipcxx! This should be marked as # 'REQUIRED' once minimal ROCm version is bumped to 8.0 or later. find_package_and_print_version(libhipcxx) diff --git a/docs/source/cuda.md b/docs/source/cuda.md index a3288a39e2c0..bd46d0a4afab 100644 --- a/docs/source/cuda.md +++ b/docs/source/cuda.md @@ -398,6 +398,7 @@ See the docs for {class}`~torch.cuda.gds.GdsFile` for an example of how to use t :toctree: generated :nosignatures: + is_available gds_register_buffer gds_deregister_buffer GdsFile diff --git a/test/test_cuda.py b/test/test_cuda.py index 6dd689fac077..9f6179b28bce 100644 --- a/test/test_cuda.py +++ b/test/test_cuda.py @@ -5703,11 +5703,15 @@ print(f"{{r1}}, {{r2}}") _get_torch_cuda_version() >= (13, 1), "This test does not fail on CUDA 13.1 or newer", ) + @unittest.skipIf( + TEST_WITH_ROCM and torch.cuda.gds.is_available(), + "hipFile uses a POSIX compat fallback on ROCm, so GdsFile does not fail here", + ) def test_gds_fails_in_ci(self): - if IS_WINDOWS or TEST_WITH_ROCM: - error_msg = "is not supported on this platform" - else: + if torch.cuda.gds.is_available(): error_msg = "cuFileHandleRegister failed" + else: + error_msg = "is not supported on this platform" with TemporaryFileName() as f: with self.assertRaisesRegex(RuntimeError, error_msg): torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR) @@ -10149,26 +10153,101 @@ class TestGDS(TestCase): return part.fstype return root_type - @unittest.skip("Disabling as USE_CUFILE=0 by default in builds") - def test_gds_read_write_tensors(self): + def _require_gds(self): + # GDS needs the cuFile/hipFile bindings built in, plus an O_DIRECT-capable + # local filesystem (ext4/xfs) for the temp file the transfer targets. + if not torch.cuda.gds.is_available(): + self.skipTest("GDS (cuFile/hipFile) not built into this install") if self._get_tmp_dir_fs_type() not in ("ext4", "xfs"): self.skipTest("GPUDirect Storage requires ext4/xfs for local filesystem") + + def test_gds_is_available(self): + # is_available() must be callable and return a bool even when GDS is off. + self.assertIsInstance(torch.cuda.gds.is_available(), bool) + + def test_gds_read_write_tensors(self): + # Register buffers, pack two tensors at different offsets, read them back. + self._require_gds() src1 = torch.randn(1024, device="cuda") src2 = torch.randn(2, 1024, device="cuda") torch.cuda.gds.gds_register_buffer(src1.untyped_storage()) torch.cuda.gds.gds_register_buffer(src2.untyped_storage()) dest1 = torch.empty(1024, device="cuda") dest2 = torch.empty(2, 1024, device="cuda") + try: + with TemporaryFileName() as f: + file = torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR) + file.save_storage(src1.untyped_storage(), offset=0) + file.save_storage(src2.untyped_storage(), offset=src1.nbytes) + file.load_storage(dest1.untyped_storage(), offset=0) + file.load_storage(dest2.untyped_storage(), offset=src1.nbytes) + self.assertEqual(src1, dest1) + self.assertEqual(src2, dest2) + finally: + torch.cuda.gds.gds_deregister_buffer(src1.untyped_storage()) + torch.cuda.gds.gds_deregister_buffer(src2.untyped_storage()) + + def test_gds_read_write_without_register_buffer(self): + # gds_register_buffer is a performance optimization, not required for I/O. + self._require_gds() + src = torch.arange(4096, device="cuda", dtype=torch.float32) + dest = torch.empty_like(src) with TemporaryFileName() as f: file = torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR) - file.save_storage(src1.untyped_storage(), offset=0) - file.save_storage(src2.untyped_storage(), offset=src1.nbytes) - file.load_storage(dest1.untyped_storage(), offset=0) - file.load_storage(dest2.untyped_storage(), offset=src1.nbytes) - self.assertEqual(src1, dest1) - self.assertEqual(src2, dest2) - torch.cuda.gds.gds_deregister_buffer(src1.untyped_storage()) - torch.cuda.gds.gds_deregister_buffer(src2.untyped_storage()) + file.save_storage(src.untyped_storage()) + file.load_storage(dest.untyped_storage()) + self.assertEqual(src, dest) + + @parametrize("dtype", [torch.float32, torch.float64, torch.int64, torch.uint8]) + def test_gds_dtypes(self, dtype): + # The transfer is a raw-byte copy, so it must round-trip any dtype. + self._require_gds() + if dtype.is_floating_point: + src = torch.randn(4096, device="cuda", dtype=dtype) + else: + src = torch.randint(0, 128, (4096,), device="cuda", dtype=dtype) + dest = torch.empty_like(src) + torch.cuda.gds.gds_register_buffer(src.untyped_storage()) + try: + with TemporaryFileName() as f: + file = torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR) + file.save_storage(src.untyped_storage()) + file.load_storage(dest.untyped_storage()) + self.assertEqual(src, dest) + finally: + torch.cuda.gds.gds_deregister_buffer(src.untyped_storage()) + + def test_gds_handle_register_deregister(self): + # Explicit handle lifecycle plus the guard assertions in GdsFile. + self._require_gds() + storage = torch.randn(1024, device="cuda").untyped_storage() + with TemporaryFileName() as f: + file = torch.cuda.gds.GdsFile(f, os.O_CREAT | os.O_RDWR) + # The constructor already registered the handle. + self.assertIsNotNone(file.handle) + with self.assertRaisesRegex( + AssertionError, "Cannot register a handle that is already registered" + ): + file.register_handle() + file.deregister_handle() + self.assertIsNone(file.handle) + # Operations without a registered handle are rejected. + with self.assertRaisesRegex( + AssertionError, "Cannot deregister a handle that is not registered" + ): + file.deregister_handle() + with self.assertRaisesRegex( + AssertionError, "Cannot save data to a file that is not registered" + ): + file.save_storage(storage) + with self.assertRaisesRegex( + AssertionError, "Cannot load data from a file that is not registered" + ): + file.load_storage(storage) + # Re-registering restores usability. + file.register_handle() + self.assertIsNotNone(file.handle) + file.save_storage(storage) @unittest.skipIf(not TEST_CUDA, "CUDA not available, skipping tests") @@ -11628,6 +11707,7 @@ instantiate_parametrized_tests(TestCudaAllocator) instantiate_parametrized_tests(TestCompileKernel) instantiate_parametrized_tests(TestCachingHostAllocatorCudaGraph) instantiate_parametrized_tests(TestCachingHostAllocatorConfig) +instantiate_parametrized_tests(TestGDS) instantiate_device_type_tests(TestCudaOptims, globals()) instantiate_device_type_tests(TestCudaDeviceParametrized, globals()) instantiate_device_type_tests(TestCudaGreenContexts, globals(), except_for="cpu") diff --git a/torch/CMakeLists.txt b/torch/CMakeLists.txt index cccd87f91279..4f7d6da7187c 100644 --- a/torch/CMakeLists.txt +++ b/torch/CMakeLists.txt @@ -178,6 +178,10 @@ if(USE_ROCM) if(USE_HIPSPARSELT) list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_HIPSPARSELT) endif() + if(USE_CUFILE) + list(APPEND TORCH_PYTHON_LINK_LIBRARIES hip::hipfile) + list(APPEND TORCH_PYTHON_COMPILE_DEFINITIONS USE_CUFILE) + endif() endif() if(USE_XPU) @@ -321,10 +325,6 @@ if(USE_NUMPY) target_compile_definitions(torch_python PRIVATE USE_NUMPY) endif() -if(USE_CUFILE AND NOT USE_ROCM) - target_compile_definitions(torch_python PRIVATE USE_CUFILE) -endif() - if(BUILD_LAZY_TS_BACKEND) target_compile_definitions(torch_python PRIVATE BUILD_LAZY_TS_BACKEND) endif() diff --git a/torch/_C/__init__.pyi.in b/torch/_C/__init__.pyi.in index 0801f563203c..92b5bd335c01 100644 --- a/torch/_C/__init__.pyi.in +++ b/torch/_C/__init__.pyi.in @@ -1495,6 +1495,7 @@ _has_mkldnn: _bool _has_mkldnn_acl: _bool _has_cudnn: _bool _has_cusparselt: _bool +_has_gds: _bool has_spectral: _bool _GLIBCXX_USE_CXX11_ABI: _bool default_generator: Generator diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp index 224af241c867..075e0dfac35c 100644 --- a/torch/csrc/Module.cpp +++ b/torch/csrc/Module.cpp @@ -2675,6 +2675,13 @@ PyObject* initModule() { #endif ASSERT_TRUE(set_module_attr("_has_cusparselt", has_cusparselt)); +#if defined(USE_CUFILE) + PyObject* has_gds = Py_True; +#else + PyObject* has_gds = Py_False; +#endif + ASSERT_TRUE(set_module_attr("_has_gds", has_gds)); + #if AT_MKL_ENABLED() || AT_POCKETFFT_ENABLED() PyObject* has_spectral = Py_True; #else diff --git a/torch/cuda/gds.py b/torch/cuda/gds.py index ca4e295d864a..d9ece65ad429 100644 --- a/torch/cuda/gds.py +++ b/torch/cuda/gds.py @@ -7,6 +7,7 @@ from torch.types import Storage __all__: list[str] = [ + "is_available", "gds_register_buffer", "gds_deregister_buffer", "GdsFile", @@ -20,7 +21,16 @@ def _dummy_fn(name: str) -> Callable: return fn -if not hasattr(torch._C, "_gds_register_buffer"): +def is_available() -> bool: + r"""Return ``True`` if GDS (GPUDirect Storage) support is built in. + + This requires PyTorch to be built with cuFile (CUDA) or hipFile (ROCm). + """ + return torch._C._has_gds + + +# Without GDS built in, install dummy stubs so the API raises a clear error. +if not is_available(): if hasattr(torch._C, "_gds_deregister_buffer"): raise AssertionError( "_gds_deregister_buffer exists but _gds_register_buffer does not" diff --git a/torch/utils/hipify/cuda_to_hip_mappings.py b/torch/utils/hipify/cuda_to_hip_mappings.py index 51a7be96aefe..9b2b7727ef32 100644 --- a/torch/utils/hipify/cuda_to_hip_mappings.py +++ b/torch/utils/hipify/cuda_to_hip_mappings.py @@ -283,6 +283,9 @@ CUDA_TYPE_NAME_MAP = collections.OrderedDict([ ("cudaGraphExec_t", "hipGraphExec_t"), ("__nv_bfloat16", "__hip_bfloat16"), ("__nv_bfloat162", "__hip_bfloat162"), + ("CUfileHandle_t", "hipFileHandle_t"), + ("CUfileDescr_t", "hipFileDescr_t"), + ("CUfileError_t", "hipFileError_t"), ]) CUDA_INCLUDE_MAP = collections.OrderedDict([ @@ -325,6 +328,7 @@ CUDA_INCLUDE_MAP = collections.OrderedDict([ ("cusparseLt.h", "hipsparselt/hipsparselt.h"), ("cufft.h", "hipfft/hipfft.h"), ("cufftXt.h", "hipfft/hipfftXt.h"), + ("cufile.h", "hipfile.h"), ("", _RCCL_HEADER), ("nvrtc.h", "hip/hiprtc.h"), ("thrust/system/cuda", "thrust/system/hip"), @@ -2732,6 +2736,18 @@ CUDA_IDENTIFIER_MAP = collections.OrderedDict([ ("nvmlGpuP2PStatus_t", "bool"), ("nvmlProcessInfo_t", "amdsmi_proc_info_t"), ("nvmlGpuP2PCapsIndex_t", "uint32_t"), + ("cuFileRead", "hipFileRead"), + ("cuFileWrite", "hipFileWrite"), + ("cuFileBufRegister", "hipFileBufRegister"), + ("cuFileBufDeregister", "hipFileBufDeregister"), + ("cuFileHandleRegister", "hipFileHandleRegister"), + ("cuFileHandleDeregister", "hipFileHandleDeregister"), + ("CU_FILE_HANDLE_TYPE_OPAQUE_FD", "hipFileHandleTypeOpaqueFD"), + ("CU_FILE_SUCCESS", "hipFileSuccess"), + ("IS_CUFILE_ERR", "IS_HIPFILE_ERR"), + ("CUFILE_ERRSTR", "HIPFILE_ERRSTR"), + ("IS_CUDA_ERR", "IS_HIP_DRV_ERR"), + ("cu_err", "hip_drv_err"), ]) CUDA_SPECIAL_MAP = collections.OrderedDict([