STRA-338 size-fn: split_run and reference attention split further
This commit is contained in:
@@ -67,8 +67,8 @@ pub(super) fn reference_logits(w: &World, tokens: &[u32]) -> Vec<Vec<f64>> {
|
||||
all
|
||||
}
|
||||
|
||||
/// Attention of layer `l` at `pos`: appends the position's half precision key and value to the cache.
|
||||
fn attention_ref(layer: &RefLayer, cache: &mut RefCache, l: usize, pos: usize, h: &[f64]) -> Vec<f64> {
|
||||
/// The rotated query of layer `l` at `pos`; the position's rotated key and value go into the cache as half precision.
|
||||
fn project_qkv_ref(layer: &RefLayer, cache: &mut RefCache, l: usize, pos: usize, h: &[f64]) -> Vec<f64> {
|
||||
let mut q = matvec(&layer.wq, E, h, Some(&layer.bq));
|
||||
let mut k = matvec(&layer.wk, E, h, Some(&layer.bk));
|
||||
let v = matvec(&layer.wv, E, h, Some(&layer.bv));
|
||||
@@ -81,6 +81,12 @@ fn attention_ref(layer: &RefLayer, cache: &mut RefCache, l: usize, pos: usize, h
|
||||
// the decoder's cache holds half precision, so the reference does too
|
||||
cache.ks[l].push(k.iter().map(|x| half_round(*x)).collect());
|
||||
cache.vs[l].push(v.iter().map(|x| half_round(*x)).collect());
|
||||
q
|
||||
}
|
||||
|
||||
/// Attention of layer `l` at `pos`: appends the position's half precision key and value to the cache.
|
||||
fn attention_ref(layer: &RefLayer, cache: &mut RefCache, l: usize, pos: usize, h: &[f64]) -> Vec<f64> {
|
||||
let q = project_qkv_ref(layer, cache, l, pos, h);
|
||||
let mut attn = vec![0.0f64; QW];
|
||||
let first = if l % 2 == 0 {
|
||||
(pos + 1).saturating_sub(WINDOW)
|
||||
@@ -88,26 +94,40 @@ fn attention_ref(layer: &RefLayer, cache: &mut RefCache, l: usize, pos: usize, h
|
||||
0
|
||||
};
|
||||
for head in 0..HEADS {
|
||||
let kvh = head / (HEADS / KV_HEADS);
|
||||
let qh = &q[head * HD..(head + 1) * HD];
|
||||
let mut logits: Vec<f64> = (first..=pos)
|
||||
.map(|p| (0..HD).map(|d| qh[d] * cache.ks[l][p][kvh * HD + d]).sum::<f64>() / (HD as f64).sqrt())
|
||||
.collect();
|
||||
let sink = f64::from(layer.sinks[head]);
|
||||
let m = logits.iter().cloned().fold(sink, f64::max);
|
||||
let z: f64 = logits.iter().map(|s| (s - m).exp()).sum::<f64>() + (sink - m).exp();
|
||||
for s in &mut logits {
|
||||
*s = (*s - m).exp() / z;
|
||||
}
|
||||
for d in 0..HD {
|
||||
attn[head * HD + d] = (first..=pos)
|
||||
.map(|p| logits[p - first] * cache.vs[l][p][kvh * HD + d])
|
||||
.sum();
|
||||
}
|
||||
let out = attend_head_ref(layer, cache, l, (first, pos), (&q, head));
|
||||
attn[head * HD..(head + 1) * HD].copy_from_slice(&out);
|
||||
}
|
||||
attn
|
||||
}
|
||||
|
||||
/// One query head over the cached positions `first..=pos` with its sink: the softmax-weighted values.
|
||||
fn attend_head_ref(
|
||||
layer: &RefLayer,
|
||||
cache: &RefCache,
|
||||
l: usize,
|
||||
(first, pos): (usize, usize),
|
||||
(q, head): (&[f64], usize),
|
||||
) -> Vec<f64> {
|
||||
let kvh = head / (HEADS / KV_HEADS);
|
||||
let qh = &q[head * HD..(head + 1) * HD];
|
||||
let mut logits: Vec<f64> = (first..=pos)
|
||||
.map(|p| (0..HD).map(|d| qh[d] * cache.ks[l][p][kvh * HD + d]).sum::<f64>() / (HD as f64).sqrt())
|
||||
.collect();
|
||||
let sink = f64::from(layer.sinks[head]);
|
||||
let m = logits.iter().cloned().fold(sink, f64::max);
|
||||
let z: f64 = logits.iter().map(|s| (s - m).exp()).sum::<f64>() + (sink - m).exp();
|
||||
for s in &mut logits {
|
||||
*s = (*s - m).exp() / z;
|
||||
}
|
||||
(0..HD)
|
||||
.map(|d| {
|
||||
(first..=pos)
|
||||
.map(|p| logits[p - first] * cache.vs[l][p][kvh * HD + d])
|
||||
.sum()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The experts a token is routed to, best first, and the softmax-normalised weight of each.
|
||||
fn route_ref(layer: &RefLayer, h: &[f64]) -> Vec<(usize, f64)> {
|
||||
let router: Vec<f64> = (0..NEXP)
|
||||
|
||||
@@ -33,33 +33,46 @@ pub(super) fn run_parts(
|
||||
let npu_done = AtomicU64::new(0);
|
||||
std::thread::scope(|scope| {
|
||||
let npu_job = scope.spawn(|| run_npu_part(npu, job, &first, &npu_done, &stop));
|
||||
let t = Instant::now();
|
||||
let gpu_result = gpu.prefill_part(
|
||||
job.model,
|
||||
second,
|
||||
&job.tokens[job.split..],
|
||||
job.pos0 + job.split,
|
||||
&mut |fraction| {
|
||||
let npu_fraction = npu_done.load(Ordering::Relaxed) as f64 / 1e6;
|
||||
let go_on = progress(fraction.min(npu_fraction));
|
||||
if !go_on {
|
||||
stop.store(true, Ordering::Relaxed);
|
||||
}
|
||||
go_on
|
||||
},
|
||||
job.all_hidden,
|
||||
);
|
||||
let gpu_time = t.elapsed();
|
||||
if !matches!(gpu_result, Ok(Some(_))) {
|
||||
stop.store(true, Ordering::Relaxed);
|
||||
}
|
||||
let gpu_outcome = run_gpu_part(gpu, job, second, progress, (&npu_done, &stop));
|
||||
let npu_outcome = npu_job
|
||||
.join()
|
||||
.unwrap_or_else(|_| (Err(ModelError::Invalid("the NPU part panicked".into())), Duration::ZERO));
|
||||
(npu_outcome, (gpu_result, gpu_time))
|
||||
(npu_outcome, gpu_outcome)
|
||||
})
|
||||
}
|
||||
|
||||
/// The GPU part: the rest of the tokens; its progress is held back to the NPU part's, and a stop request or a
|
||||
/// failure stops the NPU part.
|
||||
fn run_gpu_part(
|
||||
gpu: &mut GpuPrefill,
|
||||
job: &SplitJob<'_>,
|
||||
second: &SecondPart<'_>,
|
||||
progress: &mut dyn FnMut(f64) -> bool,
|
||||
(npu_done, stop): (&AtomicU64, &AtomicBool),
|
||||
) -> PartOutcome {
|
||||
let t = Instant::now();
|
||||
let result = gpu.prefill_part(
|
||||
job.model,
|
||||
second,
|
||||
&job.tokens[job.split..],
|
||||
job.pos0 + job.split,
|
||||
&mut |fraction| {
|
||||
let npu_fraction = npu_done.load(Ordering::Relaxed) as f64 / 1e6;
|
||||
let go_on = progress(fraction.min(npu_fraction));
|
||||
if !go_on {
|
||||
stop.store(true, Ordering::Relaxed);
|
||||
}
|
||||
go_on
|
||||
},
|
||||
job.all_hidden,
|
||||
);
|
||||
let time = t.elapsed();
|
||||
if !matches!(result, Ok(Some(_))) {
|
||||
stop.store(true, Ordering::Relaxed);
|
||||
}
|
||||
(result, time)
|
||||
}
|
||||
|
||||
/// The NPU part: the first tokens, announcing every stored layer; the hand-off is abandoned if it does not finish.
|
||||
fn run_npu_part(
|
||||
npu: &mut NpuPrefill,
|
||||
|
||||
Reference in New Issue
Block a user