Skip to content

feat(cli): larql memit — batch fact editing (Phase C of RFC-0001)#8

Merged
mikeumus merged 1 commit into
mainfrom
feat/memit-command-v2
Apr 18, 2026
Merged

feat(cli): larql memit — batch fact editing (Phase C of RFC-0001)#8
mikeumus merged 1 commit into
mainfrom
feat/memit-command-v2

Conversation

@mikeumus

Copy link
Copy Markdown

Phase C rebased directly onto main after Phase B merge. Supersedes #5 (auto-closed). Cherry-picks ad9f411 from feat/memit-command.

Adds larql memit CLI wrapping existing run_memit, plus extends the patch format to support kind = dense for MEMIT multi-fact output. See #5 for full description.

Compile-checked against current main.

Wraps the existing covariance-MEMIT solver (larql_inference::forward::memit::
run_memit) with a CLI, an edits.json file format, and automatic crown-layer
discovery for each edit. Groups edits by crown layer, invokes the joint
least-squares solve, emits one dense `.lqpatch` per affected layer plus a
manifest.json. Phase C of RFC-0001 (#2), stacked on Phase B (#4).

### Extended patch file format (still backward compatible)
- Bumped patch version 1 → 2 with a `kind` field (defaults to "rank_one")
- New `kind = "dense"` variant carries a flat row-major ΔW matrix, needed
  because MEMIT's covariance-projected solve isn't natively a rank-1 outer
  product. Larger on disk (~72 MB per Gemma 4 4B layer) but semantically
  exact — no SVD approximation step.
- `write_patch`, `read_patch`, `apply_patch` all dispatch on kind. Phase B
  rank-1 patches continue to round-trip unchanged.
- New `compute_dense()` helper builds a Dense patch from an Array2<f32>.

### New CLI: `larql memit`
- Reads edits.json (list of {label, src, new_token, layer?} records).
- For each edit: tokenises src, resolves target_token_id, resolves crown
  layer (explicit or auto-scan).
- Calls `run_memit` with Vec<MemitFact>, receives one `MemitResult` per
  affected layer.
- Serialises each layer's ΔW as a Dense patch into the output directory,
  writes a manifest.json enumerating them.
- Prints the apply-patch command to install the batch.

### Usage

    cat > edits.json <<EOF
    [
      {"label":"france-to-tokyo","src":"Capital of France? A:",
       "new_token":" Tokyo","layer":27},
      {"label":"germany-to-rome","src":"Capital of Germany? A:",
       "new_token":" Rome","layer":27}
    ]
    EOF

    larql memit /path/to/gemma4 --edits edits.json --output patches/
    larql apply-patch /path/to/gemma4 \\
        -p patches/memit_L27.lqpatch \\
        --prompt "Capital of France? A:"

### Known ceiling
Chapter 22 established that single-layer MEMIT with correlated keys (~60%
cosine) lands ~3/5 concurrent targets. For 5+ correlated edits, users can
now distribute across multiple crown layers via `layer` overrides in
edits.json — MEMIT runs once per layer group.

Compile-checked with `cargo check --package larql-cli`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mikeumus mikeumus merged commit ed369cb into main Apr 18, 2026
@mikeumus mikeumus deleted the feat/memit-command-v2 branch April 18, 2026 00:02
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