从零实现llama3(学习)

news/2024/10/16 7:23:31 标签: 学习

中文学习链接:https://github.com/Czi24/Awesome-MLLM-LLM-Colab
官网链接:https://github.com/naklecha/llama3-from-scratch

从零实现llama3

在这个文件中,我从零开始实现了llama3,一次一个张量和矩阵乘法。


此外,我将直接从Meta提供的llama3模型文件中加载张量,在运行此文件之前,您需要下载模型的权重。
以下是下载权重的官方链接:https://llama.meta.com/llama-downloads/

分词器

我不会实现一个bpe分词器(但Andrej Karpathy有一个非常简洁的实现)


链接到他的实现:https://github.com/karpathy/minbpe

from pathlib import Path
import tiktoken
from tiktoken.load import load_tiktoken_bpe
import torch
import json
import matplotlib.pyplot as plt

tokenizer_path = "Meta-Llama-3-8B/tokenizer.model"
special_tokens = [
            "<|begin_of_text|>",
            "<|end_of_text|>",
            "<|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|>",  # end of turn
        ] + [f"<|reserved_special_token_{i}|>" for i in range(5, 256 - 5)]
mergeable_ranks = load_tiktoken_bpe(tokenizer_path)
tokenizer = tiktoken.Encoding(
    name=Path(tokenizer_path).name,
    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, token in enumerate(special_tokens)},
)

tokenizer.decode(tokenizer.encode("hello world!"))
'hello world!'

读取模型文件

通常,读取模型文件取决于模型类的编写方式以及内部的变量名。


但由于我们从零开始实现llama3,所以我们将一次读取一个张量。

model = torch.load("Meta-Llama-3-8B/consolidated.00.pth")
print(json.dumps(list(model.keys())[:20], indent=4))
[
    "tok_embeddings.weight",
    "layers.0.attention.wq.weight",
    "layers.0.attention.wk.weight",
    "layers.0.attention.wv.weight",
    "layers.0.attention.wo.weight",
    "layers.0.feed_forward.w1.weight",
    "layers.0.feed_forward.w3.weight",
    "layers.0.feed_forward.w2.weight",
    "layers.0.attention_norm.weight",
    "layers.0.ffn_norm.weight",
    "layers.1.attention.wq.weight",
    "layers.1.attention.wk.weight",
    "layers.1.attention.wv.weight",
    "layers.1.attention.wo.weight",
    "layers.1.feed_forward.w1.weight",
    "layers.1.feed_forward.w3.weight",
    "layers.1.feed_forward.w2.weight",
    "layers.1.attention_norm.weight",
    "layers.1.ffn_norm.weight",
    "layers.2.attention.wq.weight"
]
with open("Meta-Llama-3-8B/params.json", "r") as f:
    config = json.load(f)
config
{'dim': 4096,
 'n_layers': 32,
 'n_heads': 32,
 'n_kv_heads': 8,
 'vocab_size': 128256,
 'multiple_of': 1024,
 'ffn_dim_multiplier': 1.3,
 'norm_eps': 1e-05,
 'rope_theta': 500000.0}

我们使用此配置来推断有关模型的细节,例如

  1. 该模型有32个transformer层
  2. 每个多头注意力块有32个头
  3. 词汇量大小等等
dim = config["dim"]
n_layers = config["n_layers"]
n_heads = config["n_heads"]
n_kv_heads = config["n_kv_heads"]
vocab_size = config["vocab_size"]
multiple_of = config["multiple_of"]
ffn_dim_multiplier = config["ffn_dim_multiplier"]
norm_eps = config["norm_eps"]
rope_theta = torch.tensor(config["rope_theta"])

将文本转换为标记

这里我们使用tiktoken(我认为是OpenAI的一个库)作为分词器

