Skip to content

Commit

Permalink
[ROCm] HIP stream priority fix post pytorch#101956 (pytorch#106157)
Browse files Browse the repository at this point in the history
PR pytorch#101956 introduced additional stream priorities for cuda streams. HIP streams have slightly different semantics.
- HIP: 1=low, 0=default, -1=high
- CUDA: 0=default, -1=high, -2=higher, etc.

This PR forces HIP stream priority to just 0 and -1 to match the pytorch semantics.

This fixes a broken unit test.

```
python3 test_cuda_multigpu.py TestCudaMultiGPU.test_streams_priority -v

Test results will be stored in test-reports/python-unittest/test_cuda_multigpu

Running tests...
----------------------------------------------------------------------
  test_streams_priority (__main__.TestCudaMultiGPU) ... ERROR (0.200s)

======================================================================
ERROR [0.200s]: test_streams_priority (__main__.TestCudaMultiGPU)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/conda/envs/py_3.8/lib/python3.8/site-packages/torch/testing/_internal/common_utils.py", line 2354, in wrapper
    method(*args, **kwargs)
  File "test_cuda_multigpu.py", line 656, in test_streams_priority
    low, high = torch.cuda.Stream.priority_range()
RuntimeError: least_priority == 0 INTERNAL ASSERT FAILED at "/var/lib/jenkins/pytorch-upstream/c10/hip/HIPStream.h":184, please report a bug to PyTorch. Unexpected HIP stream priority range
```

Pull Request resolved: pytorch#106157
Approved by: https://github.com/malfet
  • Loading branch information
jeffdaily authored and pytorchmergebot committed Jul 31, 2023
1 parent 2b427ae commit 50e3f9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions c10/cuda/CUDAStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ static void initGlobalStreamState() {
int leastPriority = -1, greatestPriority = -1;
C10_CUDA_CHECK(
cudaDeviceGetStreamPriorityRange(&leastPriority, &greatestPriority));
// Note [HIP stream priorities]
// HIP stream priorities are 1=low, 0=default, -1=high which differs from CUDA
// which is 0=default, -1=high, -2=higher etc.
// Clamp leastPriority to 0 for HIP.
#ifdef USE_ROCM
leastPriority = 0;
#endif
// greatestPriority is negative
auto range = leastPriority - greatestPriority + 1;
max_stream_priorities = range >= c10::cuda::max_compile_time_stream_priorities
Expand Down
7 changes: 7 additions & 0 deletions c10/cuda/CUDAStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,15 @@ class C10_CUDA_API CUDAStream {
int least_priority, greatest_priority;
C10_CUDA_CHECK(
cudaDeviceGetStreamPriorityRange(&least_priority, &greatest_priority));
#ifdef USE_ROCM
// See Note [HIP stream priorities]
TORCH_INTERNAL_ASSERT(
least_priority == 1, "Unexpected HIP stream priority range");
least_priority = 0;
#else
TORCH_INTERNAL_ASSERT(
least_priority == 0, "Unexpected CUDA stream priority range");
#endif
TORCH_INTERNAL_ASSERT(
greatest_priority <= -1, "Unexpected CUDA stream priority range");
greatest_priority = std::max(
Expand Down

0 comments on commit 50e3f9c

Please sign in to comment.