$ show Cambridge-ICCS/FTorch
FTorch is a production-stable Fortran library that enables direct coupling of PyTorch machine learning models to Fortran scientific codes without requiring a Python runtime. It provides a comprehensive Fortran interface to LibTorch (PyTorch's C++ backend) for tensor operations, model inference, automatic differentiation, optimizers (SGD, Adam, AdamW), and loss functions (MSE, cross-entropy). The architecture uses a three-layer design: Fortran modules with derived types (torch_tensor, torch_model, torch_optim) that call a thin C API (ctorch.h/ctorch.cpp), which in turn wraps LibTorch. This layered approach isolates Fortran code from C++ ABI changes and allows the library to be built as a shared or static library with CMake, supporting CPU, CUDA, HIP, XPU, and MPS backends. The library includes extensive examples covering tensor manipulation, simple neural networks, ResNet-18 inference, batching, multi-I/O, looping, multi-GPU, MPI parallelism, autograd, and full training loops. Unit tests use pFUnit and integration tests are built from examples, with CI testing across multiple compilers (gfortran, ifx, flang) and platforms. A Python utility package (ftorch_utils) provides model conversion (PyTorch to TorchScript) and validation. FTorch is published in the Journal of Open Source Software (JOSS) and is used in several HPC climate and fusion simulation codes (E3SM, CAM, ICON, UKAEA). The codebase demonstrates strong engineering practices: fypp code generation reduces boilerplate for multiple tensor ranks and data types, C interoperability is handled cleanly via iso_c_binding, and finalizers ensure automatic resource cleanup. However, there are some maintenance risks including reliance on fypp preprocessing, a large CI matrix, and the need to track LibTorch releases. A legacy API (torch_tensor_from_array_legacy) is still present but deprecated. Overall, FTorch is a well-crafted, domain-specific library that fills a critical gap for Fortran-HPC users needing ML integration.
The LLM identified 3 files worth highlighting for reuse:
Provides a pure C API that abstracts LibTorch's C++ interface, enabling Fortran code to call PyTorch functions via iso_c_binding without direct C++ exposure. This pattern of wrapping a C++ library in a C layer is reusable for any Fortran project needing to interface with C++ libraries, and it isolates the Fortran code from C++ ABI instability.
A fypp template that generates the Fortran tensor module with overloaded procedures for 5 array ranks and 6 data types (int8 to float64). This non-obvious use of a Fortran preprocessor dramatically reduces code duplication and maintenance burden. The pattern can be extracted and applied to any Fortran library that needs type/rank polymorphism without relying on modern Fortran generics.
Provides reusable Fortran functions `isclose` and `allclose` for comparing floating-point arrays with relative and absolute tolerances, along with a colored test output helper. This is a self-contained, dependency-free module that can be extracted into any Fortran project for robust numerical testing, similar to numpy's allclose.