prompt = "the answer to the ultimate question of life, the universe, and everything is "
tokens = [128000] + tokenizer.encode(prompt)
print(tokens)
tokens = torch.tensor(tokens)
prompt_split_as_tokens = [tokenizer.decode([token.item()]) for token in tokens]
print(prompt_split_as_tokens)
[128000, 1820, 4320, 311, 279, 17139, 3488, 315, 2324, 11, 279, 15861, 11, 323, 4395, 374, 220]
['<|begin_of_text|>', 'the', ' answer', ' to', ' the', ' ultimate', ' question', ' of', ' life', ',', ' the', ' universe', ',', ' and', ' everything', ' is', ' ']

将标记转换为它们的嵌入

抱歉,这是代码库中唯一使用内置神经网络模块的部分。


无论如何,我们的 [17x1] 标记现在变为 [17x4096],即17个长度为4096的嵌入(每个标记一个嵌入)。




注意:跟踪这些形状,会让理解所有内容变得更容易。

embedding_layer = torch.nn.Embedding(vocab_size, dim)
embedding_layer.weight.data.copy_(model["tok_embeddings.weight"])
token_embeddings_unnormalized = embedding_layer(tokens).to(torch.bfloat16)
token_embeddings_unnormalized.shape
torch.Size([17, 4096])

然后我们使用rms归一化对嵌入进行标准化

请注意,经过此步骤后,形状不会改变,值只是被归一化了。


需要记住的是,我们需要一个norm_eps(来自配置),因为我们不希望不小心将rms设置为0并导致除以0。


这里是公式:

# def rms_norm(tensor, norm_weights):
#     rms = (tensor.pow(2).mean(-1, keepdim=True) + norm_eps)**0.5
#     return tensor * (norm_weights / rms)
def rms_norm(tensor, norm_weights):
    return (tensor * torch.rsqrt(tensor.pow(2).mean(-1, keepdim=True) + norm_eps)) * norm_weights

构建Transformer的第一层

归一化

你会看到我从模型字典中访问 layer.0(这是第一层)。


总之,经过归一化处理后,我们的形状仍然是 [17x4096],与嵌入相同,但已归一化。

token_embeddings = rms_norm(token_embeddings_unnormalized, model["layers.0.attention_norm.weight"])
token_embeddings.shape
torch.Size([17, 4096])

从零实现注意力机制

让我们加载Transformer第一层的注意力头


> 当我们从模型中加载query、key、value和output向量时,我们注意到它们的形状分别是[4096x4096]、[1024x4096]、[1024x4096]、[4096x4096]


> 乍一看这很奇怪,因为理想情况下我们希望为每个head单独获取q、k、v和o


> 代码的作者将它们捆绑在一起是因为这样可以更容易并行化注意力头的乘法运算。


> 我现在要将它们解开…

print(
    model["layers.0.attention.wq.weight"].shape,
    model["layers.0.attention.wk.weight"].shape,
    model["layers.0.attention.wv.weight"].shape,
    model["layers.0.attention.wo.weight"].shape
)
torch.Size([4096, 4096]) torch.Size([1024, 4096]) torch.Size([1024, 4096]) torch.Size([4096, 4096])

解开查询

在接下来的部分中,我们将解开多个注意力头中的查询,结果的形状是 [32x128x4096]。



这里,32 是llama3中的注意力头数,128 是查询向量的大小,4096 是标记嵌入的大小。

q_layer0 = model["layers.0.attention.wq.weight"]
head_dim = q_layer0.shape[0] // n_heads
q_layer0 = q_layer0.view(n_heads, head_dim, dim)
q_layer0.shape
torch.Size([32, 128, 4096])

我将实现第一层的第一个头

这里我首先访问第一层第一个头的查询权重矩阵,这个查询权重矩阵的大小是 [128x4096]。

q_layer0_head0 = q_layer0[0]
q_layer0_head0.shape
torch.Size([128, 4096])

现在我们将查询权重与标记嵌入相乘,得到该标记的查询

这里你可以看到结果的形状是 [17x128],这是因为我们有17个标记,并且对于每个标记都有一个长度为128的查询。

