| 1257 | } |
| 1258 | |
| 1259 | int llama_context::encode(const llama_batch & batch_inp) { |
| 1260 | GGML_ASSERT((!batch_inp.token && batch_inp.embd) || (batch_inp.token && !batch_inp.embd)); // NOLINT |
| 1261 | |
| 1262 | if (batch_inp.n_tokens == 0) { |
| 1263 | LLAMA_LOG_ERROR("%s: n_tokens == 0\n", __func__); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | const auto & hparams = model.hparams; |
| 1268 | |
| 1269 | const int64_t n_embd = hparams.n_embd_inp(); |
| 1270 | const int64_t n_vocab = model.vocab.n_tokens(); |
| 1271 | |
| 1272 | // note: during encode, we always pass the full sequence starting from pos = 0 |
| 1273 | if (!balloc->init(batch_inp, model.vocab, nullptr, n_embd, cparams.kv_unified ? LLAMA_MAX_SEQ : cparams.n_seq_max, true)) { |
| 1274 | LLAMA_LOG_ERROR("%s: failed to initialize batch\n", __func__); |
| 1275 | return -1; |
| 1276 | } |
| 1277 | |
| 1278 | const uint32_t n_tokens = balloc->get_n_tokens(); |
| 1279 | |
| 1280 | // [TAG_NO_CACHE_PAD] |
| 1281 | // TODO: add new split mode where we pad the input sequences so that ubatch.equal_seqs == true |
| 1282 | const llama_ubatch ubatch = balloc->split_simple(n_tokens); |
| 1283 | |
| 1284 | // micro-batching is not possible for non-causal encoding, so we process the batch in a single shot |
| 1285 | GGML_ASSERT(cparams.n_ubatch >= n_tokens && "encoder requires n_ubatch >= n_tokens"); |
| 1286 | |
| 1287 | if (t_compute_start_us == 0) { |
| 1288 | t_compute_start_us = ggml_time_us(); |
| 1289 | } |
| 1290 | |
| 1291 | // TODO: this clear of the buffer can easily be forgotten - need something better |
| 1292 | embd_seq.clear(); |
| 1293 | |
| 1294 | sched_reserve(); |
| 1295 | |
| 1296 | n_queued_tokens += n_tokens; |
| 1297 | |
| 1298 | // reserve output buffer |
| 1299 | if (output_reserve(n_tokens) < n_tokens) { |
| 1300 | LLAMA_LOG_ERROR("%s: could not reserve space for batch with %u outputs\n", __func__, n_tokens); |
| 1301 | return -2; |
| 1302 | }; |
| 1303 | |
| 1304 | for (uint32_t i = 0; i < n_tokens; ++i) { |
| 1305 | output_ids[i] = i; |
| 1306 | } |
| 1307 | |
| 1308 | n_outputs = n_tokens; |
| 1309 | |
| 1310 | const auto causal_attn_org = cparams.causal_attn; |
| 1311 | |
| 1312 | // always use non-causal attention for encoder graphs |
| 1313 | // TODO: this is a tmp solution until we have a proper way to support enc-dec models |
| 1314 | // ref: https://github.com/ggml-org/llama.cpp/pull/12181#issuecomment-2730451223 |
| 1315 | cparams.causal_attn = false; |
| 1316 |
no test coverage detected