mozihe commited on
Commit
f8d9262
·
verified ·
1 Parent(s): e70cc91

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +148 -3
README.md CHANGED
@@ -1,3 +1,148 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - zh
5
+ tags:
6
+ - moe
7
+ - chinese
8
+ - vlm
9
+ - from-scratch
10
+ - lora
11
+ pipeline_tag: text-generation
12
+ ---
13
+
14
+ # MoXin(墨心)
15
+
16
+ 从零实现的中文大语言模型 & 多模态视觉语言模型。
17
+
18
+ - GitHub: [https://github.com/mozihe/moxin](https://github.com/mozihe/moxin)
19
+
20
+ ## 模型概述
21
+
22
+ MoXin 是一个完全从零构建的中文语言模型项目,涵盖 Tokenizer 训练、预训练、多阶段 SFT、LoRA 微调、多模态 VLM 扩展的完整流程。所有组件基于 PyTorch 原生实现,不依赖第三方训练框架。
23
+
24
+ ## 模型架构
25
+
26
+ - **类型**: Decoder-only Transformer + Mixture of Experts (MoE)
27
+ - **总参数量**: ~270M
28
+ - **隐藏维度**: 768
29
+ - **层数**: 2 Dense + 10 MoE = 12 层
30
+ - **注意力**: GQA(8 Q heads / 2 KV heads)
31
+ - **FFN**: SwiGLU,隐藏维度 2048
32
+ - **MoE**: 4 专家,top-2 激活,1 共享专家,负载均衡辅助损失
33
+ - **位置编码**: RoPE(θ=1e6)
34
+ - **归一化**: RMSNorm
35
+ - **词表**: 9600(BPE,自训练)
36
+ - **最大序列长度**: 1024
37
+
38
+ ### VLM 扩展
39
+
40
+ - **视觉编码器**: CLIP ViT-B/16(冻结,~86M)
41
+ - **投影层**: VisionProj(Linear → GELU → Linear,768 → 768)
42
+ - **图像表示**: 196 个 patch token 注入文本序列
43
+
44
+ ## 权重文件
45
+
46
+ | 文件 | 说明 |
47
+ |---|---|
48
+ | `pretrain.pth` | 文本预训练权重 |
49
+ | `sft01.pth` | SFT 第一阶段(max_seq_len=512) |
50
+ | `sft02.pth` | SFT 第二阶段(max_seq_len=1024) |
51
+ | `moxin-lora.pt` | LoRA 微调权重(基于 sft02) |
52
+ | `pretrain_vlm.pth` | VLM 预训练权重 |
53
+ | `sft_vlm.pth` | VLM SFT 权重 |
54
+
55
+ ## 训练流程
56
+
57
+ ```
58
+ Tokenizer 训练
59
+
60
+ 文本预训练 → pretrain.pth
61
+
62
+ SFT-1 (seq_len=512) → sft01.pth
63
+
64
+ SFT-2 (seq_len=1024) → sft02.pth
65
+ ↓ ↓
66
+ LoRA 微调 → moxin-lora.pt VLM 预训练 → pretrain_vlm.pth
67
+
68
+ VLM SFT → sft_vlm.pth
69
+ ```
70
+
71
+ ## 快速使用
72
+
73
+ ```python
74
+ import torch
75
+ from transformers import AutoTokenizer
76
+
77
+ # 需要先 clone 项目代码
78
+ # git clone https://github.com/mozihe/moxin
79
+ # cd moxin
80
+
81
+ from config.moxin_config import MoXinConfig
82
+ from model.moxin_model import MoXinModel
83
+
84
+ config = MoXinConfig()
85
+ tokenizer = AutoTokenizer.from_pretrained("tokenizer/moxin_tokenizer")
86
+
87
+ model = MoXinModel(config)
88
+ state_dict = torch.load("out/sft02.pth", map_location="cpu")
89
+ model.load_state_dict(state_dict, strict=False)
90
+ model.eval()
91
+
92
+ messages = [{"role": "user", "content": "你好,请介绍一下你自己。"}]
93
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
94
+ input_ids = torch.tensor(tokenizer(prompt)["input_ids"]).unsqueeze(0)
95
+
96
+ output = model.generate(
97
+ input_ids,
98
+ eos_token_id=tokenizer.eos_token_id,
99
+ max_new_tokens=512,
100
+ temperature=0.85,
101
+ top_p=0.85,
102
+ )
103
+ print(tokenizer.decode(output[0], skip_special_tokens=True))
104
+ ```
105
+
106
+ ## 评测结果
107
+
108
+ ### 中文语言能力
109
+
110
+ | 指标 | 值 |
111
+ |---|---|
112
+ | Perplexity | 147.36 |
113
+ | Distinct-1 | 0.492 |
114
+ | Distinct-2 | 0.864 |
115
+ | Distinct-3 | 0.943 |
116
+ | Repetition | 0.009 |
117
+ | Empty Rate | 0.0% |
118
+
119
+ ### C-Eval(Zero-shot)
120
+
121
+ | 类别 | 准确率 |
122
+ |---|---|
123
+ | STEM | 24.4% |
124
+ | Social Science | 25.0% |
125
+ | Humanities | 24.8% |
126
+ | Other | 22.0% |
127
+ | **Overall** | **24.2%** |
128
+
129
+ ### VLM 图文理解
130
+
131
+ | 指标 | 值 |
132
+ |---|---|
133
+ | CharOverlap | 0.410 |
134
+ | BLEU-1 | 0.305 |
135
+ | Distinct-2 | 0.714 |
136
+ | Repetition | 0.036 |
137
+ | Empty Rate | 0.0% |
138
+
139
+ ## 致谢
140
+
141
+ - [MiniMind](https://github.com/jingyaogong/minimind) — 项目灵感来源
142
+ - [OpenAI CLIP](https://github.com/openai/CLIP) — 视觉编码器
143
+ - [HuggingFace Transformers](https://github.com/huggingface/transformers) — Tokenizer 与模型基类
144
+ - [C-Eval](https://cevalbenchmark.com/) — 中文评测基准
145
+
146
+ ## License
147
+
148
+ MIT