├── .github └── workflows │ └── tests.yml ├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md └── fortran-tf-lib ├── CMakeLists.txt ├── README.md ├── my_model ├── keras_metadata.pb ├── saved_model.pb └── variables │ ├── variables.data-00000-of-00001 │ └── variables.index ├── src └── fortran_tensorflow_lib.F90 └── tests ├── gen_model.py ├── generated_code_test ├── CMakeLists.txt └── test_stub.F90 ├── load_model_c.c ├── load_model_f.F90 ├── load_model_py.py └── load_wavenet.F90 /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | # This is a basic workflow to help you get started with Actions 2 | 3 | name: Runtests 4 | 5 | # Controls when the workflow will run 6 | on: 7 | # Triggers the workflow on push or pull request events but only for the "main" branch 8 | push: 9 | branches: [ "tensorflow" ] 10 | pull_request: 11 | branches: [ "tensorflow" ] 12 | 13 | # Allows you to run this workflow manually from the Actions tab 14 | workflow_dispatch: 15 | branches: [ "tensorflow" ] 16 | 17 | # A workflow run is made up of one or more jobs that can run sequentially or in parallel 18 | jobs: 19 | # This workflow contains a single job called "build" 20 | build: 21 | # The type of runner that the job will run on 22 | runs-on: ubuntu-latest 23 | 24 | # Steps represent a sequence of tasks that will be executed as part of the job 25 | steps: 26 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 27 | - uses: actions/checkout@v3 28 | - uses: actions/setup-python@v4 29 | 30 | # Install dependencies 31 | - name: apt install deps 32 | run: sudo apt install gfortran 33 | 34 | # Runs a set of commands using the runners shell 35 | - name: Run the pytest tests in tools 36 | working-directory: $GITHUB_WORKSPACE/fortran-tf-lib/tools 37 | run: | 38 | pytest tests 39 | 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Byte-compiled / optimized / DLL files 35 | __pycache__/ 36 | *.py[cod] 37 | *$py.class 38 | 39 | # C extensions 40 | *.so 41 | 42 | # Distribution / packaging 43 | .Python 44 | build/ 45 | develop-eggs/ 46 | dist/ 47 | downloads/ 48 | eggs/ 49 | .eggs/ 50 | lib/ 51 | lib64/ 52 | parts/ 53 | sdist/ 54 | var/ 55 | wheels/ 56 | share/python-wheels/ 57 | *.egg-info/ 58 | .installed.cfg 59 | *.egg 60 | MANIFEST 61 | 62 | # PyInstaller 63 | # Usually these files are written by a python script from a template 64 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 65 | *.manifest 66 | *.spec 67 | 68 | # Installer logs 69 | pip-log.txt 70 | pip-delete-this-directory.txt 71 | 72 | # Unit test / coverage reports 73 | htmlcov/ 74 | .tox/ 75 | .nox/ 76 | .coverage 77 | .coverage.* 78 | .cache 79 | nosetests.xml 80 | coverage.xml 81 | *.cover 82 | *.py,cover 83 | .hypothesis/ 84 | .pytest_cache/ 85 | cover/ 86 | 87 | # Translations 88 | *.mo 89 | *.pot 90 | 91 | # Django stuff: 92 | *.log 93 | local_settings.py 94 | db.sqlite3 95 | db.sqlite3-journal 96 | 97 | # Flask stuff: 98 | instance/ 99 | .webassets-cache 100 | 101 | # Scrapy stuff: 102 | .scrapy 103 | 104 | # Sphinx documentation 105 | docs/_build/ 106 | 107 | # PyBuilder 108 | .pybuilder/ 109 | target/ 110 | 111 | # Jupyter Notebook 112 | .ipynb_checkpoints 113 | 114 | # IPython 115 | profile_default/ 116 | ipython_config.py 117 | 118 | # pyenv 119 | # For a library or package, you might want to ignore these files since the code is 120 | # intended to run in multiple environments; otherwise, check them in: 121 | # .python-version 122 | 123 | # pipenv 124 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 125 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 126 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 127 | # install all needed dependencies. 128 | #Pipfile.lock 129 | 130 | # poetry 131 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 132 | # This is especially recommended for binary packages to ensure reproducibility, and is more 133 | # commonly ignored for libraries. 134 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 135 | #poetry.lock 136 | 137 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 138 | __pypackages__/ 139 | 140 | # Celery stuff 141 | celerybeat-schedule 142 | celerybeat.pid 143 | 144 | # SageMath parsed files 145 | *.sage.py 146 | 147 | # Environments 148 | .env 149 | .venv 150 | env/ 151 | venv/ 152 | ENV/ 153 | env.bak/ 154 | venv.bak/ 155 | 156 | # Spyder project settings 157 | .spyderproject 158 | .spyproject 159 | 160 | # Rope project settings 161 | .ropeproject 162 | 163 | # mkdocs documentation 164 | /site 165 | 166 | # mypy 167 | .mypy_cache/ 168 | .dmypy.json 169 | dmypy.json 170 | 171 | # Pyre type checker 172 | .pyre/ 173 | 174 | # pytype static type analyzer 175 | .pytype/ 176 | 177 | # Cython debug symbols 178 | cython_debug/ 179 | 180 | # PyCharm 181 | # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can 182 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 183 | # and can be added to the global gitignore or merged into this file. For a more nuclear 184 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 185 | #.idea/ 186 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "process_model"] 2 | path = process_model 3 | url = https://github.com/Cambridge-ICCS/process_model.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Institute of Computing for Climate Science 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fortran-TF-lib 2 | 3 | ![GitHub](https://img.shields.io/github/license/Cambridge-ICCS/fortran-tf-lib) 4 | 5 | Code and examples for directly calling Tensorflow ML models from Fortran. 6 | For calling *PyTorch* from Fortran see the [FTorch repository](https://github.com/Cambridge-ICCS/fortran-pytorch-lib). 7 | 8 | ## Contents 9 | - [Description](#description) 10 | - [Installation](#installation) 11 | - [Usage](#usage) 12 | - [Examples](#examples) 13 | - [License](#license) 14 | - [Contributions](#contributions) 15 | - [Authors and Acknowledgment](#authors-and-acknowledgment) 16 | - [Users](#used-by) 17 | 18 | ## Description 19 | 20 | It is desirable be able to run machine learning (ML) models directly in Fortran. 21 | Such models are often trained in some other language (say Python) using popular frameworks (say TensorFlow) and saved. 22 | We want to run inference on this model without having to call a Python executable. 23 | To achieve this we use the existing TensorFlow C interface. 24 | 25 | This project provides a library enabling a user to directly couple their TensorFlow models to Fortran code. 26 | We provide installation instructions for the library as well as instructions and examples for performing coupling. 27 | This library implements only enough of the TensorFlow C API to allow inference, no training. 28 | 29 | Project status: This project is currently in pre-release with documentation and code being prepared for a first release. 30 | As such breaking changes may be made. 31 | If you are interested in using this library please get in touch. 32 | 33 | 34 | ## Installation 35 | 36 | ### Dependencies 37 | 38 | To install the library requires the following to be installed on the system: 39 | 40 | * cmake >= 3.1 41 | * TensorFlow C API, download from 1 42 | * Fortran and C compilers 43 | 44 | 1 Note that this page sometimes does not list the latest version of 45 | the library. You can try altering the library download URLs on the page to 46 | reflect the newest version. E.g. if the URL ends `...-2.11.tar.gz` try 47 | changing it to `...-2.13.tar.gz`. 48 | 49 | ### Library installation 50 | 51 | To build and install the library: 52 | 53 | 1. Navigate to the location in which you wish to install the source and run: 54 | ``` 55 | git clone git@github.com:Cambridge-ICCS/fortran-tf-lib.git 56 | ``` 57 | to clone via ssh, or 58 | ``` 59 | git clone https://github.com/Cambridge-ICCS/fortran-tf-lib.git 60 | ``` 61 | to clone via https. 62 | 2. Navigate into the library directory by running: 63 | ``` 64 | cd fortran-tf-lib/fortran-tf-lib/ 65 | ``` 66 | 3. Create a `build` directory and execute cmake from within it using the relevant flags: 67 | ``` 68 | mkdir build 69 | cd build 70 | cmake .. -DCMAKE_BUILD_TYPE=Release 71 | ``` 72 | It is likely that you will need to provide at least the `TENSORFLOW_LOCATION` flag. 73 | The Fortran compiler must be the same one that you are planning to compile your Fortran 74 | code with. It is advisable to use C and Fortran compilers from the same provider. 75 | 76 | The following CMake flags are available and can be passed as arguments through `-D