Skip to main content

PyHIP - Python Interface of HIP and hip-on-nv

Introduction

This small python lib which hooks into HIP library and provides pythonic interface to it.

There are two parts of it, hip library and hiprtc library.

This also works with hip-on-nv.

Installation

Prerequisites

Before installing pyhip-interface, please make sure you have the following prerequisites installed:

Install with pip

PyHIP can be installed using pip, a package manager for Python. Run the following command in your terminal to install:

pip install pyhip-interface

Install from source

Alternatively, you can install PyHIP from the source code. First, clone the repository from GitHub:

git clone https://github.com/jatinx/PyHIP

Then, navigate to the repository directory and run the following command to install:

python setup.py install

Example Usage

import ctypes
from pyhip import hip, hiprtc
source = """
extern "C" __global__ void set(int *a) {
  *a = 10;
}
"""
prog = hiprtc.hiprtcCreateProgram(source, 'set', [], [])
device_properties = hip.hipGetDeviceProperties(0)
print(f"Compiling kernel for {device_properties.gcnArchName}")
if hip.hipGetPlatformName() == 'amd': # offload arch is amd only
  hiprtc.hiprtcCompileProgram(prog, [f'--offload-arch={device_properties.gcnArchName}'])
else:
  hiprtc.hiprtcCompileProgram(prog, [])
code = hiprtc.hiprtcGetCode(prog)
module = hip.hipModuleLoadData(code)
kernel = hip.hipModuleGetFunction(module, 'set')
ptr = hip.hipMalloc(4)

class PackageStruct(ctypes.Structure):
  _fields_ = [("a", ctypes.c_void_p)]

struct = PackageStruct(ptr)
hip.hipModuleLaunchKernel(kernel, 1, 1, 1, 1, 1, 1, 0, 0, struct)
res = ctypes.c_int(0)
hip.hipMemcpy_dtoh(ctypes.byref(res), ptr, 4)
print(res.value)

Testing

Testing is in it's infancy. I'll try to add more as and when I get some time. Any help here is appreciated :)

How to run tests

Make sure you have pytest package installed.

From the project folder run pytest ./tests/* -v -r A

Help needed

If you are an opensource developer and want to contribute to this project, I can use your help in several places depending on your skillset.

  • Adding more tests to the project
  • Adding API documentation and samples
  • Improving HIP API coverage - adding meaningful APIs instead of direct mapping.

Common Problems

  • Unable to load hip or hiprtc library - OSError: libamdhip64.so: cannot open shared object file: No such file or directory.

    • Make sure that LD_LIBRARY_PATH has hip path on it (/opt/rocm/lib or a custom installation path)
  • Getting error and need to debug

    • Set AMD_LOG_LEVEL=7 for maximum verbosity of HIP APIs (this only works on amd platform).

Metadata

Release files for pyhip-interface 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhip-interface 0.1.2
File Size Uploaded
pyhip-interface-0.1.2.tar.gz 16.0 kB Details

Release files / pyhip-interface-0.1.2.tar.gz

Download URL pyhip-interface-0.1.2.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0a19f4c2a6ae1ece88d537b8890523d149a12d676591b2ba073ff3ec9b11dfbb
BLAKE2b-256 checksum
How to use checksums
2bebbd84247bc3b902cb1e12ffb0c2ff49680a4e88bccd42760dd8e230bb4596
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page