chenbhao commited on
Commit
e14a9b6
·
1 Parent(s): 2c87b68

docs: update README and docs for model_type rename, RoPE guard fix, config paths

Browse files

- Rename model_type: 'minimind'→'omni', 'minimind-v'→'omni-v', 'minimind-o'→'omni-o'
- Fix RoPE guard: 'freqs_cos[0,0] == 0' → '!= 1.0' with dtype matching
- Add model_type='omni' class attribute to LMForCausalLM
- Update doc tables to show only current class names
- Update config paths: configs/{model/→lm/,vlm/,vam/}
- Fix eval/convert command examples in docs

README.md CHANGED
@@ -181,8 +181,8 @@ python scripts/eval_llm.py --native --save_dir checkpoint/lm_pretrain_mini \
181
  --weight pretrain --hidden_size 128
182
 
183
  # HuggingFace 格式(config.json + model.safetensors)
184
- python scripts/eval_llm.py --load_from checkpoint/omni \
185
- --tokenizer_path checkpoint/omni
186
 
187
  python scripts/eval_llm.py --load_from checkpoint/lm_full_sft_mini/hf \
188
  --tokenizer_path checkpoint/lm_full_sft_mini/hf
@@ -200,8 +200,13 @@ python scripts/convert_model.py checkpoint/lm_full_sft_mini/full_sft_128.pth \
200
  checkpoint/lm_full_sft_mini/hf \
201
  --tokenizer_path checkpoint/tokenizer
202
 
203
- # 省略 --mode,默认 omni;也可输出 Qwen3 兼容格式
204
- python scripts/convert_model.py checkpoint/omni/full_sft.pth output/dir --mode qwen
 
 
 
 
 
