MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / evaluateTokens

Function evaluateTokens

plugins/wasi_nn/wasinn_bitnet.cpp:1647–1691  ·  view source on GitHub ↗

Evaluate tokens. Construct the batch from tokens and decode.

Source from the content-addressed store, hash-verified

1645
1646// Evaluate tokens. Construct the batch from tokens and decode.
1647ErrNo 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.
1694void clearContext(Graph &GraphRef, Context &CxtRef) noexcept {

Callers 2

evaluateInputFunction · 0.70
sampleOutputFunction · 0.70

Calls 4

fillBatchFunction · 0.70
getMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected