huggingface 笔记:Llama3-8B

1 基本使用

from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM
import transformers
import torch
import os


os.environ["HF_TOKEN"] = '*******'
# 设置环境变量,用于存储Hugging Face的访问令牌

model='meta-llama/Meta-Llama-3-8B'
# 定义模型名称

tokenizer=AutoTokenizer.from_pretrained(model)
# 使用预训练模型名称加载分词器

llama=AutoModelForCausalLM.from_pretrained(model, device_map="cuda:1")
# 使用预训练模型名称加载因果语言模型,并将其加载到指定的GPU设备上
llama.device
#device(type='cuda', index=1)

2 推理

import time

begin=time.time()

input_text = "Write me a poem about maching learning."

input_ids = tokenizer(input_text, return_tensors="pt").to(llama.device)

outputs = llama.generate(**input_ids)


print(tokenizer.decode(outputs[0]))

end=time.time()
print(end-begin)
'''
<|begin_of_text|>Write me a poem about maching learning. I will use it for a project in my class. You can use whatever words you want. I will use it for a project in my class. You can use whatever words you want.<|end_of_text|>
1.718801736831665
'''

3 模型架构

llama
LlamaForCausalLM(
  (model): LlamaModel(
    (embed_tokens): Embedding(128256, 4096)
    (layers): ModuleList(
      (0-31): 32 x LlamaDecoderLayer(
        (self_attn): LlamaSdpaAttention(
          (q_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (k_proj): Linear(in_features=4096, out_features=1024, bias=False)
          (v_proj): Linear(in_features=4096, out_features=1024, bias=False)
          (o_proj): Linear(in_features=4096, out_features=4096, bias=False)
          (rotary_emb): LlamaRotaryEmbedding()
        )
        (mlp): LlamaMLP(
          (gate_proj): Linear(in_features=4096, out_features=14336, bias=False)
          (up_proj): Linear(in_features=4096, out_features=14336, bias=False)
          (down_proj): Linear(in_features=14336, out_features=4096, bias=False)
          (act_fn): SiLU()
        )
        (input_layernorm): LlamaRMSNorm()
        (post_attention_layernorm): LlamaRMSNorm()
      )
    )
    (norm): LlamaRMSNorm()
  )
  (lm_head): Linear(in_features=4096, out_features=128256, bias=False)
)
1
​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UQI-LIUWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值