Skip to main content

Develop C++/CUDA extensions with PyTorch like Python scripts

Project description

CharonLoad

PyPI - Version PyPI - Python Version GitHub License Tests Lint Documentation

CharonLoad is a bridge between Python code and rapidly developed custom C++/CUDA extensions to make writing high-performance research code with PyTorch easy:

  • 🔥 PyTorch C++ API detection and linking
  • 🔨 Automatic just-in-time (JIT) compilation of the C++/CUDA part
  • 📦 Cached incremental builds and automatic clean builds
  • 🔗 Full power of CMake for handling C++ dependencies
  • ⌨️ Python stub file generation for syntax highlighting and auto-completion in VS Code
  • 🐛 Interactive mixed Python/C++ debugging support in VS Code via Python C++ Debugger extension

CharonLoad reduces the burden to start writing and experimenting with custom GPU kernels in PyTorch by getting complex boilerplate code and common pitfalls out of your way. Developing C++/CUDA code with CharonLoad feels similar to writing Python scripts and lets you follow the same familiar workflow.

Installation

CharonLoad requires Python >=3.8 and can be installed from PyPI:

pip install charonload

Quick Start

CharonLoad only requires minimal changes to existing projects. In particular, a small configuration of the C++/CUDA project is added in the Python part while the CMake and C++ part should adopt some predefined functions:

  • <your_project>/main.py

    import charonload
    
    VSCODE_STUBS_DIRECTORY = pathlib.Path(__file__).parent / "typings"
    
    charonload.module_config["my_cpp_cuda_ext"] = charonload.Config(
        project_directory=pathlib.Path(__file__).parent / "<my_cpp_cuda_ext>",
        build_directory="custom/build/directory",  # optional
        stubs_directory=VSCODE_STUBS_DIRECTORY,  # optional
    )
    
    import other_module
    
  • <your_project>/other_module.py

    import my_cpp_cuda_ext  # JIT compiles and loads the extension
    
    tensor_from_ext = my_cpp_cuda_ext.generate_tensor()
    
  • <your_project>/<my_cpp_cuda_ext>/CMakeLists.txt

    find_package(charonload)
    
    if(charonload_FOUND)
        charonload_add_torch_library(${TORCH_EXTENSION_NAME} MODULE)
    
        target_sources(${TORCH_EXTENSION_NAME} PRIVATE src/<my_bindings>.cpp)
        # Further properties, e.g. link against other 3rd-party libraries, etc.
        # ...
    endif()
    
  • <your_project>/<my_cpp_cuda_ext>/src/<my_bindings>.cpp

    #include <torch/python.h>
    
    torch::Tensor generate_tensor();  // Implemented somewhere in <my_cpp_cuda_ext>
    
    PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
    {
        m.def("generate_tensor", &generate_tensor, "Optional Python docstring");
    }
    

Contributing

If you would like to contribute to CharonLoad, you can find more information in the Contributing guide.

License

MIT

Contact

Patrick Stotko - stotko@cs.uni-bonn.de

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

charonload-0.1.2.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

charonload-0.1.2-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file charonload-0.1.2.tar.gz.

File metadata

  • Download URL: charonload-0.1.2.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for charonload-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9e23e82570da7ad95bb418bbae7a91b3bbce9b93bd34c37991ec7cdeb45253b7
MD5 c63ccd957e32528e4caad1ac11ae6564
BLAKE2b-256 4a82a9053b0066a20e8bb51011036109595b669197c9cb45d3a44a94cc0c6928

See more details on using hashes here.

Provenance

File details

Details for the file charonload-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: charonload-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 30.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for charonload-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4c4e3e97eb3d4c4a555d1ef26aba5c84beb3e222ea54adb4cb0f01c8a97159fe
MD5 d4d4a797e986b84bc3ff6b257a86c7ef
BLAKE2b-256 72867dd4a5e58a3da5791dc6d5c479e88ced1a57a85e498e4707b8b23fef0317

See more details on using hashes here.

Provenance

Supported by

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