|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "args.h" |
| 18 | +#include "ggma_api.h" |
| 19 | + |
| 20 | +#include <cstdlib> |
| 21 | +#include <iostream> |
| 22 | +#include <stdexcept> |
| 23 | + |
| 24 | +#define GGMA_ENSURE(a) \ |
| 25 | + do \ |
| 26 | + { \ |
| 27 | + if ((a) != GGMA_STATUS_NO_ERROR) \ |
| 28 | + { \ |
| 29 | + exit(-1); \ |
| 30 | + } \ |
| 31 | + } while (0) |
| 32 | + |
| 33 | +int main(const int argc, char **argv) |
| 34 | +{ |
| 35 | + using namespace ggma_run; |
| 36 | + |
| 37 | + try |
| 38 | + { |
| 39 | + Args args(argc, argv); |
| 40 | + |
| 41 | + std::string prompt = "Lily picked up a flower."; |
| 42 | + constexpr size_t n_tokens_max = 32; |
| 43 | + ggma_token tokens[n_tokens_max]; |
| 44 | + size_t n_tokens; |
| 45 | + |
| 46 | + // Create tokenizer first |
| 47 | + ggma_tokenizer *tokenizer = nullptr; |
| 48 | + GGMA_ENSURE(ggma_create_tokenizer(&tokenizer, (args.packagePath() + "/tokenizer").c_str())); |
| 49 | + |
| 50 | + // Tokenize using the created tokenizer |
| 51 | + GGMA_ENSURE( |
| 52 | + ggma_tokenize(tokenizer, prompt.c_str(), prompt.size(), tokens, n_tokens_max, &n_tokens)); |
| 53 | + |
| 54 | + ggma_context *context = nullptr; |
| 55 | + GGMA_ENSURE(ggma_create_context(&context, args.packagePath().c_str())); |
| 56 | + |
| 57 | + size_t n_predict = 22; |
| 58 | + GGMA_ENSURE(ggma_generate(context, tokens, n_tokens, n_tokens_max, &n_predict)); |
| 59 | + |
| 60 | + // Output generated token IDs |
| 61 | + std::cout << "prompt: " << prompt << std::endl; |
| 62 | + std::cout << "generated: { "; |
| 63 | + for (size_t i = n_tokens; i < n_tokens + n_predict; ++i) |
| 64 | + { |
| 65 | + std::cout << tokens[i]; |
| 66 | + if (i < n_tokens + n_predict - 1) |
| 67 | + { |
| 68 | + std::cout << ", "; |
| 69 | + } |
| 70 | + } |
| 71 | + std::cout << " }" << std::endl; |
| 72 | + |
| 73 | + // Detokenize and output the generated text |
| 74 | + constexpr size_t detokenize_max = 256; |
| 75 | + char detokenized[detokenize_max]; |
| 76 | + GGMA_ENSURE( |
| 77 | + ggma_detokenize(tokenizer, tokens + n_tokens, n_predict, detokenized, detokenize_max)); |
| 78 | + std::cout << "detokenized: " << detokenized << std::endl; |
| 79 | + |
| 80 | + GGMA_ENSURE(ggma_free_context(context)); |
| 81 | + GGMA_ENSURE(ggma_free_tokenizer(tokenizer)); |
| 82 | + } |
| 83 | + catch (std::runtime_error &e) |
| 84 | + { |
| 85 | + std::cerr << "E: Fail to run by runtime error: " << e.what() << std::endl; |
| 86 | + exit(-1); |
| 87 | + } |
| 88 | +} |
0 commit comments