[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
This commit is contained in:
Z Liu
2026-08-08 18:55:04 +00:00
committed by PyTorch MergeBot
parent 0754e4e682
commit 4c5b5ab032
12 changed files with 149 additions and 20 deletions
+6
View File
@@ -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."""
+1 -2
View File
@@ -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)
+5
View File
@@ -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.
+1
View File
@@ -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}")
+3
View File
@@ -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)
+1
View File
@@ -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
+93 -13
View File
@@ -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")
+4 -4
View File
@@ -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()
+1
View File
@@ -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
+7
View File
@@ -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
+11 -1
View File
@@ -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"
@@ -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"),
("<nccl.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([