A high-performance inference engine for AI models
DFlash-TfM clean up and radix top-k race fix (#991)
uuuvn56dbb91e9bd6
diff --git a/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.metal b/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.metalindex 90501bbb0..a35c677cb 100644--- a/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.metal+++ b/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.metal@@ -127,10 +127,13 @@ KERNEL(RadixTopKSmallPass)(if (lid < simdgroups)simd_prefixes[lid] = simd_prefix;}- threadgroup_barrier(mem_flags::mem_threadgroup);- higher += simd_prefixes[lid / METAL_SIMD_SIZE];uint rank = pass == 0 ? k - 1 : ranks[row];++ threadgroup_barrier(mem_flags::mem_threadgroup | mem_flags::mem_device);++ higher += simd_prefixes[lid / METAL_SIMD_SIZE];+if (higher <= rank && rank < higher + sum) {rank -= higher;const uint group_begin = BUCKETS - (lid + 1) * BUCKETS_PER_THREAD;diff --git a/crates/uzu-engine/src/engine/language_model/stream/stream.rs b/crates/uzu-engine/src/engine/language_model/stream/stream.rsindex 1fc7ee90c..911d3188e 100644--- a/crates/uzu-engine/src/engine/language_model/stream/stream.rs+++ b/crates/uzu-engine/src/engine/language_model/stream/stream.rs@@ -545,7 +545,6 @@ impl<'a, B: Backend> LanguageModelStream<'a, B> {let (input_trie, chain_copy, full_accept) = if let Some(speculator) = &self.model.speculator&& let Some(shape) = speculator.make_shape(self.model_state.max_context_length.map(|max_context_length| max_context_length - context_length),- &self.options.sampling_method,)&& let (root_token, Some(output_norm)) = {let resolved = prev_output@@ -564,6 +563,7 @@ impl<'a, B: Backend> LanguageModelStream<'a, B> {root_token as u32,self.model.decoder.embedding(),shape,+ &self.options.sampling_method,#[cfg(grammar)]self.options.grammar.as_mut(),&self.model_state.prng,diff --git a/crates/uzu-engine/src/speculators/dflash_tfm.rs b/crates/uzu-engine/src/speculators/dflash_tfm.rsindex 28c7f35bd..0806f00b2 100644--- a/crates/uzu-engine/src/speculators/dflash_tfm.rs+++ b/crates/uzu-engine/src/speculators/dflash_tfm.rs@@ -65,17 +65,13 @@ pub enum DFlashTfmTreeConstructionMethod {rounds: u32,expand_per_round: u32,expand_width: u32,- /// A shape without it prunes with `DEFAULT_PRUNE_SIGMA`; `null` prunes on the model logprobs.#[serde(default = "default_prune_sigma")]prune_sigma: Option<f32>,},}-/// The final pruning noise scale calibrated for DFlash + Weaver trees.-pub const DEFAULT_PRUNE_SIGMA: f32 = 1.5;-fn default_prune_sigma() -> Option<f32> {- Some(DEFAULT_PRUNE_SIGMA)+ Some(1.5)}#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]@@ -176,7 +172,6 @@ impl<B: Backend> DFlashTfmSpeculator<B> {pub fn make_shape(&self,max_depth: Option<u32>,- sampling_method: &SamplingMethod,) -> Option<DFlashTfmTreeShape> {if max_depth.is_some_and(|max_depth| max_depth < 2) {return None;@@ -196,16 +191,6 @@ impl<B: Backend> DFlashTfmSpeculator<B> {}}- // Greedy verification adds no Gumbel noise to the target logits, so pruning has none to anticipate.- if matches!(sampling_method, SamplingMethod::Greedy)- && let DFlashTfmTreeConstructionMethod::Weaver {- prune_sigma,- ..- } = &mut shape.construction_method- {- *prune_sigma = None;- }-Some(shape)}@@ -216,6 +201,7 @@ impl<B: Backend> DFlashTfmSpeculator<B> {target_output_token: u32,target_embedding: &Embedding<B>,shape: DFlashTfmTreeShape,+ sampling_method: &SamplingMethod,#[cfg(grammar)] grammar: Option<&mut Grammar>,prng: &PRng,allocation_pool: Arc<B::AllocationPool>,@@ -329,13 +315,6 @@ impl<B: Backend> DFlashTfmSpeculator<B> {dflash_depth)));}- if let Some(prune_sigma) = prune_sigma- && !(prune_sigma > 0.0 && prune_sigma.is_finite() && prune_sigma.recip().is_finite())- {- return Err(DFlashTreeError::InvalidTreeShape(format!(- "prune sigma {prune_sigma} is not positive and finite"- )));- }let dflash_output = self.dflash.encode_draft(target_embedding,target_output_token,@@ -346,6 +325,12 @@ impl<B: Backend> DFlashTfmSpeculator<B> {let depth_seeds = (0..weaver.max_depth()).map(|depth| prng.derive(root_position as u64 + depth as u64)).collect::<Box<[u64]>>();+ let prune_sigma = match sampling_method {+ SamplingMethod::Greedy => None,+ SamplingMethod::Stochastic {+ ..+ } => prune_sigma,+ };let tree = weaver.encode_tree(target_output_norm,&dflash_output.draft_hidden,@@ -390,8 +375,6 @@ impl<B: Backend> DFlashTfmSpeculator<B> {node.logprob,);for &child_index in &node.child_indices {- // Grammar-illegal proposals are dropped; nothing may be proposed- // past termination either, since only stop tokens can follow.#[cfg(grammar)]if let Some(grammar) = grammar.as_mut()&& (grammar.is_terminated() || grammar.accept_token(nodes[child_index].token_id as u64).is_err())