diff --git a/crates/cuda-driver/src/ffi.rs b/crates/cuda-driver/src/ffi.rs index effdd9b5..462782c2 100644 --- a/crates/cuda-driver/src/ffi.rs +++ b/crates/cuda-driver/src/ffi.rs @@ -117,6 +117,88 @@ impl MatvecArguments { } } } + +struct Q8QuantizeStorage { + addresses: [u64; 3], + elements: u32, + parameters: [*mut std::ffi::c_void; 4], +} +pub(crate) struct Q8QuantizeArguments { + storage: std::ptr::NonNull, +} +impl Q8QuantizeArguments { + pub(crate) fn new( + input: BufferHandle, + scratch: BufferHandle, + status: BufferHandle, + elements: u32, + ) -> Self { + let storage = Box::into_raw(Box::new(Q8QuantizeStorage { + addresses: [input.0, scratch.0, status.0], + elements, + parameters: [std::ptr::null_mut(); 4], + })); + // SAFETY: raw-owned, stable allocation; initialize the exact fixed ABI. + unsafe { + for i in 0..3 { + (*storage).parameters[i] = (&raw mut (*storage).addresses[i]).cast(); + } + (*storage).parameters[3] = (&raw mut (*storage).elements).cast(); + } + Self { + storage: std::ptr::NonNull::new(storage).expect("Box allocation is non-null"), + } + } +} +impl Drop for Q8QuantizeArguments { + fn drop(&mut self) { + // SAFETY: uniquely reconstructed before launch or after confirmed sync. + unsafe { drop(Box::from_raw(self.storage.as_ptr())) } + } +} + +struct Q5ProjectionStorage { + addresses: [u64; 3], + rows: u32, + columns: u32, + parameters: [*mut std::ffi::c_void; 5], +} +pub(crate) struct Q5ProjectionArguments { + storage: std::ptr::NonNull, +} +impl Q5ProjectionArguments { + pub(crate) fn new( + weights: BufferHandle, + scratch: BufferHandle, + output: BufferHandle, + rows: u32, + columns: u32, + ) -> Self { + let storage = Box::into_raw(Box::new(Q5ProjectionStorage { + addresses: [weights.0, scratch.0, output.0], + rows, + columns, + parameters: [std::ptr::null_mut(); 5], + })); + // SAFETY: raw-owned, stable allocation; initialize the exact fixed ABI. + unsafe { + for i in 0..3 { + (*storage).parameters[i] = (&raw mut (*storage).addresses[i]).cast(); + } + (*storage).parameters[3] = (&raw mut (*storage).rows).cast(); + (*storage).parameters[4] = (&raw mut (*storage).columns).cast(); + } + Self { + storage: std::ptr::NonNull::new(storage).expect("Box allocation is non-null"), + } + } +} +impl Drop for Q5ProjectionArguments { + fn drop(&mut self) { + // SAFETY: uniquely reconstructed before launch or after confirmed sync. + unsafe { drop(Box::from_raw(self.storage.as_ptr())) } + } +} impl Drop for MatvecArguments { fn drop(&mut self) { // SAFETY: reconstructed exactly once before launch or after successful @@ -170,6 +252,7 @@ mod enabled { ) -> CuResult; static FIXED_PTX: &[u8] = concat!(include_str!("vector_add.ptx"), "\0").as_bytes(); static MATVEC_PTX: &[u8] = concat!(include_str!("matvec.ptx"), "\0").as_bytes(); + static Q5_K_PTX: &[u8] = concat!(include_str!("q5_k_q8_1.ptx"), "\0").as_bytes(); type MemcpyDtoHV2 = unsafe extern "C" fn(*mut c_void, CuDevicePtr, usize) -> CuResult; struct Library(*mut c_void); @@ -271,6 +354,9 @@ mod enabled { match operation { FixedOperation::VectorAdd => FIXED_PTX, FixedOperation::Matvec => MATVEC_PTX, + FixedOperation::Q5K + | FixedOperation::Q5KQuantize + | FixedOperation::Q5KProject => Q5_K_PTX, } .as_ptr() .cast(), @@ -296,6 +382,11 @@ mod enabled { match operation { FixedOperation::VectorAdd => c"leleka_vector_add_f32", FixedOperation::Matvec => c"leleka_matvec_f32", + FixedOperation::Q5K => { + return Err(Error::InvalidDriverValue("fixed Q5_K entry selector")); + } + FixedOperation::Q5KQuantize => c"leleka_quantize_q8_1", + FixedOperation::Q5KProject => c"leleka_q5_k_q8_1", } .as_ptr(), ) @@ -368,6 +459,56 @@ mod enabled { ) }) } + + pub(crate) fn launch_q8_quantize( + &self, + function: FunctionHandle, + grid: u32, + arguments: &mut Q8QuantizeArguments, + ) -> Result<()> { + // SAFETY: fixed entry, exact bounded grid, 256 threads, and stable + // raw-owned scalar/pointer table retained through synchronization. + check("cuLaunchKernel", unsafe { + (self.launch_kernel)( + function.0 as *mut c_void, + grid, + 1, + 1, + 256, + 1, + 1, + 0, + std::ptr::null_mut(), + (&raw mut (*arguments.storage.as_ptr()).parameters).cast(), + std::ptr::null_mut(), + ) + }) + } + + pub(crate) fn launch_q5_projection( + &self, + function: FunctionHandle, + grid: u32, + arguments: &mut Q5ProjectionArguments, + ) -> Result<()> { + // SAFETY: fixed entry, 32x4 threads, fixed launch geometry and + // stable raw-owned argument storage through synchronized completion. + check("cuLaunchKernel", unsafe { + (self.launch_kernel)( + function.0 as *mut c_void, + grid, + 1, + 1, + 32, + 4, + 1, + 0, + std::ptr::null_mut(), + (&raw mut (*arguments.storage.as_ptr()).parameters).cast(), + std::ptr::null_mut(), + ) + }) + } pub(crate) fn init(&self) -> Result<()> { // SAFETY: resolved exact ABI, mandated zero flags, live library. check("cuInit", unsafe { (self.init)(0) }) @@ -515,6 +656,24 @@ impl Api { let _ = (function.0, arguments.storage); Err(Error::UnsupportedPlatform) } + pub(crate) fn launch_q8_quantize( + &self, + function: FunctionHandle, + _: u32, + arguments: &mut Q8QuantizeArguments, + ) -> Result<()> { + let _ = (function.0, arguments.storage); + Err(Error::UnsupportedPlatform) + } + pub(crate) fn launch_q5_projection( + &self, + function: FunctionHandle, + _: u32, + arguments: &mut Q5ProjectionArguments, + ) -> Result<()> { + let _ = (function.0, arguments.storage); + Err(Error::UnsupportedPlatform) + } // These methods cannot be reached through the safe API: load always fails. pub(crate) fn init(&self) -> Result<()> { Err(Error::UnsupportedPlatform) diff --git a/crates/cuda-driver/src/lib.rs b/crates/cuda-driver/src/lib.rs index 8e4529f8..77077907 100644 --- a/crates/cuda-driver/src/lib.rs +++ b/crates/cuda-driver/src/lib.rs @@ -29,6 +29,8 @@ pub enum Error { ContextMismatch, InvalidVectorShape, InvalidMatvecShape, + InvalidQ5KProjectionShape, + InvalidQ8Activation, OutputAliasesInput, InvalidOrdinal, InvalidSize, @@ -70,6 +72,10 @@ impl Error { Self::InvalidMatvecShape => { "matvec requires exact nonempty bounded row-major f32 shapes" } + Self::InvalidQ5KProjectionShape => { + "Q5_K projection requires exact bounded row-major single-column shapes" + } + Self::InvalidQ8Activation => "activation cannot be represented by native Q8_1", Self::OutputAliasesInput => "output must not alias an input", Self::InvalidSize => "byte size must be nonzero and fit isize", Self::BudgetExceeded => "allocation exceeds context byte budget", @@ -184,6 +190,7 @@ impl<'driver> Device<'driver> { poisoned: Cell::new(false), vector_add_kernel: Cell::new(None), matvec_kernel: Cell::new(None), + q5_k_kernel: Cell::new(None), }) } } @@ -197,18 +204,92 @@ pub struct Context<'driver> { poisoned: Cell, vector_add_kernel: Cell>, matvec_kernel: Cell>, + q5_k_kernel: Cell>, } #[derive(Clone, Copy)] struct FixedKernel { module: ffi::ModuleHandle, function: ffi::FunctionHandle, } +#[derive(Clone, Copy)] +struct Q5Kernels { + module: ffi::ModuleHandle, + quantize: ffi::FunctionHandle, + project: ffi::FunctionHandle, +} // Closed selector: callers cannot choose executable bytes, names or geometry. #[derive(Clone, Copy)] enum FixedOperation { VectorAdd, Matvec, + Q5K, + Q5KQuantize, + Q5KProject, +} + +/// Exact one-column Q5_K matrix geometry: `rows × columns` packed weights. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Q5KProjectionShape { + pub rows: usize, + pub columns: usize, +} + +#[derive(Debug, PartialEq, Eq)] +struct Q5KShape { + weight_bytes: usize, + activation_bytes: usize, + output_bytes: usize, + scratch_bytes: usize, + rows: u32, + columns: u32, + quant_grid: u32, + projection_grid: u32, +} + +fn q5_k_shape(rows: usize, columns: usize) -> Result { + if rows == 0 || columns == 0 || !columns.is_multiple_of(256) { + return Err(Error::InvalidQ5KProjectionShape); + } + let rows_abi = u32::try_from(rows).map_err(|_| Error::InvalidQ5KProjectionShape)?; + let columns_abi = u32::try_from(columns).map_err(|_| Error::InvalidQ5KProjectionShape)?; + if rows_abi > i32::MAX as u32 || columns_abi > i32::MAX as u32 { + return Err(Error::InvalidQ5KProjectionShape); + } + let checked_bytes = |elements: usize, width: usize| -> Result { + let bytes = elements + .checked_mul(width) + .ok_or(Error::InvalidQ5KProjectionShape)?; + valid_size(bytes).map_err(|_| Error::InvalidQ5KProjectionShape)?; + Ok(bytes) + }; + let weight_blocks = rows + .checked_mul(columns / 256) + .ok_or(Error::InvalidQ5KProjectionShape)?; + let weight_bytes = checked_bytes(weight_blocks, 176)?; + let activation_bytes = checked_bytes(columns, 4)?; + let output_bytes = checked_bytes(rows, 4)?; + let scratch_bytes = checked_bytes(columns / 32, 36)?; + let quant_grid = columns / 256; + if quant_grid == 0 || quant_grid > i32::MAX as usize { + return Err(Error::InvalidQ5KProjectionShape); + } + let small_k = columns / 256 < 8; + let projection_ctas = if small_k { rows.div_ceil(4) } else { rows }; + if projection_ctas == 0 || projection_ctas > i32::MAX as usize { + return Err(Error::InvalidQ5KProjectionShape); + } + Ok(Q5KShape { + weight_bytes, + activation_bytes, + output_bytes, + scratch_bytes, + rows: rows_abi, + columns: columns_abi, + quant_grid: u32::try_from(quant_grid).map_err(|_| Error::InvalidQ5KProjectionShape)?, + projection_grid: u32::try_from(projection_ctas) + .map_err(|_| Error::InvalidQ5KProjectionShape)?, + }) } #[derive(Debug, PartialEq, Eq)] @@ -397,11 +478,17 @@ impl Context<'_> { let sync = self.driver.api.synchronize(); if sync.is_err() { self.poisoned.set(true); + } + let restore = guard.finish(); + if restore.is_err() { + self.poisoned.set(true); + } + if sync.is_err() || restore.is_err() { // Both scalars and their table may still be read. The raw owner // remains forgotten even if later context destruction succeeds. std::mem::forget(arguments); } - combine(combine(launch, sync), guard.finish()) + combine(combine(launch, sync), restore) } /// Compute each row's dot product in ascending column order with separate /// binary32 multiply/add rounding. All buffers are exact whole allocations; @@ -434,6 +521,157 @@ impl Context<'_> { .launch_matvec(kernel.function, shape.grid, &mut arguments); self.complete_launch(launch, arguments, guard) } + + /// Project one resident F32 activation through row-major Q5_K weights. + /// Activations are quantized using the native Q8_1 block/reduction contract. + pub fn q5_k_q8_1( + &self, + weights: &DeviceBuffer<'_>, + activation: &DeviceBuffer<'_>, + output: &mut DeviceBuffer<'_>, + shape: Q5KProjectionShape, + ) -> Result<()> { + let (shape, handles, temporary_bytes) = + self.q5_k_admission(weights, activation, output, shape)?; + + // Account the complete worst-case scratch/status requirement before + // the first context push, module load, or allocation. + let total = self + .allocated + .get() + .checked_add(temporary_bytes) + .ok_or(Error::BudgetExceeded)?; + if total > self.budget { + return Err(Error::BudgetExceeded); + } + + let scratch = self.allocate(shape.scratch_bytes)?; + let mut status = match self.allocate(4) { + Ok(status) => status, + Err(error) => { + if self.poisoned.get() { + std::mem::forget(scratch); + return Err(error); + } + return combine(Err(error), scratch.release()); + } + }; + + let operation = (|| { + status.copy_from(&[0; 4])?; + let kernels = { + let guard = self.enter()?; + let kernels = match self.q5_kernels() { + Ok(kernels) => kernels, + Err(error) => return combine(Err(error), guard.finish()), + }; + combine(Ok(kernels), guard.finish())? + }; + + let mut quant_arguments = ffi::Q8QuantizeArguments::new( + handles[1], + scratch.handle.ok_or(Error::ContextPoisoned)?, + status.handle.ok_or(Error::ContextPoisoned)?, + shape.columns, + ); + let quant_guard = self.enter()?; + let quantize = self.driver.api.launch_q8_quantize( + kernels.quantize, + shape.quant_grid, + &mut quant_arguments, + ); + self.complete_launch(quantize, quant_arguments, quant_guard)?; + + let mut native_status = [0u8; 4]; + status.copy_to(&mut native_status)?; + if u32::from_le_bytes(native_status) != 0 { + return Err(Error::InvalidQ8Activation); + } + + let mut project_arguments = ffi::Q5ProjectionArguments::new( + handles[0], + scratch.handle.ok_or(Error::ContextPoisoned)?, + handles[2], + shape.rows, + shape.columns, + ); + let project_guard = self.enter()?; + let project = self.driver.api.launch_q5_projection( + kernels.project, + shape.projection_grid, + &mut project_arguments, + ); + self.complete_launch(project, project_arguments, project_guard) + })(); + + self.release_q5_temporaries(operation, scratch, status) + } + + fn q5_k_admission( + &self, + weights: &DeviceBuffer<'_>, + activation: &DeviceBuffer<'_>, + output: &DeviceBuffer<'_>, + dimensions: Q5KProjectionShape, + ) -> Result<(Q5KShape, [ffi::BufferHandle; 3], usize)> { + if self.poisoned.get() || self.handle.is_none() { + return Err(Error::ContextPoisoned); + } + if !std::ptr::eq(self, weights.context) + || !std::ptr::eq(self, activation.context) + || !std::ptr::eq(self, output.context) + { + return Err(Error::ContextMismatch); + } + let shape = q5_k_shape(dimensions.rows, dimensions.columns)?; + if weights.bytes != shape.weight_bytes + || activation.bytes != shape.activation_bytes + || output.bytes != shape.output_bytes + { + return Err(Error::InvalidQ5KProjectionShape); + } + let weight = weights.handle.ok_or(Error::ContextPoisoned)?; + let activation = activation.handle.ok_or(Error::ContextPoisoned)?; + let output = output.handle.ok_or(Error::ContextPoisoned)?; + if output == weight || output == activation { + return Err(Error::OutputAliasesInput); + } + weight + .validate_extent(shape.weight_bytes) + .map_err(|_| Error::InvalidQ5KProjectionShape)?; + activation + .validate_extent(shape.activation_bytes) + .map_err(|_| Error::InvalidQ5KProjectionShape)?; + output + .validate_extent(shape.output_bytes) + .map_err(|_| Error::InvalidQ5KProjectionShape)?; + let temporary_bytes = shape + .scratch_bytes + .checked_add(4) + .ok_or(Error::BudgetExceeded)?; + Ok((shape, [weight, activation, output], temporary_bytes)) + } + + fn release_q5_temporaries( + &self, + operation: Result, + scratch: DeviceBuffer<'_>, + status: DeviceBuffer<'_>, + ) -> Result { + if self.poisoned.get() { + // Unknown completion/restoration means these allocations and + // their context accounting must outlive this call. + std::mem::forget(scratch); + std::mem::forget(status); + return operation; + } + let status_free = status.release(); + if self.poisoned.get() { + std::mem::forget(scratch); + return combine(operation, status_free); + } + combine(combine(operation, status_free), scratch.release()) + } fn matvec_admission( &self, buffers: [&DeviceBuffer<'_>; 3], @@ -464,6 +702,9 @@ impl Context<'_> { match operation { FixedOperation::VectorAdd => &self.vector_add_kernel, FixedOperation::Matvec => &self.matvec_kernel, + FixedOperation::Q5K | FixedOperation::Q5KQuantize | FixedOperation::Q5KProject => { + unreachable!("Q5_K kernels are cached as an atomic pair") + } } } fn fixed_kernel(&self, operation: FixedOperation) -> Result { @@ -484,6 +725,7 @@ impl Context<'_> { let cleanup = self.driver.api.unload(module); if cleanup.is_err() { self.poisoned.set(true); + std::mem::forget(Rc::clone(&self.driver.api)); } combine(Err(error), cleanup) } @@ -495,6 +737,8 @@ impl Context<'_> { return Ok(()); }; if self.poisoned.get() { + // An uncertain context may still own code from this module. + std::mem::forget(Rc::clone(&self.driver.api)); return Ok(()); } let guard = match self.enter() { @@ -510,6 +754,74 @@ impl Context<'_> { } combine(unload, guard.finish()) } + fn q5_kernels(&self) -> Result { + if let Some(kernels) = self.q5_k_kernel.get() { + return Ok(kernels); + } + let module = self.driver.api.load_fixed_module(FixedOperation::Q5K)?; + let quantize = match self + .driver + .api + .fixed_function(module, FixedOperation::Q5KQuantize) + { + Ok(function) => function, + Err(error) => { + let cleanup = self.driver.api.unload(module); + if cleanup.is_err() { + self.poisoned.set(true); + std::mem::forget(Rc::clone(&self.driver.api)); + } + return combine(Err(error), cleanup); + } + }; + let project = match self + .driver + .api + .fixed_function(module, FixedOperation::Q5KProject) + { + Ok(function) => function, + Err(error) => { + let cleanup = self.driver.api.unload(module); + if cleanup.is_err() { + self.poisoned.set(true); + std::mem::forget(Rc::clone(&self.driver.api)); + } + return combine(Err(error), cleanup); + } + }; + let kernels = Q5Kernels { + module, + quantize, + project, + }; + // Publish only after both fixed entries resolve successfully. + self.q5_k_kernel.set(Some(kernels)); + Ok(kernels) + } + fn unload_q5_kernels(&self) -> Result<()> { + let Some(kernels) = self.q5_k_kernel.take() else { + return Ok(()); + }; + if self.poisoned.get() { + // A failed completion makes module use uncertain; retain the + // Driver library with the intentionally abandoned module handle. + std::mem::forget(Rc::clone(&self.driver.api)); + return Ok(()); + } + let guard = match self.enter() { + Ok(guard) => guard, + Err(error) => { + self.poisoned.set(true); + return Err(error); + } + }; + let unload = self.driver.api.unload(kernels.module); + if unload.is_err() { + self.poisoned.set(true); + std::mem::forget(Rc::clone(&self.driver.api)); + } + combine(unload, guard.finish()) + } /// Consume the context and report destruction failure. Borrows prevent /// closing a context while a device buffer can still be used. pub fn close(mut self) -> Result<()> { @@ -518,8 +830,11 @@ impl Context<'_> { fn destroy(&mut self) -> Result<()> { // Fixed teardown order; shared poison suppresses the second native unload. let unload = combine( - self.unload_kernel(FixedOperation::VectorAdd), - self.unload_kernel(FixedOperation::Matvec), + combine( + self.unload_kernel(FixedOperation::VectorAdd), + self.unload_kernel(FixedOperation::Matvec), + ), + self.unload_q5_kernels(), ); if let Some(handle) = self.handle.take() { // cuCtxDestroy accepts an owned non-current context. Pushing here @@ -768,6 +1083,7 @@ mod tests { poisoned: Cell::new(false), vector_add_kernel: Cell::new(None), matvec_kernel: Cell::new(None), + q5_k_kernel: Cell::new(None), }; let make = |handle| DeviceBuffer { context: &context, diff --git a/crates/cuda-driver/src/q5_k_q8_1.ptx b/crates/cuda-driver/src/q5_k_q8_1.ptx new file mode 100644 index 00000000..df63ef3c --- /dev/null +++ b/crates/cuda-driver/src/q5_k_q8_1.ptx @@ -0,0 +1,1148 @@ +// Adapted from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d: +// ggml/src/ggml-cuda/{quantize.cu,vecdotq.cuh,mmvq.cu,common.cuh} +// and ggml/src/ggml-common.h. See docs/native-mmvq.md for exact scope. +// +// MIT License +// Copyright (c) 2023-2026 The ggml authors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + + +// +// Generated by NVIDIA NVVM Compiler +// +// Compiler Build ID: CL-36424714 +// Cuda compilation tools, release 13.0, V13.0.88 +// Based on NVVM 20.0.0 +// + +.version 9.0 +.target sm_120 +.address_size 64 + + // .globl leleka_quantize_q8_1 +// _ZZ16leleka_q5_k_q8_1E7partial has been demoted + +.visible .entry leleka_quantize_q8_1( + .param .u64 .ptr .align 1 leleka_quantize_q8_1_param_0, + .param .u64 .ptr .align 1 leleka_quantize_q8_1_param_1, + .param .u64 .ptr .align 1 leleka_quantize_q8_1_param_2, + .param .u32 leleka_quantize_q8_1_param_3 +) +.maxntid 256 +.minnctapersm 1 +{ + .reg .pred %p<38>; + .reg .b16 %rs<15>; + .reg .b32 %r<37>; + .reg .b32 %f<46>; + .reg .b64 %rd<14>; + + ld.param.u64 %rd3, [leleka_quantize_q8_1_param_0]; + ld.param.u64 %rd4, [leleka_quantize_q8_1_param_1]; + ld.param.u64 %rd5, [leleka_quantize_q8_1_param_2]; + ld.param.u32 %r4, [leleka_quantize_q8_1_param_3]; + cvta.to.global.u64 %rd1, %rd5; + mov.u32 %r5, %ctaid.x; + shl.b32 %r6, %r5, 8; + mov.u32 %r1, %tid.x; + or.b32 %r2, %r6, %r1; + setp.ge.s32 %p3, %r2, %r4; + @%p3 bra $L__BB0_18; + cvta.to.global.u64 %rd6, %rd3; + mul.wide.u32 %rd7, %r2, 4; + add.s64 %rd8, %rd6, %rd7; + ld.global.nc.f32 %f1, [%rd8]; + abs.ftz.f32 %f2, %f1; + mov.b32 %r7, %f2; + shfl.sync.bfly.b32 %r8|%p4, %r7, 16, 31, -1; + mov.b32 %f11, %r8; + max.ftz.f32 %f12, %f2, %f11; + mov.b32 %r9, %f12; + shfl.sync.bfly.b32 %r10|%p5, %r9, 8, 31, -1; + mov.b32 %f13, %r10; + max.ftz.f32 %f14, %f12, %f13; + mov.b32 %r11, %f14; + shfl.sync.bfly.b32 %r12|%p6, %r11, 4, 31, -1; + mov.b32 %f15, %r12; + max.ftz.f32 %f16, %f14, %f15; + mov.b32 %r13, %f16; + shfl.sync.bfly.b32 %r14|%p7, %r13, 2, 31, -1; + mov.b32 %f17, %r14; + max.ftz.f32 %f18, %f16, %f17; + mov.b32 %r15, %f18; + shfl.sync.bfly.b32 %r16|%p8, %r15, 1, 31, -1; + mov.b32 %f19, %r16; + max.ftz.f32 %f3, %f18, %f19; + mov.b32 %r17, %f1; + shfl.sync.bfly.b32 %r18|%p9, %r17, 16, 31, -1; + mov.b32 %f20, %r18; + add.ftz.f32 %f21, %f1, %f20; + mov.b32 %r19, %f21; + shfl.sync.bfly.b32 %r20|%p10, %r19, 8, 31, -1; + mov.b32 %f22, %r20; + add.ftz.f32 %f23, %f21, %f22; + mov.b32 %r21, %f23; + shfl.sync.bfly.b32 %r22|%p11, %r21, 4, 31, -1; + mov.b32 %f24, %r22; + add.ftz.f32 %f25, %f23, %f24; + mov.b32 %r23, %f25; + shfl.sync.bfly.b32 %r24|%p12, %r23, 2, 31, -1; + mov.b32 %f26, %r24; + add.ftz.f32 %f27, %f25, %f26; + mov.b32 %r25, %f27; + shfl.sync.bfly.b32 %r26|%p13, %r25, 1, 31, -1; + mov.b32 %f28, %r26; + add.ftz.f32 %f9, %f27, %f28; + mov.f32 %f29, 0f42FE0000; + div.approx.ftz.f32 %f8, %f3, %f29; + // begin inline asm + { cvt.rn.f16.f32 %rs5, %f8;} + + // end inline asm + // begin inline asm + { cvt.rn.f16.f32 %rs6, %f9;} + + // end inline asm + setp.eq.ftz.f32 %p14, %f3, 0f00000000; + mov.f32 %f7, 0f00000000; + @%p14 bra $L__BB0_3; + div.approx.ftz.f32 %f7, %f1, %f8; +$L__BB0_3: + abs.ftz.f32 %f30, %f3; + setp.equ.ftz.f32 %p16, %f30, 0f7F800000; + abs.ftz.f32 %f31, %f9; + setp.equ.ftz.f32 %p17, %f31, 0f7F800000; + or.pred %p18, %p16, %p17; + abs.ftz.f32 %f33, %f8; + setp.equ.ftz.f32 %p19, %f33, 0f7F800000; + or.pred %p20, %p18, %p19; + mov.pred %p37, -1; + @%p20 bra $L__BB0_8; + setp.eq.ftz.f32 %p21, %f3, 0f00000000; + @%p21 bra $L__BB0_6; + setp.eq.ftz.f32 %p23, %f8, 0f00000000; + abs.ftz.f32 %f35, %f7; + setp.equ.ftz.f32 %p24, %f35, 0f7F800000; + or.pred %p25, %p23, %p24; + @%p25 bra $L__BB0_8; +$L__BB0_6: + // begin inline asm + { cvt.f32.f16 %f37, %rs5;} + + // end inline asm + abs.ftz.f32 %f38, %f37; + setp.equ.ftz.f32 %p27, %f38, 0f7F800000; + @%p27 bra $L__BB0_8; + // begin inline asm + { cvt.f32.f16 %f39, %rs6;} + + // end inline asm + abs.ftz.f32 %f40, %f39; + setp.equ.ftz.f32 %p37, %f40, 0f7F800000; +$L__BB0_8: + setp.equ.ftz.f32 %p28, %f2, 0f7F800000; + mov.b32 %r27, -1; + vote.sync.ballot.b32 %r28, %p28, %r27; + vote.sync.ballot.b32 %r3, %p37, %r27; + setp.eq.s32 %p31, %r28, 0; + @%p31 bra $L__BB0_11; + and.b32 %r29, %r1, 31; + setp.ne.s32 %p32, %r29, 0; + mov.b16 %rs14, 0; + @%p32 bra $L__BB0_16; + atom.global.or.b32 %r30, [%rd1], 1; + bra.uni $L__BB0_16; +$L__BB0_11: + setp.eq.s32 %p33, %r3, 0; + @%p33 bra $L__BB0_14; + and.b32 %r31, %r1, 31; + setp.ne.s32 %p34, %r31, 0; + mov.b16 %rs14, 0; + @%p34 bra $L__BB0_16; + atom.global.or.b32 %r32, [%rd1], 2; + bra.uni $L__BB0_16; +$L__BB0_14: + setp.eq.ftz.f32 %p35, %f3, 0f00000000; + mov.b16 %rs14, 0; + @%p35 bra $L__BB0_16; + mov.f32 %f41, 0f3F000000; + copysign.f32 %f42, %f7, %f41; + add.rz.ftz.f32 %f43, %f7, %f42; + cvt.rzi.f32.f32 %f44, %f43; + cvt.rzi.ftz.s32.f32 %r33, %f44; + cvt.u16.u32 %rs14, %r33; +$L__BB0_16: + shr.u32 %r34, %r2, 5; + cvta.to.global.u64 %rd9, %rd4; + mul.wide.u32 %rd10, %r34, 36; + add.s64 %rd11, %rd9, %rd10; + add.s64 %rd2, %rd11, 4; + and.b32 %r35, %r1, 31; + cvt.u64.u32 %rd12, %r35; + add.s64 %rd13, %rd2, %rd12; + st.global.u8 [%rd13], %rs14; + setp.ne.s32 %p36, %r35, 0; + @%p36 bra $L__BB0_18; + mov.b32 %r36, {%rs5, %rs6}; + st.global.u32 [%rd2+-4], %r36; +$L__BB0_18: + ret; + +} + // .globl leleka_q5_k_q8_1 +.visible .entry leleka_q5_k_q8_1( + .param .u64 .ptr .align 1 leleka_q5_k_q8_1_param_0, + .param .u64 .ptr .align 1 leleka_q5_k_q8_1_param_1, + .param .u64 .ptr .align 1 leleka_q5_k_q8_1_param_2, + .param .u32 leleka_q5_k_q8_1_param_3, + .param .u32 leleka_q5_k_q8_1_param_4 +) +.maxntid 128 +.minnctapersm 1 +{ + .reg .pred %p<51>; + .reg .b16 %rs<36>; + .reg .b32 %r<482>; + .reg .b32 %f<177>; + .reg .b64 %rd<87>; + // demoted variable + .shared .align 4 .b8 _ZZ16leleka_q5_k_q8_1E7partial[1536]; + ld.param.u64 %rd25, [leleka_q5_k_q8_1_param_0]; + ld.param.u64 %rd26, [leleka_q5_k_q8_1_param_1]; + ld.param.u64 %rd27, [leleka_q5_k_q8_1_param_2]; + ld.param.u32 %r15, [leleka_q5_k_q8_1_param_3]; + ld.param.u32 %r16, [leleka_q5_k_q8_1_param_4]; + mov.u32 %r1, %tid.y; + shl.b32 %r17, %r1, 5; + mov.u32 %r2, %tid.x; + add.s32 %r18, %r17, %r2; + shr.s32 %r19, %r16, 31; + shr.u32 %r20, %r19, 24; + add.s32 %r21, %r16, %r20; + shr.s32 %r3, %r21, 8; + setp.lt.s32 %p1, %r16, 2048; + mov.u32 %r22, %ctaid.x; + selp.b32 %r23, 2, 0, %p1; + shl.b32 %r4, %r22, %r23; + shr.u32 %r481, %r18, 4; + setp.ge.s32 %p2, %r481, %r3; + mov.f32 %f169, 0f00000000; + mov.f32 %f170, %f169; + mov.f32 %f171, %f169; + mov.f32 %f172, %f169; + @%p2 bra $L__BB1_14; + shl.b32 %r24, %r2, 1; + and.b32 %r6, %r24, 30; + cvt.s64.s32 %rd28, %r4; + cvt.u64.u32 %rd29, %r3; + shr.u32 %r25, %r6, 3; + shl.b32 %r7, %r25, 1; + shl.b32 %r26, %r25, 5; + shl.b32 %r27, %r2, 2; + and.b32 %r28, %r27, 12; + or.b32 %r29, %r28, %r26; + cvt.u64.u32 %rd30, %r29; + cvt.u64.u32 %rd1, %r28; + add.s32 %r8, %r4, 1; + cvt.s64.s32 %rd31, %r8; + add.s32 %r9, %r4, 2; + cvt.s64.s32 %rd32, %r9; + add.s32 %r10, %r4, 3; + cvt.s64.s32 %rd33, %r10; + shl.b32 %r30, %r481, 3; + or.b32 %r31, %r30, %r7; + mul.wide.u32 %rd34, %r31, 36; + cvta.to.global.u64 %rd35, %rd26; + add.s64 %rd86, %rd35, %rd34; + mul.wide.u32 %rd36, %r1, 32; + cvt.u64.u32 %rd37, %r2; + add.s64 %rd38, %rd36, %rd37; + shr.u64 %rd39, %rd38, 4; + mad.lo.s64 %rd40, %rd28, %rd29, %rd39; + mul.lo.s64 %rd3, %rd40, 176; + cvt.u64.u32 %rd41, %r7; + and.b64 %rd42, %rd41, 2; + or.b64 %rd43, %rd3, %rd42; + or.b64 %rd4, %rd43, 8; + mad.lo.s64 %rd44, %rd31, %rd29, %rd39; + mul.lo.s64 %rd5, %rd44, 176; + or.b64 %rd45, %rd3, %rd1; + add.s64 %rd6, %rd45, 16; + add.s64 %rd46, %rd5, %rd30; + add.s64 %rd7, %rd46, 64; + add.s64 %rd47, %rd3, %rd30; + add.s64 %rd8, %rd47, 48; + or.b64 %rd48, %rd5, %rd1; + add.s64 %rd9, %rd48, 32; + mad.lo.s64 %rd49, %rd33, %rd29, %rd39; + mul.lo.s64 %rd10, %rd49, 176; + or.b64 %rd50, %rd10, %rd42; + or.b64 %rd11, %rd50, 8; + or.b64 %rd51, %rd5, %rd42; + or.b64 %rd12, %rd51, 12; + mad.lo.s64 %rd52, %rd32, %rd29, %rd39; + mul.lo.s64 %rd13, %rd52, 176; + or.b64 %rd53, %rd10, %rd1; + add.s64 %rd14, %rd53, 16; + add.s64 %rd54, %rd13, %rd30; + add.s64 %rd15, %rd54, 64; + add.s64 %rd55, %rd10, %rd30; + add.s64 %rd16, %rd55, 48; + or.b64 %rd56, %rd13, %rd1; + add.s64 %rd17, %rd56, 32; + or.b64 %rd57, %rd13, %rd42; + or.b64 %rd18, %rd57, 8; + cvta.to.global.u64 %rd85, %rd25; + mov.f32 %f169, 0f00000000; +$L__BB1_2: + setp.gt.s32 %p3, %r16, 2047; + @%p3 bra $L__BB1_11; + setp.ge.s32 %p6, %r4, %r15; + @%p6 bra $L__BB1_5; + setp.gt.u32 %p7, %r6, 15; + add.s64 %rd63, %rd85, %rd3; + add.s64 %rd64, %rd85, %rd8; + add.s64 %rd65, %rd85, %rd6; + ld.global.nc.u32 %r148, [%rd64]; + ld.global.nc.u32 %r149, [%rd64+16]; + ld.global.nc.u32 %r150, [%rd65]; + shr.s32 %r151, %r150, %r7; + ld.global.nc.u32 %r152, [%rd65+16]; + shr.s32 %r153, %r152, %r7; + add.s64 %rd66, %rd85, %rd4; + ld.global.nc.u16 %rs8, [%rd66+-4]; + cvt.u32.u16 %r154, %rs8; + ld.global.nc.u16 %rs9, [%rd66]; + cvt.u32.u16 %r155, %rs9; + ld.global.nc.u16 %rs10, [%rd66+4]; + cvt.u32.u16 %r156, %rs10; + and.b32 %r157, %r154, 16191; + and.b32 %r158, %r156, 3855; + shr.u32 %r159, %r154, 2; + and.b32 %r160, %r159, 12336; + or.b32 %r161, %r158, %r160; + selp.b32 %r162, %r161, %r157, %p7; + cvt.u16.u32 %rs11, %r162; + and.b32 %r163, %r155, 16191; + shr.u32 %r164, %r156, 4; + and.b32 %r165, %r164, 3855; + shr.u32 %r166, %r155, 2; + and.b32 %r167, %r166, 12336; + or.b32 %r168, %r165, %r167; + selp.b32 %r169, %r168, %r163, %p7; + cvt.u16.u32 %rs12, %r169; + ld.global.nc.u32 %r112, [%rd86]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r112; + cvt.f32.f16 %f42, low;} + + // end inline asm + add.s64 %rd67, %rd86, %rd1; + ld.global.nc.u32 %r120, [%rd67+4]; + ld.global.nc.u32 %r116, [%rd67+20]; + ld.global.nc.u32 %r113, [%rd86+36]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r113; + cvt.f32.f16 %f43, low;} + + // end inline asm + ld.global.nc.u32 %r136, [%rd67+40]; + ld.global.nc.u32 %r132, [%rd67+56]; + and.b32 %r170, %r148, 252645135; + and.b32 %r171, %r149, 252645135; + shl.b32 %r172, %r151, 4; + and.b32 %r173, %r172, 269488144; + shl.b32 %r174, %r153, 4; + and.b32 %r175, %r174, 269488144; + or.b32 %r119, %r170, %r173; + or.b32 %r115, %r171, %r175; + mov.b32 %r141, 0; + // begin inline asm + dp4a.s32.s32 %r114, %r115, %r116, %r141; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r118, %r119, %r120, %r114; + // end inline asm + mov.b32 %r143, 16843009; + // begin inline asm + dp4a.s32.s32 %r122, %r143, %r116, %r141; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r126, %r143, %r120, %r122; + // end inline asm + shr.u16 %rs13, %rs11, 8; + and.b32 %r176, %r162, 63; + mul.lo.s32 %r177, %r118, %r176; + cvt.rn.f32.s32 %f46, %r177; + fma.rn.ftz.f32 %f47, %f42, %f46, 0f00000000; + shr.u16 %rs14, %rs12, 8; + and.b32 %r178, %r169, 63; + mul.lo.s32 %r179, %r126, %r178; + cvt.rn.f32.s32 %f48, %r179; + fma.rn.ftz.f32 %f49, %f42, %f48, 0f00000000; + shr.u32 %r180, %r148, 4; + and.b32 %r181, %r180, 252645135; + shr.u32 %r182, %r149, 4; + and.b32 %r183, %r182, 252645135; + shl.b32 %r184, %r151, 3; + and.b32 %r185, %r184, 269488144; + shl.b32 %r186, %r153, 3; + and.b32 %r187, %r186, 269488144; + or.b32 %r135, %r181, %r185; + or.b32 %r131, %r183, %r187; + // begin inline asm + dp4a.s32.s32 %r130, %r131, %r132, %r141; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r134, %r135, %r136, %r130; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r138, %r143, %r132, %r141; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r142, %r143, %r136, %r138; + // end inline asm + cvt.u32.u16 %r188, %rs13; + mul.lo.s32 %r189, %r134, %r188; + cvt.rn.f32.s32 %f50, %r189; + fma.rn.ftz.f32 %f51, %f43, %f50, %f47; + cvt.u32.u16 %r190, %rs14; + mul.lo.s32 %r191, %r142, %r190; + cvt.rn.f32.s32 %f52, %r191; + fma.rn.ftz.f32 %f53, %f43, %f52, %f49; + ld.global.nc.u32 %r147, [%rd63]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r147; + cvt.f32.f16 %f44, low;} + + // end inline asm + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r147; + cvt.f32.f16 %f45, high;} + + // end inline asm + mul.ftz.f32 %f54, %f51, %f44; + mul.ftz.f32 %f55, %f53, %f45; + sub.ftz.f32 %f56, %f54, %f55; + add.ftz.f32 %f172, %f56, %f172; +$L__BB1_5: + setp.ge.s32 %p8, %r8, %r15; + @%p8 bra $L__BB1_7; + setp.gt.u32 %p9, %r6, 15; + add.s64 %rd68, %rd85, %rd5; + add.s64 %rd69, %rd85, %rd7; + add.s64 %rd70, %rd85, %rd9; + ld.global.nc.u32 %r228, [%rd69+-16]; + ld.global.nc.u32 %r229, [%rd69]; + ld.global.nc.u32 %r230, [%rd70+-16]; + shr.s32 %r231, %r230, %r7; + ld.global.nc.u32 %r232, [%rd70]; + shr.s32 %r233, %r232, %r7; + add.s64 %rd71, %rd85, %rd12; + ld.global.nc.u16 %rs15, [%rd71+-8]; + cvt.u32.u16 %r234, %rs15; + ld.global.nc.u16 %rs16, [%rd71+-4]; + cvt.u32.u16 %r235, %rs16; + ld.global.nc.u16 %rs17, [%rd71]; + cvt.u32.u16 %r236, %rs17; + and.b32 %r237, %r234, 16191; + and.b32 %r238, %r236, 3855; + shr.u32 %r239, %r234, 2; + and.b32 %r240, %r239, 12336; + or.b32 %r241, %r238, %r240; + selp.b32 %r242, %r241, %r237, %p9; + cvt.u16.u32 %rs18, %r242; + and.b32 %r243, %r235, 16191; + shr.u32 %r244, %r236, 4; + and.b32 %r245, %r244, 3855; + shr.u32 %r246, %r235, 2; + and.b32 %r247, %r246, 12336; + or.b32 %r248, %r245, %r247; + selp.b32 %r249, %r248, %r243, %p9; + cvt.u16.u32 %rs19, %r249; + ld.global.nc.u32 %r192, [%rd86]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r192; + cvt.f32.f16 %f57, low;} + + // end inline asm + add.s64 %rd72, %rd86, %rd1; + ld.global.nc.u32 %r200, [%rd72+4]; + ld.global.nc.u32 %r196, [%rd72+20]; + ld.global.nc.u32 %r193, [%rd86+36]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r193; + cvt.f32.f16 %f58, low;} + + // end inline asm + ld.global.nc.u32 %r216, [%rd72+40]; + ld.global.nc.u32 %r212, [%rd72+56]; + and.b32 %r250, %r228, 252645135; + and.b32 %r251, %r229, 252645135; + shl.b32 %r252, %r231, 4; + and.b32 %r253, %r252, 269488144; + shl.b32 %r254, %r233, 4; + and.b32 %r255, %r254, 269488144; + or.b32 %r199, %r250, %r253; + or.b32 %r195, %r251, %r255; + mov.b32 %r221, 0; + // begin inline asm + dp4a.s32.s32 %r194, %r195, %r196, %r221; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r198, %r199, %r200, %r194; + // end inline asm + mov.b32 %r223, 16843009; + // begin inline asm + dp4a.s32.s32 %r202, %r223, %r196, %r221; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r206, %r223, %r200, %r202; + // end inline asm + shr.u16 %rs20, %rs18, 8; + and.b32 %r256, %r242, 63; + mul.lo.s32 %r257, %r198, %r256; + cvt.rn.f32.s32 %f61, %r257; + fma.rn.ftz.f32 %f62, %f57, %f61, 0f00000000; + shr.u16 %rs21, %rs19, 8; + and.b32 %r258, %r249, 63; + mul.lo.s32 %r259, %r206, %r258; + cvt.rn.f32.s32 %f63, %r259; + fma.rn.ftz.f32 %f64, %f57, %f63, 0f00000000; + shr.u32 %r260, %r228, 4; + and.b32 %r261, %r260, 252645135; + shr.u32 %r262, %r229, 4; + and.b32 %r263, %r262, 252645135; + shl.b32 %r264, %r231, 3; + and.b32 %r265, %r264, 269488144; + shl.b32 %r266, %r233, 3; + and.b32 %r267, %r266, 269488144; + or.b32 %r215, %r261, %r265; + or.b32 %r211, %r263, %r267; + // begin inline asm + dp4a.s32.s32 %r210, %r211, %r212, %r221; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r214, %r215, %r216, %r210; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r218, %r223, %r212, %r221; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r222, %r223, %r216, %r218; + // end inline asm + cvt.u32.u16 %r268, %rs20; + mul.lo.s32 %r269, %r214, %r268; + cvt.rn.f32.s32 %f65, %r269; + fma.rn.ftz.f32 %f66, %f58, %f65, %f62; + cvt.u32.u16 %r270, %rs21; + mul.lo.s32 %r271, %r222, %r270; + cvt.rn.f32.s32 %f67, %r271; + fma.rn.ftz.f32 %f68, %f58, %f67, %f64; + ld.global.nc.u32 %r227, [%rd68]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r227; + cvt.f32.f16 %f59, low;} + + // end inline asm + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r227; + cvt.f32.f16 %f60, high;} + + // end inline asm + mul.ftz.f32 %f69, %f66, %f59; + mul.ftz.f32 %f70, %f68, %f60; + sub.ftz.f32 %f71, %f69, %f70; + add.ftz.f32 %f171, %f71, %f171; +$L__BB1_7: + setp.ge.s32 %p10, %r9, %r15; + @%p10 bra $L__BB1_9; + setp.gt.u32 %p11, %r6, 15; + add.s64 %rd73, %rd85, %rd13; + add.s64 %rd74, %rd85, %rd15; + add.s64 %rd75, %rd85, %rd17; + ld.global.nc.u32 %r308, [%rd74+-16]; + ld.global.nc.u32 %r309, [%rd74]; + ld.global.nc.u32 %r310, [%rd75+-16]; + shr.s32 %r311, %r310, %r7; + ld.global.nc.u32 %r312, [%rd75]; + shr.s32 %r313, %r312, %r7; + add.s64 %rd76, %rd85, %rd18; + ld.global.nc.u16 %rs22, [%rd76+-4]; + cvt.u32.u16 %r314, %rs22; + ld.global.nc.u16 %rs23, [%rd76]; + cvt.u32.u16 %r315, %rs23; + ld.global.nc.u16 %rs24, [%rd76+4]; + cvt.u32.u16 %r316, %rs24; + and.b32 %r317, %r314, 16191; + and.b32 %r318, %r316, 3855; + shr.u32 %r319, %r314, 2; + and.b32 %r320, %r319, 12336; + or.b32 %r321, %r318, %r320; + selp.b32 %r322, %r321, %r317, %p11; + cvt.u16.u32 %rs25, %r322; + and.b32 %r323, %r315, 16191; + shr.u32 %r324, %r316, 4; + and.b32 %r325, %r324, 3855; + shr.u32 %r326, %r315, 2; + and.b32 %r327, %r326, 12336; + or.b32 %r328, %r325, %r327; + selp.b32 %r329, %r328, %r323, %p11; + cvt.u16.u32 %rs26, %r329; + ld.global.nc.u32 %r272, [%rd86]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r272; + cvt.f32.f16 %f72, low;} + + // end inline asm + add.s64 %rd77, %rd86, %rd1; + ld.global.nc.u32 %r280, [%rd77+4]; + ld.global.nc.u32 %r276, [%rd77+20]; + ld.global.nc.u32 %r273, [%rd86+36]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r273; + cvt.f32.f16 %f73, low;} + + // end inline asm + ld.global.nc.u32 %r296, [%rd77+40]; + ld.global.nc.u32 %r292, [%rd77+56]; + and.b32 %r330, %r308, 252645135; + and.b32 %r331, %r309, 252645135; + shl.b32 %r332, %r311, 4; + and.b32 %r333, %r332, 269488144; + shl.b32 %r334, %r313, 4; + and.b32 %r335, %r334, 269488144; + or.b32 %r279, %r330, %r333; + or.b32 %r275, %r331, %r335; + mov.b32 %r301, 0; + // begin inline asm + dp4a.s32.s32 %r274, %r275, %r276, %r301; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r278, %r279, %r280, %r274; + // end inline asm + mov.b32 %r303, 16843009; + // begin inline asm + dp4a.s32.s32 %r282, %r303, %r276, %r301; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r286, %r303, %r280, %r282; + // end inline asm + shr.u16 %rs27, %rs25, 8; + and.b32 %r336, %r322, 63; + mul.lo.s32 %r337, %r278, %r336; + cvt.rn.f32.s32 %f76, %r337; + fma.rn.ftz.f32 %f77, %f72, %f76, 0f00000000; + shr.u16 %rs28, %rs26, 8; + and.b32 %r338, %r329, 63; + mul.lo.s32 %r339, %r286, %r338; + cvt.rn.f32.s32 %f78, %r339; + fma.rn.ftz.f32 %f79, %f72, %f78, 0f00000000; + shr.u32 %r340, %r308, 4; + and.b32 %r341, %r340, 252645135; + shr.u32 %r342, %r309, 4; + and.b32 %r343, %r342, 252645135; + shl.b32 %r344, %r311, 3; + and.b32 %r345, %r344, 269488144; + shl.b32 %r346, %r313, 3; + and.b32 %r347, %r346, 269488144; + or.b32 %r295, %r341, %r345; + or.b32 %r291, %r343, %r347; + // begin inline asm + dp4a.s32.s32 %r290, %r291, %r292, %r301; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r294, %r295, %r296, %r290; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r298, %r303, %r292, %r301; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r302, %r303, %r296, %r298; + // end inline asm + cvt.u32.u16 %r348, %rs27; + mul.lo.s32 %r349, %r294, %r348; + cvt.rn.f32.s32 %f80, %r349; + fma.rn.ftz.f32 %f81, %f73, %f80, %f77; + cvt.u32.u16 %r350, %rs28; + mul.lo.s32 %r351, %r302, %r350; + cvt.rn.f32.s32 %f82, %r351; + fma.rn.ftz.f32 %f83, %f73, %f82, %f79; + ld.global.nc.u32 %r307, [%rd73]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r307; + cvt.f32.f16 %f74, low;} + + // end inline asm + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r307; + cvt.f32.f16 %f75, high;} + + // end inline asm + mul.ftz.f32 %f84, %f81, %f74; + mul.ftz.f32 %f85, %f83, %f75; + sub.ftz.f32 %f86, %f84, %f85; + add.ftz.f32 %f170, %f86, %f170; +$L__BB1_9: + setp.ge.s32 %p12, %r10, %r15; + @%p12 bra $L__BB1_13; + setp.gt.u32 %p13, %r6, 15; + add.s64 %rd78, %rd85, %rd10; + add.s64 %rd79, %rd85, %rd16; + add.s64 %rd80, %rd85, %rd14; + ld.global.nc.u32 %r388, [%rd79]; + ld.global.nc.u32 %r389, [%rd79+16]; + ld.global.nc.u32 %r390, [%rd80]; + shr.s32 %r391, %r390, %r7; + ld.global.nc.u32 %r392, [%rd80+16]; + shr.s32 %r393, %r392, %r7; + add.s64 %rd81, %rd85, %rd11; + ld.global.nc.u16 %rs29, [%rd81+-4]; + cvt.u32.u16 %r394, %rs29; + ld.global.nc.u16 %rs30, [%rd81]; + cvt.u32.u16 %r395, %rs30; + ld.global.nc.u16 %rs31, [%rd81+4]; + cvt.u32.u16 %r396, %rs31; + and.b32 %r397, %r394, 16191; + and.b32 %r398, %r396, 3855; + shr.u32 %r399, %r394, 2; + and.b32 %r400, %r399, 12336; + or.b32 %r401, %r398, %r400; + selp.b32 %r402, %r401, %r397, %p13; + cvt.u16.u32 %rs32, %r402; + and.b32 %r403, %r395, 16191; + shr.u32 %r404, %r396, 4; + and.b32 %r405, %r404, 3855; + shr.u32 %r406, %r395, 2; + and.b32 %r407, %r406, 12336; + or.b32 %r408, %r405, %r407; + selp.b32 %r409, %r408, %r403, %p13; + cvt.u16.u32 %rs33, %r409; + ld.global.nc.u32 %r352, [%rd86]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r352; + cvt.f32.f16 %f87, low;} + + // end inline asm + add.s64 %rd82, %rd86, %rd1; + ld.global.nc.u32 %r360, [%rd82+4]; + ld.global.nc.u32 %r356, [%rd82+20]; + ld.global.nc.u32 %r353, [%rd86+36]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r353; + cvt.f32.f16 %f88, low;} + + // end inline asm + ld.global.nc.u32 %r376, [%rd82+40]; + ld.global.nc.u32 %r372, [%rd82+56]; + and.b32 %r410, %r388, 252645135; + and.b32 %r411, %r389, 252645135; + shl.b32 %r412, %r391, 4; + and.b32 %r413, %r412, 269488144; + shl.b32 %r414, %r393, 4; + and.b32 %r415, %r414, 269488144; + or.b32 %r359, %r410, %r413; + or.b32 %r355, %r411, %r415; + mov.b32 %r381, 0; + // begin inline asm + dp4a.s32.s32 %r354, %r355, %r356, %r381; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r358, %r359, %r360, %r354; + // end inline asm + mov.b32 %r383, 16843009; + // begin inline asm + dp4a.s32.s32 %r362, %r383, %r356, %r381; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r366, %r383, %r360, %r362; + // end inline asm + shr.u16 %rs34, %rs32, 8; + and.b32 %r416, %r402, 63; + mul.lo.s32 %r417, %r358, %r416; + cvt.rn.f32.s32 %f91, %r417; + fma.rn.ftz.f32 %f92, %f87, %f91, 0f00000000; + shr.u16 %rs35, %rs33, 8; + and.b32 %r418, %r409, 63; + mul.lo.s32 %r419, %r366, %r418; + cvt.rn.f32.s32 %f93, %r419; + fma.rn.ftz.f32 %f94, %f87, %f93, 0f00000000; + shr.u32 %r420, %r388, 4; + and.b32 %r421, %r420, 252645135; + shr.u32 %r422, %r389, 4; + and.b32 %r423, %r422, 252645135; + shl.b32 %r424, %r391, 3; + and.b32 %r425, %r424, 269488144; + shl.b32 %r426, %r393, 3; + and.b32 %r427, %r426, 269488144; + or.b32 %r375, %r421, %r425; + or.b32 %r371, %r423, %r427; + // begin inline asm + dp4a.s32.s32 %r370, %r371, %r372, %r381; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r374, %r375, %r376, %r370; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r378, %r383, %r372, %r381; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r382, %r383, %r376, %r378; + // end inline asm + cvt.u32.u16 %r428, %rs34; + mul.lo.s32 %r429, %r374, %r428; + cvt.rn.f32.s32 %f95, %r429; + fma.rn.ftz.f32 %f96, %f88, %f95, %f92; + cvt.u32.u16 %r430, %rs35; + mul.lo.s32 %r431, %r382, %r430; + cvt.rn.f32.s32 %f97, %r431; + fma.rn.ftz.f32 %f98, %f88, %f97, %f94; + ld.global.nc.u32 %r387, [%rd78]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r387; + cvt.f32.f16 %f89, low;} + + // end inline asm + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r387; + cvt.f32.f16 %f90, high;} + + // end inline asm + mul.ftz.f32 %f99, %f96, %f89; + mul.ftz.f32 %f100, %f98, %f90; + sub.ftz.f32 %f101, %f99, %f100; + add.ftz.f32 %f169, %f101, %f169; + bra.uni $L__BB1_13; +$L__BB1_11: + setp.ge.s32 %p4, %r4, %r15; + @%p4 bra $L__BB1_13; + setp.gt.u32 %p5, %r6, 15; + add.s64 %rd58, %rd85, %rd3; + add.s64 %rd59, %rd85, %rd8; + add.s64 %rd60, %rd85, %rd6; + ld.global.nc.u32 %r68, [%rd59]; + ld.global.nc.u32 %r69, [%rd59+16]; + ld.global.nc.u32 %r70, [%rd60]; + shr.s32 %r71, %r70, %r7; + ld.global.nc.u32 %r72, [%rd60+16]; + shr.s32 %r73, %r72, %r7; + add.s64 %rd61, %rd85, %rd4; + ld.global.nc.u16 %rs1, [%rd61+-4]; + cvt.u32.u16 %r74, %rs1; + ld.global.nc.u16 %rs2, [%rd61]; + cvt.u32.u16 %r75, %rs2; + ld.global.nc.u16 %rs3, [%rd61+4]; + cvt.u32.u16 %r76, %rs3; + and.b32 %r77, %r74, 16191; + and.b32 %r78, %r76, 3855; + shr.u32 %r79, %r74, 2; + and.b32 %r80, %r79, 12336; + or.b32 %r81, %r78, %r80; + selp.b32 %r82, %r81, %r77, %p5; + cvt.u16.u32 %rs4, %r82; + and.b32 %r83, %r75, 16191; + shr.u32 %r84, %r76, 4; + and.b32 %r85, %r84, 3855; + shr.u32 %r86, %r75, 2; + and.b32 %r87, %r86, 12336; + or.b32 %r88, %r85, %r87; + selp.b32 %r89, %r88, %r83, %p5; + cvt.u16.u32 %rs5, %r89; + ld.global.nc.u32 %r32, [%rd86]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r32; + cvt.f32.f16 %f27, low;} + + // end inline asm + add.s64 %rd62, %rd86, %rd1; + ld.global.nc.u32 %r40, [%rd62+4]; + ld.global.nc.u32 %r36, [%rd62+20]; + ld.global.nc.u32 %r33, [%rd86+36]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r33; + cvt.f32.f16 %f28, low;} + + // end inline asm + ld.global.nc.u32 %r56, [%rd62+40]; + ld.global.nc.u32 %r52, [%rd62+56]; + and.b32 %r90, %r68, 252645135; + and.b32 %r91, %r69, 252645135; + shl.b32 %r92, %r71, 4; + and.b32 %r93, %r92, 269488144; + shl.b32 %r94, %r73, 4; + and.b32 %r95, %r94, 269488144; + or.b32 %r39, %r90, %r93; + or.b32 %r35, %r91, %r95; + mov.b32 %r61, 0; + // begin inline asm + dp4a.s32.s32 %r34, %r35, %r36, %r61; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r38, %r39, %r40, %r34; + // end inline asm + mov.b32 %r63, 16843009; + // begin inline asm + dp4a.s32.s32 %r42, %r63, %r36, %r61; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r46, %r63, %r40, %r42; + // end inline asm + shr.u16 %rs6, %rs4, 8; + and.b32 %r96, %r82, 63; + mul.lo.s32 %r97, %r38, %r96; + cvt.rn.f32.s32 %f31, %r97; + fma.rn.ftz.f32 %f32, %f27, %f31, 0f00000000; + shr.u16 %rs7, %rs5, 8; + and.b32 %r98, %r89, 63; + mul.lo.s32 %r99, %r46, %r98; + cvt.rn.f32.s32 %f33, %r99; + fma.rn.ftz.f32 %f34, %f27, %f33, 0f00000000; + shr.u32 %r100, %r68, 4; + and.b32 %r101, %r100, 252645135; + shr.u32 %r102, %r69, 4; + and.b32 %r103, %r102, 252645135; + shl.b32 %r104, %r71, 3; + and.b32 %r105, %r104, 269488144; + shl.b32 %r106, %r73, 3; + and.b32 %r107, %r106, 269488144; + or.b32 %r55, %r101, %r105; + or.b32 %r51, %r103, %r107; + // begin inline asm + dp4a.s32.s32 %r50, %r51, %r52, %r61; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r54, %r55, %r56, %r50; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r58, %r63, %r52, %r61; + // end inline asm + // begin inline asm + dp4a.s32.s32 %r62, %r63, %r56, %r58; + // end inline asm + cvt.u32.u16 %r108, %rs6; + mul.lo.s32 %r109, %r54, %r108; + cvt.rn.f32.s32 %f35, %r109; + fma.rn.ftz.f32 %f36, %f28, %f35, %f32; + cvt.u32.u16 %r110, %rs7; + mul.lo.s32 %r111, %r62, %r110; + cvt.rn.f32.s32 %f37, %r111; + fma.rn.ftz.f32 %f38, %f28, %f37, %f34; + ld.global.nc.u32 %r67, [%rd58]; + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r67; + cvt.f32.f16 %f29, low;} + + // end inline asm + // begin inline asm + {.reg .f16 low,high; + mov.b32 {low,high},%r67; + cvt.f32.f16 %f30, high;} + + // end inline asm + mul.ftz.f32 %f39, %f36, %f29; + mul.ftz.f32 %f40, %f38, %f30; + sub.ftz.f32 %f41, %f39, %f40; + add.ftz.f32 %f172, %f41, %f172; +$L__BB1_13: + add.s32 %r481, %r481, 8; + add.s64 %rd86, %rd86, 2304; + add.s64 %rd85, %rd85, 1408; + setp.lt.s32 %p14, %r481, %r3; + @%p14 bra $L__BB1_2; +$L__BB1_14: + setp.eq.s32 %p15, %r1, 0; + @%p15 bra $L__BB1_17; + setp.gt.s32 %p16, %r16, 2047; + shl.b32 %r432, %r1, 9; + mov.u32 %r433, _ZZ16leleka_q5_k_q8_1E7partial; + add.s32 %r434, %r433, %r432; + shl.b32 %r435, %r2, 2; + add.s32 %r13, %r434, %r435; + st.shared.f32 [%r13+-512], %f172; + @%p16 bra $L__BB1_17; + st.shared.f32 [%r13+-384], %f171; + st.shared.f32 [%r13+-256], %f170; + st.shared.f32 [%r13+-128], %f169; +$L__BB1_17: + setp.ne.s32 %p17, %r1, 0; + bar.sync 0; + @%p17 bra $L__BB1_27; + shl.b32 %r436, %r2, 2; + mov.u32 %r437, _ZZ16leleka_q5_k_q8_1E7partial; + add.s32 %r14, %r437, %r436; + ld.shared.f32 %f102, [%r14]; + add.ftz.f32 %f103, %f102, %f172; + ld.shared.f32 %f104, [%r14+512]; + add.ftz.f32 %f105, %f104, %f103; + ld.shared.f32 %f106, [%r14+1024]; + add.ftz.f32 %f107, %f106, %f105; + mov.b32 %r438, %f107; + shfl.sync.bfly.b32 %r439|%p18, %r438, 16, 31, -1; + mov.b32 %f108, %r439; + add.ftz.f32 %f109, %f107, %f108; + mov.b32 %r440, %f109; + shfl.sync.bfly.b32 %r441|%p19, %r440, 8, 31, -1; + mov.b32 %f110, %r441; + add.ftz.f32 %f111, %f109, %f110; + mov.b32 %r442, %f111; + shfl.sync.bfly.b32 %r443|%p20, %r442, 4, 31, -1; + mov.b32 %f112, %r443; + add.ftz.f32 %f113, %f111, %f112; + mov.b32 %r444, %f113; + shfl.sync.bfly.b32 %r445|%p21, %r444, 2, 31, -1; + mov.b32 %f114, %r445; + add.ftz.f32 %f115, %f113, %f114; + mov.b32 %r446, %f115; + shfl.sync.bfly.b32 %r447|%p22, %r446, 1, 31, -1; + mov.b32 %f116, %r447; + add.ftz.f32 %f21, %f115, %f116; + setp.ne.s32 %p23, %r2, 0; + setp.ge.s32 %p24, %r4, %r15; + cvta.to.global.u64 %rd83, %rd27; + mul.wide.s32 %rd84, %r4, 4; + add.s64 %rd24, %rd83, %rd84; + or.pred %p25, %p23, %p24; + @%p25 bra $L__BB1_20; + st.global.f32 [%rd24], %f21; +$L__BB1_20: + setp.gt.s32 %p26, %r16, 2047; + @%p26 bra $L__BB1_27; + ld.shared.f32 %f117, [%r14+128]; + add.ftz.f32 %f118, %f117, %f171; + ld.shared.f32 %f119, [%r14+640]; + add.ftz.f32 %f120, %f119, %f118; + ld.shared.f32 %f121, [%r14+1152]; + add.ftz.f32 %f122, %f121, %f120; + mov.b32 %r448, %f122; + shfl.sync.bfly.b32 %r449|%p27, %r448, 16, 31, -1; + mov.b32 %f123, %r449; + add.ftz.f32 %f124, %f122, %f123; + mov.b32 %r450, %f124; + shfl.sync.bfly.b32 %r451|%p28, %r450, 8, 31, -1; + mov.b32 %f125, %r451; + add.ftz.f32 %f126, %f124, %f125; + mov.b32 %r452, %f126; + shfl.sync.bfly.b32 %r453|%p29, %r452, 4, 31, -1; + mov.b32 %f127, %r453; + add.ftz.f32 %f128, %f126, %f127; + mov.b32 %r454, %f128; + shfl.sync.bfly.b32 %r455|%p30, %r454, 2, 31, -1; + mov.b32 %f129, %r455; + add.ftz.f32 %f130, %f128, %f129; + mov.b32 %r456, %f130; + shfl.sync.bfly.b32 %r457|%p31, %r456, 1, 31, -1; + mov.b32 %f131, %r457; + add.ftz.f32 %f22, %f130, %f131; + setp.ne.s32 %p32, %r2, 1; + add.s32 %r458, %r4, 1; + setp.ge.s32 %p33, %r458, %r15; + or.pred %p34, %p32, %p33; + @%p34 bra $L__BB1_23; + st.global.f32 [%rd24+4], %f22; +$L__BB1_23: + ld.shared.f32 %f132, [%r14+256]; + add.ftz.f32 %f133, %f132, %f170; + ld.shared.f32 %f134, [%r14+768]; + add.ftz.f32 %f135, %f134, %f133; + ld.shared.f32 %f136, [%r14+1280]; + add.ftz.f32 %f137, %f136, %f135; + mov.b32 %r459, %f137; + shfl.sync.bfly.b32 %r460|%p35, %r459, 16, 31, -1; + mov.b32 %f138, %r460; + add.ftz.f32 %f139, %f137, %f138; + mov.b32 %r461, %f139; + shfl.sync.bfly.b32 %r462|%p36, %r461, 8, 31, -1; + mov.b32 %f140, %r462; + add.ftz.f32 %f141, %f139, %f140; + mov.b32 %r463, %f141; + shfl.sync.bfly.b32 %r464|%p37, %r463, 4, 31, -1; + mov.b32 %f142, %r464; + add.ftz.f32 %f143, %f141, %f142; + mov.b32 %r465, %f143; + shfl.sync.bfly.b32 %r466|%p38, %r465, 2, 31, -1; + mov.b32 %f144, %r466; + add.ftz.f32 %f145, %f143, %f144; + mov.b32 %r467, %f145; + shfl.sync.bfly.b32 %r468|%p39, %r467, 1, 31, -1; + mov.b32 %f146, %r468; + add.ftz.f32 %f23, %f145, %f146; + setp.ne.s32 %p40, %r2, 2; + add.s32 %r469, %r4, 2; + setp.ge.s32 %p41, %r469, %r15; + or.pred %p42, %p40, %p41; + @%p42 bra $L__BB1_25; + st.global.f32 [%rd24+8], %f23; +$L__BB1_25: + ld.shared.f32 %f147, [%r14+384]; + add.ftz.f32 %f148, %f147, %f169; + ld.shared.f32 %f149, [%r14+896]; + add.ftz.f32 %f150, %f149, %f148; + ld.shared.f32 %f151, [%r14+1408]; + add.ftz.f32 %f152, %f151, %f150; + mov.b32 %r470, %f152; + shfl.sync.bfly.b32 %r471|%p43, %r470, 16, 31, -1; + mov.b32 %f153, %r471; + add.ftz.f32 %f154, %f152, %f153; + mov.b32 %r472, %f154; + shfl.sync.bfly.b32 %r473|%p44, %r472, 8, 31, -1; + mov.b32 %f155, %r473; + add.ftz.f32 %f156, %f154, %f155; + mov.b32 %r474, %f156; + shfl.sync.bfly.b32 %r475|%p45, %r474, 4, 31, -1; + mov.b32 %f157, %r475; + add.ftz.f32 %f158, %f156, %f157; + mov.b32 %r476, %f158; + shfl.sync.bfly.b32 %r477|%p46, %r476, 2, 31, -1; + mov.b32 %f159, %r477; + add.ftz.f32 %f160, %f158, %f159; + mov.b32 %r478, %f160; + shfl.sync.bfly.b32 %r479|%p47, %r478, 1, 31, -1; + mov.b32 %f161, %r479; + add.ftz.f32 %f24, %f160, %f161; + setp.ne.s32 %p48, %r2, 3; + add.s32 %r480, %r4, 3; + setp.ge.s32 %p49, %r480, %r15; + or.pred %p50, %p48, %p49; + @%p50 bra $L__BB1_27; + st.global.f32 [%rd24+12], %f24; +$L__BB1_27: + ret; + +} diff --git a/crates/cuda-driver/tests/q5_gpu.rs b/crates/cuda-driver/tests/q5_gpu.rs new file mode 100644 index 00000000..01531d0f --- /dev/null +++ b/crates/cuda-driver/tests/q5_gpu.rs @@ -0,0 +1,313 @@ +#![cfg(all( + feature = "cuda-driver", + target_os = "linux", + target_pointer_width = "64" +))] +#[path = "q5_oracle.rs"] +mod oracle; +use leleka_cuda_driver::{Context, DeviceBuffer, Driver, Error, Q5KProjectionShape}; +use std::{ + fs, + path::{Path, PathBuf}, + process::Command, +}; +fn upload<'a>(ctx: &'a Context<'_>, data: &[u8]) -> DeviceBuffer<'a> { + let mut b = ctx.allocate(data.len()).unwrap(); + b.copy_from(data).unwrap(); + b +} +fn read(b: &DeviceBuffer<'_>) -> Vec { + let mut x = vec![0; b.len()]; + b.copy_to(&mut x).unwrap(); + x +} +fn compare_output(actual: &[u8], expected: &[u8]) { + assert_eq!(actual.len(), expected.len()); + for (i, (a, b)) in actual + .as_chunks::<4>() + .0 + .iter() + .zip(expected.as_chunks::<4>().0.iter()) + .enumerate() + { + let a = u32::from_le_bytes(*a); + let b = u32::from_le_bytes(*b); + if f32::from_bits(b).is_nan() { + assert!(f32::from_bits(a).is_nan(), "row {i} NaN classification"); + } else { + assert_eq!(a, b, "row {i} native exactbits"); + } + } +} +fn run_case( + ctx: &Context<'_>, + root: &Path, + native: &Path, + ptx: &Path, + rows: usize, + columns: usize, + pattern: usize, +) { + let w = oracle::weights(rows, columns, pattern); + let x = oracle::activation(columns, pattern); + let xb = oracle::f32bytes(&x); + let stem = format!("m{rows}-n{columns}-p{pattern}"); + let input = root.join(format!("{stem}.input")); + let result = root.join(format!("{stem}.native")); + let mut data = Vec::new(); + for h in [0x35314b51, rows as u32, columns as u32] { + data.extend_from_slice(&h.to_le_bytes()); + } + data.extend_from_slice(&w); + data.extend_from_slice(&xb); + fs::write(&input, &data).unwrap(); + let output = Command::new("timeout") + .args(["--signal=TERM", "--kill-after=5", "30"]) + .arg(native) + .arg(&input) + .arg(&result) + .arg(ptx) + .output() + .unwrap(); + fs::write(root.join(format!("{stem}.stdout")), &output.stdout).unwrap(); + fs::write(root.join(format!("{stem}.stderr")), &output.stderr).unwrap(); + assert!( + output.status.success(), + "native {stem}: {}", + String::from_utf8_lossy(&output.stderr) + ); + let expected = fs::read(&result).unwrap(); + let qb = columns / 32 * 36; + assert_eq!(expected.len(), 16 + 2 * qb + rows * 4); + assert_eq!( + u32::from_le_bytes(expected[0..4].try_into().unwrap()), + 0x35314f51 + ); + assert_eq!(&expected[4..8], &(rows as u32).to_le_bytes()); + assert_eq!(&expected[8..12], &(columns as u32).to_le_bytes()); + assert_eq!( + &expected[12..16], + &0u32.to_le_bytes(), + "native-math activation admission" + ); + let nativeq = &expected[16..16 + qb]; + let candidateq = &expected[16 + qb..16 + 2 * qb]; + assert_eq!( + candidateq, nativeq, + "all independently quantized Q8 bytes {stem}" + ); + if pattern == 1 { + for block in nativeq.as_chunks::<36>().0.iter() { + assert_eq!(&block[..4], &[0, 0x3c, 0, 0]); + assert_eq!(&block[4..8], &[127, 129, 1, 255]); + assert!(block[8..].iter().all(|&v| v == 0)); + } + } + if pattern == 2 || pattern == 3 { + for block in nativeq.as_chunks::<36>().0.iter() { + assert_eq!(&block[..2], &[0, 0]); + assert_eq!(&block[2..4], if pattern == 3 { &[0, 128] } else { &[0, 0] }); + assert!(block[4..].iter().all(|&v| v == 0)); + } + } + if pattern == 7 { + for word in expected[16 + 2 * qb..].as_chunks::<4>().0.iter() { + assert_eq!( + f32::from_le_bytes(*word), + 959104.0 * (columns / 256) as f32, + "independent hand projection" + ); + } + } + let dw = upload(ctx, &w); + let dx = upload(ctx, &xb); + let sentinel = vec![0xa5; rows * 4]; + let mut dy = upload(ctx, &sentinel); + let charged = ctx.allocated_bytes(); + ctx.q5_k_q8_1(&dw, &dx, &mut dy, Q5KProjectionShape { rows, columns }) + .expect("safe resident public projection"); + let actual = read(&dy); + compare_output(&actual, &expected[16 + 2 * qb..]); + assert_eq!(read(&dw), w, "compressed weights immutable"); + assert_eq!(read(&dx), xb, "resident activation immutable"); + assert_eq!(ctx.allocated_bytes(), charged, "healthy temporary cleanup"); + // Repeat proves the public cache path and stable ownership, against the SAME + // frozen native result, not a first candidate run used as a baseline. + ctx.q5_k_q8_1(&dw, &dx, &mut dy, Q5KProjectionShape { rows, columns }) + .unwrap(); + compare_output(&read(&dy), &expected[16 + 2 * qb..]); + fs::write(root.join(format!("{stem}.actual")), &actual).unwrap(); + dw.release().unwrap(); + dx.release().unwrap(); + dy.release().unwrap(); + assert_eq!(ctx.allocated_bytes(), 0); + println!( + "Q5_NATIVE PASS {stem} q8_bytes={qb} outputs={rows} exactbits=true repeat=true cleanup=0" + ); +} +#[test] +#[ignore = "requires installed NVIDIA GPU, pinned independent native fixture and explicit private evidence root"] +fn installed_driver_q5_native_parity() { + assert_eq!(std::env::var("LELEKA_CUDA_REAL_GPU").as_deref(), Ok("1")); + let root = PathBuf::from(std::env::var("LELEKA_Q5_EVIDENCE").unwrap()); + let native = PathBuf::from(std::env::var("LELEKA_Q5_NATIVE_ORACLE").unwrap()); + let ptx = PathBuf::from(std::env::var("LELEKA_Q5_TRUSTED_PTX").unwrap()); + assert!(root.is_absolute() && native.is_absolute() && ptx.is_absolute()); + fs::create_dir_all(&root).unwrap(); + let driver = Driver::load().unwrap(); + let device = driver.device(0).unwrap(); + let ctx = device.create_context(1 << 20).unwrap(); + for rows in [1, 3, 4, 5] { + for columns in [256, 512, 2048, 2560, 6144] { + run_case(&ctx, &root, &native, &ptx, rows, columns, 0); + } + } + for pattern in 1..=6 { + run_case(&ctx, &root, &native, &ptx, 5, 6144, pattern); + } + for pattern in 7..=9 { + run_case(&ctx, &root, &native, &ptx, 3, 256, pattern); + } + ctx.close().unwrap(); + println!( + "Q5_PARITY_SUMMARY cases=29 public_launches=58 q8_exact=true outputs_exact=true context_close=ok" + ); +} +#[test] +#[ignore = "requires installed NVIDIA driver, explicit opt-in"] +fn installed_driver_q5_numeric_refusals() { + assert_eq!(std::env::var("LELEKA_CUDA_REAL_GPU").as_deref(), Ok("1")); + let driver = Driver::load().unwrap(); + let device = driver.device(0).unwrap(); + let ctx = device.create_context(1 << 20).unwrap(); + let w = oracle::weights(1, 256, 0); + let dw = upload(&ctx, &w); + let sentinel = vec![0xa5; 4]; + let mut dy = upload(&ctx, &sentinel); + let cases = [ + ("nan", f32::NAN), + ("positive_inf", f32::INFINITY), + ("negative_inf", f32::NEG_INFINITY), + ("half_sum_overflow", 4096.0), + ("scale_overflow", f32::MAX), + ("flushed_nonzero_divisor", f32::MIN_POSITIVE), + ]; + for (name, value) in cases { + let mut x = vec![0.0; 256]; + if name == "half_sum_overflow" { + x[..32].fill(value); + } else { + x[0] = value; + } + let xb = oracle::f32bytes(&x); + let dx = upload(&ctx, &xb); + let charged = ctx.allocated_bytes(); + assert_eq!( + ctx.q5_k_q8_1( + &dw, + &dx, + &mut dy, + Q5KProjectionShape { + rows: 1, + columns: 256 + } + ), + Err(Error::InvalidQ8Activation), + "{name}" + ); + assert_eq!(read(&dy), sentinel, "{name} output untouched"); + assert_eq!(read(&dx), xb); + assert_eq!(ctx.allocated_bytes(), charged); + ctx.synchronize().unwrap(); + dx.release().unwrap(); + println!("Q5_REFUSAL PASS {name} output_unchanged=true healthy=true"); + } + dw.release().unwrap(); + dy.release().unwrap(); + assert_eq!(ctx.allocated_bytes(), 0); + ctx.close().unwrap(); +} + +#[test] +#[ignore = "one explicitly selected private Qwen Q5_K tensor; synthetic activation; no model-forward claim"] +fn installed_driver_q5_real_tensor_component() { + assert_eq!(std::env::var("LELEKA_CUDA_REAL_GPU").as_deref(), Ok("1")); + let root = PathBuf::from(std::env::var("LELEKA_Q5_EVIDENCE").unwrap()); + let input = PathBuf::from(std::env::var("LELEKA_Q5_REAL_INPUT").unwrap()); + let native = PathBuf::from(std::env::var("LELEKA_Q5_NATIVE_ORACLE").unwrap()); + let ptx = PathBuf::from(std::env::var("LELEKA_Q5_TRUSTED_PTX").unwrap()); + assert!(root.is_absolute() && input.is_absolute() && native.is_absolute() && ptx.is_absolute()); + fs::create_dir_all(&root).unwrap(); + let data = fs::read(&input).unwrap(); + let (m, n) = (640usize, 2560usize); + let wb = m * (n / 256) * 176; + let xb = n * 4; + let qb = n / 32 * 36; + assert_eq!(data.len(), 12 + wb + xb); + assert_eq!(&data[..4], &0x35314b51u32.to_le_bytes()); + assert_eq!(&data[4..8], &(m as u32).to_le_bytes()); + assert_eq!(&data[8..12], &(n as u32).to_le_bytes()); + // Synthetic activation must equal our independent deterministic input, + // rather than a captured request or an arbitrary external fixture. + assert_eq!( + &data[12 + wb..], + oracle::f32bytes(&oracle::activation(n, 0)) + ); + let result = root.join("real-qwen-q5.native"); + let out = Command::new("timeout") + .args(["--signal=TERM", "--kill-after=5", "30"]) + .arg(native) + .arg(&input) + .arg(&result) + .arg(ptx) + .output() + .unwrap(); + fs::write(root.join("real-qwen-q5.stdout"), &out.stdout).unwrap(); + fs::write(root.join("real-qwen-q5.stderr"), &out.stderr).unwrap(); + assert!( + out.status.success(), + "{}", + String::from_utf8_lossy(&out.stderr) + ); + let expected = fs::read(result).unwrap(); + assert_eq!(expected.len(), 16 + 2 * qb + m * 4); + assert_eq!(&expected[..4], &0x35314f51u32.to_le_bytes()); + assert_eq!(&expected[12..16], &0u32.to_le_bytes()); + assert_eq!( + &expected[16..16 + qb], + &expected[16 + qb..16 + 2 * qb], + "separate fresh Q8 bytes" + ); + let driver = Driver::load().unwrap(); + let device = driver.device(0).unwrap(); + let ctx = device.create_context(8 << 20).unwrap(); + let w = upload(&ctx, &data[12..12 + wb]); + let x = upload(&ctx, &data[12 + wb..]); + let mut y = upload(&ctx, &vec![0xa5; m * 4]); + let resident = ctx.allocated_bytes(); + for _ in 0..2 { + ctx.q5_k_q8_1( + &w, + &x, + &mut y, + Q5KProjectionShape { + rows: m, + columns: n, + }, + ) + .unwrap(); + compare_output(&read(&y), &expected[16 + 2 * qb..]); + assert_eq!(ctx.allocated_bytes(), resident); + } + assert_eq!(read(&w), data[12..12 + wb]); + assert_eq!(read(&x), data[12 + wb..]); + fs::write(root.join("real-qwen-q5.actual"), read(&y)).unwrap(); + w.release().unwrap(); + x.release().unwrap(); + y.release().unwrap(); + assert_eq!(ctx.allocated_bytes(), 0); + ctx.close().unwrap(); + println!( + "Q5_REAL_COMPONENT PASS tensor=blk.4.ffn_up_shexp.weight rows=640 columns=2560 packed_bytes=1126400 q8_bytes=2880 outputs=640 exactbits=true repeat=true synthetic_activation=true cleanup=0 scope=single_projection" + ); +} diff --git a/crates/cuda-driver/tests/q5_mock.rs b/crates/cuda-driver/tests/q5_mock.rs new file mode 100644 index 00000000..8d1d3560 --- /dev/null +++ b/crates/cuda-driver/tests/q5_mock.rs @@ -0,0 +1,355 @@ +#![cfg(all( + feature = "cuda-driver", + target_os = "linux", + target_pointer_width = "64" +))] +use leleka_cuda_driver::{Driver, Error, Q5KProjectionShape}; +use std::{fs, path::PathBuf, process::Command}; +#[derive(Debug)] +struct Row { + stage: String, + code: i32, + current: usize, + depth: usize, + charged: usize, + bytes: usize, +} +fn trace() -> Vec { + fs::read_to_string(std::env::var("MOCK_CUDA_TRACE").unwrap()) + .unwrap_or_default() + .lines() + .map(|l| { + let v: Vec<_> = l.split_whitespace().collect(); + assert_eq!(v.len(), 7); + Row { + stage: v[0].into(), + code: v[1].parse().unwrap(), + current: v[2].parse().unwrap(), + depth: v[3].parse().unwrap(), + charged: v[5].parse().unwrap(), + bytes: v[6].parse().unwrap(), + } + }) + .collect() +} +fn count(rows: &[Row], name: &str) -> usize { + rows.iter().filter(|r| r.stage == name).count() +} +fn driver_codes(e: &Error) -> Vec { + match e { + Error::Driver { code, .. } => vec![*code], + Error::Multiple { primary, secondary } => { + let mut c = driver_codes(primary); + c.extend(driver_codes(secondary)); + c + } + _ => vec![], + } +} +const CASES: &[&str] = &[ + "success", + "small_tail", + "large_boundary", + "bounds", + "sizes", + "context", + "budget", + "nan", + "inf", + "sum_overflow", + "load", + "second_lookup", + "null_module", + "null_function", + "quant_launch", + "projection_launch", + "quant_sync", + "projection_sync", + "quant_restore", + "projection_restore", + "free", + "unload", +]; +#[test] +#[ignore = "isolated CPU ELF protocol fixture only, no GPU arithmetic claim"] +fn isolated_q5_protocol_matrix() { + assert_eq!( + std::env::var("LELEKA_CUDA_MOCK_HARNESS").as_deref(), + Ok("1") + ); + let root = PathBuf::from(std::env::var("LELEKA_CUDA_Q5_MOCK_ROOT").unwrap()); + assert!(root.is_absolute()); + for case in CASES { + let mut c = Command::new(std::env::current_exe().unwrap()); + c.args(["--exact", "q5_fixture_child", "--ignored", "--nocapture"]) + .env("LELEKA_Q5_CASE", case) + .env("MOCK_CUDA_TRACE", root.join(format!("trace-q5-{case}"))) + .env("LD_LIBRARY_PATH", root.join("q5")) + .env("MOCK_CUDA_VECTOR", "1") + .env_remove("MOCK_CUDA_FAIL") + .env_remove("MOCK_CUDA_Q5_FAIL") + .env_remove("MOCK_CUDA_NULL_LOAD") + .env_remove("MOCK_CUDA_NULL_FUNCTION"); + match *case { + "load" | "free" | "unload" => { + c.env("MOCK_CUDA_FAIL", case); + } + "second_lookup" => { + c.env("MOCK_CUDA_FAIL", "lookup:2"); + } + "null_module" => { + c.env("MOCK_CUDA_NULL_LOAD", "1"); + } + "null_function" => { + c.env("MOCK_CUDA_NULL_FUNCTION", "1"); + } + v if v.starts_with("quant_") || v.starts_with("projection_") => { + c.env("MOCK_CUDA_Q5_FAIL", v); + } + _ => {} + } + let out = c.output().unwrap(); + fs::write(root.join(format!("q5-{case}.stdout")), &out.stdout).unwrap(); + fs::write(root.join(format!("q5-{case}.stderr")), &out.stderr).unwrap(); + assert!( + out.status.success(), + "{case}\n{}\n{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + println!("Q5_MOCK PASS {case}"); + } +} +#[test] +#[ignore = "only invoked by isolated_q5_protocol_matrix"] +fn q5_fixture_child() { + assert_eq!( + std::env::var("LELEKA_CUDA_MOCK_HARNESS").as_deref(), + Ok("1") + ); + let case = std::env::var("LELEKA_Q5_CASE").unwrap(); + assert!(CASES.contains(&case.as_str())); + let (m, n) = match case.as_str() { + "small_tail" => (5, 512), + "large_boundary" => (3, 2048), + _ => (1, 256), + }; + let wb = m * (n / 256) * 176; + let xb = n * 4; + let ob = m * 4; + let resident = wb + xb + ob; + let driver = Driver::load().unwrap(); + let device = driver.device(0).unwrap(); + let ctx = device + .create_context(if case == "budget" { resident } else { 16384 }) + .unwrap(); + let mut w = ctx.allocate(wb).unwrap(); + w.copy_from(&vec![0; wb]).unwrap(); + let mut x = ctx.allocate(xb).unwrap(); + let mut input = vec![0; xb]; + let v = match case.as_str() { + "nan" => f32::NAN, + "inf" => f32::INFINITY, + "sum_overflow" => 4096.0, + _ => 1.0, + }; + for word in input.as_chunks_mut::<4>().0.iter_mut() { + word.copy_from_slice(&v.to_le_bytes()); + } + x.copy_from(&input).unwrap(); + let mut y = ctx.allocate(ob).unwrap(); + let sentinel = vec![0xa5; ob]; + y.copy_from(&sentinel).unwrap(); + let before = trace().len(); + let shape = Q5KProjectionShape { + rows: m, + columns: n, + }; + if case == "bounds" { + for shape in [ + Q5KProjectionShape { + rows: 0, + columns: 256, + }, + Q5KProjectionShape { + rows: 1, + columns: 0, + }, + Q5KProjectionShape { + rows: 1, + columns: 255, + }, + Q5KProjectionShape { + rows: 1, + columns: 257, + }, + Q5KProjectionShape { + rows: usize::MAX, + columns: 256, + }, + Q5KProjectionShape { + rows: 1, + columns: usize::MAX, + }, + Q5KProjectionShape { + rows: i32::MAX as usize + 1, + columns: 256, + }, + ] { + assert_eq!( + ctx.q5_k_q8_1(&w, &x, &mut y, shape), + Err(Error::InvalidQ5KProjectionShape) + ); + } + assert_eq!( + trace().len(), + before, + "structural admission before native mutation" + ); + } else if case == "sizes" { + for which in 0..3 { + let mut short = ctx + .allocate(if which == 0 { + wb - 1 + } else if which == 1 { + xb - 1 + } else { + ob - 1 + }) + .unwrap(); + let mark = trace().len(); + let r = match which { + 0 => ctx.q5_k_q8_1(&short, &x, &mut y, shape), + 1 => ctx.q5_k_q8_1(&w, &short, &mut y, shape), + _ => ctx.q5_k_q8_1(&w, &x, &mut short, shape), + }; + assert_eq!(r, Err(Error::InvalidQ5KProjectionShape)); + assert_eq!(trace().len(), mark); + short.release().unwrap(); + } + } else if case == "context" { + let other = device.create_context(2048).unwrap(); + let otherx = other.allocate(xb).unwrap(); + let mark = trace().len(); + assert_eq!( + ctx.q5_k_q8_1(&w, &otherx, &mut y, shape), + Err(Error::ContextMismatch) + ); + assert_eq!(trace().len(), mark); + otherx.release().unwrap(); + other.close().unwrap(); + } else { + let result = ctx.q5_k_q8_1(&w, &x, &mut y, shape); + match case.as_str() { + "success" | "small_tail" | "large_boundary" => { + result.unwrap(); + assert_eq!(ctx.allocated_bytes(), resident); + ctx.q5_k_q8_1(&w, &x, &mut y, shape).unwrap(); + let rows = trace(); + assert_eq!(count(&rows, "module_q5"), 1); + assert_eq!(count(&rows, "lookup_quant"), 1); + assert_eq!(count(&rows, "lookup_projection"), 1); + assert_eq!(count(&rows, "q5_quant_complete"), 2); + assert_eq!(count(&rows, "q5_projection_complete"), 2); + assert!( + rows.iter() + .filter(|r| r.stage == "q5_projection_launch") + .all(|r| r.bytes == if n < 2048 { m.div_ceil(4) } else { m }) + ); + } + "budget" => { + assert_eq!(result, Err(Error::BudgetExceeded)); + assert_eq!(trace().len(), before); + } + "nan" | "inf" | "sum_overflow" => { + assert_eq!(result, Err(Error::InvalidQ8Activation)); + assert_eq!(ctx.allocated_bytes(), resident); + assert_eq!(count(&trace(), "q5_projection_launch"), 0); + ctx.synchronize().unwrap(); + } + "unload" => { + result.unwrap(); + } + "null_module" | "null_function" => { + assert!(matches!(result, Err(Error::InvalidDriverValue(_)))); + assert_eq!(count(&trace(), "q5_projection_launch"), 0); + } + "quant_sync" | "projection_sync" | "quant_restore" | "projection_restore" => { + assert!(driver_codes(&result.unwrap_err()).contains(&700)); + assert!( + ctx.allocated_bytes() > resident, + "uncertain scratch stays charged" + ); + let mark = trace().len(); + assert_eq!(ctx.synchronize(), Err(Error::ContextPoisoned)); + assert_eq!(trace().len(), mark); + } + _ => { + assert!(driver_codes(&result.unwrap_err()).contains(&700)); + if case != "free" && case != "unload" { + assert_eq!(ctx.allocated_bytes(), resident); + } + } + } + } + let poisoned = [ + "quant_sync", + "projection_sync", + "quant_restore", + "projection_restore", + ] + .contains(&case.as_str()); + if !poisoned { + if [ + "bounds", + "sizes", + "context", + "budget", + "nan", + "inf", + "sum_overflow", + "load", + "second_lookup", + "null_module", + "null_function", + "quant_launch", + ] + .contains(&case.as_str()) + { + let mut data = vec![0; ob]; + y.copy_to(&mut data).unwrap(); + assert_eq!(data, sentinel, "refusal did not publish output"); + } + let mut data = vec![0; xb]; + x.copy_to(&mut data).unwrap(); + assert_eq!(data, input); + w.release().unwrap(); + x.release().unwrap(); + y.release().unwrap(); + } else { + drop(w); + drop(x); + drop(y); + } + let close = ctx.close(); + if case == "unload" { + assert!(driver_codes(&close.unwrap_err()).contains(&700)); + } else { + close.unwrap(); + } + let rows = trace(); + assert_eq!( + count(&rows, "destroy"), + if case == "context" { 2 } else { 1 } + ); + assert_eq!(count(&rows, "unsafe_q5_pending_free"), 0); + assert_eq!(count(&rows, "unsafe_pending_unload"), 0); + if !poisoned { + assert_eq!(rows.last().unwrap().charged, 0); + for r in rows.iter().filter(|r| r.stage == "pop" && r.code == 0) { + assert_eq!(r.current, 0x11); + assert_eq!(r.depth, 0); + } + } + println!("Q5_CHILD {case} owned_cleanup=consumed numeric_proof=mock_only"); +} diff --git a/crates/cuda-driver/tests/q5_mock_cuda.c b/crates/cuda-driver/tests/q5_mock_cuda.c new file mode 100644 index 00000000..7daa5683 --- /dev/null +++ b/crates/cuda-driver/tests/q5_mock_cuda.c @@ -0,0 +1,84 @@ +/* Test-only protocol oracle: reuse existing owned-byte fixture unchanged. + This mock proves routing/lifetime/refusals, NOT GPU arithmetic parity. */ +#define cuModuleLoadData old_load +#define cuModuleGetFunction old_get +#define cuModuleUnload old_unload +#define cuLaunchKernel old_launch +#define cuCtxSynchronize old_sync +#define cuCtxDestroy_v2 old_destroy +#define cuCtxPopCurrent_v2 old_pop +#define cuMemFree_v2 old_free +#include "mock_cuda.c" +#undef cuModuleLoadData +#undef cuModuleGetFunction +#undef cuModuleUnload +#undef cuLaunchKernel +#undef cuCtxSynchronize +#undef cuCtxDestroy_v2 +#undef cuCtxPopCurrent_v2 +#undef cuMemFree_v2 +#include +static void **qargs; +static uint64_t qp[3]; +static uint32_t qrows,qcols; +static int qkind; +static int restore_fault; +static int qfail(const char*name){const char*v=getenv("MOCK_CUDA_Q5_FAIL");return v&&!strcmp(v,name);} +static int exact_q5_image(const char *image){ + const char*path=getenv("MOCK_CUDA_Q5_PTX");if(!path)return 0;FILE*f=fopen(path,"rb");if(!f)return 0;fseek(f,0,SEEK_END);long n=ftell(f);rewind(f);if(n<=0||n>262144){fclose(f);return 0;}char*p=malloc((size_t)n+1);if(!p)abort();if(fread(p,1,n,f)!=(size_t)n)abort();p[n]=0;int ok=strcmp(p,image)==0;free(p);fclose(f);return ok; +} +static void qcomplete(void){ + if(!qargs)return; + for(size_t i=0;i<3;i++)if(*(uint64_t*)qargs[i]!=qp[i])abort(); + if(*(uint32_t*)qargs[3]!=(qkind==1?qcols:qrows))abort(); + if(qkind==2&&*(uint32_t*)qargs[4]!=qcols)abort(); + if(qkind==1){ + float*x=(float*)(uintptr_t)qp[0];unsigned char*q=(unsigned char*)(uintptr_t)qp[1];uint32_t*s=(uint32_t*)(uintptr_t)qp[2]; + for(uint32_t b=0;b=65520||!isfinite(sum)||fabsf(sum)>=65520)*s|=2; + /* Delayed write proves arguments/scratch survive completion. Math is NOT + the native half/reduction oracle; successful mock scratch is synthetic. */ + memset(q+b*36,0,36); + } trace("q5_quant_complete",0,qcols); + }else{float*y=(float*)(uintptr_t)qp[2];for(uint32_t r=0;r=16)return 2;size_t i=next_module++;modules[i].handle=(void*)(uintptr_t)(0x100+i);modules[i].function=(void*)(uintptr_t)(0x200+i);modules[i].owner=current;modules[i].matvec=2;*out=modules[i].handle;trace("load",0,0);trace("module_q5",0,i);return 0; +} +CUresult cuModuleGetFunction(void**out,void*mod,const char*name){ + for(size_t i=0;i<16;i++)if(modules[i].handle==mod&&modules[i].matvec==2){ + if(fault(13))return 700;if(require_context()||modules[i].owner!=current)return 201; + if(getenv("MOCK_CUDA_NULL_FUNCTION")){*out=NULL;trace("lookup_null",0,0);return 0;} + int quant=!strcmp(name,"leleka_quantize_q8_1"),proj=!strcmp(name,"leleka_q5_k_q8_1");if(!quant&&!proj)return 201; + *out=(void*)(uintptr_t)((quant?0x200:0x300)+i);trace("lookup",0,0);trace(quant?"lookup_quant":"lookup_projection",0,0);return 0; + }return old_get(out,mod,name); +} +CUresult cuModuleUnload(void*mod){if(qargs){trace("unsafe_pending_unload",1,0);abort();}return old_unload(mod);} +static int heap_arg(void*p,uintptr_t stackaddr){uintptr_t a=(uintptr_t)p;return a&&(astackaddr+1048576);} +CUresult cuLaunchKernel(void*f,unsigned gx,unsigned gy,unsigned gz,unsigned bx,unsigned by,unsigned bz,unsigned shared,void*stream,void**args,void**extra){ + int kind=0;for(size_t i=0;i<16;i++)if(modules[i].matvec==2&&modules[i].owner==current){if(f==(void*)(uintptr_t)(0x200+i))kind=1;if(f==(void*)(uintptr_t)(0x300+i))kind=2;} + if(!kind)return old_launch(f,gx,gy,gz,bx,by,bz,shared,stream,args,extra); + if(require_context()||!args||extra||stream||shared||gy!=1||gz!=1||bz!=1)return 201;uintptr_t sp=(uintptr_t)&kind; + if(!heap_arg(args,sp))abort();for(size_t i=0;i<(kind==1?4u:5u);i++)if(!heap_arg(args[i],sp))abort(); + uint32_t rows=kind==1?0:*(uint32_t*)args[3],cols=*(uint32_t*)args[kind==1?3:4];if(!cols||cols%256)return 1; + if(kind==1){if(bx!=256||by!=1||gx!=(cols+255ull)/256)return 1;} + else{if(!rows||bx!=32||by!=4||gx!=(cols<2048?(rows+3ull)/4:rows))return 1;} + for(size_t i=0;i<3;i++)qp[i]=*(uint64_t*)args[i]; + size_t qbytes=(size_t)cols/32*36; + if(kind==1){if(!lookup(qp[0],(size_t)cols*4)||!lookup(qp[1],qbytes)||!lookup(qp[2],4))return 1;} + else{if(!lookup(qp[0],(size_t)rows*(cols/256)*176)||!lookup(qp[1],qbytes)||!lookup(qp[2],(size_t)rows*4))return 1;} + if(qp[0]==qp[1]||qp[0]==qp[2]||qp[1]==qp[2])return 1; + qkind=kind;qargs=args;qrows=rows;qcols=cols;trace(kind==1?"q5_quant_launch":"q5_projection_launch",0,gx);trace("q5_columns",0,cols);trace("q5_rows",0,rows); + if(fault(15)||qfail(kind==1?"quant_launch":"projection_launch")){trace("launch",700,kind);return 700;}trace("launch",0,kind);return 0; +} diff --git a/crates/cuda-driver/tests/q5_oracle.rs b/crates/cuda-driver/tests/q5_oracle.rs new file mode 100644 index 00000000..0296d179 --- /dev/null +++ b/crates/cuda-driver/tests/q5_oracle.rs @@ -0,0 +1,185 @@ +// Independent GGUF Q5_K layout/hand oracle. No production decoder/PTX imports. +// General native fast-math numerical parity is measured on the GPU separately. +#![allow(dead_code)] +pub fn half(bits: u16) -> f32 { + let sign = ((bits & 0x8000) as u32) << 16; + let e = (bits >> 10) & 31; + let f = bits & 1023; + if e == 0 { + let x = f as f32 * (1.0 / 16_777_216.0); + if sign == 0 { x } else { -x } + } else { + f32::from_bits( + sign | if e == 31 { + 0x7f800000 | ((f as u32) << 13) + } else { + ((e as u32 + 112) << 23) | ((f as u32) << 13) + }, + ) + } +} +pub fn scale_min(scales: &[u8], group: usize) -> (u8, u8) { + assert!(scales.len() == 12 && group < 8); + if group < 4 { + (scales[group] & 63, scales[group + 4] & 63) + } else { + ( + (scales[group + 4] & 15) | ((scales[group - 4] >> 6) << 4), + (scales[group + 4] >> 4) | ((scales[group] >> 6) << 4), + ) + } +} +pub fn pack_scale_min(scales: &mut [u8], group: usize, scale: u8, min: u8) { + assert!(scale < 64 && min < 64 && group < 8); + if group < 4 { + scales[group] |= scale; + scales[group + 4] |= min; + } else { + scales[group + 4] |= (scale & 15) | ((min & 15) << 4); + scales[group - 4] |= (scale >> 4) << 6; + scales[group] |= (min >> 4) << 6; + } +} +pub fn code(block: &[u8], i: usize) -> u8 { + assert!(block.len() == 176 && i < 256); + let group = i / 32; + let lane = i % 32; + let low = (block[48 + (group / 2) * 32 + lane] >> (4 * (group % 2))) & 15; + low | (((block[16 + lane] >> group) & 1) << 4) +} +pub fn scalar(block: &[u8], i: usize) -> f32 { + let (s, m) = scale_min(&block[4..16], i / 32); + half(u16::from_le_bytes(block[0..2].try_into().unwrap())) * s as f32 * code(block, i) as f32 + - half(u16::from_le_bytes(block[2..4].try_into().unwrap())) * m as f32 +} +pub fn weights(rows: usize, columns: usize, pattern: usize) -> Vec { + let mut out = Vec::new(); + for row in 0..rows { + for k in 0..columns / 256 { + let mut b = [0u8; 176]; + // Exactly representable halves; row/block vary independently. + b[0..2].copy_from_slice(&0x3000u16.to_le_bytes()); // 1/8 + b[2..4].copy_from_slice(&0x2800u16.to_le_bytes()); // 1/32 + for g in 0..8 { + pack_scale_min( + &mut b[4..16], + g, + ((g * 9 + row * 7 + k * 3 + 1) % 64) as u8, + ((g * 13 + row * 3 + k * 5) % 64) as u8, + ); + } + for i in 0..256 { + let g = i / 32; + let lane = i % 32; + let q = ((i * 7 + row * 11 + k * 17 + pattern * 5) % 32) as u8; + b[48 + (g / 2) * 32 + lane] |= (q & 15) << (4 * (g % 2)); + b[16 + lane] |= (q >> 4) << g; + } + if pattern == 7 { + b.fill(0); + b[0..2].copy_from_slice(&0x3c00u16.to_le_bytes()); + b[2..4].copy_from_slice(&0x3800u16.to_le_bytes()); + for g in 0..8 { + pack_scale_min(&mut b[4..16], g, 2, 3); + for lane in 0..32 { + let q = (g * 4 + lane % 4) as u8; + b[48 + (g / 2) * 32 + lane] |= (q & 15) << (4 * (g % 2)); + b[16 + lane] |= (q >> 4) << g; + } + } + } else if pattern == 8 { + b[0..2].copy_from_slice(&0x7c00u16.to_le_bytes()); + } else if pattern == 9 { + b[0..2].copy_from_slice(&0x7e00u16.to_le_bytes()); + } + out.extend_from_slice(&b); + } + } + out +} +pub fn activation(n: usize, pattern: usize) -> Vec { + (0..n) + .map(|i| match pattern { + 0 => ((i * 37 % 251) as i32 - 125) as f32 * 0.03125, + 1 => match i % 32 { + 0 => 127.0, + 1 => -127.0, + 2 => 0.5, + 3 => -0.5, + _ => 0.0, + }, + 2 => 0.0, + 3 => -0.0, + 4 => { + if i % 2 == 0 { + 1000.0 + } else { + -1000.0 + } + } + 5 => { + if i % 2 == 0 { + 1.0 / 1_048_576.0 + } else { + -1.0 / 1_048_576.0 + } + } + 6 => match i % 32 { + 0 => 65504.0, + 1 => -65504.0, + _ => 0.0, + }, + 7 => 127.0, + 8 | 9 => ((i * 37 % 251) as i32 - 125) as f32 * 0.03125, + _ => unreachable!(), + }) + .collect() +} +pub fn f32bytes(x: &[f32]) -> Vec { + x.iter().flat_map(|v| v.to_le_bytes()).collect() +} +#[test] +fn packed_layout_independent_hand_oracle() { + assert_eq!(half(0x3c00), 1.0); + assert_eq!(half(0x3000), 0.125); + assert_eq!(half(0x0001).to_bits(), 0x33800000); + assert_eq!(half(0x8000).to_bits(), 0x80000000); + let mut scales = [0u8; 12]; + let pairs = [ + (0, 63), + (1, 32), + (15, 16), + (16, 15), + (31, 1), + (32, 0), + (48, 48), + (63, 63), + ]; + for (g, (s, m)) in pairs.into_iter().enumerate() { + pack_scale_min(&mut scales, g, s, m); + } + // Recorded packed words derived from the GGUF six-bit layout, not decoder output. + assert_eq!(scales, [64, 129, 207, 208, 63, 32, 208, 207, 31, 0, 0, 255]); + for (g, pair) in pairs.into_iter().enumerate() { + assert_eq!(scale_min(&scales, g), pair); + } + let mut b = [0u8; 176]; + b[0..2].copy_from_slice(&0x3c00u16.to_le_bytes()); + b[2..4].copy_from_slice(&0x3800u16.to_le_bytes()); + for g in 0..8 { + pack_scale_min(&mut b[4..16], g, 2, 3); + for lane in 0..32 { + let q = (g * 4 + lane % 4) as u8; + b[48 + (g / 2) * 32 + lane] |= (q & 15) << (4 * (g % 2)); + b[16 + lane] |= (q >> 4) << g; + } + } + for i in 0..256 { + let expected = (i / 32 * 4 + i % 4) as u8; + assert_eq!(code(&b, i), expected); + assert_eq!(scalar(&b, i), 2.0 * expected as f32 - 1.5); + } + // A constant d=1, scale=2, min=3, dmin=.5 block dot ones is exactly 7552. + let hand: f32 = (0..256).map(|i| scalar(&b, i)).sum(); + assert_eq!(hand, 7552.0); +} diff --git a/scripts/build-cuda-q5-oracle.sh b/scripts/build-cuda-q5-oracle.sh new file mode 100755 index 00000000..e3a4afa5 --- /dev/null +++ b/scripts/build-cuda-q5-oracle.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Test-only bounded native reference; no serving/model build or GPU execution. +# TOOLKIT must be the verified deploy/cuda-toolkit.lock.json 13.0.88 distribution. +set -euo pipefail +[[ $# == 2 ]] || { echo 'Usage: build-cuda-q5-oracle.sh TOOLKIT PRIVATE_OUTPUT' >&2; exit 2; } +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +toolkit="$(cd "$1" && pwd)" +output="$2" +[[ ! -e "$output" && -f "$toolkit/TOOLKIT-PROVENANCE.json" ]] +"$toolkit/bin/nvcc" --version | grep 'V13.0.88' +python3 - "$toolkit/TOOLKIT-PROVENANCE.json" "$root/deploy/cuda-toolkit.lock.json" <<'PY' +import json,sys +assert json.load(open(sys.argv[1]))['lock']==json.load(open(sys.argv[2])) +PY +# Exact native source identity is an oracle gate, not caller-selected reference. +echo 'f2fc636e5e8f6db9bbb8146fbf2b78a717c0e0076bf8c72ac2b784df7b3f455f engine/strata/src/kernels/cuda/native_mmvq.cu' | (cd "$root" && sha256sum --check) +sha256sum "$root/engine/strata/src/kernels/cuda/native_mmvq.cu" "$root/scripts/cuda-q5-oracle/native_fixture.cu" +"$toolkit/bin/nvcc" -std=c++17 -O3 -arch=sm_120 --use_fast_math \ + -Xcompiler=-ffunction-sections,-fdata-sections -Xlinker=--gc-sections \ + -I"$root/engine/strata/include" "$root/engine/strata/src/kernels/cuda/native_mmvq.cu" \ + "$root/scripts/cuda-q5-oracle/native_fixture.cu" -ldl -o "$output" +sha256sum "$output" diff --git a/scripts/cuda-q5-oracle/README.md b/scripts/cuda-q5-oracle/README.md new file mode 100644 index 00000000..36612cbe --- /dev/null +++ b/scripts/cuda-q5-oracle/README.md @@ -0,0 +1,77 @@ +# Independent Q5_K projection oracle + +Test-only fixtures for STRA-142. They do not run a model forward pass, measure +performance, or install a Toolkit on the GPU server. Production uses its existing +owned CUDA Driver boundary and embedded PTX. + +## Reference and independent expectations + +`native_fixture.cu` links the **unchanged** imported +`engine/strata/src/kernels/cuda/native_mmvq.cu` (SHA-256 +`f2fc636e5e8f6db9bbb8146fbf2b78a717c0e0076bf8c72ac2b784df7b3f455f`). +The native source is pinned through `c3020b71ba6cc49a81d8b374c29dd3be36fe239c` +and attributed to llama.cpp `3cf03257f219afbe7334045ff7c6a06ac68c627d`. +Its source/MIT notice is preserved; no quantizer or projection implementation is +copied into this test runner. + +Build with verified CUDA 13.0.88, `-arch=sm_120 --use_fast_math -O3`. +`--use_fast_math` is essential: FTZ, approximate division and contraction are the +native baseline. Function/data sections and linker garbage collection remove +unused format dispatch references without editing the original translation unit. +`scripts/prepare-cuda-q5-oracle-toolkit.py PRIVATE_OUTPUT` fetches only the six +hash-pinned compiler/runtime components in `deploy/cuda-toolkit.lock.json`, verifies +the manifest and archive hashes, and records the selection. BLAS is not needed. +`scripts/build-cuda-q5-oracle.sh TOOLKIT PRIVATE_ELF` refuses an existing output, +checks the native source hash, and compiles the bounded reference. Build in a +GPU-hidden isolated Linux amd64 CPU container; preserve its actual architecture, +compiler versions, exact commands, source hashes and ELF hash as evidence. + +The native runner accepts a private input file: three little-endian u32 words +(magic `0x35314b51`, M, N), exactly `M*(N/256)*176` packed bytes, then `4*N` bytes +of finite F32 activation. Bounds are `1 <= M <= 640`, `256 <= N <= 6144`, N%256=0. +It quantizes one native scratch allocation, then calls `native_q5_k_f32` with a +**second fresh native scratch** and checks those native bytes agree. Neither +native call receives candidate scratch. Separately it loads the trusted candidate +asset and launches only `leleka_quantize_q8_1` in the private test Driver boundary. +All allocations, stream and module are consumed on success; errors terminate the +isolated process. Original native quantization is never fed nonfinite inputs. + +Output has four LE u32 words (magic `0x35314f51`, M, N, candidate admission status), +then native Q8 bytes, candidate Q8 bytes, and native F32 output bits. These are +separate assertions: every Q8 byte must match; finite/zero/Inf outputs require +identical bits; NaN outputs require NaN classification. No tolerance is calibrated +from candidate results. + +`tests/q5_oracle.rs` independently constructs/decodes GGUF six-bit scales/minima, +high bits and nibbles; its packed words and exact scalar dot are recorded hand +expectations. It does not import a production decoder. The GPU hand case uses +constant activation 127: native d8=1 and original-input sums are exact, yielding +959104 per Q5 block. CPU scalar arithmetic is supplementary and does not redefine +native fast-math output. + +## Executable gates + +- `tests/q5_gpu.rs`: installed-driver opt-in tests. The standard test covers + M=1/3/4/5 × N=256/512/2048/2560/6144, ties away from zero, signed zeros, + cancellation, half-scale underflow, FP16 range, hand arithmetic and nonfinite + weight halves. Public calls repeat against the frozen native result, verify + immutable caller inputs and zero remaining temporary charge. Six numeric + refusal cases exercise the native status path and preserve caller output. +- Its separate real-tensor test accepts only the explicitly selected Qwen + `blk.4.ffn_up_shexp.weight` 640×2560 private fixture and our deterministic + synthetic activation. This proves one projection, not model quality or forward + execution. Packed weights stay outside Git. +- `tests/q5_mock.rs` / `q5_mock_cuda.c`: independent CPU ELF routing, geometry, + admission, cache, delayed argument ownership, sync/restore poison, and consuming + cleanup proof. The existing fixture is included unchanged. Synthetic mock + scratch/output arithmetic is **not** the native numerical oracle. +- `test-cuda-driver-bootstrap.sh` preserves the complete existing CPU gate and + adds the focused Q5 matrix and an output-alias compile refusal. + +GPU tests require `LELEKA_CUDA_REAL_GPU=1`, use one test thread and a bounded outer +`timeout`. The parity test additionally requires absolute private paths in +`LELEKA_Q5_EVIDENCE`, `LELEKA_Q5_NATIVE_ORACLE`, `LELEKA_Q5_TRUSTED_PTX`. +The real-tensor test also requires `LELEKA_Q5_REAL_INPUT`. Fixture subprocesses have +30-second deadlines. Capture stdout/stderr/exits and hardware/driver/pre-post GPU +state, retain private digests and remove only owned remote resources. Ordinary +`cargo test` leaves these GPU tests ignored. diff --git a/scripts/cuda-q5-oracle/native_fixture.cu b/scripts/cuda-q5-oracle/native_fixture.cu new file mode 100644 index 00000000..69bc2ab3 --- /dev/null +++ b/scripts/cuda-q5-oracle/native_fixture.cu @@ -0,0 +1,46 @@ +// Independent test-only runner. Compile the ORIGINAL native_mmvq.cu unchanged. +// Does not decode native weights, share candidate scratch, or select arbitrary entries. +#include "strata/kernels/native_mmvq.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +static void ck(cudaError_t e){if(e!=cudaSuccess)throw std::runtime_error(cudaGetErrorString(e));} +static void dc(CUresult e){if(e!=CUDA_SUCCESS)throw std::runtime_error("Driver test fixture status "+std::to_string(e));} +template static T sym(void* lib,const char* name){void* p=dlsym(lib,name);if(!p)throw std::runtime_error(name);return reinterpret_cast(p);} +int main(int argc,char**argv) try { + if(argc!=4)throw std::runtime_error("Usage: native_fixture INPUT OUTPUT TRUSTED_Q5_PTX"); + std::ifstream in(argv[1],std::ios::binary);uint32_t h[3];in.read(reinterpret_cast(h),sizeof h); + if(!in||h[0]!=0x35314b51||h[1]<1||h[1]>640||h[2]<256||h[2]>6144||h[2]%256)throw std::runtime_error("bounded synthetic geometry"); + const auto m=h[1],n=h[2];size_t wb=size_t(m)*(n/256)*176,qb=size_t(n/32)*36; + std::vectorw(wb),q(qb),fresh(qb),candidate(qb);std::vectorx(n),y(m); + in.read(reinterpret_cast(w.data()),wb);in.read(reinterpret_cast(x.data()),n*4);if(!in||in.peek()!=EOF)throw std::runtime_error("fixture length"); + // Invalid activation is tested in Rust/candidate admission; never feed it into + // the original unchecked native float-to-integer quantizer. + for(float v:x)if(!std::isfinite(v))throw std::runtime_error("native fixture requires finite input"); + ck(cudaSetDevice(0));cudaStream_t stream;ck(cudaStreamCreate(&stream));void *dw,*dx,*dq,*df,*dcq,*dy,*ds; + ck(cudaMalloc(&dw,wb));ck(cudaMalloc(&dx,n*4));ck(cudaMalloc(&dq,qb));ck(cudaMalloc(&df,qb));ck(cudaMalloc(&dcq,qb));ck(cudaMalloc(&dy,m*4));ck(cudaMalloc(&ds,4)); + ck(cudaMemcpy(dw,w.data(),wb,cudaMemcpyHostToDevice));ck(cudaMemcpy(dx,x.data(),n*4,cudaMemcpyHostToDevice)); + strata::kernels::native_quantize_q8_1(static_cast(dx),dq,n,1,stream);ck(cudaStreamSynchronize(stream));ck(cudaMemcpy(q.data(),dq,qb,cudaMemcpyDeviceToHost)); + // Composition independently quantizes a second fresh native scratch allocation. + strata::kernels::native_q5_k_f32(dw,static_cast(dx),df,static_cast(dy),n,m,1,stream);ck(cudaStreamSynchronize(stream)); + ck(cudaMemcpy(fresh.data(),df,qb,cudaMemcpyDeviceToHost));ck(cudaMemcpy(y.data(),dy,m*4,cudaMemcpyDeviceToHost));if(q!=fresh)throw std::runtime_error("fresh native quantization disagrees"); + std::ifstream pt(argv[3],std::ios::binary);std::string ptx((std::istreambuf_iterator(pt)),{});if(ptx.empty()||ptx.size()>262144)throw std::runtime_error("bounded trusted test asset");ptx.push_back(0); + void* lib=dlopen("libcuda.so.1",RTLD_NOW|RTLD_LOCAL);if(!lib)throw std::runtime_error("installed Driver missing"); + auto load=sym(lib,"cuModuleLoadData");auto get=sym(lib,"cuModuleGetFunction"); + auto launch=sym(lib,"cuLaunchKernel");auto unload=sym(lib,"cuModuleUnload"); + CUmodule mod;CUfunction f;dc(load(&mod,ptx.data()));dc(get(&f,mod,"leleka_quantize_q8_1"));ck(cudaMemset(ds,0,4)); + CUdeviceptr px=reinterpret_cast(dx),pq=reinterpret_cast(dcq),ps=reinterpret_cast(ds);uint32_t cols=n;void*args[]={&px,&pq,&ps,&cols}; + dc(launch(f,(n+255)/256,1,1,256,1,1,0,reinterpret_cast(stream),args,nullptr));ck(cudaStreamSynchronize(stream)); + uint32_t status;ck(cudaMemcpy(&status,ds,4,cudaMemcpyDeviceToHost));ck(cudaMemcpy(candidate.data(),dcq,qb,cudaMemcpyDeviceToHost));dc(unload(mod));dlclose(lib); + std::ofstream out(argv[2],std::ios::binary);uint32_t oh[]={0x35314f51,m,n,status};out.write(reinterpret_cast(oh),sizeof oh);out.write(reinterpret_cast(q.data()),qb);out.write(reinterpret_cast(candidate.data()),qb);out.write(reinterpret_cast(y.data()),m*4);if(!out)throw std::runtime_error("output write"); + for(void*p:{dw,dx,dq,df,dcq,dy,ds})ck(cudaFree(p));ck(cudaStreamDestroy(stream));ck(cudaDeviceReset()); + std::cout<<"NATIVE_ORACLE rows="<"$work/reject.log"; then echo 'matvec output alias escaped' >&2; exit 1; fi grep -q 'cannot borrow.*buffer.*as mutable because it is also borrowed as immutable' "$work/reject.log" echo 'matvec output alias compile refusal: PASS' + cat > "$work/reject.rs" <<'RSQ5' +use leleka_cuda_driver::{Driver,Q5KProjectionShape}; +fn main(){let d=Driver::load().unwrap();let c=d.device(0).unwrap().create_context(4096).unwrap();let mut w=c.allocate(176).unwrap();let x=c.allocate(1024).unwrap();c.q5_k_q8_1(&w,&x,&mut w,Q5KProjectionShape{rows:1,columns:256}).unwrap();} +RSQ5 + if rustc --edition=2024 --extern "leleka_cuda_driver=$rlib" -L "dependency=$work/target/debug/deps" \ + "$work/reject.rs" -o "$work/reject" 2>"$work/reject.log"; then echo 'Q5 output alias escaped' >&2; exit 1; fi + grep -q 'cannot borrow.*w.*as mutable because it is also borrowed as immutable' "$work/reject.log" + echo 'Q5 output alias compile refusal: PASS' cargo tree --offline --locked --manifest-path "${repo_root}/Cargo.toml" -p leleka-cuda-driver --edges normal exit 0 fi