From 846c51feacfc87fbcb2a7f9bc411de714e09256e Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Fri, 29 Sep 2023 17:41:55 -0400 Subject: [PATCH 1/4] convert : use bytes_to_unicode from transformers --- convert-falcon-hf-to-gguf.py | 25 ++----------------------- convert-gptneox-hf-to-gguf.py | 24 ++---------------------- convert-starcoder-hf-to-gguf.py | 25 ++----------------------- 3 files changed, 6 insertions(+), 68 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index 958358563ccdc..9e553657edf7e 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -14,34 +14,13 @@ import numpy as np import torch from transformers import AutoTokenizer # type: ignore[import] +from transformers.models.gpt2 import tokenization_gpt2 # type: ignore[import] if 'NO_LOCAL_GGUF' not in os.environ: sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf -def bytes_to_unicode(): - # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py - """ - Returns list of utf-8 byte and a corresponding list of unicode strings. - The reversible bpe codes work on unicode strings. - This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. - When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. - This is a significant percentage of your normal, say, 32K bpe vocab. - To avoid that, we want lookup tables between utf-8 bytes and unicode strings. - And avoids mapping to whitespace/control characters the bpe code barfs on. - """ - bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) - cs = bs[:] - n = 0 - for b in range(2**8): - if b not in bs: - bs.append(b) - cs.append(2**8+n) - n += 1 - return dict(zip(bs, (chr(n) for n in cs))) - - def count_model_parts(dir_model: Path) -> int: num_parts = 0 for filename in os.listdir(dir_model): @@ -155,7 +134,7 @@ def parse_args() -> argparse.Namespace: tokenizer = AutoTokenizer.from_pretrained(dir_model) reverse_vocab = {id: encoded_tok for encoded_tok, id in tokenizer.vocab.items()} -byte_encoder = bytes_to_unicode() +byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} for i in range(vocab_size): diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index 782410e44f2d1..42413e7484da6 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -14,6 +14,7 @@ import numpy as np import torch from transformers import AutoTokenizer # type: ignore[import] +from transformers.models.gpt2 import tokenization_gpt2 # type: ignore[import] if 'NO_LOCAL_GGUF' not in os.environ: sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) @@ -22,27 +23,6 @@ # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py -def bytes_to_unicode(): - """ - Returns list of utf-8 byte and a corresponding list of unicode strings. - The reversible bpe codes work on unicode strings. - This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. - When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. - This is a significant percentage of your normal, say, 32K bpe vocab. - To avoid that, we want lookup tables between utf-8 bytes and unicode strings. - And avoids mapping to whitespace/control characters the bpe code barfs on. - """ - bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) - cs = bs[:] - n = 0 - for b in range(2**8): - if b not in bs: - bs.append(b) - cs.append(2**8+n) - n += 1 - return dict(zip(bs, (chr(n) for n in cs))) - - def count_model_parts(dir_model: Path) -> int: num_parts = 0 for filename in os.listdir(dir_model): @@ -150,7 +130,7 @@ def parse_args() -> argparse.Namespace: tokenizer = AutoTokenizer.from_pretrained(dir_model) reverse_vocab = {id: encoded_tok for encoded_tok, id in tokenizer.vocab.items()} -byte_encoder = bytes_to_unicode() +byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} for i in range(vocab_size): diff --git a/convert-starcoder-hf-to-gguf.py b/convert-starcoder-hf-to-gguf.py index 48e88a777fea1..9a213a499b351 100755 --- a/convert-starcoder-hf-to-gguf.py +++ b/convert-starcoder-hf-to-gguf.py @@ -14,34 +14,13 @@ import numpy as np import torch from transformers import AutoTokenizer # type: ignore[import] +from transformers.models.gpt2 import tokenization_gpt2 # type: ignore[import] if 'NO_LOCAL_GGUF' not in os.environ: sys.path.insert(1, str(Path(__file__).parent / 'gguf-py' / 'gguf')) import gguf -def bytes_to_unicode(): - # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py - """ - Returns list of utf-8 byte and a corresponding list of unicode strings. - The reversible bpe codes work on unicode strings. - This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. - When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. - This is a significant percentage of your normal, say, 32K bpe vocab. - To avoid that, we want lookup tables between utf-8 bytes and unicode strings. - And avoids mapping to whitespace/control characters the bpe code barfs on. - """ - bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) - cs = bs[:] - n = 0 - for b in range(2**8): - if b not in bs: - bs.append(b) - cs.append(2**8+n) - n += 1 - return dict(zip(bs, (chr(n) for n in cs))) - - def count_model_parts(dir_model: Path) -> int: num_parts = 0 for filename in os.listdir(dir_model): @@ -139,7 +118,7 @@ def parse_args() -> argparse.Namespace: tokenizer = AutoTokenizer.from_pretrained(dir_model) reverse_vocab = {id: encoded_tok for encoded_tok, id in tokenizer.vocab.items()} -byte_encoder = bytes_to_unicode() +byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} for i in range(vocab_size): From 8c8b5b0666d7fe55a765cb826462d0225db8cfd4 Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Fri, 29 Sep 2023 18:00:31 -0400 Subject: [PATCH 2/4] convert : fix handling of added tokens --- convert-falcon-hf-to-gguf.py | 19 ++++++++----------- convert-gptneox-hf-to-gguf.py | 19 ++++++++----------- convert-starcoder-hf-to-gguf.py | 19 ++++++++----------- convert.py | 3 --- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index 9e553657edf7e..f72068dee87d3 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -137,21 +137,18 @@ def parse_args() -> argparse.Namespace: byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} +added_token_ids = tokenizer.get_added_vocab().values() + for i in range(vocab_size): - if i in reverse_vocab: - try: - text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) - except KeyError: - text = bytearray() - for c in reverse_vocab[i]: - if ord(c) < 256: # single byte character - text.append(byte_decoder[ord(c)]) - else: # multibyte special token character - text.extend(c.encode('utf-8')) - else: + if i not in reverse_vocab: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) + else if i in added_tokens: + # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 + text = bytearray(reverse_vocab[i].encode('utf-8')) + else: + text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) tokens.append(text) diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index 42413e7484da6..e47df20ea90d7 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -133,21 +133,18 @@ def parse_args() -> argparse.Namespace: byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} +added_token_ids = tokenizer.get_added_vocab().values() + for i in range(vocab_size): - if i in reverse_vocab: - try: - text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) - except KeyError: - text = bytearray() - for c in reverse_vocab[i]: - if ord(c) < 256: # single byte character - text.append(byte_decoder[ord(c)]) - else: # multibyte special token character - text.extend(c.encode('utf-8')) - else: + if i not in reverse_vocab: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) + else if i in added_tokens: + # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 + text = bytearray(reverse_vocab[i].encode('utf-8')) + else: + text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) tokens.append(text) diff --git a/convert-starcoder-hf-to-gguf.py b/convert-starcoder-hf-to-gguf.py index 9a213a499b351..e1647f8341d18 100755 --- a/convert-starcoder-hf-to-gguf.py +++ b/convert-starcoder-hf-to-gguf.py @@ -121,21 +121,18 @@ def parse_args() -> argparse.Namespace: byte_encoder = tokenization_gpt2.bytes_to_unicode() byte_decoder = {v: k for k, v in byte_encoder.items()} +added_token_ids = tokenizer.get_added_vocab().values() + for i in range(vocab_size): - if i in reverse_vocab: - try: - text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) - except KeyError: - text = bytearray() - for c in reverse_vocab[i]: - if ord(c) < 256: # single byte character - text.append(byte_decoder[ord(c)]) - else: # multibyte special token character - text.extend(c.encode('utf-8')) - else: + if i not in reverse_vocab: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) + else if i in added_tokens: + # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 + text = bytearray(reverse_vocab[i].encode('utf-8')) + else: + text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) tokens.append(text) diff --git a/convert.py b/convert.py index 8bb6c7e410852..b106a30e10446 100755 --- a/convert.py +++ b/convert.py @@ -338,9 +338,6 @@ def __init__(self, fname_tokenizer: Path, fname_added_tokens: Path | None) -> No def bpe_tokens(self) -> Iterable[tuple[bytes, float, gguf.TokenType]]: tokenizer = self.bpe_tokenizer - from transformers.models.gpt2 import tokenization_gpt2 # type: ignore[import] - byte_encoder = tokenization_gpt2.bytes_to_unicode() - byte_decoder = {v: k for k, v in byte_encoder.items()} score = 0.0 for i, item in enumerate(tokenizer): text: bytes = item.encode("utf-8") From f18cfeab62ca426be5d73cdf8547d8cd48bd9cde Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Sun, 1 Oct 2023 17:02:39 -0400 Subject: [PATCH 3/4] s/else if/elif/ --- convert-falcon-hf-to-gguf.py | 2 +- convert-gptneox-hf-to-gguf.py | 2 +- convert-starcoder-hf-to-gguf.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index f72068dee87d3..339c8e4c505f4 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -144,7 +144,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - else if i in added_tokens: + elif i in added_tokens: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index e47df20ea90d7..80b09110fb722 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -140,7 +140,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - else if i in added_tokens: + elif i in added_tokens: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: diff --git a/convert-starcoder-hf-to-gguf.py b/convert-starcoder-hf-to-gguf.py index e1647f8341d18..0e0a1ccc7fcf8 100755 --- a/convert-starcoder-hf-to-gguf.py +++ b/convert-starcoder-hf-to-gguf.py @@ -128,7 +128,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - else if i in added_tokens: + elif i in added_tokens: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: From 02fbbf90990fb8d5903caee9740b0a74b83eff3e Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Sun, 1 Oct 2023 17:04:01 -0400 Subject: [PATCH 4/4] fix typo --- convert-falcon-hf-to-gguf.py | 2 +- convert-gptneox-hf-to-gguf.py | 2 +- convert-starcoder-hf-to-gguf.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/convert-falcon-hf-to-gguf.py b/convert-falcon-hf-to-gguf.py index 339c8e4c505f4..95631e7e26b67 100755 --- a/convert-falcon-hf-to-gguf.py +++ b/convert-falcon-hf-to-gguf.py @@ -144,7 +144,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - elif i in added_tokens: + elif i in added_token_ids: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: diff --git a/convert-gptneox-hf-to-gguf.py b/convert-gptneox-hf-to-gguf.py index 80b09110fb722..1908e6440a404 100755 --- a/convert-gptneox-hf-to-gguf.py +++ b/convert-gptneox-hf-to-gguf.py @@ -140,7 +140,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - elif i in added_tokens: + elif i in added_token_ids: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: diff --git a/convert-starcoder-hf-to-gguf.py b/convert-starcoder-hf-to-gguf.py index 0e0a1ccc7fcf8..e3c06b227e0a6 100755 --- a/convert-starcoder-hf-to-gguf.py +++ b/convert-starcoder-hf-to-gguf.py @@ -128,7 +128,7 @@ def parse_args() -> argparse.Namespace: print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") pad_token = f"[PAD{i}]".encode("utf8") text = bytearray(pad_token) - elif i in added_tokens: + elif i in added_token_ids: # these tokens are not encoded, see https://github.com/huggingface/transformers/issues/1133 text = bytearray(reverse_vocab[i].encode('utf-8')) else: