Repository navigation
Improve Enzyme compile times and update AD example scripts - #202
Draft
bgroenks96 wants to merge 19 commits into
Draft
bgroenks96 wants to merge 19 commits into
bgroenks96 wants to merge 19 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR consists of three basic sets of changes (that could admittedly be separate PRs...):
StateVariablesand allAbstractModelsubtypes are nowmutablestructs; this results in a roughly ~4x speedup in Enzyme compile time. The latter change also helps to make Enzyme able to compilereconstructfor model parameter types.ColumnRingGridonmainand adds tests to fill the gapEnzymeextension module with custom rule fortick!that fixes an Enzyme error introduced by themutable StateVariableschangeIf this is too much @maximilian-gelbrecht , I can ask Claude to re-organize into a few different PRs.