-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Manual Conversion
Yiming Cui edited this page Apr 15, 2023
·
17 revisions
- 确保机器有足够的内存加载完整模型(例如7B模型需要13-15G)以进行合并模型操作。
- 务必确认基模型和下载的LoRA模型完整性,检查是否与SHA256.md所示的值一致,否则无法进行合并操作。原版LLaMA包含:
tokenizer.model
、tokenizer_checklist.chk
、consolidated.*.pth
、params.json
- 主要依赖库如下(如果出问题就请安装以下指定版本):
-
transformers
(4.28.0测试通过) -
sentencepiece
(0.1.97测试通过) -
peft
(0.2.0测试通过) - python版本建议在3.9以上
-
pip install transformers
pip install sentencepiece
pip install peft
注意:本项目不对使用第三方(非Facebook官方)权重的合规性和正确性负责,例如HuggingFace模型库中的decapoda-research/llama-7b-hf
(use at your own risk)。
请使用🤗transformers提供的脚本convert_llama_weights_to_hf.py,将原版LLaMA模型转换为HuggingFace格式。将原版LLaMA的tokenizer.model
放在--input_dir
指定的目录,其余文件放在${input_dir}/${model_size}
下。执行以下命令后,--output_dir
中将存放转换好的HF版权重。
python src/transformers/models/llama/convert_llama_weights_to_hf.py \
--input_dir path_to_original_llama_root_dir \
--model_size 7B \
--output_dir path_to_original_llama_hf_dir
这一步骤会对原版LLaMA模型(HF格式)扩充中文词表,合并LoRA权重并生成全量模型权重。此处可有两种选择:
-
输出PyTorch版本权重(
.pth
文件),使用merge_llama_with_chinese_lora.py
脚本 -
输出HuggingFace版本权重(
.bin
文件),使用merge_llama_with_chinese_lora_to_hf.py
脚本(感谢@sgsdxzy 提供)
以上两个脚本所需参数一致,仅输出文件格式不同。下面以生成PyTorch版本权重为例,介绍相应的参数设置。
python scripts/merge_llama_with_chinese_lora.py \
--base_model path_to_original_llama_hf_dir \
--lora_model path_to_chinese_llama_or_alpaca_lora \
--output_dir path_to_output_dir
参数说明:
-
--base_model
:存放HF格式的LLaMA模型权重和配置文件的目录(Step 1生成) -
--lora_model
:中文LLaMA/Alpaca LoRA解压后文件所在目录,也可使用🤗Model Hub模型调用名称 -
--output_dir
:指定保存全量模型权重的目录,默认为./
- (可选)
--offload_dir
:对于低内存用户需要指定一个offload缓存路径
- 模型合并与转换
- 模型量化、推理、部署
- 效果与评测
- 训练细节
- 常见问题
- Model Reconstruction
- Model Quantization, Inference and Deployment
- System Performance
- Training Details
- FAQ