graphyco 0.1.0__tar.gz

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 (30) hide show
  1. graphyco-0.1.0/PKG-INFO +494 -0
  2. graphyco-0.1.0/README.md +481 -0
  3. graphyco-0.1.0/pyproject.toml +25 -0
  4. graphyco-0.1.0/setup.cfg +4 -0
  5. graphyco-0.1.0/src/graphyco/__init__.py +44 -0
  6. graphyco-0.1.0/src/graphyco/bridge/__init__.py +32 -0
  7. graphyco-0.1.0/src/graphyco/bridge/grad_bridge.py +430 -0
  8. graphyco-0.1.0/src/graphyco/bridge/torch_bridge.py +191 -0
  9. graphyco-0.1.0/src/graphyco/core/__init__.py +11 -0
  10. graphyco-0.1.0/src/graphyco/core/evaluation.py +85 -0
  11. graphyco-0.1.0/src/graphyco/core/graph.py +79 -0
  12. graphyco-0.1.0/src/graphyco/core/invariants.py +64 -0
  13. graphyco-0.1.0/src/graphyco/core/primitives.py +108 -0
  14. graphyco-0.1.0/src/graphyco/observer/__init__.py +47 -0
  15. graphyco-0.1.0/src/graphyco/observer/grad_eval.py +580 -0
  16. graphyco-0.1.0/src/graphyco/observer/grad_observer.py +387 -0
  17. graphyco-0.1.0/src/graphyco/tests/test_visualizer.py +185 -0
  18. graphyco-0.1.0/src/graphyco/visualizer/__init__.py +29 -0
  19. graphyco-0.1.0/src/graphyco/visualizer/__main__.py +106 -0
  20. graphyco-0.1.0/src/graphyco/visualizer/app.py +382 -0
  21. graphyco-0.1.0/src/graphyco/visualizer/diagnostics.py +234 -0
  22. graphyco-0.1.0/src/graphyco/visualizer/gui.py +476 -0
  23. graphyco-0.1.0/src/graphyco/visualizer/query.py +492 -0
  24. graphyco-0.1.0/src/graphyco/visualizer/renderer.py +107 -0
  25. graphyco-0.1.0/src/graphyco.egg-info/PKG-INFO +494 -0
  26. graphyco-0.1.0/src/graphyco.egg-info/SOURCES.txt +28 -0
  27. graphyco-0.1.0/src/graphyco.egg-info/dependency_links.txt +1 -0
  28. graphyco-0.1.0/src/graphyco.egg-info/entry_points.txt +2 -0
  29. graphyco-0.1.0/src/graphyco.egg-info/requires.txt +7 -0
  30. graphyco-0.1.0/src/graphyco.egg-info/top_level.txt +1 -0
