Skip to content

support loading fit checkpoints in JacobianLens.load() - #1574

Open
priyanka25aug wants to merge 2 commits into
TransformerLensOrg:devfrom
priyanka25aug:jonah-review-1539
Open

support loading fit checkpoints in JacobianLens.load()#1574
priyanka25aug wants to merge 2 commits into
TransformerLensOrg:devfrom
priyanka25aug:jonah-review-1539

Conversation

@priyanka25aug

Copy link
Copy Markdown

What this does

Extends JacobianLens.load() to accept fit checkpoints (files saved by the fitting pipeline with a jacobian_sum key) in addition to the existing artifact format (files with a J key).

The conversion lives in a new _from_checkpoint_payload() classmethod:

  • Divides the running Jacobian sums by n_prompts to recover the per-prompt mean
  • Harvests safe scalar provenance keys (model_name, model_revision, corpus) from the flat payload namespace into metadata
  • Strips fit-reserved keys (transformer_lens_fit, transformer_lens_version, model_system, etc.) so they don't leak into the converted lens
  • Drops tensor-valued metadata fields that can't survive weights_only=True reload, recording their names and shapes in dropped_fields for transparency
  • Sets converted_from: "jacobian_lens_checkpoint" so merge() naturally refuses to mix converted and natively TL-fitted lenses (provenance keys differ)

Raises ValueError with a clear message if the file has neither key, or if n_prompts <= 0. Tuned-lens support deferred.

Files changed

  • transformer_lens/tools/analysis/jacobian_lens.py — new _FIT_RESERVED_KEYS and _CHECKPOINT_FLAT_PROVENANCE frozensets; updated load(); new _from_checkpoint_payload()
  • tests/unit/tools/test_jacobian_lens_import.py — unit tests on synthetic fixtures covering artifact regression, checkpoint round-trip, dtype preservation, n_prompts=0 guard, tensor field dropping, fit-key stripping, and merge provenance rejection; no model/oracle dependency
  • docs/source/content/jacobian_lens_fitting.md — "Importing an existing lens" section added

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Testing

uv run pytest tests/unit/tools/test_jacobian_lens_import.py -v

@priyanka25aug

Copy link
Copy Markdown
Author

Hi @jlarson4 — all format, type, docstring, and benchmark checks are passing. The remaining long-running jobs (compatibility across Python 3.10/3.11/3.12 and full coverage) are still in progress but looking clean so far.

Would you mind taking a look when you get a chance? Happy to make any changes based on your feedback.

@jlarson4 jlarson4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @priyanka25aug! Thanks for taking this on. Checkpoint import is a genuinely useful capability, and the conversion math, artifact-path safety, and merge sentinel are all done right.

A couple comments below that we should address before merging

fields, and marks the result as converted so :meth:`merge` refuses to
mix it with natively TL-fitted lenses.
"""
n_prompts = int(payload.get("n_prompts", 0))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference package's write_checkpoint() stores the count as n_done, so genuine checkpoints are rejected here with n_prompts=0. Could the loader read the key set that writer actually emits, as the pre-existing fixture does (tests/unit/tools/test_jacobian_lens.py:507)?


# Keys written by fit() that must not appear in converted-lens metadata so that
# merge() can refuse to mix TL-fitted lenses with externally converted ones.
_FIT_RESERVED_KEYS: frozenset = frozenset(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real checkpoints can record a non-final target_layer, but this conversion discards the key, so the target-convention refusal in validate_model silently passes (jacobian_lens.py:605). Could the conversion preserve target_layer, or refuse checkpoints recording a non-final target?

*n_prompts_override* lets callers inject a bad value without changing the
sums (useful for zero/negative n_prompts tests).
"""
payload = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fixtures encode the loader's own invented schema, so the suite continues to pass while real reference checkpoints fail to load. Once the schema is fixed, could one fixture mirror the reference write_checkpoint() payload key-for-key so format drift causes a failure?


### Note on tuned-lens

Tuned-lens checkpoints are not currently supported. The tuned-lens format has no

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuned-lens translators are affine and carry the bias. It is the Jacobian artifact format that has no bias slot to receive it. Can we ensure this detail is included as part of this note?

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.

2 participants