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

Updated C wrapper wrt. Torch v1.10 #61

Merged
merged 12 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .dev/install_build_deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

GCC_VERSION=$1

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
cmake \
gcc-$GCC_VERSION \
g++-$GCC_VERSION \
jq \
unzip \
wget
27 changes: 27 additions & 0 deletions .dev/install_cuda_sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -e

CUDA_VERSION=$1

CUDA_VERSION_MAJOR_MINOR=$(echo $CUDA_VERSION | cut -d . -f 1-2)
TMP_PROJECT=$(mktemp -d)
cd $TMP_PROJECT
touch Project.toml
cat <<EOT > LocalPreferences.toml
[CUDA_Runtime_jll]
version = "$CUDA_VERSION_MAJOR_MINOR"
EOT
CUDA_ROOT=$(julia --project --eval '
using Pkg
CUDA_VERSION = VersionNumber(ENV["CUDA_VERSION"])
CUDA_SDK_jll_pkg = :CUDA_SDK_jll
if CUDA_VERSION < v"11.4"
CUDA_SDK_jll_pkg = :CUDA_full_jll
end
Pkg.add(name=string(CUDA_SDK_jll_pkg), version=ENV["CUDA_VERSION"])
@eval using $CUDA_SDK_jll_pkg
println(@eval $CUDA_SDK_jll_pkg.artifact_dir)
')
ln -s $CUDA_ROOT/cuda /usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin
30 changes: 30 additions & 0 deletions .dev/install_cudnn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

set -e

CUDA_VERSION=$1
CUDNN_VERSION=$2

CUDA_VERSION_MAJOR_MINOR=$(echo $CUDA_VERSION | cut -d . -f 1-2)
TMP_PROJECT=$(mktemp -d)
cd $TMP_PROJECT
touch Project.toml
cat <<EOT > LocalPreferences.toml
[CUDA_Runtime_jll]
version = "$CUDA_VERSION_MAJOR_MINOR"
EOT
CUDNN_ROOT=$(julia --project --eval '
using Pkg;
CUDA_VERSION = VersionNumber(ENV["CUDA_VERSION"])
if CUDA_VERSION < v"11"
Pkg.add(name="CUDA_Runtime_jll", version="0.2")
elseif CUDA_VERSION < v"11.4"
Pkg.add(name="CUDA_Runtime_jll", version="0.7")
else
Pkg.add(name="CUDA_Runtime_jll")
end
Pkg.add(name="CUDNN_jll", version=ENV["CUDNN_VERSION"]);
using CUDNN_jll;
println(CUDNN_jll.artifact_dir)')
for F in $CUDNN_ROOT/include/cudnn*.h; do ln -sf $F /usr/local/cuda/include/$(basename $F); done
for F in $CUDNN_ROOT/lib/libcudnn*; do ln -sf $F /usr/local/cuda/lib64/$(basename $F); done
11 changes: 11 additions & 0 deletions .dev/install_torch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

TORCH_VARIANT=$1
TORCH_VERSION=$2

cd /usr/local
wget -q "https://download.pytorch.org/libtorch/$TORCH_VARIANT/libtorch-cxx11-abi-shared-with-deps-$TORCH_VERSION%2B$TORCH_VARIANT.zip"
unzip -q libtorch-*.zip
rm libtorch-*.zip
43 changes: 33 additions & 10 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
ARG BASE_IMAGE_TAG
ARG BASE_IMAGE_VARIANT=debian
ARG BASE_IMAGE_VERSION=11

ARG BASE_IMAGE_TAG=$BASE_IMAGE_VARIANT-$BASE_IMAGE_VERSION

FROM mcr.microsoft.com/devcontainers/cpp:$BASE_IMAGE_TAG

ARG OCAML_VERSION
ARG OPAM_VERSION
ARG TORCH_VERSION
ARG CUDA_VERSION=11.8.0
ARG CUDNN_VERSION=8.9.4

ARG GCC_VERSION=10

ARG OCAML_VERSION=4
ARG OPAM_VERSION=2

ARG TORCH_VARIANT
ARG TORCH_VERSION=2.1.1

COPY .dev /opt/container

RUN sudo apt-get update \
&& sudo apt-get satisfy -y "ocaml (>= $OCAML_VERSION)" "opam (>= $OPAM_VERSION)" \
RUN /opt/container/install_build_deps.sh $GCC_VERSION \
&& apt-get satisfy -y "ocaml (>= $OCAML_VERSION)" "opam (>= $OPAM_VERSION)" \
&& rm -rf /var/lib/apt/lists/*

RUN cd /usr/local \
&& sudo wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-$TORCH_VERSION%2Bcpu.zip \
&& sudo unzip libtorch-*.zip \
&& sudo rm libtorch-*.zip
ENV JULIA_DEPOT_PATH=/opt/julia_depot
ENV JULIAUP_DEPOT_PATH=/opt/juliaup
RUN curl -fsSL https://install.julialang.org | sh -s -- --default-channel 1.9 --path /opt/juliaup --yes
ENV PATH=/opt/juliaup/bin:$PATH

ENV CUDA_VERSION=$CUDA_VERSION
ENV CUDNN_VERSION=$CUDNN_VERSION

RUN /opt/container/install_cuda_sdk.sh $CUDA_VERSION
ENV PATH=$PATH:/usr/local/cuda/bin

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN export TORCH_VARIANT="cu$(echo $CUDA_VERSION | cut -d . -f 1-2 | tr -d '.')" \
&& /opt/container/install_torch.sh $TORCH_VARIANT $TORCH_VERSION

ENV CMAKE_PREFIX_PATH=/usr/local/libtorch
16 changes: 11 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"build": {
"context": "..",
"dockerfile": "Dockerfile",
"args": {
"BASE_IMAGE_TAG": "debian-11",
"BASE_IMAGE_VARIANT": "debian",
"BASE_IMAGE_VERSION": "11",
"CUDA_VERSION": "11.3.1",
"CUDNN_VERSION": "8.2.4",
"GCC_VERSION": "10",
"OCAML_VERSION": "4",
"OPAM_VERSION": "2",
"TORCH_VERSION": "1.4.0"
"TORCH_VERSION": "1.10.2"
}
},
"customizations": {
"vscode": {
"extensions": [
"julialang.language-julia",
"ms-vscode.cpptools-extension-pack"
]
}
},
"features": {
"ghcr.io/julialang/devcontainer-features/julia:1": {}
"hostRequirements": {
"gpu": "optional"
},
"postCreateCommand": "opam init --auto-setup"
"postCreateCommand": ".devcontainer/postCreate.sh"
}
5 changes: 5 additions & 0 deletions .devcontainer/postCreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo chown -R vscode:vscode /opt/juliaup /opt/julia_depot

./.dev/install_cudnn.sh $CUDA_VERSION $CUDNN_VERSION

opam init --disable-sandboxing --auto-setup
97 changes: 97 additions & 0 deletions .github/workflows/BuildCWrapper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Build C Wrapper

on:
push:
branches:
- master
tags: ['*']
pull_request:
workflow_dispatch:

concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

jobs:
build_cpu:
name: Build (CPU)
runs-on: ubuntu-latest
timeout-minutes: 60
permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created
actions: write
contents: read
container:
image: debian:10
env:
GCC_VERSION: "8"
TORCH_VARIANT: cpu
TORCH_VERSION: "1.10.2"
USE_CUDA: "OFF"
steps:
- uses: actions/checkout@v4
- run: ./.dev/install_build_deps.sh $GCC_VERSION
- name: Install Torch
run: ./.dev/install_torch.sh $TORCH_VARIANT $TORCH_VERSION
- name: Build
run: |
export CMAKE_PREFIX_PATH=/usr/local/libtorch
cd deps/c_wrapper
cmake -S . -B build -DUSE_CUDA=$USE_CUDA
cmake --build build
build_cuda:
name: Build (CUDA ${{ matrix.cuda_version }}, CUDNN ${{ matrix.cudnn_version }}, GCC ${{ matrix.gcc_version }}, Torch ${{ matrix.torch_version }})
runs-on: ubuntu-latest
timeout-minutes: 60
permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created
actions: write
contents: read
strategy:
fail-fast: false
matrix:
arch:
- x64
cuda_version:
- "10.2.89"
- "11.3.1"
cudnn_version:
- "8.2.4"
gcc_version:
- "8"
julia_version:
- "1.9"
os_version:
- "10"
torch_version:
- "1.10.2"
container:
image: debian:${{ matrix.os_version }}
env:
CUDA_VERSION: ${{ matrix.cuda_version }}
CUDNN_VERSION: ${{ matrix.cudnn_version }}
GCC_VERSION: ${{ matrix.gcc_version }}
TORCH_VERSION: ${{ matrix.torch_version }}
USE_CUDA: "ON"
steps:
- uses: actions/checkout@v4
- run: ./.dev/install_build_deps.sh $GCC_VERSION
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.julia_version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- name: Install CUDA SDK ${{ matrix.cuda_version }} and CUDNN ${{ matrix.cudnn_version }}
run: |
./.dev/install_cuda_sdk.sh $CUDA_VERSION
./.dev/install_cudnn.sh $CUDA_VERSION $CUDNN_VERSION
- name: Install Torch
run: |
export TORCH_VARIANT="cu$(echo $CUDA_VERSION | cut -d . -f 1-2 | tr -d '.')"
./.dev/install_torch.sh $TORCH_VARIANT $TORCH_VERSION
- name: Build
run: |
export CMAKE_PREFIX_PATH=/usr/local/libtorch
cd deps/c_wrapper
cmake -S . -B build -DUSE_CUDA=$USE_CUDA
cmake --build build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/Manifest.toml
/build
/.vscode
10 changes: 8 additions & 2 deletions deps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ Since Torch is a C++-library, a C wrapper is needed for Julia to interact with T

The C wrapper can be generated from the `Declarations.yaml`-file included with `Torch_jll`:
```sh
mkdir c_wrapper_generator/data
curl https://raw.githubusercontent.com/LaurentMazare/ocaml-torch/main/third_party/pytorch/Declarations-v1.4.0.yaml -o c_wrapper_generator/data/Declarations.yaml
mkdir -p c_wrapper_generator/data
cp -v `julia --eval '
using Pkg
Pkg.activate(; temp=true)
Pkg.add(name="Torch_jll", version="1.10")
import Torch_jll
print(joinpath(dirname(Torch_jll.libtorch_path), "..", "share", "ATen", "Declarations.yaml"))
'` c_wrapper_generator/data/
```

The C wrapper can then be generated by building and running the (OCaml-based) C wrapper generator, e.g. by using the dev. container (which includes OCaml and OPAM):
Expand Down
16 changes: 12 additions & 4 deletions deps/c_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(doeye_caml)
project(torch_c_api)

option(USE_CUDA "Use CUDA" ON)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR};${CMAKE_MODULE_PATH}")

find_package(Torch REQUIRED)

include_directories(SYSTEM path)

add_library(doeye_caml "SHARED" torch_api.cpp)
target_link_libraries(doeye_caml "${TORCH_LIBRARIES}")
add_library(torch_c_api "SHARED" torch_api.cpp)
target_link_libraries(torch_c_api "${TORCH_LIBRARIES}")

if(USE_CUDA)
enable_language(CUDA)
target_compile_definitions(torch_c_api PRIVATE USE_CUDA)
target_include_directories(torch_c_api PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
endif()

set_property(TARGET doeye_caml PROPERTY CXX_STANDARD 14)
set_property(TARGET torch_c_api PROPERTY CXX_STANDARD 14)
Loading
Loading