Skip to content

[hipFile] Python API using Cython bindings#4865

Merged
riley-dixon merged 32 commits intodevelopfrom
import/develop/ROCm_hipFile/pr-248
Apr 14, 2026
Merged

[hipFile] Python API using Cython bindings#4865
riley-dixon merged 32 commits intodevelopfrom
import/develop/ROCm_hipFile/pr-248

Conversation

@systems-assistant
Copy link
Copy Markdown
Contributor

@systems-assistant systems-assistant bot commented Apr 9, 2026

This is an initial release for Python bindings for hipFile. This is still in its early stages and will likely change substantially between now and what could be called an "official" release. The generated Cython bindings should be considered temporary as we work with another team for a long-term solution, but they at least give a base to build off of for the higher-level Python API.

Addresses ROCm/hipFile#201

Test Plan

Can we load the C hipFile library from Python and run select functions?
Can we run IO?

Test Result

It works :)

AIHIPFILE-140


🔁 Imported from ROCm/hipFile#248
🧑‍💻 Originally authored by @riley-dixon

Also move _hipfile driver functions out of public __init__.py
This exception will be thrown by the Python hipFile module
rather than returning an error struct.

The hipFile C error macros are unused since they aren't very
Pythonic in the first place. If the error is a hipFile error
(even if it is a wrapped HIP error), we raise HipFileException.
If it is a different error, a different Exception will be raised.

We also won't be exposing the error struct/tuple to Python users,
though it can be accessed via the HipFileException when raised.
This module will contain the functions related to querying
driver/library configuration/properties in a Python friendly
manner.
A fairly rudimentary interface for opening/closing files with hipFile.
A simple wrapper. Still need to figure out how exactly a
hipFile Buffer object should look like and how it is tied
to the underlying device memory.
We need some GPU memory allocated for registered a buffer
with hipFile.
This isn't perfect, but it provides a starting point to at least
running IO.
Not meant to be permanent. Just a quick and dirty script to verify
running IO works.
Also adds limited typing hint for this method only.
This import is only run if TYPE_CHECKING is defined.

At runtime, if the ctypes package is not found, no error is raised
since __future__.annotations is imported which treats the hint as a
string literal and does not try to evaluate the name.
hipFileOpError and hipFileFileHandleType are exposed to the Python
layer to provide the user direct access to these enum values.
A proper fix will come next where the C enum values are namespace
qualified so that enum values exposed to Python do not need to have
their names modified.
This puts the C hipFile symbols being imported by Cython into a
"_c" namespace to avoid a namespace conflict when trying to set
a Python variable/function with the same name.
The C functions hipFileRead()/hipFileWrite() report error conditions
through a negative return value. If this return value is -1 or -5011,
it indicates additional error information needs to be fetched as it
cannot be included in a single return value.

However, this error information needs to be fetched prior to control
returning to Python. Otherwise there is no guarantee that these error
values are still valid for the error that occurred.
Cleans up what is imported in __init__.py to not pollute
the namespace when a user imports 'hipfile'.
Originally these functions were given different names when
the initial Cython code was generated. In part, this was to
avoid namespace conflicts. Now that the C functions are imported
into their own "_c" namespace, we can reuse the same function
names without conflict.

Reusing the names improves consistency of what Cython function
calls which C function.
@jordan-turbofish jordan-turbofish changed the title POC: hipFile Python API using Cython bindings [hipFile] POC: hipFile Python API using Cython bindings Apr 9, 2026
Removes supporting Win32 from FileHandle. FileHandle was designed
to manage opening & closing its own file reference. Python does
not provide a public API to open a file which returns a Win32 handle.
This leads to a contrived process of opening a file, getting a file
descriptor, and converting it to a Win32 handle. To be clear,
os.open() on Windows provides a POSIX FD.

FileHandle does not currently support a user providing an already
open file resource, which at this time seems to be the only
plausible use case of a Win32 handle being used.

