forked from EvilFreelancer/ruGPT-3.5-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_native.py
64 lines (51 loc) · 1.65 KB
/
convert_to_native.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fire
import torch
from peft import PeftModel, PeftConfig
from transformers import GPT2LMHeadModel
from tqdm.auto import tqdm
def translate_state_dict_key(k): # noqa: C901
if 'lora' in k:
return None
else:
return k
def convert_to_native(
model_name: str,
output_path: str,
device: str = "cpu",
enable_offloading: bool = False
):
assert output_path.endswith(".bin")
config = PeftConfig.from_pretrained(model_name)
base_model_path = config.base_model_name_or_path
base_model = GPT2LMHeadModel.from_pretrained(
base_model_path,
load_in_8bit=False,
torch_dtype=torch.float32,
device_map={'': device},
)
lora_model = PeftModel.from_pretrained(
base_model,
model_name,
device_map={'': device},
torch_dtype=torch.float32,
)
lora_model = lora_model.merge_and_unload()
lora_model.train(False)
lora_model_sd = lora_model.state_dict()
del lora_model, base_model
total = len(lora_model_sd)
with tqdm(list(lora_model_sd.keys())) as progress_bar:
for i, k in enumerate(progress_bar):
# new_k = k
new_k = translate_state_dict_key(k)
if new_k is None:
continue
v = lora_model_sd.pop(k)
lora_model_sd[new_k] = v
if enable_offloading and i <= total // 2:
# offload half of all tensors to RAM
lora_model_sd[new_k] = lora_model_sd[new_k].cpu()
print('Saving state_dict...')
torch.save(lora_model_sd, f'{output_path}')
if __name__ == '__main__':
fire.Fire(convert_to_native)