3
3
import re
4
4
import struct
5
5
import sys
6
- from dataclasses import dataclass
7
- from typing import Any , Sequence
6
+ from typing import Any , Dict , Sequence , TextIO
8
7
9
- import numpy as np
10
8
import torch
11
9
12
-
13
- # TODO: import this from convert.py once #545 is merged
14
- @dataclass (frozen = True )
15
- class UnquantizedDataType :
16
- name : str
17
-
18
-
19
- DT_F16 = UnquantizedDataType ("F16" )
20
- DT_F32 = UnquantizedDataType ("F32" )
21
-
22
-
23
- @dataclass (frozen = True )
24
- class QuantizedDataType :
25
- groupsize : int
26
- have_addends : bool
27
- have_g_idx : bool
28
-
29
-
30
- DataType = UnquantizedDataType
31
-
32
- DATA_TYPE_TO_FTYPE : dict [DataType , int ] = {
33
- DT_F32 : 0 ,
34
- DT_F16 : 1 ,
35
- }
36
-
37
- DATA_TYPE_TO_NUMPY : dict [DataType , np .dtype [Any ]] = {
38
- DT_F16 : np .dtype (np .float16 ),
39
- DT_F32 : np .dtype (np .float32 ),
40
- }
41
-
42
- NUMPY_TYPE_TO_DATA_TYPE : dict [np .dtype [Any ], DataType ] = {
43
- dtype : data_type for (data_type , dtype ) in DATA_TYPE_TO_NUMPY .items ()
44
- }
10
+ from convert import DATA_TYPE_TO_FTYPE , NUMPY_TYPE_TO_DATA_TYPE , DataType
45
11
46
12
HF_SUBLAYER_TO_GGML = {
47
13
"self_attn.q_proj" : "attention.wq" ,
@@ -59,7 +25,7 @@ class QuantizedDataType:
59
25
}
60
26
61
27
62
- def translate_tensor_name (t ) :
28
+ def translate_tensor_name (t : str ) -> str :
63
29
match = re .match (r".*layers\.(\d+)\.(\w+\.\w+)\.lora_(A|B)\.weight" , t )
64
30
if match :
65
31
nn = match .group (1 )
@@ -80,13 +46,15 @@ def translate_tensor_name(t):
80
46
sys .exit (1 )
81
47
82
48
83
- def write_file_header (fout , params ) :
49
+ def write_file_header (fout : TextIO , params : Dict [ str , Any ]) -> None :
84
50
fout .write (b"ggla" [::- 1 ]) # magic (ggml lora)
85
51
fout .write (struct .pack ("i" , 1 )) # file version
86
52
fout .write (struct .pack ("ii" , params ["r" ], params ["lora_alpha" ]))
87
53
88
54
89
- def write_tensor_header (self , name : str , shape : Sequence [int ], data_type : 1 ) -> None :
55
+ def write_tensor_header (
56
+ self , name : str , shape : Sequence [int ], data_type : DataType
57
+ ) -> None :
90
58
sname = name .encode ("utf-8" )
91
59
fout .write (
92
60
struct .pack (
0 commit comments