| 1399 | } |
| 1400 | |
| 1401 | common_context_seq_rm_type common_context_can_seq_rm(llama_context * ctx) { |
| 1402 | auto * mem = llama_get_memory(ctx); |
| 1403 | if (mem == nullptr) { |
| 1404 | return COMMON_CONTEXT_SEQ_RM_TYPE_NO; |
| 1405 | } |
| 1406 | |
| 1407 | common_context_seq_rm_type res = COMMON_CONTEXT_SEQ_RM_TYPE_PART; |
| 1408 | |
| 1409 | llama_memory_clear(mem, true); |
| 1410 | |
| 1411 | // eval 2 tokens to check if the context is compatible |
| 1412 | std::vector<llama_token> tmp; |
| 1413 | tmp.push_back(0); |
| 1414 | tmp.push_back(0); |
| 1415 | |
| 1416 | int ret = llama_decode(ctx, llama_batch_get_one(tmp.data(), tmp.size())); |
| 1417 | if (ret != 0) { |
| 1418 | LOG_ERR("%s: llama_decode() failed: %d\n", __func__, ret); |
| 1419 | res = COMMON_CONTEXT_SEQ_RM_TYPE_NO; |
| 1420 | goto done; |
| 1421 | } |
| 1422 | |
| 1423 | // try to remove the last tokens |
| 1424 | if (!llama_memory_seq_rm(mem, 0, 1, -1)) { |
| 1425 | LOG_WRN("%s: the target context does not support partial sequence removal\n", __func__); |
| 1426 | res = COMMON_CONTEXT_SEQ_RM_TYPE_FULL; |
| 1427 | goto done; |
| 1428 | } |
| 1429 | |
| 1430 | done: |
| 1431 | llama_memory_clear(mem, true); |
| 1432 | llama_synchronize(ctx); |
| 1433 | |
| 1434 | return res; |
| 1435 | } |
| 1436 | |
| 1437 | void common_set_adapter_lora(struct llama_context * ctx, std::vector<common_adapter_lora_info> & lora) { |
| 1438 | std::vector<llama_adapter_lora *> loras; |