forked from skeskinen/bert.cpp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
242a2f8
commit d1f919f
Showing
4 changed files
with
114 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
from ast import arg | ||
from transformers import AutoTokenizer, AutoModel | ||
import tiktoken | ||
import argparse | ||
|
||
tokenizer_name = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1" | ||
# tokenizer_name = "jinaai/jina-embeddings-v2-base-en" | ||
# tokenizer_name = "mymusise/CPM-GPT2" | ||
# tokenizer_name = "gpt2" | ||
# tokenizer_name = "bert-base-chinese" | ||
# tokenizer_name = "BAAI/llm-embedder" | ||
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name) | ||
def main(args): | ||
# tokenizer_name = "sentence-transformers/multi-qa-MiniLM-L6-cos-v1" | ||
if "MiniLM" in args.model_name: | ||
tokenizer_name = f"sentence-transformers/{args.model_name}" | ||
elif "bge-" in args.model_name: | ||
tokenizer_name = f"BAAI/{args.model_name}" | ||
else: | ||
raise ValueError(f"Unknown model name: {args.model_name}") | ||
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name) | ||
|
||
inps = [ | ||
"Hell12o,, world! complexness123", | ||
",大12家好gpt,我是GPT。你好。中國龍", | ||
"你好,世界!", | ||
"こんにちは、世界!", | ||
"syömme \t täällä tänään", | ||
"🙂🙂🙂😒😒😒🍍🍍🍑😗⚜️🕕⛄☃️", | ||
"1231 2431431", | ||
] | ||
with open("examples/test_prompts.txt", "r", encoding="utf-8") as f: | ||
inps = f.readlines() | ||
inps = list(map(lambda x: x.strip(), inps)) | ||
|
||
print("Using tokenizer:", tokenizer_name) | ||
for inp in inps: | ||
oup = tokenizer(inp, return_tensors="pt").input_ids[0].tolist() | ||
print(f"{oup} is {tokenizer.decode(oup)}") | ||
for token in oup: | ||
print(f"{token} <--> {tokenizer.decode([token])}") | ||
print("\n\n") | ||
# print(f"{oup} is {tokenizer.decode(oup)}") | ||
print("Using tokenizer:", tokenizer_name) | ||
output = [] | ||
for inp in inps: | ||
oup = tokenizer(inp, return_tensors="pt").input_ids[0].tolist() | ||
output.append(",".join([str(x) for x in oup])) | ||
for token in oup: | ||
print(f"{token} <--> {tokenizer.decode([token])}") | ||
|
||
# print(f"{inp} is tokenized as {oup}") | ||
# for token in oup: | ||
# print(f"{token} is {tokenizer.decode([token])}") | ||
with open("examples/hf_tokenized_ids.txt", "w", encoding="utf-8") as f: | ||
f.write("\n".join(output)) | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Download original repo files') | ||
parser.add_argument('model_name', help='Name of the repo') | ||
args = parser.parse_args() | ||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Hell12o,, world! complexness123 A a | ||
,大12家好gpt,我是GPT。你好。中國龍 | ||
你好,世界! | ||
こんにちは、世界! | ||
syömme \t täällä tänään | ||
1231 2431431 | ||
Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mkdir build | ||
cd build | ||
cmake .. -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release | ||
make | ||
cd .. | ||
python examples/test_hf_tokenizer.py $1 | ||
build/bin/test_tokenizer -m models/$1/ggml-model-q4_0.bin |