q_per_token = torch.matmul(token_embeddings, q_layer0_head0.T)
q_per_token.shape
torch.Size([17, 128])

位置编码

我们现在已经为提示中的每个标记生成了查询向量,但如果你仔细想想——单独的查询向量并不了解它在提示中的位置。



示例查询:“the answer to the ultimate question of life, the universe, and everything is ”



在我们的提示中,“the” 出现了三次,我们需要基于它们在查询中的位置,让这三个 “the” 标记的查询向量有不同的查询向量(每个大小为 [1x128])。我们使用RoPE(旋转位置嵌入)来执行这些旋转。


RoPE

观看这个视频(我也是通过这个视频理解的)来了解其中的数学原理。
https://www.youtube.com/watch?v=o29P0Kpobz0&t=530s

q_per_token_split_into_pairs = q_per_token.float().view(q_per_token.shape[0], -1, 2)
q_per_token_split_into_pairs.shape
torch.Size([17, 64, 2])

在上一步中,我们将查询向量分成了对,并对每对应用一个旋转角度偏移!



现在我们得到一个大小为 [17x64x2] 的向量,这是128长度的查询向量被分成了64对,每个标记对应提示中的一个查询!这64对中的每一对都将通过 m*(theta) 旋转,其中 m 是我们为其旋转查询的标记位置!

使用复数的点积来旋转向量

zero_to_one_split_into_64_parts = torch.tensor(range(64))/64
zero_to_one_split_into_64_parts
tensor([0.0000, 0.0156, 0.0312, 0.0469, 0.0625, 0.0781, 0.0938, 0.1094, 0.1250,
        0.1406, 0.1562, 0.1719, 0.1875, 0.2031, 0.2188, 0.2344, 0.2500, 0.2656,
        0.2812, 0.2969, 0.3125, 0.3281, 0.3438, 0.3594, 0.3750, 0.3906, 0.4062,
        0.4219, 0.4375, 0.4531, 0.4688, 0.4844, 0.5000, 0.5156, 0.5312, 0.5469,
        0.5625, 0.5781, 0.5938, 0.6094, 0.6250, 0.6406, 0.6562, 0.6719, 0.6875,
        0.7031, 0.7188, 0.7344, 0.7500, 0.7656, 0.7812, 0.7969, 0.8125, 0.8281,
        0.8438, 0.8594, 0.8750, 0.8906, 0.9062, 0.9219, 0.9375, 0.9531, 0.9688,
        0.9844])
freqs = 1.0 / (rope_theta ** zero_to_one_split_into_64_parts)
freqs
tensor([1.0000e+00, 8.1462e-01, 6.6360e-01, 5.4058e-01, 4.4037e-01, 3.5873e-01,
        2.9223e-01, 2.3805e-01, 1.9392e-01, 1.5797e-01, 1.2869e-01, 1.0483e-01,
        8.5397e-02, 6.9566e-02, 5.6670e-02, 4.6164e-02, 3.7606e-02, 3.0635e-02,
        2.4955e-02, 2.0329e-02, 1.6560e-02, 1.3490e-02, 1.0990e-02, 8.9523e-03,
        7.2927e-03, 5.9407e-03, 4.8394e-03, 3.9423e-03, 3.2114e-03, 2.6161e-03,
        2.1311e-03, 1.7360e-03, 1.4142e-03, 1.1520e-03, 9.3847e-04, 7.6450e-04,
        6.2277e-04, 5.0732e-04, 4.1327e-04, 3.3666e-04, 2.7425e-04, 2.2341e-04,
        1.8199e-04, 1.4825e-04, 1.2077e-04, 9.8381e-05, 8.0143e-05, 6.5286e-05,
        5.3183e-05, 4.3324e-05, 3.5292e-05, 2.8750e-05, 2.3420e-05, 1.9078e-05,
        1.5542e-05, 1.2660e-05, 1.0313e-05, 8.4015e-06, 6.8440e-06, 5.5752e-06,
        4.5417e-06, 3.6997e-06, 3.0139e-06, 2.4551e-06])
