FTorch: a library for coupling PyTorch models to Fortran

Jack Atkinson, Athena Elafrou, Elliott Kasoar, Joseph G. Wallwork, Thomas Meltzer, Simon Clifford, Dominic Orchard, Chris EdsallJ. Open Source Softw 2025 · DOI · DBLP

Abstract

High-performance computing (HPC) often faces a "two-language problem," where legacy scientific simulations are written in Fortran while modern machine learning (ML) developments occur in Python. FTorch addresses this by providing a performant, native-feeling bridge that allows Fortran codes to call PyTorch models directly. By leveraging the LibTorch C++ backend and TorchScript, FTorch eliminates Python runtime overhead and enables zero-copy data transfer, making it suitable for large-scale scientific emulation and parameterization.

Introduction

Large-scale scientific simulations in fields such as climate modeling, fluid dynamics, and materials science are predominantly written in Fortran due to its efficiency and native array support. Conversely, the ML ecosystem is centered around Python and frameworks like PyTorch. Integrating these two domains typically requires either re-implementing models in Fortran—a process prone to error—or calling Python from Fortran, which introduces significant performance bottlenecks and complex dependency management on HPC systems.

FTorch solves this by acting as a wrapper for LibTorch, the C++ distribution of PyTorch. It allows researchers to train models in Python and deploy them directly within Fortran simulations, bypassing the Python interpreter entirely.

Implementation and Methods

FTorch's architecture is designed for high performance and ease of use within existing HPC workflows. Key technical features include:

  • C Interoperability: Utilizing the standard iso_c_binding module (Fortran 2003+), FTorch interfaces directly with the LibTorch C++ API.
  • TorchScript Serialization: Models are trained in Python and exported as TorchScript files. These serialized models can be loaded and executed by the C++ backend without a Python environment.
  • Zero-Copy Data Transfer: To minimize overhead, FTorch supports shared memory. Fortran simulations and ML models can operate on the same memory addresses, avoiding costly data duplication for large multidimensional arrays.
  • Metaprogramming: The library uses the fypp preprocessor to automatically generate Fortran code for various data types and array ranks (up to 15D), ensuring broad compatibility with scientific data structures.
  • Hardware Agnostic Execution: By sitting atop LibTorch, FTorch inherits support for diverse hardware, including CPUs, NVIDIA GPUs (via CUDA), Intel XPUs, and Apple MPS, without requiring device-specific code from the user.

Applications in Scientific Computing

FTorch is currently deployed in several high-impact Earth system modeling projects:

  • The DataWave Project: Used for exploring gravity wave drag in atmospheric models like MiMA and CAM.
  • ICON (Icosahedral Nonhydrostatic) Model: Facilitates the coupling of U-Net-based models for multi-scale convection.
  • CESM (Community Earth System Model): Provides a general framework for integrating ML components across the Earth system suite.

Results and Discussion

FTorch is a production-ready tool that bridges the gap between traditional simulation and modern ML. Its primary advantages are:

  1. Performance: By avoiding the Python runtime and utilizing zero-copy transfers, it maintains the high throughput required for simulations where ML models are invoked millions of times.
  2. Deployment Simplicity: It simplifies the transition from research (Python) to production (Fortran/HPC) via utility scripts like pt2ts.py.
  3. Portability: Models developed on local workstations can be deployed on supercomputing clusters with minimal architectural adjustments.

Conclusion and Future Work

While FTorch currently focuses on inference, future developments aim to support "online training" by exposing PyTorch’s autograd functionality. This would allow for model updates during the simulation run, enhancing long-term stability and accuracy in coupled physical-ML systems. FTorch remains an open-source, community-driven effort to streamline the integration of deep learning into the scientific computing landscape.

Summary updated

BibTeX
@article{atkinson2025ftorch,
  author = {Jack Atkinson and Athena Elafrou and Elliott Kasoar and Joseph G. Wallwork and Thomas Meltzer and Simon Clifford and Dominic Orchard and Chris Edsall and Jack Atkinson and Athena Elafrou and Elliott Kasoar and Thomas Meltzer and S. F. Clifford and Dominic Orchard and Christopher Edsall},
  title = {{FTorch: a library for coupling PyTorch models to Fortran}},
  journal = {J. Open Source Softw.},
  year = {2025},
  doi = {10.21105/joss.07602},
}
Figure