god-graph 0.6.0-alpha

A graph-based LLM white-box optimization toolbox: topology validation, Lie group orthogonalization, tensor ring compression
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# 模型可视化教程

**版本**: 0.5.0-alpha
**日期**: 2026-03-29

---

## 📋 概述

God-Graph 支持多种可视化方式,帮助你直观理解 LLM 架构:

| 可视化类型 | 格式 | 适用场景 | 工具 |
|-----------|------|---------|------|
| **DOT/Graphviz** | 静态图 | 整体架构、论文插图 | Graphviz |
| **SVG 矢量图** | 交互式 SVG | Web 展示、缩放查看 | 浏览器 |
| **HTML 交互式** | D3.js/Cytoscape | 动态探索、调试 | 浏览器 |

---

## 🚀 快速开始

### 方法 1:DOT 格式(推荐)

#### 步骤 1:导出模型

```bash
# 使用示例模型(无需真实模型文件)
cargo run --example export_model_dot --features transformer

# 使用真实模型
cargo run --example export_model_dot --features transformer -- model.safetensors model.dot
```

#### 步骤 2:渲染为图片

```bash
# 安装 Graphviz
# Ubuntu/Debian
sudo apt-get install graphviz

# macOS
brew install graphviz

# 渲染为 PNG
dot -Tpng model.dot -o model.png

# 渲染为 SVG(推荐,可缩放)
dot -Tsvg model.dot -o model.svg

# 渲染为 PDF(高质量打印)
dot -Tpdf model.dot -o model.pdf
```

#### 步骤 3:查看结果

```bash
# Linux
xdg-open model.png

# macOS
open model.png

# Windows
start model.png
```

---

### 方法 2:SVG 直接导出

```rust
use god_graph::export::svg::{to_svg, SvgOptions};
use god_graph::transformer::optimization::ModelSwitch;

// 加载模型
let graph = ModelSwitch::load_from_safetensors("model.safetensors")?;

// 导出为 SVG
let options = SvgOptions::new()
    .with_size(1200, 800)
    .with_node_radius(25.0)
    .with_layout(god_graph::export::svg::LayoutAlgorithm::Hierarchical);

let svg = to_svg(&graph, options);
std::fs::write("model.svg", svg)?;
```

---

## 📊 可视化示例

### 示例 1:完整 Transformer 架构

```bash
cargo run --example export_model_dot --features transformer
dot -Tsvg model.dot -o transformer.svg
```

**输出示例**:

```
digraph Transformer {
    rankdir=TB;
    splines=ortho;
    
    subgraph cluster_embedding {
        label="Embedding";
        fillcolor=lightyellow;
        n0 [label="Embedding\n32000x512" fillcolor="#FFC107"];
    }
    
    subgraph cluster_attention {
        label="Attention Layers";
        fillcolor=lightgreen;
        n1 [label="Attention\n8 heads\n512 dim" fillcolor="#4CAF50"];
        n4 [label="Attention\n8 heads\n512 dim" fillcolor="#4CAF50"];
    }
    
    subgraph cluster_mlp {
        label="MLP Layers";
        fillcolor=lightcoral;
        n3 [label="MLP\n512\nSiLU" fillcolor="#f44336"];
    }
    
    n0 -> n1;
    n1 -> n2;
    n2 -> n3;
}
```

---

### 示例 2:单层细节可视化

```rust
use god_graph::graph::traits::GraphQuery;
use god_graph::export::dot::{to_dot_with_options, DotOptions};

// 提取单层子图
let layer_graph = extract_layer(&graph, layer_idx=5);

// 自定义样式
let options = DotOptions::new()
    .with_name("Layer_5")
    .with_graph_attribute("rankdir", "LR")  // 从左到右
    .with_node_attribute("shape", "ellipse")
    .hide_edge_labels();

let dot = to_dot_with_options(&layer_graph, &options);
std::fs::write("layer_5.dot", dot)?;
```

---

### 示例 3:注意力头可视化

```rust
// 导出注意力权重矩阵
use god_graph::tensor::DenseTensor;

for edge in graph.edges() {
    let weight = edge.data();
    if weight.name.contains("attn") {
        // 导出为 CSV(可用 Python matplotlib 可视化)
        let csv = tensor_to_csv(&weight.data);
        std::fs::write(format!("{}.csv", weight.name), csv)?;
    }
}
```

**Python 可视化**:
```python
import matplotlib.pyplot as plt
import pandas as pd

# 读取权重
df = pd.read_csv('layer_5_attn_weight.csv', header=None)

# 热力图
plt.figure(figsize=(10, 8))
plt.imshow(df.values, cmap='viridis')
plt.colorbar(label='Weight Value')
plt.title('Attention Weight Matrix - Layer 5')
plt.xlabel('Output Dimension')
plt.ylabel('Input Dimension')
plt.savefig('attention_heatmap.png', dpi=300)
plt.show()
```

---

## 🎨 自定义样式

### 颜色方案

