A high-performance inference engine for AI models
use crate::{
backends::common::{
Backend, BufferRef, CommandBuffer, CommandBufferEncoding,
gpu_types::hadamard_order::HADAMARD_TRANSFORM_BLOCK_SIZE,
kernel::matmul::{ActivationFormat, MatmulA},
},
config::weight_matrix::{
AnyWeightMatrixSpec,
hybrid_spec::{AnyIncoherenceProcessorSpec, HadamardSpec, HybridSpec, IncoherenceProcessingMode},
},
data_type::DataType,
encodable_block::linear::{
Linear, LinearEncodeError, LinearInputPreparation, LinearMatmul, LinearMatmulError, input_rht::InputRht,
},
safetensors::ParameterTree,
};
pub struct UntiedReadout<B: Backend> {
linear: LinearMatmul<B>,
input_rht: Option<InputRht<B>>,
}
impl<B: Backend> UntiedReadout<B> {
pub async fn load(
vocab_size: u32,
model_dim: u32,
data_type: DataType,
spec: AnyWeightMatrixSpec,
tree: &ParameterTree<'_, B>,
context: &B::Context,
) -> Result<Self, LinearMatmulError<B>> {
let AnyWeightMatrixSpec::HybridSpec(HybridSpec {
quantization_spec,
incoherence_spec:
Some(AnyIncoherenceProcessorSpec::HadamardSpec(HadamardSpec {
block_size: HADAMARD_TRANSFORM_BLOCK_SIZE,
processing_mode: IncoherenceProcessingMode::Input,
..
})),
..
}) = spec
else {
return Ok(Self {
linear: LinearMatmul::load(
model_dim, vocab_size, data_type, data_type, data_type, spec, None, tree, None, context,
)
.await?,
input_rht: None,
});
};
let mut linear = LinearMatmul::load(
model_dim,
vocab_size,
data_type,
data_type,
data_type,
*quantization_spec,
None,
&tree.subtree("quantized"),
None,
context,
)
.await?;
let rht_signs =
tree.subtree("incoherence_signs").read_buffer("input_signs", &[model_dim], DataType::I32).await?;
let activation_format = linear.prepare_activation(context).map_err(LinearMatmulError::BackendError)?;
let preparation = LinearInputPreparation {
rht_signs,
activation_format,
};
let input_rht = InputRht::new(data_type, preparation, /* in_place */ false, context)
.map_err(LinearMatmulError::BackendError)?;
Ok(Self {
linear,
input_rht: Some(input_rht),
})
}
pub fn encode(
&self,
input: impl BufferRef<Backend = B>,
batch_dim: u32,
output_dim: u32,
gather_indices: Option<impl BufferRef<Backend = B>>,
command_buffer: &mut <B::CommandBuffer as CommandBuffer>::Encoding,
) -> Result<B::ScratchBuffer, LinearEncodeError<B>> {
let Some(input_rht) = &self.input_rht else {
let a = MatmulA::full_precision(input);
return self.linear.encode_with_a(a, batch_dim, output_dim, gather_indices, command_buffer);
};
let format = if gather_indices.is_some() {
ActivationFormat::Bf16
} else {
self.linear.select_activation_format(batch_dim, Some(output_dim), command_buffer.context())
};
let input = input_rht.prepare(input, batch_dim, format, command_buffer).map_err(LinearEncodeError::Backend)?;
self.linear.encode_with_a(input.as_ref(), batch_dim, output_dim, gather_indices, command_buffer)
}
}