Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Support logit_soft_cap (#466)

CCYeh13668d884906
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/logit_soft_cap.rs b/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/logit_soft_cap.rs
new file mode 100644
index 000000000..1810115b2
--- /dev/null
+++ b/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/logit_soft_cap.rs
@@ -0,0 +1,21 @@
+use half::bf16;
+use num_traits::Float;
+use proc_macros::kernel;
+
+use crate::ArrayElement;
+
+#[kernel(LogitSoftCap)]
+#[variants(T, f32, bf16)]
+pub fn logit_soft_cap<T: ArrayElement + Float>(
+ logits: *mut T,
+ length: u32,
+ soft_cap: f32,
+) {
+ let length = length as usize;
+ unsafe {
+ for position in 0..length {
+ let value = (*logits.add(position)).to_f32().unwrap();
+ *logits.add(position) = T::from((value / soft_cap).tanh() * soft_cap).unwrap();
+ }
+ }
+}
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/mod.rs b/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/mod.rs
new file mode 100644
index 000000000..f4f071829
--- /dev/null
+++ b/crates/backend-uzu/src/backends/cpu/kernel/logit_soft_cap/mod.rs
@@ -0,0 +1 @@
+pub mod logit_soft_cap;
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/mod.rs b/crates/backend-uzu/src/backends/cpu/kernel/mod.rs
index 0658c20c0..e3f7f5b79 100644
--- a/crates/backend-uzu/src/backends/cpu/kernel/mod.rs
+++ b/crates/backend-uzu/src/backends/cpu/kernel/mod.rs
@@ -7,6 +7,7 @@ mod embedding;
mod hadamard_transform;
mod kv_cache_update;
mod layer_norm;
+mod logit_soft_cap;
mod matmul;
mod mlp;
diff --git a/crates/backend-uzu/src/backends/metal/kernel/logit_soft_cap/logit_soft_cap.metal b/crates/backend-uzu/src/backends/metal/kernel/logit_soft_cap/logit_soft_cap.metal
new file mode 100644
index 000000000..e3e57cb75
--- /dev/null
+++ b/crates/backend-uzu/src/backends/metal/kernel/logit_soft_cap/logit_soft_cap.metal
@@ -0,0 +1,14 @@
+#include <metal_stdlib>
+#include "../common/dsl.h"
+
+template <typename T>
+VARIANTS(T, float, bfloat)
+PUBLIC KERNEL(LogitSoftCap)(
+ device T* logits,
+ constant uint& length,
+ constant float& soft_cap,
+ const uint position AXIS(length, 256)
+) {
+ const float value = float(logits[position]);
+ logits[position] = T(fast::tanh(value / soft_cap) * soft_cap);
+}
diff --git a/crates/backend-uzu/src/encodable_block/embedding.rs b/crates/backend-uzu/src/encodable_block/embedding.rs
index 405cf7728..970898e0f 100644
--- a/crates/backend-uzu/src/encodable_block/embedding.rs
+++ b/crates/backend-uzu/src/encodable_block/embedding.rs
@@ -9,7 +9,7 @@ use crate::{
Allocation, Backend, Encoder, Kernels,
gpu_types::{QuantizationMethod, QuantizationMode},
kernel::{
- FullPrecisionEmbeddingLookupKernel, ManualKernels, QuantizedEmbeddingLookupKernel,
+ FullPrecisionEmbeddingLookupKernel, LogitSoftCapKernel, ManualKernels, QuantizedEmbeddingLookupKernel,
matmul::{MatmulArguments, MatmulB, MatmulDOps, MatmulKernel, MatmulQuantCombo},
},
},
@@ -104,10 +104,16 @@ pub struct Embedding<B: Backend> {
tying: EmbeddingTying<B>,
input_scale: f32,
data_type: DataType,
+ logit_soft_cap: Option<LogitSoftCap<B>>,
vocab_size: u32,
model_dim: u32,
}
+struct LogitSoftCap<B: Backend> {
+ value: f32,
+ kernel: <B::Kernels as Kernels>::LogitSoftCapKernel,
+}
+
impl<B: Backend> Embedding<B> {
pub fn new(
context: &B::Context,
@@ -403,15 +409,23 @@ impl<B: Backend> Embedding<B> {
};
let input_scale = config.input_scale().unwrap_or(1.0);
- if let Some(logit_soft_cap) = config.logit_soft_cap() {
- return Err(EmbeddingError::UnsupportedConfiguration(format!("logit_soft_cap={logit_soft_cap:?}")));
- }
+ let logit_soft_cap = if let Some(value) = *config.logit_soft_cap() {
+ let kernel = <B::Kernels as Kernels>::LogitSoftCapKernel::new(context, model_shape.data_type)
+ .map_err(EmbeddingError::BackendError)?;
+ Some(LogitSoftCap {
+ value,
+ kernel,
+ })
+ } else {
+ None
+ };
Ok((
Self {
tying,
input_scale,
data_type: model_shape.data_type,
+ logit_soft_cap,
vocab_size,
model_dim,
},
@@ -626,6 +640,15 @@ impl<B: Backend> Embedding<B> {
},
};
+ if let Some(logit_soft_cap) = &self.logit_soft_cap {
+ logit_soft_cap.kernel.encode(
+ &mut output_allocation,
+ (batch_dim * self.vocab_size as usize) as u32,
+ logit_soft_cap.value,
+ encoder,
+ );
+ }
+
Ok(output_allocation)
}
}
diff --git a/crates/backend-uzu/tests/unit/kernel/logit_soft_cap_test.rs b/crates/backend-uzu/tests/unit/kernel/logit_soft_cap_test.rs
new file mode 100644
index 000000000..181dcad23
--- /dev/null
+++ b/crates/backend-uzu/tests/unit/kernel/logit_soft_cap_test.rs
@@ -0,0 +1,65 @@
+use std::fmt::Display;
+
+use backend_uzu::{
+ ArrayElement, DataType,
+ backends::{
+ common::{Backend, Context, Encoder, Kernels, kernel::LogitSoftCapKernel},
+ cpu::Cpu,
+ },
+};
+use half::bf16;
+use num_traits::Float;
+
+use crate::{
+ common::{
+ assert::assert_eq_float,
+ helpers::{alloc_allocation_with_data, allocation_to_vec},
+ },
+ uzu_test,
+};
+
+fn get_output<T: ArrayElement + Float, B: Backend>(
+ logits: &[T],
+ soft_cap: f32,
+) -> Vec<T> {
+ let context = B::Context::new().expect("Failed to create Context");
+ let kernel = <<B as Backend>::Kernels as Kernels>::LogitSoftCapKernel::new(&context, T::data_type())
+ .expect("Failed to create LogitSoftCapKernel");
+
+ let mut logits_allocation = alloc_allocation_with_data::<B, T>(&context, logits);
+ let mut encoder = Encoder::new(context.as_ref()).expect("Failed to create encoder");
+ kernel.encode(&mut logits_allocation, logits.len() as u32, soft_cap, &mut encoder);
+ encoder.end_encoding().submit().wait_until_completed().unwrap();
+
+ allocation_to_vec::<B, T>(&logits_allocation)
+}
+
+fn test_logit_soft_cap<T: ArrayElement + Float + Display>() {
+ let logits = [-120.0f32, -30.0, -3.5, 0.0, 2.0, 30.0, 120.0]
+ .into_iter()
+ .map(|value| T::from(value).unwrap())
+ .collect::<Box<[_]>>();
+ let soft_cap = 30.0;
+ let expected = get_output::<T, Cpu>(&logits, soft_cap);
+ let epsilon = if matches!(T::data_type(), DataType::BF16) {
+ 0.05
+ } else {
+ 1e-5
+ };
+
+ for_each_non_cpu_backend!(|B| {
+ let actual = get_output::<T, B>(&logits, soft_cap);
+ let message = format!("LogitSoftCap failed with backend={}", std::any::type_name::<B>());
+ assert_eq_float::<T>(&expected, &actual, epsilon, &message);
+ });
+}
+
+#[uzu_test]
+fn test_logit_soft_cap_f32() {
+ test_logit_soft_cap::<f32>();
+}
+
+#[uzu_test]
+fn test_logit_soft_cap_bf16() {
+ test_logit_soft_cap::<bf16>();
+}
diff --git a/crates/backend-uzu/tests/unit/kernel/mod.rs b/crates/backend-uzu/tests/unit/kernel/mod.rs
index 0135b5223..6495f7bd1 100644
--- a/crates/backend-uzu/tests/unit/kernel/mod.rs
+++ b/crates/backend-uzu/tests/unit/kernel/mod.rs
@@ -7,6 +7,7 @@ mod embedding;
mod hadamard_transform_test;
mod kv_cache_update_test;
mod layer_norm_test;
+mod logit_soft_cap_test;
mod matmul;
mod moe;
mod pooling;