liblloyal 1.0.0
Branched Inference for llama.cpp
Loading...
Searching...
No Matches
logits.hpp
Go to the documentation of this file.
1#pragma once
2
3// SPDX-License-Identifier: Apache-2.0
4// Copyright 2026 Lloyal Labs
5
27#include <llama/llama.h>
28#include <cstring>
29#include <span>
30#include <stdexcept>
31#include <string>
32#include <vector>
33
34#include "decode.hpp"
35#include "kv.hpp"
36
37namespace lloyal::logits {
38
78inline float* get(llama_context* ctx, int32_t index = -1) {
79 if (!ctx) {
80 throw std::runtime_error("logits::get - NULL context");
81 }
82
83 float* ptr = llama_get_logits_ith(ctx, index);
84 if (!ptr) {
85 throw std::runtime_error(
86 "logits::get - Failed to get logits at index " +
87 std::to_string(index) + ". "
88 "Ensure decode() was called with logits=true for this index."
89 );
90 }
91
92 return ptr;
93}
94
108inline void process_chunks(
109 llama_context* ctx,
110 const std::vector<std::span<const llama_token>>& prompts,
111 std::vector<float*>& output,
112 int32_t n_vocab) {
113
114 if (!ctx) throw std::runtime_error("logits::process_chunks - NULL context");
115 if (prompts.size() != output.size())
116 throw std::runtime_error("logits::process_chunks - prompts/output size mismatch");
117 if (prompts.empty()) return;
118
119 const int32_t seq_max = static_cast<int32_t>(llama_n_seq_max(ctx));
120 const int32_t batch_limit = static_cast<int32_t>(llama_n_batch(ctx));
121 const int32_t n = static_cast<int32_t>(prompts.size());
122 thread_local decode::Scratch scratch;
123
124 for (int32_t group_start = 0; group_start < n; group_start += seq_max) {
125 int32_t group_size = std::min(seq_max, n - group_start);
126
127 // bin_pack skips empties internally — pass group slice directly
128 auto chunks = decode::bin_pack(&prompts[group_start], group_size, batch_limit);
129 if (chunks.empty()) continue;
130
131 for (const auto& chunk : chunks) {
132 if (chunk.oversized) {
133 int32_t gi = group_start + chunk.indices[0];
134 llama_seq_id seq = static_cast<llama_seq_id>(chunk.indices[0]);
135
136 if (decode::many(ctx, prompts[gi].data(),
137 static_cast<int32_t>(prompts[gi].size()),
138 0, batch_limit, seq) != 0)
139 throw std::runtime_error("logits::process_chunks - decode::many failed");
140
141 std::memcpy(output[gi], get(ctx, -1), n_vocab * sizeof(float));
142 continue;
143 }
144
145 // Normal chunk — build ScatterItems
146 std::vector<decode::ScatterItem> scatter_items(chunk.indices.size());
147 for (size_t k = 0; k < chunk.indices.size(); ++k) {
148 int32_t gi = group_start + chunk.indices[k];
149 scatter_items[k].tokens = prompts[gi];
150 scatter_items[k].start_pos = 0;
151 scatter_items[k].seq_id = static_cast<llama_seq_id>(chunk.indices[k]);
152 scatter_items[k].output_logits = true;
153 }
154
155 if (decode::scatter(ctx, scatter_items.data(),
156 static_cast<int32_t>(scatter_items.size()),
157 scratch) != 0)
158 throw std::runtime_error("logits::process_chunks - decode::scatter failed");
159
160 // Capture logits
161 int32_t cursor = 0;
162 for (size_t k = 0; k < scatter_items.size(); ++k) {
163 int32_t gi = group_start + chunk.indices[k];
164 int32_t item_n = static_cast<int32_t>(scatter_items[k].tokens.size());
165 std::memcpy(output[gi], get(ctx, cursor + item_n - 1), n_vocab * sizeof(float));
166 cursor += item_n;
167 }
168 }
169
170 // Evict KV for this group's seq_ids (no-op on unused ids)
171 for (int32_t s = 0; s < group_size; ++s) {
172 kv::remove_range(ctx, static_cast<llama_seq_id>(s), 0, -1);
173 }
174 }
175}
176
177} // namespace lloyal::logits
Batch Decoding Operations.
KV Cache Physics.
std::vector< PackedChunk > bin_pack(const std::span< const llama_token > *items, int32_t n, int32_t n_batch)
Greedy first-fit bin-packing of token spans into n_batch-sized chunks.
Definition decode.hpp:480
int many(llama_context *ctx, const llama_token *tokens, int32_t n_tokens, int32_t n_past, int32_t n_batch, llama_seq_id seq_id=0)
Decode multiple tokens into the KV cache with auto-chunking.
Definition decode.hpp:124
int scatter(llama_context *ctx, const ScatterItem *items, int32_t n, Scratch &scratch)
Decode multiple tokens per sequence in a single llama_decode() call.
Definition decode.hpp:395
bool remove_range(llama_context *ctx, llama_seq_id seq, llama_pos p0, llama_pos p1)
Remove token range from KV cache sequence.
Definition kv.hpp:77
float * get(llama_context *ctx, int32_t index=-1)
Definition logits.hpp:78
void process_chunks(llama_context *ctx, const std::vector< std::span< const llama_token > > &prompts, std::vector< float * > &output, int32_t n_vocab)
Process arbitrary number of complete prompts for logit extraction.
Definition logits.hpp:108
Reusable scratch buffers for multi-sequence batch construction.
Definition decode.hpp:290