当前位置:   article > 正文

huggingface 笔记:Llama3-8B_huggingface中llama模型的访问令牌

huggingface中llama模型的访问令牌

1 基本使用

  1. from transformers import AutoTokenizer
  2. from transformers import AutoModelForCausalLM
  3. import transformers
  4. import torch
  5. import os
  6. os.environ["HF_TOKEN"] = '*******'
  7. # 设置环境变量,用于存储Hugging Face的访问令牌
  8. model='meta-llama/Meta-Llama-3-8B'
  9. # 定义模型名称
  10. tokenizer=AutoTokenizer.from_pretrained(model)
  11. # 使用预训练模型名称加载分词器
  12. llama=AutoModelForCausalLM.from_pretrained(model, device_map="cuda:1")
  13. # 使用预训练模型名称加载因果语言模型,并将其加载到指定的GPU设备上
  1. llama.device
  2. #device(type='cuda', index=1)

2 推理

  1. import time
  2. begin=time.time()
  3. input_text = "Write me a poem about maching learning."
  4. input_ids = tokenizer(input_text, return_tensors="pt").to(llama.device)
  5. outputs = llama.generate(**input_ids)
  6. print(tokenizer.decode(outputs[0]))
  7. end=time.time()
  8. print(end-begin)
  9. '''
  10. <|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|>
  11. 1.718801736831665
  12. '''

3 模型架构

  1. llama
  2. LlamaForCausalLM(
  3. (model): LlamaModel(
  4. (embed_tokens): Embedding(128256, 4096)
  5. (layers): ModuleList(
  6. (0-31): 32 x LlamaDecoderLayer(
  7. (self_attn): LlamaSdpaAttention(
  8. (q_proj): Linear(in_features=4096, out_features=4096, bias=False)
  9. (k_proj): Linear(in_features=4096, out_features=1024, bias=False)
  10. (v_proj): Linear(in_features=4096, out_features=1024, bias=False)
  11. (o_proj): Linear(in_features=4096, out_features=4096, bias=False)
  12. (rotary_emb): LlamaRotaryEmbedding()
  13. )
  14. (mlp): LlamaMLP(
  15. (gate_proj): Linear(in_features=4096, out_features=14336, bias=False)
  16. (up_proj): Linear(in_features=4096, out_features=14336, bias=False)
  17. (down_proj): Linear(in_features=14336, out_features=4096, bias=False)
  18. (act_fn): SiLU()
  19. )
  20. (input_layernorm): LlamaRMSNorm()
  21. (post_attention_layernorm): LlamaRMSNorm()
  22. )
  23. )
  24. (norm): LlamaRMSNorm()
  25. )
  26. (lm_head): Linear(in_features=4096, out_features=128256, bias=False)
  27. )
  28. 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/747201
推荐阅读
相关标签
  

闽ICP备14008679号