Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 33 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,86 +61,49 @@ The demonstration applications
### Building and Testing
Because this repository supports programming language research, the code exercises new language features in novel ways.
We recommend using any compiler's latest release or even building open-source compilers from source.
The [handy-dandy] repository contains scripts capturing steps for building the [LLVM] compiler suite.
The remainder of this section contains commands for building Fiats with a recent Fortran compiler and the Fortran Package Manager ([`fpm`]).

#### Dependencies
* [Fortran Package Manager](https://github.com/fortran-lang) (`fpm`)
- [Julienne](https://go.lbl.gov/julienne) (automatically downloaded by `fpm`)
- [Assert](https://go.lbl.gov/assert) (automatically downloaded by `fpm`)
* A supported Fortran compiler:
- LLVM (`flang-new`) version 19 or higher
- NAG (`nagfor`) version 7.2 Build 7235 or higher
- Intel (`ifx`) version 2025.2 or higher

#### Supported Compilers
##### LLVM (`flang-new`)
Testing Fiats with LLVM `flang-new` version 20 or later with the following command should report that all tests pass:
```
fpm test --compiler flang-new --flag "-cpp -O3"
```
With LLVM 19, add `-mmlir -allow-assumed-rank` to the `--flag` argument.
With LLVM 21, it should be possible to automatically parallelize batch inferences on central processing units (CPUs).
With the one of the compilers below and the Fortran Package Manager ([`fpm`]) installed and in your `PATH`, the commands in the table below will build and run the Fiats test suite.

Compiler |Example build/test commands
---------|------------------------------------------------------------------------------------------------
LLVM |`fpm test --compiler flang --profile release`
GCC |`fpm test --compiler gfortran --profile release`
Intel |`fpm test --compiler ifx --flag "-fpp -O3 -coarray" --profile release`
LFortran |`fpm test --compiler lfortran --flag "--fast --cpp --realloc-lhs-arrays --separate-compilation"`
NAG |`fpm test --compiler nagfor --profile release`

where `fpm` automatically downloads, builds, and links against the two Fiats dependencies:
[Julienne](https://go.lbl.gov/julienne) (automatically downloaded by `fpm`) and
[Assert](https://go.lbl.gov/assert) (automatically downloaded by `fpm`)

#### Known issues
1. With `fpm` versions older than 0.13.0, replace `flang` with `flang-new` above.
2. Testing with `ifx` reports one failure: a test that converts a neural network with varying-width hidden layers to and from JSON.

### Parallelism
#### Flang
With [LLVM] `flang`, one can automatically parallelize batch inferences on central processing units (CPUs).
For example, run [concurrent-inferences](./example/concurrent-inferences.f90) in parallel as follows:
```
fpm run \
--example concurrent-inferences \
--compiler flang-new \
--compiler flang
--flag "-cpp -O3 -fopenmp -fdo-concurrent-to-openmp=host" \
-- --network model.json
```
where `model.json` must be a neural network in the [JSON] format used by Fiats and the companion [nexport] package.
[Rouson et al. (2025)](https://dx.doi.org/10.25344/S4VG6T) demonstrated that this approach achieves performance on par with using OpenMP compiler directives.
[Rouson et al. (June, 2025)](https://dx.doi.org/10.25344/S4VG6T) demonstrated that this approach achieves performance on par with using OpenMP compiler directives.
Work is under way to automatically parallelize training on CPUs and to offload inference and training to GPUs.

##### NAG (`nagfor`)
With `nagfor` 7.2 Build 7235 or later, the following command builds Fiats and reports that all tests pass:
```
fpm test --compiler nagfor --flag "-fpp -O3"
```

#### Partially Supported Compilers

Fiats release 0.14.0 and earlier support the use of the GNU and Intel Fortran compilers.
We are corresponding with these compilers' developers about addressing the issues preventing building newer Fiats releases.

##### GNU (`gfortran`)
Compiler bugs related to parameterized derived types currently prevent `gfortran` from building Fiats versions 0.15.0 or later.
Test and build earlier versions of Fiats build with the following command:
```
fpm test --compiler gfortran --profile release
```

##### Intel (`ifx`)
Testing with `ifx` versions higher than 2025.1 with the following command should report all tests passing except one:
```
fpm test --compiler ifx --flag "-fpp -coarray" --profile release
```
The failing test converts a neural network with varying-width hidden layers to and from JSON.
The reason for this failure is under investigation.
Please submit an issue if you would like to use `ifx` and require hidden layers of varying width.

##### _Experimental:_ Automatic offloading of `do concurrent` to GPUs
This capability is under development with the goal to facilitate automatic GPU offloading via the following command:
```
fpm test --compiler ifx --profile release --flag "-fpp -fopenmp-target-do-concurrent -qopenmp -fopenmp-targets=spir64 -O3 -coarray"
```

#### Under Development
We are corresponding with the developers of the compiler(s) below about addressing the compiler issues preventing building Fiats.

#### HPE Cray Compiler Environment (CCE) (`crayftn.sh`)
Building with the CCE `ftn` compiler wrapper requires an additional trivial wrapper.
For example, create a file `crayftn.sh` with the following contents and place this file's location in your `PATH`:
```
#!/bin/bash

ftn "$@"
```
Then execute
#### LFortran
With `lfortran`, one can run the Fiats test suite on GPUs using commands of the form
```
fpm test --compiler crayftn.sh
fpm test --compiler lfortran --flag "--fast --cpp --realloc-lhs-arrays --separate-compilation --gpu=<model>"`
```
after replacing `<model>` above with one of the following options according to your platform:
1. `cuda` for NVIDIA GPUs
2. `hip` for AMD GPUs
3. `metal` for Apple GPUs
3. `openmp` for CPUs

### Configuring a training run
Fiats imports hyperparameters and network configurations to and from JSON files.
Expand Down Expand Up @@ -178,7 +141,6 @@ Project is up to date
```
The Fiats JSON file format is fragile: splitting or combining lines breaks the file reader.
It should be ok, however, to add or removed white space or to reordered whole objects such as placing the "network configuration" object above the "hyperparameters" object.
A future release will leverage the [rojff] JSON interface to allow for more flexible file formatting.

### Training a neural network
Running the following command will train a neural network to learn the saturated mixing ratio function that is one component of the ICAR SB04 cloud microphysics model (see the [saturated_mixing_ratio_m] module for an implementation of the involved function):
Expand All @@ -196,10 +158,12 @@ The following is representative output after 3000 epochs:
The example program halts execution after reaching a cost-function threshold (which requires millions of epochs) or a maximum number of iterations or if the program detects a file named `stop` in the source-tree root directory.
Before halting, the program will print a table of expected and predicted saturated mixing ratio values across a range of input pressures and temperatures, wherein two the inputs have each been mapped to the unit interval [0,1].
The program also writes the neural network initial condition to `initial-network.json` and the final (trained) network to the file specified in the above command: `sat-mix-rat.json`.
Please see [Rouson et al. (September, 2025)](https://dx.doi.org/10.25344/S4QS3J) for more background and details on this training example .

### Performing inference
Users with a PyTorch model may use [nexport] to export the model to JSON files that Fiats can read.
Examples of performing inference using a neural-network JSON file are in [example/concurrent-inferences].
Please see [Rouson et al. (September, 2025)](https://dx.doi.org/10.25344/S4QS3J) for a detailed description of using Fiats to perform inference with an aerosol surrogate for [E3SM].

Documentation
-------------
Expand Down Expand Up @@ -246,25 +210,19 @@ the U.S. DOE under Contract No. DE-AC02-05CH11231.
[Building and testing]: #building-and-testing
[Caffeine]: https://go.lbl.gov/caffeine
[class diagram]: https://github.com/BerkeleyLab/fiats/blob/main/doc/uml/class-diagram.md
[Documentation]: #documentation
[demo]: https://github.com/BerkeleyLab/fiats/blob/main/demo
[E3SM]: https://e3sm.org
[example]: https://github.com/BerkeleyLab/fiats/blob/main/example
[example/print-training-configuration.F90]: https://github.com/BerkeleyLab/fiats/blob/main/example/print-training-configuration.F90
[example/concurrent-inferences]: https://github.com/BerkeleyLab/fiats/blob/main/example/concurrent-inferences.f90
[`ford`]: https://github.com/Fortran-FOSS-Programmers/ford
[`fpm`]: https://github.com/fortran-lang/fpm
[Getting Started]: #getting-started
[GitHub Pages site]: https://berkeleylab.github.io/fiats/
[handy-dandy]: https://github.com/rouson/handy-dandy/blob/main/src
[HDF5]: https://www.hdfgroup.org/solutions/hdf5/
[ICAR]: https://github.com/BerkeleyLab/icar/tree/neural-net
[JSON]: https://www.json.org/json-en.html
[LLVM]: https://github.com/llvm/llvm-project
[Mermaid]: https://mermaid.js.org
[NetCDF]: https://www.unidata.ucar.edu/software/netcdf/
[nexport]: https://go.lbl.gov/nexport
[Overview]: #overview
[ROCm fork]: https://github.com/ROCm/llvm-project
[rojff]: https://gitlab.com/everythingfunctional/rojff
[saturated_mixing_ratio_m]: https://github.com/BerkeleyLab/fiats/blob/main/example/supporting-modules/saturated_mixing_ratio_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ function random_inputs()
allocate(input_components(lat,lev,lon,neural_network%num_inputs()))
call random_number(input_components)

#ifdef __GFORTRAN__
do concurrent(i=1:lat, k=1:lev, j=1:lon)
#else
do concurrent(i=1:lat, k=1:lev, j=1:lon) default(none) shared(random_inputs, input_components)
#endif
random_inputs(i,k,j) = tensor_t(input_components(i,k,j,:))
end do
end function
Expand Down
159 changes: 0 additions & 159 deletions example/no-hidden-layer.F90

This file was deleted.

39 changes: 39 additions & 0 deletions example/simple_net.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
! Copyright (c) 2023-2026, The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

program simplenet_fiats
!! Define a neural network with no hidden layers and with
!! output tensor components matching the corresponding input
!! tensor components. Perform inference and print results.
use iso_fortran_env, only : output_unit
use julienne_m, only : string_t, csv
use fiats_m, only : neural_network_t, tensor_t
implicit none
! nodes per layer:
integer, parameter :: nodes(*) = [5, 5]
integer, parameter :: m = maxval(nodes), n = size(nodes)
integer i, j

associate( & ! w: identity-matrix weights, b: zero biases
w => [([(merge(1., 0., i==j), i=1,m)], j=1,m)] &
,b => [(0., i=1,m)] &
,in_data => tensor_t([0., 1., 2., 3., 4.]) &
)
associate( & ! construct neural network:
simple_net => neural_network_t( &
nodes = nodes &
,weights = reshape(w, [m,m,n-1]) &
,biases = reshape(b, [m,n-1]) & ! network name:
,metadata = [ string_t("simplenet") &
! author | time stamp
,string_t("John Doe"), string_t("27Nov26") &
! activation function | not a resnet
,string_t("identity"), string_t("false")] &
) ) ! perform inference:
associate(out_data => simple_net%infer(in_data))
write (output_unit, '(a)') "Model output:"
write (output_unit, csv) out_data%values()
end associate
end associate
end associate
end program simplenet_fiats
7 changes: 4 additions & 3 deletions src/fiats/activation_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ module activation_m

private
public :: activation_t
public :: gelu, relu, sigmoid, step, swish, tanh_
public :: gelu, identity, relu, sigmoid, step, swish, tanh_

enum, bind(C)
enumerator :: gelu=1, relu, sigmoid, step, swish, tanh_
enumerator :: gelu=1, identity, relu, sigmoid, step, swish, tanh_
end enum

character(len=*), parameter :: activation_name(*) = [character(len("sigmoid")) :: "gelu", "relu", "sigmoid", "step", "swish", "tanh"]
character(len=*), parameter :: activation_name(*) = &
[character(len("sigmoid")) :: "gelu", "identity", "relu", "sigmoid", "step", "swish", "tanh"]

type activation_t
private
Expand Down
Loading
Loading