Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
llama_cpp_sampler.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
21#include "llama_cpp_sampler.h"
23
24#include <llama.h>
25
26#include <cstdint>
27#include <memory>
28#include <unordered_map>
29#include <vector>
30
31static auto logger = entropic::log::get("inference.sampler");
32
33namespace entropic {
34
35namespace {
36
54 const llama_vocab* vocab,
55 const std::string& grammar) {
56 if (grammar.empty()) { return; }
58 vocab, grammar.c_str(), "root");
59 if (g) {
61 // gh#95: surface grammar attachment — the issue (and the
62 // tool-staged enforcement gap) had no log to confirm the sampler
63 // actually engaged the constraint.
64 logger->info("Grammar sampler attached ({} bytes)", grammar.size());
65 } else {
66 logger->error("Grammar sampler init FAILED for root rule — output "
67 "will be UNCONSTRAINED. Grammar ({} bytes): {}",
68 grammar.size(), grammar);
69 }
70}
71
83 const llama_vocab* vocab,
84 const std::unordered_map<int32_t, float>& biases) {
85 if (biases.empty()) { return; }
86 std::vector<llama_logit_bias> entries;
87 entries.reserve(biases.size());
88 for (auto& [tok, val] : biases) {
89 entries.push_back({tok, val});
90 }
94 static_cast<int32_t>(entries.size()),
95 entries.data()));
96}
97
123 const llama_vocab* vocab,
124 const GenerationParams& params) {
125 const bool any_penalty = params.repeat_penalty != 1.0f
126 || params.presence_penalty > 0.0f
127 || params.frequency_penalty > 0.0f;
128 if (!any_penalty) { return; }
132 64, params.repeat_penalty,
133 params.frequency_penalty,
134 params.presence_penalty));
135}
136
148 return caller_seed < 0
150 : static_cast<uint32_t>(caller_seed);
151}
152
153} // anonymous namespace
154
155// ── LlamaCppSampler ────────────────────────────────────────
156
164
171 if (chain_) {
172 llama_sampler_free(chain_);
173 chain_ = nullptr;
174 }
175}
176
184 if (chain_ == nullptr || ctx_ == nullptr) { return -1; }
185 return llama_sampler_sample(chain_, ctx_, -1);
186}
187
194 if (chain_ != nullptr) {
195 llama_sampler_reset(chain_);
196 }
197}
198
199// ── LlamaCppSamplerFactory ─────────────────────────────────
200
209
223std::unique_ptr<Sampler> LlamaCppSamplerFactory::create(
225{
229
230 add_grammar_sampler(chain, vocab_, params.grammar);
231 add_logit_bias_sampler(chain, vocab_, params.logit_bias);
232
233 add_penalties_sampler(chain, vocab_, params);
234 if (params.temperature > 0.0f) {
236 llama_sampler_init_temp(params.temperature));
237 }
238 if (params.top_k > 0) {
241 }
242 if (params.top_p < 1.0f) {
245 }
246 // Min-P (gh#23 MVP item 1, v2.3.10) — gated so default 0.0f is no-op
247 if (params.min_p > 0.0f) {
250 }
251
254
255 return std::make_unique<LlamaCppSampler>(chain, ctx_);
256}
257
258} // namespace entropic
LlamaCppSamplerFactory(llama_context *ctx, const llama_vocab *vocab)
Construct with borrowed context + vocab pointers.
std::unique_ptr< Sampler > create(const GenerationParams &params) override
Build the v2.3.10 sampler chain from GenerationParams.
~LlamaCppSampler() override
Free the underlying llama.cpp sampler chain.
LlamaCppSampler(llama_sampler *chain, llama_context *ctx)
Construct with an already-built llama_sampler chain.
int32_t sample() override
Sample one token from the current logits via the wrapped chain.
void reset() override
Reset llama_sampler internal state.
Concrete llama.cpp Sampler + SamplerFactory (v2.3.10 seam impl).
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Activate model on GPU (WARM → ACTIVE).
@ optional
optional: every escape asks the host's path approver
Generation parameters for a single inference call.
Definition config.h:389