-
Notifications
You must be signed in to change notification settings - Fork 7
POC: hipFile Python API using Cython bindings #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
fcdb792
Python: Generate low-level Cython code
riley-dixon 0f7545e
Ignore: Python venv's
riley-dixon f69ae0a
Python/CMake: Define __HIP_PLATFORM_AMD__
riley-dixon 4e06092
Python: Create Driver class
riley-dixon cbfb587
Python: Create HipFileException class
riley-dixon 2bb2d30
Python: Create a properties module.
riley-dixon 013733a
Python: Create a FileHandle class.
riley-dixon ac4f6cd
Python: Create a buffer module
riley-dixon fbaf27c
Python: Add error handling to Driver.
riley-dixon 66e9988
Python: Hack in hipMalloc & hipFree
riley-dixon 2cc15e5
Python: Add read/write methods to FileHandle
riley-dixon 37fd240
Python: Add a hacked up testing script.
riley-dixon 4984f6c
Python: Add a README for building the bindings
riley-dixon bb7fa2a
Python: Turn buffer functions into a class
riley-dixon 6df21ad
Python: Change read()/write() to accept Buffer
riley-dixon 84b1818
Python: Add alternate ctor for Buffer using ctypes.c_void_p
riley-dixon 02b3915
Python: Expose 2 hipFile C enums to Python
riley-dixon 79589b0
Python: Temp fix C->Python enum namespace collisions
riley-dixon 43e86ff
Python: FileHandle can now choose HandleType
riley-dixon 539c096
Python: Fix importing C symbols in Cython
riley-dixon a80d1fb
Python: Return extra error info from read()/write()
riley-dixon d8ecba1
Python: Cleanup __init__.py for public consumption
riley-dixon 834c16f
Python: Formatting with Black
riley-dixon 57546b3
Python: Formatting with pylint
riley-dixon 737d1e6
Python/CMake: Add single lint ignore for Python_add_library
riley-dixon a337f12
Python: Align Cython functions to their C counterpart
riley-dixon e0039c9
Python: Address copilot review
riley-dixon 0a38456
Python: Remove Win32 support from FileHandle
riley-dixon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| cmake_minimum_required(VERSION 3.21) | ||
| project(hipfile-python LANGUAGES C) | ||
|
|
||
| # ---- Python & Cython ----------------------------------------------------- | ||
|
|
||
| find_package(Python REQUIRED COMPONENTS Interpreter Development.Module) | ||
| find_program(CYTHON_EXECUTABLE cython REQUIRED) | ||
|
|
||
| # ---- hipFile library & headers -------------------------------------------- | ||
|
|
||
| # hipfile.h location (default: sibling include/ directory) | ||
| set(HIPFILE_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../include" | ||
| CACHE PATH "Path to directory containing hipfile.h") | ||
|
|
||
| # libhipfile.so location | ||
| find_library(HIPFILE_LIBRARY hipfile | ||
| HINTS | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/../build/src/amd_detail" | ||
| "/opt/rocm/lib" | ||
| ) | ||
| if(NOT HIPFILE_LIBRARY) | ||
| message(FATAL_ERROR | ||
| "Could not find libhipfile. Set -DHIPFILE_LIBRARY=<path> or " | ||
| "install hipfile to /opt/rocm.") | ||
| endif() | ||
|
|
||
| # HIP runtime headers (needed because hipfile.h includes hip/hip_runtime_api.h) | ||
| find_path(HIP_INCLUDE_DIR hip/hip_runtime_api.h | ||
| HINTS | ||
| "/opt/rocm/include" | ||
| "/opt/rocm/hip/include" | ||
| ) | ||
| if(NOT HIP_INCLUDE_DIR) | ||
| message(FATAL_ERROR | ||
| "Could not find hip/hip_runtime_api.h. Set -DHIP_INCLUDE_DIR=<path> " | ||
| "or install HIP development headers.") | ||
| endif() | ||
|
|
||
| # ---- Cythonize ------------------------------------------------------------ | ||
|
|
||
| set(_PYX_SRC "${CMAKE_CURRENT_SOURCE_DIR}/hipfile/_hipfile.pyx") | ||
| set(_C_OUT "${CMAKE_CURRENT_BINARY_DIR}/_hipfile.c") | ||
|
|
||
| add_custom_command( | ||
| OUTPUT "${_C_OUT}" | ||
| COMMAND "${CYTHON_EXECUTABLE}" "${_PYX_SRC}" -o "${_C_OUT}" -3 | ||
| DEPENDS | ||
| "${_PYX_SRC}" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/hipfile/_chipfile.pxd" | ||
| COMMENT "Cythonizing _hipfile.pyx" | ||
| ) | ||
|
|
||
| # ---- Build extension module ----------------------------------------------- | ||
| # lint_cmake: -readability/wonkycase | ||
| Python_add_library(_hipfile MODULE "${_C_OUT}" WITH_SOABI) | ||
| # lint_cmake: +readability/wonkycase | ||
| target_include_directories(_hipfile PRIVATE | ||
| "${HIPFILE_INCLUDE_DIR}" | ||
| "${HIP_INCLUDE_DIR}" | ||
| ) | ||
|
|
||
| target_link_libraries(_hipfile PRIVATE "${HIPFILE_LIBRARY}") | ||
|
|
||
| # _hipfile bindings only support AMD for now | ||
| target_compile_definitions(_hipfile PRIVATE __HIP_PLATFORM_AMD__) | ||
|
|
||
| install(TARGETS _hipfile DESTINATION hipfile) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # hipFile Python Bindings | ||
|
|
||
| > [!CAUTION] | ||
| > These bindings in particular are *experimental* and the API will change. | ||
|
|
||
| ## Building & Installing | ||
|
|
||
| 1. Setup a Python virtual environment. | ||
| ```bash | ||
| $ python3 -m venv .venv | ||
| ``` | ||
| 2. Activate the Python virtual environment. | ||
| ```bash | ||
| $ source .venv/bin/activate | ||
| ``` | ||
| 3. Build the C hipFile library. See [INSTALL.md](../INSTALL.md). | ||
| 4. Build & Install the hipFile package. | ||
| ```bash | ||
| (.venv) $ pip install -e python -Ccmake.define.HIPFILE_INCLUDE_DIR=../include -Ccmake.define.HIP_INCLUDE_DIR=/opt/rocm/include | ||
| ``` | ||
|
|
||
| This will install an editable version of hipFile in your virtual environment. | ||
| It is editable in the sense that any changes you make to the hipFile <ins>Python</ins> | ||
| source code will be immediately available for any tests/scripts that use the hipFile library. | ||
| Any changes to the <ins>Cython</ins> source code will require a rebuild step. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # pylint: disable=C0114 | ||
|
|
||
| from hipfile._hipfile import ( # pylint: disable=E0401,E0611 | ||
| # Constants | ||
| VERSION_MAJOR as _VERSION_MAJOR, | ||
| VERSION_MINOR as _VERSION_MINOR, | ||
| VERSION_PATCH as _VERSION_PATCH, | ||
| ) | ||
| from hipfile.buffer import Buffer | ||
| from hipfile.driver import Driver | ||
| from hipfile.enums import FileHandleType, OpError | ||
| from hipfile.error import HipFileException | ||
| from hipfile.file import FileHandle | ||
| from hipfile.properties import driver_get_properties, get_version | ||
|
|
||
| __all__ = [ | ||
| "__version__", | ||
| "Driver", | ||
| "FileHandle", | ||
| "Buffer", | ||
| "HipFileException", | ||
| "FileHandleType", | ||
| "OpError", | ||
| "driver_get_properties", | ||
| "get_version", | ||
| ] | ||
| __version__ = f"{_VERSION_MAJOR}.{_VERSION_MINOR}.{_VERSION_PATCH}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| # cython: language_level=3 | ||
| """ | ||
| C declarations for hipFile API (extern from hipfile.h). | ||
|
|
||
| This .pxd file declares the subset of the hipFile C API that is | ||
| wrapped by the low-level Cython bindings. | ||
| """ | ||
|
|
||
| from libc.stdint cimport int64_t, uint64_t | ||
| from posix.types cimport off_t | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # HIP runtime stub — only hipError_t is needed | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| cdef extern from "hip/hip_runtime_api.h": | ||
| ctypedef enum hipError_t: | ||
| hipSuccess = 0 | ||
|
|
||
| hipError_t hipPeekAtLastError() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # hipFile public API | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| cdef extern from "hipfile.h": | ||
|
|
||
| # -- Version constants -------------------------------------------------- | ||
|
|
||
| int HIPFILE_VERSION_MAJOR | ||
| int HIPFILE_VERSION_MINOR | ||
| int HIPFILE_VERSION_PATCH | ||
| int HIPFILE_BASE_ERR | ||
|
|
||
| # -- Platform-independent types ----------------------------------------- | ||
|
|
||
| ctypedef off_t hoff_t | ||
|
|
||
| # -- Error handling ----------------------------------------------------- | ||
|
|
||
| ctypedef enum hipFileOpError_t: | ||
| hipFileSuccess | ||
| hipFileDriverNotInitialized | ||
| hipFileDriverInvalidProps | ||
| hipFileDriverUnsupportedLimit | ||
| hipFileDriverVersionMismatch | ||
| hipFileDriverVersionReadError | ||
| hipFileDriverClosing | ||
| hipFilePlatformNotSupported | ||
| hipFileIONotSupported | ||
| hipFileDeviceNotSupported | ||
| hipFileDriverError | ||
| hipFileHipDriverError | ||
| hipFileHipPointerInvalid | ||
| hipFileHipMemoryTypeInvalid | ||
| hipFileHipPointerRangeError | ||
| hipFileHipContextMismatch | ||
| hipFileInvalidMappingSize | ||
| hipFileInvalidMappingRange | ||
| hipFileInvalidFileType | ||
| hipFileInvalidFileOpenFlag | ||
| hipFileDIONotSet | ||
| # 5021 intentionally unused | ||
| hipFileInvalidValue | ||
| hipFileMemoryAlreadyRegistered | ||
| hipFileMemoryNotRegistered | ||
| hipFilePermissionDenied | ||
| hipFileDriverAlreadyOpen | ||
| hipFileHandleNotRegistered | ||
| hipFileHandleAlreadyRegistered | ||
| hipFileDeviceNotFound | ||
| hipFileInternalError | ||
| hipFileGetNewFDFailed | ||
| # 5032 intentionally unused | ||
| hipFileDriverSetupError | ||
| hipFileIODisabled | ||
| hipFileBatchSubmitFailed | ||
| hipFileGPUMemoryPinningFailed | ||
| hipFileBatchFull | ||
| hipFileAsyncNotSupported | ||
| hipFileIOMaxError | ||
|
|
||
| ctypedef struct hipFileError_t: | ||
| hipFileOpError_t err | ||
| hipError_t hip_drv_err | ||
|
|
||
| # -- Opaque handles ----------------------------------------------------- | ||
riley-dixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ctypedef void *hipFileHandle_t | ||
|
|
||
| # -- File handle types -------------------------------------------------- | ||
|
|
||
| ctypedef enum hipFileFileHandleType_t: | ||
| hipFileHandleTypeOpaqueFD | ||
| hipFileHandleTypeOpaqueWin32 | ||
| hipFileHandleTypeUserspaceFS | ||
|
|
||
| # -- Userspace FS ops (opaque — only needed as pointer type) ------------ | ||
|
|
||
| ctypedef struct hipFileFSOps_t: | ||
| pass | ||
|
|
||
| # -- File descriptor ---------------------------------------------------- | ||
| # The anonymous union is accessed via Cython C-name strings. | ||
|
|
||
| ctypedef struct hipFileDescr_t: | ||
| hipFileFileHandleType_t type | ||
| int fd "handle.fd" | ||
| void *hFile "handle.hFile" | ||
| const hipFileFSOps_t *fs_ops | ||
|
|
||
| # -- Driver status / control / feature flag enums ----------------------- | ||
|
|
||
| ctypedef enum hipFileDriverStatusFlags_t: | ||
| hipFileLustreSupported | ||
| hipFileWekaFSSupported | ||
| hipFileNFSSupported | ||
| hipFileGPFSSupported | ||
| hipFileNVMeSupported | ||
| hipFileNVMeoFSupported | ||
| hipFileSCSISupported | ||
| hipFileScaleFluxCSDSupported | ||
| hipFileNVMeshSupported | ||
| hipFileBeeGFSSupported | ||
| # 10 reserved for YRCloudFile | ||
| hipFileNVMeP2PSupported | ||
| hipFileScatefsSupported | ||
|
|
||
| ctypedef enum hipFileDriverControlFlags_t: | ||
| hipFileUsePollMode | ||
| hipFileAllowCompatMode | ||
|
|
||
| ctypedef enum hipFileFeatureFlags_t: | ||
| hipFileDynRoutingSupported | ||
| hipFileBatchIOSupported | ||
| hipFileStreamsSupported | ||
| hipFileParallelIOSupported | ||
|
|
||
| # -- Driver properties -------------------------------------------------- | ||
| # Nested anonymous struct ``nvfs`` is flattened with C-name strings. | ||
|
|
||
| ctypedef struct hipFileDriverProps_t: | ||
| unsigned int nvfs_major_version "nvfs.major_version" | ||
| unsigned int nvfs_minor_version "nvfs.minor_version" | ||
| uint64_t nvfs_poll_thresh_size "nvfs.poll_thresh_size" | ||
| uint64_t nvfs_max_direct_io_size "nvfs.max_direct_io_size" | ||
| unsigned int nvfs_driver_status_flags "nvfs.driver_status_flags" | ||
| unsigned int nvfs_driver_control_flags "nvfs.driver_control_flags" | ||
| unsigned int feature_flags | ||
| uint64_t max_device_cache_size | ||
| uint64_t per_buffer_cache_size | ||
| uint64_t max_device_pinned_mem_size | ||
| unsigned int max_batch_io_count | ||
| unsigned int max_batch_io_timeout_msecs | ||
|
|
||
| # -- Function declarations ---------------------------------------------- | ||
|
|
||
| # Error | ||
| const char *hipFileGetOpErrorString(hipFileOpError_t status) | ||
|
|
||
| # File handles | ||
| hipFileError_t hipFileHandleRegister(hipFileHandle_t *fh, | ||
| hipFileDescr_t *descr) | ||
| void hipFileHandleDeregister(hipFileHandle_t fh) | ||
|
|
||
| # Buffer registration | ||
| hipFileError_t hipFileBufRegister(const void *buffer_base, | ||
| size_t length, int flags) | ||
| hipFileError_t hipFileBufDeregister(const void *buffer_base) | ||
|
|
||
| # Synchronous I/O | ||
| ssize_t hipFileRead(hipFileHandle_t fh, void *buffer_base, size_t size, | ||
| hoff_t file_offset, hoff_t buffer_offset) | ||
| ssize_t hipFileWrite(hipFileHandle_t fh, const void *buffer_base, | ||
| size_t size, hoff_t file_offset, | ||
| hoff_t buffer_offset) | ||
riley-dixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Driver lifecycle | ||
| hipFileError_t hipFileDriverOpen() | ||
| hipFileError_t hipFileDriverClose() | ||
| int64_t hipFileUseCount() | ||
|
|
||
| # Driver properties | ||
| hipFileError_t hipFileDriverGetProperties(hipFileDriverProps_t *props) | ||
|
|
||
| # Version | ||
| hipFileError_t hipFileGetVersion(unsigned *major, unsigned *minor, | ||
| unsigned *patch) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
.pxdcimportsoff_tfromposix.types, which will fail to build on Windows even though other parts of the Python layer have win32 branches. If bindings are intended to be Linux-only for now, consider adding an explicit platform guard/error (or documenting Linux-only in packaging metadata); otherwise switch to a portableoff_tdefinition/import for non-POSIX builds.