bigdl-core-cpp 2.5.0rc1__py3-none-win_amd64.whl → 2.6.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. bigdl/cpp/{convert-hf-to-gguf.py → convert_hf_to_gguf.py} +1673 -278
  2. bigdl/cpp/convert_hf_to_gguf_update.py +381 -0
  3. bigdl/cpp/convert_llama_ggml_to_gguf.py +450 -0
  4. bigdl/cpp/convert_lora_to_gguf.py +461 -0
  5. bigdl/cpp/gguf-py/gguf/__init__.py +1 -1
  6. bigdl/cpp/gguf-py/gguf/constants.py +698 -171
  7. bigdl/cpp/gguf-py/gguf/gguf.py +1 -1
  8. bigdl/cpp/gguf-py/gguf/gguf_reader.py +5 -6
  9. bigdl/cpp/gguf-py/gguf/gguf_writer.py +108 -17
  10. bigdl/cpp/gguf-py/gguf/lazy.py +3 -1
  11. bigdl/cpp/gguf-py/gguf/metadata.py +195 -76
  12. bigdl/cpp/gguf-py/gguf/quants.py +1210 -64
  13. bigdl/cpp/gguf-py/gguf/tensor_mapping.py +262 -43
  14. bigdl/cpp/gguf-py/gguf/utility.py +2 -2
  15. bigdl/cpp/gguf-py/gguf/vocab.py +325 -3
  16. bigdl/cpp/libs/common.lib +0 -0
  17. bigdl/cpp/libs/ggml-base.dll +0 -0
  18. bigdl/cpp/libs/ggml-cpu.dll +0 -0
  19. bigdl/cpp/libs/ggml-sycl.dll +0 -0
  20. bigdl/cpp/libs/ggml.dll +0 -0
  21. bigdl/cpp/libs/libc++.dll +0 -0
  22. bigdl/cpp/libs/llama-batched.exe +0 -0
  23. bigdl/cpp/libs/llama-bench.exe +0 -0
  24. bigdl/cpp/libs/llama-cli.exe +0 -0
  25. bigdl/cpp/libs/llama-embedding.exe +0 -0
  26. bigdl/cpp/libs/llama-gemma3-cli.exe +0 -0
  27. bigdl/cpp/libs/llama-gguf.exe +0 -0
  28. bigdl/cpp/libs/llama-llava-cli.exe +0 -0
  29. bigdl/cpp/libs/llama-lookup.exe +0 -0
  30. bigdl/cpp/libs/llama-ls-sycl-device.exe +0 -0
  31. bigdl/cpp/libs/llama-minicpmv-cli.exe +0 -0
  32. bigdl/cpp/libs/llama-perplexity.exe +0 -0
  33. bigdl/cpp/libs/llama-quantize.exe +0 -0
  34. bigdl/cpp/libs/llama-server.exe +0 -0
  35. bigdl/cpp/libs/llama-simple.exe +0 -0
  36. bigdl/cpp/libs/llama-speculative.exe +0 -0
  37. bigdl/cpp/libs/llama-tokenize.exe +0 -0
  38. bigdl/cpp/libs/llama.dll +0 -0
  39. bigdl/cpp/libs/llava_shared.dll +0 -0
  40. bigdl/cpp/libs/ollama-ggml-base.dll +0 -0
  41. bigdl/cpp/libs/ollama-ggml-cpu.dll +0 -0
  42. bigdl/cpp/libs/ollama-ggml-sycl.dll +0 -0
  43. bigdl/cpp/libs/ollama-lib.exe +0 -0
  44. bigdl/cpp/libs/ollama.exe +0 -0
  45. bigdl/cpp/libs/ollama_ggml.dll +0 -0
  46. bigdl/cpp/libs/ollama_llama.dll +0 -0
  47. bigdl/cpp/libs/ollama_llava_shared.dll +0 -0
  48. {bigdl_core_cpp-2.5.0rc1.data → bigdl_core_cpp-2.6.0.data}/scripts/init-llama-cpp.bat +7 -2
  49. bigdl_core_cpp-2.6.0.data/scripts/init-ollama.bat +16 -0
  50. {bigdl_core_cpp-2.5.0rc1.dist-info → bigdl_core_cpp-2.6.0.dist-info}/METADATA +9 -5
  51. bigdl_core_cpp-2.6.0.dist-info/RECORD +57 -0
  52. {bigdl_core_cpp-2.5.0rc1.dist-info → bigdl_core_cpp-2.6.0.dist-info}/WHEEL +1 -1
  53. bigdl/cpp/convert.py +0 -1714
  54. bigdl/cpp/libs/baby-llama.exe +0 -0
  55. bigdl/cpp/libs/batched-bench.exe +0 -0
  56. bigdl/cpp/libs/batched.exe +0 -0
  57. bigdl/cpp/libs/beam-search.exe +0 -0
  58. bigdl/cpp/libs/benchmark.exe +0 -0
  59. bigdl/cpp/libs/convert-llama2c-to-ggml.exe +0 -0
  60. bigdl/cpp/libs/dist/windows-amd64/ollama_runners/cpu/ollama_llama_server.exe +0 -0
  61. bigdl/cpp/libs/dist/windows-amd64/ollama_runners/cpu_avx/ollama_llama_server.exe +0 -0
  62. bigdl/cpp/libs/dist/windows-amd64/ollama_runners/cpu_avx2/ollama_llama_server.exe +0 -0
  63. bigdl/cpp/libs/embedding.exe +0 -0
  64. bigdl/cpp/libs/export-lora.exe +0 -0
  65. bigdl/cpp/libs/finetune.exe +0 -0
  66. bigdl/cpp/libs/ggml_shared.dll +0 -0
  67. bigdl/cpp/libs/gguf.exe +0 -0
  68. bigdl/cpp/libs/gritlm.exe +0 -0
  69. bigdl/cpp/libs/imatrix.exe +0 -0
  70. bigdl/cpp/libs/infill.exe +0 -0
  71. bigdl/cpp/libs/llava-cli.exe +0 -0
  72. bigdl/cpp/libs/lookahead.exe +0 -0
  73. bigdl/cpp/libs/lookup.exe +0 -0
  74. bigdl/cpp/libs/ls-sycl-device.exe +0 -0
  75. bigdl/cpp/libs/main.exe +0 -0
  76. bigdl/cpp/libs/parallel.exe +0 -0
  77. bigdl/cpp/libs/passkey.exe +0 -0
  78. bigdl/cpp/libs/perplexity.exe +0 -0
  79. bigdl/cpp/libs/q8dot.exe +0 -0
  80. bigdl/cpp/libs/quantize-stats.exe +0 -0
  81. bigdl/cpp/libs/quantize.exe +0 -0
  82. bigdl/cpp/libs/save-load-state.exe +0 -0
  83. bigdl/cpp/libs/server.exe +0 -0
  84. bigdl/cpp/libs/simple.exe +0 -0
  85. bigdl/cpp/libs/speculative.exe +0 -0
  86. bigdl/cpp/libs/tokenize.exe +0 -0
  87. bigdl/cpp/libs/train-text-from-scratch.exe +0 -0
  88. bigdl/cpp/libs/vdot.exe +0 -0
  89. bigdl_core_cpp-2.5.0rc1.data/scripts/init-ollama.bat +0 -13
  90. bigdl_core_cpp-2.5.0rc1.dist-info/RECORD +0 -63
  91. {bigdl_core_cpp-2.5.0rc1.data → bigdl_core_cpp-2.6.0.data}/scripts/init-llama-cpp.ps1 +0 -0
  92. {bigdl_core_cpp-2.5.0rc1.dist-info → bigdl_core_cpp-2.6.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,381 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # This script downloads the tokenizer models of the specified models from Huggingface and
5
+ # generates the get_vocab_base_pre() function for convert_hf_to_gguf.py
6
+ #
7
+ # This is necessary in order to analyze the type of pre-tokenizer used by the model and
8
+ # provide the necessary information to llama.cpp via the GGUF header in order to implement
9
+ # the same pre-tokenizer.
10
+ #
11
+ # ref: https://github.com/ggml-org/llama.cpp/pull/6920
12
+ #
13
+ # Instructions:
14
+ #
15
+ # - Add a new model to the "models" list
16
+ # - Run the script with your huggingface token:
17
+ #
18
+ # python3 convert_hf_to_gguf_update.py <huggingface_token>
19
+ #
20
+ # - The convert_hf_to_gguf.py script will have had its get_vocab_base_pre() function updated
21
+ # - Update llama.cpp with the new pre-tokenizer if necessary
22
+ #
23
+ # TODO: generate tokenizer tests for llama.cpp
24
+ #
25
+
26
+ import logging
27
+ import os
28
+ import pathlib
29
+ import re
30
+
31
+ import requests
32
+ import sys
33
+ import json
34
+ import shutil
35
+
36
+ from hashlib import sha256
37
+ from enum import IntEnum, auto
38
+ from transformers import AutoTokenizer
39
+
40
+ logging.basicConfig(level=logging.DEBUG)
41
+ logger = logging.getLogger("convert_hf_to_gguf_update")
42
+ sess = requests.Session()
43
+
44
+
45
+ class TOKENIZER_TYPE(IntEnum):
46
+ SPM = auto()
47
+ BPE = auto()
48
+ WPM = auto()
49
+ UGM = auto()
50
+
51
+
52
+ # TODO: this string has to exercise as much pre-tokenizer functionality as possible
53
+ # will be updated with time - contributions welcome
54
+ CHK_TXT = '\n \n\n \n\n\n \t \t\t \t\n \n \n \n \n🚀 (normal) 😶‍🌫️ (multiple emojis concatenated) ✅ 🦙🦙 3 33 333 3333 33333 333333 3333333 33333333 3.3 3..3 3...3 កាន់តែពិសេសអាច😁 ?我想在apple工作1314151天~ ------======= нещо на Български \'\'\'\'\'\'```````\"\"\"\"......!!!!!!?????? I\'ve been \'told he\'s there, \'RE you sure? \'M not sure I\'ll make it, \'D you like some tea? We\'Ve a\'lL'
55
+
56
+ if len(sys.argv) == 2:
57
+ token = sys.argv[1]
58
+ if not token.startswith("hf_"):
59
+ logger.info("Huggingface token seems invalid")
60
+ logger.info("Usage: python convert_hf_to_gguf_update.py <huggingface_token>")
61
+ sys.exit(1)
62
+ else:
63
+ logger.info("Usage: python convert_hf_to_gguf_update.py <huggingface_token>")
64
+ sys.exit(1)
65
+
66
+ # TODO: add models here, base models preferred
67
+ models = [
68
+ {"name": "llama-spm", "tokt": TOKENIZER_TYPE.SPM, "repo": "https://huggingface.co/meta-llama/Llama-2-7b-hf", },
69
+ {"name": "llama-bpe", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/meta-llama/Meta-Llama-3-8B", },
70
+ {"name": "phi-3", "tokt": TOKENIZER_TYPE.SPM, "repo": "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct", },
71
+ {"name": "deepseek-llm", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/deepseek-llm-7b-base", },
72
+ {"name": "deepseek-coder", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/deepseek-coder-6.7b-base", },
73
+ {"name": "falcon", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/tiiuae/falcon-7b", },
74
+ {"name": "bert-bge", "tokt": TOKENIZER_TYPE.WPM, "repo": "https://huggingface.co/BAAI/bge-small-en-v1.5", },
75
+ {"name": "falcon3", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/tiiuae/Falcon3-7B-Base", },
76
+ {"name": "bert-bge-large", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/BAAI/bge-large-zh-v1.5", },
77
+ {"name": "mpt", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/mosaicml/mpt-7b", },
78
+ {"name": "starcoder", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/bigcode/starcoder2-3b", },
79
+ {"name": "gpt-2", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/openai-community/gpt2", },
80
+ {"name": "stablelm2", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/stabilityai/stablelm-2-zephyr-1_6b", },
81
+ {"name": "refact", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/smallcloudai/Refact-1_6-base", },
82
+ {"name": "command-r", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/CohereForAI/c4ai-command-r-v01", },
83
+ {"name": "qwen2", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/Qwen/Qwen1.5-7B", },
84
+ {"name": "olmo", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/allenai/OLMo-1.7-7B-hf", },
85
+ {"name": "dbrx", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/databricks/dbrx-base", },
86
+ {"name": "jina-v1-en", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-reranker-v1-tiny-en", },
87
+ {"name": "jina-v2-en", "tokt": TOKENIZER_TYPE.WPM, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-en", }, # WPM!
88
+ {"name": "jina-v2-es", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-es", },
89
+ {"name": "jina-v2-de", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-de", },
90
+ {"name": "smaug-bpe", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/abacusai/Smaug-Llama-3-70B-Instruct", },
91
+ {"name": "poro-chat", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/LumiOpen/Poro-34B-chat", },
92
+ {"name": "jina-v2-code", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jinaai/jina-embeddings-v2-base-code", },
93
+ {"name": "viking", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/LumiOpen/Viking-7B", }, # Also used for Viking 13B and 33B
94
+ {"name": "gemma", "tokt": TOKENIZER_TYPE.SPM, "repo": "https://huggingface.co/google/gemma-2b", },
95
+ {"name": "gemma-2", "tokt": TOKENIZER_TYPE.SPM, "repo": "https://huggingface.co/google/gemma-2-9b", },
96
+ {"name": "jais", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/core42/jais-13b", },
97
+ {"name": "t5", "tokt": TOKENIZER_TYPE.UGM, "repo": "https://huggingface.co/google-t5/t5-small", },
98
+ {"name": "codeshell", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/WisdomShell/CodeShell-7B", },
99
+ {"name": "tekken", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/mistralai/Mistral-Nemo-Base-2407", },
100
+ {"name": "smollm", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/HuggingFaceTB/SmolLM-135M", },
101
+ {'name': "bloom", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/bigscience/bloom", },
102
+ {'name': "gpt3-finnish", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/TurkuNLP/gpt3-finnish-small", },
103
+ {"name": "exaone", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct", },
104
+ {"name": "phi-2", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/microsoft/phi-2", },
105
+ {"name": "chameleon", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/facebook/chameleon-7b", },
106
+ {"name": "minerva-7b", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/sapienzanlp/Minerva-7B-base-v1.0", },
107
+ {"name": "roberta-bpe", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/sentence-transformers/stsb-roberta-base"},
108
+ {"name": "gigachat", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/ai-sage/GigaChat-20B-A3B-instruct"},
109
+ {"name": "megrez", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/Infinigence/Megrez-3B-Instruct"},
110
+ {"name": "deepseek-v3", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/DeepSeek-V3"},
111
+ {"name": "deepseek-r1-qwen", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"},
112
+ ]
113
+
114
+
115
+ def download_file_with_auth(url, token, save_path):
116
+ headers = {"Authorization": f"Bearer {token}"}
117
+ response = sess.get(url, headers=headers)
118
+ response.raise_for_status()
119
+ os.makedirs(os.path.dirname(save_path), exist_ok=True)
120
+ with open(save_path, 'wb') as downloaded_file:
121
+ downloaded_file.write(response.content)
122
+ logger.info(f"File {save_path} downloaded successfully")
123
+
124
+
125
+ def download_model(model):
126
+ name = model["name"]
127
+ repo = model["repo"]
128
+ tokt = model["tokt"]
129
+
130
+ os.makedirs(f"models/tokenizers/{name}", exist_ok=True)
131
+
132
+ files = ["config.json", "tokenizer.json", "tokenizer_config.json"]
133
+
134
+ if tokt == TOKENIZER_TYPE.SPM:
135
+ files.append("tokenizer.model")
136
+
137
+ if tokt == TOKENIZER_TYPE.UGM:
138
+ files.append("spiece.model")
139
+
140
+ if os.path.isdir(repo):
141
+ # If repo is a path on the file system, copy the directory
142
+ for file in files:
143
+ src_path = os.path.join(repo, file)
144
+ dst_path = f"models/tokenizers/{name}/{file}"
145
+ if os.path.isfile(dst_path):
146
+ logger.info(f"{name}: File {dst_path} already exists - skipping")
147
+ continue
148
+ if os.path.isfile(src_path):
149
+ shutil.copy2(src_path, dst_path)
150
+ logger.info(f"{name}: Copied {src_path} to {dst_path}")
151
+ else:
152
+ logger.warning(f"{name}: Source file {src_path} does not exist")
153
+ else:
154
+ # If repo is a URL, download the files
155
+ for file in files:
156
+ save_path = f"models/tokenizers/{name}/{file}"
157
+ if os.path.isfile(save_path):
158
+ logger.info(f"{name}: File {save_path} already exists - skipping")
159
+ continue
160
+ download_file_with_auth(f"{repo}/resolve/main/{file}", token, save_path)
161
+
162
+
163
+ for model in models:
164
+ try:
165
+ download_model(model)
166
+ except Exception as e:
167
+ logger.error(f"Failed to download model {model['name']}. Error: {e}")
168
+
169
+
170
+ # generate the source code for the convert_hf_to_gguf.py:get_vocab_base_pre() function:
171
+
172
+ src_ifs = ""
173
+ for model in models:
174
+ name = model["name"]
175
+ tokt = model["tokt"]
176
+
177
+ if tokt == TOKENIZER_TYPE.SPM or tokt == TOKENIZER_TYPE.UGM:
178
+ continue
179
+
180
+ # Skip if the tokenizer folder does not exist or there are other download issues previously
181
+ if not os.path.exists(f"models/tokenizers/{name}"):
182
+ logger.warning(f"Directory for tokenizer {name} not found. Skipping...")
183
+ continue
184
+
185
+ # create the tokenizer
186
+ try:
187
+ if name == "t5":
188
+ tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}", use_fast=False)
189
+ else:
190
+ tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}")
191
+ except OSError as e:
192
+ logger.error(f"Error loading tokenizer for model {name}. The model may not exist or is not accessible with the provided token. Error: {e}")
193
+ continue # Skip to the next model if the tokenizer can't be loaded
194
+
195
+ chktok = tokenizer.encode(CHK_TXT)
196
+ chkhsh = sha256(str(chktok).encode()).hexdigest()
197
+
198
+ logger.info(f"model: {name}")
199
+ logger.info(f"tokt: {tokt}")
200
+ logger.info(f"repo: {model['repo']}")
201
+ logger.info(f"chktok: {chktok}")
202
+ logger.info(f"chkhsh: {chkhsh}")
203
+
204
+ # print the "pre_tokenizer" content from the tokenizer.json
205
+ with open(f"models/tokenizers/{name}/tokenizer.json", "r", encoding="utf-8") as f:
206
+ cfg = json.load(f)
207
+ normalizer = cfg["normalizer"]
208
+ logger.info("normalizer: " + json.dumps(normalizer, indent=4))
209
+ pre_tokenizer = cfg["pre_tokenizer"]
210
+ logger.info("pre_tokenizer: " + json.dumps(pre_tokenizer, indent=4))
211
+ if "ignore_merges" in cfg["model"]:
212
+ logger.info("ignore_merges: " + json.dumps(cfg["model"]["ignore_merges"], indent=4))
213
+
214
+ logger.info("")
215
+
216
+ src_ifs += f" if chkhsh == \"{chkhsh}\":\n"
217
+ src_ifs += f" # ref: {model['repo']}\n"
218
+ src_ifs += f" res = \"{name}\"\n"
219
+
220
+ src_func = f"""
221
+ def get_vocab_base_pre(self, tokenizer) -> str:
222
+ # encoding this string and hashing the resulting tokens would (hopefully) give us a unique identifier that
223
+ # is specific for the BPE pre-tokenizer used by the model
224
+ # we will use this unique identifier to write a "tokenizer.ggml.pre" entry in the GGUF file which we can
225
+ # use in llama.cpp to implement the same pre-tokenizer
226
+
227
+ chktxt = {repr(CHK_TXT)}
228
+
229
+ chktok = tokenizer.encode(chktxt)
230
+ chkhsh = sha256(str(chktok).encode()).hexdigest()
231
+
232
+ logger.debug(f"chktok: {{chktok}}")
233
+ logger.debug(f"chkhsh: {{chkhsh}}")
234
+
235
+ res = None
236
+
237
+ # NOTE: if you get an error here, you need to update the convert_hf_to_gguf_update.py script
238
+ # or pull the latest version of the model from Huggingface
239
+ # don't edit the hashes manually!
240
+ {src_ifs}
241
+ if res is None:
242
+ logger.warning("\\n")
243
+ logger.warning("**************************************************************************************")
244
+ logger.warning("** WARNING: The BPE pre-tokenizer was not recognized!")
245
+ logger.warning("** There are 2 possible reasons for this:")
246
+ logger.warning("** - the model has not been added to convert_hf_to_gguf_update.py yet")
247
+ logger.warning("** - the pre-tokenization config has changed upstream")
248
+ logger.warning("** Check your model files and convert_hf_to_gguf_update.py and update them accordingly.")
249
+ logger.warning("** ref: https://github.com/ggml-org/llama.cpp/pull/6920")
250
+ logger.warning("**")
251
+ logger.warning(f"** chkhsh: {{chkhsh}}")
252
+ logger.warning("**************************************************************************************")
253
+ logger.warning("\\n")
254
+ raise NotImplementedError("BPE pre-tokenizer was not recognized - update get_vocab_base_pre()")
255
+
256
+ logger.debug(f"tokenizer.ggml.pre: {{repr(res)}}")
257
+ logger.debug(f"chkhsh: {{chkhsh}}")
258
+
259
+ return res
260
+ """
261
+
262
+ convert_py_pth = pathlib.Path("convert_hf_to_gguf.py")
263
+ convert_py = convert_py_pth.read_text(encoding="utf-8")
264
+ convert_py = re.sub(
265
+ r"(# Marker: Start get_vocab_base_pre)(.+?)( +# Marker: End get_vocab_base_pre)",
266
+ lambda m: m.group(1) + src_func + m.group(3),
267
+ convert_py,
268
+ flags=re.DOTALL | re.MULTILINE,
269
+ )
270
+
271
+ convert_py_pth.write_text(convert_py, encoding="utf-8")
272
+
273
+ logger.info("+++ convert_hf_to_gguf.py was updated")
274
+
275
+ # generate tests for each tokenizer model
276
+
277
+ tests = [
278
+ "ied 4 ½ months",
279
+ "Führer",
280
+ "",
281
+ " ",
282
+ " ",
283
+ " ",
284
+ "\t",
285
+ "\n",
286
+ "\n\n",
287
+ "\n\n\n",
288
+ "\t\n",
289
+ "Hello world",
290
+ " Hello world",
291
+ "Hello World",
292
+ " Hello World",
293
+ " Hello World!",
294
+ "Hello, world!",
295
+ " Hello, world!",
296
+ " this is 🦙.cpp",
297
+ "w048 7tuijk dsdfhu",
298
+ "нещо на Български",
299
+ "កាន់តែពិសេសអាចខលចេញ",
300
+ "🚀 (normal) 😶‍🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)",
301
+ "Hello",
302
+ " Hello",
303
+ " Hello",
304
+ " Hello",
305
+ " Hello",
306
+ " Hello\n Hello",
307
+ " (",
308
+ "\n =",
309
+ "' era",
310
+ "Hello, y'all! How are you 😁 ?我想在apple工作1314151天~",
311
+ "!!!!!!",
312
+ "3",
313
+ "33",
314
+ "333",
315
+ "3333",
316
+ "33333",
317
+ "333333",
318
+ "3333333",
319
+ "33333333",
320
+ "333333333",
321
+ "Cửa Việt", # llama-bpe fails on this
322
+ " discards",
323
+ CHK_TXT,
324
+ ]
325
+
326
+ # write the tests to ./models/ggml-vocab-{name}.gguf.inp
327
+ # the format is:
328
+ #
329
+ # test0
330
+ # __ggml_vocab_test__
331
+ # test1
332
+ # __ggml_vocab_test__
333
+ # ...
334
+ #
335
+
336
+ # with each model, encode all tests and write the results in ./models/ggml-vocab-{name}.gguf.out
337
+ # for each test, write the resulting tokens on a separate line
338
+
339
+ for model in models:
340
+ name = model["name"]
341
+ tokt = model["tokt"]
342
+
343
+ # Skip if the tokenizer folder does not exist or there are other download issues previously
344
+ if not os.path.exists(f"models/tokenizers/{name}"):
345
+ logger.warning(f"Directory for tokenizer {name} not found. Skipping...")
346
+ continue
347
+
348
+ # create the tokenizer
349
+ try:
350
+ if name == "t5":
351
+ tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}", use_fast=False)
352
+ else:
353
+ tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}")
354
+ except OSError as e:
355
+ logger.error(f"Failed to load tokenizer for model {name}. Error: {e}")
356
+ continue # Skip this model and continue with the next one in the loop
357
+
358
+ with open(f"models/ggml-vocab-{name}.gguf.inp", "w", encoding="utf-8") as f:
359
+ for text in tests:
360
+ f.write(f"{text}")
361
+ f.write("\n__ggml_vocab_test__\n")
362
+
363
+ with open(f"models/ggml-vocab-{name}.gguf.out", "w") as f:
364
+ for text in tests:
365
+ res = tokenizer.encode(text, add_special_tokens=False)
366
+ for r in res:
367
+ f.write(f" {r}")
368
+ f.write("\n")
369
+
370
+ logger.info(f"Tests for {name} written in ./models/ggml-vocab-{name}.gguf.*")
371
+
372
+ # generate commands for creating vocab files
373
+
374
+ logger.info("\nRun the following commands to generate the vocab files for testing:\n")
375
+
376
+ for model in models:
377
+ name = model["name"]
378
+
379
+ print(f"python3 convert_hf_to_gguf.py models/tokenizers/{name}/ --outfile models/ggml-vocab-{name}.gguf --vocab-only") # noqa: NP100
380
+
381
+ logger.info("\n")