mirror of
https://github.com/tloen/alpaca-lora.git
synced 2024-10-01 01:05:56 -04:00
630d1146c8
* Update export_hf_checkpoint.py * Update finetune.py New tokenizer base model for the current dev branch of transformers * Update generate.py * Update export_state_dict_checkpoint.py * Update export_hf_checkpoint.py
56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import os
|
|
|
|
import torch
|
|
import transformers
|
|
from peft import PeftModel
|
|
from transformers import LlamaForCausalLM, LlamaTokenizer # noqa: F402
|
|
|
|
BASE_MODEL = os.environ.get("BASE_MODEL", None)
|
|
assert (
|
|
BASE_MODEL
|
|
), "Please specify a value for BASE_MODEL environment variable, e.g. `export BASE_MODEL=huggyllama/llama-7b`" # noqa: E501
|
|
|
|
tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
|
|
|
|
base_model = LlamaForCausalLM.from_pretrained(
|
|
BASE_MODEL,
|
|
load_in_8bit=False,
|
|
torch_dtype=torch.float16,
|
|
device_map={"": "cpu"},
|
|
)
|
|
|
|
first_weight = base_model.model.layers[0].self_attn.q_proj.weight
|
|
first_weight_old = first_weight.clone()
|
|
|
|
lora_model = PeftModel.from_pretrained(
|
|
base_model,
|
|
"tloen/alpaca-lora-7b",
|
|
device_map={"": "cpu"},
|
|
torch_dtype=torch.float16,
|
|
)
|
|
|
|
lora_weight = lora_model.base_model.model.model.layers[
|
|
0
|
|
].self_attn.q_proj.weight
|
|
|
|
assert torch.allclose(first_weight_old, first_weight)
|
|
|
|
# merge weights - new merging method from peft
|
|
lora_model = lora_model.merge_and_unload()
|
|
|
|
lora_model.train(False)
|
|
|
|
# did we do anything?
|
|
assert not torch.allclose(first_weight_old, first_weight)
|
|
|
|
lora_model_sd = lora_model.state_dict()
|
|
deloreanized_sd = {
|
|
k.replace("base_model.model.", ""): v
|
|
for k, v in lora_model_sd.items()
|
|
if "lora" not in k
|
|
}
|
|
|
|
LlamaForCausalLM.save_pretrained(
|
|
base_model, "./hf_ckpt", state_dict=deloreanized_sd, max_shard_size="400MB"
|
|
)
|