@@ -0,0 +1,494 @@
1
+ Metadata-Version: 2.4
2
+ Name: graphyco
3
+ Version: 0.1.0
4
+ Summary: Computational graph topology and dynamic gradient-flow monitoring for PyTorch
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: torch
8
+ Requires-Dist: numpy
9
+ Requires-Dist: pandas
10
+ Requires-Dist: scipy
11
+ Provides-Extra: gui
12
+ Requires-Dist: PySide6; extra == "gui"
13
+
14
+ # Graphyco
15
+
16
+ Computational graph topology and dynamic gradient-flow monitoring for PyTorch neural networks.
17
+
18
+ Graphyco maps neural network architectures into a formal computational state graph:
19
+
20
+ $$G_t = (V, E, X_t, A_t, G_t)$$
21
+
22
+ - **$V$ (Operational Nodes):** Computational units (layers, functions, torch operators, input/output placeholders).
23
+ - **$E$ (Directed Dependency Edges):** Directed dataflow dependencies classified as `operational`, `informational`, or `governance`.
24
+ - **$X_t$ (Structural Invariants & Capacity):** Fixed-point graph metrics, structural capacity vectors, and domain constants.
25
+ - **$A_t$ (Forward Activation Telemetry):** Forward tensor statistics (RMS, L1, L2, sparsity, mean, std) captured during execution.
26
+ - **$G_t$ (Backward Gradient Telemetry):** Backward tensor statistics across activation gradients, parameter gradients, and edge-intercepted gradients.
27
+
28
+ ---
29
+
30
+ ## Architecture Overview
31
+
32
+ ```
33
+ graphyco/
34
+ ├── src/
35
+ │ └── graphyco/ # Core Python package & library
36
+ │ ├── core/ # Deterministic graph primitives & invariant verification
37
+ │ │ ├── primitives.py # Node, Edge, CapacityVector, GraphState, checked arithmetic
38
+ │ │ ├── graph.py # Adjacency, density, degree metrics, DFS critical cycle detection
39
+ │ │ ├── evaluation.py # Coherence, topological bottlenecks, perturbation resilience
40
+ │ │ └── invariants.py # Seven enforced graph invariant validation rules
41
+ │ ├── bridge/ # PyTorch model translation & execution orchestration
42
+ │ │ ├── torch_bridge.py # FX symbolic tracing, module fallback, parameter counting
43
+ │ │ └── grad_bridge.py # DynamicExecutionBridge, DynamicGraphState, LiveTrainingMonitor
44
+ │ ├── observer/ # Observational runtime telemetry engine
45
+ │ │ ├── grad_observer.py # TensorStats, EdgeAutogradHook, FXGradObserver, ModuleGradObserver
46
+ │ │ └── grad_eval.py # Edge attenuation, dynamic bottleneck G_max, Types I-IV, temporal CV
47
+ │ ├── visualizer/ # Query engine, REST API, CLI, and PySide6 Desktop GUI
48
+ │ │ ├── app.py # visualize(), extract_benchmark_json()
49
+ │ │ ├── query.py # QueryEngine, describe(), get_bottlenecks(), get_neighbors()
50
+ │ │ ├── server.py # AppServer HTTP daemon (/api/summary, /api/bottlenecks, etc.)
51
+ │ │ ├── diagnostics.py # LiveTrainingDiagnostics runtime monitor
52
+ │ │ ├── gui.py # VisualizerDesktopApp (PySide6 Qt GUI)
53
+ │ │ └── __main__.py # CLI runner (`python -m graphyco.visualizer`)
54
+ │ └── tests/ # Automated test suite (20 unit & integration tests)
55
+ │ ├── test_dynamic_flow.py # Dynamic gradient flow unit tests (12 tests)
56
+ │ ├── test_visualizer.py # Query engine & visualizer integration tests (8 tests)
57
+ │ ├── test_torch_eval.py # Standalone architecture evaluation runner
58
+ │ └── test_compare.py # Comparative topology runner
59
+ ├── notebooks/ # Interactive Jupyter notebooks
60
+ │ ├── tutorials/ # Step-by-step tutorial notebooks
61
+ │ │ ├── 01_quickstart_and_model_evaluation.ipynb
62
+ │ │ ├── 02_dynamic_gradient_monitoring.ipynb
63
+ │ │ ├── 03_live_training_diagnostics.ipynb
64
+ │ │ └── 04_query_engine_and_visualization.ipynb
65
+ │ └── experiments/ # Empirical validation experiment notebooks
66
+ │ ├── 01_static_topological_validation.ipynb
67
+ │ ├── 02_dynamic_gradient_flow_validation.ipynb
68
+ │ ├── 03_ablation_reachability_benchmark.ipynb
69
+ │ ├── 04_model_initialization_and_export.ipynb
70
+ │ └── 05_telemetry_query_and_analysis.ipynb
71
+ ├── validation/ # Validation runners and benchmark data
72
+ │ ├── validate_framework.py # 6 controlled static topological validation protocols
73
+ │ ├── validate_dynamic_flow.py # 7 dynamic gradient-flow validation experiments (Q1–Q5)
74
+ │ ├── canonical_benchmark.json # Canonical 5-model benchmark data
75
+ │ ├── dynamic_flow_benchmark.json # Dynamic flow gradient telemetry export
76
+ │ ├── custom_model_benchmark.json # Custom architecture benchmark
77
+ │ ├── dense_benchmark.json # DenseNet benchmark
78
+ │ ├── mlp_benchmark.json # Sequential MLP benchmark
79
+ │ └── resnet_benchmark.json # ResNet Block benchmark
80
+ ├── pyproject.toml # Package configuration and CLI entrypoint
81
+ ├── research_understanding.md # Mathematical research documentation
82
+ └── README.md # Project documentation
83
+ ```
84
+
85
+ ---
86
+
87
+ ## Mathematical Specifications & Deterministic State Kernel
88
+
89
+ ### 1. Deterministic Fixed-Point Arithmetic
90
+
91
+ All structural state metrics and invariant calculations are performed using 64-bit integer fixed-point arithmetic to guarantee bit-for-bit reproducibility:
92
+
93
+ - **Fixed-Point Scale Factor:** $\text{SCALE} = 10{,}000$ (e.g., $1.0000 \to 10000$).
94
+ - **Bounds:** Int64 integer range $[-2^{63}, 2^{63}-1]$.
95
+ - **Checked Arithmetic:** Operations use `checked_add(a, b)` and `checked_mul(a, b)`, raising `OverflowError` if results cross bounds.
96
+ - **Accumulator Guard:** Matrix multiplications and vector reductions employ an intermediate 128-bit accumulator (`safe_mul`) before dividing by `SCALE` to eliminate integer overflow during covariance operations:
97
+ $$\text{safe\_mul}(a, b) = \left\lfloor \frac{\text{int128}(a) \times \text{int128}(b)}{\text{SCALE}} \right\rfloor$$
98
+
99
+ ### 2. Resource Representation & Non-Negative Stress
100
+
101
+ To avoid unconstrained algebraic cancellation, resources are defined as an ordered real pair in $\mathbb{R} \times \mathbb{R}_{\ge 0}$:
102
+
103
+ $$R_v = (a_v, b_v)$$
104
+
105
+ - $a_v \in \mathbb{R}$: Available operational capacity / activation baseline.
106
+ - $b_v \in \mathbb{R}_{\ge 0}$: Accumulated structural stress, strictly non-negative ($\forall v \in V, b_v \ge 0$).
107
+ - **Pathological State Condition:** A node is in a pathological state if and only if $b_v > 0$:
108
+ $$\text{pathological}(v) \iff b_v > 0$$
109
+ - When $b_v$ exceeds a collapse threshold $\theta_{\text{collapse}}$, stress is redistributed via explicit governed operations, ensuring $\sum_{v} b_v$ is non-decreasing without an explicit discharge event.
110
+
111
+ ### 3. Seven Enforced Graph Invariants (`graphyco/core/invariants.py`)
112
+
113
+ Validation function `validate_invariants(state: GraphState)` verifies that the graph satisfies all seven structural invariants:
114
+
115
+ | Invariant | Rule Identifier | Formal Requirement |
116
+ |---|---|---|
117
+ | **INV-1** | `node_id_format` | Every node ID must match `^[a-zA-Z0-9_-]+$`. |
118
+ | **INV-2** | `edge_integrity` | For every edge $e = (u, v)$, both $u \in V$ and $v \in V$. |
119
+ | **INV-3** | `port_connectivity` | Every output port produced by a node must be consumed as an input port by $\ge 1$ node. |
120
+ | **INV-4** | `node_uniqueness` | All node identifiers in $V$ must be distinct (no duplicates). |
121
+ | **INV-5** | `graph_liveness` | The graph must contain at least one active node ($\exists v \in V: v.\text{active} = \text{True}$). |
122
+ | **INV-6** | `attributes_nonempty` | Every node must contain $\ge 1$ descriptive attribute string. |
123
+ | **INV-7** | `acyclic_critical_subgraph` | The critical operational subgraph $G_{\text{crit}} = (V, E_{\text{critical}})$ must be a Directed Acyclic Graph (DAG) with zero cycles. |
124
+
125
+ Violation of any rule raises an explicit `InvariantViolationError(rule, detail)`.
126
+
127
+ ### 4. Dynamic State Transitions & Schur Complement Update
128
+
129
+ When new operational nodes are introduced (e.g., dynamic splitting or expansion $V \to V+1$), the covariance matrix $\Sigma \in \mathbb{R}^{V \times V}$ expands to:
130
+
131
+ $$\Sigma' = \begin{bmatrix} \Sigma & \mathbf{b} \\ \mathbf{b}^T & c \end{bmatrix}$$
132
+
133
+ where $\mathbf{b} \in \mathbb{R}^V$ is the covariance between the new node and existing nodes, and $c \in \mathbb{R}$ is the node variance. The inverse $(\Sigma')^{-1}$ is computed in $O(V^2)$ rather than $O(V^3)$ via the Schur complement:
134
+
135
+ $$(\Sigma')^{-1} = \begin{bmatrix} P + \frac{1}{S_{\text{safe}}} P \mathbf{b} \mathbf{b}^T P & -\frac{1}{S_{\text{safe}}} P \mathbf{b} \\ -\frac{1}{S_{\text{safe}}} \mathbf{b}^T P & \frac{1}{S_{\text{safe}}} \end{bmatrix}$$
136
+
137
+ where $P = \Sigma^{-1}$ is the prior inverse, and $S_{\text{safe}}$ applies deterministic Tikhonov regularization against singularity:
138
+
139
+ $$S_{\text{safe}} = \max\left(c - \mathbf{b}^T P \mathbf{b},\; \epsilon_{\min}\right)$$
140
+
141
+ with $\epsilon_{\min} \in \text{DomainConstants}$.
142
+
143
+ ### 5. Transition Metrics: Gromov-Wasserstein vs. KL Divergence
144
+
145
+ - **Topological Transitions (Variable Support):** When node splits, merges, or graph mutations alter the dimensionality of state space ($\mathcal{X}_N \to \mathcal{X}_{M}$), Kullback-Leibler divergence is undefined ($D_{\text{KL}} \to \infty$). Structural distance is computed via the Gromov-Wasserstein distance:
146
+ $$GW_p(\mu, \nu) = \left( \inf_{\gamma \in \Pi(\mu, \nu)} \iint |d_{\mathcal{X}}(x, x') - d_{\mathcal{Y}}(y, y')|^p \, d\gamma(x, y) \, d\gamma(x', y') \right)^{1/p}$$
147
+ To maintain temporal determinism, the Sinkhorn solver iteration count is bounded by a fixed invariant: $K_{\max} = \text{const} \in \text{DomainConstants}$.
148
+ - **Parametric Transitions (Fixed Support):** For transformations preserving graph topology (e.g., capacity adjustments, weight updates, phase shifts), divergence is measured via standard discrete $D_{\text{KL}}$.
149
+
150
+ ### 6. Topological Observer Expressivity (Cycle Detection & GSN)
151
+
152
+ Standard 1-Weisfeiler-Lehman (1-WL) graph isomorphism tests fail to detect cycles (e.g., cannot distinguish a 6-cycle from two disjoint 3-cycles). To preserve cycle sensitivity without the $O(V^k)$ memory explosion of $k$-WL ($k \ge 3$):
153
+ - Cycles of length 3, 4, 5, and 6 are extracted deterministically via depth-first traversal during structural mutations.
154
+ - The resulting counts form a static cycle feature vector $\mathbf{c}_v \in \mathbb{Z}^4$ attached directly to each node:
155
+ $$R_v = (a_v, b_v, \mathbf{c}_v)$$
156
+
157
+ ---
158
+
159
+ ## Static Topological Metrics (`graphyco/core/evaluation.py`)
160
+
161
+ All metrics return scaled integer values ($\text{SCALE} = 10{,}000$):
162
+
163
+ ### 1. Graph Density ($D$)
164
+ Ratio of directed edges present to maximum possible directed edges:
165
+ $$D = \left\lfloor \frac{|E| \cdot \text{SCALE}}{|V|(|V|-1)} \right\rfloor$$
166
+
167
+ ### 2. Connectivity Coherence ($C$)
168
+ Measures degree distribution uniformity across all nodes. Max variance $\text{MaxVar} = \frac{(N-1) \cdot \text{SCALE}^2}{N}$:
169
+ $$C = \text{SCALE} - \left\lfloor \frac{\text{Var}(d)}{\text{MaxVar}} \cdot \text{SCALE} \right\rfloor, \quad C \in [0, \text{SCALE}]$$
170
+ - $C = 10{,}000$ indicates identical degree distribution across all nodes (perfect balance).
171
+ - Lower $C$ indicates concentrated hub architectures.
172
+
173
+ ### 3. Topological Bottleneck Ratio ($B$)
174
+ Ratio of maximum node degree to mean node degree:
175
+ $$B = \left\lfloor \frac{\max_i(d_i) \cdot \text{SCALE}}{\bar{d}} \right\rfloor$$
176
+ - $B = 10{,}000$ ($1.0$) corresponds to perfectly uniform connectivity.
177
+ - Elevated $B$ ($> 1.5$) identifies structural choke nodes.
178
+
179
+ ### 4. Perturbation Profile & Structural Perturbation Resilience ($R$)
180
+ Local cascade cost per node $i$:
181
+ $$\text{cost}_i = \max\left(1 + \left\lfloor \frac{d_i}{\text{SCALE}} \right\rfloor, 1\right) + \sum_{j \in \mathcal{N}(i)} \max\left(\left\lfloor \frac{d_j}{\text{SCALE}} \right\rfloor, 1\right)$$
182
+ Structural perturbation resilience across graph order $N = |V|$:
183
+ $$R = \left\lfloor \frac{N \cdot \text{SCALE}}{\sum_{i=1}^N \text{cost}_i} \right\rfloor$$
184
+
185
+ ### 5. Fan Balance & Dead Ratio
186
+ - **Fan-in / Fan-out Imbalance:**
187
+ $$\text{imbalance}_i = \left\lfloor \frac{|\text{fan\_in}_i - \text{fan\_out}_i| \cdot \text{SCALE}}{\text{fan\_in}_i + \text{fan\_out}_i} \right\rfloor$$
188
+ - **Dead Ratio:** Fraction of isolated nodes with zero incoming and outgoing edges:
189
+ $$\text{dead\_ratio} = \left\lfloor \frac{|V_{\text{isolated}}| \cdot \text{SCALE}}{|V|} \right\rfloor$$
190
+
191
+ ---
192
+
193
+ ## Dynamic Gradient-Flow Telemetry (`graphyco/observer/`)
194
+
195
+ ### 1. Tensor Statistics Engine (`TensorStats`)
196
+ Computes observational metrics over detached float64 representations without modifying autograd computation:
197
+ - **Element Count:** $N = \text{numel}(T)$
198
+ - **Norms:** $L_1 = \sum |x_i|$, $L_2 = \sqrt{\sum x_i^2}$, $\text{RMS} = \frac{L_2}{\sqrt{N}}$
199
+ - **Moments:** Mean $\mu$, Standard Deviation $\sigma$, Minimum, Maximum
200
+ - **Sparsity & Health:** Zero-element fraction ($\frac{\sum \mathbb{I}[x_i = 0]}{N}$), Non-finite fraction ($\frac{\sum \mathbb{I}[\neg \text{isfinite}(x_i)]}{N}$)
201
+ - **Fixed-point RMS:** $\text{round}(\text{RMS} \cdot \text{SCALE})$
202
+
203
+ ### 2. Edge Gradient Attenuation (`compute_gradient_attenuation`)
204
+ For directed edge $e = (\text{source} \to \text{target})$:
205
+ - **Forward Ratio:**
206
+ $$\alpha = \frac{\|g_{\text{source}}\|_2}{\|g_{\text{target}}\|_2 + \epsilon}$$
207
+ - **Backward Ratio:**
208
+ $$\beta = \frac{\|g_{\text{target}}\|_2}{\|g_{\text{source}}\|_2 + \epsilon}$$
209
+ - **Log Attenuation:**
210
+ $$\Delta = \ln\left(\frac{\|g_{\text{source}}\|_2 + \epsilon}{\|g_{\text{target}}\|_2 + \epsilon}\right)$$
211
+ - $\Delta < 0$: Gradient attenuates as it flows backward toward the input.
212
+ - $\Delta > 0$: Gradient amplifies along the backward path.
213
+
214
+ ### 3. Dynamic Gradient Bottleneck ($G_{\max}$)
215
+ Concentration of backward gradient energy relative to uniform flow:
216
+ $$q_i = \frac{g_i}{\frac{1}{|V|}\sum_{j \in V} g_j + \epsilon}, \quad G_{\max} = \max_{i \in V} q_i, \quad \text{choke\_node} = \arg\max_{i \in V} q_i$$
217
+ where $g_i$ is the normalized gradient RMS of node $i$.
218
+
219
+ ### 4. Forward-Backward Alignment (`compute_forward_backward_alignment`)
220
+ Quantifies relationship between activation magnitude $a_i = \text{RMS}(A_i)$ and gradient magnitude $g_i = \text{RMS}(G_i)$:
221
+ - **Local Sensitivity Ratio:** $r_i = \frac{g_i}{a_i + \epsilon}$
222
+ - **Spearman Rank Correlation:** $\rho_{AG} = \text{spearmanr}(\mathbf{a}, \mathbf{g})$
223
+ - **Pearson Linear Correlation:** $r_{AG} = \text{pearsonr}(\mathbf{a}, \mathbf{g})$
224
+
225
+ ### 5. Four-Quadrant Structural-Functional Classification
226
+ Nodes are partitioned by median splits on static topology perturbation score ($t_i$) and dynamic gradient RMS ($g_i$):
227
+
228
+ | Quadrant | Structural Importance | Gradient Flux | Functional Role |
229
+ |---|---|---|---|
230
+ | **Type I** | High ($t_i \ge \tilde{t}$) | High ($g_i \ge \tilde{g}$) | **Dual Hub:** Central structural router carrying high computational signal. |
231
+ | **Type II** | High ($t_i \ge \tilde{t}$) | Low ($g_i < \tilde{g}$) | **Structural Hub / Low Flux:** High connectivity but suppressed gradient flow. |
232
+ | **Type III** | Low ($t_i < \tilde{t}$) | High ($g_i \ge \tilde{g}$) | **Dynamic Conduit:** Peripheral structure absorbing disproportionate gradient flux. |
233
+ | **Type IV** | Low ($t_i < \tilde{t}$) | Low ($g_i < \tilde{g}$) | **Peripheral:** Low structural impact and low gradient activity. |
234
+
235
+ ### 6. Temporal Stability & Anomaly Detection
236
+ Tracks node behavior across execution steps $t = 1, \dots, T$:
237
+ - **Temporal Coefficient of Variation (CV):**
238
+ $$\text{CV}_i = \frac{\sigma_{g_i}}{|\mu_{g_i}| + \epsilon}$$
239
+ - **Behavioral Classification:**
240
+ - `intermittent`: Zero-gradient fraction $> 0.30$.
241
+ - `unstable`: $\text{CV}_i > 0.50$.
242
+ - `consistently_strong`: $\mu_{g_i} \ge \text{median}(\boldsymbol{\mu}_g)$ with low variance.
243
+ - `consistently_weak`: $\mu_{g_i} < \text{median}(\boldsymbol{\mu}_g)$.
244
+ - **Anomaly Detection:**
245
+ - **Vanishing Nodes:** $g_i < \tau_{\text{low}}$ (default absolute $10^{-6}$ or relative $0.01 \times \tilde{g}$).
246
+ - **Exploding Nodes:** $g_i > \tau_{\text{high}}$ (default absolute $100.0$ or relative $100.0 \times \tilde{g}$).
247
+
248
+ ---
249
+
250
+ ## Bridge Layer Architecture (`graphyco/bridge/`)
251
+
252
+ ### 1. PyTorch FX Tracing & Module Extraction (`torch_bridge.py`)
253
+ - `trace_to_graph(model, concrete_args)`: Executes `torch.fx.symbolic_trace`, converting PyTorch FX IR nodes into `Node` primitives and dataflow arguments into directed `Edge` primitives.
254
+ - `module_to_graph(model)`: Fallback recursive `nn.Module` inspector when symbolic tracing is unsupported (e.g., dynamic control flow, third-party C++ bindings).
255
+ - `evaluate_model(model, mode="trace")`: Translates model and returns static topological metrics.
256
+
257
+ ### 2. Autograd Execution Bridge (`grad_bridge.py`)
258
+ - `DynamicExecutionBridge`: Manages forward/backward execution, recording tensor stats on each step.
259
+ - Intercepts activation tensors via `FXGradObserver` or forward hooks.
260
+ - Intercepts backward gradients using `torch.nn.Module.register_full_backward_hook` and autograd hooks on edge tensors.
261
+ - Generates unified `DynamicGraphState` containing $G_t = (V, E, X_t, A_t, G_t)$.
262
+ - Supports export to structured JSON (`to_json()`) and CSV tables (`to_csv_nodes()`).
263
+ - `LiveTrainingMonitor`: Non-invasive context manager for training loops:
264
+ ```python
265
+ monitor = LiveTrainingMonitor(model, log_interval=5)
266
+ with monitor.observe(step):
267
+ loss.backward()
268
+ ```
269
+
270
+ ---
271
+
272
+ ## Empirical Validation Benchmark Results
273
+
274
+ ### 1. Static Validation Suite (`validation/validate_framework.py`)
275
+
276
+ Six controlled protocols executed across canonical architecture families:
277
+
278
+ #### Protocol 1: Architectural Discriminability
279
+ Evaluated on FX-traced operational graphs:
280
+
281
+ | Architecture | Nodes ($|V|$) | Edges ($|E|$) | Density ($D$) | Coherence ($C$) | Bottleneck ($B$) | Resilience ($R$) |
282
+ |---|---|---|---|---|---|---|
283
+ | **Sequential MLP** | 9 | 8 | 0.1111 | 0.9970 | 1.1251 | 0.3600 |
284
+ | **Feedforward CNN** | 9 | 8 | 0.1111 | 0.9970 | 1.1251 | 0.3600 |
285
+ | **ResNet Block** | 8 | 9 | 0.1607 | 0.9956 | 1.5000 | 0.3333 |
286
+ | **DenseNet Block** | 12 | 17 | 0.1287 | 0.9951 | 1.7653 | 0.2608 |
287
+ | **U-Net Toy** | 14 | 14 | 0.0769 | 0.9985 | 1.5000 | 0.3333 |
288
+ | **Self-Attention Toy** | 10 | 12 | 0.1333 | 0.9951 | 1.6673 | 0.2941 |
289
+
290
+ *Key Result:* Sequential MLP and CNN produce identical topological metrics ($B=1.1251, R=0.3600$) due to identical sequential dependency structures. DenseNet exhibits maximum connectivity dispersion ($B=1.7653$), while Self-Attention exhibits highest concentration ($B=1.6673$).
291
+
292
+ #### Protocol 2: Controlled Synthetic Confounder Isolation
293
+ Fixed-size graphs with identical order $|V|=10$ and size $|E|=9$:
294
+
295
+ | Topology Family | $|V|$ | $|E|$ | Density | Coherence | Bottleneck | Resilience |
296
+ |---|---|---|---|---|---|---|
297
+ | **Linear Chain** | 10 | 9 | 0.1000 | 0.9979 | 1.1115 | 0.3571 |
298
+ | **Star (Single Hub)** | 10 | 9 | 0.1000 | 0.9210 | 5.0025 | 0.3448 |
299
+ | **Binary Tree** | 10 | 9 | 0.1000 | 0.9896 | 1.6673 | 0.3571 |
300
+ | **Bottleneck Funnel** | 10 | 9 | 0.1000 | 0.9814 | 2.2231 | 0.3571 |
301
+
302
+ *Key Result:* Under identical $|V|$ and $|E|$, the Star topology yields maximum bottleneck ($5.0025$) and lowest coherence ($0.9210$), proving metrics measure connectivity geometry rather than node/edge counts.
303
+
304
+ #### Protocol 3: Width-Scale Invariance
305
+ Tested on 4-layer MLPs across parameter scales:
306
+ - **Width 64 (13,130 parameters):** $|V|=9, |E|=8, B=1.1251, C=0.9970$.
307
+ - **Width 512 (793,098 parameters):** $|V|=9, |E|=8, B=1.1251, C=0.9970$.
308
+ *Key Result:* A $\sim 60\times$ increase in parameter volume yields identical topological metrics, proving decoupling of topology from parameter scale.
309
+
310
+ #### Protocol 4: Metric Correlation & Non-Collinearity (Pearson $r$)
311
+
312
+ | Metric | Density | Coherence | Bottleneck | Resilience |
313
+ |---|---|---|---|---|
314
+ | **Density** | $1.000$ | $-0.936$ | $0.328$ | $-0.389$ |
315
+ | **Coherence** | $-0.936$ | $1.000$ | $-0.559$ | $0.666$ |
316
+ | **Bottleneck** | $0.328$ | $-0.559$ | $1.000$ | $-0.928$ |
317
+ | **Resilience** | $-0.389$ | $0.666$ | $-0.928$ | $1.000$ |
318
+
319
+ #### Protocol 5: Systematic Perturbation Predictive Validity (Node Ablation)
320
+ Evaluates whether topological perturbation scores predict empirical graph reachability collapse under single-node deletion:
321
+ - **ResNet Block:** Spearman $\rho = 0.7555$ ($p = 0.0495$), Pearson $r = 0.7783$ ($p = 0.0392$).
322
+ - **U-Net Toy:** Spearman $\rho = 0.4513$ ($p = 0.1216$), Pearson $r = 0.3284$ ($p = 0.2732$).
323
+
324
+ #### Protocol 6: Asymptotic Graph Order Scaling ($N \in \{10, 25, 50, 100\}$)
325
+ - **Linear Chain:** $B \to 1.0$ asymptotically ($1.1115 \to 1.0150$).
326
+ - **Star Graph:** $B$ scales linearly with $\frac{N}{2}$ ($5.0025 \to 50.2512$).
327
+
328
+ ---
329
+
330
+ ### 2. Dynamic Flow Benchmark Suite (`validation/validate_dynamic_flow.py`)
331
+
332
+ Seven dynamic protocols addressing core research questions Q1–Q5 across canonical models:
333
+
334
+ #### Architecture-Level Dynamic Summary Table
335
+
336
+ | Architecture | Density | Coherence | Bottleneck | Resilience | Act Flow (RMS) | Grad Flow (RMS) | $G_{\max}$ | $\rho(A, G)$ | Mean CV |
337
+ |---|---|---|---|---|---|---|---|---|---|
338
+ | **Sequential MLP** | 0.1111 | 0.9970 | 1.1251 | 0.3600 | 0.3011 | 0.0036 | 3.5114 | $-1.0000$ | 0.0409 |
339
+ | **ResNet Block** | 0.1428 | 0.9956 | 1.5000 | 0.3333 | 0.6126 | 0.0045 | 2.8582 | $-0.2182$ | 0.0243 |
340
+ | **DenseBlock** | 0.1287 | 0.9951 | 1.7653 | 0.2608 | 0.5014 | 0.0034 | 3.6713 | $-0.3825$ | 0.0220 |
341
+ | **U-Net Toy** | 0.0769 | 0.9985 | 1.5000 | 0.3333 | 0.2512 | 0.0031 | 4.0313 | $-0.6784$ | 0.0534 |
342
+ | **Self-Attention Toy** | 0.1333 | 0.9951 | 1.6673 | 0.2941 | 0.5895 | 0.0039 | 3.2122 | $+0.2997$ | 0.0365 |
343
+
344
+ #### Findings for Core Research Questions (Q1–Q5)
345
+
346
+ 1. **Q1 (Structural vs. Functional Importance):**
347
+ Across all 53 evaluated nodes, Spearman correlation between static structural centrality and dynamic gradient magnitude is $\rho = -0.0583$ ($p = 0.678$). High structural connectivity does not guarantee high gradient flux. The empirical emergence of Type II nodes (high topology, low gradient) and Type III nodes (low topology, high gradient) confirms structural-functional divergence.
348
+ 2. **Q2 (Structural vs. Gradient Bottleneck):**
349
+ Static topological bottleneck $B$ identifies structural routing fan-in choke points (e.g., skip merges), whereas dynamic concentration $G_{\max}$ identifies computational signal concentration points (e.g., final linear projections `net_6`, `out`, `head`).
350
+ 3. **Q3 (Gradient Attenuation Across Paths):**
351
+ Edge log ratios ($\Delta$) reveal attenuation dynamics along skip paths. In ResNet Block, gradient ratio between parallel paths directly quantifies the signal fraction conveyed via skip vs. residual conv branches.
352
+ 4. **Q4 (Forward-Backward Alignment):**
353
+ Correlation between forward activation magnitude and backward gradient magnitude is negative or weak across standard models (MLP $\rho = -1.0000$, U-Net $\rho = -0.6784$, ResNet $\rho = -0.2182$, Self-Attention $\rho = +0.2997$), showing that activation scale is an unreliable proxy for gradient flux.
354
+ 5. **Q5 (Static Perturbation vs. Dynamic Stability):**
355
+ Static perturbation resilience correlates positively with reachability retention under ablation ($\rho = 0.1699$), verifying that topological fragility scores predict structural disconnection under node removal.
356
+
357
+ ---
358
+
359
+ ## Quickstart & Usage
360
+
361
+ ### 1. One-Line Model Profiling and Web Visualization
362
+
363
+ ```python
364
+ import torch
365
+ import torch.nn as nn
366
+ from graphyco import visualize
367
+
368
+ model = nn.Sequential(
369
+ nn.Linear(64, 128),
370
+ nn.ReLU(),
371
+ nn.Linear(128, 10)
372
+ )
373
+ x = torch.randn(8, 64)
374
+
375
+ # Profiles 5 forward/backward steps, exports benchmark JSON, and starts local server
376
+ app = visualize(model, inputs=x, steps=5, port=8000)
377
+ ```
378
+
379
+ ### 2. In-Memory Query Engine
380
+
381
+ ```python
382
+ from graphyco import QueryEngine
383
+
384
+ engine = QueryEngine("validation/dynamic_flow_benchmark.json")
385
+
386
+ # Invariant and metric summary table
387
+ summary_df = engine.describe()
388
+ print(summary_df)
389
+
390
+ # Side-by-side architecture comparison
391
+ comparison_df = engine.describe(siblings=True)
392
+ print(comparison_df)
393
+
394
+ # Retrieve bottleneck choke points
395
+ for b in engine.get_bottlenecks():
396
+ print(f"{b['architecture']}: G_max={b['gradient_bottleneck_gmax']:.2f} at node '{b['bottleneck_node']}'")
397
+ ```
398
+
399
+ ### 3. Non-Invasive Live Training Diagnostics
400
+
401
+ ```python
402
+ import torch
403
+ import torch.nn as nn
404
+ from graphyco import LiveTrainingMonitor
405
+
406
+ model = nn.Sequential(nn.Linear(64, 32), nn.ReLU(), nn.Linear(32, 10))
407
+ optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
408
+ criterion = nn.CrossEntropyLoss()
409
+
410
+ monitor = LiveTrainingMonitor(model, log_interval=5)
411
+
412
+ for step in range(20):
413
+ x = torch.randn(16, 64)
414
+ y = torch.randint(0, 10, (16,))
415
+
416
+ with monitor.observe(step):
417
+ optimizer.zero_grad()
418
+ loss = criterion(model(x), y)
419
+ loss.backward()
420
+ optimizer.step()
421
+
422
+ if monitor.has_new_data():
423
+ bneck = monitor.get_latest_bottleneck()
424
+ print(f"Step {step}: Choke Node = {bneck.get('bottleneck_node')} (G_max={bneck.get('max_concentration'):.2f})")
425
+ ```
426
+
427
+ ---
428
+
429
+ ## CLI Reference
430
+
431
+ Execute commands via the package CLI entrypoint:
432
+
433
+ ```bash
434
+ # Query all architectural bottlenecks from benchmark JSON
435
+ python -m graphyco.visualizer --json validation/dynamic_flow_benchmark.json --query bottlenecks
436
+
437
+ # Query invariant summary for a specific model architecture
438
+ python -m graphyco.visualizer --json validation/dynamic_flow_benchmark.json --query summary --arch "ResNet Block"
439
+
440
+ # Launch local HTTP REST server
441
+ python -m graphyco.visualizer --json validation/dynamic_flow_benchmark.json --server --port 8080
442
+
443
+ # Launch native PySide6 desktop GUI
444
+ python -m graphyco.visualizer --json validation/dynamic_flow_benchmark.json --gui
445
+
446
+ # Profile canonical model and export benchmark JSON
447
+ python -m graphyco.visualizer --model resnet --steps 10 --export validation/resnet_benchmark.json
448
+ ```
449
+
450
+ ---
451
+
452
+ ## Verification & Test Execution
453
+
454
+ Run the automated test suite (20 unit and integration tests):
455
+
456
+ ```bash
457
+ pytest
458
+ ```
459
+
460
+ Run standalone evaluation runners:
461
+
462
+ ```bash
463
+ python src/graphyco/tests/test_torch_eval.py
464
+ python src/graphyco/tests/test_compare.py
465
+ ```
466
+
467
+ Run the 6 static topological validation protocols:
468
+
469
+ ```bash
470
+ python validation/validate_framework.py
471
+ ```
472
+
473
+ Run the 7 dynamic gradient-flow benchmark experiments:
474
+
475
+ ```bash
476
+ python validation/validate_dynamic_flow.py
477
+ ```
478
+
479
+ ---
480
+
481
+ ## Interactive Notebook Catalog (`notebooks/`)
482
+
483
+ ### Tutorials (`notebooks/tutorials/`)
484
+ - `01_quickstart_and_model_evaluation.ipynb`: FX graph tracing, invariant verification, and static metric computation.
485
+ - `02_dynamic_gradient_monitoring.ipynb`: Dynamic telemetry extraction, edge attenuation calculations, and JSON export.
486
+ - `03_live_training_diagnostics.ipynb`: Live training loop monitoring with real-time bottleneck detection.
487
+ - `04_query_engine_and_visualization.ipynb`: Programmatic querying, REST API access, and dashboard integration.
488
+
489
+ ### Experiments (`notebooks/experiments/`)
490
+ - `01_static_topological_validation.ipynb`: Interactive execution of Protocols 1–6 from `validate_framework.py`.
491
+ - `02_dynamic_gradient_flow_validation.ipynb`: Interactive execution of Experiments A–G addressing Questions Q1–Q5.
492
+ - `03_ablation_reachability_benchmark.ipynb`: BFS reachability simulation engine under node ablation.
493
+ - `04_model_initialization_and_export.ipynb`: Multi-architecture telemetry generation and standalone export.
494
+ - `05_telemetry_query_and_analysis.ipynb`: Offline query analysis and invariant cross-comparison.