A high-performance inference engine for AI models
Correct manual kernels definition (#770)
uuuvn16fb633a3d2e
diff --git a/crates/legacy/uzu/tests/tool/calls.rs b/crates/legacy/uzu/tests/tool/calls.rsindex c996dbbc1..28d191e61 100644--- a/crates/legacy/uzu/tests/tool/calls.rs+++ b/crates/legacy/uzu/tests/tool/calls.rs@@ -6,7 +6,7 @@ use uzu::{engine::{Engine, EngineConfig},session::tool::{func_def::ToolDescriptor, uzu_tool_function},types::{- basic::{SamplingMethod, SamplingPolicy},+ basic::SamplingSeed,session::chat::{ChatConfig, ChatMessage, ChatReplyConfig, ChatReplyFinishReason, ChatRole},},};@@ -116,7 +116,15 @@ async fn run_tool_calls_test(for case in cases {println!("Prompt: {}", case.prompt);- let mut session = engine.chat(model.clone(), ChatConfig::default()).await.unwrap();+ let mut session = engine+ .chat(+ model.clone(),+ ChatConfig::default().with_sampling_seed(SamplingSeed::Custom {+ seed: 0,+ }),+ )+ .await+ .unwrap();session.add_tools(case.tools.iter().map(|definition| definition()).collect()).await.unwrap();let mut messages = Vec::new();@@ -125,16 +133,7 @@ async fn run_tool_calls_test(}messages.push(ChatMessage::user().with_text(case.prompt.to_string()));- // Greedy sampling keeps these tests deterministic; with the default stochastic sampling- // the models occasionally skip a tool call, hallucinate that part of the answer, or- // finish without any text in the final reply.- let reply_config = ChatReplyConfig {- sampling_policy: SamplingPolicy::Custom {- method: SamplingMethod::Greedy {},- },- ..ChatReplyConfig::default()- };- let replies = session.reply(messages, reply_config).await.unwrap();+ let replies = session.reply(messages, ChatReplyConfig::default()).await.unwrap();let final_reply = replies.last().expect("Expected at least one reply");assert_eq!(final_reply.finish_reason,diff --git a/crates/uzu-engine/src/backends/common/kernel/attention_gemm/kernel.rs b/crates/uzu-engine/src/backends/common/kernel/attention_gemm/kernel.rsindex 44a1d0911..ce758b8f1 100644--- a/crates/uzu-engine/src/backends/common/kernel/attention_gemm/kernel.rs+++ b/crates/uzu-engine/src/backends/common/kernel/attention_gemm/kernel.rs@@ -1,29 +1,34 @@-use std::convert::Infallible;-use crate::{- backends::common::{Allocation, Backend, BufferArg, Encoder, kernel::Kernels},+ backends::common::{+ Allocation, Backend, BufferArg, Encoder,+ kernel::{Kernels, Unsupported},+ },encodable_block::mixer::attention::core::{AttentionCoreEncodeArguments, AttentionCoreNewArguments},};-pub trait AttentionGemmCore<B: Backend<Kernels: Kernels<AttentionGemmCore = Self>>>: Sized + Send + Sync {+pub trait AttentionGemmCore: Sized + Send + Sync {+ type Backend: Backend<Kernels: Kernels<AttentionGemmCore = Self>>;+fn is_supported(arguments: &AttentionCoreNewArguments,- context: &B::Context,- ) -> Result<bool, B::Error>;+ context: &<Self::Backend as Backend>::Context,+ ) -> Result<bool, <Self::Backend as Backend>::Error>;fn new(- context: &B::Context,+ context: &<Self::Backend as Backend>::Context,arguments: &AttentionCoreNewArguments,- ) -> Result<Self, B::Error>;+ ) -> Result<Self, <Self::Backend as Backend>::Error>;- fn encode<'a, KT: BufferArg<'a, B>, VT: BufferArg<'a, B>>(+ fn encode<'a, KT: BufferArg<'a, Self::Backend>, VT: BufferArg<'a, Self::Backend>>(&self,- arguments: AttentionCoreEncodeArguments<'a, B, KT, VT>,- encoder: &mut Encoder<B>,- ) -> Result<Allocation<B>, B::Error>;+ arguments: AttentionCoreEncodeArguments<'a, Self::Backend, KT, VT>,+ encoder: &mut Encoder<Self::Backend>,+ ) -> Result<Allocation<Self::Backend>, <Self::Backend as Backend>::Error>;}-impl<B: Backend<Kernels: Kernels<AttentionGemmCore = Infallible>>> AttentionGemmCore<B> for Infallible {+impl<B: Backend<Kernels: Kernels<AttentionGemmCore = Unsupported<B>>>> AttentionGemmCore for Unsupported<B> {+ type Backend = B;+fn is_supported(_arguments: &AttentionCoreNewArguments,_context: &B::Context,@@ -43,6 +48,6 @@ impl<B: Backend<Kernels: Kernels<AttentionGemmCore = Infallible>>> AttentionGemm_arguments: AttentionCoreEncodeArguments<'a, B, KT, VT>,_encoder: &mut Encoder<B>,) -> Result<Allocation<B>, B::Error> {- match *self {}+ match self.never {}}}diff --git a/crates/uzu-engine/src/backends/common/kernel/delta_net_chunked_prefill.rs b/crates/uzu-engine/src/backends/common/kernel/delta_net_chunked_prefill.rsindex 9d1e3ec4c..c303c6be2 100644--- a/crates/uzu-engine/src/backends/common/kernel/delta_net_chunked_prefill.rs+++ b/crates/uzu-engine/src/backends/common/kernel/delta_net_chunked_prefill.rs@@ -1,7 +1,5 @@-use std::convert::Infallible;-use crate::{- backends::common::{Allocation, Backend, Encoder},+ backends::common::{Allocation, Backend, Encoder, Kernels, kernel::Unsupported},data_type::DataType,};@@ -19,12 +17,14 @@ pub struct DeltaNetChunkedPrefillArgs<'a, B: Backend> {pub suffix_len: u32,}-pub trait DeltaNetChunkedPrefill<B: Backend>: Sized + Send + Sync {+pub trait DeltaNetChunkedPrefill: Sized + Send + Sync {+ type Backend: Backend<Kernels: Kernels<DeltaNetChunkedPrefill = Self>>;+fn new(- context: &B::Context,+ context: &<Self::Backend as Backend>::Context,outer_data_type: DataType,head_dim: u32,- ) -> Result<Option<Self>, B::Error>;+ ) -> Result<Option<Self>, <Self::Backend as Backend>::Error>;fn should_use(&self,@@ -33,12 +33,14 @@ pub trait DeltaNetChunkedPrefill<B: Backend>: Sized + Send + Sync {fn encode(&self,- args: DeltaNetChunkedPrefillArgs<'_, B>,- encoder: &mut Encoder<B>,- ) -> Result<(), B::Error>;+ args: DeltaNetChunkedPrefillArgs<'_, Self::Backend>,+ encoder: &mut Encoder<Self::Backend>,+ ) -> Result<(), <Self::Backend as Backend>::Error>;}-impl<B: Backend> DeltaNetChunkedPrefill<B> for Infallible {+impl<B: Backend<Kernels: Kernels<DeltaNetChunkedPrefill = Unsupported<B>>>> DeltaNetChunkedPrefill for Unsupported<B> {+ type Backend = B;+fn new(_context: &B::Context,_outer_data_type: DataType,@@ -51,7 +53,7 @@ impl<B: Backend> DeltaNetChunkedPrefill<B> for Infallible {&self,_suffix_len: u32,) -> bool {- match *self {}+ match self.never {}}fn encode(@@ -59,6 +61,6 @@ impl<B: Backend> DeltaNetChunkedPrefill<B> for Infallible {_args: DeltaNetChunkedPrefillArgs<'_, B>,_encoder: &mut Encoder<B>,) -> Result<(), B::Error> {- match *self {}+ match self.never {}}}diff --git a/crates/uzu-engine/src/backends/common/kernel/delta_net_tree_verify.rs b/crates/uzu-engine/src/backends/common/kernel/delta_net_tree_verify.rsindex c9de6f3bf..e33a569d8 100644--- a/crates/uzu-engine/src/backends/common/kernel/delta_net_tree_verify.rs+++ b/crates/uzu-engine/src/backends/common/kernel/delta_net_tree_verify.rs@@ -1,26 +1,28 @@-use std::convert::Infallible;-use crate::{- backends::common::{Allocation, Backend, Encoder, Kernels},+ backends::common::{Allocation, Backend, Encoder, Kernels, kernel::Unsupported},encodable_block::mixer::delta_net::tree_verify::{TreeVerifyEncodeArguments, TreeVerifyNewArguments},};-pub trait DeltaNetTreeVerify<B: Backend<Kernels: Kernels<DeltaNetTreeVerify = Self>>>: Sized + Send + Sync {- fn is_supported(context: &B::Context) -> bool;+pub trait DeltaNetTreeVerify: Sized + Send + Sync {+ type Backend: Backend<Kernels: Kernels<DeltaNetTreeVerify = Self>>;++ fn is_supported(context: &<Self::Backend as Backend>::Context) -> bool;fn new(- context: &B::Context,+ context: &<Self::Backend as Backend>::Context,arguments: &TreeVerifyNewArguments,- ) -> Result<Self, B::Error>;+ ) -> Result<Self, <Self::Backend as Backend>::Error>;fn encode(&self,- arguments: TreeVerifyEncodeArguments<'_, B>,- encoder: &mut Encoder<B>,- ) -> Result<Allocation<B>, B::Error>;+ arguments: TreeVerifyEncodeArguments<'_, Self::Backend>,+ encoder: &mut Encoder<Self::Backend>,+ ) -> Result<Allocation<Self::Backend>, <Self::Backend as Backend>::Error>;}-impl<B: Backend<Kernels: Kernels<DeltaNetTreeVerify = Infallible>>> DeltaNetTreeVerify<B> for Infallible {+impl<B: Backend<Kernels: Kernels<DeltaNetTreeVerify = Unsupported<B>>>> DeltaNetTreeVerify for Unsupported<B> {+ type Backend = B;+fn is_supported(_context: &B::Context) -> bool {false}diff --git a/crates/uzu-engine/src/backends/common/kernel/mod.rs b/crates/uzu-engine/src/backends/common/kernel/mod.rsindex 4a700860e..ea0b5ea2e 100644--- a/crates/uzu-engine/src/backends/common/kernel/mod.rs+++ b/crates/uzu-engine/src/backends/common/kernel/mod.rs@@ -1,3 +1,5 @@+use std::{convert::Infallible, marker::PhantomData};+use crate::backends::common::Backend;pub mod activation_transform;@@ -17,11 +19,16 @@ pub trait Kernels: Sized {type Backend: Backend<Kernels = Self>;autogen_kernels!();- type AttentionGemmCore: attention_gemm::AttentionGemmCore<Self::Backend>;- type DeltaNetChunkedPrefill: delta_net_chunked_prefill::DeltaNetChunkedPrefill<Self::Backend>;- type DeltaNetTreeVerify: delta_net_tree_verify::DeltaNetTreeVerify<Self::Backend>;+ type AttentionGemmCore: attention_gemm::AttentionGemmCore<Backend = Self::Backend>;+ type DeltaNetChunkedPrefill: delta_net_chunked_prefill::DeltaNetChunkedPrefill<Backend = Self::Backend>;+ type DeltaNetTreeVerify: delta_net_tree_verify::DeltaNetTreeVerify<Backend = Self::Backend>;type MatmulKernel: matmul::MatmulKernel<Backend = Self::Backend>;- type RadixTopKSmall: radix_top_k_small::RadixTopKSmall<Self::Backend>;+ type RadixTopKSmall: radix_top_k_small::RadixTopKSmall<Backend = Self::Backend>;+}++pub struct Unsupported<B: Backend> {+ never: Infallible,+ _marker: PhantomData<fn() -> B>,}#[cfg(test)]diff --git a/crates/uzu-engine/src/backends/common/kernel/radix_top_k_small.rs b/crates/uzu-engine/src/backends/common/kernel/radix_top_k_small.rsindex 2d67a4bca..07045138b 100644--- a/crates/uzu-engine/src/backends/common/kernel/radix_top_k_small.rs+++ b/crates/uzu-engine/src/backends/common/kernel/radix_top_k_small.rs@@ -1,20 +1,22 @@-use crate::backends::common::{Allocation, Backend, Encoder};+use crate::backends::common::{Allocation, Backend, Encoder, Kernels};pub const MAX_K: u32 = 512;-pub trait RadixTopKSmall<B: Backend>: Sized + Send + Sync {+pub trait RadixTopKSmall: Sized + Send + Sync {+ type Backend: Backend<Kernels: Kernels<RadixTopKSmall = Self>>;+fn new(- context: &B::Context,+ context: &<Self::Backend as Backend>::Context,columns: u32,- ) -> Result<Self, B::Error>;+ ) -> Result<Self, <Self::Backend as Backend>::Error>;fn encode(&self,- input: &Allocation<B>,- output_ids: &mut Allocation<B>,- output_scores: &mut Allocation<B>,+ input: &Allocation<Self::Backend>,+ output_ids: &mut Allocation<Self::Backend>,+ output_scores: &mut Allocation<Self::Backend>,rows: u32,k: u32,- encoder: &mut Encoder<B>,- ) -> Result<(), B::Error>;+ encoder: &mut Encoder<Self::Backend>,+ ) -> Result<(), <Self::Backend as Backend>::Error>;}diff --git a/crates/uzu-engine/src/backends/cpu/kernel/mod.rs b/crates/uzu-engine/src/backends/cpu/kernel/mod.rsindex 3446d081e..fe72506a6 100644--- a/crates/uzu-engine/src/backends/cpu/kernel/mod.rs+++ b/crates/uzu-engine/src/backends/cpu/kernel/mod.rs@@ -1,6 +1,7 @@-use std::convert::Infallible;--use crate::backends::{common::Kernels, cpu::Cpu};+use crate::backends::{+ common::{Kernels, kernel::Unsupported},+ cpu::Cpu,+};mod activation;pub(crate) mod activation_transform;@@ -32,9 +33,9 @@ impl Kernels for CpuKernels {type Backend = Cpu;autogen_kernels!();- type AttentionGemmCore = Infallible;- type DeltaNetChunkedPrefill = Infallible;- type DeltaNetTreeVerify = Infallible;+ type AttentionGemmCore = Unsupported<Cpu>;+ type DeltaNetChunkedPrefill = Unsupported<Cpu>;+ type DeltaNetTreeVerify = Unsupported<Cpu>;type MatmulKernel = matmul::MatmulCpuKernel;type RadixTopKSmall = radix_top_k_small::CpuRadixTopKSmall;}diff --git a/crates/uzu-engine/src/backends/cpu/kernel/radix_top_k_small.rs b/crates/uzu-engine/src/backends/cpu/kernel/radix_top_k_small.rsindex eb3e2bf73..f5f4825e6 100644--- a/crates/uzu-engine/src/backends/cpu/kernel/radix_top_k_small.rs+++ b/crates/uzu-engine/src/backends/cpu/kernel/radix_top_k_small.rs@@ -13,7 +13,9 @@ pub struct CpuRadixTopKSmall {columns: usize,}-impl RadixTopKSmall<Cpu> for CpuRadixTopKSmall {+impl RadixTopKSmall for CpuRadixTopKSmall {+ type Backend = Cpu;+fn new(_context: &CpuContext,columns: u32,diff --git a/crates/uzu-engine/src/backends/metal/kernel/attention.rs b/crates/uzu-engine/src/backends/metal/kernel/attention.rsindex bd53cdca9..92b80f3be 100644--- a/crates/uzu-engine/src/backends/metal/kernel/attention.rs+++ b/crates/uzu-engine/src/backends/metal/kernel/attention.rs@@ -80,7 +80,9 @@ impl AttentionGemmMetalCore {}}-impl AttentionGemmCore<Metal> for AttentionGemmMetalCore {+impl AttentionGemmCore for AttentionGemmMetalCore {+ type Backend = Metal;+fn is_supported(arguments: &AttentionCoreNewArguments,_context: &MetalContext,diff --git a/crates/uzu-engine/src/backends/metal/kernel/gdn/chunked/mod.rs b/crates/uzu-engine/src/backends/metal/kernel/gdn/chunked/mod.rsindex c5d6c6ad3..be76ae7ef 100644--- a/crates/uzu-engine/src/backends/metal/kernel/gdn/chunked/mod.rs+++ b/crates/uzu-engine/src/backends/metal/kernel/gdn/chunked/mod.rs@@ -32,7 +32,9 @@ pub struct MetalDeltaNetChunkedPrefill {output_and_state: DeltaNetChunkedOutputAndStateMetalKernel,}-impl DeltaNetChunkedPrefill<Metal> for MetalDeltaNetChunkedPrefill {+impl DeltaNetChunkedPrefill for MetalDeltaNetChunkedPrefill {+ type Backend = Metal;+fn new(context: &MetalContext,outer_data_type: DataType,diff --git a/crates/uzu-engine/src/backends/metal/kernel/gdn/tree_verify.rs b/crates/uzu-engine/src/backends/metal/kernel/gdn/tree_verify.rsindex 13578fedd..475fc2254 100644--- a/crates/uzu-engine/src/backends/metal/kernel/gdn/tree_verify.rs+++ b/crates/uzu-engine/src/backends/metal/kernel/gdn/tree_verify.rs@@ -62,7 +62,9 @@ pub struct MetalDeltaNetTreeVerify {out: <MetalKernels as Kernels>::BuildTreeOutKernel,}-impl DeltaNetTreeVerify<Metal> for MetalDeltaNetTreeVerify {+impl DeltaNetTreeVerify for MetalDeltaNetTreeVerify {+ type Backend = Metal;+fn is_supported(_context: &MetalContext) -> bool {true}diff --git a/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.rs b/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.rsindex 4a52abd8f..5d21a696f 100644--- a/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.rs+++ b/crates/uzu-engine/src/backends/metal/kernel/radix_top_k_small.rs@@ -30,7 +30,9 @@ pub struct MetalRadixTopKSmall {passes: u32,}-impl RadixTopKSmall<Metal> for MetalRadixTopKSmall {+impl RadixTopKSmall for MetalRadixTopKSmall {+ type Backend = Metal;+fn new(context: &MetalContext,columns: u32,diff --git a/crates/uzu-engine/src/encodable_block/mixer/attention/core/mod.rs b/crates/uzu-engine/src/encodable_block/mixer/attention/core/mod.rsindex f55fe883c..94622ac1b 100644--- a/crates/uzu-engine/src/encodable_block/mixer/attention/core/mod.rs+++ b/crates/uzu-engine/src/encodable_block/mixer/attention/core/mod.rs@@ -49,10 +49,10 @@ impl<B: Backend> AttentionCores<B> {arguments: AttentionCoreNewArguments,context: &B::Context,) -> Result<Self, B::Error> {- let gemm = if <<B::Kernels as Kernels>::AttentionGemmCore as AttentionGemmCoreTrait<B>>::is_supported(+ let gemm = if <<B::Kernels as Kernels>::AttentionGemmCore as AttentionGemmCoreTrait>::is_supported(&arguments, context,)? {- Some(<<B::Kernels as Kernels>::AttentionGemmCore as AttentionGemmCoreTrait<B>>::new(context, &arguments)?)+ Some(<<B::Kernels as Kernels>::AttentionGemmCore as AttentionGemmCoreTrait>::new(context, &arguments)?)} else {None};diff --git a/crates/uzu-engine/src/encodable_block/mixer/delta_net.rs b/crates/uzu-engine/src/encodable_block/mixer/delta_net.rsindex 7f4629c0e..a69aa54c6 100644--- a/crates/uzu-engine/src/encodable_block/mixer/delta_net.rs+++ b/crates/uzu-engine/src/encodable_block/mixer/delta_net.rs@@ -263,9 +263,9 @@ impl<B: Backend> DeltaNet<B> {let delta_net_norm_gate = <B::Kernels as Kernels>::DeltaNetNormGateKernel::new(context, outer_data_type).map_err(DeltaNetNewError::Backend)?;let tree_verify =- if <<B::Kernels as Kernels>::DeltaNetTreeVerify as DeltaNetTreeVerifyTrait<B>>::is_supported(context) {+ if <<B::Kernels as Kernels>::DeltaNetTreeVerify as DeltaNetTreeVerifyTrait>::is_supported(context) {Some(- <<B::Kernels as Kernels>::DeltaNetTreeVerify as DeltaNetTreeVerifyTrait<B>>::new(+ <<B::Kernels as Kernels>::DeltaNetTreeVerify as DeltaNetTreeVerifyTrait>::new(context,&TreeVerifyNewArguments {data_type: outer_data_type,diff --git a/crates/uzu-engine/unit/backends/common/kernel/gated_act_mul_test.rs b/crates/uzu-engine/unit/backends/common/kernel/gated_act_mul_test.rsindex 829cb9d52..7d37bb5de 100644--- a/crates/uzu-engine/unit/backends/common/kernel/gated_act_mul_test.rs+++ b/crates/uzu-engine/unit/backends/common/kernel/gated_act_mul_test.rs@@ -98,6 +98,7 @@ fn interleaved_test<T: ArrayElement + Float + Debug + Display>(act_type: Activat}#[uzu_test]+#[ignore = "flaky"] // TODOfn test_gated_act_mul_interleaved_hadamard_bf16() {let input = interleaved_input::<bf16>(ActivationType::SILU);let expected = run_interleaved::<bf16, Cpu>(&input, true);