🧠 两小时从 0 训出你的 64M 大模型。Train your own 64M LLM from scratch in 2 hours.
[update] trim merged pr leftovers
jingyaogong2de2308d7175
diff --git a/model/model_lora.py b/model/model_lora.pyindex 4b54a96..d323266 100644--- a/model/model_lora.py+++ b/model/model_lora.py@@ -7,7 +7,6 @@ class LoRA(nn.Module):def __init__(self, in_features, out_features, rank, lora_alpha=None):super().__init__()self.rank = rank # LoRA的秩(rank),控制低秩矩阵的大小- # 标准 LoRA 的 alpha/r 缩放;lora_alpha=None 表示 alpha=rank(scaling=1),保持历史行为scaling = (rank if lora_alpha is None else lora_alpha) / rankself.register_buffer("scaling", torch.tensor(scaling, dtype=torch.float32))self.A = nn.Linear(in_features, rank, bias=False) # 低秩矩阵A@@ -42,7 +41,6 @@ def load_lora(model, path):for name, module in model.named_modules():if hasattr(module, 'lora'):lora_state = {k.replace(f'{name}.lora.', ''): v for k, v in state_dict.items() if f'{name}.lora.' in k}- # 兼容旧 checkpoint:旧版本没有 scaling buffer(等价于 scaling=1),缺失时保留默认值module.lora.load_state_dict(lora_state, strict=False)@@ -52,11 +50,8 @@ def save_lora(model, path):for name, module in raw_model.named_modules():if hasattr(module, 'lora'):clean_name = name[7:] if name.startswith("module.") else name- lora_state = {}- for k, v in module.lora.state_dict().items():- # 权重保持 half 以维持文件体积;scaling 等 buffer 用 float32,避免精度损失- v = v.cpu().half() if k.endswith('.weight') else v.cpu().float()- lora_state[f'{clean_name}.lora.{k}'] = v+ lora_state = {f'{clean_name}.lora.{k}': v.cpu().half() if k.endswith('.weight') else v.cpu().float()+ for k, v in module.lora.state_dict().items()}state_dict.update(lora_state)torch.save(state_dict, path)@@ -69,7 +64,6 @@ def merge_lora(model, lora_path, save_path):if isinstance(module, nn.Linear) and '.lora.' not in name:state_dict[f'{name}.weight'] = module.weight.data.clone().cpu().half()if hasattr(module, 'lora'):- delta = module.lora.B.weight.data @ module.lora.A.weight.data- delta = (delta * module.lora.scaling.to(delta.device)).cpu().half()- state_dict[f'{name}.weight'] += delta+ delta = (module.lora.B.weight.data @ module.lora.A.weight.data) * module.lora.scaling+ state_dict[f'{name}.weight'] += delta.cpu().half()torch.save(state_dict, save_path)diff --git a/scripts/convert_model.py b/scripts/convert_model.pyindex a4162a3..9771ca6 100644--- a/scripts/convert_model.py+++ b/scripts/convert_model.py@@ -102,12 +102,12 @@ def convert_transformers2torch(transformers_path, torch_path):print(f"模型已保存为 PyTorch 格式: {torch_path}")-def convert_merge_base_lora(base_torch_path, lora_path, merged_torch_path):+def convert_merge_base_lora(base_torch_path, lora_path, merged_torch_path, lora_rank=16):device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')lm_model = MiniMindForCausalLM(lm_config).to(device)state_dict = torch.load(base_torch_path, map_location=device)lm_model.load_state_dict(state_dict, strict=False)- apply_lora(lm_model)+ apply_lora(lm_model, rank=lora_rank)merge_lora(lm_model, lora_path, merged_torch_path)print(f"LoRA 已合并并保存为基模结构 PyTorch 格式: {merged_torch_path}")diff --git a/trainer/train_agent.py b/trainer/train_agent.pyindex b92075b..cb1128d 100644--- a/trainer/train_agent.py+++ b/trainer/train_agent.py@@ -183,7 +183,6 @@ def rollout_batch(rollout_engine, tokenizer, messages_batch, tools_batch, num_ge# ======== Reward 计算 ========def validate_gt_in_text(text, gt_list):- # 数值型 gt 只按数值比对(子串会让 gt=9 命中 "19"、gt=100 命中 "1000");数字边界只看 ASCII 字母数字,中文紧挨数字("答案是59。")也能取到text, text_num = str(text), str(text).replace(',', '')nums = [float(x) for x in re.findall(r'(?<![A-Za-z0-9_.])[-+]?\d+(?:\.\d+)?(?![A-Za-z0-9_]|\.\d)', text_num)]return {g for g in gt_list if (s := str(g).strip()) and (any(abs(float(s.replace(',', '')) - n) < 1e-6 for n in nums) if re.fullmatch(r'[-+]?\d+(?:\.\d+)?', s.replace(',', '')) else s.lower() in text.lower())}