-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_pytorch_to_ggml.test.py
54 lines (46 loc) · 1.44 KB
/
convert_pytorch_to_ggml.test.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
import os
import struct
import torch
import convert_pytorch_to_ggml
from typing import Dict
def test() -> None:
test_file_path: str = 'convert_pytorch_rwkv_to_ggml_test.tmp'
try:
state_dict: Dict[str, torch.Tensor] = {
'emb.weight': torch.tensor([[1, 2], [3, 4], [5, 6]], dtype=torch.float32),
'blocks.0.ln1.weight': torch.tensor([1], dtype=torch.float32)
}
convert_pytorch_to_ggml.write_state_dict(state_dict, dest_path=test_file_path, data_type='FP32')
with open(test_file_path, 'rb') as test_file:
actual_bytes: bytes = test_file.read()
expected_bytes: bytes = struct.pack(
'=iiiiii' + 'iiiii10sffffff' + 'iiii19sf',
0x67676d66,
101,
3,
2,
1,
0,
# emb.weight
2,
10,
0,
2, 3,
'emb.weight'.encode('utf-8'),
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
# blocks.0.ln1.weight
1,
19,
0,
1,
'blocks.0.ln1.weight'.encode('utf-8'),
1.0
)
assert list(actual_bytes) == list(expected_bytes), f'\nActual: {list(actual_bytes)}\nExpected: {list(expected_bytes)}'
print('All tests pass')
finally:
if os.path.isfile(test_file_path):
os.remove(test_file_path)
if __name__ == "__main__":
test()