-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_fusion.py
More file actions
37 lines (29 loc) · 1.27 KB
/
Copy pathexport_fusion.py
File metadata and controls
37 lines (29 loc) · 1.27 KB
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
import torch
import os
import struct
def export_fusion_weights(ckpt_path, out_dir):
print(f"Loading {ckpt_path}...")
ckpt = torch.load(ckpt_path, map_location='cpu', weights_only=False)
state_dict = ckpt # Assuming it was saved as the state_dict directly
os.makedirs(out_dir, exist_ok=True)
for name, param in state_dict.items():
# Only export trainable parameters (refiners, rag_scales, inj_projs)
if not any(k in name for k in ['refiner', 'rag_scale', 'inj_proj']):
continue
# Clean name
clean_name = name.replace('.', '_')
data = param.float()
shape = list(data.shape)
if len(shape) == 0:
shape = [1, 1]
data = data.reshape(shape)
elif len(shape) == 1:
shape = [1, shape[0]]
data = data.reshape(shape)
bin_path = os.path.join(out_dir, f"{clean_name}.bin")
print(f"Exporting {name} ({shape}) to {bin_path}")
with open(bin_path, 'wb') as f:
f.write(struct.pack('ii', shape[0], shape[1]))
f.write(data.detach().numpy().tobytes())
if __name__ == "__main__":
export_fusion_weights('checkpoints-fusion-13k/fused_refiners.pt', 'fusion-ggml-weights-13k')