Evaluate tokens. Construct the batch from tokens and decode.
| 1645 | |
| 1646 | // Evaluate tokens. Construct the batch from tokens and decode. |
| 1647 | ErrNo evaluateTokens(Span<const llama_token> Tokens, Graph &GraphRef, |
| 1648 | llama_batch &Batch, int &NPos, |
| 1649 | bool IsLogits = false) noexcept { |
| 1650 | // End the inference if the context is full. |
| 1651 | uint32_t NCtx = llama_n_ctx(GraphRef.LlamaContext.get()); |
| 1652 | if (NPos + static_cast<uint32_t>(Tokens.size()) > NCtx) { |
| 1653 | LOG_INFO( |
| 1654 | GraphRef.EnableLog, |
| 1655 | "evaluateTokens: the context if full ({} / {} tokens). Please increase your "sv |
| 1656 | "context size."sv, |
| 1657 | NPos + static_cast<uint32_t>(Tokens.size()), NCtx) |
| 1658 | return ErrNo::ContextFull; |
| 1659 | } |
| 1660 | |
| 1661 | // Loop for decoding batches. Split tokens by batch size. |
| 1662 | for (int I = 0; I < static_cast<int>(Tokens.size()); |
| 1663 | I += static_cast<int>(GraphRef.Params.n_batch)) { |
| 1664 | int NEval = static_cast<int>(Tokens.size()) - I; |
| 1665 | if (NEval > static_cast<int>(GraphRef.Params.n_batch)) { |
| 1666 | NEval = static_cast<int>(GraphRef.Params.n_batch); |
| 1667 | } |
| 1668 | |
| 1669 | // Fill the batch with pos information. |
| 1670 | fillBatch(Span<const llama_token>(Tokens.begin() + I, NEval), GraphRef, |
| 1671 | Batch, NPos, |
| 1672 | IsLogits && I + NEval >= static_cast<int>(Tokens.size())); |
| 1673 | |
| 1674 | // Decode the batch. |
| 1675 | auto Status = llama_decode(GraphRef.LlamaContext.get(), Batch); |
| 1676 | if (Status == 1) { |
| 1677 | RET_ERROR( |
| 1678 | ErrNo::RuntimeError, |
| 1679 | "evaluateTokens: failed to llama_decode: try reducing the size of the batch "sv |
| 1680 | "or increasing the size of context."sv) |
| 1681 | } |
| 1682 | if (Status < 0) { |
| 1683 | RET_ERROR( |
| 1684 | ErrNo::RuntimeError, |
| 1685 | "evaluateTokens: failed to llama_decode: internal fatal error. Please open "sv |
| 1686 | "an issue on GitHub."sv) |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | return ErrNo::Success; |
| 1691 | } |
| 1692 | |
| 1693 | // Clear the context and reset the sampler. |
| 1694 | void clearContext(Graph &GraphRef, Context &CxtRef) noexcept { |
no test coverage detected