Skip to content

refactor(csp-batch): the adjoint age sums for one, two and four slabs from one template (step 1) - #130

Merged
elijahmathews merged 1 commit into
feat/hotpath-00-grad-dumpsfrom
feat/hotpath-01-adjoint-slabs
Nov 19, 2026
Merged

elijahmathews merged 1 commit into
feat/hotpath-00-grad-dumpsfrom
feat/hotpath-01-adjoint-slabs

Conversation

@elijahmathews

Copy link
Copy Markdown
Owner

Stack: the unification plan (doc/HOT_PATHS_PLAN.md), step 1 of 5, after #129. Merge order: ..., #128, #129, this.

What changes

The adjoint of the age sums and of L_bol had two routines, backward_age_sums (no [alpha/Fe], K = 1 or 2 slabs, with a CPU dot-product branch and a compensated per-pixel device branch) and backward_age_sums_afe (K = 4): about 280 lines. They are now one template, src/physics/age_sums_adj_slabs.inc, instantiated for K = 1, 2, 4 on the same per-model descriptor as the forward age sums (#127, #128), with one call per (model, age) in a small device-callable routine.

The adjoint of a linear blend is the dot of each slab with the cotangents, combined with the weights: the template forms each slab's dots once (over the non-EUV and the EUV pixels, compensated at kernel precision on the device and with dot_lanes on CPUs, as the CPU K = 2 branch already did), and then w_bar, the derivatives with respect to dz and da and the L_bol blend are FP64 combinations of those dots with the weights and the derivatives of the weights. The wrapper backward_age_sums keeps its interface. Conditionals 965 -> 961, .inc files counted.

Numerics: not bit-identical, deliberately

This step was chosen over a bit-identical skeleton because the per-slab form is the only one that is one body for every K (the old K = 2 and K = 4 used different blend and derivative formulas). Consequences, measured:

  • Host FP64, the 45 gradient dumps of the matrix (N = 300) against the old code: 43 equal bit for bit (K = 1, K = 2); the two sets with [alpha/Fe] (K = 4, a different summation) differ by up to 1e-12 of the scale of their parameters.
  • R9700 (FP32 kernels, ROCm container, forced rebuild): the rows that change (the SFH rows, the logzsol and afe rows) differ from the old code by 2e-8 - 1e-7 of their scale; against the FP64 reference their errors stay at 1e-7, as before. One row is worse: the afe derivative, 2.2e-7 against 1.5e-7 (and 2.1e-7 against 9.7e-8 with logzsol free): within the 1e-5 FP32 requirement, but the one place the new form is less accurate than the old, and the cost of forming a difference of two sums instead of a sum of differences (the rule in doc/MIXED_PRECISION.md); the median error over all rows is unchanged.

Verification

  • Host FP64 and FP32: unit suite (45), test_csp_batch, test_batch_equivalence, test_csp_batch_grad (finite differences) green.
  • R9700: ssp_operator, csp_batch, csp_batch_grad, batch_equivalence, c_batch_grad, physics_oracles pass, on a tree rebuilt from scratch (checked: the compile log has csp_batch.f90).
  • nvfortran 26.1 (NVHPC container): the library compiles for the A100 (cc80, FP32 kernels). Not run there (no NVIDIA GPU): the nvfortran device kernels and the A100 gradient accuracy are for the ICDS session. The first version of the template used a BLOCK construct in the kernel, which flang accepts and nvfortran rejects ("BLOCK construct in the scope of a parallel directive"); the compile check found it, and the per-(model, age) work is now a routine with local arrays, which both compile.

Not in this PR

Nothing else changed. (doc/HOT_PATHS_PLAN.md gets the status of steps 0 and 1 and two lessons for the checks: force a rebuild of the tree under test, and compile the nvfortran device build for every device kernel change.)

🤖 Generated with Claude Code

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

… from one template (unification plan, step 1)

backward_age_sums and backward_age_sums_afe (about 280 lines, a CPU dot-product branch and a compensated per-pixel branch for the device) become one template, age_sums_adj_slabs.inc, instantiated for K = 1, 2, 4 on the descriptor of the forward age sums: the dots of each slab with the cotangents (compensated at kernel precision, dot_lanes on CPUs) and an FP64 combination with the weights and their derivatives. K = 1 and K = 2 on the host give the same gradients bit for bit; the others differ at rounding level (R9700 FP32 error against FP64 unchanged at 1e-7). Conditionals: 965 to 961.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5
@elijahmathews
elijahmathews added this pull request to stack #114 November 18, 2026 21:35
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.96%. Comparing base (988b200) to head (969d20d).

Files with missing lines Patch % Lines
src/physics/opensps_csp_batch.f90 75.00% 0 Missing and 3 partials ⚠️
src/physics/age_sums_adj_slabs.inc 97.26% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@                      Coverage Diff                       @@
##           feat/hotpath-00-grad-dumps     #130      +/-   ##
==============================================================
- Coverage                       74.99%   74.96%   -0.03%     
==============================================================
  Files                              50       51       +1     
  Lines                           14423    14414       -9     
  Branches                         6062     6066       +4     
==============================================================
- Hits                            10816    10805      -11     
  Misses                           1479     1479              
- Partials                         2128     2130       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@elijahmathews
elijahmathews marked this pull request as ready for review November 19, 2026 16:55
@elijahmathews
elijahmathews merged commit 8b713ec into main Nov 19, 2026
29 of 30 checks passed
@elijahmathews
elijahmathews deleted the feat/hotpath-01-adjoint-slabs branch November 19, 2026 17:49
elijahmathews added a commit that referenced this pull request Nov 19, 2026
…age-sum dispatch (step 2, first slice) (#131)

Stack: the unification plan (`doc/HOT_PATHS_PLAN.md`), step 2 of 5,
first slice, after #130. Merge order: ..., #129, #130, this.

## What changes

The driver chose where the SSPs of a batch come from with a chain of
flags, twice: for the path label of `batch_info` and, further down, for
which age-sum kernel to call. This gives the choice a name and one
place:

- `ssp_source_t` (module-private): the kind (resident basis with one
SSP, with two metallicity slabs, with two [alpha/Fe] planes too, the
weights over ages and metallicities, or the operators of the batched
SSP), the number of slabs or groups it blends (the K of `batch_info`)
and the path label.
- `ssp_source_select(zw, zmode, naf, nzb, ssp_ops)`: the selection that
was an inline chain in the driver (and the label block of `batch_info`).
- `ssp_source_age_sums(src, ctx, ...)`: the dispatch over `age_sums`,
`age_sums_w`, `age_sums_z`, `age_sums_z4`, replacing the chain of calls;
the operators' sums are done by their own pass, as before.

This is the first slice of step 2, not the whole of it: the flags
`zmode`, `zw`, `imf`, `zimf` are still locals of the driver for the rest
of their uses (the mass and L_bol block, the nebular calls, the backward
pass, about 70 uses), and the descriptor arrays are still formed in the
driver. Moving those behind the source is what makes the driver read as
one path; it is the next slice, and I'd rather see this one through CI
first. `doc/HOT_PATHS_PLAN.md` gets the status.

## Verification

- 45 forward dumps of the free-parameter matrix (host FP64, N = 1000;
the standard set with [alpha/Fe] and the IMF limits) before and after:
`tools/compare_forward_dump.py`, 0 differ bit for bit.
- Host FP64 and FP32: unit suite (45), `test_csp_batch` (including the
`batch_info` labels), `test_batch_equivalence`, `test_csp_batch_grad`
green.
- R9700 device tests (ROCm container, from-scratch rebuild; the compile
log has `csp_batch.f90`): `ssp_operator`, `csp_batch`, `csp_batch_grad`,
`batch_equivalence`, `c_batch_grad`, `physics_oracles` pass.
- nvfortran 26.1 (NVHPC container): the library compiles for the A100.
Not run (no NVIDIA GPU). The change is host-side dispatch; no device
kernel changed.

🤖 Generated with [Claude Code](https://claude.ai/code)

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
elijahmathews added a commit that referenced this pull request Nov 19, 2026
…hts block behind the SSP source (step 2, second slice) (#132)

Stack: the unification plan (`doc/HOT_PATHS_PLAN.md`), step 2 of 5,
second slice, after #131. Merge order: ..., #130, #131, this.

## What changes

Two more pieces of the driver that were steered by the SSP-source flags
move behind the source of #131:

- `ssp_source_describe`: the per-model slab descriptor of the resident
basis or the operators (`zlo`, `zhi`, `dz` from the model's `logzsol`
with a basis per metallicity, and with the [alpha/Fe] planes of the
context's stack the plane stride `nzs`, `da` and the slabs of the lower
plane), and the refusal (status 2) of a model whose horizontal-branch
modification is not in the operators of its metallicity.
- `ssp_source_scalars`: the mass, L_bol and weights loops of the three
kinds of source (weights over metallicities, the operators' own
surviving mass and L_bol, the resident basis blended or its first
plane), host and device, with the weights cast to kernel precision (the
body of the loops is `model_sums` / `model_sums_zw` of #125).

The driver calls them in place of the blocks (about 90 lines out of
`compute_csp_batch`); the routines hold what they replace, so the file
is about 95 lines longer with the type and the routines of both slices
(7060 lines against 6965), not shorter: this slice is about the
structure (the driver no longer forms the descriptor or runs the loops
of a source), and the line count falls only when the remaining
flag-steered code goes behind the source as well (the nebular calls, the
backward pass: step 4). `doc/HOT_PATHS_PLAN.md` gets the status.

## Verification

- 45 forward dumps of the free-parameter matrix, host FP64 (N = 1000)
before and after: `tools/compare_forward_dump.py`, 0 differ bit for bit.
- 45 forward dumps on the R9700 (FP32 kernels, ROCm container, both
trees rebuilt from scratch, the compile logs have `csp_batch.f90`): 0
differ bit for bit. This slice moved device launch code (the mass and
L_bol kernels) into a routine, so the device dumps are the check of it.
- Host FP64 and FP32: unit suite (45), `test_csp_batch`,
`test_batch_equivalence`, `test_csp_batch_grad` green. R9700 device
tests (`ssp_operator`, `csp_batch`, `csp_batch_grad`,
`batch_equivalence`, `c_batch_grad`, `physics_oracles`) pass. nvfortran
26.1 (NVHPC container): compiles for the A100; not run (no NVIDIA GPU):
this PR moves device launches, so it joins the list for the ICDS
session.

🤖 Generated with [Claude Code](https://claude.ai/code)

https://claude.ai/code/session_01MXDRCs7ATQiqhT6RLPLXR5

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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