Skip to content

The 80B Qwen3-Coder-Next on a 16 GB Mac, about 13 tok/s, fully offline. A mixture-of-experts inference engine for Apple silicon.

Rust

lab: rolling conversation space: past the reserved space the full-attention layers keep the opening 256 words and the newest, the oldest middle rolls out; the door never refuses a long chat; mask rebuilt for the kept words

Picassoai4ec460786a05
diff --git a/crates/engine/src/model_next.rs b/crates/engine/src/model_next.rs
index a7075df..ba01c89 100644
--- a/crates/engine/src/model_next.rs
+++ b/crates/engine/src/model_next.rs
@@ -74,8 +74,23 @@ impl KvBuf {
fn append(&mut self, k: &Tensor, v: &Tensor) -> Result<(Tensor, Tensor)> {
let l = k.dim(2)?;
if let Some(fixed) = conv_tokens() {
+ if l > fixed / 2 {
+ candle::bail!("a piece of {l} words is over half the {fixed}-word conversation space")
+ }
if self.len + l > fixed {
- candle::bail!("conversation space full: {} + {l} words over the {fixed} reserved", self.len)
+ // Roll: keep the opening and the newest, let the oldest middle go.
+ let sink = 256.min(self.len);
+ let drop = self.len + l - fixed;
+ let keep_from = (sink + drop).min(self.len);
+ if let (Some(kb), Some(vb)) = (&self.k, &self.v) {
+ if keep_from < self.len {
+ let tail_k = kb.narrow(2, keep_from, self.len - keep_from)?.contiguous()?;
+ let tail_v = vb.narrow(2, keep_from, self.len - keep_from)?.contiguous()?;
+ kb.slice_set(&tail_k, 2, sink)?;
+ vb.slice_set(&tail_v, 2, sink)?;
+ }
+ }
+ self.len = sink + (self.len - keep_from);
}
if self.k.is_none() || self.cap != fixed {
let (b, h, _, d) = k.dims4()?;
@@ -666,6 +681,14 @@ impl FullAttn {
let q = q.contiguous()?.reshape((1, self.n_kv, g * l, self.hd))?;
let mut scores = (q.matmul(&k.transpose(2, 3)?)? * (1.0 / (self.hd as f64).sqrt()))?;
if let Some(m) = mask {
+ // After the space has rolled, the mask is rebuilt for the words actually kept.
+ let rebuilt;
+ let m = if m.dim(D::Minus1)? != s_len {
+ rebuilt = candle_transformers::utils::build_additive_causal_mask(l, s_len - l, None, scores.device(), DType::F32)?;
+ &rebuilt
+ } else {
+ m
+ };
scores = scores
.reshape((1, self.n_kv, g, l, s_len))?
.broadcast_add(&m.to_dtype(scores.dtype())?.unsqueeze(1)?)?
diff --git a/crates/server/src/chat.rs b/crates/server/src/chat.rs
index dfb8fff..a4fac9c 100644
--- a/crates/server/src/chat.rs
+++ b/crates/server/src/chat.rs
@@ -334,9 +334,7 @@ pub fn generate_full(
sampling: &Sampling,
emit: &mut dyn FnMut(&str) -> Result<()>,
) -> Result<Answer> {
- if prompt.len() + max_new > context_limit() {
- anyhow::bail!("the conversation and its answer would be over {} tokens; start a new one", context_limit());
- }
+ // No refusal: past the reserved space the conversation rolls (see the engine's KvBuf).
let known = session.fed.len();
let mut fresh = known == 0 || prompt.len() <= known || !prompt.starts_with(&session.fed);
if fresh {
@@ -752,8 +750,9 @@ pub fn own_page(host: &str, origin: &str, port: u16) -> bool {
/// How many tokens an API answer may write: what the client asked for, capped by Coachwhip's own
/// limit; with no limit asked for, whatever room the 16K conversation leaves.
pub fn answer_budget(asked: Option<u64>, max_new: usize, prompt_len: usize) -> usize {
- let room = context_limit().saturating_sub(prompt_len);
- asked.map_or(max_new.min(room), |m| (m as usize).min(max_new))
+ // The conversation rolls past its space, so the answer is never squeezed to fit.
+ let _ = prompt_len;
+ asked.map_or(max_new, |m| (m as usize).min(max_new))
}
pub fn html_escape(t: &str) -> String {