1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! `{arch}.attention.clamp_kqv`, for the architectures whose graph
//! applies it.
//!
//! `LLM_KV_ATTENTION_CLAMP_KQV` (`llama-arch.cpp:231`) is a symmetric
//! clamp on the Q, K and V projections, applied INSIDE the shared
//! `build_qkv` rather than in any one architecture's graph:
//!
//! ```text
//! // llama-graph.cpp:1611-1612, and again at :1631, :1641, :1651
//! if (hparams.f_clamp_kqv > 0.0f) {
//! qkv = ggml_clamp(ctx0, qkv, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
//! }
//! ```
//!
//! The fused branch clamps the fused projection once; the split branch
//! clamps `Qcur`, `Kcur` and `Vcur` separately. Either way it happens
//! AFTER the bias add and before the reshape, before any QK-norm and
//! before RoPE. `build_qkv` clamps for ANY architecture whose
//! `f_clamp_kqv` is positive, but only three `load_arch_hparams` read
//! the key, so for every other architecture the key is dead metadata
//! and frink ignores it the same way.
//!
//! **Three architectures read the key**, and no other: `dbrx.cpp:5`
//! (REQUIRED -- no `false` argument, so a DBRX file without the key is
//! refused by llama.cpp's loader), `mpt.cpp:5` and `olmo.cpp:5` (both
//! optional). `mpt` never reaches the generic decoder (ALiBi), so this
//! module serves `olmo` and `dbrx`. It is live for both: the DBRX
//! converter writes `attn_config.clip_qkv` unconditionally
//! (`conversion/dbrx.py:28`, and every DBRX checkpoint sets it to 8),
//! and `conversion/olmo.py:23-25` writes the key whenever the HF config
//! has a `clip_qkv`, which OLMo-7B-Twin-2T and OLMo-1.7-7B do (`8.0`)
//! and the original OLMo-7B does not (`null`).
//!
//! **This used to be a refusal**, and the reason it was one is the
//! reason for the shape of the implementation. The projections are
//! computed in the CPU decode body, the prefill body and the
//! continuous-batching body, and each of those applied the QKV bias in
//! its own hand-written loop; a clamp added to some of them and not the
//! others would have been this repo's single most expensive defect
//! shape. So the three bias loops collapsed onto ONE helper,
//! `Decoder::apply_qkv_bias_and_clamp` (`decoder/qkv_bias.rs`), and the
//! clamp lives in that helper after the bias, where `build_qkv` puts
//! it. The fused Metal launches apply the bias inside their kernels via
//! `AttnExtras` and have no clamp, so `Decoder::metal_can_serve_model`
//! -- the one predicate every Metal eligibility check reads -- keeps a
//! clamped model on the host bodies rather than letting two backends
//! answer differently from the same weights.
//!
//! The evidence is `tests/olmo_graphs.rs`
//! (`olmo_clamped_tiny.gguf`, whose llama.cpp logits differ measurably
//! from the unclamped file's) and `tests/dbrx_graphs.rs`.
/// Architectures whose graph clamps Q, K and V by
/// `{arch}.attention.clamp_kqv`.
///
/// `mpt` was deliberately absent while it was `DedicatedOnly`; it is
/// audited since 2026-09-14 (`crate::alibi`, tests/alibi_graphs.rs, a
/// fixture with `clamp_kqv = 4` that the unclamped body misses by 4.8)
/// and `mpt.cpp:5` reads the key optional (`conversion/mpt.py:36`
/// writes it from `clip_qkv`). The rule
/// [`crate::rope_finetuned::ROPE_GATED_ON_FINETUNED`] follows for
/// `granitehybrid` still applies: a row this list does not serve is
/// not listed.
pub const CLAMPED_QKV_ARCHITECTURES: & = &;
/// The subset of [`CLAMPED_QKV_ARCHITECTURES`] whose loader reads the
/// key as REQUIRED.
///
/// `dbrx.cpp:5` is `ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV,
/// hparams.f_clamp_kqv)` with no `required = false`, so llama.cpp throws
/// on a DBRX file that omits it. `olmo.cpp:5` passes `false`. Refusing
/// the same file llama.cpp refuses is the honest answer; defaulting it
/// to "no clamp" would run a graph the reference cannot.
pub const CLAMP_KQV_REQUIRED: & = &;
/// Why a file's clamp declaration cannot be honoured.
/// The clamp the decoder applies for `arch`, from what the file
/// declares (`None` when the key is absent).
///
/// `Ok(None)` is "no clamp": an architecture whose graph never clamps,
/// or a file declaring llama.cpp's own `0.0f` default or a negative
/// value -- `> 0.0f` is llama.cpp's test (llama-graph.cpp:1611), so
/// zero and a negative value both mean "no clamp" there and must mean
/// it here. Reading zero as a clamp would zero every projection.
/// `ggml_clamp(x, -c, c)`, in place, over a slice that may hold one row
/// or a whole batch: the clamp is elementwise, so the two are the same
/// call.