erikschultheis
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Building and distributing a (non-trivial) pytorch extension -- The story so far ## 'Official' ways of building pytorch extensions * [JIT loading mechanism](https://pytorch.org/tutorials/advanced/cpp_extension.html#jit-compiling-extensions) ```python from torch.utils.cpp_extension import load lltm_cpp = load(name="lltm_cpp", sources=["lltm.cpp"]) ``` - :heavy_plus_sign: ensures correct pytorch build flags :heavy_plus_sign: can distribute pure python code :heavy_minus_sign: requires build tools on the end users' side :heavy_minus_sign: need to handle C++ side dependencies manually * [setuptools-based building](https://pytorch.org/tutorials/advanced/cpp_extension.html#building-with-setuptools) ```python from setuptools import setup, Extension from torch.utils import cpp_extension setup(name='lltm_cpp', ext_modules=[cpp_extension.CppExtension('lltm_cpp', ['lltm.cpp'])], cmdclass={'build_ext': cpp_extension.BuildExtension}) ``` - :heavy_plus_sign: ensures correct pytorch build flags :heavy_plus_sign: can be built ahead of time :heavy_minus_sign: need to handle C++ side dependencies manually ## Binding Code Similar to [pybind11](https://github.com/pybind/pybind11) :gem: ```cpp #include <torch/extension.h> TORCH_LIBRARY(torch_sparse_ops, m) { m.def("ffi_forward(Tensor features, Tensor weights, Tensor locations, Tensor? bias) -> Tensor"); } TORCH_LIBRARY_IMPL(torch_sparse_ops, CPU, m) { m.impl("ffi_forward", &SparseForwardOp<float, xck::ForwardImplementations::CPU, false>::dispatch); } TORCH_LIBRARY_IMPL(torch_sparse_ops, CUDA, m) { m.impl("ffi_forward", &SparseForwardOp<float, xck::ForwardImplementations::GPU_Unit_FanBatch_Vector, false>::dispatch); } ``` ## [Scikit-build](https://scikit-build.readthedocs.io/en/latest/) Python packaging for CMake-based projects. :gem:? Setup file with some customizations (`setup.py`): ```python from skbuild import setup pkgdir_mapping = {'torch_sparse': 'src/python'} def should_exclude(name: str): return name.endswith('.a') or name.endswith('.cmake') or name.endswith(".hpp") or "include/experimental" in name def exclude_files(cmake_manifest): return list(filter(lambda name: not should_exclude(name), cmake_manifest)) setup( # ... regular setup args cmake_args=["-DCMAKE_BUILD_TYPE=RelWithDebInfo"], packages=pkgdir_mapping.keys(), package_dir=pkgdir_mapping, cmake_process_manifest_hook=exclude_files, cmake_languages=("C", "CXX", "CUDA") ) ``` Create and make available the extension library: ```cmake add_library(torch_sparse_ops SHARED ...) # ... install(TARGETS torch_sparse_ops LIBRARY DESTINATION src/python) ``` Modern way of specifying package metadata. `pyproject.toml`: ``` [build-system] requires = [ "setuptools>=42", "scikit-build>=0.13", "cmake>=3.20", "numpy", "pybind11", "ninja", "torch==2.0.1", ] build-backend = "setuptools.build_meta" ``` Notable: Does not require user(even when building from source) to have `cmake` or `ninja` installed; will go to temporariy building virtualenv. - :heavy_plus_sign: cmake handles C++ dependencies :heavy_plus_sign: can be built ahead of time :heavy_plus_sign: can build just the C++ part :heavy_minus_sign: need to handle pytorch compile flags :page_facing_up::scissors: Build extension may depend on exact (major) version of pytorch :page_facing_up::scissors: Requires CUDA development libraries to be present ([pip packages](https://pypi.org/project/nvidia-cuda-runtime-cu11/):gem: exist for _runtime_ library) ## Adding pytorch to a CMake project * Query pytorch for cmake package path: ```cmake find_package(Python3 COMPONENTS Interpreter REQUIRED) execute_process( COMMAND "${Python3_EXECUTABLE}" "-c" "import torch;print(torch.utils.cmake_prefix_path)" OUTPUT_VARIABLE PT_CMAKE_PREFIX COMMAND_ECHO STDOUT OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY ) set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${PT_CMAKE_PREFIX}) find_package(Torch REQUIRED CONFIG) ``` and then `target_link_libraries(torch_sparse_ops PUBLIC torch)` * :page_facing_up::scissors: skbuild caches build directory, but the virtualenv is re-created every build -> paths break ```cmake if(SKBUILD) message(STATUS "Building using SKBUILD: Resetting torch directories") unset(C10_CUDA_LIBRARY CACHE) unset(TORCH_LIBRARY CACHE) endif() ``` * :page_facing_up::scissors: Sufficient for C++ based projects (?), but does not _link_ (in the cmake sense) required python parts ```cmake find_package(Python3 REQUIRED Development) target_link_libraries(torch_sparse_ops PUBLIC Python3::Python) ``` * :page_facing_up::scissors: Cannot load because of missing symbols. Links `libtorch.so`, `libtorch_cpu.so`, `libtorch_gpu.so`, but not `libtorch_python.so` ```cmake # this shared library isn't linked with the default `torch` target, # but it is required for # _ZN8pybind116detail11type_casterIN2at6TensorEvE4loadENS_6handleEb cmake_path(REPLACE_FILENAME TORCH_LIBRARY libtorch_python.so OUTPUT_VARIABLE LIBTORCH_PYTHON) target_link_libraries(torch INTERFACE ${LIBTORCH_PYTHON}) ``` Can use the full power of CMake for the build process: ```cmake cmake_minimum_required(VERSION 3.20) # we need 3.20 for `CUDAARCHS` environment variable project(XMC-Kernels-PyTorch CXX CUDA) include(cmake/pytorch.cmake) find_package(Python3 REQUIRED Development) find_package(OpenMP) set(CMAKE_CXX_STANDARD 17) set(KERNELS_REPO_URL "https://version.aalto.fi/gitlab/xmc/xmc-kernels.git" CACHE STRING "URL to the git repository containing the XMC kernels" ) include(FetchContent) FetchContent_Declare( xmc-kernels GIT_TAG master GIT_REPOSITORY ${KERNELS_REPO_URL} GIT_SHALLOW TRUE ) set(XCK_BUILD_TESTS OFF) FetchContent_MakeAvailable(xmc-kernels) add_library(torch_sparse_ops SHARED src/cc/sparse.cpp src/cc/aten_device.cpp) target_link_libraries(torch_sparse_ops PUBLIC torch implement-all-kernels Python3::Python OpenMP::OpenMP_CXX) target_compile_definitions(torch_sparse_ops PUBLIC gsl_CONFIG_CONTRACT_VIOLATION_THROWS gsl_CONFIG_DEVICE_CONTRACT_CHECKING_OFF) if(OpenMP_CXX_FOUND) target_link_libraries(torch_sparse_ops PUBLIC OpenMP::OpenMP_CXX) target_compile_definitions(torch_sparse_ops PUBLIC ATEN_THREADING=OMP) endif() install(TARGETS torch_sparse_ops LIBRARY DESTINATION src/python) ``` * :gem: [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) * :gem:? [pytorch.cmake](https://version.aalto.fi/gitlab/AaltoRSE/xmc-sparse-pytorch/-/blob/master/cmake/pytorch.cmake) ## Building and running locally * cmake based build process: `cmake -S . -B build && cmake --build build` * skbuild/python `pip install .` * wheel `pip wheel --no-deps .` Note: `pip install .` takes longer than `cmake`, as it first sets up a _new_ virtualenv and install dependencies. ## Making things portable(ish) :page_facing_up::scissors: Chances are, if you try to use the `wheel` file on a different machine, there are problems ### CUDA compute capability * Default: Compile for local GPUs * Library loads and works on different computer until you try to actually call a GPU kernel * => explicitly tell cmake to build for more architectures: [CUDAARCHS](https://cmake.org/cmake/help/latest/envvar/CUDAARCHS.html) :page_facing_up::scissors: `find_package(torch)` somehow resets cuda architecture options :interrobang: :wrench:: ```cmake # cache CUDA_ARCHITECTURES, which seems to be reset by Torch set(TMP_STORE_CUDA_ARCHITECTURES "${CMAKE_CUDA_ARCHITECTURES}") set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH};${PT_CMAKE_PREFIX}) find_package(Torch REQUIRED CONFIG) set(CMAKE_CUDA_ARCHITECTURES ${TMP_STORE_CUDA_ARCHITECTURES}) ``` ### glibc and manylinux Building on recent linux (e.g. aalto desktop) links recent c standard libraries with symbols not available on older systems (triton; CSC clusters) [PEP600](https://peps.python.org/pep-0600/) defines `manylinux` standards to ensure basic portability: Maximum allowed versions of some foundational libraries. * :gem: [auditwheel](https://github.com/pypa/auditwheel) for detecting required symbol versions and patching wheel to include all non-standard `.so`s * First attempt: centos7 (supposedly corresponding to `manylinux2014`) image from docker [nvidia/cuda](https://hub.docker.com/r/nvidia/cuda/) Does not work: `patchelf` version that comes with Centos7 too old for `auditwheel` :interrobang: * manylinux [docker images](https://github.com/pypa/manylinux) :gem:? do not come with CUDA already set-up * Finally: pytorch's own build/ci docker images are publically available, e.g. `pytorch/manylinux-builder:cuda11.7` ### Building in `pytorch/manylinux-builder` Finding the correct python versions: `/opt/python/${PYTHON}-${PYTHON}` * :page_facing_up::scissors: Finding the correct CUDA version -- by default, the cuda 11.7 imgage uses Cuda 11.2 * CMake can figure things out: `export CUDACXX=/usr/local/cuda-11.7/bin/nvcc` * :page_facing_up::scissors: pre-installed `auditwheel` does not work with python310/311 * => Install it *after* setting up the python version ```bash export PIP=${PYPATH}/bin/pip ${PIP} install auditwheel export AUDITWHEEL="${PYPATH}/bin/python -m auditwheel" ``` * :page_facing_up::scissors: Don't include the entire (wrong!) CUDA toolkit in the wheel ```bash ${AUDITWHEEL} repair --exclude libcudart.so.10.2 --exclude libtorch.so --exclude libtorch_cpu.so --exclude libtorch_cuda.so --exclude libc10.so --plat manylinux2014_x86_64 *.whl ``` Full CI script: ```yaml - export CUDAARCHS="${CUDAARCHS}" - export CUDACXX=/usr/local/cuda-11.7/bin/nvcc - export PYPATH=/opt/python/${PYTHON}-${PYTHON} - export PIP=${PYPATH}/bin/pip - export AUDITWHEEL="${PYPATH}/bin/python -m auditwheel" - export XCK_PT_DEV_SUFFIX=${CI_PIPELINE_IID} # print versions so we have them logged - g++ --version - ${CUDACXX} --version - ${PIP} --version # the system-installed auditwheel version doesn't work for py310/py311, as it wants to include libpython into the wheel # and cannot find it in the search paths. With this fresh install, libpython does not get included in the wheel. - ${PIP} install auditwheel # build the wheel - ${PIP} wheel --progress-bar off --no-deps . # make it manylinux - ${AUDITWHEEL} show *.whl # auditwheel picks up the system level cuda 10.2 here. Since we include it from the wheel anyway, I don't think it matters, # but this might be a brittle assumption - ${AUDITWHEEL} repair --exclude libcudart.so.10.2 --exclude libtorch.so --exclude libtorch_cpu.so --exclude libtorch_cuda.so --exclude libc10.so --plat manylinux2014_x86_64 *.whl ``` ### Open problems: :page_facing_up::scissors: Version matching: cuda versions; pytorch versions :page_facing_up::scissors: Pip package versioning? current: set a `dev` version based on the pipeline id ```bash export XCK_PT_DEV_SUFFIX=${CI_PIPELINE_IID} ``` and dynamically adjust the version string in setup.py ```python version = "0.0.4" # potentially add a development version suffix if "XCK_PT_DEV_SUFFIX" in os.environ: version = version + ".dev" + os.environ["XCK_PT_DEV_SUFFIX"] ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully