File size: 5,667 Bytes
22cd791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 贡献指南

感谢你对 Fusion 项目的兴趣!这份文档将指导你如何参与贡献。

## 🚀 快速开始

### 1. Fork 和克隆

```bash
# Fork 本仓库(在 GitHub 上点击 Fork 按钮)

# 克隆你的 Fork
git clone https://github.com/YOUR_USERNAME/fusion-llm.git
cd fusion-llm

# 添加上游仓库
git remote add upstream https://github.com/zhan1206/fusion-llm.git
```

### 2. 创建开发环境

```bash
# 创建虚拟环境(推荐)
conda create -n fusion python=3.10
conda activate fusion

# 安装依赖
pip install -r requirements.txt

# 安装开发依赖
pip install -e ".[dev]"
```

### 3. 创建分支

```bash
git checkout -b feature/your-feature-name
```

---

## 🎯 如何贡献

### 报告 Bug

打开 [Issue](https://github.com/zhan1206/fusion-llm/issues),并包含:

- 操作系统版本
- Python 版本
- 依赖版本(`pip freeze`- 重现步骤
- 期望行为 vs 实际行为

### 建议新功能

打开 [Discussion](https://github.com/zhan1206/fusion-llm/discussions) 讨论你的想法。

### 提交代码

1. **遵循代码规范**
   - 使用 Python 3.8+ 语法
   - 遵循 PEP 8 规范
   - 使用类型注解(type hints)
   - 添加文档字符串(docstrings)

2. **编写测试**
   - 为所有新功能编写单元测试
   - 确保测试覆盖率 >80%
   - 运行 `pytest tests/`

3. **提交信息规范**
   
   使用 [Conventional Commits](https://www.conventionalcommits.org/) 格式:

   ```
   feat: 添加 DyQuant 混合精度量化工具
   ^
   |__ 类型:feat|fix|docs|style|refactor|test|chore
   ```

   示例:
   ```
   feat: 实现 SBLA 注意力机制的 CPU 版本
   fix: 修复 Thinking Dial 在 batch 生成时的维度错误
   docs: 更新训练指南,添加 24GB 显卡配置示例
   test: 为 bilingual_filter.py 添加单元测试
   ```

4. **提交 Pull Request**
   - 填写 PR 模板
   - 关联相关的 Issue(`Fixes #123`   - 等待代码审查

---

## 🧪 测试指南

### 运行测试

```bash
# 运行所有测试
pytest tests/ -v

# 运行特定测试文件
pytest tests/test_sbla_attention.py -v

# 检查测试覆盖率
pytest tests/ --cov=models --cov-report=html
```

### 编写测试

测试文件位于 `tests/` 目录,命名格式:`test_*.py`

示例:

```python
import torch
from models.sbla_attention import SlidingBlockLatentAttention

def test_sbla_forward():
    """测试 SBLA 注意力前向传播"""
    batch_size = 2
    seq_len = 1024
    d_model = 512
    
    attn = SlidingBlockLatentAttention(
        d_model=d_model,
        n_heads=8,
        block_size=512,
    )
    
    x = torch.randn(batch_size, seq_len, d_model)
    output = attn(x)
    
    assert output.shape == (batch_size, seq_len, d_model)
```

---

## 📂 项目结构

```
fusion-llm/
├── models/              # 模型架构
│   ├── fusion_model.py      # 完整模型定义
│   ├── sbla_attention.py   # SBLA 注意力
│   └── thinking_dial.py    # Thinking Dial
├── train/               # 训练脚本
│   ├── lora_finetune.py    # LoRA/QLoRA
│   └── full_finetune.py    # 全参数微调
├── data_pipeline/       # 数据处理
│   └── bilingual_filter.py  # 双母语清洗
├── inference/           # 推理部署
│   └── ollama_deploy.py    # Ollama 部署
├── configs/             # 配置文件
│   └── ds_zero3.json       # DeepSpeed 配置
├── tests/               # 单元测试
├── docs/                # 文档
└── scripts/             # 工具脚本
```

---

## 🎨 代码风格

### Python 规范

- 使用 **Black** 格式化:`black models/ train/`
- 使用 **isort** 排序导入:`isort models/ train/`
- 使用 **Flake8** 检查:`flake8 models/ --max-line-length=120`

### 文档规范

- 使用 Google 风格 docstrings:

```python
def my_function(param1: int, param2: str) -> bool:
    """
    函数简短描述
    
    详细描述(可选)
    
    Args:
        param1: 参数1的描述
        param2: 参数2的描述
        
    Returns:
        返回值的描述
        
    Raises:
        ValueError: 参数错误时
    """
    pass
```

---

## 🐛 Debug 技巧

### 调试 SBLA 注意力

```python
# 在 models/sbla_attention.py 添加调试信息
import pdb; pdb.set_trace()

# 或使用 print
print(f"Q shape: {q.shape}")
print(f"Attention weights: {attn_weights.shape}")
```

### 调试训练问题

```bash
# 使用 --debug 模式(如果支持)
python train/lora_finetune.py --debug ...

# 检查显存使用
nvidia-smi -l 1

# 使用 TensorBoard
tensorboard --logdir=runs
```

---

## 📊 性能优化建议

### 模型训练

- 使用 **混合精度训练**`--fp16``--bf16`- 使用 **梯度累积**`--gradient_accumulation_steps 8`- 使用 **DeepSpeed ZeRO**`--deepspeed configs/ds_zero3.json`### 推理优化

- 使用 **量化**`q4_k_m``q8_0`- 使用 **KV 缓存**`use_cache=True`- 使用 **批处理**`batch_size > 1`)

---

## 💬 社区

- **讨论区**: [GitHub Discussions](https://github.com/zhan1206/fusion-llm/discussions)
- **Discord**: (待创建)
- **微信/QQ 群**: (待创建)

---

## 📜 行为准则

请阅读并遵守我们的 [行为准则](CODE_OF_CONDUCT.md)(待创建)。

---

## ❓ 需要帮助?

- 查看 [文档](docs/)
- 搜索已有 [Issues](https://github.com/zhan1206/fusion-llm/issues)
- 在 [Discussions](https://github.com/zhan1206/fusion-llm/discussions) 提问

---

**再次感谢你的贡献!** 🙏

让我们一起打造最强的开源大模型! 🚀