| 模块类型 | 颜色代码 | 说明 |
|---------|---------|------|
| Embedding | `#FFC107` | 琥珀色 |
| Attention | `#4CAF50` | 绿色 |
| MLP | `#f44336` | 红色 |
| Norm | `#2196F3` | 蓝色 |
| Linear | `#9E9E9E` | 灰色 |

### 布局方向

```rust
// 从上到下(适合整体架构)
dot.push_str("    rankdir=TB;\n");

// 从左到右(适合单层细节)
dot.push_str("    rankdir=LR;\n");
```

### 边样式

```rust
// 正交边(直角转弯,更清晰)
dot.push_str("    splines=ortho;\n");

// 曲线边(更美观)
dot.push_str("    splines=curved;\n");

// 直线边(最简单)
dot.push_str("    splines=line;\n");
```

---

## 🔧 高级用法

### 1. 批量导出多个模型

```bash
#!/bin/bash
# export_all_models.sh

for model in models/*.safetensors; do
    name=$(basename "$model" .safetensors)
    echo "Exporting $name..."
    
    cargo run --example export_model_dot --features transformer -- \
        "$model" "output/${name}.dot"
    
    dot -Tsvg "output/${name}.dot" -o "output/${name}.svg"
done
```

---

### 2. 比较优化前后架构

```rust
// 导出优化前
export_transformer_to_dot(&original_graph, "before_optimization.dot")?;

// 应用李群正交化
optimizer.orthogonalize_weights(&mut graph)?;

// 导出优化后
export_transformer_to_dot(&graph, "after_optimization.dot")?;

// 并排比较(使用 diff 工具)
// dot -Tpng before_optimization.dot -o before.png
// dot -Tpng after_optimization.dot -o after.png
```

---

### 3. 生成报告

```rust
use god_graph::transformer::optimization::ModelSwitch;

let graph = ModelSwitch::load_from_safetensors("model.safetensors")?;

// 生成拓扑报告
let topology_report = ModelSwitch::validate_topology(&graph)?;
println!("拓扑验证报告:");
println!("  节点数:{}", topology_report.node_count);
println!("  边数:{}", topology_report.edge_count);
println!("  连通分量:{}", topology_report.connected_components);
println!("  是否 DAG: {}", topology_report.is_dag);
println!("  问题:{:?}", topology_report.issues);

// 导出可视化
export_transformer_to_dot(&graph, "model_with_report.dot")?;
```

---

## 📱 Web 交互式可视化(即将推出)

### HTML 交互式导出

```rust
use god_graph::export::html::export_transformer_html;

let graph = ModelSwitch::load_from_safetensors("model.safetensors")?;
export_transformer_html(&graph, "model_visualization.html")?;

// 在浏览器中打开
// open model_visualization.html  (macOS)
// xdg-open model_visualization.html  (Linux)
```

**功能**:
- ✅ 拖拽缩放
- ✅ 节点搜索
- ✅ 点击查看详情
- ✅ 权重分布直方图
- ✅ 注意力热力图

---

## 🐛 常见问题

### Q1: Graphviz 安装失败

**问题**:`dot: command not found`

**解决**:
```bash
# Ubuntu/Debian
sudo apt-get install graphviz graphviz-dev

# macOS
brew install graphviz

# Windows
# 下载 https://graphviz.org/download/#windows
# 安装后添加到 PATH
```

---

### Q2: 导出的图太大/太小

**问题**:节点太多看不清

**解决**:
```rust
// 方法 1:只导出部分层
let subgraph = extract_layers(&graph, &[0, 1, 2]);  // 只导出前 3 层
export_transformer_to_dot(&subgraph, "first_3_layers.dot")?;

// 方法 2:增大画布
dot.push_str("    size=\"20,30\";\n");  // 20x30 英寸
dot.push_str("    ratio=expand;\n");
```

---

### Q3: 中文标签乱码

**问题**:中文显示为方框

**解决**:
```rust
// 指定中文字体
dot.push_str("    node [fontname=\"Microsoft YaHei\"];\n");
dot.push_str("    edge [fontname=\"Microsoft YaHei\"];\n");

// 或使用英文标签
let label = format!("Attention\\n{} heads", num_heads);  // 避免中文
```

---

### Q4: 边交叉太多,看不清

**问题**:图太混乱

**解决**:
```rust
// 使用正交边
dot.push_str("    splines=ortho;\n");

// 启用层级布局
dot.push_str("    rankdir=TB;\n");

// 添加边的权重(粗边表示重要连接)
dot.push_str(&format!("    n{} -> n{} [penwidth={}];\n", 
    source, target, weight));
```

---

## 📚 相关资源

- [Graphviz 官方文档]https://graphviz.org/documentation/
- [DOT 语言指南]https://graphviz.org/doc/info/lang.html
- [Cytoscape.js 交互式可视化]https://js.cytoscape.org/
- [God-Graph DifferentiableGraph 教程]differentiable_graph.md

---

## 🎯 下一步

1. **尝试示例**`cargo run --example export_model_dot`
2. **可视化你的模型**:导出为 SVG 查看
3. **探索 DifferentiableGraph**:用梯度优化架构

---

**最后更新**: 2026-03-29