Skip to content

Commit b7b5507

Browse files
committed
py : cleanup the code
- use f-strings where possible - drop first param of encode/decode functions since "utf-8" is the default
1 parent 3df890a commit b7b5507

7 files changed

+44
-28
lines changed

Diff for: convert-ggml-to-pth.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def read_tokens(fin, vocab_size):
2727
text_len = struct.unpack("i", fin.read(4))[0]
2828
text_bytes = fin.read(text_len)
2929
try:
30-
text = text_bytes.decode("utf-8")
30+
text = text_bytes.decode()
3131
except UnicodeDecodeError:
32-
text = text_bytes.decode("utf-8", "replace")
32+
text = text_bytes.decode(errors="replace")
3333
score = struct.unpack("f", fin.read(4))[0]
3434
tokens.append((text, score))
3535
return tokens
@@ -82,7 +82,7 @@ def read_variables(fin):
8282

8383
shape = tuple(struct.unpack("i" * n_dims, fin.read(4 * n_dims)))
8484
shape = shape[::-1]
85-
name = fin.read(name_length).decode("utf-8")
85+
name = fin.read(name_length).decode()
8686

8787
# ensure tensor data is aligned
8888
tensor_data_offset = fin.tell()
@@ -199,19 +199,19 @@ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, stops
199199
device = torch.device("cpu")
200200
llama = llama.to(device)
201201

