Skip to content

Commit ab2a6bb

Browse files
committed
convert: use token types for BERT special tokens check
1 parent b2c12cf commit ab2a6bb

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

convert_hf_to_gguf.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,18 +5284,14 @@ def set_vocab(self):
52845284
self.gguf_writer.add_token_type_count(self.hparams.get("type_vocab_size", 1))
52855285

52865286
# convert to phantom space vocab
5287-
def phantom(tok):
5288-
if tok.startswith("[") and tok.endswith("]"):
5287+
def phantom(tok, toktype):
5288+
if toktype != gguf.TokenType.NORMAL:
52895289
return tok
52905290
if tok.startswith("##"):
5291-
suffix = tok[2:]
5292-
# If the suffix resembles a special token, keep the original to avoid collision
5293-
# E.g., preserve "##[1]" to avoid conflict with an existing "[1]" token.
5294-
if suffix.startswith("[") and suffix.endswith("]"):
5295-
return tok
5296-
return suffix
5291+
return tok[2:]
52975292
return "\u2581" + tok
5298-
tokens = list(map(phantom, tokens))
5293+
assert len(tokens) == len(toktypes)
5294+
tokens = list(map(phantom, tokens, toktypes))
52995295

53005296
# add vocab to gguf
53015297
self.gguf_writer.add_tokenizer_model("bert")

0 commit comments

Comments
 (0)