Skip to content
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

Move FFI linking to the new crate torch-build #447

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jerry73204
Copy link
Contributor

The work is an attempt to support third-party C++/CUDA extensions in compile time. The current torch-sys/src/build.rs design runs all linking jobs for torch-sys crate only, making it difficult to link extra C++/CUDA code. The patch moves FFI linking and probing utilities to a standalone torch-build crate, supporting C++ ABI probing and CUDA so file probing. The torch-sys uses torch-build to link libtorch. The patch refactors the dependency tree into this hierarchy.

tch-rs ← torch-sys
              └─ build.rs ← torch-build

This way enables third party extensions to share the FFI linking code with tch-rs. In compile time, the libtorch probing is done only once in torch-build. The downstream torch-sys and extension code links to libtorch a common interface.

// tch-rs
tch-rs ← torch-sys
              └─ build.rs ← torch-build

// extension
3rd_party_ext
├ build.rs ← torch-build
├ src      ← torch-sys
└ csrc     ← C++/CUDA code

// downstream
program
├ tch-rs
└ 3rd_party_ext

The torch-build is designed to be idempotent. The probing is always done at most once, so that dependent crates see identical probing information. Further improvements are done in this work.

  • Add "download-libtorch" and "parallel-build" Cargo features.
  • Automatic C++11 ABI probing.
  • Rewrite CUDA shared library probing.
    • Probe system device to use correct CUDA arch list.
    • Probe AMD HIP, CUDA and separated CPU/CUDA libraries and link
      accordingly.
  • Expose public API to let users to obtain detailed probing information.

The remaining work is Windows support. The PR is marked as a draft to enable discussion and see how Windows support can be done.

jerry73204 pushed a commit to jerry73204/tch-nms that referenced this pull request Jan 20, 2022
@jerry73204
Copy link
Contributor Author

I added a demo crate tch-nms that integrates C++/CUDA code with tch-rs.

@jerry73204
Copy link
Contributor Author

I made torch-build crate to be available on crates.io (link) so those willing to write FFI to pytorch C++/CUDA extension to make use of it. The following examples are starters to integrate existing C++/CUDA code with tch.

The patch move most FFI linking code in `build.rs` to a separate
torch-build crate. It paves the way to support 3rd party C++/CUDA
extensions directly linking to libtorch. The patch refactors the
code into this hierarchy.

tch-rs
`-- src <-- torch-sys
            `-- build.rs <-- torch-build

In this way, potential contributors can setup an extension crate in
this layout, where FFI linkage to custom C++/CUDA code is supported by
torch-build.

3rd_party_ext
|-- build.rs <-- torch-build
|-- src      <-- torch-sys
`-- csrc     <-- C++/CUDA code

The torch-build is designed to be idempotent. If there are more than one
crates depend on torch-build. The probing is always done at most once,
so that dependent crates see identical probing information.

The following improvements are done in torch-build compare to original torch-sys.

- Allow more control on building process through "download-libtorch" and
  "parallel-build" Cargo features.
- Automatic CXX11 ABI probing.
- Rewrite CUDA shared library probing.
  - Probe system device to compile correct arches.
  - Probe AMD HIP, CUDA and separated CPU/CUDA libraries and link
    accordingly.
- Expose public API to let users to obtain detailed probing information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant