Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Generalize Activation

eugene733c938d7240
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
new file mode 100644
index 000000000..e670b5581
--- /dev/null
+++ b/.vscode/tasks.json
@@ -0,0 +1,11 @@
+{
+ "version": "2.0.0",
+ "tasks": [
+ {
+ "label": "Format Code",
+ "type": "shell",
+ "command": "cargo +nightly fmt && ((command -v metal-analyzer >/dev/null 2>&1 && metal-analyzer format) || ./scripts/clang-format.sh)",
+ "problemMatcher": []
+ }
+ ]
+}
diff --git a/.zed/tasks.json b/.zed/tasks.json
new file mode 100644
index 000000000..00dd3d250
--- /dev/null
+++ b/.zed/tasks.json
@@ -0,0 +1,11 @@
+[
+ {
+ "label": "Format Code",
+ "command": "cargo +nightly fmt && ((command -v metal-analyzer >/dev/null 2>&1 && metal-analyzer format) || ./scripts/clang-format.sh)",
+ "cwd": "$ZED_WORKTREE_ROOT",
+ "use_new_terminal": false,
+ "allow_concurrent_runs": false,
+ "reveal": "always",
+ "hide": "never"
+ }
+]
diff --git a/crates/uzu/src/backends/metal/classifier_context.rs b/crates/uzu/src/backends/metal/classifier_context.rs
index 059f4441c..c509eb89c 100644
--- a/crates/uzu/src/backends/metal/classifier_context.rs
+++ b/crates/uzu/src/backends/metal/classifier_context.rs
@@ -217,7 +217,7 @@ impl ClassifierContext {
);
let prediction_head_activation = Box::new(
- Activation::new(
+ Activation::<Metal>::new(
&context,
prediction_head_data_type,
prediction_head_config.activation.clone(),
diff --git a/crates/uzu/src/backends/metal/encodable_block/activation/mod.rs b/crates/uzu/src/backends/metal/encodable_block/activation/mod.rs
deleted file mode 100644
index be83d717e..000000000
--- a/crates/uzu/src/backends/metal/encodable_block/activation/mod.rs
+++ /dev/null
@@ -1,86 +0,0 @@
-use super::{
- super::{MTLContext, MTLError, kernel::dsl::ActivationMetalKernel},
- EncodableBlock,
-};
-use crate::{
- DataType,
- backends::{
- common::kernel::ActivationKernel,
- metal::{MTLCommandBuffer, MTLCommandEncoder, MTLComputeCommandEncoder, Metal, ProtocolObject, Retained},
- },
- config::Activation as ActivationConfig,
- encodable_block::EncodingParameters,
- forward_pass::state::{ArrayId, ForwardPassState},
-};
-
-pub struct Activation {
- kernel: ActivationMetalKernel,
- config: ActivationConfig,
- input_array_id: ArrayId,
- output_array_id: ArrayId,
-}
-
-impl Activation {
- pub fn new(
- context: &MTLContext,
- data_type: DataType,
- config: ActivationConfig,
- input_array_id: ArrayId,
- output_array_id: ArrayId,
- ) -> Result<Self, MTLError> {
- let kernel = ActivationMetalKernel::new(context, data_type.into())?;
- Ok(Self {
- kernel,
- config,
- input_array_id,
- output_array_id,
- })
- }
-}
-
-impl EncodableBlock<Metal> for Activation {
- fn encode(
- &self,
- state: &mut ForwardPassState<Metal>,
- _parameters: &EncodingParameters<Metal>,
- command_buffer: &Retained<ProtocolObject<dyn MTLCommandBuffer>>,
- ) {
- let encoder = command_buffer.new_compute_command_encoder().expect("Failed to create compute command encoder");
-
- self.encode_with_shared_encoder(state, _parameters, &encoder);
-
- encoder.end_encoding();
- }
-
- fn supports_shared_encoder(&self) -> bool {
- true
- }
-
- fn encode_with_shared_encoder(
- &self,
- state: &mut ForwardPassState<Metal>,
- _parameters: &EncodingParameters<Metal>,
- encoder: &ProtocolObject<dyn MTLComputeCommandEncoder>,
- ) {
- let arrays = state.arrays(&[self.input_array_id, self.output_array_id]);
- let input_array = arrays[0].borrow_mut();
- let output_array = arrays[1].borrow_mut();
-
- let n = input_array.shape().iter().product::<usize>();
-
- let input_buffer = input_array.buffer();
- let output_buffer = output_array.buffer();
-
- let act_type = match self.config {
- ActivationConfig::SiLU {
- ..
- } => 0,
- ActivationConfig::Gelu => 1,
- ActivationConfig::Identity => {
- panic!("Identity activation is not supported for kernel")
- },
- };
-
- self.kernel.encode(input_buffer, output_buffer, n as u32, act_type, encoder);
- }
-}
diff --git a/crates/uzu/src/backends/metal/encodable_block/mod.rs b/crates/uzu/src/backends/metal/encodable_block/mod.rs
index ec2fea55b..02e992b92 100644
--- a/crates/uzu/src/backends/metal/encodable_block/mod.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/mod.rs
@@ -7,9 +7,8 @@
//! kernels to perform operations on `ForwardPassState`.
pub use super::Metal;
-pub use crate::encodable_block::{EncodableBlock, TensorAddSwap, TensorCopy};
+pub use crate::encodable_block::{Activation, EncodableBlock, TensorAddSwap, TensorCopy};
-mod activation;
mod attention;
mod classifier_layer;
mod decoder;
@@ -26,7 +25,6 @@ mod rope;
mod short_conv_mixer;
pub mod transformer_layer;
-pub use activation::Activation;
pub use attention::Attention;
pub use classifier_layer::ClassifierLayer;
pub use decoder::Decoder;
diff --git a/crates/uzu/src/encodable_block/activation.rs b/crates/uzu/src/encodable_block/activation.rs
new file mode 100644
index 000000000..dd09a8315
--- /dev/null
+++ b/crates/uzu/src/encodable_block/activation.rs
@@ -0,0 +1,65 @@
+//! Activation encodable.
+
+use super::{EncodableBlock, EncodingParameters};
+use crate::{
+ DataType,
+ backends::common::{
+ Backend,
+ kernel::{ActivationKernel, Kernels},
+ },
+ config::Activation as ActivationConfig,
+ forward_pass::state::{ArrayId, ForwardPassState},
+};
+
+pub struct Activation<B: Backend> {
+ kernel: <B::Kernels as Kernels>::ActivationKernel,
+ config: ActivationConfig,
+ input_array_id: ArrayId,
+ output_array_id: ArrayId,
+}
+
+impl<B: Backend> Activation<B> {
+ pub fn new(
+ context: &B::Context,
+ data_type: DataType,
+ config: ActivationConfig,
+ input_array_id: ArrayId,
+ output_array_id: ArrayId,
+ ) -> Result<Self, B::Error> {
+ let kernel = <B::Kernels as Kernels>::ActivationKernel::new(context, data_type)?;
+ Ok(Self {
+ kernel,
+ config,
+ input_array_id,
+ output_array_id,
+ })
+ }
+}
+
+impl<B: Backend> EncodableBlock<B> for Activation<B> {
+ fn supports_shared_encoder(&self) -> bool {
+ true
+ }
+
+ fn encode_with_shared_encoder(
+ &self,
+ state: &mut ForwardPassState<B>,
+ _parameters: &EncodingParameters<B>,
+ encoder: &B::ComputeEncoder,
+ ) {
+ let arrays = state.arrays(&[self.input_array_id, self.output_array_id]);
+ let input_array = arrays[0].borrow_mut();
+ let output_array = arrays[1].borrow_mut();
+
+ let n = input_array.shape().iter().product::<usize>();
+ let act_type = match self.config {
+ ActivationConfig::SiLU {
+ ..
+ } => 0,
+ ActivationConfig::Gelu => 1,
+ ActivationConfig::Identity => panic!("Identity activation is not supported for kernel"),
+ };
+
+ self.kernel.encode(input_array.buffer(), output_array.buffer(), n as u32, act_type, encoder);
+ }
+}
diff --git a/crates/uzu/src/encodable_block/mod.rs b/crates/uzu/src/encodable_block/mod.rs
index cc4d2b06b..5e807e483 100644
--- a/crates/uzu/src/encodable_block/mod.rs
+++ b/crates/uzu/src/encodable_block/mod.rs
@@ -3,10 +3,12 @@ use crate::{
forward_pass::state::ForwardPassState,
};
+mod activation;
mod sampling;
mod tensor_add_swap;
mod tensor_copy;
+pub use activation::Activation;
pub use sampling::Sampling;
pub use tensor_add_swap::TensorAddSwap;
pub use tensor_copy::TensorCopy;