summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzakkor <edward.partenie@gmail.com>2023-11-17 17:36:44 +0200
committerGitHub <noreply@github.com>2023-11-17 17:36:44 +0200
commit2fa02b4b3d86182381311c98b75065ee1b7c2930 (patch)
treeb267f1f721a76afdcdb66230c236a1cbc67b6958
parent2ab0707acbf3a3ca9e5bc5959c7920c22eba2257 (diff)
examples : add tokenize (#4039)
-rw-r--r--examples/CMakeLists.txt1
-rw-r--r--examples/tokenize/CMakeLists.txt5
-rw-r--r--examples/tokenize/tokenize.cpp44
3 files changed, 50 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 75b8df67..71bcb689 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -24,6 +24,7 @@ else()
add_subdirectory(llama-bench)
add_subdirectory(llava)
add_subdirectory(main)
+ add_subdirectory(tokenize)
add_subdirectory(parallel)
add_subdirectory(perplexity)
add_subdirectory(quantize)
diff --git a/examples/tokenize/CMakeLists.txt b/examples/tokenize/CMakeLists.txt
new file mode 100644
index 00000000..5e6654d7
--- /dev/null
+++ b/examples/tokenize/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(TARGET tokenize)
+add_executable(${TARGET} tokenize.cpp)
+install(TARGETS ${TARGET} RUNTIME)
+target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
+target_compile_features(${TARGET} PRIVATE cxx_std_11)
diff --git a/examples/tokenize/tokenize.cpp b/examples/tokenize/tokenize.cpp
new file mode 100644
index 00000000..72b60f9a
--- /dev/null
+++ b/examples/tokenize/tokenize.cpp
@@ -0,0 +1,44 @@
+#include "common.h"
+#include "llama.h"
+
+#include <cmath>
+#include <cstdio>
+#include <string>
+#include <vector>
+
+int main(int argc, char ** argv) {
+ if (argc < 3 || argv[1][0] == '-') {
+ printf("usage: %s MODEL_PATH PROMPT [--ids]\n" , argv[0]);
+ return 1;
+ }
+
+ auto model_path = argv[1];
+ auto prompt = argv[2];
+
+ const bool printing_ids = argc > 3 && std::string(argv[3]) == "--ids";
+
+ llama_backend_init(false);
+
+ llama_model_params model_params = llama_model_default_params();
+ model_params.vocab_only = true;
+ llama_model * model = llama_load_model_from_file(model_path, model_params);
+
+ llama_context_params ctx_params = llama_context_default_params();
+ llama_context * ctx = llama_new_context_with_model(model, ctx_params);
+
+ const bool add_bos = true;
+
+ std::vector<llama_token> tokens;
+
+ tokens = ::llama_tokenize(model, prompt, add_bos, true);
+
+ for (int i = 0; i < (int) tokens.size(); i++) {
+ if (printing_ids) {
+ printf("%d\n", tokens[i]);
+ } else {
+ printf("%6d -> '%s'\n", tokens[i], llama_token_to_piece(ctx, tokens[i]).c_str());
+ }
+ }
+
+ return 0;
+}