Skip to content

Improve Enzyme compile times and update AD example scripts - #202

Draft
bgroenks96 wants to merge 19 commits into
mainfrom
bg/differentiability-examples
Draft

bgroenks96 wants to merge 19 commits into
mainfrom
bg/differentiability-examples

Conversation

@bgroenks96

Copy link
Copy Markdown
Collaborator

This PR consists of three basic sets of changes (that could admittedly be separate PRs...):

  • StateVariables and all AbstractModel subtypes are now mutable structs; this results in a roughly ~4x speedup in Enzyme compile time. The latter change also helps to make Enzyme able to compile reconstruct for model parameter types.
  • Add a new example script that attempts (and fails) to differentiate through the coupled Speedy + Terrarium model. Currently results in a segfault on the Speedy side.
  • Fixes some bugs with ColumnRingGrid on main and adds tests to fill the gap
  • Adds a new Enzyme extension module with custom rule for tick! that fixes an Enzyme error introduced by the mutable StateVariables change

If this is too much @maximilian-gelbrecht , I can ask Claude to re-organize into a few different PRs.

All seven fields are marked `const`, so the type's semantics are unchanged.

The motivation is Enzyme compile time. An immutable non-isbits struct has no stable
identity for LLVM, so SROA may decompose it into flattened SSA values independently at
every non-inlined call boundary it crosses. Enzyme then redoes the full type-tree and
activity analysis of the whole aggregate at each of those occurrences, and
`StateVariables` is a deeply nested aggregate of NamedTuples of Fields that passes
through nearly every function in a time step. Making it mutable gives it one heap
pointer that persists through the call graph, so that analysis happens once.

Measured on a single-column LandModel reverse pass (no snow, no vegetation,
homogeneous soil): 3051 s to 739 s, a 4.1x speedup.
Use dispatch with Val to avoid a type unstable branch that messes up
Enzyme
`examples/autodiff/speedy_terrarium_lateral_coupling.jl` takes the reverse-mode derivative of one land column's surface soil temperature, after a short coupled SpeedyWeather + Terrarium integration, with respect to the initial soil internal-energy field over every land column. Terrarium has no lateral transport of its own, so any nonzero sensitivity away from the target column is coupling mediated by the atmosphere.

The forward model, spin-up, target-cell selection, objective, and finite-difference check all work. The `autodiff` call does not compile at any configuration tried; the two remaining failures, both outside Terrarium: an `EnzymeInternalError` in SpeedyWeather's `vertical_advection!` and an Enzyme `TypeAnalysis` assertion failure.
Oceananigans' grid metrics (`Δxᶜᵃᵃ` and friends) dispatch on the concrete grid
type and have no generic fallback, so a grid wrapper has to forward them
explicitly.

Also adds a `LandModel: ColumnRingGrid` regression test

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant