Как загрузить точно настроенную модель, такую как Альпака-Лора (PeftModel()), из локальных файлов, а не из моделей Huggingface?
Я настроил модель Llama, используя адаптацию низкого ранга (LoRA), основанную на пакете peft. Файлы результатов и сохраняются.
Я могу загрузить точно настроенную модель из Huggingface, используя следующие коды:
model = LlamaForCausalLM.from_pretrained(<model_name>,
torch_dtype=torch.float16,
device_map='auto',
llm_int8_enable_fp32_cpu_offload=True
)
peft_model_id = <hub_model_name>
peft_model = PeftModelForCausalLM.from_pretrained(model, peft_model_id)
Если я хочу напрямую загрузить настроенную модель, используя локальные файлыadapter_config.json
иadapter_model.bin
(вместо того, чтобы запихивать их в хаб), как это сделать?
Заранее спасибо!
1 ответ
Это очень похоже на то, как вы настроили модели из Huggingface. Основная часть — получить локальный путь к используемой исходной модели. Это можно сделать, создав объект PeftConfig, используя локальный путь к точно настроенной модели Peft (папка, в которой находится ваш файл адаптера_config.json и все настроенные веса). Если вы использовали какие-либо виды квантования, такие как 4-битная точная настройка с использованием битов и байтов, вам также потребуется настроить объект конфигурации для них и передать их в модель. Вот пример сценария:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftConfig, PeftModel
#Set Path to folder that contains adapter_config.json and the associated .bin files for the Peft model
peft_model_id = '/path/to/local/peft_model_folder'
#Get PeftConfig from the finetuned Peft Model. This config file contains the path to the base model
config = PeftConfig.from_pretrained(model_id)
# If you quantized the model while finetuning using bits and bytes
# and want to load the model in 4bit for inference use the following code.
# NOTE: Make sure the quant and compute types match what you did during finetuning
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
###
#Load the base model - if you are not using the bnb_config then remove the quantization_config argument
#You may or may not need to set use_auth_token to True depending on your model.
model = AutoModelForCausalLM.from_pretrained(
config.base_model_name_or_path,
quantization_config=bnb_config,
use_auth_token=True,
torch_dtype=torch.bfloat16,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
tokenizer.pad_token = tokenizer.eos_token
# Load the Peft/Lora model
model = PeftModel.from_pretrained(model, peft_model_id)