freqs_for_each_token = torch.outer(torch.arange(17), freqs)
freqs_cis = torch.polar(torch.ones_like(freqs_for_each_token), freqs_for_each_token)
freqs_cis.shape

# viewing tjhe third row of freqs_cis
value = freqs_cis[3]
plt.figure()
for i, element in enumerate(value[:17]):
    plt.plot([0, element.real], [0, element.imag], color='blue', linewidth=1, label=f"Index: {i}")
    plt.annotate(f"{i}", xy=(element.real, element.imag), color='red')
plt.xlabel('Real')
plt.ylabel('Imaginary')
plt.title('Plot of one row of freqs_cis')
plt.show()

现在我们为每个标记的查询元素得到了一个复数(角度变化向量)

我们可以将我们的查询(之前分成的对)转换为复数,然后通过点积运算来根据位置旋转查询。


说实话,这个过程真的很美妙 😃

q_per_token_as_complex_numbers = torch.view_as_complex(q_per_token_split_into_pairs)
q_per_token_as_complex_numbers.shape
torch.Size([17, 64])
q_per_token_as_complex_numbers_rotated = q_per_token_as_complex_numbers * freqs_cis
q_per_token_as_complex_numbers_rotated.shape
torch.Size([17, 64])

得到旋转后的向量之后

我们可以通过将复数再次视为实数来恢复查询向量为对的形式。

q_per_token_split_into_pairs_rotated = torch.view_as_real(q_per_token_as_complex_numbers_rotated)
q_per_token_split_into_pairs_rotated.shape
torch.Size([17, 64, 2])

旋转后的对现在已合并,我们现在得到了一个新的查询向量(旋转后的查询向量),其形状为 [17x128],其中17是标记的数量,128是查询向量的维度。

q_per_token_rotated = q_per_token_split_into_pairs_rotated.view(q_per_token.shape)
q_per_token_rotated.shape
torch.Size([17, 128])

键(几乎和查询相同)

我超级懒,所以我不会详细讲解键的数学部分,你只需要记住以下几点:


> 键生成的键向量的维度也是128


> 键的权重数量只有查询的1/4,这是因为键的权重在一次计算中会在4个头之间共享,从而减少所需的计算量


> 键同样会通过旋转添加位置信息,和查询一样,出于相同的原因