202-
ctx = """You are AI.
202+
ctx = """You are AI.
203203
This is a dialog, where User interacts with AI. AI is helpful, kind, obedient, honest, respectful, direct, concise, should try to protect User's privacy, and knows its own limits. Also, AI must answer User and AI cannot stop the conversation by itself.
204204
User: Hello, AI.
205205
AI: Hello! How can I assist you today?
206206
"""
207207
print(ctx.rstrip("\n"))
208208
while True:
209209
print("-" * 60)
210-
prompt = input(f"User: ")
210+
prompt = input("User: ")
211211
if ctx != "":
212-
ctx = ctx + "User: " + prompt + "\n"
212+
ctx = f"{ctx}User: {prompt}\n"
213213
else:
214-
ctx = prompt + "\nAI:"
214+
ctx = f"{prompt}\nAI:"
215215

216216
ctx = (ctx[-1920:]) if len(ctx) >= 2048 else ctx
217217

@@ -236,7 +236,7 @@ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, stops
236236
)
237237
s = generation_output.sequences[0]
238238
decoded = tokenizer.decode(s)
239-
ctx = decoded + "\n"
239+
ctx = f"{decoded}\n"
240240

241241

242242
def main():

Diff for: convert-gpt4all-to-ggml.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def write_header(f_out, header):
4949
def write_tokens(fout, tokenizer):
5050
for i in range(tokenizer.vocab_size()):
5151
if tokenizer.is_unknown(i):
52-
text = " \u2047 ".encode("utf-8")
52+
text = " \u2047 ".encode()
5353
elif tokenizer.is_control(i):
5454
text = b""
5555
elif tokenizer.is_byte(i):
@@ -60,13 +60,13 @@ def write_tokens(fout, tokenizer):
6060
byte_value = int(piece[3:-1], 16)
6161
text = struct.pack("B", byte_value)
6262
else:
63-
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode("utf-8")
63+
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode()
6464
fout.write(struct.pack("i", len(text)))
6565
fout.write(text)
6666
fout.write(struct.pack("f", tokenizer.get_score(i)))
6767

6868
# TODO: GPT4All - add extra <pad> token
69-
text = "<pad>".encode("utf-8")
69+
text = "<pad>".encode()
7070
fout.write(struct.pack("i", len(text)))
7171
fout.write(text)
7272
fout.write(struct.pack("f", 0.0))

Diff for: convert-gptq-to-ggml.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# This loop unchanged from convert-pth-to-ggml.py:
5151
for i in range(tokenizer.vocab_size()):
5252
if tokenizer.is_unknown(i):
53-
text = " \u2047 ".encode("utf-8")
53+
text = " \u2047 ".encode()
5454
elif tokenizer.is_control(i):
5555
text = b""
5656
elif tokenizer.is_byte(i):
@@ -61,13 +61,13 @@
6161
byte_value = int(piece[3:-1], 16)
6262
text = struct.pack("B", byte_value)
6363
else:
64-
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode("utf-8")
64+
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode()
6565
fout.write(struct.pack("i", len(text)))
6666
fout.write(text)
6767
fout.write(struct.pack("f", tokenizer.get_score(i)))
6868

6969
def write_header(shape, dst_name, ftype_cur):
70-
sname = dst_name.encode('utf-8')
70+
sname = dst_name.encode()
7171
fout.write(struct.pack("iii", len(shape), len(sname), ftype_cur))
7272
fout.write(struct.pack("i" * len(shape), *shape[::-1]))
7373
fout.write(sname)
@@ -80,7 +80,7 @@ def write_header(shape, dst_name, ftype_cur):
8080
def convert_non_q4(src_name, dst_name):
8181
v = model[src_name]
8282
shape = v.shape
83-
print("Processing non-Q4 variable: " + src_name + " with shape: ", shape, " and type: ", v.dtype)
83+
print(f"Processing non-Q4 variable: {src_name} with shape: {shape} and type: {v.dtype}")
8484
if len(shape) == 1:
8585
print(" Converting to float32")
8686
v = v.to(torch.float32)
@@ -105,7 +105,7 @@ def convert_q4(src_name, dst_name, permute=False):
105105
# Each int32 item is actually 8 int4 items packed together, and it's transposed.
106106
shape = (qweight.shape[0], qweight.shape[1] * 8)
107107

108-
print("Processing Q4 variable: " + src_name + " with shape: ", shape)
108+
print(f"Processing Q4 variable: {src_name} with shape: {shape}")
109109

110110
# The output format has the int4 weights in groups of 32 rather than 8.
111111
# It looks like this:
@@ -168,5 +168,5 @@ def convert_q4(src_name, dst_name, permute=False):
168168

169169
fout.close()
170170

171-
print("Done. Output file: " + fname_out)
172-
print("")
171+
print(f"Done. Output file: {fname_out}")
172+
print()

Diff for: convert-pth-to-ggml.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def write_header(fout, hparams, ftype):
120120
def write_tokens(fout, tokenizer):
121121
for i in range(tokenizer.vocab_size()):
122122
if tokenizer.is_unknown(i):
123-
text = " \u2047 ".encode("utf-8")
123+
text = " \u2047 ".encode()
124124
elif tokenizer.is_control(i):
125125
text = b""
126126
elif tokenizer.is_byte(i):
@@ -131,7 +131,7 @@ def write_tokens(fout, tokenizer):
131131
byte_value = int(piece[3:-1], 16)
132132
text = struct.pack("B", byte_value)
133133
else:
134-
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode("utf-8")
134+
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode()
135135
fout.write(struct.pack("i", len(text)))
136136
fout.write(text)
137137
fout.write(struct.pack("f", tokenizer.get_score(i)))
@@ -191,7 +191,7 @@ def process_and_write_variables(fout, model, ftype, part_id, n_parts):
191191
fullshape = list(partshape)
192192
if n_dims > 1:
193193
fullshape[split_dim] *= n_parts
194-
sname = name.encode('utf-8')
194+
sname = name.encode()
195195
fout.write(struct.pack("iii", n_dims, len(sname), ftype_cur))
196196
for dim in reversed(fullshape):
197197
fout.write(struct.pack("i", dim))
@@ -268,7 +268,25 @@ def main():
268268
process_and_write_variables(fout, model, ftype, part_id, n_parts)
269269
del model
270270

271+
<<<<<<< HEAD
271272
print(f"Done. Output file: {fname_out}\n")
273+
=======
274+
print(f"Processing part {p+1} of {n_parts}\n")
275+
276+
fname_model = f"{dir_model}/consolidated.0{p}.pth"
277+
fname_out = f"{dir_model}/ggml-model-{ftype_str[ftype]}.bin{'' if p == 0 else f'.{p}'}"
278+
279+
model = torch.load(fname_model, map_location="cpu")
280+
281+
with open(fname_out, "wb") as fout:
282+
write_header(fout, hparams, ftype)
283+
write_tokens(fout, tokenizer)
284+
process_and_write_variables(fout, model, ftype)
285+
286+
del model
287+
288+
print(f"Done. Output file: {fname_out}, (part {p})\n")
289+
>>>>>>> 33a6544 (py : cleanup the code)
272290

273291
if __name__ == "__main__":
274292
main()

Diff for: convert-unversioned-ggml-to-ggml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def write_header(f_out, header):
4444
def write_tokens(fout, tokenizer):
4545
for i in range(tokenizer.vocab_size()):
4646
if tokenizer.is_unknown(i):
47-
text = " \u2047 ".encode("utf-8")
47+
text = " \u2047 ".encode()
4848
elif tokenizer.is_control(i):
4949
text = b""
5050
elif tokenizer.is_byte(i):
@@ -55,7 +55,7 @@ def write_tokens(fout, tokenizer):
5555
byte_value = int(piece[3:-1], 16)
5656
text = struct.pack("B", byte_value)
5757
else:
58-
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode("utf-8")
58+
text = tokenizer.id_to_piece(i).replace("\u2581", " ").encode()
5959
fout.write(struct.pack("i", len(text)))
6060
fout.write(text)
6161
fout.write(struct.pack("f", tokenizer.get_score(i)))

Diff for: migrate-ggml-2023-03-30-pr613.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,11 @@ def main():
272272
tokens = read_tokens(fin, hparams)
273273

274274
if hparams['magic'] == 0x67676a74: # ggjt
275-
print("%s: input ggml has already been converted to 'ggjt' magic\n" %
276-
(args.fin_path))
275+
print(f"{args.fin_path}: input ggml has already been converted to 'ggjt' magic\n")
277276
sys.exit(1)
278277

279278
if hparams['magic'] != 0x67676d66: # ggmf
280-
print("%s: input ggml file doesn't have expected 'ggmf' magic: %#x\n" %
281-
(args.fin_path, hparams['magic']))
279+
print(f"{args.fin_path}: input ggml file doesn't have expected 'ggmf' magic: {hparams['magic']:#x}\n")
282280
sys.exit(1)
283281

284282
hparams['magic'] = 0x67676a74 # ggjt

Diff for: quantize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def main():
7575
)
7676

7777
if not os.path.isfile(f16_model_path_base):
78-
print(f'The file %s was not found' % f16_model_path_base)
78+
print(f"The file {f16_model_path_base} was not found")
7979
sys.exit(1)
8080

8181
f16_model_parts_paths = map(

0 commit comments

Comments
 (0)