SpeedyWeather + Reactant #970
SpeedyWeather + Reactant #970maximilian-gelbrecht wants to merge 68 commits intomk/matrix_transformfrom
Conversation
|
Test script import Pkg
Pkg.activate("SpeedyWeather")
using SpeedyWeather, Reactant, LinearAlgebra
#using CUDA
arch = SpeedyWeather.ReactantDevice()
nlayers = 1
spectral_grid = SpectralGrid(; architecture = arch, nlayers)
M = MatrixSpectralTransform(spectral_grid)
spec = randn(Complex{Float32}, spectral_grid.spectrum, nlayers)
field = zeros(Float32, spectral_grid.grid, nlayers)
tr = @compile transform!(spec, field, M)
# model construction works
#model = PrimitiveWetModel(spectral_grid; spectral_transform=M)
# model initialization currently fails (because of the transforms used)
#simulation = initialize!(model) |
|
At the moment, the model construction of all models works. Next, we have to think about the full model initialisation. That's more a question for our own code organisation. When we construct the model with Reactant Arrays, we should probably do all the pre-compute with it as well, but how to do this best? The alternative would be doing the pre-compute without Reactant and then transferring and converting later, but that's a bit annoying in practice. I have to have a think about the best approach. |
|
Btw calling NN parameterizations that are compiled with Reactant already works on the CPU, see #973 |
| """$(TYPEDSIGNATURES) | ||
| Precomputes the hyper diffusion terms for all layers based on the | ||
| model time step in `L`, the vertical level sigma level in `G`.""" | ||
| function initialize!( |
There was a problem hiding this comment.
Better to do really do all the initialization in kernels, with Reactant you also get warnings about scalar indexing here
We just have one or two more of those that transfer back and forth, I'll do the others later.
|
Good news before the weekend: Both initialization and time stepping of the I haven't done any form of correctness checks, and there are issues with the initial conditions, at the moment I tested it with just with zero ICs. import Pkg
Pkg.activate("SpeedyWeather")
using SpeedyWeather, Reactant, LinearAlgebra, CUDA # cuda is loaded for reactant but this is still on CPU
arch = SpeedyWeather.ReactantDevice()
nlayers = 1
spectral_grid = SpectralGrid(; architecture = arch, nlayers)
M = MatrixSpectralTransform(spectral_grid)
spec = zeros(Complex{Float32}, spectral_grid.spectrum, nlayers)
field = rand(Float32, spectral_grid.grid, nlayers)
# model construction works
#model = PrimitiveWetModel(spectral_grid; spectral_transform=M)
initial_conditions = InitialConditions(; vordiv=ZeroInitially())
# feedback == nothing is needed currently due to issues with Reactant
model = BarotropicModel(spectral_grid; spectral_transform=M, feedback=nothing, initial_conditions=initial_conditions)
simulation = initialize!(model)
@jit SpeedyWeather.timestep!(simulation) |
|
@milankl For proper correctness of our models with Reactant we need be to aware of the functions that do have significant branch divergences / control flow those limits/conditions change while running the model. Those need to be explicitly traced by Reactant. I don't have the best overview of that currently. I can see those e.g. in convection and condensation scheme, can you think about more like these? |
for this, you can add the ReactantCore dependency to SpeedyWeather (which just adds the
|
|
Thanks!
Just to be sure, what do you mean with the "the resulting code is too large", when do we have to watch out there? The typical situation in our model is that loop limits depend only on the model configuration that isn't changed at runtime, so I thought we don't need to trace them, with typical loop lengths of ~8-80 (for vertical layers). There are just some very few exceptions in our parametrizations. |
|
Works on GPU as well, expect I get a repeated warning Not sure what that means. |
|
While, the forward model is working, the reverse model / autodiff isn't. |
|
After talking to @mofeing , a custom |
Sorry only trying to catch up with the updates here now. Ditching |
|
With regards to Dates I just made a PR for Reactant EnzymeAD/Reactant.jl#2540 that implements the custom dates format there to make |
Initial preparations to run Speedy with Reactant.
This is based on
mk/matrix_transformas I am thinking that it's easier to avoid FFT plans for the first attempt.