hugging face llama使用
文章目录
记录一下使用hugging face llama推理时遇到的问题.
首先使用如下代码进行推理:
from transformers import AutoConfig, AutoModel  | 
这里得到的输入为tensor([[    1,  1619,  1024,   338,  1085,  2829, 29874, 29892,   590, 25448]]),
attention_mask为tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
generate过程
加载模型的
generation.jsonGenerationConfig {
"bos_token_id": 0,
"eos_token_id": 1,
"max_new_tokens": 32,
"pad_token_id": -1,
"use_cache": false
}配置最大长度
generation_config.max_length = generation_config.max_new_tokens + input_ids_length目前是32+10 = 42greedy_search
根据输出策略, 进入
greedy_search进行推理.- 循环推理
 - 准备输入 
'input_ids':
tensor([[ 1, 1619, 1024, 338, 1085, 2829, 29874, 29892, 590, 25448]])
'position_ids':
tensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
'past_key_values':
None
'use_cache':
False
'attention_mask':
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) - 进入forward
 - decode layers.
输出hidden_states为
torch.Size([1, 10, 8192]) - 执行lm head,
得到logits为
torch.Size([1, 10, 32000]) - 取logits最后一个
torch.Size([1, 32000])求最大概率 这里我得到的是color. 而整个输出得到是# name is Ktha and and I friends color. 本来以为是输入<s> xxx会得到xxxy这样, 然后取y作为一个输出. 现在看来其实前面部分也会被脑补一些.