Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Optimize weight layout for untied readout (#1027)

CCYeh1763a366c048
diff --git a/crates/uzu-engine/src/encodable_block/linear/matmul.rs b/crates/uzu-engine/src/encodable_block/linear/matmul.rs
index 172ecda61..d0d5a8bfe 100644
--- a/crates/uzu-engine/src/encodable_block/linear/matmul.rs
+++ b/crates/uzu-engine/src/encodable_block/linear/matmul.rs
@@ -108,23 +108,22 @@ impl<B: Backend> LinearMatmul<B> {
})
}
- pub(super) fn prepare_a8(
+ pub(super) fn prepare_a8_matmul(
&mut self,
context: &B::Context,
- ) -> Option<ActivationQuantization> {
+ ) -> Result<Option<ActivationQuantization>, B::Error> {
let mut candidate = self.matmul_shape(1, false);
- candidate.signed_codes = self.matrix.a8_signed_codes()?;
- let quantization = self.kernel.lock().select_activation_quantization(&candidate, context)?;
- self.matrix.try_prepare_a8_storage().then_some(quantization)
- }
-
- pub fn optimize_weights_layout(
- &mut self,
- context: &B::Context,
- ) -> Result<(), B::Error> {
- let shape = self.matmul_shape(1, false);
+ let Some(signed_codes) = self.matrix.a8_signed_codes() else {
+ return Ok(None);
+ };
+ candidate.signed_codes = signed_codes;
let kernel = self.kernel.lock();
- self.matrix.optimize_weights_layout(|weights| kernel.optimize_weights_layout(&shape, weights, context))
+ let quantization = kernel.select_activation_quantization(&candidate, context);
+ if quantization.is_none() || !self.matrix.try_prepare_a8_storage() {
+ return Ok(None);
+ }
+ self.matrix.optimize_weights_layout(|weights| kernel.optimize_weights_layout(&candidate, weights, context))?;
+ Ok(quantization)
}
pub(super) fn encode_with_a(
diff --git a/crates/uzu-engine/src/encodable_block/linear/rht_wrapper.rs b/crates/uzu-engine/src/encodable_block/linear/rht_wrapper.rs
index 4772b0f08..ce00dda75 100644
--- a/crates/uzu-engine/src/encodable_block/linear/rht_wrapper.rs
+++ b/crates/uzu-engine/src/encodable_block/linear/rht_wrapper.rs
@@ -72,10 +72,8 @@ impl<B: Backend> RHTLinearWrapper<B> {
parameter_tree,
context,
)?;
- let activation_quantization = inner_linear.prepare_a8(context);
- if activation_quantization.is_some() {
- inner_linear.optimize_weights_layout(context).map_err(RHTLinearWrapperError::BackendError)?;
- }
+ let activation_quantization =
+ inner_linear.prepare_a8_matmul(context).map_err(RHTLinearWrapperError::BackendError)?;
Self::build_self_contained(
input_data_type,
LinearInputPreparation {
@@ -116,11 +114,10 @@ impl<B: Backend> RHTLinearWrapper<B> {
)?;
let input_preparation = LinearInputPreparation {
rht_signs,
- activation_quantization: inner_linear.prepare_a8(context),
+ activation_quantization: inner_linear
+ .prepare_a8_matmul(context)
+ .map_err(RHTLinearWrapperError::BackendError)?,
};
- if input_preparation.activation_quantization.is_some() {
- inner_linear.optimize_weights_layout(context).map_err(RHTLinearWrapperError::BackendError)?;
- }
if input_preparation.activation_quantization.is_some() && !allow_prequantized_activation {
let wrapper = Self::build_self_contained(input_data_type, input_preparation, inner_linear, context)?;
Ok(Some((Box::new(wrapper), None)))
diff --git a/crates/uzu-engine/src/encodable_block/linear/untied_readout.rs b/crates/uzu-engine/src/encodable_block/linear/untied_readout.rs
index 1237d2233..5c21fe3f0 100644
--- a/crates/uzu-engine/src/encodable_block/linear/untied_readout.rs
+++ b/crates/uzu-engine/src/encodable_block/linear/untied_readout.rs
@@ -59,7 +59,7 @@ impl<B: Backend> UntiedReadout<B> {
let rht_signs = tree.subtree("incoherence_signs").read_buffer("input_signs", &[model_dim], DataType::I32)?;
let preparation = LinearInputPreparation {
rht_signs,
- activation_quantization: linear.prepare_a8(context),
+ activation_quantization: linear.prepare_a8_matmul(context).map_err(LinearMatmulError::BackendError)?,
};
let input_rht = InputRht::new(data_type, preparation, /* in_place */ false, context)
.map_err(LinearMatmulError::BackendError)?;