mirror of
https://github.com/wassname/jsteer.git
synced 2026-10-28 10:30:44 +08:00
- Jacobian.fit wraps prompts in tqdm (jlens has no bar; safe since fit only enumerate/len's them), logs the full first prompt (special tokens on, SHOULD line) and a done-summary -- token-efficient-logging style, both tqdm intervals set - config.py sets up loguru on import (compact single-char icons, routed through tqdm.write so bars survive), so every script/notebook importing config gets it - notebooks drop their manual logger setup and import config in cell 1 Co-Authored-By: Claudypoo <288921227+claudypoo@users.noreply.github.com>
50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
"""Repo-local paths, slug/cache conventions, and the fit corpus, shared by
|
|
scripts/ and notebooks.
|
|
|
|
NOT imported by the jsteer library (which stays path-agnostic so `pip install
|
|
jsteer` never needs a repo root). The corpus content is jlens's own WikiText
|
|
(`load_wikitext_prompts`, not hand-rolled), but wrapped in the model's chat
|
|
template -- see chat_corpus for why.
|
|
"""
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
from loguru import logger
|
|
from tqdm.auto import tqdm
|
|
|
|
# Configure loguru once, on import, so every script/notebook that imports config
|
|
# gets the same compact format. Routed through tqdm.write so log lines don't
|
|
# break a live progress bar (e.g. the fit bar).
|
|
logger.remove()
|
|
logger.add(lambda m: tqdm.write(m, end=""), colorize=True,
|
|
format="<level>{level.icon}</level> {message}", level="INFO")
|
|
for _lvl, _icon in (("INFO", "I"), ("WARNING", "W"), ("ERROR", "E"), ("DEBUG", "D")):
|
|
logger.level(_lvl, icon=_icon)
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
ART = ROOT / "artifacts"
|
|
DEVICE = "cuda"
|
|
DTYPE = torch.bfloat16
|
|
|
|
|
|
def chat_corpus(tok, n_prompts: int) -> list[str]:
|
|
"""jlens's WikiText prompts wrapped in the chat template. Fitting on chat-
|
|
formatted text (not raw documents) puts J closer to the distribution the
|
|
model steers in; run-524's verified vectors were fit this way too. Called
|
|
via a lambda in fit_cached, so it only downloads WikiText on a cache miss."""
|
|
from jlens.examples import load_wikitext_prompts
|
|
return [tok.apply_chat_template([{"role": "user", "content": p}],
|
|
add_generation_prompt=True, tokenize=False,
|
|
enable_thinking=True)
|
|
for p in load_wikitext_prompts(n_prompts)]
|
|
|
|
|
|
def slug(model_name: str) -> str:
|
|
"""'Qwen/Qwen3-0.6B' -> 'qwen3-0.6b': a filesystem-safe cache stem."""
|
|
return model_name.split("/")[-1].lower()
|
|
|
|
|
|
def cache_path(model_name: str, suffix: str = "jac") -> Path:
|
|
"""Where the fitted Jacobian for `model_name` is cached."""
|
|
return ART / f"{slug(model_name)}.{suffix}"
|