How cambridge-iccs/fortran-tf-lib works
Project type and purpose
• This is an open-source library (“Fortran-TF-lib”) that provides Fortran bindings to the TensorFlow C inference API.
• Its goal is to let Fortran programs load pre-trained TensorFlow SavedModel files and perform inference without invoking Python at runtime.
File structure
Top-level
• .github/workflows: CI for running tests
• .gitignore, LICENSE, README.md, .gitmodules
• process_model (Python script entry point)
• fortran-tf-lib/
– CMakeLists.txt (build orchestration)
– README.md (library docs)
– my_model/ (example SavedModel files: saved_model.pb, variables/)
– src/fortran_tensorflow_lib.F90 (Fortran module implementing the C bindings)
– tests/
· gen_model.py (helper script)
· generated_code_test/ (example CMake + stub Fortran)
· load_model_{c,f,py}.c/F90/py (runtime loading examples)
· load_wavenet.F90
README
• Describes installation (CMake flags, dependencies: cmake, Fortran/C compiler, TensorFlow C API)
• Usage pattern:
a. Train/save a model in Python → SavedModel on disk
b. In Fortran: use TF_Interface; call TF_LoadSessionFromSavedModel(...) → run inference
• Shows minimal Fortran example + how to integrate with CMake
Main components
1. User Workflow Components
• Python Training Environment
– TensorFlow/Keras → SavedModel folder
• process_model CLI (Python script)
– Input: SavedModel
– Output: Fortran module stub (hard-coded ops & dims)
• Fortran Application Code
– Uses generated stub + TF_Interface module
2. Library Components (fortran-tf-lib)
• TF_Types module (Fortran types for C API structs)
• TF_Interface module (Fortran <–> C API binding layer)
• CMake build system (builds and installs .mod + .so)
• TensorFlow C library (external dependency)
3. Runtime Components
• SavedModel files on disk
• Dynamically linked libtensorflow.so
• Fortran executable
• TensorFlow graph execution (SessionRun)
Relationships & data flows
• [Python Training] produces → SavedModel folder
• [process_model] reads SavedModel → generates Fortran stub
• [CMake build] compiles → Fortran-TF-lib and user Fortran code, links with libtensorflow.so
• [Fortran Executable] at runtime:
• TF_LoadSessionFromSavedModel loads graph from SavedModel into a TF_Graph object
• TF_SessionRun executes inference, reading input tensors and returning output tensors
• Under the hood this calls into the TensorFlow C API (libtensorflow.so)
Highlighted architectural patterns
• FFI Bridge: Fortran modules wrap the C API via the iso_c_binding standard
• Code Generation: Python script auto-generates boilerplate Fortran interface to opaque model details
• Modular Library + Examples: clear separation of core library (src/) vs. examples/tests vs. codegen tool
Essential architectural elements to capture
• Separation of build-time (codegen, CMake) vs. run-time (inference)
• FFI boundary between Fortran and C (TensorFlow C API)
• Data flow: model files → Fortran stub → Fortran exe → TensorFlow runtime