File size: 2,648 Bytes
70e674c
 
 
c1a46f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
---

# EasyTranslate: 基于 Transformer 的英中翻译系统

> 研究生 NLP 期末项目 — 使用前沿技术实现高质量英文到中文机器翻译

## 亮点

- **Pre-LayerNorm Transformer**: 从零构建,训练更稳定
- **RoPE 旋转位置编码**: 替代传统正弦编码,更好捕获相对位置信息
- **Flash Attention 2**: 利用 PyTorch 2.0+ 原生支持,训练显著加速
- **NLLB 预训练微调**: 基于 Meta NLLB-200 模型,支持 LoRA 参数高效微调
- **完整评估体系**: SacreBLEU + COMET + chrF++ 多维度评估
- **6 组消融实验**: 系统验证各前沿组件的效果

## 快速开始

```bash
# 安装
conda create -n easytranslate python=3.11 && conda activate easytranslate
pip install torch --index-url https://download.pytorch.org/whl/cu121
pip install -r requirements.txt && pip install -e .

# 从头训练 Transformer
python scripts/train.py --config configs/default_config.yaml

# 微调 NLLB + LoRA
python scripts/train.py --config configs/default_config.yaml model.type=finetune_nllb

# 评估
python scripts/evaluate.py --checkpoint checkpoints/best_model.pt

# 交互翻译
python scripts/translate.py --checkpoint checkpoints/best_model.pt
```

## 项目结构

```
src/easytranslate/
├── data/          # 数据加载、分词、预处理、动态批处理
├── model/         # Transformer (Encoder-Decoder) + 注意力 + 位置编码 + LoRA
├── training/      # 训练循环、优化器、损失函数、分布式训练
├── evaluation/    # BLEU/COMET 指标、Beam Search 解码、评估器
└── utils/         # 配置管理、日志、随机种子
```

## 技术架构

```
英文输入 → BPE Tokenizer → Source Embedding + RoPE
    → Transformer Encoder (6 layers, Flash Attention, Pre-LN)
    → Transformer Decoder (6 layers, Causal Mask, Cross-Attention)
    → Linear Projection → Beam Search → 中文输出
```

## 团队分工

详见 [TASK_ASSIGNMENT.md](TASK_ASSIGNMENT.md)

| 角色 | 负责模块 | 核心任务 |
|------|----------|----------|
| Person A | `data/` | 数据集加载、BPE 分词器、预处理流水线 |
| Person B | `model/` | Transformer 架构、Flash Attention、RoPE、LoRA |
| Person C | `training/` | 训练循环、混合精度、梯度累积、早停 |
| Person D | `evaluation/` | BLEU/COMET 评估、Beam Search、推理服务 |
| Person E | `utils/` + 实验 | 工具模块、消融实验、可视化、报告 |

## 依赖

- Python >= 3.10
- PyTorch >= 2.1 (Flash Attention 支持)
- HuggingFace Transformers / Datasets / Tokenizers
- PEFT (LoRA)
- SacreBLEU, COMET (评估)