189 lines
7.5 KiB
Rust
189 lines
7.5 KiB
Rust
//! Moved out of the parent module by STRA-338: impl Context (2).
|
|
|
|
#[allow(unused_imports)]
|
|
use super::*;
|
|
|
|
impl Context<'_> {
|
|
/// Compute each row's dot product in ascending column order with separate
|
|
/// binary32 multiply/add rounding. All buffers are exact whole allocations;
|
|
/// output is usable as a computed vector only after a successful return.
|
|
pub fn matvec_f32(
|
|
&self,
|
|
matrix: &DeviceBuffer<'_>,
|
|
vector: &DeviceBuffer<'_>,
|
|
output: &mut DeviceBuffer<'_>,
|
|
rows: usize,
|
|
columns: usize,
|
|
) -> Result<()> {
|
|
let (shape, handles) = self.matvec_admission([matrix, vector, output], rows, columns)?;
|
|
let [matrix_handle, vector_handle, output_handle] = handles;
|
|
let mut arguments =
|
|
ffi::MatvecArguments::new(matrix_handle, vector_handle, output_handle, shape.rows, shape.columns);
|
|
let guard = self.enter()?;
|
|
let kernel = match self.fixed_kernel(FixedOperation::Matvec) {
|
|
Ok(kernel) => kernel,
|
|
Err(error) => return combine(Err(error), guard.finish()),
|
|
};
|
|
let launch = self.driver.api.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_quantized_temporaries(operation, scratch, status)
|
|
}
|
|
|
|
/// Project one resident F32 activation through row-major Q6_K weights.
|
|
/// Activations use the native Q8_1 block and reduction contract.
|
|
pub fn q6_k_q8_1(
|
|
&self,
|
|
weights: &DeviceBuffer<'_>,
|
|
activation: &DeviceBuffer<'_>,
|
|
output: &mut DeviceBuffer<'_>,
|
|
dimensions: Q6KProjectionShape,
|
|
) -> Result<()> {
|
|
let (shape, handles, temporary_bytes) = self.q6_k_admission(weights, activation, output, dimensions)?;
|
|
|
|
// Charge the complete scratch/status requirement before native mutation.
|
|
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.q6_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::Q6ProjectionArguments::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_q6_projection(kernels.project, shape.projection_grid, &mut project_arguments);
|
|
self.complete_launch(project, project_arguments, project_guard)
|
|
})();
|
|
|
|
self.release_quantized_temporaries(operation, scratch, status)
|
|
}
|
|
|
|
/// Project one resident activation using a closed packed format/role.
|
|
/// Unintegrated formats refuse before any native mutation. Output is usable
|
|
/// only after successful synchronized return, never after a partial failure.
|
|
pub fn project_q8_1(
|
|
&self,
|
|
weights: &DeviceBuffer<'_>,
|
|
activation: &DeviceBuffer<'_>,
|
|
output: &mut DeviceBuffer<'_>,
|
|
spec: ProjectionSpec,
|
|
) -> Result<()> {
|
|
self.project_q8_1_weight(NativeWeights::Raw(weights), activation, output, spec)
|
|
}
|
|
}
|