Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Remove EmbeddingsBuffers

Aleksei Savin59f1b768a542
diff --git a/crates/uzu/src/backends/metal/classifier_context.rs b/crates/uzu/src/backends/metal/classifier_context.rs
index 3ab7ddcff..fbf9a37cc 100644
--- a/crates/uzu/src/backends/metal/classifier_context.rs
+++ b/crates/uzu/src/backends/metal/classifier_context.rs
@@ -108,7 +108,6 @@ impl ClassifierContext {
{
let mut shared_bufs = shared_buffers.borrow_mut();
- shared_bufs.embeddings.update_data(&root_loader_view);
if let Some(global_rope) = &mut shared_bufs.global_rope {
global_rope.update_data(
&transformer_tree,
diff --git a/crates/uzu/src/backends/metal/forward_pass/mod.rs b/crates/uzu/src/backends/metal/forward_pass/mod.rs
index 72714b73b..4451eaeb2 100644
--- a/crates/uzu/src/backends/metal/forward_pass/mod.rs
+++ b/crates/uzu/src/backends/metal/forward_pass/mod.rs
@@ -19,10 +19,10 @@ pub use scratch_buffers::ScratchBuffers;
pub use short_conv_layer::ShortConvLayer;
pub use ssm_layer::SSMLayer;
pub use state::{
- ArrayCell, ArrayId, ClassifierModeState, CommonAuxBuffers,
- EmbeddingsBuffers, ForwardPassMode, ForwardPassState, HashMapId,
- LanguageModelGeneratorAuxBuffers, LanguageModelGeneratorModeState,
- MoeExpertWeights, RopeBuffers, RopeType, SharedBuffers,
+ ArrayCell, ArrayId, ClassifierModeState, CommonAuxBuffers, ForwardPassMode,
+ ForwardPassState, HashMapId, LanguageModelGeneratorAuxBuffers,
+ LanguageModelGeneratorModeState, MoeExpertWeights, RopeBuffers, RopeType,
+ SharedBuffers,
};
pub use super::encodable_block::{EncodableBlock, EncodingParameters};
diff --git a/crates/uzu/src/backends/metal/forward_pass/state/array_id.rs b/crates/uzu/src/backends/metal/forward_pass/state/array_id.rs
index 5558fa56a..de32f4a8b 100644
--- a/crates/uzu/src/backends/metal/forward_pass/state/array_id.rs
+++ b/crates/uzu/src/backends/metal/forward_pass/state/array_id.rs
@@ -49,9 +49,6 @@ pub enum ArrayId {
AttentionSums,
AttentionMaxs,
- EmbeddingsInputWeights,
- EmbeddingsOutputWeights,
- EmbeddingsScales,
RopeCosines(RopeType),
RopeSines(RopeType),
diff --git a/crates/uzu/src/backends/metal/forward_pass/state/embeddings_buffers.rs b/crates/uzu/src/backends/metal/forward_pass/state/embeddings_buffers.rs
deleted file mode 100644
index 14c49022b..000000000
--- a/crates/uzu/src/backends/metal/forward_pass/state/embeddings_buffers.rs
+++ /dev/null
@@ -1,355 +0,0 @@
-use std::{cell::RefCell, rc::Rc};
-
-use super::super::ModelShape;
-use crate::{
- DeviceContext,
- backends::metal::{MTLContext, MetalArray},
- config::EmbeddingConfig,
- parameters::ParameterTree,
-};
-
-type ArrayCell = RefCell<MetalArray>;
-
-#[allow(dead_code)]
-pub enum EmbeddingsBuffers {
- Tied {
- /// [vocab_size, model_dim]
- weights: ArrayCell,
- },
- Untied {
- /// [vocab_size, model_dim]
- input_weights: ArrayCell,
- /// [vocab_size, model_dim]
- output_weights: ArrayCell,
- },
- QuantizedTied {
- /// [vocab_size, model_dim]
- weights: ArrayCell,
- /// [vocab_size]
- scales: ArrayCell,
- },
- MLXSemiQuantizedUntied {
- /// [vocab_size, model_dim]
- input_weights: ArrayCell,
- /// [vocab_size, model_dim]
- packed_output_weights: ArrayCell,
- /// [vocab_size, num_groups]
- output_scales: ArrayCell,
- /// [vocab_size, num_groups]
- output_biases: ArrayCell,
- },
- MLXQuantizedUntied {
- /// [vocab_size, model_dim/pack]
- packed_input_weights: ArrayCell,
- /// [vocab_size, num_groups]
- input_scales: ArrayCell,
- /// [vocab_size, num_groups]
- input_biases: ArrayCell,
- /// [vocab_size, model_dim/pack]
- packed_output_weights: ArrayCell,
- /// [vocab_size, num_groups]
- output_scales: ArrayCell,
- /// [vocab_size, num_groups]
- output_biases: ArrayCell,
- },
-}
-
-impl EmbeddingsBuffers {
- pub fn new(
- context: &MTLContext,
- embeddings_config: &EmbeddingConfig,
- model_shape: &ModelShape,
- ) -> Self {
- let array_label = |config: &EmbeddingConfig, name: &str| -> String {
- let prefix = "embeddings_buffers";
- let config_name = match config {
- EmbeddingConfig::Tied {
- ..
- } => "tied",
- EmbeddingConfig::Untied {
- ..
- } => "untied",
- EmbeddingConfig::QuantizedTied {
- ..
- } => "quantized_tied",
- EmbeddingConfig::MLXQuantizedTied {
- ..
- } => "mlx_quantized_tied",
- EmbeddingConfig::MLXSemiQuantizedUntied {
- ..
- } => "mlx_semi_quantized_untied",
- EmbeddingConfig::MLXQuantizedUntied {
- ..
- } => "mlx_quantized_untied",
- };
- format!("{prefix}_{config_name}_{name}")
- };
-
- unsafe {
- match embeddings_config {
- EmbeddingConfig::Tied {
- common: _,
- precision: _,
- } => Self::Tied {
- weights: RefCell::new(context.array_uninitialized(
- &model_shape.embeddings_input_shape(),
- model_shape.activation_data_type(),
- array_label(embeddings_config, "weights"),
- )),
- },
- EmbeddingConfig::Untied {
- common: _,
- precision: _,
- } => Self::Untied {
- input_weights: RefCell::new(context.array_uninitialized(
- &model_shape.embeddings_input_shape(),
- model_shape.activation_data_type(),
- array_label(embeddings_config, "input_weights"),
- )),
- output_weights: RefCell::new(context.array_uninitialized(
- &model_shape.embeddings_output_shape(),
- model_shape.activation_data_type(),
- array_label(embeddings_config, "output_weights"),
- )),
- },
- EmbeddingConfig::QuantizedTied {
- embedding_quantization_mode,
- ..
- } => {
- let [vocab_size, model_dim] =
- model_shape.quantized_embeddings_weights_shape();
- Self::QuantizedTied {
- weights: RefCell::new(
- context.array_uninitialized(
- &[
- vocab_size,
- model_dim
- / embedding_quantization_mode
- .packing_divisor(),
- ],
- embedding_quantization_mode.storage_type(),
- array_label(embeddings_config, "weights"),
- ),
- ),
- scales: RefCell::new(context.array_uninitialized(
- &model_shape.quantized_embeddings_scales_shape(),
- model_shape.activation_data_type(),
- array_label(embeddings_config, "scales"),
- )),
- }
- },
- EmbeddingConfig::MLXQuantizedTied {
- group_size,
- embedding_quantization_mode,
- ..
- } => {
- let [vocab_size, model_dim] =
- model_shape.quantized_embeddings_weights_shape();
- let num_groups = model_dim / group_size;
- Self::QuantizedTied {
- weights: RefCell::new(
- context.array_uninitialized(
- &[
- vocab_size,
- model_dim
- / embedding_quantization_mode
- .packing_divisor(),
- ],
- embedding_quantization_mode.storage_type(),
- array_label(embeddings_config, "weights"),
- ),
- ),
- scales: RefCell::new(context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "scales"),
- )),
- }
- },
- EmbeddingConfig::MLXSemiQuantizedUntied {
- group_size,
- embedding_quantization_mode,
- ..
- } => {
- let [vocab_size, model_dim] =
- model_shape.quantized_embeddings_weights_shape();
- let num_groups = model_dim / group_size;
- Self::MLXSemiQuantizedUntied {
- input_weights: RefCell::new(
- context.array_uninitialized(
- &model_shape.embeddings_input_shape(),
- model_shape.activation_data_type(),
- array_label(embeddings_config, "input_weights"),
- ),
- ),
- packed_output_weights: RefCell::new(
- context.array_uninitialized(
- &[
- vocab_size,
- model_dim
- / embedding_quantization_mode
- .packing_divisor(),
- ],
- embedding_quantization_mode.storage_type(),
- array_label(
- embeddings_config,
- "packed_output_weights",
- ),
- ),
- ),
- output_scales: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "output_scales"),
- ),
- ),
- output_biases: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "output_biases"),
- ),
- ),
- }
- },
- EmbeddingConfig::MLXQuantizedUntied {
- group_size,
- embedding_quantization_mode,
- ..
- } => {
- let [vocab_size, model_dim] =
- model_shape.quantized_embeddings_weights_shape();
- let num_groups = model_dim / group_size;
- let pack = embedding_quantization_mode.packing_divisor();
- Self::MLXQuantizedUntied {
- packed_input_weights: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, model_dim / pack],
- embedding_quantization_mode.storage_type(),
- array_label(
- embeddings_config,
- "packed_input_weights",
- ),
- ),
- ),
- input_scales: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "input_scales"),
- ),
- ),
- input_biases: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "input_biases"),
- ),
- ),
- packed_output_weights: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, model_dim / pack],
- embedding_quantization_mode.storage_type(),
- array_label(
- embeddings_config,
- "packed_output_weights",
- ),
- ),
- ),
- output_scales: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "output_scales"),
- ),
- ),
- output_biases: RefCell::new(
- context.array_uninitialized(
- &[vocab_size, num_groups],
- model_shape.activation_data_type(),
- array_label(embeddings_config, "output_biases"),
- ),
- ),
- }
- },
- }
- }
- }
-
- pub fn update_data(
- &mut self,
- parameter_tree: &ParameterTree<Rc<MTLContext>>,
- ) {
- let embeddings_tree = parameter_tree.subtree("embedding").unwrap();
- match self {
- EmbeddingsBuffers::Tied {
- weights,
- } => {
- let embeddings_view = embeddings_tree.leaf("weights").unwrap();
- weights.borrow_mut().copy_from_array(&embeddings_view);
- },
- EmbeddingsBuffers::Untied {
- input_weights,
- output_weights,
- } => {
- let mapping = vec![
- ("input_weights", input_weights),
- ("output_weights", output_weights),
- ];
- for (name, buffer) in mapping {
- let view = embeddings_tree.leaf(name).unwrap();
- buffer.borrow_mut().copy_from_array(&view);
- }
- },
- EmbeddingsBuffers::QuantizedTied {
- weights,
- scales,
- } => {
- let mapping = vec![("weights", weights), ("scales", scales)];
- for (name, buffer) in mapping {
- let view = embeddings_tree.leaf(name).unwrap();
- buffer.borrow_mut().copy_from_array(&view);
- }
- },
- EmbeddingsBuffers::MLXSemiQuantizedUntied {
- input_weights,
- packed_output_weights,
- output_scales,
- output_biases,
- } => {
- let mapping = vec![
- ("input_weights", input_weights),
- ("output_weights", packed_output_weights),
- ("output_scales", output_scales),
- ("output_biases", output_biases),
- ];
- for (name, buffer) in mapping {
- let view = embeddings_tree.leaf(name).unwrap();
- buffer.borrow_mut().copy_from_array(&view);
- }
- },
- EmbeddingsBuffers::MLXQuantizedUntied {
- packed_input_weights,
- input_scales,
- input_biases,
- packed_output_weights,
- output_scales,
- output_biases,
- } => {
- let mapping = vec![
- ("input_weights", packed_input_weights),
- ("input_scales", input_scales),
- ("input_biases", input_biases),
- ("output_weights", packed_output_weights),
- ("output_scales", output_scales),
- ("output_biases", output_biases),
- ];
- for (name, buffer) in mapping {
- let view = embeddings_tree.leaf(name).unwrap();
- buffer.borrow_mut().copy_from_array(&view);
- }
- },
- }
- }
-}
diff --git a/crates/uzu/src/backends/metal/forward_pass/state/mod.rs b/crates/uzu/src/backends/metal/forward_pass/state/mod.rs
index eb484bfd0..c12d5681f 100644
--- a/crates/uzu/src/backends/metal/forward_pass/state/mod.rs
+++ b/crates/uzu/src/backends/metal/forward_pass/state/mod.rs
@@ -1,6 +1,5 @@
mod array_id;
mod common_aux_buffers;
-mod embeddings_buffers;
mod hash_map_id;
mod language_model_generator_aux_buffers;
mod mode;
@@ -12,7 +11,6 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
pub use array_id::ArrayId;
pub use common_aux_buffers::CommonAuxBuffers;
-pub use embeddings_buffers::EmbeddingsBuffers;
pub use hash_map_id::HashMapId;
pub use language_model_generator_aux_buffers::LanguageModelGeneratorAuxBuffers;
pub use mode::{
@@ -608,11 +606,7 @@ impl ForwardPassState {
ArrayId::AttentionMaxs => self.common_aux.attention_maxs.clone(),
// Shared buffer arrays
- ArrayId::EmbeddingsInputWeights
- | ArrayId::EmbeddingsOutputWeights
- | ArrayId::EmbeddingsScales
- | ArrayId::RopeCosines(_)
- | ArrayId::RopeSines(_) => self
+ ArrayId::RopeCosines(_) | ArrayId::RopeSines(_) => self
.shared_buffer_array(id)
.expect("Shared buffer array should be available"),
@@ -823,67 +817,6 @@ impl ForwardPassState {
) -> Option<ArrayCell> {
let shared = self.shared_buffers.borrow();
match id {
- ArrayId::EmbeddingsInputWeights => Some(match &shared.embeddings {
- EmbeddingsBuffers::Tied {
- weights,
- } => weights.clone(),
- EmbeddingsBuffers::Untied {
- input_weights,
- ..
- } => input_weights.clone(),
- EmbeddingsBuffers::QuantizedTied {
- weights,
- ..
- } => weights.clone(),
- EmbeddingsBuffers::MLXSemiQuantizedUntied {
- input_weights,
- ..
- } => input_weights.clone(),
- EmbeddingsBuffers::MLXQuantizedUntied {
- packed_input_weights,
- ..
- } => packed_input_weights.clone(),
- }),
- ArrayId::EmbeddingsOutputWeights => {
- Some(match &shared.embeddings {
- EmbeddingsBuffers::Tied {
- weights,
- } => weights.clone(),
- EmbeddingsBuffers::Untied {
- output_weights,
- ..
- } => output_weights.clone(),
- EmbeddingsBuffers::QuantizedTied {
- weights,
- ..
- } => weights.clone(),
- EmbeddingsBuffers::MLXSemiQuantizedUntied {
- packed_output_weights,
- ..
- } => packed_output_weights.clone(),
- EmbeddingsBuffers::MLXQuantizedUntied {
- packed_output_weights,
- ..
- } => packed_output_weights.clone(),
- })
- },
- ArrayId::EmbeddingsScales => Some(match &shared.embeddings {
- EmbeddingsBuffers::QuantizedTied {
- scales,
- ..
- } => scales.clone(),
- EmbeddingsBuffers::MLXSemiQuantizedUntied {
- output_scales,
- ..
- } => output_scales.clone(),
- EmbeddingsBuffers::MLXQuantizedUntied {
- output_scales,
- ..
- } => output_scales.clone(),
- _ => panic!(
- "Expected QuantizedTied or MLXSemiQuantizedUntied embeddings"
- ),
- }),
ArrayId::RopeCosines(rope_type) => Some(match rope_type {
RopeType::Global => shared
.global_rope
diff --git a/crates/uzu/src/backends/metal/forward_pass/state/shared_buffers.rs b/crates/uzu/src/backends/metal/forward_pass/state/shared_buffers.rs
index c3d974aa7..cf6446d79 100644
--- a/crates/uzu/src/backends/metal/forward_pass/state/shared_buffers.rs
+++ b/crates/uzu/src/backends/metal/forward_pass/state/shared_buffers.rs
@@ -2,7 +2,7 @@ use std::{cell::RefCell, rc::Rc};
use half::{bf16, f16};
-use super::{super::ModelShape, EmbeddingsBuffers, RopeBuffers};
+use super::{super::ModelShape, RopeBuffers};
use crate::{
Array, DataType, DeviceContext,
backends::metal::{MTLContext, MetalArray},
@@ -18,7 +18,6 @@ pub struct MoeExpertWeights {
}
pub struct SharedBuffers {
- pub embeddings: EmbeddingsBuffers,
pub global_rope: Option<RopeBuffers>,
pub local_rope: Option<RopeBuffers>,
pub moe_expert_weights: Option<Vec<MoeExpertWeights>>,
@@ -31,12 +30,6 @@ impl SharedBuffers {
decoder_config: &crate::config::DecoderConfig,
model_shape: &ModelShape,
) -> Self {
- let embeddings = EmbeddingsBuffers::new(
- context,
- &decoder_config.embedding_config,
- model_shape,
- );
-
let global_rope = if decoder_config.global_rope_config.is_some() {
Some(RopeBuffers::new(context, model_shape))
} else {
@@ -82,7 +75,6 @@ impl SharedBuffers {
};
Self {
- embeddings,
global_rope,
local_rope,
moe_expert_weights,
@@ -94,8 +86,6 @@ impl SharedBuffers {
&mut self,
parameter_tree: &ParameterTree<Rc<MTLContext>>,
) {
- self.embeddings.update_data(parameter_tree);
-
let transformer_tree = parameter_tree
.subtree("transformer")
.expect("transformer subtree not found");