model-debugger 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdb/__init__.py +43 -0
- mdb/__main__.py +5 -0
- mdb/analysis/__init__.py +34 -0
- mdb/analysis/circuits/__init__.py +6 -0
- mdb/analysis/circuits/build.py +690 -0
- mdb/analysis/circuits/test.py +600 -0
- mdb/analysis/features/__init__.py +5 -0
- mdb/analysis/features/basis.py +685 -0
- mdb/analysis/features/sae.py +374 -0
- mdb/analysis/features/watch.py +268 -0
- mdb/analysis/measure/__init__.py +7 -0
- mdb/analysis/measure/branches.py +806 -0
- mdb/analysis/measure/bridge.py +379 -0
- mdb/analysis/measure/changes.py +201 -0
- mdb/analysis/measure/compare.py +348 -0
- mdb/analysis/measure/donors.py +193 -0
- mdb/analysis/measure/novelty.py +227 -0
- mdb/analysis/measure/paths.py +146 -0
- mdb/analysis/measure/pools.py +454 -0
- mdb/analysis/measure/readout.py +387 -0
- mdb/analysis/measure/reads.py +455 -0
- mdb/analysis/measure/sweep.py +215 -0
- mdb/analysis/numerics/__init__.py +6 -0
- mdb/analysis/numerics/accumulation.py +180 -0
- mdb/analysis/numerics/finite.py +1712 -0
- mdb/analysis/numerics/labels.py +80 -0
- mdb/analysis/numerics/pooling.py +89 -0
- mdb/analysis/numerics/stability.py +183 -0
- mdb/analysis/numerics/summary.py +306 -0
- mdb/analysis/numerics/units.py +86 -0
- mdb/analysis/numerics/verdict.py +244 -0
- mdb/analysis/numerics/why_frames.py +162 -0
- mdb/analysis/predict/__init__.py +7 -0
- mdb/analysis/predict/capture.py +265 -0
- mdb/analysis/predict/consumers.py +52 -0
- mdb/analysis/predict/explain.py +776 -0
- mdb/analysis/predict/forecast.py +635 -0
- mdb/analysis/predict/plan.py +248 -0
- mdb/analysis/predict/propagate.py +596 -0
- mdb/analysis/predict/rounding.py +95 -0
- mdb/analysis/predict/rowmodel.py +817 -0
- mdb/analysis/predict/supplied.py +103 -0
- mdb/analysis/predict/verify.py +433 -0
- mdb/analysis/predict/whatif.py +840 -0
- mdb/analysis/symbols/__init__.py +7 -0
- mdb/analysis/symbols/binding.py +138 -0
- mdb/analysis/symbols/candidates.py +320 -0
- mdb/analysis/symbols/circuit_map.py +592 -0
- mdb/analysis/symbols/components.py +436 -0
- mdb/analysis/symbols/frame.py +451 -0
- mdb/analysis/symbols/hop.py +439 -0
- mdb/analysis/symbols/liveness.py +292 -0
- mdb/analysis/symbols/probe.py +393 -0
- mdb/analysis/symbols/rows.py +400 -0
- mdb/analysis/symbols/sealed.py +149 -0
- mdb/analysis/symbols/subspace.py +380 -0
- mdb/analysis/symbols/symbol_finite.py +417 -0
- mdb/analysis/symbols/symbol_runs.py +683 -0
- mdb/analysis/symbols/table.py +1110 -0
- mdb/cli.py +105 -0
- mdb/core/__init__.py +4 -0
- mdb/core/address.py +1353 -0
- mdb/core/bounded.py +101 -0
- mdb/core/checkpoint_schema.py +202 -0
- mdb/core/checkpoint_store.py +377 -0
- mdb/core/circuit_pages.py +117 -0
- mdb/core/custody.py +629 -0
- mdb/core/errors.py +51 -0
- mdb/core/foreign_names.py +152 -0
- mdb/core/graph_seal.py +426 -0
- mdb/core/index.py +403 -0
- mdb/core/kinds.py +255 -0
- mdb/core/locate.py +161 -0
- mdb/core/migrate.py +280 -0
- mdb/core/neuronpedia.py +116 -0
- mdb/core/plugins.py +78 -0
- mdb/core/pointers.py +621 -0
- mdb/core/project.py +248 -0
- mdb/core/project_read.py +1038 -0
- mdb/core/recipe.py +457 -0
- mdb/core/records.py +1027 -0
- mdb/core/resolve.py +296 -0
- mdb/core/settings.py +718 -0
- mdb/engine/__init__.py +21 -0
- mdb/engine/attention_observer.py +53 -0
- mdb/engine/attention_port.py +903 -0
- mdb/engine/breakpoints.py +1465 -0
- mdb/engine/bundle.py +137 -0
- mdb/engine/checkpoint.py +1265 -0
- mdb/engine/core_file.py +306 -0
- mdb/engine/edits.py +323 -0
- mdb/engine/holds.py +256 -0
- mdb/engine/models.py +172 -0
- mdb/engine/ports.py +1424 -0
- mdb/engine/refs.py +223 -0
- mdb/engine/run.py +2162 -0
- mdb/engine/selfcheck.py +347 -0
- mdb/engine/trace.py +536 -0
- mdb/interop/__init__.py +17 -0
- mdb/interop/circuit_tracer_graph.py +253 -0
- mdb/interop/circuit_tracer_names.py +44 -0
- mdb/interop/clone.py +22 -0
- mdb/interop/names.py +295 -0
- mdb/interop/nn.py +431 -0
- mdb/interop/pyvene_compat.py +40 -0
- mdb/interop/pyvene_names.py +82 -0
- mdb/interop/tl.py +149 -0
- mdb/interop/verify.py +85 -0
- mdb/machines/__init__.py +53 -0
- mdb/machines/x86/__init__.py +13 -0
- mdb/machines/x86/address.py +171 -0
- mdb/machines/x86/binary.py +285 -0
- mdb/machines/x86/grid.py +188 -0
- mdb/machines/x86/index.py +184 -0
- mdb/machines/x86/interchange.py +156 -0
- mdb/machines/x86/lanes.py +281 -0
- mdb/machines/x86/method.py +252 -0
- mdb/machines/x86/method_v2.py +105 -0
- mdb/machines/x86/mlp.py +430 -0
- mdb/machines/x86/records.py +138 -0
- mdb/machines/x86/session.py +631 -0
- mdb/machines/x86/trace.py +351 -0
- mdb/ops/__init__.py +31 -0
- mdb/ops/builtin.py +1977 -0
- mdb/ops/context.py +60 -0
- mdb/ops/debugger.py +926 -0
- mdb/ops/errors.py +8 -0
- mdb/ops/features.py +513 -0
- mdb/ops/graph.py +58 -0
- mdb/ops/inspect.py +906 -0
- mdb/ops/lines.py +496 -0
- mdb/ops/named.py +672 -0
- mdb/ops/predict.py +450 -0
- mdb/ops/registry.py +332 -0
- mdb/ops/sets.py +988 -0
- mdb/ops/sweep.py +574 -0
- mdb/repl/__init__.py +5 -0
- mdb/repl/__main__.py +244 -0
- mdb/repl/config.py +213 -0
- mdb/repl/remote.py +324 -0
- mdb/repl/render.py +1699 -0
- mdb/repl/shell.py +409 -0
- mdb/serve/__init__.py +17 -0
- mdb/serve/__main__.py +180 -0
- mdb/serve/_legacy.py +97 -0
- mdb/serve/app.py +526 -0
- mdb/serve/blobs.py +280 -0
- mdb/serve/caps.py +148 -0
- mdb/serve/export.py +9 -0
- mdb/serve/guard.py +216 -0
- mdb/serve/live.py +412 -0
- mdb/serve/live_reduce.py +90 -0
- mdb/serve/recorded.py +515 -0
- mdb/serve/reduce.py +449 -0
- mdb/serve/schema.py +969 -0
- mdb/serve/states.py +125 -0
- mdb/serve/static/assets/index-BJpByNCO.js +58 -0
- mdb/serve/static/assets/index-H9zlNeqq.css +1 -0
- mdb/serve/static/index.html +14 -0
- mdb/serve/static/manifest.json +5 -0
- mdb/serve/stored.py +34 -0
- mdb/serve/typegen.py +131 -0
- mdb/serve/views/__init__.py +1 -0
- mdb/serve/views/common.py +201 -0
- mdb/serve/views/frame.py +235 -0
- mdb/serve/views/graph.py +224 -0
- mdb/serve/views/live.py +402 -0
- mdb/serve/views/records.py +557 -0
- mdb/serve/views/sessions.py +151 -0
- mdb/serve/views/symbols.py +185 -0
- mdb/serve/views/timeline.py +121 -0
- model_debugger-0.4.0.dist-info/METADATA +1516 -0
- model_debugger-0.4.0.dist-info/RECORD +177 -0
- model_debugger-0.4.0.dist-info/WHEEL +4 -0
- model_debugger-0.4.0.dist-info/entry_points.txt +2 -0
- model_debugger-0.4.0.dist-info/licenses/LICENSE +202 -0
- model_debugger-0.4.0.dist-info/licenses/NOTICE +6 -0
mdb/engine/refs.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""Selectors and cross-run origins for the Python ``Run.set``.
|
|
2
|
+
|
|
3
|
+
The REPL's ``set ADDR[sel] = ORIGIN:ADDR[sel]`` builds one masked edit from a selector and takes
|
|
4
|
+
the donor value with its origin pointer (``ops.builtin.port_request``); a Python script had to
|
|
5
|
+
build the mask by hand and pass a bare tensor (origin lost). Here the same two pieces are engine
|
|
6
|
+
functions, so ``Run.set`` and ``Run.set_cell`` take them directly:
|
|
7
|
+
|
|
8
|
+
* ``Run.ref(ADDR[sel])`` reads a value of a run (a port or stop occurrence, or a cache cell
|
|
9
|
+
``cache.L<l>.k|v[kv,row,d]`` as the next read sees it) and returns a ``ValueRef``: the selected
|
|
10
|
+
block plus its origin pointer ``{origin, via, address, sha256, shape, dtype, where, cut}`` (the
|
|
11
|
+
``Debugger.origin_value`` pointer shape, ``via`` = ``live``). Reading leaves the donor as it
|
|
12
|
+
was.
|
|
13
|
+
* ``Run.set("t5.L1.v[kv1]", ref_or_value)`` resolves the selector against the port's axes
|
|
14
|
+
(``breakpoints.plan``, the REPL's planner), builds ONE masked edit and records the selector text
|
|
15
|
+
as ``selection`` and the ref's pointer as ``origin``. A ref must have the selected block's shape
|
|
16
|
+
exactly (a patch copies a block, it does not broadcast); a number or a tensor broadcasts to the
|
|
17
|
+
selection. ``Run.set("cache.L1.v[kv0,row=2]", ...)`` is a state-cell edit (``set_cell``) with
|
|
18
|
+
rows/kv/d from the selector.
|
|
19
|
+
|
|
20
|
+
Address rows: ``t<row>.``, ``t@last.`` (the last prompt row) or none (the cursor's row).
|
|
21
|
+
Casts: a donor block is moved to this run's device and dtype in one ``.to`` (never twice).
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
from dataclasses import dataclass
|
|
27
|
+
from typing import Any
|
|
28
|
+
|
|
29
|
+
import torch
|
|
30
|
+
|
|
31
|
+
from .ports import tensor_digest
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass(frozen=True)
|
|
35
|
+
class ValueRef:
|
|
36
|
+
"""A value read from a run at an address, with where it came from (``pointer``)."""
|
|
37
|
+
|
|
38
|
+
value: torch.Tensor
|
|
39
|
+
pointer: dict[str, Any]
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def address(self) -> str:
|
|
43
|
+
return self.pointer["address"]
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def shape(self) -> tuple[int, ...]:
|
|
47
|
+
return tuple(self.value.shape)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def split(text: str) -> tuple[str, str | None]:
|
|
51
|
+
"""``(base, selector text or None)`` of ``ADDR[sel]``."""
|
|
52
|
+
text = text.strip()
|
|
53
|
+
if "[" not in text:
|
|
54
|
+
return text, None
|
|
55
|
+
base, _, rest = text.partition("[")
|
|
56
|
+
if not rest.endswith("]"):
|
|
57
|
+
raise ValueError(f"unbalanced selector in {text!r}")
|
|
58
|
+
return base.strip(), rest[:-1].strip() or None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _row_text(run: Any, base: str) -> str:
|
|
62
|
+
"""``t@last.`` -> ``t<last prompt row>.``; other text unchanged."""
|
|
63
|
+
if base.startswith("t@last."):
|
|
64
|
+
return f"t{len(run.tokens) - 1}." + base[len("t@last."):]
|
|
65
|
+
if base.startswith("t@"):
|
|
66
|
+
raise ValueError(f"{base}: the only row role a run knows is @last (prompt sets bind others)")
|
|
67
|
+
return base
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _selector(text: str | None) -> Any:
|
|
71
|
+
from mdb.core import address as addresses
|
|
72
|
+
|
|
73
|
+
if not text:
|
|
74
|
+
return addresses.Selector()
|
|
75
|
+
from .breakpoints import parse_selector
|
|
76
|
+
|
|
77
|
+
return parse_selector(text)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def resolve(run: Any, text: str) -> dict[str, Any]:
|
|
81
|
+
"""The parts of ``ADDR[sel]`` on ``run``: ``kind`` (``cell`` or ``port``), the canonical
|
|
82
|
+
``address`` text, the ``selector`` (parsed), ``selection`` (its text) and, for a port,
|
|
83
|
+
``key``/``label``/``row``; for a cell ``layer``/``tensor``."""
|
|
84
|
+
base, sel = split(text)
|
|
85
|
+
base = _row_text(run, base)
|
|
86
|
+
selector = _selector(sel)
|
|
87
|
+
if base.startswith("cache."):
|
|
88
|
+
parts = base.split(".")
|
|
89
|
+
if len(parts) != 3 or not parts[1].startswith("L") or parts[2] not in ("k", "v"):
|
|
90
|
+
raise ValueError(f"a cache cell is cache.L<layer>.k|v[...], not {base!r}")
|
|
91
|
+
layer = int(parts[1][1:])
|
|
92
|
+
return {"kind": "cell", "layer": layer, "tensor": parts[2], "selector": selector,
|
|
93
|
+
"selection": sel, "address": base + (f"[{sel}]" if sel else "")}
|
|
94
|
+
key = run.parse(base)
|
|
95
|
+
label = run.stops()[key[1]]
|
|
96
|
+
canonical = f"t{key[0]}.{label}" + (f"[{sel}]" if sel else "")
|
|
97
|
+
return {"kind": "port", "key": key, "label": label, "row": key[0], "selector": selector,
|
|
98
|
+
"selection": sel, "address": canonical}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _axes(run: Any, label: str) -> tuple[str, ...]:
|
|
102
|
+
from .breakpoints import stop_axes
|
|
103
|
+
|
|
104
|
+
return stop_axes(run.program, label)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _picks(axes, shape, selector, run, row):
|
|
108
|
+
from .breakpoints import SelectionError, plan
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
return plan(axes, list(shape), selector, head_dim=run.adapter.head_dim, row=row)
|
|
112
|
+
except SelectionError as error:
|
|
113
|
+
raise ValueError(str(error)) from None
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _block(value: torch.Tensor, picks) -> torch.Tensor:
|
|
117
|
+
out = value
|
|
118
|
+
for axis, pick in enumerate(picks):
|
|
119
|
+
if pick is not None:
|
|
120
|
+
out = out.index_select(axis, torch.tensor(list(pick), dtype=torch.long,
|
|
121
|
+
device=out.device))
|
|
122
|
+
return out
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def run_label(run: Any) -> str:
|
|
126
|
+
"""The origin name of a run: its debugger name, else ``run@<root cut[:12]>``."""
|
|
127
|
+
if getattr(run, "name", None):
|
|
128
|
+
return run.name
|
|
129
|
+
return f"run@{run.cut_trail()[0][0][:12]}"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def ref(run: Any, text: str) -> ValueRef:
|
|
133
|
+
"""``Run.ref``: the selected block of ``ADDR[sel]`` read from ``run`` and its pointer."""
|
|
134
|
+
found = resolve(run, text)
|
|
135
|
+
if found["kind"] == "cell":
|
|
136
|
+
value = run.read_cache(found["layer"], found["tensor"])
|
|
137
|
+
from .edits import CELL_AXES
|
|
138
|
+
|
|
139
|
+
picks = _picks(CELL_AXES, value.shape, found["selector"], run, None)
|
|
140
|
+
else:
|
|
141
|
+
value = run.read(found["address"].partition("[")[0])
|
|
142
|
+
axes = _axes(run, found["label"])
|
|
143
|
+
row = found["row"] if "row" in axes and found["label"] not in ("start",) else None
|
|
144
|
+
picks = _picks(axes, value.shape, found["selector"], run, row)
|
|
145
|
+
block = _block(value, picks).detach().clone()
|
|
146
|
+
pointer = {"origin": run_label(run), "via": "live", "address": found["address"],
|
|
147
|
+
"sha256": tensor_digest(block), "shape": list(block.shape),
|
|
148
|
+
"dtype": str(block.dtype).removeprefix("torch."), "where": run.where,
|
|
149
|
+
"cut": run.checkpoint(pin=False)["cut"]}
|
|
150
|
+
return ValueRef(block, pointer)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _cast(run: Any, value: torch.Tensor) -> torch.Tensor:
|
|
154
|
+
"""One move to this run's device and dtype together (a no-op when both already match)."""
|
|
155
|
+
device, dtype = run.adapter.device, run.adapter.dtype
|
|
156
|
+
if value.dtype != dtype or value.device != torch.device(device):
|
|
157
|
+
value = value.to(device=device, dtype=dtype)
|
|
158
|
+
return value
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def port_edit(run: Any, found: dict[str, Any], value: Any, origin: dict[str, Any] | None,
|
|
162
|
+
mask: Any, *, shape: tuple[int, ...] | None = None) -> dict[str, Any]:
|
|
163
|
+
"""``{value, mask, selection, origin}`` of a port edit from a resolved address (``resolve``)
|
|
164
|
+
and a value (number, tensor or ``ValueRef``). ``shape``: the port's shape in the address's
|
|
165
|
+
own row (default: the pending transition's, ``Run.port_shape``)."""
|
|
166
|
+
if isinstance(value, ValueRef):
|
|
167
|
+
origin = origin if origin is not None else dict(value.pointer)
|
|
168
|
+
is_ref, value = True, value.value
|
|
169
|
+
else:
|
|
170
|
+
is_ref = False
|
|
171
|
+
selector = found["selector"]
|
|
172
|
+
if not (selector.keys or selector.tokens):
|
|
173
|
+
if is_ref:
|
|
174
|
+
value = _cast(run, value)
|
|
175
|
+
return {"value": value, "mask": mask, "selection": None, "origin": origin}
|
|
176
|
+
if mask is not None:
|
|
177
|
+
raise ValueError("give a selector or a mask, not both")
|
|
178
|
+
port = found["label"].split(".", 1)[1]
|
|
179
|
+
shape = tuple(run.port_shape(port)) if shape is None else tuple(shape)
|
|
180
|
+
axes = _axes(run, found["label"])
|
|
181
|
+
picks = _picks(axes, shape, selector, run, found["row"] if "row" in axes else None)
|
|
182
|
+
index = tuple(
|
|
183
|
+
torch.tensor(list(range(shape[a]) if pick is None else pick), dtype=torch.long).view(
|
|
184
|
+
[-1 if b == a else 1 for b in range(len(shape))])
|
|
185
|
+
for a, pick in enumerate(picks))
|
|
186
|
+
chosen = tuple(shape[a] if pick is None else len(pick) for a, pick in enumerate(picks))
|
|
187
|
+
full_mask = torch.zeros(shape, dtype=torch.bool)
|
|
188
|
+
full_mask[index] = True
|
|
189
|
+
if isinstance(value, torch.Tensor):
|
|
190
|
+
if is_ref and tuple(value.shape) != chosen:
|
|
191
|
+
raise ValueError(f"{origin['address']} selects shape {list(value.shape)}; "
|
|
192
|
+
f"{found['address']} selects {list(chosen)}: select the same block "
|
|
193
|
+
"on both sides")
|
|
194
|
+
if torch.broadcast_shapes(tuple(value.shape), chosen) != chosen:
|
|
195
|
+
raise ValueError(f"value of shape {list(value.shape)} does not broadcast to the "
|
|
196
|
+
f"selection {list(chosen)}")
|
|
197
|
+
value = _cast(run, value)
|
|
198
|
+
full = torch.zeros(shape, dtype=run.adapter.dtype, device=value.device)
|
|
199
|
+
full[tuple(i.to(value.device) for i in index)] = value.expand(chosen)
|
|
200
|
+
value = full
|
|
201
|
+
return {"value": value, "mask": full_mask, "selection": found["selection"], "origin": origin}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def cell_edit(run: Any, found: dict[str, Any], value: Any,
|
|
205
|
+
origin: dict[str, Any] | None) -> dict[str, Any]:
|
|
206
|
+
"""``set_cell`` keyword arguments of a cache-cell address (``resolve``) and a value."""
|
|
207
|
+
keys = dict(found["selector"].keys)
|
|
208
|
+
if "row" not in keys:
|
|
209
|
+
raise ValueError(f"{found['address']}: a cache cell edit names its rows (row=...)")
|
|
210
|
+
from .breakpoints import _indices
|
|
211
|
+
|
|
212
|
+
def picks(key: str) -> tuple[int, ...] | None:
|
|
213
|
+
return None if key not in keys else tuple(_indices(keys[key], None, key))
|
|
214
|
+
|
|
215
|
+
if isinstance(value, ValueRef):
|
|
216
|
+
origin = origin if origin is not None else dict(value.pointer)
|
|
217
|
+
value = _cast(run, value.value)
|
|
218
|
+
return {"layer": found["layer"], "tensor": found["tensor"], "rows": picks("row"),
|
|
219
|
+
"kv": picks("kv"), "d": picks("d"), "value": value, "origin": origin,
|
|
220
|
+
"selection": found["selection"]}
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
__all__ = ["ValueRef", "cell_edit", "port_edit", "ref", "resolve", "run_label", "split"]
|