k_layer0 = model["layers.0.attention.wk.weight"]
k_layer0 = k_layer0.view(n_kv_heads, k_layer0.shape[0] // n_kv_heads, dim)
k_layer0.shape
torch.Size([8, 128, 4096])
k_layer0_head0 = k_layer0[0]
k_layer0_head0.shape
torch.Size([128, 4096])
k_per_token = torch.matmul(token_embeddings, k_layer0_head0.T)
k_per_token.shape
torch.Size([17, 128])
k_per_token_split_into_pairs = k_per_token.float().view(k_per_token.shape[0], -1, 2)
k_per_token_split_into_pairs.shape
torch.Size([17, 64, 2])
k_per_token_as_complex_numbers = torch.view_as_complex(k_per_token_split_into_pairs)
k_per_token_as_complex_numbers.shape
torch.Size([17, 64])
k_per_token_split_into_pairs_rotated = torch.view_as_real(k_per_token_as_complex_numbers * freqs_cis)
k_per_token_split_into_pairs_rotated.shape
torch.Size([17, 64, 2])
k_per_token_rotated = k_per_token_split_into_pairs_rotated.view(k_per_token.shape)
k_per_token_rotated.shape
torch.Size([17, 128])

现在我们已经得到了每个标记的查询和键的旋转值

每个查询和键的形状现在都是 [17x128]。

在下一步中,我们将对查询和键矩阵进行相乘

这样做将为我们提供每个标记与其他标记之间的得分映射。


这个得分描述了每个标记的查询与每个标记的键之间的关联程度。
这就是自注意力机制 😃


注意力得分矩阵(qk_per_token)的形状是 [17x17],其中17是提示中的标记数量。

qk_per_token = torch.matmul(q_per_token_rotated, k_per_token_rotated.T)/(head_dim)**0.5
qk_per_token.shape
torch.Size([17, 17])

现在我们需要对查询-键得分进行掩码处理

在llama3的训练过程中,未来标记的qk得分会被掩码。


为什么?因为在训练过程中,我们只能使用过去的标记来预测当前标记。


因此,在推理过程中,我们将未来的标记得分设置为零。

def display_qk_heatmap(qk_per_token):
    _, ax = plt.subplots()
    im = ax.imshow(qk_per_token.to(float).detach(), cmap='viridis')
    ax.set_xticks(range(len(prompt_split_as_tokens)))
    ax.set_yticks(range(len(prompt_split_as_tokens)))
    ax.set_xticklabels(prompt_split_as_tokens)
    ax.set_yticklabels(prompt_split_as_tokens)
    ax.figure.colorbar(im, ax=ax)
    
display_qk_heatmap(qk_per_token)
mask = torch.full((len(tokens), len(tokens)), float("-inf"), device=tokens.device)
mask = torch.triu(mask, diagonal=1)
mask
tensor([[0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf, -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -inf],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
qk_per_token_after_masking = qk_per_token + mask
display_qk_heatmap(qk_per_token_after_masking)
qk_per_token_after_masking_after_softmax = torch.nn.functional.softmax(qk_per_token_after_masking, dim=1).to(torch.bfloat16)
display_qk_heatmap(qk_per_token_after_masking_after_softmax)

值(注意力机制的接近尾声)

这些得分(0-1)用于确定每个标记使用多少值矩阵。


> 和键一样,值的权重也是在每4个注意力头之间共享的(以节省计算)。


> 因此,下面值权重矩阵的形状为 [8x128x4096]。

v_layer0 = model["layers.0.attention.wv.weight"]
v_layer0 = v_layer0.view(n_kv_heads, v_layer0.shape[0] // n_kv_heads, dim)
v_layer0.shape
torch.Size([8, 128, 4096])

第一层第一个头的值权重矩阵如下所示:

v_layer0_head0 = v_layer0[0]
v_layer0_head0.shape
torch.Size([128, 4096])

值向量

我们现在使用值权重来获取每个标记的注意力值,其大小为 [17x128],其中17是提示中的标记数量,128是每个标记的值向量的维度。

v_per_token = torch.matmul(token_embeddings, v_layer0_head0.T)
v_per_token.shape
torch.Size([17, 128])

注意力

与每个标记的值相乘后得到的注意力向量的形状是 [17x128]。

qkv_attention = torch.matmul(qk_per_token_after_masking_after_softmax, v_per_token)
qkv_attention.shape
torch.Size([17, 128])

多头注意力

我们现在得到了第一层和第一个头的注意力值。


接下来,我将运行一个循环,并对第一层中的每个头执行与上面相同的数学运算。

qkv_attention_store = []

for head in range(n_heads):
    q_layer0_head = q_layer0[head]
    k_layer0_head = k_layer0[head//4] # key weights are shared across 4 heads
    v_layer0_head = v_layer0[head//4] # value weights are shared across 4 heads
    q_per_token = torch.matmul(token_embeddings, q_layer0_head.T)
    k_per_token = torch.matmul(token_embeddings, k_layer0_head.T)
    v_per_token = torch.matmul(token_embeddings, v_layer0_head.T)

    q_per_token_split_into_pairs = q_per_token.float().view(q_per_token.shape[0], -1, 2)
    q_per_token_as_complex_numbers = torch.view_as_complex(q_per_token_split_into_pairs)
    q_per_token_split_into_pairs_rotated = torch.view_as_real(q_per_token_as_complex_numbers * freqs_cis[:len(tokens)])
    q_per_token_rotated = q_per_token_split_into_pairs_rotated.view(q_per_token.shape)

    k_per_token_split_into_pairs = k_per_token.float().view(k_per_token.shape[0], -1, 2)
    k_per_token_as_complex_numbers = torch.view_as_complex(k_per_token_split_into_pairs)
    k_per_token_split_into_pairs_rotated = torch.view_as_real(k_per_token_as_complex_numbers * freqs_cis[:len(tokens)])
    k_per_token_rotated = k_per_token_split_into_pairs_rotated.view(k_per_token.shape)

    qk_per_token = torch.matmul(q_per_token_rotated, k_per_token_rotated.T)/(128)**0.5
    mask = torch.full((len(tokens), len(tokens)), float("-inf"), device=tokens.device)
    mask = torch.triu(mask, diagonal=1)
    qk_per_token_after_masking = qk_per_token + mask
    qk_per_token_after_masking_after_softmax = torch.nn.functional.softmax(qk_per_token_after_masking, dim=1).to(torch.bfloat16)
    qkv_attention = torch.matmul(qk_per_token_after_masking_after_softmax, v_per_token)
    qkv_attention = torch.matmul(qk_per_token_after_masking_after_softmax, v_per_token)
    qkv_attention_store.append(qkv_attention)

len(qkv_attention_store)
32

我们现在得到了第一层中32个头的qkv_attention矩阵,接下来我要将所有的注意力得分合并成一个大小为 [17x4096] 的大矩阵。


我们快要完成了 😃

stacked_qkv_attention = torch.cat(qkv_attention_store, dim=-1)
stacked_qkv_attention.shape
torch.Size([17, 4096])

权重矩阵,最后的步骤之一

对于第0层注意力机制,最后要做的事情之一是将其与权重矩阵相乘。

w_layer0 = model["layers.0.attention.wo.weight"]
w_layer0.shape
torch.Size([4096, 4096])

这是一个简单的线性层,所以我们只需进行矩阵相乘

embedding_delta = torch.matmul(stacked_qkv_attention, w_layer0.T)
embedding_delta.shape
torch.Size([17, 4096])

我们现在得到了注意力机制之后的嵌入值变化,它应该被加到原始的标记嵌入上。

embedding_after_edit = token_embeddings_unnormalized + embedding_delta
embedding_after_edit.shape
torch.Size([17, 4096])

我们对嵌入增量进行归一化,然后通过一个前馈神经网络

embedding_after_edit_normalized = rms_norm(embedding_after_edit, model["layers.0.ffn_norm.weight"])
embedding_after_edit_normalized.shape
torch.Size([17, 4096])

加载前馈网络的权重并实现前馈网络

在llama3中,他们使用了SwiGLU前馈网络,这种网络架构在模型需要时非常擅长添加非线性元素。


在如今的大型语言模型中,使用这种前馈网络架构已经相当普遍。

w1 = model["layers.0.feed_forward.w1.weight"]
w2 = model["layers.0.feed_forward.w2.weight"]
w3 = model["layers.0.feed_forward.w3.weight"]
output_after_feedforward = torch.matmul(torch.functional.F.silu(torch.matmul(embedding_after_edit_normalized, w1.T)) * torch.matmul(embedding_after_edit_normalized, w3.T), w2.T)
output_after_feedforward.shape
torch.Size([17, 4096])

我们终于得到了第一层之后每个标记的新编辑嵌入

只剩下31层就完成了(只需一个循环)。


你可以想象这个编辑后的嵌入包含了在第一层中所有查询的信息。


现在每一层都会对所提出的问题进行越来越复杂的编码,直到我们得到一个可以完全推测下一个标记的嵌入。

layer_0_embedding = embedding_after_edit+output_after_feedforward
layer_0_embedding.shape
torch.Size([17, 4096])

天啊,一切都在一起

没错,就是这样。我们之前做的所有事情,现在一次性完成,针对每一层。

祝你阅读愉快 😃

final_embedding = token_embeddings_unnormalized
for layer in range(n_layers):
    qkv_attention_store = []
    layer_embedding_norm = rms_norm(final_embedding, model[f"layers.{layer}.attention_norm.weight"])
    q_layer = model[f"layers.{layer}.attention.wq.weight"]
    q_layer = q_layer.view(n_heads, q_layer.shape[0] // n_heads, dim)
    k_layer = model[f"layers.{layer}.attention.wk.weight"]
    k_layer = k_layer.view(n_kv_heads, k_layer.shape[0] // n_kv_heads, dim)
    v_layer = model[f"layers.{layer}.attention.wv.weight"]
    v_layer = v_layer.view(n_kv_heads, v_layer.shape[0] // n_kv_heads, dim)
    w_layer = model[f"layers.{layer}.attention.wo.weight"]
    for head in range(n_heads):
        q_layer_head = q_layer[head]
        k_layer_head = k_layer[head//4]
        v_layer_head = v_layer[head//4]
        q_per_token = torch.matmul(layer_embedding_norm, q_layer_head.T)
        k_per_token = torch.matmul(layer_embedding_norm, k_layer_head.T)
        v_per_token = torch.matmul(layer_embedding_norm, v_layer_head.T)
        q_per_token_split_into_pairs = q_per_token.float().view(q_per_token.shape[0], -1, 2)
        q_per_token_as_complex_numbers = torch.view_as_complex(q_per_token_split_into_pairs)
        q_per_token_split_into_pairs_rotated = torch.view_as_real(q_per_token_as_complex_numbers * freqs_cis)
        q_per_token_rotated = q_per_token_split_into_pairs_rotated.view(q_per_token.shape)
        k_per_token_split_into_pairs = k_per_token.float().view(k_per_token.shape[0], -1, 2)
        k_per_token_as_complex_numbers = torch.view_as_complex(k_per_token_split_into_pairs)
        k_per_token_split_into_pairs_rotated = torch.view_as_real(k_per_token_as_complex_numbers * freqs_cis)
        k_per_token_rotated = k_per_token_split_into_pairs_rotated.view(k_per_token.shape)
        qk_per_token = torch.matmul(q_per_token_rotated, k_per_token_rotated.T)/(128)**0.5
        mask = torch.full((len(token_embeddings_unnormalized), len(token_embeddings_unnormalized)), float("-inf"))
        mask = torch.triu(mask, diagonal=1)
        qk_per_token_after_masking = qk_per_token + mask
        qk_per_token_after_masking_after_softmax = torch.nn.functional.softmax(qk_per_token_after_masking, dim=1).to(torch.bfloat16)
        qkv_attention = torch.matmul(qk_per_token_after_masking_after_softmax, v_per_token)
        qkv_attention_store.append(qkv_attention)

    stacked_qkv_attention = torch.cat(qkv_attention_store, dim=-1)
    w_layer = model[f"layers.{layer}.attention.wo.weight"]
    embedding_delta = torch.matmul(stacked_qkv_attention, w_layer.T)
    embedding_after_edit = final_embedding + embedding_delta
    embedding_after_edit_normalized = rms_norm(embedding_after_edit, model[f"layers.{layer}.ffn_norm.weight"])
    w1 = model[f"layers.{layer}.feed_forward.w1.weight"]
    w2 = model[f"layers.{layer}.feed_forward.w2.weight"]
    w3 = model[f"layers.{layer}.feed_forward.w3.weight"]
    output_after_feedforward = torch.matmul(torch.functional.F.silu(torch.matmul(embedding_after_edit_normalized, w1.T)) * torch.matmul(embedding_after_edit_normalized, w3.T), w2.T)
    final_embedding = embedding_after_edit+output_after_feedforward

我们现在得到了最终的嵌入,这是模型对下一个标记做出的最佳猜测

嵌入的形状与常规标记嵌入相同,为 [17x4096],其中17是标记的数量,4096是嵌入的维度。

final_embedding = rms_norm(final_embedding, model["norm.weight"])
final_embedding.shape
torch.Size([17, 4096])

最后,让我们将嵌入解码为标记值

我们将使用输出解码器将最终的嵌入转换为一个标记。

model["output.weight"].shape
torch.Size([128256, 4096])

我们使用最后一个标记的嵌入来预测下一个值

希望在我们的案例中,答案是42 😃
注意:42是书籍《银河系漫游指南》中对“生命、宇宙及一切终极问题的答案”的回答。根据这本书,大多数现代大型语言模型都会在这里回答42,这应该验证我们整个代码的正确性!祝我好运 😃

logits = torch.matmul(final_embedding[-1], model["output.weight"].T)
logits.shape
torch.Size([128256])

模型预测下一个标记是编号2983的标记,这是“42”的标记编号吗?

我为你打气!这是最后一个代码单元,希望你玩得开心 😃

next_token = torch.argmax(logits, dim=-1)
next_token
tensor(2983)

太棒了!

tokenizer.decode([next_token.item()])
'42'

http://www.niftyadmin.cn/n/5707556.html

相关文章

51c大模型~合集2

我自己的原文哦~ https://blog.51cto.com/whaosoft/11474385 #大模型高效推理综述 本文对现有的关于高效大模型推理的文献进行了全面的综述总结。 大模型由于其在各种任务中的出色表现而引起了广泛的关注。然而&#xff0c;大模型推理的大量计算和内存需求对其在资源受限场景…

类的初步学习(关于类的私有与公有成员、类链表、析构函数、友元函数)

1.关于类的私有与公有成员 #include<iostream> using namespace std; class Node {int data;//私有数据成员可以在该类的公有函数里面调用&#xff0c;但是在main函数下不可调用 public:void print(int n); }; void Node::print(int n) {data n;cout << data; } …

AI大模型时代:这20种职业可能首当其冲面临替代风险

随着ChatGPT为代表的大语言模型人工智能技术发展&#xff0c;“机器替代人工”的话题在最近半年持续火热。 近日&#xff0c;北京大学国家发展研究院联合智联招聘&#xff0c;发布了《AI大模型对我国劳动力市场潜在影响研究》报告&#xff0c;并首次构建了各种职业的人工智能影…

第三方软件检测机构收费标准是怎样的?

随着信息时代的发展&#xff0c;软件产品为我们带来了极大便利&#xff0c;产品数量也逐渐增多&#xff0c;因此软件企业为了更好的在市场上竞争&#xff0c;在产品上线前的软件检测必不可少。软件检测一般有两种方式&#xff1a;一种是公司自有团队进行检测工作&#xff0c;另…

fork中的死锁问题

背景 当我们通过fork去创建子进程时&#xff0c;当父/子进程都涉及到锁的操作&#xff0c;可能会产生死锁。 代码样例 #include <iostream> #include <mutex> #include <unistd.h> std::mutex m; int main() {std::cout << "main process begi…

ROS2初级面试题汇总

大家好&#xff0c;我是小白小帅&#xff0c;在日常基于ros2开发移动机器人和学习过程中&#xff0c;个人总结和收集了一些关于ros2的问题&#xff08;共25道&#xff09;&#xff0c;这些问题也是面试中可能涉及到的一些知识点&#xff0c;对于我个人就是一个简单的记录&#…

VS如何修改生成的exe程序的名称

一、简述 在日常开发中&#xff0c;我们一般会将工程名称设置为生成的exe程序的名称&#xff0c;在VS编译器中&#xff0c;默认生成的exe名称取自工程名称&#xff0c;所以如果是已经建立好工程&#xff0c;想要修改生成的exe名称&#xff0c;可以通过工程属性修改&#xff1a…

学习vue20.17.0-列表渲染

列表渲染 官方中文网站:列表渲染 | Vue.js (vuejs.org) v-for 我们可以使用 v-for 指令基于一个数组来渲染一个列表。v-for 指令的值需要使用 item in items 形式的特殊语法,其中 items 是源数据的数组,而 item 是迭代项的别名:演示代码: js 代码: const items = ref([{…