Skip to main content

Python Interface to HIP and hiprtc Library

Project description

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhip-interface-0.1.2.tar.gz (16.0 kB view details)

Uploaded Source

File details

Details for the file pyhip-interface-0.1.2.tar.gz.

File metadata

  • Download URL: pyhip-interface-0.1.2.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pyhip-interface-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a19f4c2a6ae1ece88d537b8890523d149a12d676591b2ba073ff3ec9b11dfbb
MD5 3ecdcd0adaedea916515c9cc59c1e38a
BLAKE2b-256 2bebbd84247bc3b902cb1e12ffb0c2ff49680a4e88bccd42760dd8e230bb4596

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page