2025-12-18
To access links or follow on your own device these slides can be found at:
joewallwork.com/pwp/slides/ftorch/2025-12-18_DLR-Seminar
Except where otherwise noted, these presentation materials are licensed under the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License.
Vectors and icons by SVG Repo under CC0(1.0) or FontAwesome under SIL OFL 1.1
Large, complex, many-part systems.

Neural Net by 3Blue1Brown under fair dealing.
Pikachu © The Pokemon Company, used under fair dealing.
Many large scientific models are written in Fortran (or C, or C++).
Much machine learning is conducted in Python.


![]()


![]()


![]()
Mathematical Bridge by cmglee used under CC BY-SA 3.0
PyTorch, the PyTorch logo and any related marks are trademarks of The Linux Foundation.”
TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.
We consider 2 types:
Computational
Developer
In research both have an effect on ‘time-to-science’.
Especially when extensive research software support is unavailable.
iso_c_binding.We will:
libtorch C++libtorch C++ API![]()

![]()

![]()
Python
env
Python
runtime

![]()

xkcd #1987 by Randall Munroe, used under CC BY-NC 2.5
![]()
Build using CMake,
or link via Make like NetCDF (instructions included)
FCFLAGS += -I<path/to/install>/include/ftorch
LDFLAGS += -L<path/to/install>/lib64 -lftorchFind it on :
pt2ts.py aids users in saving PyTorch models to TorchscriptFind it on :
Find it on :
Find it on :
import torch
import torchvision
# Load pre-trained model and put in eval mode
model = torchvision.models.resnet18(weights="IMAGENET1K_V1")
model.eval()
# Create dummmy input
dummy_input = torch.ones(1, 3, 224, 224)
# Save to TorchScript
if trace:
ts_model = torch.jit.trace(model, dummy_input)
elif script:
ts_model = torch.jit.script(model)
frozen_model = torch.jit.freeze(ts_model)
frozen_model.save("/path/to/saved_model.pt")
TorchScript
trace for simple models, script more generally use ftorch
implicit none
real, dimension(5), target :: in_data, out_data ! Fortran data structures
type(torch_tensor), dimension(1) :: input_tensors, output_tensors ! Set up Torch data structures
type(torch_model) :: torch_net
integer, dimension(1) :: tensor_layout = [1]
in_data = ... ! Prepare data in Fortran
! Create Torch input/output tensors from the Fortran arrays
call torch_tensor_from_array(input_tensors(1), in_data, torch_kCPU)
call torch_tensor_from_array(output_tensors(1), out_data, torch_kCPU)
call torch_model_load(torch_net, 'path/to/saved/model.pt', torch_kCPU) ! Load ML model
call torch_model_forward(torch_net, input_tensors, output_tensors) ! Infer
call further_code(out_data) ! Use output data in Fortran immediately
! Cleanup
call torch_delete(model)
call torch_delete(in_tensors)
call torch_delete(out_tensor)Cast Tensors to GPU in Fortran:
! Load in from Torchscript
call torch_model_load(torch_net, 'path/to/saved/model.pt', torch_kCUDA, device_index=0)
! Cast Fortran data to Tensors
call torch_tensor_from_array(in_tensor(1), in_data, torch_kCUDA, device_index=0)
call torch_tensor_from_array(out_tensor(1), out_data, torch_kCPU)
FTorch supports NVIDIA CUDA, AMD HIP, Intel XPU, and AppleSilicon MPS hardwares.
Use of multiple devices supported.
Effective HPC simulation requires MPI_Gather() for efficient data transfer.
FTorch is published in JOSS!
Atkinson et al. (2025)
FTorch: a library for coupling PyTorch models to Fortran.
Journal of Open Source Software, 10(107), 7602,
doi.org/10.21105/joss.07602
Please cite if you use FTorch!

In addition to the comprehensive examples in the FTorch repository we provide an online workshop at /Cambridge-ICCS/FTorch-workshop
forpy in Espinosa et al. (2022)1

![]()

libtorch is included on the software stack on Derecho

Derecho by NCAR
GloSea6 Seasonal Forecasting modelCESM through learning model biases compared to ERA5WaveWatch III modelE3SMICON for stable 20-year AMIP runCAM model.CAM model.Join the FTorch mailing list for updates!
Get in touch:
Thanks to Tom Metlzer, Elliott Kasoar, Niccolò Zanotti
and the rest of the FTorch team.
The ICCS received support from 
FTorch has been supported by 
