| 627 | } |
| 628 | |
| 629 | static void bark_tokenize_input(struct bark_context* bctx, const std::string& text) { |
| 630 | auto& model = bctx->text_model.semantic_model; |
| 631 | bark_vocab* vocab = &bctx->text_model.vocab; |
| 632 | |
| 633 | auto& params = bctx->params; |
| 634 | |
| 635 | int32_t block_size = model.hparams.block_size; |
| 636 | int32_t max_ctx_size = std::min(block_size, 256); |
| 637 | int32_t n_tokens; |
| 638 | |
| 639 | bark_sequence tokens(max_ctx_size); |
| 640 | bert_tokenize(vocab, text.data(), tokens.data(), &n_tokens, max_ctx_size); |
| 641 | |
| 642 | for (int i = 0; i < (int)tokens.size(); i++) |
| 643 | tokens[i] += params.text_encoding_offset; |
| 644 | |
| 645 | if (n_tokens < max_ctx_size) { |
| 646 | for (int i = n_tokens; i < max_ctx_size; i++) |
| 647 | tokens[i] = params.text_pad_token; |
| 648 | } else if (n_tokens > max_ctx_size) { |
| 649 | fprintf(stderr, "%s: input sequence is too long (%d > 256), truncating sequence", __func__, n_tokens); |
| 650 | } |
| 651 | |
| 652 | tokens.resize(max_ctx_size); |
| 653 | |
| 654 | // semantic history |
| 655 | for (int i = 0; i < 256; i++) |
| 656 | tokens.push_back(params.semantic_pad_token); |
| 657 | tokens.push_back(params.semantic_infer_token); |
| 658 | |
| 659 | assert(tokens.size() == 256 + 256 + 1); |
| 660 | |
| 661 | bctx->tokens = tokens; |
| 662 | |
| 663 | printf("%s: prompt: '%s'\n", __func__, text.c_str()); |
| 664 | printf("%s: number of tokens in prompt = %zu, first 8 tokens: ", __func__, bctx->tokens.size()); |
| 665 | for (int i = 0; i < std::min(8, (int)bctx->tokens.size()); i++) { |
| 666 | printf("%d ", bctx->tokens[i]); |
| 667 | } |
| 668 | printf("\n\n"); |
| 669 | } |
| 670 | |
| 671 | static bool bark_vocab_load(std::ifstream& fin, bark_vocab* vocab) { |
| 672 | int32_t n_vocab; |
no test coverage detected