diff --git a/.github/workflows/build_docs.yml b/.github/workflows/build_docs.yml new file mode 100644 index 00000000..d0dcb9dc --- /dev/null +++ b/.github/workflows/build_docs.yml @@ -0,0 +1,55 @@ +# workflow to build and deploy FORD docs for FTorch + +name: BuildDocs + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the "main" branch + push: + branches: [ "main"] + pull_request: + branches: [ "main" ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# Workflow run - one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build-docs" + build-docs: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install ford + + - name: Build docs with FORD + run: ford FTorch.md + + - name: Test docs build + if: github.ref != 'refs/heads/main' + uses: JamesIves/github-pages-deploy-action@v4 + with: + branch: docs-test # The branch the action should deploy to. + folder: doc # The folder the action should deploy. + dry-run: true # Don't actually push to pages, just test if we can + + - name: Deploy Documentation for main + if: github.ref == 'refs/heads/main' + uses: JamesIves/github-pages-deploy-action@v4 + with: + branch: gh-pages # The branch the action should deploy to. + folder: doc # The folder the action should deploy. diff --git a/.gitignore b/.gitignore index a20ace75..870daca0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Documentation files +/doc/* + # Prerequisites *.d diff --git a/FTorch.md b/FTorch.md new file mode 100644 index 00000000..a562ee2a --- /dev/null +++ b/FTorch.md @@ -0,0 +1,76 @@ +--- +project: FTorch +summary: A library for coupling (Py)Torch machine learning models to Fortran +author: ICCS Cambridge +license: mit +github: https://github.com/Cambridge-ICCS +project_github: https://github.com/Cambridge-ICCS/FTorch +page_dir: pages +src_dir: ./src + ./utils +output_dir: ./doc +exclude_dir: **/build* +extra_filetypes: c // + cpp // + h // + py # +sort: alpha +source: true +graph: true +externalize: true +md_extensions: markdown.extensions.toc + markdown.extensions.tables + markdown.extensions.fenced_code +--- + +-------------------- + +[TOC] + +Brief description +----------------- + +It is desirable to be able to run machine learning (ML) models directly in Fortran. +ML models are often trained in some other language (say, Python) using a popular frameworks (say, PyTorch) and saved. +We want to run inference on this model without having to call a Python executable. +To achieve this we use the existing Torch C++ interface, libtorch. + +FTorch provides a library enabling a user to directly couple their PyTorch models to Fortran code. +There are also installation instructions for the library and examples of performing coupling. + +We support running on both CPU and GPU, and have tested the library on UNIX and Windows based operating systems + +Presentations +------------- + +The following presentations contain information about FTorch: + +* Reducing the overheads for coupling PyTorch machine learning models to Fortran\ + ML & DL Seminars, LSCE, IPSL, Paris - November 2023\ + [Slides](IPSL_FTorch/IPSL_FTorch.html) - [Recording](https://www.youtube.com/watch?v=-NJGuV6Rz6U) +* Reducing the Overhead of Coupled Machine Learning Models between Python and Fortran\ + RSECon23, Swansea - September 2023\ + [Slides](https://jackatkinson.net/slides/RSECon23/RSECon23.html) - [Recording](https://www.youtube.com/watch?v=Ei6H_BoQ7g4&list=PL27mQJy8eDHmibt_aL3M68x-4gnXpxvZP&index=33) + +License +------- + +The FTorch source code, related files and documentation are +distributed under an [MIT License which can be viewed here](page/LICENSE.html). + + +Projects using FTorch +--------------------- + +The following projects make use of FTorch. +If you use our library in your work please let us know. + +* [M2LInES CAM-ML](https://github.com/m2lines/CAM-ML)\ + Using FTorch to couple a neural net parameterisation of convection to the CAM + atmospheric model in CESM. +* [DataWave CAM-GW](https://github.com/DataWaveProject/CAM/)\ + Using FTorch to couple neural net parameterisations of gravity waves to the CAM + atmospheric model in CESM. +* [MiMA Machine Learning](https://github.com/DataWaveProject/MiMA-machine-learning)\ + Using FTorch to couple a neural net parameterisation of gravity waves to the MiMA + atmospheric model. diff --git a/README.md b/README.md index 69df0dcb..efb52443 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,15 @@ This repository contains code, utilities, and examples for directly calling PyTorch ML models from Fortran. +For full API and user documentation please see the +[online documentation](https://cambridge-iccs.github.io/FTorch/) which is +significantly more detailed than this README. ## Contents - [Description](#description) - [Installation](#installation) - [Usage](#usage) +- [GPU Support](#gpu-support) - [Examples](#examples) - [License](#license) - [Contributions](#contributions) @@ -21,14 +25,41 @@ models from Fortran. ## Description It is desirable to be able to run machine learning (ML) models directly in Fortran. -Such models are often trained in some other language (say Python) using popular frameworks (say PyTorch) and saved. +Such models are often trained in some other language (say Python) using popular +frameworks (say PyTorch) and saved. We want to run inference on this model without having to call a Python executable. To achieve this we use the existing Torch C++ interface. -This project provides a library enabling a user to directly couple their PyTorch models to Fortran code. -We provide installation instructions for the library as well as instructions and examples for performing coupling. +This project provides a library enabling a user to directly couple their PyTorch +models to Fortran code. +We provide installation instructions for the library as well as instructions and +examples for performing coupling. -Project status: This project is currently in pre-release with documentation and code being prepared for a first release. +```fortran +use ftorch +... +type(torch_module) :: model +type(torch_tensor), dimension(n_inputs) :: model_input_arr +type(torch_tensor) :: model_output +... +model = torch_module_load("/my/saved/TorchScript/model.pt") +model_input_arr(1) = torch_tensor_from_array(input_fortran, in_layout, torch_kCPU) +model_output = torch_tensor_from_array(output_fortran, out_layout, torch_kCPU) + +call torch_module_forward(model, model_input_arr, n_inputs, model_output) +``` + +The following presentations provide an introduction and overview of _FTorch_: + +* Reducing the overheads for coupling PyTorch machine learning models to Fortran\ + ML & DL Seminars, LSCE, IPSL, Paris - November 2023\ + [Slides](IPSL_FTorch/IPSL_FTorch.html) - [Recording](https://www.youtube.com/watch?v=-NJGuV6Rz6U) +* Reducing the Overhead of Coupled Machine Learning Models between Python and Fortran\ + RSECon23, Swansea - September 2023\ + [Slides](https://jackatkinson.net/slides/RSECon23/RSECon23.html) - [Recording](https://www.youtube.com/watch?v=Ei6H_BoQ7g4&list=PL27mQJy8eDHmibt_aL3M68x-4gnXpxvZP&index=33) + +Project status: This project is currently in pre-release with documentation and code +being prepared for a first release. As such breaking changes may be made. If you are interested in using this library please get in touch. @@ -48,45 +79,46 @@ To install the library requires the following to be installed on the system: #### Windows Support -If building in a windows environment then you can either: - -1) Use [Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/) (WSL) - In this case the build process is the same as for Linux environment. -2) Use Visual Studio and the Intel Fortran Compiler - In this case you must install [Visual Studio](https://visualstudio.microsoft.com/) followed by [Intel OneAPI Base and HPC toolkit](https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit-download.html) (ensure that the Intel Fortran compiler and VS integration is selected in the latter). - +If building in a Windows environment then you can either use +[Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/) (WSL) +or Visual Studio and the Intel Fortran Compiler. +For full details on the process see the +[online Windows documentation](https://cambridge-iccs.github.io/FTorch/page/troubleshooting.html#windows).\ Note that libTorch is not supported for the GNU Fortran compiler with MinGW. #### Apple Silicon Support -At the time of writing, libtorch is currently only officially available for x86 architectures (according to https://pytorch.org/). However, the version of PyTorch provided by `pip install` provides an ARM binary for libtorch which works on Apple Silicon. Therefore you should `pip install` PyTorch in this situation and follow the guidance below on locating Torch for cmake. +At the time of writing, libtorch is only officially available for x86 architectures +(according to https://pytorch.org/). However, the version of PyTorch provided by +`pip install torch` uses an ARM binary for libtorch which works on Apple Silicon. ### Library installation +For detailed installation instructions please see the +[online installation documentation](https://cambridge-iccs.github.io/FTorch/page/cmake.html). + To build and install the library: 1. Navigate to the location in which you wish to install the source and run: - ```bash + ``` git clone git@github.com:Cambridge-ICCS/FTorch.git ``` to clone via ssh, or - ```bash + ``` git clone https://github.com/Cambridge-ICCS/FTorch.git ``` to clone via https. -2. Navigate into the library directory by running: - ```bash +2. Navigate to the source directory by running: + ``` cd FTorch/src/ ``` -3. Create a `build` directory and execute cmake from within it using the relevant options (see below): - ```bash +3. Build the library using CMake with the relevant options from the table below: + ``` mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release ``` - If building on Windows you will need to add `-G "NMake Makefiles"` to the `cmake` command. Also you will need to load the - intel fortran compilers using `setvars.bat` ([see Intel docs](https://www.intel.com/content/www/us/en/docs/oneapi/programming-guide/2023-2/use-the-setvars-script-with-windows.html)) which is found in the Intel compiler install directory. - + The following table of CMake options are available to be passed as arguments to `cmake` through `-D