HIP (ROCm) 语义¶
ROCm™ 是 AMD 的开源软件平台,用于 GPU 加速的高性能计算和机器学习。HIP 是 ROCm 的 C++ 方言,旨在简化 CUDA 应用程序转换为可移植 C++ 代码的过程。当将现有的 CUDA 应用程序(如 PyTorch)转换为可移植 C++ 代码以及需要在 AMD 和 NVIDIA 之间实现可移植性的新项目时,会使用 HIP。
HIP 接口重用 CUDA 接口¶
PyTorch for HIP 有意重用了现有的 torch.cuda 接口。
这有助于加速现有 PyTorch 代码和模型的移植,因为
如果有的话,只需要很少的代码更改。
来自 CUDA 语义 的示例对 HIP 来说将完全相同:
cuda = torch.device('cuda') # Default HIP device
cuda0 = torch.device('cuda:0') # 'rocm' or 'hip' are not valid, use 'cuda'
cuda2 = torch.device('cuda:2') # GPU 2 (these are 0-indexed)
x = torch.tensor([1., 2.], device=cuda0)
# x.device is device(type='cuda', index=0)
y = torch.tensor([1., 2.]).cuda()
# y.device is device(type='cuda', index=0)
with torch.cuda.device(1):
# allocates a tensor on GPU 1
a = torch.tensor([1., 2.], device=cuda)
# transfers a tensor from CPU to GPU 1
b = torch.tensor([1., 2.]).cuda()
# a.device and b.device are device(type='cuda', index=1)
# You can also use ``Tensor.to`` to transfer a tensor:
b2 = torch.tensor([1., 2.]).to(device=cuda)
# b.device and b2.device are device(type='cuda', index=1)
c = a + b
# c.device is device(type='cuda', index=1)
z = x + y
# z.device is device(type='cuda', index=0)
# even within a context, you can specify the device
# (or give a GPU index to the .cuda call)
d = torch.randn(2, device=cuda2)
e = torch.randn(2).to(cuda2)
f = torch.randn(2).cuda(cuda2)
# d.device, e.device, and f.device are all device(type='cuda', index=2)
检查HIP¶
无论您是使用 PyTorch 的 CUDA 还是 HIP,调用is_available()的结果都是一样的。如果您使用的是支持 GPU 的 PyTorch 版本,它将返回True。如果您必须检查您正在使用的 PyTorch 版本,请参阅下面的示例:
if torch.cuda.is_available() and torch.version.hip:
# do something specific for HIP
elif torch.cuda.is_available() and torch.version.cuda:
# do something specific for CUDA
ROCm上的TensorFloat-32(TF32)¶
TF32在ROCm上不受支持。
内存管理¶
PyTorch 使用缓存内存分配器来加速内存分配。这允许在不进行设备同步的情况下快速释放内存。然而,由分配器管理的未使用内存仍然会在
rocm-smi 中显示为已使用。您可以使用 memory_allocated() 和
max_memory_allocated() 来监控张量占用的内存,并使用 memory_reserved() 和
max_memory_reserved() 来监控缓存分配器管理的总内存量。调用 empty_cache()
将释放 PyTorch 中所有未使用的缓存内存,以便其他 GPU 应用程序可以使用这些内存。但是,张量占用的 GPU 内存不会被释放,因此无法增加可用于 PyTorch 的 GPU 内存量。
对于更高级的用户,我们提供更全面的内存基准测试,通过
memory_stats()。我们还提供捕获内存分配器状态完整快照的能力,通过
memory_snapshot(),这可以帮助您理解代码生成的底层分配模式。
要调试内存错误,请在您的环境中设置
PYTORCH_NO_CUDA_MEMORY_CACHING=1 以禁用缓存。
hipFFT/rocFFT 计划缓存¶
设置 hipFFT/rocFFT 计划的缓存大小不受支持。
torch.distributed 后端¶
目前,仅支持在ROCm上使用“nccl”和“gloo”后端进行torch.distributed。
CUDA API 到 HIP API 的 C++ 映射¶
请参考:https://rocmdocs.amd.com/en/latest/Programming_Guides/HIP_API_Guide.html
注意:CUDA_VERSION 宏、cudaRuntimeGetVersion 和 cudaDriverGetVersion API 并不与 HIP_VERSION 宏、hipRuntimeGetVersion 和 hipDriverGetVersion API 在语义上对应相同的值。在进行版本检查时,请不要将它们互换使用。
例如: #if defined(CUDA_VERSION) && CUDA_VERSION >= 11000 如果希望不采用 ROCm/HIP 的代码路径: #if defined(CUDA_VERSION) && CUDA_VERSION >= 11000 && !defined(USE_ROCM) 如果希望采用 ROCm/HIP 的代码路径: #if (defined(CUDA_VERSION) && CUDA_VERSION >= 11000) || defined(USE_ROCM) 如果希望仅对特定 HIP 版本采用 ROCm/HIP 的代码路径: #if (defined(CUDA_VERSION) && CUDA_VERSION >= 11000) || (defined(USE_ROCM) && ROCM_VERSION >= 40300)