Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

update bech info

Eugene Bokhana4fdc27302d8
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs b/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
index c4934b8ee..2e887ca30 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/dispatch_descriptor.rs
@@ -4,6 +4,23 @@ use crate::{
backends::metal::{MTLContext, MTLError},
};
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum MatmulKernelVariant {
+ Gemv,
+ SplitK,
+ Gemm,
+}
+
+impl MatmulKernelVariant {
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ MatmulKernelVariant::Gemv => "GEMV",
+ MatmulKernelVariant::SplitK => "Split-K",
+ MatmulKernelVariant::Gemm => "GEMM",
+ }
+ }
+}
+
#[derive(Debug, Clone)]
pub(crate) enum MatmulDispatchDescriptor {
Gemv(gemv::DispatchDescriptor),
@@ -11,6 +28,16 @@ pub(crate) enum MatmulDispatchDescriptor {
Gemm(gemm::DispatchDescriptor),
}
+impl MatmulDispatchDescriptor {
+ pub fn variant(&self) -> MatmulKernelVariant {
+ match self {
+ MatmulDispatchDescriptor::Gemv(_) => MatmulKernelVariant::Gemv,
+ MatmulDispatchDescriptor::SplitK(_) => MatmulKernelVariant::SplitK,
+ MatmulDispatchDescriptor::Gemm(_) => MatmulKernelVariant::Gemm,
+ }
+ }
+}
+
pub(crate) fn choose_dispatch_descriptor(
context: &MTLContext,
data_type: DataType,
@@ -28,7 +55,16 @@ pub(crate) fn choose_dispatch_descriptor(
return Ok(MatmulDispatchDescriptor::SplitK(descriptor));
}
- Ok(MatmulDispatchDescriptor::Gemm(
- gemm::DispatchDescriptor::new(context, data_type, arguments)?,
- ))
+ Ok(MatmulDispatchDescriptor::Gemm(gemm::DispatchDescriptor::new(
+ context, data_type, arguments,
+ )?))
+}
+
+pub fn determine_kernel_variant(
+ context: &MTLContext,
+ data_type: DataType,
+ arguments: &MatmulArguments,
+) -> Result<MatmulKernelVariant, MTLError> {
+ choose_dispatch_descriptor(context, data_type, arguments)
+ .map(|d| d.variant())
}
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/mod.rs b/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
index 581cc089d..fcf105d50 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
+++ b/crates/uzu/src/backends/metal/kernel/matmul/mod.rs
@@ -6,6 +6,7 @@ mod kernel;
mod split_k;
pub use common::MatmulArguments;
+pub use dispatch_descriptor::{MatmulKernelVariant, determine_kernel_variant};
pub use gemv::GemvKernel;
pub use kernel::MatmulKernel;
pub use split_k::SplitKGemm;
diff --git a/crates/uzu/src/backends/metal/kernel/mlp_fused/dispatch_descriptor.rs b/crates/uzu/src/backends/metal/kernel/mlp_fused/dispatch_descriptor.rs
index 43fed2155..fca33bf46 100644
--- a/crates/uzu/src/backends/metal/kernel/mlp_fused/dispatch_descriptor.rs
+++ b/crates/uzu/src/backends/metal/kernel/mlp_fused/dispatch_descriptor.rs
@@ -4,6 +4,23 @@ use crate::{
backends::metal::{MTLContext, MTLError},
};
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum MlpFusedKernelVariant {
+ Gemv,
+ SplitK,
+ Gemm,
+}
+
+impl MlpFusedKernelVariant {
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ MlpFusedKernelVariant::Gemv => "GEMV",
+ MlpFusedKernelVariant::SplitK => "Split-K",
+ MlpFusedKernelVariant::Gemm => "GEMM",
+ }
+ }
+}
+
#[derive(Debug, Clone)]
pub(crate) enum MlpFusedDispatchDescriptor {
Gemv(gemv::DispatchDescriptor),
@@ -11,6 +28,18 @@ pub(crate) enum MlpFusedDispatchDescriptor {
Gemm(gemm::DispatchDescriptor),
}
+impl MlpFusedDispatchDescriptor {
+ pub fn variant(&self) -> MlpFusedKernelVariant {
+ match self {
+ MlpFusedDispatchDescriptor::Gemv(_) => MlpFusedKernelVariant::Gemv,
+ MlpFusedDispatchDescriptor::SplitK(_) => {
+ MlpFusedKernelVariant::SplitK
+ },
+ MlpFusedDispatchDescriptor::Gemm(_) => MlpFusedKernelVariant::Gemm,
+ }
+ }
+}
+
pub(crate) fn choose_dispatch_descriptor(
context: &MTLContext,
data_type: DataType,
@@ -38,3 +67,18 @@ pub(crate) fn choose_dispatch_descriptor(
arguments,
)?))
}
+
+pub fn determine_kernel_variant(
+ context: &MTLContext,
+ data_type: DataType,
+ weights_transposed: bool,
+ arguments: &MlpFusedArguments,
+) -> Result<MlpFusedKernelVariant, MTLError> {
+ choose_dispatch_descriptor(
+ context,
+ data_type,
+ weights_transposed,
+ arguments,
+ )
+ .map(|d| d.variant())
+}
diff --git a/crates/uzu/src/backends/metal/kernel/mlp_fused/mod.rs b/crates/uzu/src/backends/metal/kernel/mlp_fused/mod.rs
index 53118d819..e668ca697 100644
--- a/crates/uzu/src/backends/metal/kernel/mlp_fused/mod.rs
+++ b/crates/uzu/src/backends/metal/kernel/mlp_fused/mod.rs
@@ -6,6 +6,9 @@ mod kernel;
mod split_k;
pub use common::MlpFusedArguments;
+pub use dispatch_descriptor::{
+ MlpFusedKernelVariant, determine_kernel_variant,
+};
pub use gemm::GemmKernel;
pub use gemv::GemvKernel;
pub use kernel::MlpFusedKernel;
diff --git a/crates/uzu/tests/mlp_fused_performance_test.rs b/crates/uzu/tests/mlp_fused_performance_test.rs
index efa02eb14..68b5c24a1 100644
--- a/crates/uzu/tests/mlp_fused_performance_test.rs
+++ b/crates/uzu/tests/mlp_fused_performance_test.rs
@@ -11,9 +11,15 @@ use uzu::{
backends::metal::{
MTLContext,
kernel::{
- matmul::{MatmulArguments, MatmulKernel},
+ matmul::{
+ MatmulArguments, MatmulKernel,
+ determine_kernel_variant as determine_matmul_variant,
+ },
mlp::MlpActivationType,
- mlp_fused::{MlpFusedArguments, MlpFusedKernel},
+ mlp_fused::{
+ MlpFusedArguments, MlpFusedKernel,
+ determine_kernel_variant as determine_mlp_fused_variant,
+ },
},
},
};
@@ -28,12 +34,16 @@ struct BenchmarkResult {
fused_time_microseconds: f64,
unfused_time_microseconds: f64,
speedup: f64,
+ fused_kernel_name: &'static str,
+ unfused_kernel_name: &'static str,
}
impl BenchmarkResult {
fn new(
fused_duration: Duration,
unfused_duration: Duration,
+ fused_kernel_name: &'static str,
+ unfused_kernel_name: &'static str,
) -> Self {
let fused_microseconds = fused_duration.as_secs_f64() * 1_000_000.0;
let unfused_microseconds = unfused_duration.as_secs_f64() * 1_000_000.0;
@@ -41,6 +51,8 @@ impl BenchmarkResult {
fused_time_microseconds: fused_microseconds,
unfused_time_microseconds: unfused_microseconds,
speedup: unfused_microseconds / fused_microseconds,
+ fused_kernel_name,
+ unfused_kernel_name,
}
}
}
@@ -94,9 +106,7 @@ fn benchmark_gemv_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- fused_kernel
- .encode(context, &encoder, &fused_args)
- .unwrap();
+ fused_kernel.encode(context, &encoder, &fused_args).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -108,9 +118,7 @@ fn benchmark_gemv_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- fused_kernel
- .encode(context, &encoder, &fused_args)
- .unwrap();
+ fused_kernel.encode(context, &encoder, &fused_args).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -137,14 +145,22 @@ fn benchmark_gemv_fused(
transpose_b: true,
};
+ let fused_kernel_name =
+ determine_mlp_fused_variant(context, DataType::F16, true, &fused_args)
+ .map(|v| v.as_str())
+ .unwrap_or("?");
+
+ let unfused_kernel_name =
+ determine_matmul_variant(context, DataType::F16, &matmul_args)
+ .map(|v| v.as_str())
+ .unwrap_or("?");
+
// Warmup unfused
for _ in 0..warmup_count {
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- matmul_kernel
- .encode(context, &encoder, matmul_args.clone())
- .unwrap();
+ matmul_kernel.encode(context, &encoder, matmul_args.clone()).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -156,16 +172,19 @@ fn benchmark_gemv_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- matmul_kernel
- .encode(context, &encoder, matmul_args.clone())
- .unwrap();
+ matmul_kernel.encode(context, &encoder, matmul_args.clone()).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
}
let unfused_duration = unfused_start.elapsed() / iteration_count as u32;
- BenchmarkResult::new(fused_duration, unfused_duration)
+ BenchmarkResult::new(
+ fused_duration,
+ unfused_duration,
+ fused_kernel_name,
+ unfused_kernel_name,
+ )
}
fn benchmark_gemm_fused(
@@ -218,9 +237,7 @@ fn benchmark_gemm_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- fused_kernel
- .encode(context, &encoder, &fused_args)
- .unwrap();
+ fused_kernel.encode(context, &encoder, &fused_args).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -231,9 +248,7 @@ fn benchmark_gemm_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- fused_kernel
- .encode(context, &encoder, &fused_args)
- .unwrap();
+ fused_kernel.encode(context, &encoder, &fused_args).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -260,14 +275,22 @@ fn benchmark_gemm_fused(
transpose_b: true,
};
+ let fused_kernel_name =
+ determine_mlp_fused_variant(context, DataType::F16, true, &fused_args)
+ .map(|v| v.as_str())
+ .unwrap_or("?");
+
+ let unfused_kernel_name =
+ determine_matmul_variant(context, DataType::F16, &matmul_args)
+ .map(|v| v.as_str())
+ .unwrap_or("?");
+
// Warmup unfused
for _ in 0..warmup_count {
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- matmul_kernel
- .encode(context, &encoder, matmul_args.clone())
- .unwrap();
+ matmul_kernel.encode(context, &encoder, matmul_args.clone()).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
@@ -278,16 +301,19 @@ fn benchmark_gemm_fused(
let command_buffer =
context.command_queue.new_command_buffer().to_owned();
let encoder = command_buffer.new_compute_command_encoder();
- matmul_kernel
- .encode(context, &encoder, matmul_args.clone())
- .unwrap();
+ matmul_kernel.encode(context, &encoder, matmul_args.clone()).unwrap();
encoder.end_encoding();
command_buffer.commit();
command_buffer.wait_until_completed();
}
let unfused_duration = unfused_start.elapsed() / iteration_count as u32;
- BenchmarkResult::new(fused_duration, unfused_duration)
+ BenchmarkResult::new(
+ fused_duration,
+ unfused_duration,
+ fused_kernel_name,
+ unfused_kernel_name,
+ )
}
/// MLP shapes from real model configs
@@ -336,15 +362,17 @@ fn mlp_fused_performance_benchmark() {
let warmup_count = 10;
println!(
- "{:>12} {:>12} {:>12} {:>12} {:>12} {:>10}",
- "batch_size",
- "input_dim",
- "hidden_dim",
+ "{:>10} {:>10} {:>10} {:>10} {:>12} {:>10} {:>12} {:>10}",
+ "batch",
+ "input",
+ "hidden",
+ "Fused",
"Fused (µs)",
+ "Unfused",
"Unfused (µs)",
"Speedup"
);
- println!("{}", "-".repeat(74));
+ println!("{}", "-".repeat(98));
for &batch_size in BATCH_SIZES {
for &(input_dimension, hidden_dimension) in MLP_SHAPES {
@@ -368,11 +396,13 @@ fn mlp_fused_performance_benchmark() {
};
let speedup_string = format!("{:.2}x", result.speedup);
println!(
- "{:>12} {:>12} {:>12} {:>12.1} {:>12.1} {:>10}",
+ "{:>10} {:>10} {:>10} {:>10} {:>12.1} {:>10} {:>12.1} {:>10}",
batch_size,
input_dimension,
hidden_dimension,
+ result.fused_kernel_name,
result.fused_time_microseconds,
+ result.unfused_kernel_name,
result.unfused_time_microseconds,
speedup_string
);