For the sake of simplicity and getting out an initial release, we
will just error if the OPAQUE_WIN32 handle type is set.

(This however will leave Win32 support in the Cython layer as that
is a trivial switch that gets passed into the C library.)
@riley-dixon riley-dixon force-pushed the import/develop/ROCm_hipFile/pr-248 branch from e366dc3 to f4a5cf9 Compare April 9, 2026 23:10
@riley-dixon riley-dixon requested a review from a team as a code owner April 9, 2026 23:10
@riley-dixon riley-dixon self-assigned this Apr 10, 2026
@riley-dixon riley-dixon force-pushed the import/develop/ROCm_hipFile/pr-248 branch from f4a5cf9 to 96726e1 Compare April 10, 2026 20:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an initial proof-of-concept Python package for hipFile, using Cython to bind a small subset of the hipFile C API and exposing minimal higher-level Python wrappers (Driver / FileHandle / Buffer).

Changes:

  • Introduces a hipfile Python package with Cython-backed low-level bindings plus thin Python wrapper classes/enums.
  • Adds scikit-build-core/CMake build scaffolding to compile and install the _hipfile extension module.
  • Adds an experimental README and a rough local test script.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
projects/hipfile/python/pyproject.toml Defines the Python package metadata and scikit-build-core build backend.
projects/hipfile/python/CMakeLists.txt CMake build logic to Cythonize and build the _hipfile extension and link to libhipfile.
projects/hipfile/python/README.md Build/install instructions for the experimental bindings.
projects/hipfile/python/main.py Manual smoke-test script exercising driver open, buffer registration, and read/write.
projects/hipfile/python/hipfile/init.py Package exports and __version__ derived from the C extension constants.
projects/hipfile/python/hipfile/_chipfile.pxd Cython declarations for the subset of hipFile + HIP runtime APIs being wrapped.
projects/hipfile/python/hipfile/_hipfile.pyx Cython wrapper layer that re-exports enums/constants and wraps key C APIs.
projects/hipfile/python/hipfile/enums.py Python IntEnum mirrors for hipFile op errors and handle types.
projects/hipfile/python/hipfile/error.py HipFileException and conversion to readable error strings.
projects/hipfile/python/hipfile/driver.py Context-manager wrapper for hipFile driver open/close and use count.
projects/hipfile/python/hipfile/buffer.py Context-manager wrapper for hipFile buffer register/deregister.
projects/hipfile/python/hipfile/file.py Context-manager wrapper for file handle register/deregister plus sync read/write.
projects/hipfile/python/hipfile/properties.py Small helpers for driver properties and version retrieval.
projects/hipfile/python/hipfile/hipMalloc.py Temporary ctypes-based HIP malloc/free helper for early experimentation.
projects/hipfile/.gitignore Ignores local Python venvs and __pycache__ under the hipfile project.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread projects/hipfile/python/hipfile/file.py
Comment thread projects/hipfile/python/README.md Outdated
Comment thread projects/hipfile/python/main.py
Comment thread projects/hipfile/python/hipfile/file.py
Comment thread projects/hipfile/python/hipfile/_hipfile.pyx
Comment thread projects/hipfile/python/hipfile/hipMalloc.py
Comment thread projects/hipfile/python/main.py
Comment thread projects/hipfile/python/hipfile/file.py Outdated
Comment thread projects/hipfile/python/hipfile/_chipfile.pxd
@riley-dixon riley-dixon changed the title [hipFile] POC: hipFile Python API using Cython bindings [hipFile] Python API using Cython bindings Apr 14, 2026
@riley-dixon riley-dixon force-pushed the import/develop/ROCm_hipFile/pr-248 branch from b5f49f5 to 22bc5cc Compare April 14, 2026 21:27
@riley-dixon riley-dixon merged commit 4dcdfd6 into develop Apr 14, 2026
9 checks passed
@riley-dixon riley-dixon deleted the import/develop/ROCm_hipFile/pr-248 branch April 14, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants