The highlighted annotations locate attack entry, possible influence, constraints and observable effects for this concern. They are source-based hypotheses, not a trace of a demonstrated attack. GGUF supplies weights and metadata; llama.cpp builds and executes the computation graph.
Source snapshot: llama.cpp 5266f24da75dc449bd56cbed7addb9c8e4a6a73e. Concrete Llama architecture, ordinary generation path, non-fused attention as configured by this driver. Other architectures and fused kernels can use different graphs. All code below is embedded and expandable; no network or inference is required.
STAGE 01
GGUF → model weights, vocabulary and architecture
GGUF is a model file. The loader reads its metadata and tensors; C++ architecture code constructs the executable graph.
MODEL BASELINE: keep the GGUF identity and inference revision fixed. This concern does not demonstrate an attack on the GGUF loader or a modification of model weights.
562 if (!fname.empty()) {563 // Load the main GGUF564 struct ggml_context * ctx = NULL;565 struct gguf_init_params params = {566 /*.no_alloc = */ true,567 /*.ctx = */ &ctx,568 };569570 metadata_ptr.reset(gguf_init_from_file(fname.c_str(), params));571 metadata = metadata_ptr.get();572 if (metadata == nullptr) {573 throw std::runtime_error(format("%s: failed to load model from %s", __func__, fname.c_str()));574 }575576 get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false);577 llm_kv = LLM_KV(llm_arch_from_string(arch_name));578579 files.emplace_back(new llama_file(fname.c_str(), "rb", use_direct_io));580 contexts.emplace_back(ctx);
llama.cpp implementation
llama.cpp/src/llama.cpp:343–371View code
343344 model->hparams.vocab_only = params.vocab_only;345 model->hparams.no_alloc = params.no_alloc;346347 try {348 model->load_hparams(ml);349 } catch(const std::exception & e) {350 throw std::runtime_error("error loading model hyperparameters: " + std::string(e.what()));351 }352 if (model->arch == LLM_ARCH_CLIP) {353 throw std::runtime_error("CLIP cannot be used as main model, use it with --mmproj instead");354 }355 try {356 model->load_vocab(ml);357 } catch(const std::exception & e) {358 throw std::runtime_error("error loading model vocabulary: " + std::string(e.what()));359 }360361 model->load_stats(ml);362 model->print_info();363364 if (params.vocab_only) {365 LLAMA_LOG_INFO("%s: vocab only - skipping tensors\n", __func__);366 return {0, model_ptr.release()};367 }368369 if (!model->load_tensors(ml)) {370 return {-2, nullptr};371 }
↓
STAGE 02
Rendered text → token IDs
The caller supplies the final prompt. llama_tokenize delegates to the loaded vocabulary; tokenization itself does not execute the transformer.
EXPERIMENT CONTROL: keep rendered input fixed while changing one grammar or sampler stage. A malicious input is a separate variable if the experiment includes one.
Decode → build or reuse graph → set current inputs
process_ubatch applies memory state, builds or reuses the graph, sets new inputs and requests graph computation. Reusing topology still computes the new input.
EXECUTION ENTRY: the current prefix and context state reach the model here. Building or reusing topology is not itself an attack; the relevant influence is identified in the stages below.
1334llm_graph_result * llama_context::process_ubatch(const llama_ubatch & ubatch, llm_graph_type gtype, llama_memory_context_i * mctx, ggml_status & ret) {1335 if (mctx && !mctx->apply()) {1336 LLAMA_LOG_ERROR("%s: failed to apply memory context\n", __func__);1337 ret = GGML_STATUS_FAILED;1338 return nullptr;1339 }13401341 auto * res = gf_res_prev.get();1342 auto * gf = res->get_gf();13431344 // the new graph parameters1345 // in order to correctly reuse a graph, it's full topology has to be uniquely determined by these parameters1346 const auto gparams = graph_params(res, ubatch, mctx, gtype);13471348 if (!graph_reuse_disable && res->can_reuse(gparams)) {1349 //LLAMA_LOG_DEBUG("%s: reusing previous graph\n", __func__);13501351 // with pipeline parallelism, the previous graph_compute_async may still be running1352 // on the GPU. we must synchronize before set_inputs to avoid overwriting input tensors1353 // that the previous compute is still reading.1354 if (cparams.pipeline_parallel) {1355 ggml_backend_sched_synchronize(sched.get());1356 }13571358 n_reused++;1359 } else {1360 res->reset();13611362 ggml_backend_sched_reset(sched.get());1363 ggml_backend_sched_set_eval_callback(sched.get(), cparams.cb_eval, cparams.cb_eval_user_data);13641365 //const auto t_start_us = ggml_time_us();13661367 gf = model.build_graph(gparams);13681369 //LLAMA_LOG_INFO("graph build time: %.3f ms\n", (ggml_time_us() - t_start_us)/1000.0);13701371 if (!gf) {1372 LLAMA_LOG_ERROR("%s: failed to initialize graph\n", __func__);1373 ret = GGML_STATUS_FAILED;1374 return nullptr;1375 }13761377 if (!ggml_backend_sched_alloc_graph(sched.get(), gf)) {1378 LLAMA_LOG_ERROR("%s: failed to allocate graph\n", __func__);1379 ret = GGML_STATUS_ALLOC_FAILED;1380 return nullptr;1381 }1382 }13831384 // set the input data for the input tensors1385 {1386 //const auto t_start_us = ggml_time_us();13871388 // FIXME this call causes a crash if any model inputs were not used in the graph and were therefore not allocated1389 res->set_inputs(&ubatch);13901391 //LLAMA_LOG_INFO("graph set inputs time: %.3f ms\n", (ggml_time_us() - t_start_us)/1000.0);1392 }13931394 const auto status = graph_compute(res->get_gf(), ubatch.n_tokens > 1);1395 if (status != GGML_STATUS_SUCCESS) {1396 LLAMA_LOG_ERROR("%s: failed to compute graph, compute status: %d\n", __func__, status);1397 ret = status;1398 return nullptr;1399 }14001401 ret = GGML_STATUS_SUCCESS;14021403 return res;
llama.cpp implementation
llama.cpp/src/llama-model.cpp:2673–2690View code
2673ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {2674 std::unique_ptr<llm_graph_context> llm = build_arch_graph(params);26752676 // add on pooling layer2677 llm->build_pooling(cls, cls_b, cls_out, cls_out_b, cls_norm);26782679 // add backend sampling layers (if any)2680 llm->build_sampling();26812682 // if the gguf model was converted with --sentence-transformers-dense-modules2683 // there will be two additional dense projection layers2684 // dense linear projections are applied after pooling2685 // TODO: move reranking logic here and generalize2686 llm->build_dense_out(dense_2_out_layers, dense_2_out_layers_b, dense_3_out_layers);26872688 llm->res->set_outputs(params);26892690 return llm->res->get_gf();
Tensor dependency order: embeddings → per-layer attention and FFN → final logits. K/V, attention and FFN repeat for each layer. The backend executes these operations during decode; graph construction alone does not compute their values.
STAGE 04
Token embeddings + positions
Token IDs select rows of the GGUF embedding matrix. The Llama graph supplies positions and enters its transformer-layer loop.
CONTROLLED BASELINE: identical input tokens and positions enter the embedding stage. The deliberately changed grammar or sampler stage is downstream of this driver’s forward pass.
llama.cpp implementation
llama.cpp/src/llama-graph.cpp:2303–2328View code
2303ggml_tensor * llm_graph_context::build_inp_embd(ggml_tensor * tok_embd) const {2304 const int64_t n_embd_inp = hparams.n_embd_inp();2305 const int64_t n_embd = hparams.n_embd;23062307 assert(n_embd_inp >= n_embd);23082309 auto inp = std::make_unique<llm_graph_input_embd>(n_embd_inp);23102311 inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);2312 cb(inp->tokens, "inp_tokens", -1);2313 ggml_set_input(inp->tokens);2314 res->t_inp_tokens = inp->tokens;23152316 inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_inp, ubatch.n_tokens);2317 cb(inp->embd, "inp_embd", -1);2318 ggml_set_input(inp->embd);23192320 // select one of the 2 inputs, based on the batch contents2321 // ref: https://github.com/ggml-org/llama.cpp/pull/185502322 std::array<ggml_tensor *, 2> inps;23232324 // token embeddings path (ubatch.token != nullptr)2325 {2326 auto & cur = inps[0];23272328 cur = ggml_get_rows(ctx0, tok_embd, inp->tokens);
The graph computes Q/K/V, applies RoPE and adds K/V cache writes. Attention reads the available cache with a position mask.
With the same prefix, compare the same context state. Divergent selected tokens will change subsequent K/V state.
llama.cpp implementation
llama.cpp/src/models/llama.cpp:139–161View code
139 // rope freq factors for llama3; may return nullptr for llama2 and other models140 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);141142 // compute Q and K and RoPE them143 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,144 n_embd_head, n_head, n_head_kv, il);145146 Qcur = ggml_rope_ext(147 ctx0, Qcur, inp_pos, rope_factors,148 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,149 ext_factor, attn_factor, beta_fast, beta_slow150 );151152 Kcur = ggml_rope_ext(153 ctx0, Kcur, inp_pos, rope_factors,154 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,155 ext_factor, attn_factor, beta_fast, beta_slow156 );157158 cb(Qcur, "Qcur", il);159 cb(Kcur, "Kcur", il);160 cb(Vcur, "Vcur", il);161
llama.cpp implementation
llama.cpp/src/llama-graph.cpp:2815–2833View code
2815 const auto * mctx_cur = inp->mctx;28162817 // store to KV cache2818 {2819 const auto & k_idxs = inp->get_k_idxs();2820 const auto & v_idxs = inp->get_v_idxs();28212822 ggml_build_forward_expand(gf, mctx_cur->cpy_k(ctx0, k_cur, k_idxs, il));2823 ggml_build_forward_expand(gf, mctx_cur->cpy_v(ctx0, v_cur, v_idxs, il));2824 }28252826 ggml_tensor * kq_mask = inp->get_kq_mask();28272828 ggml_tensor * q = q_cur;2829 ggml_tensor * k = mctx_cur->get_k(ctx0, il);2830 ggml_tensor * v = mctx_cur->get_v(ctx0, il);28312832 ggml_tensor * cur = build_attn_mha(q, k, v, kq_b, kq_mask, sinks, v_mla, 0, kq_scale, il);2833 cb(cur, "kqv_out", il);
↓
STAGE 06
Per layer: QK scores → masked softmax → weighted V
On the non-fused path, kq_soft_max contains attention probabilities. Those weights mix value vectors into the attention output.
Attention belongs to the model’s forward pass. In this driver, grammar filtering happens after decode and does not directly edit these attention rows.
llama.cpp implementation
llama.cpp/src/llama-graph.cpp:2605–2610View code
2605 ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);2606 cb(kq, "kq", il);26072608 // note: this op tends to require high floating point range2609 // while for some models F16 is enough, for others it is not, so we default to F32 here2610 ggml_mul_mat_set_prec(kq, GGML_PREC_F32);
llama.cpp implementation
llama.cpp/src/llama-graph.cpp:2633–2650View code
26332634 if (kq_b) {2635 kq = ggml_add(ctx0, kq, kq_b);2636 cb(kq, "kq_plus_kq_b", il);2637 }26382639 kq = ggml_soft_max_ext(ctx0, kq, kq_mask, kq_scale, hparams.f_max_alibi_bias);2640 ggml_soft_max_add_sinks(kq, sinks);2641 cb(kq, "kq_soft_max", il);26422643 if (!v_trans) {2644 // note: avoid this branch2645 v = ggml_cont(ctx0, ggml_transpose(ctx0, v));2646 cb(v, "v_cont", il);2647 }26482649 ggml_tensor * kqv = ggml_mul_mat(ctx0, v, kq);2650 cb(kqv, "kqv", il);
↓
STAGE 07
Per layer: attention residual → FFN → layer output
Attention output is added to the residual, transformed by the FFN, then added again. l_out feeds the next layer; the layer loop repeats.
Hidden states precede the driver’s sampler chain. Later differences can arise when a different sampled token re-enters the next decode.
Backend executes these graph operations; callbacks observe tensors
The preceding boxes describe tensor dependencies. Numerics execute through this scheduler, which computes graph ranges, synchronizes and invokes requested observation callbacks during execution.
The tensor graph produces logits; this driver applies sampler stages outside that forward pass. Per-stage telemetry distinguishes filtering from raw prediction.
1802 if (!sched->callback_eval) {1803 enum ggml_status ec = ggml_backend_graph_compute_async(split_backend, &split->graph);1804 if (ec != GGML_STATUS_SUCCESS) {1805 return ec;1806 }1807 } else {1808 // similar to ggml_backend_compare_graph_backend1809 for (int j0 = 0; j0 < split->graph.n_nodes; j0++) {1810 struct ggml_tensor * t = split->graph.nodes[j0];18111812 // check if the user needs data from this node1813 bool need = sched->callback_eval(t, true, sched->callback_eval_user_data);18141815 int j1 = j0;18161817 // determine the range [j0, j1] of nodes that can be computed together1818 while (!need && j1 < split->graph.n_nodes - 1) {1819 t = split->graph.nodes[++j1];1820 need = sched->callback_eval(t, true, sched->callback_eval_user_data);1821 }18221823 struct ggml_cgraph gv = ggml_graph_view(&split->graph, j0, j1 + 1);18241825 enum ggml_status ec = ggml_backend_graph_compute_async(split_backend, &gv);1826 if (ec != GGML_STATUS_SUCCESS) {1827 return ec;1828 }18291830 // TODO: pass backend to the callback, then the user can decide if they want to synchronize1831 ggml_backend_synchronize(split_backend);18321833 if (need && !sched->callback_eval(t, false, sched->callback_eval_user_data)) {1834 break;1835 }18361837 j0 = j1;1838 }
Raw logits → grammar / sampler stages → selected token
This driver applies its configured samplers after decode. llama_sampler_apply dispatches each stage; a grammar stage filters the candidate distribution.
PRIMARY CONSTRAINT LOCATION: grammar masks candidate tokens and other stages transform selection. Study syntactically allowed misuse here; grammar syntax does not establish semantic safety.
llama_token_to_piece returns the token bytes. The caller records them and feeds the selected token into the next decode when another prediction is needed.
BEHAVIORAL EVIDENCE: inspect the selected output against both grammar and task requirements. Selection feeds back into the next model pass.
Generation feedback: selected token → next decode → the same forward graph with updated context. The final EOG or token-limit token is recorded without another decode. Attack success must be established from the guide’s behavioral criterion; tensor differences alone do not establish it.
The model’s raw preference can differ from the distribution permitted by a grammar or sampling chain. A constrained output does not demonstrate that the unconstrained model preferred the same choice.
Controlled experiment
Run the same rendered prompt with a documented sampler chain. For a controlled comparison, change only the grammar or one sampler stage. Keep the seed fixed for paired stochastic runs, but do not assume identical seeds guarantee matching outputs after input changes.
Choose a small GBNF grammar with known allowable strings. Inspect both raw logits and each transformed stage so model behavior can be distinguished from enforced output syntax.
Relevant telemetry
Event
What to inspect
sampler_chain
Stage order, grammar text and configured seed.
sampler_distribution
Stage-local entropy, candidate list and selected probability when present.
sampler_filter
Raw-model mass of remaining finite-logit candidates.
logit_distribution, sampling
Raw preference versus terminal selection.
Capture with this codebase
The following is a configuration overlay, not a standalone configuration. Merge it into examples/capture.json, supply a real model and prompt, and compute any region spans from that prompt.
Compare a candidate’s raw probability with its availability after grammar filtering. The retained-mass statistic measures surviving finite-logit candidates in the raw distribution; it is not the amount of probability removed by temperature. Greedy terminal sampling is a point mass even when raw model entropy is high.
Output example
Illustrative event excerpt: omitted envelope and data fields are intentional. These values are not experiment results or detection thresholds. Complete event envelopes are documented in the implementation guide.
{
"kind": "sampler_filter",
"data": {
"stage": "grammar",
"raw_model_mass_retained": 0.08,
"interpretation": "remaining finite candidates measured in raw model distribution"
}
}
Implementation source
Expand a file-and-line reference to read the actual source. These excerpts are embedded snapshots from this checkout. They show the measurement implementation; the experiments and output examples above remain illustrative.
Apply sampler stages and record retained raw probability mass
src/cst_capture.cpp:188–218View code
188 const float * raw=llama_get_logits_ith(ctx.get(),-1);if(!raw)throw std::runtime_error("missing logits");189 double raw_max=*std::max_element(raw,raw+nv),raw_z=0;190 for(int i=0;i<nv;++i)raw_z+=std::exp((double)raw[i]-raw_max);191 std::vector<llama_token_data> buffer(nv);for(int i=0;i<nv;++i)buffer[i]={i,raw[i],0};192 llama_token_data_array dist{buffer.data(),buffer.size(),-1,false};193 start=Clock::now();194 for(size_t s=0;s<samplers.size();++s){195 llama_sampler_apply(samplers[s].get(),&dist);196 if(!dist.size)throw std::runtime_error("sampler removed all candidates");197 bool any_finite=false;198 for(size_t i=0;i<dist.size;++i) {199 if(std::isnan(dist.data[i].logit) || dist.data[i].logit==INFINITY)throw std::runtime_error("invalid sampler logits");200 any_finite=any_finite || std::isfinite(dist.data[i].logit);201 }202 if(!any_finite)throw std::runtime_error("sampler masked all candidates");203 std::vector<int32_t> ids;std::vector<float> logits;204 for(size_t i=0;i<dist.size;++i){ids.push_back(dist.data[i].id);logits.push_back(dist.data[i].logit);}205 int selected=dist.selected>=0 && (size_t)dist.selected<dist.size?dist.data[dist.selected].id:-1;206 if(stage_names[s]=="greedy") {207 int32_t only=selected;float zero=0;208 cst_log_sampler_distribution("greedy_point_mass",&only,&zero,1,selected);209 } else cst_log_sampler_distribution(stage_names[s].c_str(),ids.data(),logits.data(),(int)ids.size(),selected);210 double retained_mass=0;211 for(size_t i=0;i<dist.size;++i)if(std::isfinite(dist.data[i].logit))retained_mass+=std::exp((double)raw[dist.data[i].id]-raw_max)/raw_z;212 event("sampler_filter",{{"stage",stage_names[s]},{"raw_model_mass_retained",retained_mass},213 {"interpretation","remaining finite candidates measured in raw model distribution"}});214 }215 if(dist.selected<0 || (size_t)dist.selected>=dist.size)throw std::runtime_error("sampler did not select a token");216 auto id=dist.data[dist.selected].id;double sampling_ms=ms(start);217 event("sampling",{{"elapsed_ms",sampling_ms},{"selected_id",id},218 {"selection_method",stage_names.back()},{"selected_probability",stage_names.back()=="greedy"?1.0:dist.data[dist.selected].p}});
Implementation status and limits
The documented sampler families are instrumented individually. Opaque RNG state serialization and arbitrary additional sampler families are not implemented. Grammar constrains syntax, not all semantic misuse. A sampler error or invalid configuration is an execution failure, not a refusal or successful defense.
All tensor-based interpretations depend on actual tensor_observed coverage and the tested model/backend. See validation results. There is no universal security score or calibrated threshold in this implementation.