Closed fxb392 closed 1 month ago
先把问题背景描述清楚,都不知道你用什么工具做的推理,运行的命令是什么。
model_name_or_path="./chinese-alpaca-2-7b/"
test_data_path="./test_datas2.jsonl"
inference_datas="./output/test_data_chinese-alpaca-2_base.xlsx"
temperature=None
top_p=None
do_sample=False
use_adapter=False
def init_model(use_adapter=use_adapter):
print("init model ...")
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16)
model = model.eval()
if use_adapter:
model=PeftModel.from_pretrained(model,adapter_name_or_path)
return model, tokenizer
def get_test_data(file_path): datas_list=[] with open(file_path,'r',encoding='utf-8') as f: for row in f: row_json=json.loads(row) datas_list.append(row_json['instruction'])
return datas_list
def inference(): model, tokenizer = init_model() print(model.device) print("data_path:",test_data_path) datas_test=get_test_data(test_data_path) datas_test_response=[] for i,prompt in enumerate(datas_test): print(i,prompt)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# outputs = model.generate(**inputs, max_new_tokens=50)
outputs = model.generate(**inputs,max_new_tokens=1024)
response=tokenizer.decode(outputs[0], skip_special_tokens=True)
print()
#------------------AI-ModelScope/chinese-alpaca-2-7b end---------------------
print(response)
pd.DataFrame(datas_test_response).to_excel(inference_datas)`
上面是有关的代码。 在曙光DCU上做的推理,使用代码参考了modelscope仓库中的示例(https://www.modelscope.cn/models/AI-ModelScope/chinese-alpaca-2-7b/summary) 改动了model.generate(**inputs,max_new_tokens=1024)中的max_new_tokens,从50改为了1024. 运行命令直接 写了main函数 python 文件名.py 抱歉,上面代码我调不好格式。
你指向的:https://www.modelscope.cn/models/AI-ModelScope/chinese-alpaca-2-7b/summary 并不是我们的官方目录。 我们自己的inference脚本在,正常推理一般不会出现你列出的问题:https://github.com/ymcui/Chinese-LLaMA-Alpaca-2/blob/main/scripts/inference/inference_hf.py 说明在:https://github.com/ymcui/Chinese-LLaMA-Alpaca-2/wiki/inference_with_transformers_zh
至于是不是DCU的问题,需要你们自己debug了。
ok,感谢您!
我推测此问题出在你使用的是alpaca模型,所以在推理前缺少prompt模板 可以尝试在prompt之前加上generate_prompt合并模板
def generate_prompt(instruction, system_prompt=DEFAULT_SYSTEM_PROMPT):
return TEMPLATE.format_map({'instruction': instruction,'system_prompt': system_prompt})
input = generate_prompt(instruction=prompt)
确实是这个原因,文档中也有说明,感谢! --with_prompt:是否将输入与prompt模版进行合并。如果加载Alpaca模型,请务必启用此选项!
提交前必须检查以下项目
问题类型
None
基础模型
Chinese-Alpaca-2 (7B/13B)
操作系统
Linux
详细描述问题
依赖情况(代码类问题务必提供)
运行日志或截图