A.I.(인공지능) & M.L.(머신러닝)/transformers
[실습] merge base model and LoRA adapter_model, Upload huggingface-hub
Tech엠지대표
2024. 4. 4. 16:31
Topic. Fine-tuning 후 기존 base 모델과 생성된 adapter_model 을 merge합니다.
1. huggingface login
$ pip install huggingface_hub
$ huggingface-cli login
1.1 허깅페이스에서 발급받은 토큰 입력
2. Base Model 가져오기
from transformers import AutoModelForCausalLM, AutoTokenizer
model_pretrained_name = {base model id}
path_to_lora_adapters = {adapter model path}
#모델 불러오기
model = AutoModelForCausalLM.from_pretrained(
model_pretrained_name,
device_map="cuda:0",
quantization_config=bnb_config,
)
#토크나이저 불러오기
tokenizer = AutoTokenizer.from_pretrained(
model_pretrained_name,
add_eos_token=True
)
#어뎁터 모델 불러오기
model = PeftModel.from_pretrained(
model,
path_to_lora_adapters,
torch_dtype=torch.float16
)
3. Merge
model = model.merge_and_unload()
4. Huggingface-hub upload
hub_model_id = {hub_model_id}
# model 업로드
model.push_to_hub(hub_model_id, token=True)
# tokenizer 업로드
tokenizer.push_to_hub(hub_model_id, token=True)