Installation of FTorch is done by CMake.
This is controlled by the CMakeLists.txt
file in src/
.
To install the library requires the following to be installed on the system:
To build the library, first clone it from GitHub to your local machine and then run:
cd FTorch/src/
mkdir build
cd build
Then invoke CMake with the Release
build option, plus any other options as required
from the table below in CMake build options
(note: you will likely need to add some of these options to enforce a consistent
build on your machine):
cmake .. -DCMAKE_BUILD_TYPE=Release
Finally build and install the library using:
cmake --build . --target install
or, if you want to separate these steps:
cmake --build .
cmake --install .
Note: if you are building on Windows please refer to the Windows install guidance as the process will likely differ from the UNIX-based stsyems covered here.
It is likely that you will need to provide at least the CMAKE_PREFIX_PATH
flag.
The following CMake flags are available and can be passed as arguments through -D<Option>=<Value>
:
Option | Value | Description |
---|---|---|
CMAKE_Fortran_COMPILER |
ifort / gfortran |
Specify a Fortran compiler to build the library with. This should match the Fortran compiler you're using to build the code you are calling this library from.1 |
CMAKE_C_COMPILER |
icc / gcc |
Specify a C compiler to build the library with.1 |
CMAKE_CXX_COMPILER |
icpc / g++ |
Specify a C++ compiler to build the library with.1 |
CMAKE_PREFIX_PATH |
</path/to/libTorch/> |
Location of Torch installation2 |
CMAKE_INSTALL_PREFIX |
</path/to/install/lib/at/> |
Location at which the library files should be installed. By default this is /usr/local |
CMAKE_BUILD_TYPE |
Release / Debug |
Specifies build type. The default is Debug , use Release for production code |
CMAKE_BUILD_TESTS |
TRUE / FALSE |
Specifies whether to compile FTorch's test suite as part of the build. |
ENABLE_CUDA |
TRUE / FALSE |
Specifies whether to check for and enable CUDA3 |
1 On Windows this may need to be the full path to the compiler if CMake cannot locate it by default. 2 The path to the Torch installation needs to allow CMake to locate the relevant Torch CMake files.
If Torch has been installed as libtorch then this should be the absolute path to the unzipped libtorch distribution. If Torch has been installed as PyTorch in a Python venv (virtual environment), e.g. withpip install torch
, then this should be</path/to/venv/>lib/python<3.xx>/site-packages/torch/
.3 This is often overridden by PyTorch. When installing with pip, the
index-url
flag can be used to ensure a CPU or GPU only version is installed, e.g.pip install torch --index-url https://download.pytorch.org/whl/cpu
orpip install torch --index-url https://download.pytorch.org/whl/cu118
(for CUDA 11.8). URLs for alternative versions can be found here.
For example, to build on a unix system using the gnu compilers and install to $HOME/FTorchbin/
we would need to run:
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_Fortran_COMPILER=gfortran \
-DCMAKE_C_COMPILER=gcc \
-DCMAKE_CXX_COMPILER=g++ \
-DCMAKE_PREFIX_PATH=/path/to/venv/lib/python3.xx/site-packages/torch/ \
-DCMAKE_INSTALL_PREFIX=~/FTorchbin
Once this completes you should be able to generate the code and install using:
cmake --build . --target install
Note: If you are using CMake < 3.15 then you will need to build and install separately using the make system specific commands. For example, if using
make
on UNIX this would be:
make
make install
Installation will place the following directories at the install location:
CMAKE_INSTALL_PREFIX/include/
- contains C header and Fortran mod filesCMAKE_INSTALL_PREFIX/lib64/
- contains cmake
directory and .so
filesNote: In a Windows environment this will require administrator privileges for the default install location.
We generally advise building projects that make use of FTorch with CMake where possible.
If doing this you need to include the following in the CMakeLists.txt
file to
find the FTorch installation and link it to the executable.
find_package(FTorch)
target_link_libraries( <executable> PRIVATE FTorch::ftorch )
message(STATUS "Building with Fortran PyTorch coupling")
You will then need to use the -DFTorch_DIR=</path/to/install/location>
flag
when running CMake.
To build a project with make you need to include the FTorch library when compiling and link the executable against it.
To compile with make add the following compiler flag when compiling files that use ftorch:
FCFLAGS += -I<path/to/install/location>/include/ftorch
When compiling the final executable add the following link flag:
LDFLAGS += -L<path/to/install/location>/lib64 -lftorch
You may also need to add the location of the .so
files to your LD_LIBRARY_PATH
unless installing in a default location:
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:<path/to/installation>/lib64