GitHub 热门项目: llama3-from-scratch

摘要

GitHub项目:llama3-from-scratch 仓库地址:https://github。

weight layers llama tokenizer from
2026-05-25 1 阅读 GitHub Trending
GitHub 项目:llama3-from-scratch 仓库地址:https://github.com/naklecha/llama3-from-scratch 星级:15234 | 作者:naklecha 项目描述:llama3一次实现一个矩阵乘法 =================================================== 自述文件内容: # llama3 从头开始实现 在这个文件中,我从头开始实现了 llama3,一次一个张量和矩阵乘法。
另外,我将直接从元为 llama3 提供的模型文件加载张量,您需要在运行此文件之前下载权重。 这是下载权重的官方链接:https://llama.meta.com/llama-downloads/
## 分词器 我不会实现 bpe tokenizer(但 andrej karpathy 有一个非常干净的实现)
链接到他的实现:https://github.com/karpathy/minbpe
````蟒蛇 从 pathlib 导入路径 导入抖音 从 tiktoken.load 导入 load_tiktoken_bpe 进口火炬 导入 json 将 matplotlib.pyplot 导入为 plt tokenizer_path = "Meta-Llama-3-8B/tokenizer.model" 特殊令牌 = [ “<|文本开始|>”, “<|文本结束|>”, “<|reserved_special_token_0|>”, “<|reserved_special_token_1|>”, “<|reserved_special_token_2|>”, “<|reserved_special_token_3|>”, “<|start_header_id|>”, “<|end_header_id|>”, “<|reserved_special_token_4|>”, "<|eot_id|>", # 回合结束 ] + [f"<|reserved_special_token_{i}|>" for i in range(5, 256 - 5)] mergeable_ranks = load_tiktoken_bpe(tokenizer_path) tokenizer = tiktoken.Encoding( 名称=路径(tokenizer_path).名称, pat_str=r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+", mergeable_ranks=mergeable_ranks, special_tokens={token: len(mergeable_ranks) + i for i, enumerate(special_tokens) 中的 token}, ) tokenizer.decode(tokenizer.encode("你好世界!")) ```` “你好世界!” ## 读取模型文件 通常,阅读本文取决于模型类的编写方式以及其中的变量名称。
但由于我们是从头开始实现 llama3,因此我们将一次读取一个张量该文件。
````蟒蛇 模型 = torch.load("Meta-Llama-3-8B/consolidated.00.pth") 打印(json.dumps(列表(model.keys())[:20],缩进= 4)) ```` [ “tok_embeddings.weight”, “层.0.attention.wq.weight”, “层.0.attention.wk.权重”, “层.0.attention.wv.权重”, “层数.0.attention.wo.weight”, “层.0.feed_forward.w1.weight”, “层.0.feed_forward.w3.weight”, “层.0.feed_forward.w2.weight”, “层.0.attention_norm.weight”, “层.0.ffn_norm.weight”, "layers.1.attention.wq.weight", "layers.1.attention.wk.weight", "layers.1.attention.wv.weight", "layers.1.attention.wo.weight", “层.1.feed_forward.w1.weight”, “层.1.feed_forward.w3.weight”, “层.1.feed_forward.w2.weight”, "layers.1.attention_norm.weight", “躺着