阿里云域名注册好了怎么做网站google开户
文章目录
- 1. HuggingFace模型下载
- 2. 模型推理:文本问答
1. HuggingFace模型下载
模型在 HuggingFace 下载,如果下载速度太慢,可以在 HuggingFace镜像网站 或 ModelScope 进行下载。
使用HuggingFace的下载命令(需要先注册HuggingFace账号):
第一步:安装 git-lfs
curl https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash
apt-get install git-lfs
第二步:下载 Qwen2-0.5B 模型
git lfs clone https://huggingface.co/Qwen/Qwen2-0.5B
下载完后的模型包括以下文件:
config.json # 模型配置文件,包含了模型的各种参数设置,例如层数、隐藏层大小、注意力头数
generation_config.json #文本生成相关的模型配置
merges.txt #训练tokenizer阶段所得到的合并词表结果
model.Safetensors #模型文件
tokenizer.json #分词器,将词转换为数字
tokenizer_config.json #分词模型的配置信息,如分词器的类型、词汇表大小、最大序列长度、特殊标记等
vocab.json #词表
2. 模型推理:文本问答
本文使用单卡 A100-80G 进行推理实验
注意:使用 Qwen2 模型需要将 transformers 库更新到最新版本
code:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM# 从本地加载预训练模型
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_path = "models/Qwen2-0.5B"
model = AutoModelForCausalLM.from_pretrained(model_path,device_map=device)
# 设置 device_map="auto" 会自动使用所有多卡
print(f"model: {model}")# 加载 tokenizer(分词器)
# 分词器负责将句子分割成更小的文本片段 (词元) 并为每个词元分配一个称为输入 id 的值(数字),因为模型只能理解数字。
# 每个模型都有自己的分词器词表,因此使用与模型训练时相同的分词器很重要,否则它会误解文本。
tokenizer = AutoTokenizer.from_pretrained(model_path, add_eos_token=True, padding_side='left')
# add_eos_token=True: 可选参数,表示在序列的末尾添加一个结束标记(end-of-sequence token),这有助于模型识别序列的结束。
# padding_side='left': 可选参数,表示 padding 应该在序列的哪一边进行,确保所有序列的长度一致。# 模型输入
input_text = "介绍一下悉尼这座城市。"# 对输入文本分词
input_ids = tokenizer(input_text, return_tensors="pt").to(device)
# return_tensors="pt": 指定返回的数值序列的数据类型。"pt"代表 PyTorch Tensor,表示分词器将返回一个PyTorch而不是TensorFlow对象# 生成文本回答
# max_new_tokens:模型生成的新的 token 的最大数量为 200
outputs = model.generate(input_ids["input_ids"], max_new_tokens=200)
print(f"type(outputs) = {type(outputs)}") # <class 'torch.Tensor'>
print(f"outputs.shape = {outputs.shape}") # torch.Size([1, 95]),outputs.shape是随机的,是不超过200的数# 将输出token解码为文本
decoded_outputs = tokenizer.decode(outputs[0])
print(f"decoded_outputs: {decoded_outputs}")
模型输出的文本回答如下:
decoded_outputs: 介绍一下悉尼这座城市。 悉尼这座城市位于澳大利亚东南部,是澳大利亚最大的城市之一。它是一个现代化的城市,拥有许多现代化的建筑和设施,如购物中心、博馆、剧院和音乐厅等。悉尼的气候宜人,四季分明,夏季炎热,冬季寒冷,适合旅游和度假。此外,悉尼还有许多著名的景点,如悉尼歌剧院、悉尼塔、悉尼海港大桥等,这些景点吸引来自世界各地的游客。<|endoftext|>
Qwen2-0.5B 模型结构:
Qwen2ForCausalLM((model): Qwen2Model((embed_tokens): Embedding(151936, 896)(layers): ModuleList((0-23): 24 x Qwen2DecoderLayer((self_attn): Qwen2SdpaAttention((q_proj): Linear(in_features=896, out_features=896, bias=True)(k_proj): Linear(in_features=896, out_features=128, bias=True)(v_proj): Linear(in_features=896, out_features=128, bias=True)(o_proj): Linear(in_features=896, out_features=896, bias=False)(rotary_emb): Qwen2RotaryEmbedding())(mlp): Qwen2MLP((gate_proj): Linear(in_features=896, out_features=4864, bias=False)(up_proj): Linear(in_features=896, out_features=4864, bias=False)(down_proj): Linear(in_features=4864, out_features=896, bias=False)(act_fn): SiLU())(input_layernorm): Qwen2RMSNorm()(post_attention_layernorm): Qwen2RMSNorm()))(norm): Qwen2RMSNorm())(lm_head): Linear(in_features=896, out_features=151936, bias=False)
)
参考资料:Hugging Face Transformers 萌新完全指南