feat(python): PyO3 bindings for crown/edit/apply_patch/memit (Phase D of RFC-0001)#9
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.memitas Python callables via PyO3. See #6 for full description.Compile-checked against current main, 0 warnings.