Skip to content

Commit 03c0946

Browse files
authored
convert : support models with multiple chat templates (#6588)
* Support converting models with multiple chat templates Adds the following metadata: * tokenizer.chat_templates * tokenizer.chat_template.<name1> * tokenizer.chat_template.<name2> * tokenizer.chat_template.<...> Where `tokenizer.chat_templates` is an array of the template names (except `default`), `default` is added to the regular `tokenizer.chat_template`. * replace filtered characters with underscore * New script to add/modify/remove metadata This scripts creates a copy of a GGUF file and allows you to add/modify/remove metadata in the process. Most importantly this allows you to update chat templates, either as a string or directly from an updated tokenizer_config.json file. * Add files via upload add new script to project/readme * flake--
1 parent e11b2e6 commit 03c0946

File tree

7 files changed

+226
-3
lines changed

7 files changed

+226
-3
lines changed

gguf-py/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pip install gguf
2121

2222
[scripts/gguf-convert-endian.py](https://github.com/ggerganov/llama.cpp/blob/master/gguf-py/scripts/gguf-convert-endian.py) — Allows converting the endianness of GGUF files.
2323

24+
[scripts/gguf-new-metadata.py](https://github.com/ggerganov/llama.cpp/blob/master/gguf-py/scripts/gguf-new-metadata.py) — Copies a GGUF file with added/modified/removed metadata values.
25+
2426
## Development
2527
Maintainers who participate in development of this package are advised to install it in editable mode:
2628

gguf-py/gguf/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class Tokenizer:
9090
HF_JSON = "tokenizer.huggingface.json"
9191
RWKV = "tokenizer.rwkv.world"
9292
CHAT_TEMPLATE = "tokenizer.chat_template"
93+
CHAT_TEMPLATE_N = "tokenizer.chat_template.{name}"
94+
CHAT_TEMPLATES = "tokenizer.chat_templates"
9395
# FIM/Infill special tokens constants
9496
PREFIX_ID = "tokenizer.ggml.prefix_token_id"
9597
SUFFIX_ID = "tokenizer.ggml.suffix_token_id"

gguf-py/gguf/gguf_writer.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import tempfile
77
from enum import Enum, auto
88
from io import BufferedWriter
9-
from typing import IO, Any, Sequence
9+
from typing import IO, Any, Sequence, Mapping
10+
from string import ascii_letters, digits
1011

1112
import numpy as np
1213

@@ -466,7 +467,33 @@ def add_add_eos_token(self, value: bool) -> None:
466467
def add_add_space_prefix(self, value: bool) -> None:
467468
self.add_bool(Keys.Tokenizer.ADD_PREFIX, value)
468469

469-
def add_chat_template(self, value: str) -> None:
470+
def add_chat_template(self, value: str | Sequence[Mapping[str, str]]) -> None:
471+
if isinstance(value, list):
472+
template_default = None
473+
template_names = set()
474+
475+
for choice in value:
476+
name = choice.get('name', '')
477+
template = choice.get('template')
478+
479+
# Allowing non-alphanumerical characters in template name is probably not a good idea, so filter it
480+
name = ''.join((c if c in ascii_letters + digits else '_' for c in name))
481+
482+
if name and template is not None:
483+
if name == 'default':
484+
template_default = template
485+
else:
486+
template_names.add(name)
487+
self.add_string(Keys.Tokenizer.CHAT_TEMPLATE_N.format(name=name), template)
488+
489+
if template_names:
490+
self.add_array(Keys.Tokenizer.CHAT_TEMPLATES, list(template_names))
491+
492+
if template_default is None:
493+
return
494+
495+
value = template_default
496+
470497
self.add_string(Keys.Tokenizer.CHAT_TEMPLATE, value)
471498

472499
def add_prefix_token_id(self, id: int) -> None:

gguf-py/gguf/vocab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _try_load_from_tokenizer_json(self, path: Path) -> bool:
141141
with open(tokenizer_config_file, encoding = 'utf-8') as f:
142142
tokenizer_config = json.load(f)
143143
chat_template = tokenizer_config.get('chat_template')
144-
if chat_template is None or isinstance(chat_template, str):
144+
if chat_template is None or isinstance(chat_template, (str, list)):
145145
self.chat_template = chat_template
146146
else:
147147
print(

gguf-py/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ build-backend = "poetry.core.masonry.api"
3333
gguf-convert-endian = "scripts:gguf_convert_endian_entrypoint"
3434
gguf-dump = "scripts:gguf_dump_entrypoint"
3535
gguf-set-metadata = "scripts:gguf_set_metadata_entrypoint"
36+
gguf-new-metadata = "scripts:gguf_new_metadata_entrypoint"

gguf-py/scripts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
gguf_convert_endian_entrypoint = import_module("scripts.gguf-convert-endian").main
99
gguf_dump_entrypoint = import_module("scripts.gguf-dump").main
1010
gguf_set_metadata_entrypoint = import_module("scripts.gguf-set-metadata").main
11+
gguf_new_metadata_entrypoint = import_module("scripts.gguf-new-metadata").main
1112

1213
del import_module, os

gguf-py/scripts/gguf-new-metadata.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/usr/bin/env python3
2+
import logging
3+
import argparse
4+
import os
5+
import sys
6+
import json
7+
from pathlib import Path
8+
9+
import numpy as np
10+
from typing import Any, Mapping, Sequence
11+
12+
# Necessary to load the local gguf package
13+
if "NO_LOCAL_GGUF" not in os.environ and (Path(__file__).parent.parent.parent / 'gguf-py').exists():
14+
sys.path.insert(0, str(Path(__file__).parent.parent))
15+
16+
import gguf
17+
18+
logger = logging.getLogger("gguf-new-metadata")
19+
20+
21+
def get_byteorder(reader: gguf.GGUFReader) -> gguf.GGUFEndian:
22+
if np.uint32(1) == np.uint32(1).newbyteorder("<"):
23+
# Host is little endian
24+
host_endian = gguf.GGUFEndian.LITTLE
25+
swapped_endian = gguf.GGUFEndian.BIG
26+
else:
27+
# Sorry PDP or other weird systems that don't use BE or LE.
28+
host_endian = gguf.GGUFEndian.BIG
29+
swapped_endian = gguf.GGUFEndian.LITTLE
30+
31+
if reader.byte_order == "S":
32+
return swapped_endian
33+
else:
34+
return host_endian
35+
36+
37+
def decode_field(field: gguf.ReaderField) -> Any:
38+
if field and field.types:
39+
main_type = field.types[0]
40+
41+
if main_type == gguf.GGUFValueType.ARRAY:
42+
sub_type = field.types[-1]
43+
44+
if sub_type == gguf.GGUFValueType.STRING:
45+
return [str(bytes(field.parts[idx]), encoding='utf8') for idx in field.data]
46+
else:
47+
return [pv for idx in field.data for pv in field.parts[idx].tolist()]
48+
if main_type == gguf.GGUFValueType.STRING:
49+
return str(bytes(field.parts[-1]), encoding='utf8')
50+
else:
51+
return field.parts[-1][0]
52+
53+
return None
54+
55+
56+
def get_field_data(reader: gguf.GGUFReader, key: str) -> Any:
57+
field = reader.get_field(key)
58+
59+
return decode_field(field)
60+
61+
62+
def copy_with_new_metadata(reader: gguf.GGUFReader, writer: gguf.GGUFWriter, new_metadata: Mapping[str, str], remove_metadata: Sequence[str]) -> None:
63+
for field in reader.fields.values():
64+
# Suppress virtual fields and fields written by GGUFWriter
65+
if field.name == gguf.Keys.General.ARCHITECTURE or field.name.startswith('GGUF.'):
66+
logger.debug(f'Suppressing {field.name}')
67+
continue
68+
69+
# Skip old chat templates if we have new ones
70+
if field.name.startswith(gguf.Keys.Tokenizer.CHAT_TEMPLATE) and gguf.Keys.Tokenizer.CHAT_TEMPLATE in new_metadata:
71+
logger.debug(f'Skipping {field.name}')
72+
continue
73+
74+
if field.name in remove_metadata:
75+
logger.debug(f'Removing {field.name}')
76+
continue
77+
78+
old_val = decode_field(field)
79+
val = new_metadata.get(field.name, old_val)
80+
81+
if field.name in new_metadata:
82+
logger.debug(f'Modifying {field.name}: "{old_val}" -> "{val}"')
83+
del new_metadata[field.name]
84+
elif val is not None:
85+
logger.debug(f'Copying {field.name}')
86+
87+
if val is not None:
88+
writer.add_key(field.name)
89+
writer.add_val(val, field.types[0])
90+
91+
if gguf.Keys.Tokenizer.CHAT_TEMPLATE in new_metadata:
92+
logger.debug('Adding chat template(s)')
93+
writer.add_chat_template(new_metadata[gguf.Keys.Tokenizer.CHAT_TEMPLATE])
94+
del new_metadata[gguf.Keys.Tokenizer.CHAT_TEMPLATE]
95+
96+
# TODO: Support other types than string?
97+
for key, val in new_metadata.items():
98+
logger.debug(f'Adding {key}: {val}')
99+
writer.add_key(key)
100+
writer.add_val(val, gguf.GGUFValueType.STRING)
101+
102+
for tensor in reader.tensors:
103+
# Dimensions are written in reverse order, so flip them first
104+
shape = np.flipud(tensor.shape)
105+
writer.add_tensor_info(tensor.name, shape, tensor.data.dtype, tensor.data.nbytes, tensor.tensor_type)
106+
107+
writer.write_header_to_file()
108+
writer.write_kv_data_to_file()
109+
writer.write_ti_data_to_file()
110+
111+
for tensor in reader.tensors:
112+
writer.write_tensor_data(tensor.data)
113+
114+
writer.close()
115+
116+
117+
def main() -> None:
118+
parser = argparse.ArgumentParser(description="Make a copy of a GGUF file with new metadata")
119+
parser.add_argument("input", type=Path, help="GGUF format model input filename")
120+
parser.add_argument("output", type=Path, help="GGUF format model output filename")
121+
parser.add_argument("--general-name", type=str, help="The models general.name")
122+
parser.add_argument("--general-description", type=str, help="The models general.description")
123+
parser.add_argument("--chat-template", type=str, help="Chat template string (or JSON string containing templates)")
124+
parser.add_argument("--chat-template-config", type=Path, help="Config file (tokenizer_config.json) containing chat template(s)")
125+
parser.add_argument("--remove-metadata", action="append", type=str, help="Remove metadata (by key name) from output model")
126+
parser.add_argument("--force", action="store_true", help="Bypass warnings without confirmation")
127+
parser.add_argument("--verbose", action="store_true", help="Increase output verbosity")
128+
args = parser.parse_args(None if len(sys.argv) > 2 else ["--help"])
129+
130+
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
131+
132+
new_metadata = {}
133+
remove_metadata = args.remove_metadata or []
134+
135+
if args.general_name:
136+
new_metadata[gguf.Keys.General.NAME] = args.general_name
137+
138+
if args.general_description:
139+
new_metadata[gguf.Keys.General.DESCRIPTION] = args.general_description
140+
141+
if args.chat_template:
142+
new_metadata[gguf.Keys.Tokenizer.CHAT_TEMPLATE] = json.loads(args.chat_template) if args.chat_template.startswith('[') else args.chat_template
143+
144+
if args.chat_template_config:
145+
with open(args.chat_template_config, 'r') as fp:
146+
config = json.load(fp)
147+
template = config.get('chat_template')
148+
if template:
149+
new_metadata[gguf.Keys.Tokenizer.CHAT_TEMPLATE] = template
150+
151+
if remove_metadata:
152+
logger.warning('*** Warning *** Warning *** Warning **')
153+
logger.warning('* Most metadata is required for a fully functional GGUF file,')
154+
logger.warning('* removing crucial metadata may result in a corrupt output file!')
155+
156+
if not args.force:
157+
logger.warning('* Enter exactly YES if you are positive you want to proceed:')
158+
response = input('YES, I am sure> ')
159+
if response != 'YES':
160+
logger.info("You didn't enter YES. Okay then, see ya!")
161+
sys.exit(0)
162+
163+
logger.info(f'* Loading: {args.input}')
164+
reader = gguf.GGUFReader(args.input, 'r')
165+
166+
arch = get_field_data(reader, gguf.Keys.General.ARCHITECTURE)
167+
endianess = get_byteorder(reader)
168+
169+
if os.path.isfile(args.output) and not args.force:
170+
logger.warning('*** Warning *** Warning *** Warning **')
171+
logger.warning(f'* The "{args.output}" GGUF file already exists, it will be overwritten!')
172+
logger.warning('* Enter exactly YES if you are positive you want to proceed:')
173+
response = input('YES, I am sure> ')
174+
if response != 'YES':
175+
logger.info("You didn't enter YES. Okay then, see ya!")
176+
sys.exit(0)
177+
178+
logger.info(f'* Writing: {args.output}')
179+
writer = gguf.GGUFWriter(args.output, arch=arch, endianess=endianess)
180+
181+
alignment = get_field_data(reader, gguf.Keys.General.ALIGNMENT)
182+
if alignment is not None:
183+
logger.debug(f'Setting custom alignment: {alignment}')
184+
writer.data_alignment = alignment
185+
186+
copy_with_new_metadata(reader, writer, new_metadata, remove_metadata)
187+
188+
189+
if __name__ == '__main__':
190+
main()

0 commit comments

Comments
 (0)