2023-11-17 16:36:44 +01:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2023-11-17 17:01:38 +01:00
|
|
|
const char * model_path = argv[1];
|
|
|
|
const char * prompt = argv[2];
|
2023-11-17 16:36:44 +01:00
|
|
|
|
|
|
|
const bool printing_ids = argc > 3 && std::string(argv[3]) == "--ids";
|
|
|
|
|
2024-02-16 10:31:07 +01:00
|
|
|
llama_backend_init();
|
2023-11-17 16:36:44 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::vector<llama_token> tokens;
|
|
|
|
|
2024-04-09 19:44:08 +02:00
|
|
|
tokens = ::llama_tokenize(model, prompt, true, true);
|
2023-11-17 16:36:44 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < (int) tokens.size(); i++) {
|
|
|
|
if (printing_ids) {
|
|
|
|
printf("%d\n", tokens[i]);
|
|
|
|
} else {
|
2023-11-17 17:01:38 +01:00
|
|
|
printf("%6d -> '%s'\n", tokens[i], llama_token_to_piece(ctx, tokens[i]).c_str());
|
2023-11-17 16:36:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|