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
@@ -0,0 +1,95 @@
1
+ """Rounding bounds of the margin identity: spacings of a float format, the residual-stream
2
+ adds and the head matmul (round-to-nearest, stated premises).
3
+
4
+ ``_stream_rounding_bound`` bounds the roundings of the residual adds of a captured row and
5
+ ``_head_bound`` the native head's dot products and output rounding; ``explain`` and
6
+ ``circuits.build`` use them.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import math
12
+ from collections.abc import Mapping, Sequence
13
+ from typing import Any
14
+
15
+ from ..numerics.labels import f64, f64_at
16
+ from .capture import RowCapture
17
+
18
+
19
+ def _ulp(x: float, eps: float) -> float:
20
+ """Spacing of a float format with machine epsilon ``eps`` at ``x``: ``eps 2^floor(log2|x|)``."""
21
+ x = abs(float(x))
22
+ return 0.0 if x == 0.0 else eps * 2.0 ** math.floor(math.log2(x))
23
+
24
+
25
+ def _half_ulp(z: Any) -> Any:
26
+ """Per-coordinate ``1/2 ulp(|z_i|)`` of ``z``'s own dtype as float64 (0 where ``z_i = 0``)."""
27
+ import torch
28
+
29
+ eps = float(torch.finfo(z.dtype).eps)
30
+ x = f64(z).reshape(-1).abs()
31
+ spacing = eps * torch.exp2(torch.floor(torch.log2(x.clamp_min(1e-300))))
32
+ return 0.5 * torch.where(x > 0, spacing, torch.zeros_like(x))
33
+
34
+
35
+ def _stream_rounding_bound(caps: RowCapture, originals: Sequence[Mapping[str, Any]],
36
+ c: Any) -> float:
37
+ """``sum_i |c_i| sum_adds 1/2 ulp(|z_i|)`` over the residual adds of the row (without ``rho``).
38
+
39
+ Each native add ``z = fl(x + y)`` rounds once, to nearest, in the dtype of the stream, so
40
+ ``|z - (x + y)| <= 1/2 ulp(z)`` per coordinate (``ulp`` = spacing of the binade of the result;
41
+ this also holds when the sum rounds up into the next binade). The adds are ``r = h + o`` and
42
+ ``h_out = r + m`` of every layer; ``z`` is the captured ``r`` / ``h_out`` (for an edited
43
+ ``h_out`` the pre-edit native add result when it is retained). The remainder is the exact sum
44
+ of these roundings (``e = sum_l [r - (h + o)] + [h_out - (r + m)]`` telescopes), so
45
+ ``|rho c^T e| <= rho sum_i |c_i| sum_adds 1/2 ulp(|z_i|)``.
46
+ """
47
+ total = 0 * c
48
+ for layer, ports in enumerate(caps.ports):
49
+ held = originals[layer] if layer < len(originals) else {}
50
+ total = total + _half_ulp(ports["r"]) + _half_ulp(held.get("h_out", ports["h_out"]))
51
+ return float(c.abs() @ total)
52
+
53
+
54
+ def _head_bound(caps: RowCapture, weight: Any, bias: Any, a: int, b: int,
55
+ acc: Any = None) -> tuple[float | None, list]:
56
+ """Bound on ``|native margin - (W_a - W_b).fn - (b_a - b_b)|`` and its premises.
57
+
58
+ Each native logit is ``fl(W_j . fn + b_j)``. A length-``d`` dot product in accumulation
59
+ precision ``u`` obeys ``|fl(w.x) - w.x| <= gamma_d sum_i |w_i x_i|``, ``gamma_d = d u / (1 - d u)``
60
+ (any summation order, FMA allowed); a 16-bit output is then rounded once more
61
+ (``1/2 ulp`` of the logit), and a bias add rounds once more. The margin is the float64
62
+ difference of the two native logits, so the bound is the sum over ``j in {a, b}``.
63
+
64
+ ``acc`` (``accumulation.Accumulation`` of the run) gives ``u`` for a 16-bit head: float32
65
+ unless CUDA reduced-precision reductions are allowed; when ``gamma_d`` does not exist there
66
+ the bound is None and the premises' last entry is the refusal.
67
+ """
68
+ import torch
69
+
70
+ dtype = caps.logits.dtype
71
+ sixteen = dtype in (torch.bfloat16, torch.float16)
72
+ x = f64(caps.normed).reshape(-1)
73
+ d = x.numel()
74
+ if sixteen and acc is not None:
75
+ premise = acc.premise
76
+ gamma = acc.gamma(d)
77
+ if gamma is None:
78
+ return None, ["round-to-nearest", f"logit dtype {dtype}", acc.refusal(d)]
79
+ else:
80
+ u = 2.0 ** -24 if sixteen else float(torch.finfo(dtype).eps) / 2.0
81
+ premise = "matmul accumulated in " + ("float32 (16-bit inputs)" if sixteen else str(dtype))
82
+ gamma = d * u / (1.0 - d * u)
83
+ logits = caps.logits.reshape(-1)
84
+ rounds = (1 if sixteen else 0) + (1 if bias is not None else 0)
85
+ spacing = float(torch.finfo(dtype).eps)
86
+ total = 0.0
87
+ for j in (a, b):
88
+ total += gamma * float((f64_at(weight, j) * x).abs().sum()) # one row, not the head
89
+ total += rounds * 0.5 * _ulp(float(logits[j]), spacing)
90
+ premises = ["round-to-nearest", f"logit dtype {dtype}",
91
+ premise,
92
+ "head output rounded once" if sixteen else "no extra output rounding"]
93
+ if bias is not None:
94
+ premises.append("bias added with one rounding")
95
+ return total, premises