aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGeorgi Gerganov <ggerganov@gmail.com>2023-03-21 17:29:41 +0200
committerGitHub <noreply@github.com>2023-03-21 17:29:41 +0200
commiteb34620aeceaf9d9df7fcb19acc17ad41b9f60f8 (patch)
treeefa10a024845c09ed2630bbe0675618cc7ba0a50 /tests
parent2e664f1ff413995506c9a54f3a8d5b8c64e37a91 (diff)
Add tokenizer test + revert to C++11 (#355)
* Add test-tokenizer-0 to do a few tokenizations - feel free to expand * Added option to convert-pth-to-ggml.py script to dump just the vocabulary * Added ./models/ggml-vocab.bin containing just LLaMA vocab data (used for tests) * Added utility to load vocabulary file from previous point (temporary implementation) * Avoid using std::string_view and drop back to C++11 (hope I didn't break something) * Rename gpt_vocab -> llama_vocab * All CMake binaries go into ./bin/ now
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt4
-rw-r--r--tests/test-tokenizer-0.cpp69
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 0000000..a2c1e3f
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,4 @@
+set(TEST_TARGET test-tokenizer-0)
+add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
+target_link_libraries(${TEST_TARGET} PRIVATE utils)
+add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}> ${CMAKE_CURRENT_SOURCE_DIR}/../models/ggml-vocab.bin)
diff --git a/tests/test-tokenizer-0.cpp b/tests/test-tokenizer-0.cpp
new file mode 100644
index 0000000..6bc49f2
--- /dev/null
+++ b/tests/test-tokenizer-0.cpp
@@ -0,0 +1,69 @@
+#include "utils.h"
+
+#include <cstdio>
+#include <string>
+#include <map>
+
+static const std::map<std::string, std::vector<llama_vocab::id>> k_tests = {
+ { "Hello World", { 1, 10994, 2787, }, },
+ { " Hello World", { 1, 15043, 2787, }, },
+ { " Hello World!", { 1, 15043, 2787, 29991, }, },
+ { " this is 🦙.cpp", { 1, 445, 338, 29871, 243, 162, 169, 156, 29889, 8223, }, },
+ { "w048 7tuijk dsdfhu", { 1, 29893, 29900, 29946, 29947, 29871, 29955, 9161, 13535, 18031, 2176, 6905, }, },
+ { "нещо на Български", { 1, 821, 4851, 665, 1386, 29713, 1305, }, },
+};
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s <vocab-file>\n", argv[0]);
+ return 1;
+ }
+
+ const std::string fname = argv[1];
+
+ fprintf(stderr, "%s : reading vocab from: '%s'\n", __func__, fname.c_str());
+
+ llama_vocab vocab;
+
+ if (!llama_vocab_load(fname, vocab)) {
+ fprintf(stderr, "%s : failed to load vocab from: '%s'\n", __func__, fname.c_str());
+ return 1;
+ }
+
+ const int n_vocab = vocab.id_to_token.size();
+
+ if (n_vocab != 32000) {
+ fprintf(stderr, "%s : expected 32000 tokens, got %d\n", __func__, n_vocab);
+ return 2;
+ }
+
+ for (const auto & test_kv : k_tests) {
+ const auto res = llama_tokenize(vocab, test_kv.first, true);
+
+ bool correct = res.size() == test_kv.second.size();
+
+ for (int i = 0; i < (int) res.size() && correct; ++i) {
+ if (res[i] != test_kv.second[i]) {
+ correct = false;
+ }
+ }
+
+ if (!correct) {
+ fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
+ fprintf(stderr, "%s : expected tokens: ", __func__);
+ for (const auto & t : test_kv.second) {
+ fprintf(stderr, "%6d, ", t);
+ }
+ fprintf(stderr, "\n");
+ fprintf(stderr, "%s : got tokens: ", __func__);
+ for (const auto & t : res) {
+ fprintf(stderr, "%6d, ", t);
+ }
+ fprintf(stderr, "\n");
+
+ return 3;
+ }
+ }
+
+ return 0;
+}