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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
impl OwnedQuantizedModel {
/// Compute attention with Grouped Query Attention (GQA) support (IMP-105)
///
/// GQA uses fewer KV heads than Q heads, with multiple Q heads sharing each KV head.
/// This reduces memory bandwidth and KV cache size for large models.
///
/// # Arguments
/// * `q` - Query vector for current position [hidden_dim] (num_heads Q heads)
/// * `k_cache` - Cached keys [cache_len, kv_dim] (num_kv_heads KV heads)
/// * `v_cache` - Cached values [cache_len, kv_dim] (num_kv_heads KV heads)
/// * `current_k` - Key for current position [kv_dim]
/// * `current_v` - Value for current position [kv_dim]
///
/// # Returns
/// Attention output [hidden_dim]
///
/// # GQA Mapping
/// Q head i uses KV head (i * num_kv_heads / num_heads)
/// Example: 8 Q heads, 2 KV heads → Q heads 0-3 use KV head 0, Q heads 4-7 use KV head 1
pub fn attention_with_cache_gqa(
&self,
q: &[f32],
k_cache: &[f32],
v_cache: &[f32],
current_k: &[f32],
current_v: &[f32],
) -> Vec<f32> {
let num_heads = self.config.num_heads;
let num_kv_heads = self.config.num_kv_heads;
// GH-479: Use config methods (Qwen3 head_dim != hidden/heads)
let head_dim = self.config.head_dim();
let q_dim = self.config.q_dim();
let kv_dim = self.config.kv_dim();
let scale = 1.0 / (head_dim as f32).sqrt();
// Number of Q heads that share each KV head
let q_per_kv = num_heads / num_kv_heads;
// Total sequence length = cached + 1 (current)
let cache_len = if kv_dim > 0 {
k_cache.len() / kv_dim
} else {
0
};
let total_len = cache_len + 1;
let mut output = vec![0.0f32; q_dim];
// Score buffer for the current group.
// Size: q_per_kv * total_len.
// We reuse this buffer for each KV group to minimize allocation.
let mut group_scores = vec![0.0f32; q_per_kv * total_len];
// Process each KV head group (OPTIMIZATION: Scan KV cache once per group)
for kv_head in 0..num_kv_heads {
let kv_head_offset = kv_head * head_dim;
// 1. Compute Scores (Scan K Cache Once)
for pos in 0..cache_len {
let k_start = pos * kv_dim + kv_head_offset;
let cached_key = &k_cache[k_start..k_start + head_dim];
// For each Q head in this group
for i in 0..q_per_kv {
let q_head_idx = kv_head * q_per_kv + i;
let q_head_offset = q_head_idx * head_dim;
let q_head_data = &q[q_head_offset..q_head_offset + head_dim];
let score = Self::simd_dot_f32(q_head_data, cached_key) * scale;
group_scores[i * total_len + pos] = score;
}
}
// Handle current position K
let curr_key = ¤t_k[kv_head_offset..kv_head_offset + head_dim];
for i in 0..q_per_kv {
let q_head_idx = kv_head * q_per_kv + i;
let q_head_offset = q_head_idx * head_dim;
let q_head_data = &q[q_head_offset..q_head_offset + head_dim];
let score = Self::simd_dot_f32(q_head_data, curr_key) * scale;
group_scores[i * total_len + cache_len] = score;
}
// 2. Softmax (Per Q Head)
for i in 0..q_per_kv {
let start = i * total_len;
let end = start + total_len;
crate::quantize::softmax_simd(&mut group_scores[start..end]);
}
// 3. Accumulate Values (Scan V Cache Once)
for pos in 0..cache_len {
let v_start = pos * kv_dim + kv_head_offset;
let cached_val = &v_cache[v_start..v_start + head_dim];
for i in 0..q_per_kv {
let weight = group_scores[i * total_len + pos];
let q_head_idx = kv_head * q_per_kv + i;
let out_offset = q_head_idx * head_dim;
let out_head = &mut output[out_offset..out_offset + head_dim];
Self::simd_axpy_f32(out_head, weight, cached_val);
}
}
// Handle current position V
let curr_val = ¤t_v[kv_head_offset..kv_head_offset + head_dim];
for i in 0..q_per_kv {
let weight = group_scores[i * total_len + cache_len];
let q_head_idx = kv_head * q_per_kv + i;
let out_offset = q_head_idx * head_dim;
let out_head = &mut output[out_offset..out_offset + head_dim];
Self::simd_axpy_f32(out_head, weight, curr_val);
}
}
output
}
/// Attention with cache - writes to pre-allocated buffer (IMP-131)
pub fn attention_with_cache_gqa_into(
&self,
q: &[f32],
k_cache: &[f32],
v_cache: &[f32],
current_k: &[f32],
current_v: &[f32],
output: &mut [f32],
) {
let num_heads = self.config.num_heads;
let num_kv_heads = self.config.num_kv_heads;
// GH-479: Use config methods (Qwen3 head_dim != hidden/heads)
let head_dim = self.config.head_dim();
let q_dim = self.config.q_dim();
let kv_dim = self.config.kv_dim();
let scale = 1.0 / (head_dim as f32).sqrt();
let q_per_kv = num_heads / num_kv_heads;
let cache_len = if kv_dim > 0 {
k_cache.len() / kv_dim
} else {
0
};
let total_len = cache_len + 1;
// Zero output buffer
// GH-479: Use q_dim (may differ from hidden_dim for Qwen3)
output[..q_dim].iter_mut().for_each(|x| *x = 0.0);
// Score buffer for the current group.
// Size: q_per_kv * total_len.
// We reuse this buffer for each KV group to minimize allocation.
let mut group_scores = vec![0.0f32; q_per_kv * total_len];
// Process each KV head group (OPTIMIZATION: Scan KV cache once per group)
for kv_head in 0..num_kv_heads {
let kv_head_offset = kv_head * head_dim;
// 1. Compute Scores (Scan K Cache Once)
for pos in 0..cache_len {
let k_start = pos * kv_dim + kv_head_offset;
let cached_key = &k_cache[k_start..k_start + head_dim];
// For each Q head in this group
for i in 0..q_per_kv {
let q_head_idx = kv_head * q_per_kv + i;
let q_head_offset = q_head_idx * head_dim;
let q_head_data = &q[q_head_offset..q_head_offset + head_dim];
let score = Self::simd_dot_f32(q_head_data, cached_key) * scale;
group_scores[i * total_len + pos] = score;
}
}
// Handle current position K
let curr_key = ¤t_k[kv_head_offset..kv_head_offset + head_dim];
for i in 0..q_per_kv {
let q_head_idx = kv_head * q_per_kv + i;
let q_head_offset = q_head_idx * head_dim;
let q_head_data = &q[q_head_offset..q_head_offset + head_dim];
let score = Self::simd_dot_f32(q_head_data, curr_key) * scale;
group_scores[i * total_len + cache_len] = score;
}
// 2. Softmax (Per Q Head)
for i in 0..q_per_kv {
let start = i * total_len;
let end = start + total_len;
crate::quantize::softmax_simd(&mut group_scores[start..end]);
}
// 3. Accumulate Values (Scan V Cache Once)
for pos in 0..cache_len {
let v_start = pos * kv_dim + kv_head_offset;
let cached_val = &v_cache[v_start..v_start + head_dim];
for i in 0..q_per_kv {
let weight = group_scores[i * total_len + pos];
let q_head_idx = kv_head * q_per_kv + i;
let out_offset = q_head_idx * head_dim;
let out_head = &mut output[out_offset..out_offset + head_dim];
Self::simd_axpy_f32(out_head, weight, cached_val);
}
}
// Handle current position V
let curr_val = ¤t_v[kv_head_offset..kv_head_offset + head_dim];
for i in 0..q_per_kv {
let weight = group_scores[i * total_len + cache_len];
let q_head_idx = kv_head * q_per_kv + i;
let out_offset = q_head_idx * head_dim;
let out_head = &mut output[out_offset..out_offset + head_dim];
Self::simd_axpy_f32(out_head, weight, curr_val);
}
}
}
/// Adaptive attention with KV cache - auto-selects CPU or GPU backend (IMP-122)
///
/// For short cache lengths (< 64), uses efficient CPU implementation.
/// For long cache lengths (>= 64), uses GPU-accelerated computation.
///
/// # Arguments
/// * `q` - Query vector for current position [hidden_dim]
/// * `k_cache` - Cached keys [cache_len, hidden_dim]
/// * `v_cache` - Cached values [cache_len, hidden_dim]
/// * `current_k` - Key for current position [hidden_dim]
/// * `current_v` - Value for current position [hidden_dim]
///
/// # Returns
/// Result containing attention output [hidden_dim]
///
/// # Errors
/// Returns error if GPU operations fail (for GPU path)
#[cfg(feature = "gpu")]
pub fn adaptive_attention_with_cache(
&self,
q: &[f32],
k_cache: &[f32],
v_cache: &[f32],
current_k: &[f32],
current_v: &[f32],
) -> Result<Vec<f32>> {
let hidden_dim = self.config.hidden_dim;
// Calculate cache length
let cache_len = if hidden_dim > 0 {
k_cache.len() / hidden_dim
} else {
0
};
// Threshold for GPU dispatch (matches IMP-119)
const GPU_CACHE_LEN_THRESHOLD: usize = 64;
if cache_len >= GPU_CACHE_LEN_THRESHOLD {
// GPU path for long sequences
self.gpu_attention_with_cache(q, k_cache, v_cache, current_k, current_v)
} else {
// CPU path for short sequences - use existing implementation
Ok(self.attention_with_cache(q, k_cache, v_cache, current_k, current_v))
}
}
/// CPU-only version of adaptive attention
#[cfg(not(feature = "gpu"))]
pub fn adaptive_attention_with_cache(
&self,
q: &[f32],
k_cache: &[f32],
v_cache: &[f32],
current_k: &[f32],
current_v: &[f32],
) -> Result<Vec<f32>> {
Ok(self.attention_with_cache(q, k_cache, v_cache, current_k, current_v))
}
/// GPU-accelerated attention with KV cache (IMP-122)
///
/// Uses GPU for Q@K^T computation when cache is large enough.
#[cfg(feature = "gpu")]
fn gpu_attention_with_cache(
&self,
q: &[f32],
k_cache: &[f32],
v_cache: &[f32],
current_k: &[f32],
current_v: &[f32],
) -> Result<Vec<f32>> {
use crate::gpu::HybridScheduler;
let num_heads = self.config.num_heads;
// GH-479: Use config methods (Qwen3 head_dim != hidden/heads)
let head_dim = self.config.head_dim();
let q_dim = self.config.q_dim();
let scale = 1.0 / (head_dim as f32).sqrt();
// Total sequence length = cached + 1 (current)
let cache_len = k_cache.len() / q_dim;
let total_len = cache_len + 1;
let mut output = vec![0.0f32; q_dim];
// Create scheduler for GPU operations
let mut scheduler = HybridScheduler::with_threshold(1000).map_err(|e| {
RealizarError::UnsupportedOperation {
operation: "gpu_attention_with_cache".to_string(),
reason: format!("Failed to create scheduler: {e}"),
}
})?;
// Process each head
for head in 0..num_heads {
let head_offset = head * head_dim;
let q_head = &q[head_offset..head_offset + head_dim];
// Build full K matrix for this head: [total_len, head_dim]
let mut k_full = Vec::with_capacity(total_len * head_dim);
for pos in 0..cache_len {
let k_start = pos * q_dim + head_offset;
k_full.extend_from_slice(&k_cache[k_start..k_start + head_dim]);
}
k_full.extend_from_slice(¤t_k[head_offset..head_offset + head_dim]);
// Transpose K to [head_dim, total_len] for matmul
let mut k_t = vec![0.0f32; head_dim * total_len];
for pos in 0..total_len {
for d in 0..head_dim {
k_t[d * total_len + pos] = k_full[pos * head_dim + d];
}
}
// GPU matmul: Q[1, head_dim] @ K_T[head_dim, total_len] -> [1, total_len]
let scores_raw = scheduler
.matmul(q_head, &k_t, 1, head_dim, total_len)
.map_err(|e| RealizarError::UnsupportedOperation {
operation: "gpu_attention_with_cache".to_string(),
reason: format!("GPU matmul failed: {e}"),
})?;
// Scale scores
let mut scores: Vec<f32> = scores_raw.iter().map(|&s| s * scale).collect();
// Softmax (SIMD-optimized)
crate::quantize::softmax_simd(&mut scores);
// Weighted sum of values
let out_head = &mut output[head_offset..head_offset + head_dim];
// Cached values
for (pos, &weight) in scores.iter().enumerate().take(cache_len) {
let v_start = pos * q_dim + head_offset;
let cached_val = &v_cache[v_start..v_start + head_dim];
for d in 0..head_dim {
out_head[d] += weight * cached_val[d];
}
}
// Current value
let curr_val = ¤t_v[head_offset..head_offset + head_dim];
let current_weight = scores[cache_len];
for d in 0..head_dim {
out_head[d] += current_weight * curr_val[d];
}
}
Ok(output)
}
}