| 1179 | } |
| 1180 | |
| 1181 | int llama_context::encode(const llama_batch & batch_inp) { |
| 1182 | GGML_ASSERT((!batch_inp.token && batch_inp.embd) || (batch_inp.token && !batch_inp.embd)); // NOLINT |
| 1183 | |
| 1184 | if (batch_inp.n_tokens == 0) { |
| 1185 | LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | const auto & hparams = model.hparams; |
| 1190 | |
| 1191 | const int64_t n_embd = hparams.n_embd_inp(); |
| 1192 | const int64_t n_vocab = model.vocab.n_tokens(); |
| 1193 | |
| 1194 | // note: during encode, we always pass the full sequence starting from pos = 0 |
| 1195 | if (!balloc->init(batch_inp, model.vocab, nullptr, n_embd, cparams.kv_unified ? LLAMA_MAX_SEQ : cparams.n_seq_max, true)) { |
| 1196 | LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__); |
| 1197 | return -1; |
| 1198 | } |
| 1199 | |
| 1200 | const uint32_t n_tokens = balloc->get_n_tokens(); |
| 1201 | |
| 1202 | // [TAG_NO_CACHE_PAD] |
| 1203 | // TODO: add new split mode where we pad the input sequences so that ubatch.equal_seqs == true |
| 1204 | const llama_ubatch ubatch = balloc->split_simple(n_tokens); |
| 1205 | |
| 1206 | // micro-batching is not possible for non-causal encoding, so we process the batch in a single shot |
| 1207 | GGML_ASSERT(cparams.n_ubatch >= n_tokens && "encoder requires n_ubatch >= n_tokens"); |
| 1208 | |
| 1209 | if (t_compute_start_us == 0) { |
| 1210 | t_compute_start_us = ggml_time_us(); |
| 1211 | } |
| 1212 | |
| 1213 | // TODO: this clear of the buffer can easily be forgotten - need something better |
| 1214 | embd_seq.clear(); |
| 1215 | |
| 1216 | sched_reserve(); |
| 1217 | |
| 1218 | n_queued_tokens += n_tokens; |
| 1219 | |
| 1220 | // reserve output buffer |
| 1221 | if (output_reserve(n_tokens) < n_tokens) { |
| 1222 | LLAMA_LOG_ERROR("%s: could not reserve space for batch with %u outputs\n", __func__, n_tokens); |
| 1223 | return -2; |
| 1224 | }; |
| 1225 | |
| 1226 | for (uint32_t i = 0; i < n_tokens; ++i) { |
| 1227 | output_ids[i] = i; |
| 1228 | } |
| 1229 | |
| 1230 | n_outputs = n_tokens; |
| 1231 | |
| 1232 | const auto causal_attn_org = cparams.causal_attn; |
| 1233 | |
| 1234 | // always use non-causal attention for encoder graphs |
| 1235 | // TODO: this is a tmp solution until we have a proper way to support enc-dec models |
| 1236 | // ref: https://github.com/ggml-org/llama.cpp/pull/12181#issuecomment-2730451223 |
| 1237 | cparams.causal_attn = false; |
| 1238 |
no test coverage detected