文章目录
- 本周计划
- GPT-2 BPE匹配规则
本周计划
- 学透BPE算法,动手构建,优化2部分 1 更新算法 2 多线程训练
为什么去空格
优化更新函数
优化多线程计算
GPT-2 官方实现 https://github.com/openai/gpt-2/blob/master/src/encoder.py - 收尾happy_llm,小批量训练tokenizer
- 小参数预训练happy_llm 目标:能够对话 看到loss收敛
- 开始minimind的学习,快过预训练,尽快开始SFT和强化学习。
- 周三:今天必须开始看李宏毅强化学习课!
GPT-2 BPE匹配规则
importregexasre GPT2_PATTERN=r"""'(?:[sdmt]|ll|ve|re)| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""compiled_pattern=re.compile(GPT2_PATTERN,re.UNICODE)pretokenized=[]forsegmentindoc_segments:ifnotsegment.strip():continuepre_tokens=compiled_pattern.findall(segment)print(f"pretokens:{pre_tokens}")# breakforptinpre_tokens:ifnotpt.strip():continue# byte_seq = tuple(pt) #.encode("utf-8"))# pt = pt.strip()byte_seq=tuple(char.encode("utf-8")forcharinpt)# 此步直接将单词拆分成数字序列pretokenized.append(byte_seq)returnpretokenized当我打印出来pre_token才发现,每个单词前面都有一个空格 ater’, ’ to’, ’ make’, ’ it’, ’ nice’, ’ and’, ’ bubbly’, ‘.’, ’ He’, ’ relaxed’, ’ again’, ’ and’, ’ felt’, ’ all’, ’ the’, ’ worries’, ’ wash’, ’ away’, ‘.’, ‘\n’, ‘The’, ’ king’, ’ was’, ’ so’, ’ happy’, ’ that’, ’ he’, ’ had’, ’ been’, ’ able’, ’ to’, ’ clean’, ’ up’, ’ the’, ’ mess’, ’ he’, ’ had’, ’ made’, ’ and’, ’ enjoy’, ’ a’, ’ nice’, ’ soak’, ‘.’, ’ He’, ’ dried’, ’ off’, ’ and’, ’ wrapped’, ’ himself’, ’ up’, ’ in’, ’ a’, ’ big’, ’ towel’, ‘.’, ’ Then’, ‘,’, ’ the’, ’ king’, ’ wen这是因为我使用了GPT-2的pattern进行切分的对吗?GPT-2如此做的更深层次原因是什么呢
二、核心优化方向 2:多线程 / 多进程并行(针对独立序列)
你的判断是对的:每个预分词后的序列(如单个单词 / 短语)的合并逻辑完全独立,因此可以并行处理。但注意:
❌ 合并规则(选最高频best_pair)是全局的,必须单线程确定;
✅ 合并best_pair到各序列的过程(_merge_byte_pair)、统计各序列内的字节对频次,均可并行。