ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / prompts / tokenize_huggingface.py
| 1 | import os |
| 2 | from transformers import AutoTokenizer |
| 3 | |
| 4 | os.environ['TOKENIZERS_PARALLELISM'] = "false" |
| 5 | |
| 6 | list_repo_hf = ["databricks/dolly-v2-3b", # dolly-v2 (3b, 7b, 12b models share the same tokenizer) |
| 7 | "gpt2", # gpt-2 (gpt2-xl, gpt2-large share the same tokenizer) |
| 8 | "uer/gpt2-chinese-cluecorpussmall", # gpt-2-chinese |
| 9 | "EleutherAI/gpt-j-6b", # gpt-j |
| 10 | "EleutherAI/gpt-neox-20b", # gpt-neox |
| 11 | "EleutherAI/polyglot-ko-1.3b", # gpt-neox (polyglot-ko 5.8b and 12.8b share the same tokenizer") |
| 12 | "rinna/japanese-gpt-neox-3.6b", # gpt-neox |
| 13 | # mpt-7b (uses gpt-neox-20b tokenizer) |
| 14 | "replit/replit-code-v1-3b", # replit |
| 15 | "bigcode/starcoder", # starcoder (huggingface-cli login required) |
| 16 | "openai/whisper-tiny" # whisper (base, large, large-v2 share the same tokenizer) |
| 17 | ] |
| 18 | |
| 19 | repo2ggml = {"databricks/dolly-v2-3b" : "dolly-v2", |
| 20 | "gpt2" : "gpt-2", |
| 21 | "uer/gpt2-chinese-cluecorpussmall" : "gpt-2-chinese", |
| 22 | "EleutherAI/gpt-j-6b" : "gpt-j", |
| 23 | "EleutherAI/gpt-neox-20b" : "gpt-neox", |
| 24 | "EleutherAI/polyglot-ko-1.3b" : "polyglot-ko", |
| 25 | "rinna/japanese-gpt-neox-3.6b" : "gpt-neox-japanese", |
| 26 | "replit/replit-code-v1-3b" : "replit", |
| 27 | "bigcode/starcoder" : "starcoder", |
| 28 | "openai/whisper-tiny" : "whisper"} |
| 29 | |
| 30 | repo2language = {"databricks/dolly-v2-3b" : "english", |
| 31 | "gpt2" : "english", |
| 32 | "uer/gpt2-chinese-cluecorpussmall" : "chinese", |
| 33 | "EleutherAI/gpt-j-6b" : "english", |
| 34 | "EleutherAI/gpt-neox-20b" : "english", |
| 35 | "EleutherAI/polyglot-ko-1.3b" : "korean", |
| 36 | "rinna/japanese-gpt-neox-3.6b" : "japanese", |
| 37 | "replit/replit-code-v1-3b" : "english", |
| 38 | "bigcode/starcoder" : "english", |
| 39 | "openai/whisper-tiny" : "english"} |
| 40 | |
| 41 | delimeter = ": " |
| 42 | test_sentences = [] |
| 43 | with open("test-cases.txt", "r") as f: |
| 44 | lines = [l.rstrip() for l in f.readlines()] |
| 45 | for l in lines: |
| 46 | if delimeter in l: |
| 47 | language = l[:l.index(delimeter)] |
| 48 | sentence = l[l.index(delimeter) + len(delimeter):] |
| 49 | test_sentences.append((language.lower(), sentence)) |
| 50 | |
| 51 | for repo in list_repo_hf: |
| 52 | |
| 53 | target_language = repo2language[repo] |
| 54 | |
| 55 | tokenizer = AutoTokenizer.from_pretrained(repo, trust_remote_code=True) |
| 56 | |
| 57 | tokens_hf = [] |
| 58 | for language, sentence in test_sentences: |
| 59 | if language == target_language: |
| 60 | tokens = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(sentence)) |
| 61 | tokens_hf.append((sentence, tokens)) |
| 62 | |
| 63 | save_txt = repo2ggml[repo] + ".txt" |
| 64 | with open(save_txt, "w") as f: |
| 65 | f.writelines([sentence + " => " + ",".join(str(t) for t in tokens) + "\n" for sentence, tokens in tokens_hf]) |