Skip to content

Commit 5133cc2

Browse files
committed
Making default output form more like {name}-{parameters}-{quantization}.gguf
1 parent 57d016b commit 5133cc2

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

convert.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,37 @@ def pick_output_type(model: LazyModel, output_type_str: str | None) -> GGMLFileT
12461246
raise Exception(f"Unexpected combination of types: {name_to_type}")
12471247

12481248

1249+
def model_parameter_count(model: LazyModel) -> int:
1250+
total_model_parameters = 0
1251+
for i, (name, lazy_tensor) in enumerate(model.items()):
1252+
sum_weights_in_tensor = 1
1253+
for dim in lazy_tensor.shape:
1254+
sum_weights_in_tensor *= dim
1255+
total_model_parameters += sum_weights_in_tensor
1256+
return total_model_parameters
1257+
1258+
1259+
def model_parameter_count_rounded_notation(model_params_count: int) -> str:
1260+
if model_params_count > 1e12 :
1261+
# Trillions Of Parameters
1262+
scaled_model_params = model_params_count * 1e-12
1263+
scale_suffix = "T"
1264+
elif model_params_count > 1e9 :
1265+
# Billions Of Parameters
1266+
scaled_model_params = model_params_count * 1e-9
1267+
scale_suffix = "B"
1268+
elif model_params_count > 1e6 :
1269+
# Millions Of Parameters
1270+
scaled_model_params = model_params_count * 1e-6
1271+
scale_suffix = "M"
1272+
else:
1273+
# Thousands Of Parameters
1274+
scaled_model_params = model_params_count * 1e-3
1275+
scale_suffix = "K"
1276+
1277+
return f"{round(scaled_model_params)}{scale_suffix}"
1278+
1279+
12491280
def convert_to_output_type(model: LazyModel, output_type: GGMLFileType) -> LazyModel:
12501281
return {name: tensor.astype(output_type.type_for_tensor(name, tensor))
12511282
for (name, tensor) in model.items()}
@@ -1432,13 +1463,20 @@ def load_vocab(
14321463
return vocab, special_vocab
14331464

14341465

1435-
def default_output_file(model_paths: list[Path], file_type: GGMLFileType) -> Path:
1436-
namestr = {
1437-
GGMLFileType.AllF32: "f32",
1466+
def default_outfile(model_paths: list[Path], file_type: GGMLFileType, params: Params, model_params_count: int) -> Path:
1467+
quantization = {
1468+
GGMLFileType.AllF32: "f32",
14381469
GGMLFileType.MostlyF16: "f16",
14391470
GGMLFileType.MostlyQ8_0: "q8_0",
14401471
}[file_type]
1441-
ret = model_paths[0].parent / f"ggml-model-{namestr}.gguf"
1472+
1473+
parameters = model_parameter_count_rounded_notation(model_params_count)
1474+
1475+
name = "ggml-model"
1476+
if params.path_model is not None:
1477+
name = params.path_model.name
1478+
1479+
ret = model_paths[0].parent / f"{name}-{parameters}-{quantization}.gguf"
14421480
if ret in model_paths:
14431481
sys.stderr.write(
14441482
f"Error: Default output path ({ret}) would overwrite the input. "
@@ -1580,6 +1618,9 @@ def main(argv: Optional[list[str]] = None) -> None:
15801618
model={}, paths=[args.model / "dummy"], format="none", vocab=None
15811619
)
15821620

1621+
model_params_count = model_parameter_count(model_plus.model)
1622+
print(f"model parameters count : {model_params_count} ({model_parameter_count_rounded_notation(model_params_count)})")
1623+
15831624
if args.dump:
15841625
do_dump_model(model_plus)
15851626
return
@@ -1631,11 +1672,18 @@ def main(argv: Optional[list[str]] = None) -> None:
16311672
if model_plus.vocab is not None and args.vocab_dir is None:
16321673
vocab = model_plus.vocab
16331674

1634-
model = model_plus.model
1635-
model = convert_model_names(model, params)
1636-
ftype = pick_output_type(model, args.outtype)
1637-
model = convert_to_output_type(model, ftype)
1638-
outfile = args.outfile or default_output_file(model_plus.paths, ftype)
1675+
# FIXME: Try to respect vocab_dir somehow?
1676+
print(f"Vocab info: {vocab}")
1677+
special_vocab = gguf.SpecialVocab(model_plus.paths[0].parent,
1678+
load_merges = True,
1679+
n_vocab = vocab.vocab_size)
1680+
1681+
print(f"Special vocab info: {special_vocab}")
1682+
model = model_plus.model
1683+
model = convert_model_names(model, params)
1684+
ftype = pick_output_type(model, args.outtype)
1685+
model = convert_to_output_type(model, ftype)
1686+
outfile = args.outfile or default_outfile(model_plus.paths, ftype, params, model_params_count)
16391687

16401688
params.ftype = ftype
16411689
print(f"Writing {outfile}, format {ftype}")

0 commit comments

Comments
 (0)