Skip to content

feat(python): PyO3 bindings for crown/edit/apply_patch/memit (Phase D of RFC-0001)#9

Merged
mikeumus merged 1 commit into
mainfrom
feat/python-bindings-v2
Apr 18, 2026
Merged

feat(python): PyO3 bindings for crown/edit/apply_patch/memit (Phase D of RFC-0001)#9
mikeumus merged 1 commit into
mainfrom
feat/python-bindings-v2

Conversation

@mikeumus

Copy link
Copy Markdown

Phase D rebased directly onto main after Phase C merge. Supersedes #6 (auto-closed). Cherry-picks 1510cd3 from feat/python-bindings.

Adds larql.crown, larql.edit, larql.apply_patch, larql.memit as Python callables via PyO3. See #6 for full description.

Compile-checked against current main, 0 warnings.

… of RFC-0001)

Exposes the Phase A-C commands as Python callables so the Chapter 15-23
Colab experiments from Divinci-AI/server become one-liner Rust invocations
from Jupyter — no CLI shell-outs, no JSON parsing.

### New module: crates/larql-python/src/edit_py.rs

Four #[pyfunction] entry points:

- crown(model, prompt, expect, start_layer=None, end_layer=None, top_k=100)
  Returns {crown_layer, crown_delta_prob, top_after_ablation, scan: [...]}.

- edit(model, src, tgt, new_token, output, layer=None, scales=None,
       fixed_scale=None, top_k=100, label=None)
  Writes a rank-1 .lqpatch; returns {layer, scale, output, d_norm}.

- apply_patch(model, patches: list[str], prompt=None, top_k=5, reverse=False)
  Applies patches in-memory; optional prompt returns {predictions: [(tok, prob), ...]}.

- memit(model, edits: list[dict], output_dir, ridge=0.01, target_alpha=1.0,
        top_k=100)
  Batch fact editor wrapping run_memit — writes one dense patch per layer
  into output_dir + manifest.

### Wiring

- Registered in _native pymodule (src/lib.rs) via m.add_function.
- Re-exported from python/larql/__init__.py under the public `larql`
  namespace alongside the existing load_vindex/create_session functions.

### Example

    import larql
    scan = larql.crown("/path/to/gemma4",
                       "Capital of France? A:", " Paris")
    print(scan["crown_layer"])                    # 27 (on Gemma 4 4B)

    larql.edit("/path/to/gemma4",
               src="Capital of France? A:",
               tgt="Capital of Japan? A:",
               new_token=" Tokyo",
               output="france_to_tokyo.lqpatch")

    r = larql.apply_patch("/path/to/gemma4",
                          patches=["france_to_tokyo.lqpatch"],
                          prompt="Capital of France? A:")
    print(r["predictions"][0])                    # ['Tokyo', 0.97]

This closes the RFC-0001 phased rollout: Python scripts can now drive the
mechanistic fact-editing pipeline end-to-end.

Compile-checked with `cargo check --package larql-python`. Runtime import
requires `maturin develop` — standard PyO3 workflow, no Python side of
the package changed structurally.

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