Skip to content

Documentation: properly document installation from source (without binaries) #1229

@s-u

Description

@s-u

Since binaries are not provided for current systems (e.g., nVidia only supports CUDA 12.6 on Ubuntu 24.04), it is necessary to build lantern and/or libtorch from sources, but that is entirely undocumented - both on the torch side as well as on the libtorch side. It would be nice to have some documentation for both steps and how to properly integrate them such that torch doesn't try to look for non-existent binaries.

In the hope of being useful, I'm attaching a script with which I managed to get a working torch installation from sources, but I had to reverse-engineer ci scripts both in pytorch and in torch to do that as neither is documented.

## make sure we have CUDA
export PATH=/usr/local/cuda/bin:$PATH
CUDA=$(nvcc --version | sed -nE 's:.*release ([0-9.]+).*:\1:p')
echo Building with CUDA-$CUDA

## get pytorch sources - here we go for the 2.6 release branch
git clone --branch release/2.6 --recursive https://github.com/pytorch/pytorch.git
## create venv for the build tools and dependencies
python3 -m venv pytorch-build-env
. pytorch-build-env/bin/activate
export CMAKE_PREFIX_PATH=$(pwd)/pytorch-build-env

## install dependencies
cd pytorch
pip install cmake ninja
pip install -r requirements.txt
pip install mkl-static mkl-include
make triton

## build pytorch (see docs for more torch options)
export _GLIBCXX_USE_CXX11_ABI=1
python setup.py install

## create libtorch
## this is from .ci/manywheel/build_libtorch.sh
## no idea why this is not part of the pytorch sources ...
mkdir -p libtorch/{lib,bin,include,share}

# Copy over all lib files
cp -rv build/lib/*                libtorch/lib/
cp -rv build/lib*/torch/lib/*     libtorch/lib/

# Copy over all include files
cp -rv build/include/*            libtorch/include/
cp -rv build/lib*/torch/include/* libtorch/include/

# Copy over all of the cmake files
cp -rv build/lib*/torch/share/*   libtorch/share/

## save the location so we can use it later
export TORCH_PATH=$(pwd)/libtorch

##-- now into the "torch" R package
cd ..

## just to make things a bit faster
export MAKEFLAGS=-j64

## there are no release branches, so go for main
git clone https://github.com/mlverse/torch.git

## build lantern
cd torch/src/lantern
## bugfix: this was need since getDefaultCUDAGenerator doesn't exist in CUDAHooks
## (confused with at::cuda::detail::getDefaultCUDAGenerator() perhaps?)
sed -i 's:getDefaultCUDAGenerator:getDefaultGenerator:g' src/Cuda.cpp
mkdir build
cd build
## make sure CUDA from the top of the script is correct
## otherwise lantern will fail to find some deps
CUDA=$CUDA cmake -DTORCH_PATH=${TORCH_PATH} ..
make $MAKEFLAGS
make install DESTDIR=`pwd`/dst
LANTERN_ROOT=`pwd`/dst/usr/local

## back to torch root
cd ../../..

## copy libtorch and lantern into inst/lib
mkdir inst/lib
cp ${TORCH_PATH}/lib/*.so inst/lib/
cp ${LANTERN_ROOT}/lib/*.so inst/lib/

## done, phew -- just install
cd ..
R CMD INSTALL torch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions