Skip to content

EK-FAC: average query gradients over model checkpoints - #440

Open
luciaquirke wants to merge 5 commits into
mainfrom
feat/ekfac-ckpt-avg-query
Open

EK-FAC: average query gradients over model checkpoints#440
luciaquirke wants to merge 5 commits into
mainfrom
feat/ekfac-ckpt-avg-query

Conversation

@luciaquirke

@luciaquirke luciaquirke commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Adds HessianPipelineConfig.query_model_paths, the EK-FAC counterpart of ValidationConfig.ckpt_avg_k (#439). With two or more paths, the query gradient is built once per checkpoint and averaged elementwise before the Hessian is applied. Empty (the default) builds it once from index_cfg.model, so existing runs are unchanged.

Implementation

  • average_gradient_indices in bergson/data.py averages several gradient indices into a new one. Sources must agree on row count, module layout and dtype -- they are the same query set at different checkpoints, so a mismatch means the indices aren't comparable. Accumulation is in float64 regardless of the store dtype, so a long running sum doesn't drift before the divide.
  • hessian_pipeline builds each checkpoint's query index into <query_path>__ckpt<i>, then averages them into query_path.

Note on checkpoint format

query_model_paths entries load the same way index_cfg.model does (HF model directory or hub name). The MAGIC trainer writes its trajectory as TrainerState dcp checkpoints, which are not that format -- they need exporting first. This is called out in the config docstring.

Testing

tests/test_ekfac_query_ckpt_avg.py -- one test covering the averaging math, the info.json passthrough, the single-source case, float64 accumulation over 64 sources, and the three ValueError guards. Passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JzPbQBTC457nM5dnHFz7oF

luciaquirke and others added 5 commits August 24, 2026 03:14
The EK-FAC counterpart of ValidationConfig.ckpt_avg_k, completing what D9 asks
for: both scorers seeded with the averaged query gradient.

MAGIC computes query gradients inline, so averaging there is a loop over
TrainerState checkpoints. EK-FAC instead reads them from a prebuilt mmap index,
so the averaging has to happen when that index is BUILT: with two or more
query_model_paths, step 1 of hessian_pipeline builds the query gradient once per
checkpoint into query__ckpt<i> and averages them elementwise into the usual
query path. Steps 2-4 are untouched.

query_model_paths defaults to empty, which is the existing single-model
behaviour exactly.

average_gradient_indices refuses sources that disagree on row count or module
layout -- those would be different query sets, and averaging them would be
silently wrong rather than an error. It accumulates in float64 so a long running
sum does not drift before the divide.

Documented limitation: paths must load the way index_cfg.model does (HF
directory or hub name). The MAGIC trainer writes its trajectory as TrainerState
dcp checkpoints, which are not that format and must be exported first.

7 tests.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzPbQBTC457nM5dnHFz7oF
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzPbQBTC457nM5dnHFz7oF
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzPbQBTC457nM5dnHFz7oF
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JzPbQBTC457nM5dnHFz7oF
@luciaquirke
luciaquirke requested a review from LouisYRYJ August 24, 2026 07:49
@luciaquirke

Copy link
Copy Markdown
Collaborator Author

cc @LouisYRYJ I think I merged the MAGIC one already but this one looks a bit more messy

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