205
  ```
206
 
207
  ## 配置说明
@@ -214,6 +219,8 @@ python scripts/convert_model.py checkpoint/omni/full_sft.pth output/dir --mode q
214
  - 去除 `omni` 中间包,模块直接置于 `src/` 下(`core` / `models` / `trainers` / `dataset` / `utils` …),统一绝对导入;
215
  - 类名统一:`MiniMindModel→LM`、`MiniMindVLM→VLM`、`MiniMindOmni→VAM`、`MiniMindConfig→LMConfig`、
216
  `MiniMindForCausalLM→LMForCausalLM`、`OmniConfig→VAMConfig`;
 
217
  - `trainer_utils.py` 拆分为 `utils/training.py`、`utils/distributed.py`、`utils/checkpoint.py`;
218
  - 训练脚本暴露 `main(default_config=None)`,既可由 `python -m trainers.<mod>` 调用,也可由根 `trainer/*.py` 调用;
219
- - `dataset/` 按数据集类型拆分为独立文件,供多模态扩展。
 
 
181
  --weight pretrain --hidden_size 128
182
 
183
  # HuggingFace 格式(config.json + model.safetensors)
184
+ python scripts/eval_llm.py --load_from checkpoint/omni/native_hf \
185
+ --tokenizer_path checkpoint/omni/native_hf
186
 
187
  python scripts/eval_llm.py --load_from checkpoint/lm_full_sft_mini/hf \
188
  --tokenizer_path checkpoint/lm_full_sft_mini/hf
 
200
  checkpoint/lm_full_sft_mini/hf \
201
  --tokenizer_path checkpoint/tokenizer
202
 
203
+ # 也可输出 Qwen3 兼容格式(发布到 HF Hub)
204
+ python scripts/convert_model.py checkpoint/omni/omni.pth output_dir --mode qwen \
205
+ --tokenizer_path checkpoint/tokenizer
206
+
207
+ # 自动推断 hidden_size / num_hidden_layers、自定义精度
208
+ python scripts/convert_model.py checkpoint/omni/omni.pth output_dir --dtype bfloat16 \
209
+ --hidden_size 768 --num_hidden_layers 8
210
  ```
211
 
212
  ## 配置说明
 
219
  - 去除 `omni` 中间包,模块直接置于 `src/` 下(`core` / `models` / `trainers` / `dataset` / `utils` …),统一绝对导入;
220
  - 类名统一:`MiniMindModel→LM`、`MiniMindVLM→VLM`、`MiniMindOmni→VAM`、`MiniMindConfig→LMConfig`、
221
  `MiniMindForCausalLM→LMForCausalLM`、`OmniConfig→VAMConfig`;
222
+ - `model_type` 重命名:`"minimind"`→`"omni"`、`"minimind-v"`→`"omni-v"`、`"minimind-o"`→`"omni-o"`;
223
  - `trainer_utils.py` 拆分为 `utils/training.py`、`utils/distributed.py`、`utils/checkpoint.py`;
224
  - 训练脚本暴露 `main(default_config=None)`,既可由 `python -m trainers.<mod>` 调用,也可由根 `trainer/*.py` 调用;
225
+ - `dataset/` 按数据集类型拆分为独立文件,供多模态扩展;
226
+ - 配置按模态分目录:`configs/lm/`、`configs/vlm/`、`configs/vam/`。
docs/README.md CHANGED
@@ -62,18 +62,19 @@ docs/ ↔ src/
62
  | --- | --- |
63
  | [面试准备](interview/面试速查.md) | 高频问题 + 一句话答法,按主题组织 |
64
 
65
- ## 命名约定(本仓库)
66
 
67
- 类名已在融合过程中统一重命名,阅读代码/面试时对照:
68
-
69
- | 旧名(上游) | 本仓库 |
70
  | --- | --- |
71
- | `MiniMindModel` | `LM` |
72
- | `MiniMindForCausalLM` | `LMForCausalLM` |
73
- | `MiniMindConfig` | `LMConfig` |
74
- | `MiniMindVLM` | `VLM` |
75
- | `MiniMindOmni` | `VAM` |
76
- | `OmniConfig` | `VAMConfig` |
 
 
 
77
 
78
  包结构(已扁平化,无 `omni` 中间层):
79
 
 
62
  | --- | --- |
63
  | [面试准备](interview/面试速查.md) | 高频问题 + 一句话答法,按主题组织 |
64
 
65
+ ## 类名
66
 
67
+ | 类 | 说明 |
 
 
68
  | --- | --- |
69
+ | `LM` | 纯文本主干 |
70
+ | `LMForCausalLM` | 文本模型(含 lm_head + 生成) |
71
+ | `LMConfig` | 文本模型配置 |
72
+ | `VLM` | 文本 + 视觉多模态 |
73
+ | `VAM` | 全模态(文本 + 视觉 + 语音) |
74
+ | `VAMConfig` | 全模态配置 |
75
+ | `model_type = "omni"` | LM 架构 ID |
76
+ | `model_type = "omni-v"` | VLM 架构 ID |
77
+ | `model_type = "omni-o"` | VAM 架构 ID |
78
 
79
  包结构(已扁平化,无 `omni` 中间层):
80
 
docs/interview/full_sft.md CHANGED
@@ -381,9 +381,14 @@ optimizer = AdamW(model.parameters(), lr=args.learning_rate, weight_decay=0.1)
381
 
382
  ### 本仓库评估脚本(`scripts/eval_llm.py`)
383
 
384
- ```python
385
- # 生成回复 + 即时交互评估
386
- python scripts/eval_llm.py --config configs/lm/lm_full_sft.yaml --checkpoint /path/to/full_sft_512.pth
 
 
 
 
 
387
  ```
388
 
389
  ```
 
381
 
382
  ### 本仓库评估脚本(`scripts/eval_llm.py`)
383
 
384
+ ```bash
385
+ # 原生 torch 格式
386
+ python scripts/eval_llm.py --native --save_dir checkpoint/lm_full_sft_mini \
387
+ --weight full_sft --hidden_size 128
388
+
389
+ # HuggingFace 格式
390
+ python scripts/eval_llm.py --load_from checkpoint/omni/native_hf \
391
+ --tokenizer_path checkpoint/omni/native_hf
392
  ```
393
 
394
  ```
docs/interview/transformer-arch.md CHANGED
@@ -416,21 +416,30 @@ class MOEFeedForward(nn.Module):
416
 
417
  ### 问题
418
 
419
- `torch.compile` 会重新初始化 buffer,导致 RoPE 的 freqs_cos/freqs_sin 变成零。
 
 
 
420
 
421
  ### 本仓库解决方案
422
 
423
  ```python
424
- class LM(nn.Module):
425
- def forward(self, x, start_pos=0, freqs_cos=None, freqs_sin=None, mask=None):
426
- # 延迟 RoPE 初始化
427
- if freqs_cos is not None and freqs_cos[0, 0] == 0:
428
- freqs_cos, freqs_sin = precompute_freqs_cis(...)
 
 
 
 
 
 
429
  ```
430
 
431
  ### 为什么有效?
432
 
433
- 检查 `freqs_cos[0, 0] == 0` 可以检测 buffer 是否被重置,如果是则重新计算。
434
 
435
  ---
436
 
 
416
 
417
  ### 问题
418
 
419
+ 两个场景会导致 RoPE 的 `freqs_cos`/`freqs_sin` 被重置为未初始化内存:
420
+
421
+ 1. **HF `from_pretrained`**:创建模型时在 meta device 上,非持久 buffer 之后用 `torch.empty_like` 分配(含随机/NaN 值)。
422
+ 2. **`torch.compile`**:编译后的重新初始化也会使 buffer 值丢失。
423
 
424
  ### 本仓库解决方案
425
 
426
  ```python
427
+ # register_buffer 使用 persistent=False 避免冗余
428
+ self.register_buffer("freqs_cos", freqs_cos, persistent=False)
429
+ self.register_buffer("freqs_sin", freqs_sin, persistent=False)
430
+
431
+ def forward(self, input_ids, ...):
432
+ h = self.dropout(self.embed_tokens(input_ids))
433
+ # 延迟初始化:检查 cos(0) 是否 != 1.0(未初始化时为 0/NaN/随机值)
434
+ if self.freqs_cos[0, 0] != 1.0:
435
+ freqs_cos, freqs_sin = precompute_freqs_cis(...)
436
+ self.freqs_cos = freqs_cos.to(device=h.device, dtype=h.dtype)
437
+ self.freqs_sin = freqs_sin.to(device=h.device, dtype=h.dtype)
438
  ```
439
 
440
  ### 为什么有效?
441
 
442
+ `cos(0) = 1`,所以初始化的 buffer 第一元素恒为 1.0。检查 `!= 1.0` 能捕捉所有异常值(0、NaN、随机内存),比原来的 `== 0` 更鲁棒(未初始化内存可能是任意值而非精确零)。重算时同时 `.to(dtype=...)` 确保 dtype 与模型一致。
443
 
444
  ---
445
 
docs/models/lm.md CHANGED
@@ -28,7 +28,10 @@ class LM(nn.Module):
28
 
29
  def forward(self, input_ids, ...):
30
  h = self.dropout(self.embed_tokens(input_ids))
31
- position_embeddings = (freqs_cos[start:], freqs_sin[start:]) # 按 past 长度切片
 
 
 
32
  presents = []
33
  for layer, past in zip(self.layers, past_key_values):
34
  h, present = layer(h, position_embeddings, past_key_value=past, ...)
@@ -38,7 +41,7 @@ class LM(nn.Module):
38
  return h, presents, aux_loss
39
  ```
40
 
41
- - `freqs_cos/sin` 用 `persistent=False` buffer 缓存,不进 state_dict;首次前向若 buffer 为 0 会重新计算并搬到设备。
42
  - `aux_loss` 仅在 MoE 时非 0,供 `LMForCausalLM` 加到总损失。
43
 
44
  ## LMForCausalLM(带 head + 损失 + 生成)
@@ -46,6 +49,7 @@ class LM(nn.Module):
46
  ```python
47
  class LMForCausalLM(PreTrainedModel, GenerationMixin):
48
  config_class = LMConfig
 
49
  _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
50
 
51
  def __init__(self, config):
 
28
 
29
  def forward(self, input_ids, ...):
30
  h = self.dropout(self.embed_tokens(input_ids))
31
+ if self.freqs_cos[0, 0] != 1.0: # 未初始化时重算
32
+ freqs_cos, freqs_sin = precompute_freqs_cis(...)
33
+ self.freqs_cos, self.freqs_sin = freqs_cos.to(device=h.device, dtype=h.dtype), ...
34
+ position_embeddings = (self.freqs_cos[start:], self.freqs_sin[start:]) # 按 past 长度切片
35
  presents = []
36
  for layer, past in zip(self.layers, past_key_values):
37
  h, present = layer(h, position_embeddings, past_key_value=past, ...)
 
41
  return h, presents, aux_loss
42
  ```
43
 
44
+ - `freqs_cos/sin` 用 `persistent=False` 注册,不进 state_dict(节省 3 MB 冗余);HF `from_pretrained` 创建模型时在 meta device,非持久 buffer 之后用 `torch.empty_like` 分配(未初始化内存)。forward 内 guard `if self.freqs_cos[0, 0] != 1.0` 捕获异常值并自动重算(cos(0)=1,未初始化的 0/NaN/随机值均会触发)。
45
  - `aux_loss` 仅在 MoE 时非 0,供 `LMForCausalLM` 加到总损失。
46
 
47
  ## LMForCausalLM(带 head + 损失 + 生成)
 
49
  ```python
50
  class LMForCausalLM(PreTrainedModel, GenerationMixin):
51
  config_class = LMConfig
52
+ model_type = "omni"
53
  _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
54
 
55
  def __init__(self, config):
docs/training/config-and-cli.md CHANGED
@@ -22,7 +22,10 @@ def apply_config(parser, default_config=None):
22
 
23
  ```
24
  configs/
25
- ├── model/ # lm / lm_moe / vlm / vlm_moe / vam / vam_moe .yaml
 
 
 
26
  └── tokenizer/ # tokenizer.json / tokenizer_config.json
27
  ```
28
 
 
22
 
23
  ```
24
  configs/
25
+ ├── lm/ # lm_pretrain.yaml / lm_full_sft.yaml / lm_pretrain_mini.yaml / lm_full_sft_mini.yaml / lm_pretrain_moe.yaml / lm_full_sft_moe.yaml
26
+ ├── vlm/ # vlm.yaml / vlm_moe.yaml
27
+ └── vam/ # vam.yaml / vam_moe.yaml
28
+ checkpoint/
29
  └── tokenizer/ # tokenizer.json / tokenizer_config.json
30
  ```
31
 
src/models/lm/model.py CHANGED
@@ -28,9 +28,9 @@ class LM(nn.Module):
28
  past_key_values = past_key_values or [None] * len(self.layers)
29
  start_pos = past_key_values[0][0].shape[1] if past_key_values[0] is not None else 0
30
  hidden_states = self.dropout(self.embed_tokens(input_ids))
31
- if self.freqs_cos[0, 0] == 0:
32
  freqs_cos, freqs_sin = precompute_freqs_cis(dim=self.config.head_dim, end=self.config.max_position_embeddings, rope_base=self.config.rope_theta, rope_scaling=self.config.rope_scaling)
33
- self.freqs_cos, self.freqs_sin = freqs_cos.to(hidden_states.device), freqs_sin.to(hidden_states.device)
34
  position_embeddings = (self.freqs_cos[start_pos:start_pos + seq_length], self.freqs_sin[start_pos:start_pos + seq_length])
35
  presents = []
36
  for layer, past_key_value in zip(self.layers, past_key_values):
@@ -49,6 +49,7 @@ class LM(nn.Module):
49
 
50
  class LMForCausalLM(PreTrainedModel, GenerationMixin):
51
  config_class = LMConfig
 
52
  _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
53
 
54
  def __init__(self, config: LMConfig = None):
 
28
  past_key_values = past_key_values or [None] * len(self.layers)
29
  start_pos = past_key_values[0][0].shape[1] if past_key_values[0] is not None else 0
30
  hidden_states = self.dropout(self.embed_tokens(input_ids))
31
+ if self.freqs_cos[0, 0] != 1.0:
32
  freqs_cos, freqs_sin = precompute_freqs_cis(dim=self.config.head_dim, end=self.config.max_position_embeddings, rope_base=self.config.rope_theta, rope_scaling=self.config.rope_scaling)
33
+ self.freqs_cos, self.freqs_sin = freqs_cos.to(device=hidden_states.device, dtype=hidden_states.dtype), freqs_sin.to(device=hidden_states.device, dtype=hidden_states.dtype)
34
  position_embeddings = (self.freqs_cos[start_pos:start_pos + seq_length], self.freqs_sin[start_pos:start_pos + seq_length])
35
  presents = []
36
  for layer, past_key_value in zip(self.layers, past_key_values):
 
49
 
50
  class LMForCausalLM(PreTrainedModel, GenerationMixin):
51
  config_class = LMConfig
52
+ model_type = "omni"
53
  _tied_weights_keys = {"lm_head.weight": "model.embed_tokens.weight"}
54
 
55
  def __init__(self, config: LMConfig = None):