🧠 两小时从 0 训出你的 64M 大模型。Train your own 64M LLM from scratch in 2 hours.
[update] trim merged pr leftovers
jingyaogong233d00fa068e
diff --git a/scripts/serve_openai_api.py b/scripts/serve_openai_api.pyindex 077b075..e50785f 100644--- a/scripts/serve_openai_api.py+++ b/scripts/serve_openai_api.py@@ -39,7 +39,7 @@ def init_model(args):))model.load_state_dict(torch.load(ckp, map_location=device), strict=True)if args.lora_weight != 'None':- apply_lora(model)+ apply_lora(model, rank=args.lora_rank)load_lora(model, f'../{args.save_dir}/{args.lora_weight}_{args.hidden_size}{moe_suffix}.pth')else:model = AutoModelForCausalLM.from_pretrained(args.load_from, trust_remote_code=True)@@ -172,7 +172,6 @@ def generate_stream_response(messages, temperature, top_p, max_tokens, tools=Nonfield = 'content' if thinking_ended else 'reasoning_content'pos = pending.find(marker)if pos < 0:- # 保留可能跨 chunk 的标记前缀,其余文本立即发送。keep = next((n for n in range(len(marker) - 1, 0, -1)if pending.endswith(marker[:n])), 0)end = len(pending) - keep@@ -264,6 +263,7 @@ if __name__ == "__main__":parser.add_argument('--save_dir', default='out', type=str, help="模型权重目录")parser.add_argument('--weight', default='full_sft', type=str, help="权重名称前缀(pretrain, full_sft, dpo, reason, ppo_actor, grpo, spo)")parser.add_argument('--lora_weight', default='None', type=str, help="LoRA权重名称(None表示不使用,可选:lora_identity, lora_medical)")+ parser.add_argument('--lora_rank', default=16, type=int, help="LoRA rank,需与训练时一致(默认16)")parser.add_argument('--hidden_size', default=768, type=int, help="隐藏层维度")parser.add_argument('--num_hidden_layers', default=8, type=int, help="隐藏层数量")parser.add_argument('--max_seq_len', default=8192, type=int, help="最大序列长度")diff --git a/tests/test_stream_tool_calls.py b/tests/test_stream_tool_calls.pydeleted file mode 100644index e51332f..0000000--- a/tests/test_stream_tool_calls.py+++ /dev/null@@ -1,147 +0,0 @@-"""python -m unittest discover -s tests -p test_stream_tool_calls.py -v"""-import json-import unittest-from pathlib import Path-from unittest.mock import patch--import torch-from fastapi.testclient import TestClient-from openai import OpenAI-from transformers import AutoTokenizer--from model.model_minimind import MiniMindConfig, MiniMindForCausalLM-from scripts import serve_openai_api as server---TOOL = '<tool_call>{"name":"weather","arguments":{"city":"杭州"}}</tool_call>'-TOOLS = [{'type': 'function', 'function': {'name': 'weather', 'parameters': {- 'type': 'object', 'properties': {'city': {'type': 'string'}}, 'required': ['city']}}}]---class ScriptedModel:- def __init__(self, chunks, tokenize=False):- self.chunks, self.tokenize = chunks, tokenize-- def generate(self, input_ids, streamer, **kwargs):- if self.tokenize:- streamer.put(input_ids.cpu())- for token in streamer.tokenizer.encode(''.join(self.chunks), add_special_tokens=False):- streamer.put(torch.tensor([token]))- streamer.end()- else:- for chunk in self.chunks:- streamer.on_finalized_text(chunk)- streamer.on_finalized_text('', stream_end=True)---class StreamToolTests(unittest.TestCase):- @classmethod- def setUpClass(cls):- cls.tokenizer = AutoTokenizer.from_pretrained(- Path(__file__).resolve().parents[1] / 'model', local_files_only=True)-- def setUp(self):- globals_patch = patch.multiple(server, tokenizer=self.tokenizer, device='cpu',- model=ScriptedModel([]), create=True)- globals_patch.start()- self.addCleanup(globals_patch.stop)- self.http = TestClient(server.app)- self.addCleanup(self.http.close)- self.request = {'model': 'minimind', 'messages': [{'role': 'user', 'content': 'Hi'}]}-- def frames(self, chunks, thinking=False):- server.model = ScriptedModel(chunks)- response = self.http.post('/v1/chat/completions', json={- **self.request, 'stream': True, 'open_thinking': thinking})- self.assertEqual(response.status_code, 200)- self.assertIn('text/event-stream', response.headers['content-type'])- return [json.loads(line[6:]) for line in response.text.splitlines()- if line.startswith('data: ')]-- def assert_output(self, frames, content, count, reasoning=''):- self.assertFalse(any('error' in frame for frame in frames), frames)- deltas = [frame['choices'][0]['delta'] for frame in frames]- self.assertEqual(''.join(d.get('content', '') for d in deltas), content)- self.assertEqual(''.join(d.get('reasoning_content', '') for d in deltas), reasoning)- calls = [call for d in deltas for call in d.get('tool_calls', [])]- self.assertEqual(len(calls), count)- self.assertEqual([call['index'] for call in calls], list(range(count)))- self.assertEqual(len({call['id'] for call in calls}), count)- for call in calls:- self.assertEqual(call['function']['name'], 'weather')- self.assertEqual(json.loads(call['function']['arguments']), {'city': '杭州'})- self.assertEqual(frames[-1]['choices'][0]['finish_reason'], 'tool_calls' if count else 'stop')-- def test_split_markers_and_multiple_calls(self):- text = 'Before ' + TOOL + ' between ' + TOOL + ' after'- for chunks in ([text], list(text), ['Before <tool_', text[len('Before <tool_'):]]):- with self.subTest(chunks=len(chunks)):- self.assert_output(self.frames(chunks), 'Before between after', 2)-- def test_thinking_boundary_and_tool_text_inside_reasoning(self):- reasoning = 'Consider ' + TOOL + '\n'- text = reasoning + '</think>\n\n' + TOOL + ' answer'- self.assert_output(self.frames(list(text), thinking=True), ' answer', 1, reasoning)-- def test_invalid_and_unfinished_calls_preserve_text(self):- for text in ('plain < text', 'end <tool_', '<tool_call>{"name":',- '<tool_call>bad json</tool_call>', '<tool_call>[]</tool_call>'):- with self.subTest(text=text):- self.assert_output(self.frames(list(text)), text, 0)- invalid = '<tool_call>bad json</tool_call>'- self.assert_output(self.frames([invalid, TOOL]), invalid, 1)-- def test_empty_and_unfinished_thinking(self):- self.assert_output(self.frames([]), '', 0)- self.assert_output(self.frames(['reason </thi'], thinking=True), '', 0, 'reason </thi')-- def test_plain_content_and_tool_call_arrive_before_generation_ends(self):- def generate(input_ids, streamer, **kwargs):- streamer.on_finalized_text('first ')- self.assertTrue(continue_generation.wait(5))- streamer.on_finalized_text(TOOL)- self.assertTrue(finish_generation.wait(5))- streamer.on_finalized_text(' last', stream_end=True)-- from threading import Event- continue_generation, finish_generation = Event(), Event()- server.model.generate = generate- stream = server.generate_stream_response(self.request['messages'], 0.7, 0.92, 128)- self.addCleanup(continue_generation.set)- self.addCleanup(finish_generation.set)- first = json.loads(next(stream))- if first['choices'][0]['delta'].get('role') == 'assistant':- first = json.loads(next(stream))- self.assertEqual(first['choices'][0]['delta']['content'], 'first ')- continue_generation.set()- call = json.loads(next(stream))- self.assertEqual(call['choices'][0]['delta']['tool_calls'][0]['function']['name'], 'weather')- finish_generation.set()- self.assert_output([first, call] + [json.loads(frame) for frame in stream], 'first last', 1)-- def test_real_text_streamer_and_openai_client(self):- server.model = ScriptedModel(['Before ', TOOL, ' after'], tokenize=True)- with OpenAI(api_key='local-test', base_url='http://testserver/v1',- http_client=self.http, max_retries=0) as client:- chunks = list(client.chat.completions.create(**self.request, tools=TOOLS, stream=True))- self.assertEqual(''.join(c.choices[0].delta.content or '' for c in chunks), 'Before after')- calls = [call for c in chunks for call in c.choices[0].delta.tool_calls or []]- self.assertEqual(len(calls), 1)- self.assertEqual(calls[0].index, 0)- self.assertEqual(json.loads(calls[0].function.arguments), {'city': '杭州'})-- def test_tiny_minimind_generation(self):- torch.manual_seed(42)- server.model = MiniMindForCausalLM(MiniMindConfig(- hidden_size=32, num_hidden_layers=1, num_attention_heads=4,- num_key_value_heads=2, intermediate_size=64,- vocab_size=len(self.tokenizer), max_position_embeddings=256)).eval()- response = self.http.post('/v1/chat/completions', json={- **self.request, 'stream': True, 'max_tokens': 4})- frames = [json.loads(line[6:]) for line in response.text.splitlines() if line.startswith('data: ')]- self.assertFalse(any('error' in frame for frame in frames), frames)- self.assertEqual(frames[-1]['choices'][0]['finish_reason'], 'stop')---if __name__ == '__main__':- unittest.main()diff --git a/trainer/train_agent.py b/trainer/train_agent.pyindex 1b4ff9f..c39a60e 100644--- a/trainer/train_agent.py+++ b/trainer/train_agent.py@@ -78,7 +78,7 @@ def parse_tool_calls(text):for m in re.findall(r'<tool_call>(.*?)</tool_call>', text, re.DOTALL):try: call = json.loads(m.strip())except: continue- if isinstance(call, dict): calls.append(call) # 模型偶尔输出非对象JSON,直接丢弃避免下游崩溃+ if isinstance(call, dict): calls.append(call)return callsdef execute_tool(name, args):@@ -133,7 +133,7 @@ def rollout_single(rollout_engine, tokenizer, messages, tools, max_turns=3, max_if isinstance(raw, str):try: raw = json.loads(raw)except: raw = {}- if not isinstance(raw, dict): raw = {} # arguments 结构异常按空参处理+ if not isinstance(raw, dict): raw = {}result = execute_tool(name, raw)result_str = (json.dumps(result, ensure_ascii=False) if result else '{"error": "tool not found"}')[:2048] # 防止天文数字撑爆tokenizermessages.append({"role": "tool", "content": result_str})@@ -227,7 +227,7 @@ def calculate_rewards(prompts, completions, gt_batch, tools_batch, num_gen, rewaif isinstance(raw, str):try: raw = json.loads(raw)except: raw = {}- if not isinstance(raw, dict): raw = {} # arguments 结构异常按空参处理+ if not isinstance(raw, dict): raw = {}check = CHECK_ARGS.get(name)valid_call_count += int(bool(name in valid_names and check and check(raw)))tool_gap = abs(valid_call_count - len(gt)) + max(0, len(tool_calls) - valid_call_count) # tool数差值diff --git a/trainer/trainer_utils.py b/trainer/trainer_utils.pyindex 610c1f4..277325a 100644--- a/trainer/trainer_utils.py+++ b/trainer/trainer_utils.py@@ -71,7 +71,6 @@ def lm_checkpoint(lm_config, weight='full_sft', model=None, optimizer=None, epocif model is not None:raw_model = model.module if isinstance(model, DistributedDataParallel) else modelraw_model = getattr(raw_model, '_orig_mod', raw_model)- # 续训权重保持fp32(fp16会抹掉小于半个ulp的累计更新),推理权重另存fp16;先.cpu()避免显存增长(#559)state_dict = {k: v.cpu() for k, v in raw_model.state_dict().items()}ckp_tmp = ckp_path + '.tmp'torch.save({k: v.half() for k, v in state_dict.items()}, ckp_tmp)