2022-11-11 09:08:17 +00:00
|
|
|
import os
|
|
|
|
from multiprocessing import Pool
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
import torch
|
|
|
|
|
2022-11-11 09:08:17 +00:00
|
|
|
# download pytorch model ckpt in https://huggingface.co/facebook/opt-66b/tree/main
|
|
|
|
# you can use whether wget or git lfs
|
|
|
|
|
|
|
|
path = "/path/to/your/ckpt"
|
|
|
|
new_path = "/path/to/the/processed/ckpt/"
|
|
|
|
|
|
|
|
assert os.path.isdir(path)
|
|
|
|
files = []
|
|
|
|
for filename in os.listdir(path):
|
|
|
|
filepath = os.path.join(path, filename)
|
|
|
|
if os.path.isfile(filepath):
|
|
|
|
files.append(filepath)
|
|
|
|
|
|
|
|
with Pool(14) as pool:
|
|
|
|
ckpts = pool.map(torch.load, files)
|
|
|
|
|
|
|
|
restored = {}
|
|
|
|
for ckpt in ckpts:
|
2023-09-19 06:20:26 +00:00
|
|
|
for k, v in ckpt.items():
|
|
|
|
if k[0] == "m":
|
|
|
|
k = k[6:]
|
|
|
|
if k == "lm_head.weight":
|
2022-11-11 09:08:17 +00:00
|
|
|
k = "head.dense.weight"
|
2023-09-19 06:20:26 +00:00
|
|
|
if k == "decoder.final_layer_norm.weight":
|
2022-11-11 09:08:17 +00:00
|
|
|
k = "decoder.layer_norm.weight"
|
2023-09-19 06:20:26 +00:00
|
|
|
if k == "decoder.final_layer_norm.bias":
|
2022-11-11 09:08:17 +00:00
|
|
|
k = "decoder.layer_norm.bias"
|
|
|
|
restored[k] = v
|
|
|
|
restored["decoder.version"] = "0.0"
|
|
|
|
|
|
|
|
|
|
|
|
split_num = len(restored.keys()) // 60
|
|
|
|
count = 0
|
|
|
|
file_count = 1
|
|
|
|
tmp = {}
|
2023-09-19 06:20:26 +00:00
|
|
|
for k, v in restored.items():
|
2022-11-11 09:08:17 +00:00
|
|
|
print(k)
|
|
|
|
tmp[k] = v
|
2023-09-19 06:20:26 +00:00
|
|
|
count = count + 1
|
|
|
|
if count == split_num:
|
2022-11-11 09:08:17 +00:00
|
|
|
filename = str(file_count) + "-restored.pt"
|
|
|
|
torch.save(tmp, os.path.join(new_path, filename))
|
|
|
|
file_count = file_count + 1
|
|
|
|
count = 0
|
|
|
|
tmp = {}
|
|
|
|
|
|
|
|
filename = str(file_count) + "-restored.pt"
|
|
|
|
torch.save(tmp, os.path.join(new_path, filename))
|