Skip to content

support loading vocab from fast tokenizer config in convert.py #3633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f7e377d
Add HFVocab into convert.py
strutive07 Oct 15, 2023
b0e00cb
Update convert.py
strutive07 Oct 15, 2023
f888d2e
Update convert.py
strutive07 Oct 15, 2023
ea9f35f
add bytes_to_unicode function
strutive07 Oct 15, 2023
c7b636e
change add_meta_vocab fucntion
strutive07 Oct 15, 2023
6ec856b
remove debug code
strutive07 Oct 15, 2023
1f16e5f
remove byte_encoder
strutive07 Oct 15, 2023
e876aec
Add newline between classes
strutive07 Oct 15, 2023
1778450
Check tokenizer.json when tokenizer.model is not exist.
strutive07 Oct 15, 2023
a5b26b6
Move transformers dependency to local code
strutive07 Oct 18, 2023
5a1f178
Add error context with 'raise from'
strutive07 Oct 18, 2023
89611cb
Add fast tokenizer option to BpeVocab
strutive07 Oct 23, 2023
97f690a
Merge branch 'master' into convert_hf_vocab
strutive07 Oct 29, 2023
e715442
Update convert.py
strutive07 Oct 29, 2023
d54764d
Add VocabLoader and remove *Vocab class
strutive07 Oct 30, 2023
e19b780
Add transformers dependency
strutive07 Oct 30, 2023
28f09be
remove added tokens and check newline token to decide spm or bpe
strutive07 Nov 5, 2023
4adb8b9
Update convert.py
strutive07 Nov 5, 2023
13f0701
Add special token type
strutive07 Nov 5, 2023
f37a7d7
Update convert.py
strutive07 Nov 11, 2023
9f4dc23
Update convert.py
strutive07 Nov 11, 2023
dcf372e
Update convert.py
strutive07 Nov 11, 2023
cc1f3fc
Fix typo in convert.py
strutive07 Nov 15, 2023
026eb7c
Fix when params.n_vocab < tokenizer vocab size
strutive07 Nov 18, 2023
2e263ca
update vocab class
strutive07 Nov 19, 2023
5ac1949
change funtion name
strutive07 Nov 22, 2023
74d80a8
Merge branch 'master' into convert_hf_vocab
strutive07 Nov 22, 2023
61edd1b
Remove unused variable/functions, add types to class variable and met…
strutive07 Nov 28, 2023
1f5357c
fix flake8 warnings
strutive07 Nov 28, 2023
8fabb01
code style cleanup
cebtenzzre Dec 13, 2023
c3b1c12
make mypy happy
cebtenzzre Dec 13, 2023
35e95b6
change exception
strutive07 Dec 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check tokenizer.json when tokenizer.model is not exist.
  • Loading branch information
strutive07 authored Oct 15, 2023
commit 177845089f03ed5bd528ecd307dee6d4bcbbbdeb
54 changes: 40 additions & 14 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,25 +1102,51 @@ def load_some_model(path: Path) -> ModelPlus:
return model_plus


def vocab_check_and_append_path(path: Path, vocab_file: str) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are not returning a boolean value in this method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the one you pointed out. Thank you.

path2 = path / vocab_file
Copy link

@seungduk-yanolja seungduk-yanolja Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def find_vocab_file_path(path: Path, vocab_file: str) -> Optional[Path]:
    vocab_path = path / vocab_file
    if vocab_path.exists():
        return vocab_path
    vocab_path_parent = path.parent / vocab_file
    if vocab_path_parent.exists():
        return vocab_path_parent
    return None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/ggerganov/llama.cpp/blob/8e672efe632bb6a7333964a255c4b96f018b9a65/convert.py#L1082-L1101

It seems like a good idea to keep the names of the variables the same as they are in the main branch.
I like your suggestion for the function name, so I'll incorporate it.

# Use `.parent` instead of /.. to handle the symlink case better.
path3 = path.parent / vocab_file

if path2.exists():
path = path2
elif path3.exists():
path = path3
else:
path = None

return path



def load_vocab(path: Path, vocabtype: str | None) -> Vocab:
# Be extra-friendly and accept either a file or a directory. Also, if it's
# a directory, it might be the model directory, and tokenizer.model might
# be in the parent of that.
if path.is_dir() and vocabtype != 'hf':
if path.is_dir():
find_candidates = []

vocab_file = "tokenizer.model"
if vocabtype == 'bpe':
if vocabtype == "bpe":
vocab_file = "vocab.json"
path2 = path / vocab_file
# Use `.parent` instead of /.. to handle the symlink case better.
path3 = path.parent / vocab_file
if path2.exists():
path = path2
elif path3.exists():
path = path3

path_candidate = vocab_check_and_append_path(path, vocab_file)
find_candidates.append(vocab_file)

if path_candidate is None:
vocab_file = "tokenizer.json"
hf_path = vocab_check_and_append_path(path, vocab_file)
find_candidates.append(vocab_file)

if vocabtype == "spm" and hf_path is not None:
# A case where there is no tokenizer.model but there is a tokenizer.json and it needs to be loaded into HFVocab.
vocabtype = "hf"
else:
raise FileNotFoundError(
f"Could not find {find_candidates} in {path} or its parent; "
"if it's in another directory, pass the directory as --vocab-dir")
else:
raise FileNotFoundError(
f"Could not find {vocab_file} in {path} or its parent; "
"if it's in another directory, pass the directory as --vocab-dir")
path = path_candidate


print(f"Loading vocab file '{path}', type '{vocabtype}'")

Expand Down Expand Up @@ -1209,7 +1235,7 @@ def main(args_in: list[str] | None = None) -> None:
assert args.outfile, "need --outfile if using --vocab-only"
# FIXME: Try to respect vocab_dir somehow?
vocab = load_vocab(args.vocab_dir or args.model, args.vocabtype)
special_vocab = gguf.SpecialVocab(model_plus.paths[0].parent, load_merges = args.vocabtype in ('bpe', 'hf'))
special_vocab = gguf.SpecialVocab(model_plus.paths[0].parent, load_merges = isinstance(vocab, BpeVocab) or isinstance(vocab, HFVocab))
outfile = args.outfile
OutputFile.write_vocab_only(outfile, params, vocab, special_vocab)
print(f"Wrote {outfile}")
Expand All @@ -1221,7 +1247,7 @@ def main(args_in: list[str] | None = None) -> None:
vocab_dir = args.vocab_dir if args.vocab_dir else model_plus.paths[0].parent
vocab = load_vocab(vocab_dir, args.vocabtype)
# FIXME: Try to respect vocab_dir somehow?
special_vocab = gguf.SpecialVocab(model_plus.paths[0].parent, load_merges = args.vocabtype in ('bpe', 'hf'))
special_vocab = gguf.SpecialVocab(model_plus.paths[0].parent, load_merges = isinstance(vocab, BpeVocab) or isinstance(vocab, HFVocab))

model = model_plus.model
model = convert_model_names(model, params)
Expand Down