phase19: MoE support — gpt-oss-20b end-to-end inference with TP=2

Add Mixture-of-Experts support for the gpt-oss-20b model (20.9B params,
32 experts × top-4 routing). Key additions:

- ModelConfig: MoE fields (num_local_experts, layer_types, sliding_window,
  attention_bias, explicit head_dim, rope_scaling, swiglu_limit)
- YaRN RoPE: RopeCache::new_yarn() with correct frequency interpolation
  and attention_scaling = 0.1*ln(factor)+1
- Custom GLU kernel: gpt_oss_glu_bf16 (clamped sigmoid gate activation)
- Paged attention with sinks + sliding window kernel variant
- GptOss model struct with expert-parallel TP (split 32 experts across ranks)
- bench-gpt-oss binary for TP inference benchmarking

Verified on dash5 with 2x RTX 5090: 63.6 tok/s decode, ~160ms TTFT.
Model generates topically-coherent output (needs chat template for quality).

Known issues:
- Custom GEMV kernel produces NaN with small N (workaround: pad to M=2)
- Prefill doesn't use attention sinks (uses standard flash attention)
- Output quality requires chat template formatting

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Gahow Wang
2026-05-30 15:18:01 +08:00
parent 46bfb59f30
commit 9ad91a4a92
12 changed files with 1390 additions and 44 deletions

View File

@@ -37,6 +37,81 @@ impl RopeCache {
Self { cos, sin, max_seq_len, half_dim }
}
/// YaRN (Yet another RoPE extensioN) RoPE cache. Applies frequency-dependent
/// interpolation so the model can extrapolate beyond its training context.
pub fn new_yarn(
max_seq_len: usize,
head_dim: usize,
theta: f64,
factor: f64,
original_max_pos: usize,
beta_fast: f64,
beta_slow: f64,
) -> Self {
let half_dim = head_dim / 2;
let dim = head_dim as f64;
// find_correction_dim: inverse formula to find dimension from number of rotations
let find_correction_dim = |num_rotations: f64| -> f64 {
dim * (original_max_pos as f64 / (num_rotations * 2.0 * std::f64::consts::PI)).ln()
/ (2.0 * theta.ln())
};
let low_raw = find_correction_dim(beta_fast);
let high_raw = find_correction_dim(beta_slow);
// config has truncate=false, so use raw values (no floor/ceil)
let low = low_raw.max(0.0);
let high = high_raw.min((half_dim - 1) as f64);
// Compute inv_freq with YaRN interpolation
let mut inv_freq = vec![0.0f64; half_dim];
for i in 0..half_dim {
let pos_freq = theta.powf((2 * i) as f64 / dim);
let inv_freq_extrapolation = 1.0 / pos_freq; // original
let inv_freq_interpolation = 1.0 / (factor * pos_freq); // scaled
// Linear ramp: 0 where we keep original, 1 where we interpolate
let ramp = if (high - low).abs() < 0.001 {
0.5
} else {
((i as f64 - low) / (high - low)).clamp(0.0, 1.0)
};
let extrapolation_factor = 1.0 - ramp;
inv_freq[i] = inv_freq_interpolation * (1.0 - extrapolation_factor)
+ inv_freq_extrapolation * extrapolation_factor;
}
// Attention scaling factor for YaRN: 0.1 * ln(factor) + 1.0
let attn_factor = 0.1 * factor.ln() + 1.0;
// Build cos/sin cache on CPU then upload
let total = max_seq_len * half_dim;
let mut cos_host = vec![0.0f32; total];
let mut sin_host = vec![0.0f32; total];
for pos in 0..max_seq_len {
for i in 0..half_dim {
let angle = pos as f64 * inv_freq[i];
cos_host[pos * half_dim + i] = (angle.cos() * attn_factor) as f32;
sin_host[pos * half_dim + i] = (angle.sin() * attn_factor) as f32;
}
}
let nbytes = total * std::mem::size_of::<f32>();
let mut cos = GpuBuffer::alloc(nbytes).expect("alloc yarn cos_cache");
let mut sin = GpuBuffer::alloc(nbytes).expect("alloc yarn sin_cache");
let cos_bytes = unsafe {
std::slice::from_raw_parts(cos_host.as_ptr() as *const u8, nbytes)
};
let sin_bytes = unsafe {
std::slice::from_raw_parts(sin_host.as_ptr() as *const u8, nbytes)
};
cos.copy_from_host(cos_bytes).unwrap();
sin.copy_from_host(sin_bytes).unwrap();
Self { cos, sin, max_seq_len, half_dim }
}
}
/// Apply RoPE in-place to x.