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
IQ port: read unsloth IQ2_XXS / IQ3_XXS / IQ4_XS experts (GGUF types 16, 18, 23) through ggml Metal kernels already in Candle
Picassoai5afa4860cb18
diff --git a/crates/engine/src/mv_id.rs b/crates/engine/src/mv_id.rsindex aa2692a..a5b86c9 100644--- a/crates/engine/src/mv_id.rs+++ b/crates/engine/src/mv_id.rs@@ -36,6 +36,8 @@ pub fn matvec_id(GgmlDType::Q3K | GgmlDType::Q5K => (2, 32, 4),GgmlDType::Q6K => (2, 32, 2),GgmlDType::F16 | GgmlDType::BF16 | GgmlDType::Q8K | GgmlDType::F32 => (32, 1, 8),+ GgmlDType::Iq2Xxs | GgmlDType::Iq3Xxs => (4, 16, 8),+ GgmlDType::Iq4Xs => (4, 16, 4),};let name = match dtype {GgmlDType::Q4_0 => "kernel_mul_mv_id_q4_0_f32",@@ -44,6 +46,9 @@ pub fn matvec_id(GgmlDType::Q5_1 => "kernel_mul_mv_id_q5_1_f32",GgmlDType::Q8_0 => "kernel_mul_mv_id_q8_0_f32",GgmlDType::Mxfp4 => "kernel_mul_mv_id_mxfp4_f32",+ GgmlDType::Iq2Xxs => "kernel_mul_mv_id_iq2_xxs_f32",+ GgmlDType::Iq3Xxs => "kernel_mul_mv_id_iq3_xxs_f32",+ GgmlDType::Iq4Xs => "kernel_mul_mv_id_iq4_xs_f32",GgmlDType::Q2K => "kernel_mul_mv_id_q2_K_f32",GgmlDType::Q3K => "kernel_mul_mv_id_q3_K_f32",GgmlDType::Q4K => "kernel_mul_mv_id_q4_K_f32",@@ -57,6 +62,11 @@ pub fn matvec_id(let encoder = ep.encoder();let encoder = encoder.as_ref();encoder.set_compute_pipeline_state(&pipeline);+ // IQ kernels keep ggml's grids and signs in threadgroup memory.+ let iq_shmem: usize = match dtype { GgmlDType::Iq2Xxs => 256 * 8 + 128, GgmlDType::Iq3Xxs => 256 * 4 + 128, GgmlDType::Iq4Xs => 32 * 4, _ => 0 };+ if iq_shmem > 0 {+ encoder.set_threadgroup_memory_length(0, iq_shmem);+ }let ne00 = k as i64;let ne01 = n as i64;diff --git a/crates/prepare/src/main.rs b/crates/prepare/src/main.rsindex 0cc5185..710bd4b 100644--- a/crates/prepare/src/main.rs+++ b/crates/prepare/src/main.rs@@ -95,6 +95,9 @@ fn dtype_code(d: GgmlDType) -> u32 {GgmlDType::Q8K => 15,GgmlDType::BF16 => 30,GgmlDType::Mxfp4 => 39,+ GgmlDType::Iq2Xxs => 16,+ GgmlDType::Iq3Xxs => 18,+ GgmlDType::Iq4Xs => 23,}}diff --git a/vendor/candle-core/src/quantized/ggml_file.rs b/vendor/candle-core/src/quantized/ggml_file.rsindex 8609370..20eda31 100644--- a/vendor/candle-core/src/quantized/ggml_file.rs+++ b/vendor/candle-core/src/quantized/ggml_file.rs@@ -184,6 +184,9 @@ pub fn qtensor_from_ggml(GgmlDType::Q6K => {from_raw_data::<k_quants::BlockQ6K>(raw_data, size_in_bytes, dims, device)}+ GgmlDType::Iq2Xxs => from_raw_data::<k_quants::BlockIq2Xxs>(raw_data, size_in_bytes, dims, device),+ GgmlDType::Iq3Xxs => from_raw_data::<k_quants::BlockIq3Xxs>(raw_data, size_in_bytes, dims, device),+ GgmlDType::Iq4Xs => from_raw_data::<k_quants::BlockIq4Xs>(raw_data, size_in_bytes, dims, device),GgmlDType::Mxfp4 => {from_raw_data::<k_quants::BlockMxfp4>(raw_data, size_in_bytes, dims, device)}diff --git a/vendor/candle-core/src/quantized/k_quants.rs b/vendor/candle-core/src/quantized/k_quants.rsindex bccfedd..0aeb26d 100644--- a/vendor/candle-core/src/quantized/k_quants.rs+++ b/vendor/candle-core/src/quantized/k_quants.rs@@ -2951,3 +2951,43 @@ verify_block_sizes!(BlockQ4_0, BlockQ4_1, BlockQ5_0, BlockQ5_1, BlockQ8_0, BlockQ8_1, BlockQ2K, BlockQ3K, BlockQ4K,BlockQ5K, BlockQ6K, BlockQ8K, f32, f16, bf16);++// IQ2_XXS / IQ3_XXS / IQ4_XS (ggml-common.h). Stored and run on Metal only: ggml's Metal kernels read+// them; there is no CPU dequant here, so a CPU path on these types is an error, not a silent wrong answer.+#[derive(Debug, Clone, PartialEq)]+#[repr(C)]+pub struct BlockIq2Xxs { pub(crate) d: f16, pub(crate) qs: [u16; QK_K / 8] }+const _: () = assert!(std::mem::size_of::<BlockIq2Xxs>() == 66);+#[derive(Debug, Clone, PartialEq)]+#[repr(C)]+pub struct BlockIq3Xxs { pub(crate) d: f16, pub(crate) qs: [u8; 3 * QK_K / 8] }+const _: () = assert!(std::mem::size_of::<BlockIq3Xxs>() == 98);+#[derive(Debug, Clone, PartialEq)]+#[repr(C)]+pub struct BlockIq4Xs { pub(crate) d: f16, pub(crate) scales_h: u16, pub(crate) scales_l: [u8; QK_K / 64], pub(crate) qs: [u8; QK_K / 2] }+const _: () = assert!(std::mem::size_of::<BlockIq4Xs>() == 136);++macro_rules! metal_only_block {+ ($t:ty, $dt:expr) => {+ impl GgmlType for $t {+ const DTYPE: GgmlDType = $dt;+ const BLCK_SIZE: usize = QK_K;+ type VecDotType = BlockQ8K;+ fn to_float(_xs: &[Self], _ys: &mut [f32]) {+ panic!("{:?} has no CPU dequant in Coachwhip; it runs on Metal only", Self::DTYPE)+ }+ fn from_float(_xs: &[f32], _ys: &mut [Self]) {+ panic!("{:?} cannot be quantized by Coachwhip", Self::DTYPE)+ }+ fn vec_dot(_n: usize, _xs: &[Self], _ys: &[Self::VecDotType]) -> f32 {+ panic!("{:?} has no CPU matmul in Coachwhip; it runs on Metal only", Self::DTYPE)+ }+ fn vec_dot_unopt(n: usize, xs: &[Self], ys: &[Self::VecDotType]) -> f32 {+ Self::vec_dot(n, xs, ys)+ }+ }+ };+}+metal_only_block!(BlockIq2Xxs, GgmlDType::Iq2Xxs);+metal_only_block!(BlockIq3Xxs, GgmlDType::Iq3Xxs);+metal_only_block!(BlockIq4Xs, GgmlDType::Iq4Xs);diff --git a/vendor/candle-core/src/quantized/metal.rs b/vendor/candle-core/src/quantized/metal.rsindex b00c19b..fd0a4a5 100644--- a/vendor/candle-core/src/quantized/metal.rs+++ b/vendor/candle-core/src/quantized/metal.rs@@ -115,6 +115,9 @@ impl QMetalStorage {let vec: Vec<crate::quantized::BlockQ8K> = read_to_vec(&buffer, block_len);crate::quantized::BlockQ8K::to_float(&vec, &mut out);}+ GgmlDType::Iq2Xxs | GgmlDType::Iq3Xxs | GgmlDType::Iq4Xs => {+ crate::bail!("{:?} has no CPU dequant in Coachwhip; it runs on Metal only", self.dtype)+ }GgmlDType::Mxfp4 => {let vec: Vec<crate::quantized::BlockMxfp4> = read_to_vec(&buffer, block_len);crate::quantized::BlockMxfp4::to_float(&vec, &mut out);@@ -504,6 +507,9 @@ impl TryFrom<GgmlDType> for candle_metal_kernels::GgmlDType {GgmlDType::F32 => Ok(candle_metal_kernels::GgmlDType::F32),GgmlDType::BF16 => Ok(candle_metal_kernels::GgmlDType::BF16),GgmlDType::Mxfp4 => Ok(candle_metal_kernels::GgmlDType::Mxfp4),+ GgmlDType::Iq2Xxs => Ok(candle_metal_kernels::GgmlDType::Iq2Xxs),+ GgmlDType::Iq3Xxs => Ok(candle_metal_kernels::GgmlDType::Iq3Xxs),+ GgmlDType::Iq4Xs => Ok(candle_metal_kernels::GgmlDType::Iq4Xs),}}}diff --git a/vendor/candle-core/src/quantized/mod.rs b/vendor/candle-core/src/quantized/mod.rsindex 659066b..308a433 100644--- a/vendor/candle-core/src/quantized/mod.rs+++ b/vendor/candle-core/src/quantized/mod.rs@@ -116,6 +116,9 @@ impl QStorage {GgmlDType::Q8K => metal::load_quantized(d, as_t_slice::<BlockQ8K>(data)),GgmlDType::BF16 => metal::load_quantized(d, as_t_slice::<bf16>(data)),GgmlDType::Mxfp4 => metal::load_quantized(d, as_t_slice::<BlockMxfp4>(data)),+ GgmlDType::Iq2Xxs => metal::load_quantized(d, as_t_slice::<k_quants::BlockIq2Xxs>(data)),+ GgmlDType::Iq3Xxs => metal::load_quantized(d, as_t_slice::<k_quants::BlockIq3Xxs>(data)),+ GgmlDType::Iq4Xs => metal::load_quantized(d, as_t_slice::<k_quants::BlockIq4Xs>(data)),},Device::Cuda(d) => match dtype {GgmlDType::F32 => cuda::load_quantized(d, as_t_slice::<f32>(data)),@@ -134,6 +137,7 @@ impl QStorage {GgmlDType::Q8K => cuda::load_quantized(d, as_t_slice::<BlockQ8K>(data)),GgmlDType::BF16 => cuda::load_quantized(d, as_t_slice::<bf16>(data)),GgmlDType::Mxfp4 => cuda::load_quantized(d, as_t_slice::<BlockMxfp4>(data)),+ GgmlDType::Iq2Xxs | GgmlDType::Iq3Xxs | GgmlDType::Iq4Xs => crate::bail!("{dtype:?} runs on Metal only"),},}}@@ -301,6 +305,9 @@ pub enum GgmlDType {Q6K,Q8K,Mxfp4,+ Iq2Xxs,+ Iq3Xxs,+ Iq4Xs,}impl GgmlDType {@@ -323,6 +330,9 @@ impl GgmlDType {// https://github.com/ggerganov/ggml/blob/29d87fc6676e7ed0cdfdec0804b06001d9c2bb44/include/ggml.h#L38930 => Self::BF16,39 => Self::Mxfp4,+ 16 => Self::Iq2Xxs,+ 18 => Self::Iq3Xxs,+ 23 => Self::Iq4Xs,_ => crate::bail!("unknown dtype for tensor {u}"),};Ok(dtype)@@ -347,6 +357,9 @@ impl GgmlDType {// https://github.com/ggerganov/ggml/blob/29d87fc6676e7ed0cdfdec0804b06001d9c2bb44/include/ggml.h#L389Self::BF16 => 30,Self::Mxfp4 => 39,+ Self::Iq2Xxs => 16,+ Self::Iq3Xxs => 18,+ Self::Iq4Xs => 23,}}@@ -372,6 +385,9 @@ impl GgmlDType {BlockMxfp4::zeros();elem_count / BlockMxfp4::BLCK_SIZE]),+ Self::Iq2Xxs => Box::new(vec![k_quants::BlockIq2Xxs::zeros(); elem_count / k_quants::QK_K]),+ Self::Iq3Xxs => Box::new(vec![k_quants::BlockIq3Xxs::zeros(); elem_count / k_quants::QK_K]),+ Self::Iq4Xs => Box::new(vec![k_quants::BlockIq4Xs::zeros(); elem_count / k_quants::QK_K]),}}@@ -394,6 +410,9 @@ impl GgmlDType {Self::Q8K => Box::new(as_t_slice::<BlockQ8K>(data).to_vec()),Self::BF16 => Box::new(as_t_slice::<bf16>(data).to_vec()),Self::Mxfp4 => Box::new(as_t_slice::<BlockMxfp4>(data).to_vec()),+ Self::Iq2Xxs => Box::new(as_t_slice::<k_quants::BlockIq2Xxs>(data).to_vec()),+ Self::Iq3Xxs => Box::new(as_t_slice::<k_quants::BlockIq3Xxs>(data).to_vec()),+ Self::Iq4Xs => Box::new(as_t_slice::<k_quants::BlockIq4Xs>(data).to_vec()),}}@@ -417,6 +436,9 @@ impl GgmlDType {Self::Q6K => std::mem::size_of::<BlockQ6K>(),Self::Q8K => std::mem::size_of::<BlockQ8K>(),Self::Mxfp4 => std::mem::size_of::<BlockMxfp4>(),+ Self::Iq2Xxs => std::mem::size_of::<k_quants::BlockIq2Xxs>(),+ Self::Iq3Xxs => std::mem::size_of::<k_quants::BlockIq3Xxs>(),+ Self::Iq4Xs => std::mem::size_of::<k_quants::BlockIq4Xs>(),}}@@ -433,6 +455,7 @@ impl GgmlDType {Self::Q8_1 => k_quants::QK8_1,Self::Q2K | Self::Q3K | Self::Q4K | Self::Q5K | Self::Q6K | Self::Q8K => k_quants::QK_K,Self::Mxfp4 => k_quants::QK_MXFP4,+ Self::Iq2Xxs | Self::Iq3Xxs | Self::Iq4Xs => k_quants::QK_K,}}}diff --git a/vendor/candle-metal-kernels/src/kernels/quantized.rs b/vendor/candle-metal-kernels/src/kernels/quantized.rsindex 6d98684..9fb65ad 100644--- a/vendor/candle-metal-kernels/src/kernels/quantized.rs+++ b/vendor/candle-metal-kernels/src/kernels/quantized.rs@@ -23,6 +23,9 @@ pub enum GgmlDType {F32,BF16,Mxfp4,+ Iq2Xxs,+ Iq3Xxs,+ Iq4Xs,}#[allow(clippy::too_many_arguments)]@@ -114,6 +117,8 @@ pub fn call_quantized_matmul_mv_t(let align = 8;(nth0, nth1, align)}+ GgmlDType::Iq2Xxs | GgmlDType::Iq3Xxs => (4, 16, 8),+ GgmlDType::Iq4Xs => (4, 16, 4),};let thread_groups_count = MTLSize {width: divide(ne01 as usize, align),@@ -132,6 +137,9 @@ pub fn call_quantized_matmul_mv_t(GgmlDType::Q5_1 => "kernel_mul_mv_q5_1_f32",GgmlDType::Q8_0 => "kernel_mul_mv_q8_0_f32",GgmlDType::Mxfp4 => "kernel_mul_mv_mxfp4_f32",+ GgmlDType::Iq2Xxs => "kernel_mul_mv_iq2_xxs_f32",+ GgmlDType::Iq3Xxs => "kernel_mul_mv_iq3_xxs_f32",+ GgmlDType::Iq4Xs => "kernel_mul_mv_iq4_xs_f32",GgmlDType::Q8_1 => "kernel_mul_mv_q8_1_f32",GgmlDType::Q2K => "kernel_mul_mv_q2_K_f32",GgmlDType::Q3K => "kernel_mul_mv_q3_K_f32",@@ -145,9 +153,14 @@ pub fn call_quantized_matmul_mv_t(};let pipeline = kernels.load_pipeline(device, Source::Quantized, name)?;+ // IQ shared tables (dense): ggml's grids and signs live in threadgroup memory.+ let iq_shmem: usize = match dtype { GgmlDType::Iq2Xxs => 256 * 8 + 128, GgmlDType::Iq3Xxs => 256 * 4 + 128, GgmlDType::Iq4Xs => 32 * 4, _ => 0 };let encoder = ep.encoder();let encoder: &ComputeCommandEncoder = encoder.as_ref();encoder.set_compute_pipeline_state(&pipeline);+ if iq_shmem > 0 {+ encoder.set_threadgroup_memory_length(0, iq_shmem);+ }debug_group!(encoder, "qmm_mv {name} B={b} M={m} K={k} N={n}");set_params!(@@ -249,6 +262,9 @@ pub fn call_quantized_matmul_mm_t(GgmlDType::Q8_1 => Err(MetalKernelError::UnsupportedDTypeForOp("Q8_1", "qmatmul"))?,GgmlDType::Q8K => Err(MetalKernelError::UnsupportedDTypeForOp("Q8K", "qmatmul"))?,GgmlDType::Mxfp4 => Err(MetalKernelError::UnsupportedDTypeForOp("Mxfp4", "qmatmul mm"))?,+ GgmlDType::Iq2Xxs => "kernel_mul_mm_iq2_xxs_f32",+ GgmlDType::Iq3Xxs => "kernel_mul_mm_iq3_xxs_f32",+ GgmlDType::Iq4Xs => "kernel_mul_mm_iq4_xs_f32",};let pipeline = kernels.load_pipeline(device, Source::Quantized, name)?;@@ -318,6 +334,7 @@ pub fn call_quantized_get_rows(GgmlDType::Q8_1 => Err(MetalKernelError::UnsupportedDTypeForOp("Q8_1", "get_rows"))?,GgmlDType::Q8K => Err(MetalKernelError::UnsupportedDTypeForOp("Q8K", "get_rows"))?,GgmlDType::Mxfp4 => Err(MetalKernelError::UnsupportedDTypeForOp("Mxfp4", "get_rows"))?,+ GgmlDType::Iq2Xxs | GgmlDType::Iq3Xxs | GgmlDType::Iq4Xs => Err(MetalKernelError::UnsupportedDTypeForOp("IQ", "get_rows"))?,};let pipeline = kernels.load_pipeline(device, Source::Quantized, name)?;