Spaces:
Sleeping
Sleeping
Download engine/scripts/peek_unit.py from lorcapoul/smol-neuron-studio: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/scripts/peek_unit.py
- Command line
-
hf download hf://spaces/lorcapoul/smol-neuron-studio/engine/scripts/peek_unit.py
-
curl -L -o peek_unit.py https://huggingface.co/spaces/lorcapoul/smol-neuron-studio/resolve/main/engine/scripts/peek_unit.py
2.55 kB
| """Imprime la ficha de una unidad del catálogo: estadísticas, ejemplos con el token resaltado y logit lens. | |
| Uso: cd engine && uv run python -m scripts.peek_unit L12.N847 [--examples 8] [--catalog ../data/catalog.sqlite] | |
| """ | |
| import argparse | |
| import re | |
| import sys | |
| from pathlib import Path | |
| from app.catalog import store | |
| DEFAULT_CATALOG = Path(__file__).resolve().parents[2] / "data" / "catalog.sqlite" | |
| CONTEXT = 12 # tokens a cada lado del resaltado | |
| def show_example(ex: store.Example) -> str: | |
| left = "".join(ex.tokens[max(0, ex.position - CONTEXT) : ex.position]) | |
| right = "".join(ex.tokens[ex.position + 1 : ex.position + 1 + CONTEXT]) | |
| text = f"{left}[[{ex.tokens[ex.position]}]]{right}".replace("\n", "↵") | |
| return f" {ex.activation:+8.3f} {text}" | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("unit", help="ID como L12.N847") | |
| parser.add_argument("--examples", type=int, default=8) | |
| parser.add_argument("--catalog", type=Path, default=DEFAULT_CATALOG) | |
| args = parser.parse_args() | |
| m = re.fullmatch(r"L(\d+)\.N(\d+)", args.unit, re.IGNORECASE) | |
| if not m: | |
| sys.exit("El ID debe tener la forma L12.N847") | |
| if not args.catalog.exists(): | |
| sys.exit(f"No existe {args.catalog}: corre antes `uv run python -m scripts.collect`") | |
| conn = store.open_catalog(args.catalog) | |
| meta = store.read_meta(conn) | |
| card = store.unit_card(conn, int(m[1]), int(m[2])) | |
| if card is None: | |
| sys.exit(f"{args.unit} no está en el catálogo ({meta['layers']} capas × {meta['units']} unidades)") | |
| s = card.stats | |
| print(f"L{card.layer}.N{card.unit} · {meta['model_id']} · {meta['fragments']} fragmentos de {meta['source']}") | |
| print( | |
| f" media {s['mean']:+.4f} · media|a| {s['mean_abs']:.4f} · std {s['std']:.4f} · " | |
| f"min {s['min']:+.3f} · max {s['max']:+.3f} · activa en {s['frac_active']:.2%} de los tokens" | |
| ) | |
| if card.label: | |
| lab = card.label | |
| print(f" etiqueta: \"{lab['label']}\" · confianza {lab['confidence']:.2f} · {lab['batch']}") | |
| if lab["notes"]: | |
| print(f" notas: {lab['notes']}") | |
| for title, examples in (("Mayores activaciones", card.top), ("Más negativas", card.bottom)): | |
| print(f"\n{title}:") | |
| for ex in examples[: args.examples]: | |
| print(show_example(ex)) | |
| print("\nPromueve: " + " ".join(repr(t) for t, _ in card.promoted)) | |
| print("Suprime: " + " ".join(repr(t) for t, _ in card.suppressed)) | |
| if __name__ == "__main__": | |
| main() | |