Skip to content

Commit 2a2b801

Browse files
committed
convert.py: add python logging instead of print()
1 parent 75cd4c7 commit 2a2b801

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

convert.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
33

4+
import logging
45
import argparse
56
import concurrent.futures
67
import enum
@@ -637,7 +638,7 @@ def __repr__(self) -> str:
637638

638639

639640
def permute(weights: NDArray, n_head: int, n_head_kv: int) -> NDArray:
640-
# print( "permute debug " + str(weights.shape[0]) + " x " + str(weights.shape[1]) + " nhead " + str(n_head) + " nheadkv " + str(n_kv_head) )
641+
# logging.info( "permute debug " + str(weights.shape[0]) + " x " + str(weights.shape[1]) + " nhead " + str(n_head) + " nheadkv " + str(n_kv_head) )
641642
if n_head_kv is not None and n_head != n_head_kv:
642643
n_head = n_head_kv
643644
return (weights.reshape(n_head, 2, weights.shape[0] // n_head // 2, *weights.shape[1:])
@@ -1026,12 +1027,12 @@ def check_vocab_size(params: Params, vocab: BaseVocab, pad_vocab: bool = False)
10261027

10271028
# Check for a vocab size mismatch
10281029
if params.n_vocab == vocab.vocab_size:
1029-
print("Ignoring added_tokens.json since model matches vocab size without it.")
1030+
logging.warning("Ignoring added_tokens.json since model matches vocab size without it.")
10301031
return
10311032

10321033
if pad_vocab and params.n_vocab > vocab.vocab_size:
10331034
pad_count = params.n_vocab - vocab.vocab_size
1034-
print(
1035+
logging.debug(
10351036
f"Padding vocab with {pad_count} token(s) - <dummy00001> through <dummy{pad_count:05}>"
10361037
)
10371038
for i in range(1, pad_count + 1):
@@ -1159,7 +1160,7 @@ def write_tensor_data(self, ftype: GGMLFileType, model: LazyModel, concurrency:
11591160
elapsed = time.time() - start
11601161
size = ' x '.join(f"{dim:6d}" for dim in lazy_tensor.shape)
11611162
padi = len(str(len(model)))
1162-
print(
1163+
logging.info(
11631164
f"[{i + 1:{padi}d}/{len(model)}] Writing tensor {name:38s} | size {size:16} | type {lazy_tensor.data_type.name:4} | T+{int(elapsed):4}"
11641165
)
11651166
self.gguf.write_tensor_data(ndarray)
@@ -1274,12 +1275,12 @@ def convert_model_names(model: LazyModel, params: Params, skip_unknown: bool) ->
12741275
# HF models permut or pack some of the tensors, so we need to undo that
12751276
for i in itertools.count():
12761277
if f"model.layers.{i}.self_attn.q_proj.weight" in model:
1277-
print(f"Permuting layer {i}")
1278+
logging.debug(f"Permuting layer {i}")
12781279
tmp[f"model.layers.{i}.self_attn.q_proj.weight"] = permute_lazy(model[f"model.layers.{i}.self_attn.q_proj.weight"], params.n_head, params.n_head)
12791280
tmp[f"model.layers.{i}.self_attn.k_proj.weight"] = permute_lazy(model[f"model.layers.{i}.self_attn.k_proj.weight"], params.n_head, params.n_head_kv)
12801281
# tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = model[f"model.layers.{i}.self_attn.v_proj.weight"]
12811282
elif f"model.layers.{i}.self_attn.W_pack.weight" in model:
1282-
print(f"Unpacking and permuting layer {i}")
1283+
logging.debug(f"Unpacking and permuting layer {i}")
12831284
tmp[f"model.layers.{i}.self_attn.q_proj.weight"] = permute_part_lazy(model[f"model.layers.{i}.self_attn.W_pack.weight"], 0, params.n_head, params.n_head)
12841285
tmp[f"model.layers.{i}.self_attn.k_proj.weight"] = permute_part_lazy(model[f"model.layers.{i}.self_attn.W_pack.weight"], 1, params.n_head, params.n_head_kv)
12851286
tmp[f"model.layers.{i}.self_attn.v_proj.weight"] = part_lazy (model[f"model.layers.{i}.self_attn.W_pack.weight"], 2)
@@ -1292,15 +1293,15 @@ def convert_model_names(model: LazyModel, params: Params, skip_unknown: bool) ->
12921293
tensor_type, name_new = tmap.get_type_and_name(name, try_suffixes = (".weight", ".bias")) or (None, None)
12931294
if name_new is None:
12941295
if skip_unknown:
1295-
print(f"Unexpected tensor name: {name} - skipping")
1296+
logging.warning(f"Unexpected tensor name: {name} - skipping")
12961297
continue
12971298
raise ValueError(f"Unexpected tensor name: {name}. Use --skip-unknown to ignore it (e.g. LLaVA)")
12981299

12991300
if tensor_type in should_skip:
1300-
print(f"skipping tensor {name_new}")
1301+
logging.debug(f"skipping tensor {name_new}")
13011302
continue
13021303

1303-
print(f"{name:48s} -> {name_new:40s} | {lazy_tensor.data_type.name:6s} | {lazy_tensor.shape}")
1304+
logging.debug(f"{name:48s} -> {name_new:40s} | {lazy_tensor.data_type.name:6s} | {lazy_tensor.shape}")
13041305
out[name_new] = lazy_tensor
13051306

13061307
return out
@@ -1365,7 +1366,7 @@ def load_some_model(path: Path) -> ModelPlus:
13651366
paths = find_multifile_paths(path)
13661367
models_plus: list[ModelPlus] = []
13671368
for path in paths:
1368-
print(f"Loading model file {path}")
1369+
logging.info(f"Loading model file {path}")
13691370
models_plus.append(lazy_load_file(path))
13701371

13711372
model_plus = merge_multifile_models(models_plus)
@@ -1406,7 +1407,7 @@ def _create_vocab_by_path(self, vocab_types: list[str]) -> Vocab:
14061407
else:
14071408
raise FileNotFoundError(f"Could not find a tokenizer matching any of {vocab_types}")
14081409

1409-
print(f"Loaded vocab file {vocab.fname_tokenizer!r}, type {vocab.name!r}")
1410+
logging.info(f"Loaded vocab file {vocab.fname_tokenizer!r}, type {vocab.name!r}")
14101411
return vocab
14111412

14121413
def load_vocab(self, vocab_types: list[str] | None, model_parent_path: Path) -> tuple[BaseVocab, gguf.SpecialVocab]:
@@ -1466,8 +1467,18 @@ def main(args_in: list[str] | None = None) -> None:
14661467
parser.add_argument("--big-endian", action="store_true", help="model is executed on big endian machine")
14671468
parser.add_argument("--pad-vocab", action="store_true", help="add pad tokens when model vocab expects more than tokenizer metadata provides")
14681469
parser.add_argument("--skip-unknown", action="store_true", help="skip unknown tensor names instead of failing")
1470+
parser.add_argument("--verbose", action="store_true", help="don't convert, just show the default outfile name")
14691471

14701472
args = parser.parse_args(args_in)
1473+
1474+
if args.dump_single or args.dump:
1475+
# Avoid printing anything besides the dump output
1476+
logging.basicConfig(level=logging.CRITICAL)
1477+
elif args.verbose:
1478+
logging.basicConfig(level=logging.DEBUG)
1479+
else:
1480+
logging.basicConfig(level=logging.INFO)
1481+
14711482
if args.no_vocab and args.vocab_only:
14721483
raise ValueError("--vocab-only does not make sense with --no-vocab")
14731484

@@ -1484,6 +1495,7 @@ def main(args_in: list[str] | None = None) -> None:
14841495
if args.dump:
14851496
do_dump_model(model_plus)
14861497
return
1498+
14871499
endianess = gguf.GGUFEndian.LITTLE
14881500
if args.big_endian:
14891501
endianess = gguf.GGUFEndian.BIG
@@ -1506,7 +1518,7 @@ def main(args_in: list[str] | None = None) -> None:
15061518
"q8_0": GGMLFileType.MostlyQ8_0,
15071519
}[args.outtype]
15081520

1509-
print(f"params = {params}")
1521+
logging.info(f"params = {params}")
15101522

15111523
model_parent_path = model_plus.paths[0].parent
15121524
vocab_path = Path(args.vocab_dir or args.model or model_parent_path)
@@ -1521,27 +1533,26 @@ def main(args_in: list[str] | None = None) -> None:
15211533
outfile = args.outfile
15221534
OutputFile.write_vocab_only(outfile, params, vocab, special_vocab,
15231535
endianess=endianess, pad_vocab=args.pad_vocab)
1524-
print(f"Wrote {outfile}")
1536+
logging.info(f"Wrote {outfile}")
15251537
return
15261538

15271539
if model_plus.vocab is not None and args.vocab_dir is None and not args.no_vocab:
15281540
vocab = model_plus.vocab
15291541

1530-
print(f"Vocab info: {vocab}")
1531-
print(f"Special vocab info: {special_vocab}")
1532-
1542+
logging.info(f"Vocab info: {vocab}")
1543+
logging.info(f"Special vocab info: {special_vocab}")
15331544
model = model_plus.model
15341545
model = convert_model_names(model, params, args.skip_unknown)
15351546
ftype = pick_output_type(model, args.outtype)
15361547
model = convert_to_output_type(model, ftype)
15371548
outfile = args.outfile or default_outfile(model_plus.paths, ftype)
15381549

15391550
params.ftype = ftype
1540-
print(f"Writing {outfile}, format {ftype}")
1551+
logging.info(f"Writing {outfile}, format {ftype}")
15411552

15421553
OutputFile.write_all(outfile, ftype, params, model, vocab, special_vocab,
15431554
concurrency=args.concurrency, endianess=endianess, pad_vocab=args.pad_vocab)
1544-
print(f"Wrote {outfile}")
1555+
logging.info(f"Wrote {outfile}")
15451556

15461557

15471558
if __name__ == '__main__':

0 commit comments

Comments
 (0)