17
17
# and vocabulary.
18
18
#
19
19
20
+ # Check if torch is installed and show and error and exit if not
20
21
import sys
21
22
import json
22
23
import struct
23
- import numpy as np
24
- import torch
25
24
26
- from sentencepiece import SentencePieceProcessor
25
+ try :
26
+ import torch
27
+ import numpy as np
28
+ from sentencepiece import SentencePieceProcessor
29
+ except ImportError :
30
+ print ("Error: torch, sentencepiece and numpy are required to run this script." )
31
+ print ("Please install using the following command:" )
32
+ print (" pip install torch sentencepiece numpy" )
33
+ sys .exit (1 )
34
+
27
35
28
36
def main ():
29
37
if len (sys .argv ) < 3 :
@@ -35,7 +43,7 @@ def main():
35
43
# output in the same directory as the model
36
44
dir_model = sys .argv [1 ]
37
45
38
- fname_hparams = sys .argv [1 ] + "/params.json"
46
+ fname_hparams = sys .argv [1 ] + "/params.json"
39
47
fname_tokenizer = sys .argv [1 ] + "/../tokenizer.model"
40
48
41
49
def get_n_parts (dim ):
@@ -76,35 +84,35 @@ def get_n_parts(dim):
76
84
n_parts = get_n_parts (hparams ["dim" ])
77
85
78
86
print (hparams )
79
- print (' n_parts = ' , n_parts )
87
+ print (" n_parts = " , n_parts )
80
88
81
89
for p in range (n_parts ):
82
- print (' Processing part ' , p )
90
+ print (" Processing part " , p )
83
91
84
- #fname_model = sys.argv[1] + "/consolidated.00.pth"
92
+ # fname_model = sys.argv[1] + "/consolidated.00.pth"
85
93
fname_model = sys .argv [1 ] + "/consolidated.0" + str (p ) + ".pth"
86
94
fname_out = sys .argv [1 ] + "/ggml-model-" + ftype_str [ftype ] + ".bin"
87
- if ( p > 0 ) :
95
+ if p > 0 :
88
96
fname_out = sys .argv [1 ] + "/ggml-model-" + ftype_str [ftype ] + ".bin" + "." + str (p )
89
97
90
98
# weights_only requires torch 1.13.1, remove this param or update if you get an "invalid keyword argument" error
91
99
model = torch .load (fname_model , map_location = "cpu" , weights_only = True )
92
100
93
101
fout = open (fname_out , "wb" )
94
102
95
- fout .write (struct .pack ("i" , 0x67676d6c )) # magic: ggml in hex
103
+ fout .write (struct .pack ("i" , 0x67676D6C )) # magic: ggml in hex
96
104
fout .write (struct .pack ("i" , hparams ["vocab_size" ]))
97
105
fout .write (struct .pack ("i" , hparams ["dim" ]))
98
106
fout .write (struct .pack ("i" , hparams ["multiple_of" ]))
99
107
fout .write (struct .pack ("i" , hparams ["n_heads" ]))
100
108
fout .write (struct .pack ("i" , hparams ["n_layers" ]))
101
- fout .write (struct .pack ("i" , hparams ["dim" ] // hparams ["n_heads" ])) # rot (obsolete)
109
+ fout .write (struct .pack ("i" , hparams ["dim" ] // hparams ["n_heads" ])) # rot (obsolete)
102
110
fout .write (struct .pack ("i" , ftype ))
103
111
104
112
# Is this correct??
105
113
for i in range (32000 ):
106
114
# TODO: this is probably wrong - not sure how this tokenizer works
107
- text = tokenizer .decode ([29889 , i ]).encode (' utf-8' )
115
+ text = tokenizer .decode ([29889 , i ]).encode (" utf-8" )
108
116
# remove the first byte (it's always '.')
109
117
text = text [1 :]
110
118
fout .write (struct .pack ("i" , len (text )))
@@ -120,16 +128,16 @@ def get_n_parts(dim):
120
128
121
129
print ("Processing variable: " + name + " with shape: " , shape , " and type: " , v .dtype )
122
130
123
- #data = tf.train.load_variable(dir_model, name).squeeze()
131
+ # data = tf.train.load_variable(dir_model, name).squeeze()
124
132
data = v .numpy ().squeeze ()
125
- n_dims = len (data .shape );
133
+ n_dims = len (data .shape )
126
134
127
135
# for efficiency - transpose some matrices
128
136
# "model/h.*/attn/c_attn/w"
129
137
# "model/h.*/attn/c_proj/w"
130
138
# "model/h.*/mlp/c_fc/w"
131
139
# "model/h.*/mlp/c_proj/w"
132
- #if name[-14:] == "/attn/c_attn/w" or \
140
+ # if name[-14:] == "/attn/c_attn/w" or \
133
141
# name[-14:] == "/attn/c_proj/w" or \
134
142
# name[-11:] == "/mlp/c_fc/w" or \
135
143
# name[-13:] == "/mlp/c_proj/w":
@@ -146,11 +154,11 @@ def get_n_parts(dim):
146
154
ftype_cur = 0
147
155
148
156
# header
149
- sname = name .encode (' utf-8' )
157
+ sname = name .encode (" utf-8" )
150
158
fout .write (struct .pack ("iii" , n_dims , len (sname ), ftype_cur ))
151
159
for i in range (n_dims ):
152
160
fout .write (struct .pack ("i" , dshape [n_dims - 1 - i ]))
153
- fout .write (sname );
161
+ fout .write (sname )
154
162
155
163
# data
156
164
data .tofile (fout )
@@ -163,5 +171,6 @@ def get_n_parts(dim):
163
171
print ("Done. Output file: " + fname_out + ", (part " , p , ")" )
164
172
print ("" )
165
173
166
- if __name__ == '__main__' :
174
+
175
+ if __name__ == "__main__" :
167
176
main ()
0 commit comments