| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stddef.h> |
|
|
| #include <random> |
| #include <string> |
| #include <vector> |
|
|
| #include "third_party/gemma_cpp/gemma.h" |
| #include "util/app.h" |
| #include "hwy/base.h" |
| #include "hwy/contrib/thread_pool/thread_pool.h" |
|
|
| std::vector<int> tokenize(const std::string& prompt_string, |
| const gcpp::GemmaTokenizer* tokenizer) { |
| std::string formatted = "<start_of_turn>user\n" + prompt_string + |
| "<end_of_turn>\n<start_of_turn>model\n"; |
| std::vector<int> tokens; |
| HWY_ASSERT(tokenizer->Encode(formatted, &tokens)); |
| tokens.insert(tokens.begin(), BOS_ID); |
| return tokens; |
| } |
|
|
| int main(int argc, char** argv) { |
| gcpp::LoaderArgs loader(argc, argv); |
| gcpp::AppArgs app(argc, argv); |
|
|
| |
| hwy::ThreadPool pool(app.num_threads); |
| gcpp::Gemma model = gcpp::CreateGemma(loader, pool); |
| gcpp::KVCache kv_cache = gcpp::KVCache::Create(loader.ModelType()); |
| size_t pos = 0; |
|
|
| |
| std::mt19937 gen; |
| std::random_device rd; |
| gen.seed(rd()); |
|
|
| |
| std::vector<int> tokens = |
| tokenize("Write a greeting to the world.", model.Tokenizer()); |
| size_t ntokens = tokens.size(); |
|
|
| |
| auto stream_token = [&pos, &ntokens, tokenizer = model.Tokenizer()](int token, |
| float) { |
| ++pos; |
| if (pos < ntokens) { |
| |
| } else if (token != gcpp::EOS_ID) { |
| std::string token_text; |
| HWY_ASSERT(tokenizer->Decode(std::vector<int>{token}, &token_text)); |
| std::cout << token_text << std::flush; |
| } |
| return true; |
| }; |
|
|
| GenerateGemma(model, |
| {.max_tokens = 2048, |
| .max_generated_tokens = 1024, |
| .temperature = 1.0, |
| .verbosity = 0}, |
| tokens, 0, kv_cache, pool, |
| stream_token, gen); |
| } |
|
|