从零手撕LLaMa3,怒拿5.7K星标!!

大家好,我是程序锅。

一个月前,Meta发布了开源大模型llama3系列,在多个关键基准测试中优于业界 SOTA 模型,并在代码生成任务上全面领先。

和往常剧本类似,llama系列对中文支持力度不够。于是开发者们便开始了本地部署和基于中文语言训练。

在这里推荐一个项目:

项目地址:https://github.com/naklecha/llama3-from-scratch

这个项目发布了一个从零开始实现llama3的库,包括跨多个头的注意力矩阵乘法、位置编码和toekn化等等技术有非常详细的解释。

完成学习这个项目,你不仅会对llama3的网络结构非常了解,并且可以举一反三,自己分析其它开源大模型的网络架构。

下面举几个项目中涉及到的知识点。

1.如何将文本转化为token:

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', ' ']

2.如何将token转化为embedding

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])

等等还有很多很多,从llama输入到最后softmax输入,此项目均有代码解释与实战。

Talk is cheap,show me the code。如果你熟悉Transformer的原理,赶紧动手学起来吧!

项目地址:https://github.com/naklecha/llama3-from-scratch

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值