Skip to content

Commit 3f32fed

Browse files
Merge pull request #285 from MiloLurati/update-hip-backend-with-official-rocm-python-hip
Replacing PyHIP with new official python wrapper of ROCm HIP
2 parents ac05da3 + 6b124bb commit 3f32fed

File tree

17 files changed

+404
-294
lines changed

17 files changed

+404
-294
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## Unreleased
6+
- changed HIP python bindings from pyhip-interface to the official hip-python
67

78
## [1.0.0] - 2024-04-04
89
- HIP backend to support tuning HIP kernels on AMD GPUs

INSTALL.rst

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,26 @@ Or you could install Kernel Tuner and PyOpenCL together if you haven't done so a
124124
125125
If this fails, please see the PyOpenCL installation guide (https://wiki.tiker.net/PyOpenCL/Installation)
126126

127-
HIP and PyHIP
127+
HIP and HIP Python
128128
-------------
129129

130-
Before we can install PyHIP, you'll need to have the HIP runtime and compiler installed on your system.
130+
Before we can install HIP Python, you'll need to have the HIP runtime and compiler installed on your system.
131131
The HIP compiler is included as part of the ROCm software stack. Here is AMD's installation guide:
132132

133133
* `ROCm Documentation: HIP Installation Guide <https://docs.amd.com/bundle/HIP-Installation-Guide-v5.3/page/Introduction_to_HIP_Installation_Guide.html>`__
134134

135-
After you've installed HIP, you will need to install PyHIP. Run the following command in your terminal to install:
135+
After you've installed HIP, you will need to install HIP Python. Run the following command in your terminal to install:
136136

137-
.. code-block:: bash
138-
139-
pip install pyhip-interface
137+
First identify the first three digits of the version number of your ROCm™ installation.
138+
Then install the HIP Python package(s) as follows:
140139

141-
Alternatively, you can install PyHIP from the source code. First, clone the repository from GitHub:
142-
143-
.. code-block:: bash
140+
.. code-block:: shell
144141
145-
git clone https://github.com/jatinx/PyHIP
146-
147-
Then, navigate to the repository directory and run the following command to install:
148-
149-
.. code-block:: bash
142+
python3 -m pip install -i https://test.pypi.org/simple hip-python~=$rocm_version
143+
# if you want to install the CUDA Python interoperability package too, run:
144+
python3 -m pip install -i https://test.pypi.org/simple hip-python-as-cuda~=$rocm_version
150145
151-
python setup.py install
146+
For other installation options check `hip-python on GitHub <https://github.com/ROCm/hip-python>`_
152147

153148
Installing the git version
154149
--------------------------
@@ -171,7 +166,7 @@ The runtime dependencies are:
171166

172167
- `cuda`: install pycuda along with kernel_tuner
173168
- `opencl`: install pycuda along with kernel_tuner
174-
- `hip`: install pyhip along with kernel_tuner
169+
- `hip`: install HIP Python along with kernel_tuner
175170
- `tutorial`: install packages required to run the guides
176171

177172
These can be installed by appending e.g. ``-E cuda -E opencl -E hip``.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ What Kernel Tuner does:
3232

3333
## Installation
3434

35-
- First, make sure you have your [CUDA](https://kerneltuner.github.io/kernel_tuner/stable/install.html#cuda-and-pycuda), [OpenCL](https://kerneltuner.github.io/kernel_tuner/stable/install.html#opencl-and-pyopencl), or [HIP](https://kerneltuner.github.io/kernel_tuner/stable/install.html#hip-and-pyhipl) compiler installed
35+
- First, make sure you have your [CUDA](https://kerneltuner.github.io/kernel_tuner/stable/install.html#cuda-and-pycuda), [OpenCL](https://kerneltuner.github.io/kernel_tuner/stable/install.html#opencl-and-pyopencl), or [HIP](https://kerneltuner.github.io/kernel_tuner/stable/install.html#hip-and-hip-python) compiler installed
3636
- Then type: `pip install kernel_tuner[cuda]`, `pip install kernel_tuner[opencl]`, or `pip install kernel_tuner[hip]`
3737
- or why not all of them: `pip install kernel_tuner[cuda,opencl,hip]`
3838

doc/source/backends.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ used to compile the kernels.
5858
:header: Feature, PyCUDA, CuPy, CUDA-Python, HIP
5959
:widths: auto
6060

61-
Python package, "pycuda", "cupy", "cuda-python", "pyhip-interface"
61+
Python package, "pycuda", "cupy", "cuda-python", "hip-python"
6262
Selected with lang=, "CUDA", "CUPY", "NVCUDA", "HIP"
6363
Compiler used, "nvcc", "nvrtc", "nvrtc", "hiprtc"
6464

doc/source/design.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ building blocks for implementing runners.
4949
The observers are explained in :ref:`observers`.
5050

5151
At the bottom, the backends are shown.
52-
PyCUDA, CuPy, cuda-python, PyOpenCL and PyHIP are for tuning either CUDA, OpenCL, or HIP kernels.
52+
PyCUDA, CuPy, cuda-python, PyOpenCL and HIP Python are for tuning either CUDA, OpenCL, or HIP kernels.
5353
The CompilerFunctions implementation can call any compiler, typically NVCC
5454
or GCC is used. There is limited support for tuning Fortran kernels.
5555
This backend was created not just to be able to tune C

examples/hip/test_vector_add.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from kernel_tuner import run_kernel
66
import pytest
77

8-
#Check pyhip is installed and if a HIP capable device is present, if not skip the test
8+
#Check hip is installed and if a HIP capable device is present, if not skip the test
99
try:
10-
from pyhip import hip, hiprtc
10+
from hip import hip, hiprtc
1111
except ImportError:
12-
pytest.skip("PyHIP not installed or PYTHONPATH does not includes PyHIP")
12+
pytest.skip("HIP Python not installed or PYTHONPATH does not includes HIP Python")
1313
hip = None
1414
hiprtc = None
1515

examples/hip/vector_add.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def tune():
3030
tune_params = OrderedDict()
3131
tune_params["block_size_x"] = [128+64*i for i in range(15)]
3232

33-
results, env = tune_kernel("vector_add", kernel_string, size, args, tune_params, lang="HIP",
34-
cache="vector_add_cache.json", log=logging.DEBUG)
33+
results, env = tune_kernel("vector_add", kernel_string, size, args, tune_params, lang="HIP",
34+
log=logging.DEBUG)
3535

3636
# Store the metadata of this run
3737
store_metadata_file("vector_add-metadata.json")
@@ -40,4 +40,4 @@ def tune():
4040

4141

4242
if __name__ == "__main__":
43-
tune()
43+
tune()

kernel_tuner/backends/compiler.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
except ImportError:
2727
cp = None
2828

29+
try:
30+
from hip import hip
31+
except ImportError:
32+
hip = None
33+
34+
try:
35+
from hip._util.types import DeviceArray
36+
except ImportError:
37+
Pointer = Exception # using Exception here as a type that will never be among kernel arguments
38+
DeviceArray = Exception
39+
2940

3041
def is_cupy_array(array):
3142
"""Check if something is a cupy array.
@@ -145,9 +156,9 @@ def ready_argument_list(self, arguments):
145156
ctype_args = [None for _ in arguments]
146157

147158
for i, arg in enumerate(arguments):
148-
if not (isinstance(arg, (np.ndarray, np.number)) or is_cupy_array(arg)):
149-
raise TypeError(f"Argument is not numpy or cupy ndarray or numpy scalar but a {type(arg)}")
150-
dtype_str = str(arg.dtype)
159+
if not (isinstance(arg, (np.ndarray, np.number, DeviceArray)) or is_cupy_array(arg)):
160+
raise TypeError(f"Argument is not numpy or cupy ndarray or numpy scalar or HIP Python DeviceArray but a {type(arg)}")
161+
dtype_str = arg.typestr if isinstance(arg, DeviceArray) else str(arg.dtype)
151162
if isinstance(arg, np.ndarray):
152163
if dtype_str in dtype_map.keys():
153164
# In numpy <= 1.15, ndarray.ctypes.data_as does not itself keep a reference
@@ -156,13 +167,20 @@ def ready_argument_list(self, arguments):
156167
# (This changed in numpy > 1.15.)
157168
# data_ctypes = data.ctypes.data_as(C.POINTER(dtype_map[dtype_str]))
158169
data_ctypes = arg.ctypes.data_as(C.POINTER(dtype_map[dtype_str]))
170+
numpy_arg = arg
159171
else:
160172
raise TypeError("unknown dtype for ndarray")
161173
elif isinstance(arg, np.generic):
162174
data_ctypes = dtype_map[dtype_str](arg)
175+
numpy_arg = arg
163176
elif is_cupy_array(arg):
164177
data_ctypes = C.c_void_p(arg.data.ptr)
165-
ctype_args[i] = Argument(numpy=arg, ctypes=data_ctypes)
178+
numpy_arg = arg
179+
elif isinstance(arg, DeviceArray):
180+
data_ctypes = arg.as_c_void_p()
181+
numpy_arg = None
182+
183+
ctype_args[i] = Argument(numpy=numpy_arg, ctypes=data_ctypes)
166184
return ctype_args
167185

168186
def compile(self, kernel_instance):
@@ -380,6 +398,12 @@ def memcpy_dtoh(self, dest, src):
380398
:param src: An Argument for some memory allocation
381399
:type src: Argument
382400
"""
401+
# If src.numpy is None, it means we're dealing with a HIP Python DeviceArray
402+
if src.numpy is None:
403+
# Skip memory copies for HIP Python DeviceArray
404+
# This is because DeviceArray manages its own memory and donesn't need
405+
# explicit copies like numpy arrays do
406+
return
383407
if isinstance(dest, np.ndarray) and is_cupy_array(src.numpy):
384408
# Implicit conversion to a NumPy array is not allowed.
385409
value = src.numpy.get()
@@ -397,6 +421,12 @@ def memcpy_htod(self, dest, src):
397421
:param src: A numpy or cupy array containing the source data
398422
:type src: np.ndarray or cupy.ndarray
399423
"""
424+
# If src.numpy is None, it means we're dealing with a HIP Python DeviceArray
425+
if dest.numpy is None:
426+
# Skip memory copies for HIP Python DeviceArray
427+
# This is because DeviceArray manages its own memory and donesn't need
428+
# explicit copies like numpy arrays do
429+
return
400430
if isinstance(dest.numpy, np.ndarray) and is_cupy_array(src):
401431
# Implicit conversion to a NumPy array is not allowed.
402432
value = src.get()

0 commit comments

Comments
 (0)