| 114 | } |
| 115 | |
| 116 | bool decode(const std::map<llama_seq_id, std::string> & prompts) { |
| 117 | GGML_ASSERT(ctx); |
| 118 | |
| 119 | last_batch_info.clear(); |
| 120 | llama_batch batch = llama_batch_init(512, 0, prompts.size()); |
| 121 | |
| 122 | for (const auto & [seq_id, prompt] : prompts) { |
| 123 | std::vector<llama_token> tokens; |
| 124 | tokens.push_back(llama_vocab_bos(vocab)); |
| 125 | |
| 126 | std::vector<llama_token> prompt_tokens(32); |
| 127 | int n_tokens = llama_tokenize(vocab, prompt.c_str(), prompt.length(), |
| 128 | prompt_tokens.data(), prompt_tokens.size(), |
| 129 | false, false); |
| 130 | if (n_tokens < 0) { |
| 131 | fprintf(stderr, "Warning: tokenization failed for seq_id %d\n", seq_id); |
| 132 | llama_batch_free(batch); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | for (int i = 0; i < n_tokens; i++) { |
| 137 | tokens.push_back(prompt_tokens[i]); |
| 138 | } |
| 139 | |
| 140 | if (seq_positions.find(seq_id) == seq_positions.end()) { |
| 141 | seq_positions[seq_id] = 0; |
| 142 | } |
| 143 | |
| 144 | int32_t start_pos = seq_positions[seq_id]; |
| 145 | for (size_t i = 0; i < tokens.size(); i++) { |
| 146 | common_batch_add(batch, tokens[i], start_pos + i, { seq_id }, i == tokens.size() - 1); |
| 147 | } |
| 148 | |
| 149 | seq_positions[seq_id] = start_pos + tokens.size(); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | printf("Batch contents:\n"); |
| 154 | printf("n_tokens: %d\n", batch.n_tokens); |
| 155 | for (int i = 0; i < batch.n_tokens; i++) { |
| 156 | printf("token[%d]: tok=%-5d, pos=%d, n_seq_id=%d, seq_ids=[", i, batch.token[i], batch.pos[i], batch.n_seq_id[i]); |
| 157 | |
| 158 | for (int j = 0; j < batch.n_seq_id[i]; j++) { |
| 159 | printf("%d%s", batch.seq_id[i][j], j < batch.n_seq_id[i]-1 ? ", " : ""); |
| 160 | } |
| 161 | printf("], logits=%d\n", batch.logits[i]); |
| 162 | } |
| 163 | |
| 164 | if (llama_decode(ctx.get(), batch) != 0) { |
| 165 | fprintf(stderr, "Warning: llama_decode failed\n"); |
| 166 | llama_batch_free(batch); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | // Build mapping from seq id to batch token idx |
| 171 | for (int i = 0; i < batch.n_tokens; i++) { |
| 172 | if (batch.logits[i]) { |
| 173 | llama_seq_id seq_id = batch.seq_id[i][0]; |
no test coverage detected