Skip to content

Cambridge-ICCS/FTorch

Repository files navigation

logo

A library for coupling (Py)Torch machine learning models to Fortran

GitHub Fortran DOI GitHub Actions Workflow Status

This repository contains code, utilities, and examples for directly using PyTorch ML models from Fortran.

For full API and user documentation please see the online documentation which is significantly more detailed than this README.

To cite use of this software please refer to Atkinson et al., (2025) DOI:10.21105/joss.07602. See acknowledgment below for more details.

Contents

Description

It is desirable to run machine learning (ML) models directly in Fortran. ML models are often trained in some other language using popular frameworks (say, Python and PyTorch) however.

FTorch is a library enabling users to directly couple their PyTorch models to Fortran code. It supports running on both CPU and GPU, and is tested on UNIX and Windows operating systems We provide installation instructions for the library as well as comprehensive examples for performing coupling.

use ftorch
...
type(torch_model) :: model
type(torch_tensor), dimension(n_inputs)  :: input_tensors
type(torch_tensor), dimension(n_outputs) :: output_tensors
...
call torch_model_load(model, "/my/saved/TorchScript/model.pt", torch_kCPU)
call torch_tensor_from_array(input_tensors(1), input_fortran, torch_kCPU)
call torch_tensor_from_array(output_tensors(1), output_fortran, torch_kCPU)

call torch_model_forward(model, input_tensors, output_tensors)

A recent talk providing an introduction and overview of FTorch can be seen here: Slides - Recording See our presentations page for more examples.

Installation

Dependencies

Build and Install

Detailed installation instructions are provided in the online installation documentation.

The following instructions assume a Unix system. For installation on Windows, Apple Silicon, Conda Environments, or Codespaces refer to the system-specific guidance.

The installation process has three main steps:

  1. Fetch the source code from GitHub via git,
  2. Navigate to the root FTorch directory and create a build directory,
  3. Build and install the library using CMake with the relevant options.
git clone https://github.com/Cambridge-ICCS/FTorch.git
cd FTorch/
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=</path/to/torch/installation>
cmake --build . --target install

This will place the library files at the install location which can be tailored using the CMAKE_INSTALL_PREFIX option.

The following table of CMake options are available to be passed as arguments to cmake through -D<Option>=<Value>.
It is likely that you will need to provide at least CMAKE_PREFIX_PATH.

Option Value Description
CMAKE_Fortran_COMPILER gfortran / ifx / ifort 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.
CMAKE_C_COMPILER gcc / icx / icc Specify a C compiler to build the library with.
CMAKE_CXX_COMPILER g++ / icx / icpc Specify a C++ compiler to build the library with.
CMAKE_PREFIX_PATH </path/to/LibTorch/> Location of Torch installation1
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
BUILD_SHARED_LIBS ON / OFF Specifies whether to build FTorch as a shared library. The default is ON, use OFF to build FTorch as a static library.
CMAKE_BUILD_TESTS TRUE / FALSE Specifies whether to compile FTorch's test suite as part of the build.
GPU_DEVICE NONE / CUDA / HIP / XPU / MPS Specifies the target GPU backend architecture (if any)
MULTI_GPU ON / OFF Specifies whether to build the tests that involve multiple GPU devices (ON by default if CMAKE_BUILD_TESTS and GPU_DEVICE are set).

1 _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. with pip install torch, then this should be </path/to/venv/>lib/python<3.xx>/site-packages/torch/.

Usage

In order to use FTorch users will typically need to follow these steps:

  1. Save a PyTorch model as TorchScript.
  2. Write Fortran using the FTorch bindings to use the model from within Fortran.
  3. Build and compile the code, linking against the FTorch library

These steps are described in more detail in the online documentation

GPU Support

FTorch supports running on multiple GPU hardwares including CUDA (NVIDIA), HIP (AMD/ROCm), MPS (Apple Silicon) and XPU (Intel). It also supports running on multiple devices.

For detailed guidance about running on GPU, including instructions for using multiple devices, please see the online GPU documentation.

Examples

Examples of how to use this library are provided in the examples directory.
They demonstrate different functionalities of the code and are provided with instructions to modify, build, and run as necessary.

Tests

For information on testing, see the corresponding webpage or the README in the test subdirectory.

License

Copyright © ICCS

FTorch is distributed under the MIT Licence.

Contributions

Contributions and collaborations are welcome.

For bugs, feature requests, and clear suggestions for improvement please open an issue.

If you have built something upon FTorch that would be useful to others, or can address an open issue, please fork the repository and open a pull request.

Detailed guidelines can be found in the online developer documentation.

If you are interested in using FTorch please get in touch.

Code of Conduct

Everyone participating in the FTorch project, and in particular in the issue tracker, pull requests, and social media activity, is expected to treat other people with respect and, more generally, to follow the guidelines articulated in the Python Community Code of Conduct.

Authors and Acknowledgment

FTorch is written and maintained by the ICCS

To cite FTorch in research please refer to:

Atkinson et al., (2025). FTorch: a library for coupling PyTorch models to Fortran. Journal of Open Source Software, 10(107), 7602, https://doi.org/10.21105/joss.07602

See the CITATION.cff file or click 'Cite this repository' on the right.

See Contributors for a full list of contributors.

Used by

Multiple projects build on FTorch in their work. Those we are aware of are listed on the use cases page in the online documentation.