使用Transformers推理 (original) (raw)

我们提供了命令行和Web图形界面两种方式使用原生Transformers进行推理。

以加载Chinese-Alpaca-7B模型为例(加载Chinese-Alpaca-Plus的方式见下面的加载Chinese-Alpaca-Plus)说明启动方式。

命令行交互形式

python scripts/inference/inference_hf.py
--base_model path_to_original_llama_hf_dir
--lora_model path_to_chinese_llama_or_alpaca_lora
--with_prompt
--interactive

如果之前已执行了merge_llama_with_chinese_lora_to_hf.py脚本将lora权重合并,那么无需再指定--lora_model,启动方式更简单:

python scripts/inference/inference_hf.py
--base_model path_to_merged_llama_or_alpaca_hf_dir
--with_prompt
--interactive

参数说明:

Web图形界面交互形式

该方式将启动Web前端页面进行交互,并且支持多轮对话。除transformers之外,需要安装gradio和mdtex2html:

pip install gradio pip install mdtex2html

启动命令如下:

python scripts/inference/gradio_demo.py \
    --base_model path_to_original_llama_hf_dir \
    --lora_model path_to_chinese_alpaca_lora

同样,如果已经执行了merge_llama_with_chinese_lora_to_hf.py脚本将lora权重合并,那么无需再指定--lora_model

python scripts/inference/gradio_demo.py --base_model path_to_merged_alpaca_hf_dir 

参数说明:

加载Chinese-Alpaca-Plus

目前两个脚本都不支持直接从LoRA权重加载Chinese-Alpaca-Plus进行推理;如要进行Chinese-Alpaca-Plus进的推理,请先合并模型,流程如下:

  1. 使用merge_llama_with_chinese_lora.py合并lora,生成完整的hf格式模型权重:

python scripts/merge_llama_with_chinese_lora.py
--base_model path_to_hf_llama
--lora_model path_to_chinese_llama_plus_lora,path_to_chinese_alpaca_plus_lora
--output_type huggingface
--output_dir path_to_merged_chinese_alpaca_plus

  1. 使用inference_hf.py或gradio_demo.py加载合并后的模型进行推理,如:

python scripts/inference/inference_hf.py
--base_model path_to_merged_chinese_alpaca_plus
--with_prompt --interactive

注意事项