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.
Files changed (177) hide show
  1. mdb/__init__.py +43 -0
  2. mdb/__main__.py +5 -0
  3. mdb/analysis/__init__.py +34 -0
  4. mdb/analysis/circuits/__init__.py +6 -0
  5. mdb/analysis/circuits/build.py +690 -0
  6. mdb/analysis/circuits/test.py +600 -0
  7. mdb/analysis/features/__init__.py +5 -0
  8. mdb/analysis/features/basis.py +685 -0
  9. mdb/analysis/features/sae.py +374 -0
  10. mdb/analysis/features/watch.py +268 -0
  11. mdb/analysis/measure/__init__.py +7 -0
  12. mdb/analysis/measure/branches.py +806 -0
  13. mdb/analysis/measure/bridge.py +379 -0
  14. mdb/analysis/measure/changes.py +201 -0
  15. mdb/analysis/measure/compare.py +348 -0
  16. mdb/analysis/measure/donors.py +193 -0
  17. mdb/analysis/measure/novelty.py +227 -0
  18. mdb/analysis/measure/paths.py +146 -0
  19. mdb/analysis/measure/pools.py +454 -0
  20. mdb/analysis/measure/readout.py +387 -0
  21. mdb/analysis/measure/reads.py +455 -0
  22. mdb/analysis/measure/sweep.py +215 -0
  23. mdb/analysis/numerics/__init__.py +6 -0
  24. mdb/analysis/numerics/accumulation.py +180 -0
  25. mdb/analysis/numerics/finite.py +1712 -0
  26. mdb/analysis/numerics/labels.py +80 -0
  27. mdb/analysis/numerics/pooling.py +89 -0
  28. mdb/analysis/numerics/stability.py +183 -0
  29. mdb/analysis/numerics/summary.py +306 -0
  30. mdb/analysis/numerics/units.py +86 -0
  31. mdb/analysis/numerics/verdict.py +244 -0
  32. mdb/analysis/numerics/why_frames.py +162 -0
  33. mdb/analysis/predict/__init__.py +7 -0
  34. mdb/analysis/predict/capture.py +265 -0
  35. mdb/analysis/predict/consumers.py +52 -0
  36. mdb/analysis/predict/explain.py +776 -0
  37. mdb/analysis/predict/forecast.py +635 -0
  38. mdb/analysis/predict/plan.py +248 -0
  39. mdb/analysis/predict/propagate.py +596 -0
  40. mdb/analysis/predict/rounding.py +95 -0
  41. mdb/analysis/predict/rowmodel.py +817 -0
  42. mdb/analysis/predict/supplied.py +103 -0
  43. mdb/analysis/predict/verify.py +433 -0
  44. mdb/analysis/predict/whatif.py +840 -0
  45. mdb/analysis/symbols/__init__.py +7 -0
  46. mdb/analysis/symbols/binding.py +138 -0
  47. mdb/analysis/symbols/candidates.py +320 -0
  48. mdb/analysis/symbols/circuit_map.py +592 -0
  49. mdb/analysis/symbols/components.py +436 -0
  50. mdb/analysis/symbols/frame.py +451 -0
  51. mdb/analysis/symbols/hop.py +439 -0
  52. mdb/analysis/symbols/liveness.py +292 -0
  53. mdb/analysis/symbols/probe.py +393 -0
  54. mdb/analysis/symbols/rows.py +400 -0
  55. mdb/analysis/symbols/sealed.py +149 -0
  56. mdb/analysis/symbols/subspace.py +380 -0
  57. mdb/analysis/symbols/symbol_finite.py +417 -0
  58. mdb/analysis/symbols/symbol_runs.py +683 -0
  59. mdb/analysis/symbols/table.py +1110 -0
  60. mdb/cli.py +105 -0
  61. mdb/core/__init__.py +4 -0
  62. mdb/core/address.py +1353 -0
  63. mdb/core/bounded.py +101 -0
  64. mdb/core/checkpoint_schema.py +202 -0
  65. mdb/core/checkpoint_store.py +377 -0
  66. mdb/core/circuit_pages.py +117 -0
  67. mdb/core/custody.py +629 -0
  68. mdb/core/errors.py +51 -0
  69. mdb/core/foreign_names.py +152 -0
  70. mdb/core/graph_seal.py +426 -0
  71. mdb/core/index.py +403 -0
  72. mdb/core/kinds.py +255 -0
  73. mdb/core/locate.py +161 -0
  74. mdb/core/migrate.py +280 -0
  75. mdb/core/neuronpedia.py +116 -0
  76. mdb/core/plugins.py +78 -0
  77. mdb/core/pointers.py +621 -0
  78. mdb/core/project.py +248 -0
  79. mdb/core/project_read.py +1038 -0
  80. mdb/core/recipe.py +457 -0
  81. mdb/core/records.py +1027 -0
  82. mdb/core/resolve.py +296 -0
  83. mdb/core/settings.py +718 -0
  84. mdb/engine/__init__.py +21 -0
  85. mdb/engine/attention_observer.py +53 -0
  86. mdb/engine/attention_port.py +903 -0
  87. mdb/engine/breakpoints.py +1465 -0
  88. mdb/engine/bundle.py +137 -0
  89. mdb/engine/checkpoint.py +1265 -0
  90. mdb/engine/core_file.py +306 -0
  91. mdb/engine/edits.py +323 -0
  92. mdb/engine/holds.py +256 -0
  93. mdb/engine/models.py +172 -0
  94. mdb/engine/ports.py +1424 -0
  95. mdb/engine/refs.py +223 -0
  96. mdb/engine/run.py +2162 -0
  97. mdb/engine/selfcheck.py +347 -0
  98. mdb/engine/trace.py +536 -0
  99. mdb/interop/__init__.py +17 -0
  100. mdb/interop/circuit_tracer_graph.py +253 -0
  101. mdb/interop/circuit_tracer_names.py +44 -0
  102. mdb/interop/clone.py +22 -0
  103. mdb/interop/names.py +295 -0
  104. mdb/interop/nn.py +431 -0
  105. mdb/interop/pyvene_compat.py +40 -0
  106. mdb/interop/pyvene_names.py +82 -0
  107. mdb/interop/tl.py +149 -0
  108. mdb/interop/verify.py +85 -0
  109. mdb/machines/__init__.py +53 -0
  110. mdb/machines/x86/__init__.py +13 -0
  111. mdb/machines/x86/address.py +171 -0
  112. mdb/machines/x86/binary.py +285 -0
  113. mdb/machines/x86/grid.py +188 -0
  114. mdb/machines/x86/index.py +184 -0
  115. mdb/machines/x86/interchange.py +156 -0
  116. mdb/machines/x86/lanes.py +281 -0
  117. mdb/machines/x86/method.py +252 -0
  118. mdb/machines/x86/method_v2.py +105 -0
  119. mdb/machines/x86/mlp.py +430 -0
  120. mdb/machines/x86/records.py +138 -0
  121. mdb/machines/x86/session.py +631 -0
  122. mdb/machines/x86/trace.py +351 -0
  123. mdb/ops/__init__.py +31 -0
  124. mdb/ops/builtin.py +1977 -0
  125. mdb/ops/context.py +60 -0
  126. mdb/ops/debugger.py +926 -0
  127. mdb/ops/errors.py +8 -0
  128. mdb/ops/features.py +513 -0
  129. mdb/ops/graph.py +58 -0
  130. mdb/ops/inspect.py +906 -0
  131. mdb/ops/lines.py +496 -0
  132. mdb/ops/named.py +672 -0
  133. mdb/ops/predict.py +450 -0
  134. mdb/ops/registry.py +332 -0
  135. mdb/ops/sets.py +988 -0
  136. mdb/ops/sweep.py +574 -0
  137. mdb/repl/__init__.py +5 -0
  138. mdb/repl/__main__.py +244 -0
  139. mdb/repl/config.py +213 -0
  140. mdb/repl/remote.py +324 -0
  141. mdb/repl/render.py +1699 -0
  142. mdb/repl/shell.py +409 -0
  143. mdb/serve/__init__.py +17 -0
  144. mdb/serve/__main__.py +180 -0
  145. mdb/serve/_legacy.py +97 -0
  146. mdb/serve/app.py +526 -0
  147. mdb/serve/blobs.py +280 -0
  148. mdb/serve/caps.py +148 -0
  149. mdb/serve/export.py +9 -0
  150. mdb/serve/guard.py +216 -0
  151. mdb/serve/live.py +412 -0
  152. mdb/serve/live_reduce.py +90 -0
  153. mdb/serve/recorded.py +515 -0
  154. mdb/serve/reduce.py +449 -0
  155. mdb/serve/schema.py +969 -0
  156. mdb/serve/states.py +125 -0
  157. mdb/serve/static/assets/index-BJpByNCO.js +58 -0
  158. mdb/serve/static/assets/index-H9zlNeqq.css +1 -0
  159. mdb/serve/static/index.html +14 -0
  160. mdb/serve/static/manifest.json +5 -0
  161. mdb/serve/stored.py +34 -0
  162. mdb/serve/typegen.py +131 -0
  163. mdb/serve/views/__init__.py +1 -0
  164. mdb/serve/views/common.py +201 -0
  165. mdb/serve/views/frame.py +235 -0
  166. mdb/serve/views/graph.py +224 -0
  167. mdb/serve/views/live.py +402 -0
  168. mdb/serve/views/records.py +557 -0
  169. mdb/serve/views/sessions.py +151 -0
  170. mdb/serve/views/symbols.py +185 -0
  171. mdb/serve/views/timeline.py +121 -0
  172. model_debugger-0.4.0.dist-info/METADATA +1516 -0
  173. model_debugger-0.4.0.dist-info/RECORD +177 -0
  174. model_debugger-0.4.0.dist-info/WHEEL +4 -0
  175. model_debugger-0.4.0.dist-info/entry_points.txt +2 -0
  176. model_debugger-0.4.0.dist-info/licenses/LICENSE +202 -0
  177. model_debugger-0.4.0.dist-info/licenses/NOTICE +6 -0
mdb/engine/bundle.py ADDED
@@ -0,0 +1,137 @@
1
+ """Bundles: a named checkpoint and the records that reference it, as one directory another project
2
+ imports (``export NAME to=DIR``; ``start from=DIR`` or ``Debugger.import_bundle(DIR)``).
3
+
4
+ Layout (a ``core.checkpoint_store.DirectoryStore`` plus a manifest and the recipe)::
5
+
6
+ <dir>/bundle.json schema, name, the checkpoint-state record id, the
7
+ checkpoint record and recipe ids (or null), record ids
8
+ <dir>/tensors/<sha256>.safetensors blobs: the state's document and parts, the recipe's
9
+ edit tensors and token ids, the records' tensor pointers
10
+ <dir>/records/<record id>.json sealed records
11
+ <dir>/recipes/<fingerprint>.json the replay recipe of the named checkpoint
12
+
13
+ The records that reference the checkpoint are every record of the project whose sealed JSON names
14
+ the checkpoint-state record id (a record about a run started from it carries the id in
15
+ ``custody.from_checkpoint``). Blobs are checked against their sha256 and records against their
16
+ seals both ways (``checkpoint_store``); importing twice is a no-op.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import json
22
+ from pathlib import Path
23
+ from typing import Any
24
+
25
+ SCHEMA = "mdb-bundle-v1"
26
+ MANIFEST = "bundle.json"
27
+
28
+
29
+ def is_bundle(path: Any) -> bool:
30
+ return isinstance(path, (str, Path)) and (Path(path).expanduser() / MANIFEST).is_file()
31
+
32
+
33
+ def _send(source: Any, target: Any, shas: list[str]) -> None:
34
+ for sha in dict.fromkeys(shas):
35
+ if not target.has(sha):
36
+ target.put(sha, source.get(sha))
37
+
38
+
39
+ def _recipe_blobs(recipe: dict[str, Any]) -> list[str]:
40
+ from mdb.core import recipe as recipes
41
+
42
+ refs = [p["ref"] for p in recipes.tensor_pointers(recipe)]
43
+ if isinstance(recipe["tokens"], dict):
44
+ refs.append(recipe["tokens"]["ref"])
45
+ return refs
46
+
47
+
48
+ def _tensor_refs(record: dict[str, Any]) -> list[str]:
49
+ return [p["ref"] for p in record.get("pointers", ()) if p.get("kind") == "tensor"]
50
+
51
+
52
+ def write(project: Any, name: str, path: Any, *, session_id: str | None = None) -> dict[str, Any]:
53
+ """Write checkpoint ``name`` of ``project`` (``session_id``'s first, as ``restart``) and the
54
+ records that reference it to the directory ``path``; returns the manifest."""
55
+ from mdb.core import checkpoint_store as stores
56
+ from mdb.core import index
57
+ from mdb.core import recipe as recipes
58
+
59
+ from .checkpoint import find_named
60
+
61
+ state = find_named(project, name, session_id=session_id)
62
+ if state is None:
63
+ raise ValueError(f"no checkpoint named {name!r} in project {project.root} (checkpoint "
64
+ "NAME stores one)")
65
+ out = Path(path).expanduser().absolute()
66
+ if out.exists() and any(out.iterdir()):
67
+ raise ValueError(f"{out} is not empty: export writes a new bundle directory")
68
+ local, target = stores.ProjectStore(project), stores.DirectoryStore(out)
69
+ stores.copy(state, None, local, target)
70
+ checkpoint = recipes.find_checkpoint(project, name, session_id=state["session_id"])
71
+ recipe = None
72
+ if checkpoint is not None and checkpoint["body"]["cut"] == state["body"]["cut"]["fingerprint"]:
73
+ recipe = recipes.load(project, checkpoint["body"]["recipe"])
74
+ _send(local, target, _recipe_blobs(recipe))
75
+ target._put(f"recipes/{recipe['fingerprint']}.json",
76
+ json.dumps(recipe, sort_keys=True).encode())
77
+ target.put_record(checkpoint)
78
+ else:
79
+ checkpoint = None
80
+ referencing = []
81
+ for session in project.sessions():
82
+ for record in index.list_records(project, session["session_id"]):
83
+ if record.get("verified") is False or "pointers" not in record \
84
+ or record["id"] == state["id"] or state["id"] not in json.dumps(record):
85
+ continue
86
+ _send(local, target, _tensor_refs(record))
87
+ target.put_record(record)
88
+ referencing.append(record["id"])
89
+ manifest = {"schema": SCHEMA, "name": name, "state": state["id"],
90
+ "checkpoint": None if checkpoint is None else checkpoint["id"],
91
+ "recipe": None if recipe is None else recipe["fingerprint"],
92
+ "records": referencing}
93
+ (out / MANIFEST).write_text(json.dumps(manifest, indent=1, sort_keys=True) + "\n")
94
+ return manifest
95
+
96
+
97
+ def read(project: Any, path: Any) -> dict[str, Any]:
98
+ """Import the bundle at ``path`` into ``project``: blobs, the recipe, every record (each
99
+ opens its session under the record's model identity and execution). Returns the
100
+ checkpoint-state record as the project indexes it."""
101
+ from mdb.core import checkpoint_store as stores
102
+ from mdb.core import kinds # noqa: F401 (declares the record kinds a bundle may hold)
103
+ from mdb.core import recipe as recipes
104
+
105
+ from .checkpoint import find_record
106
+
107
+ root = Path(path).expanduser().absolute()
108
+ manifest = json.loads((root / MANIFEST).read_text())
109
+ if manifest.get("schema") != SCHEMA:
110
+ raise ValueError(f"{root} is not an {SCHEMA} bundle (schema {manifest.get('schema')!r})")
111
+ source, local = stores.DirectoryStore(root), stores.ProjectStore(project)
112
+
113
+ def record(identifier: str) -> dict[str, Any]:
114
+ found = source.find_record(identifier)
115
+ if found is None or found["id"] != identifier:
116
+ raise ValueError(f"bundle {root} lacks record {identifier}")
117
+ return found
118
+
119
+ state = record(manifest["state"])
120
+ stores.copy(state, None, source, local)
121
+ if manifest["recipe"] is not None:
122
+ data = source._get(f"recipes/{manifest['recipe']}.json")
123
+ if data is None:
124
+ raise ValueError(f"bundle {root} lacks recipe {manifest['recipe']}")
125
+ recipe = json.loads(data)
126
+ checkpoint = record(manifest["checkpoint"])
127
+ _send(source, local, _recipe_blobs(recipe))
128
+ local.put_record(checkpoint)
129
+ recipes.save(project, checkpoint["session_id"], recipe)
130
+ for identifier in manifest["records"]:
131
+ found = record(identifier)
132
+ _send(source, local, _tensor_refs(found))
133
+ local.put_record(found)
134
+ return find_record(project, state["id"])
135
+
136
+
137
+ __all__ = ["MANIFEST", "SCHEMA", "is_bundle", "read", "write"]