Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

dsl buffer offsets (#163)

uuuvn910721ed31b5
diff --git a/crates/uzu/build/common/traitgen.rs b/crates/uzu/build/common/traitgen.rs
index 15452405e..a1291fc4f 100644
--- a/crates/uzu/build/common/traitgen.rs
+++ b/crates/uzu/build/common/traitgen.rs
@@ -2,9 +2,9 @@ use std::{collections::HashMap, env, path::PathBuf};
use anyhow::{Context, bail};
use itertools::Itertools;
-use proc_macro2::TokenStream;
+use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
-use syn::Type;
+use syn::{Lifetime, Type};
use crate::common::{
codegen::write_tokens,
@@ -30,28 +30,34 @@ pub fn traitgen(kernel: &Kernel) -> (TokenStream, TokenStream) {
quote! { #name: #ty }
});
- let args = kernel
+ let (encode_generics, args) = kernel
.arguments
.iter()
.map(|a| {
let name = format_ident!("{}", a.name.as_ref());
- let ty = match &a.ty {
+
+ match &a.ty {
KernelArgumentType::Buffer => {
- quote! { &<Self::Backend as Backend>::NativeBuffer }
+ let buffer_lifetime = Lifetime::new(&format!("'{}", a.name.as_ref()), Span::call_site());
+ (
+ Some(quote! { #buffer_lifetime }),
+ quote! { #name: impl BufferArg<#buffer_lifetime, <Self::Backend as Backend>::NativeBuffer> },
+ )
},
KernelArgumentType::Constant(ty) => {
let ty: Type = syn::parse_str(ty.as_ref()).unwrap();
- quote! { &[#ty] }
+ (None, quote! { #name: &[#ty] })
},
KernelArgumentType::Scalar(ty) => {
let ty: Type = syn::parse_str(ty.as_ref()).unwrap();
- quote! { #ty }
+ (None, quote! { #name: #ty })
},
- };
-
- quote! { #name: #ty }
+ }
})
- .collect::<Vec<_>>();
+ .collect::<(Vec<_>, Vec<_>)>();
+
+ let encode_generics =
+ encode_generics.into_iter().flatten().collect::<Vec<_>>();
let kernel_trait = quote! {
pub trait #trait_name: Sized {
@@ -59,8 +65,8 @@ pub fn traitgen(kernel: &Kernel) -> (TokenStream, TokenStream) {
fn new(context: &<Self::Backend as Backend>::Context #(, #params)*) -> Result<Self, <Self::Backend as Backend>::Error>;
- fn encode(&self, #(#args ,)* encoder: &<Self::Backend as Backend>::ComputeEncoder);
- fn encode_if(&self, #(#args ,)* encoder: &<Self::Backend as Backend>::ComputeEncoder, predicate: Option<&<Self::Backend as Backend>::NativeBuffer>);
+ fn encode<#(#encode_generics, )* 'encoder>(&self, #(#args ,)* encoder: &'encoder <Self::Backend as Backend>::ComputeEncoder);
+ fn encode_if<#(#encode_generics, )* 'encoder, 'predicate>(&self, #(#args ,)* encoder: &'encoder <Self::Backend as Backend>::ComputeEncoder, predicate: Option<impl BufferArg<'predicate, <Self::Backend as Backend>::NativeBuffer>>);
}
};
@@ -103,9 +109,25 @@ pub fn traitgen_all(
}
let kernel_traits = quote! {
- use crate::backends::common::Backend;
+ use crate::backends::common::{Backend, NativeBuffer};
use crate::DataType;
+ pub trait BufferArg<'a, B: NativeBuffer> {
+ fn into_parts(self) -> (&'a B, usize);
+ }
+
+ impl<'a, B: NativeBuffer> BufferArg<'a, B> for &'a B {
+ fn into_parts(self) -> (&'a B, usize) {
+ (self, 0)
+ }
+ }
+
+ impl<'a, B: NativeBuffer> BufferArg<'a, B> for (&'a B, usize) {
+ fn into_parts(self) -> (&'a B, usize) {
+ self
+ }
+ }
+
#(#kernel_traits)*
pub trait Kernels: Sized {
diff --git a/crates/uzu/build/metal/bindgen.rs b/crates/uzu/build/metal/bindgen.rs
index 7b86a977b..9b556c8da 100644
--- a/crates/uzu/build/metal/bindgen.rs
+++ b/crates/uzu/build/metal/bindgen.rs
@@ -2,9 +2,9 @@ use std::iter::repeat_n;
use anyhow::Context;
use itertools::Itertools;
-use proc_macro2::TokenStream;
+use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
-use syn::{LitInt, Type};
+use syn::{Lifetime, LitInt, Type};
use crate::metal::ast::MetalGroupsType;
@@ -113,7 +113,8 @@ pub fn bindgen(
let mut arg_count: usize = 0;
let mut indirect_flag = false;
- let (encode_args_defs, encode_args_sets, encode_args_names): (
+ let (encode_generics, encode_args_defs, encode_args_sets, encode_args_names): (
+ Vec<Option<TokenStream>>,
Vec<TokenStream>,
Vec<TokenStream>,
Vec<TokenStream>,
@@ -125,14 +126,17 @@ pub fn bindgen(
match ka.argument_type().unwrap() {
MetalArgumentType::Buffer => {
- let def = quote! { #arg_name: &Retained<ProtocolObject<dyn MTLBuffer>> };
+ let buffer_lifetime = Lifetime::new(&format!("'{}", ka.name.as_ref()), Span::call_site());
+
+ let def = quote! { #arg_name: impl crate::backends::common::kernel::BufferArg<#buffer_lifetime, Retained<ProtocolObject<dyn MTLBuffer>>> };
let set = quote! {
- compute_encoder.set_buffer(Some(#arg_name), 0, #arg_count);
+ let (__dsl_buffer, __dsl_offset) = #arg_name.into_parts();
+ compute_encoder.set_buffer(Some(__dsl_buffer), __dsl_offset, #arg_count);
};
arg_count += 1;
- Some((def, set, quote! { #arg_name }))
+ Some((Some(quote! { #buffer_lifetime }), def, set, quote! { #arg_name }))
}
MetalArgumentType::Constant((r_type, constant_type)) => {
let arg_dtype: Type = syn::parse_str(&r_type).unwrap();
@@ -150,18 +154,21 @@ pub fn bindgen(
arg_count += 1;
- Some((def, set, quote! { #arg_name }))
+ Some((None, def, set, quote! { #arg_name }))
}
MetalArgumentType::Groups(MetalGroupsType::Indirect) if !indirect_flag => {
indirect_flag = true;
- Some((quote! { __dsl_indirect_dispatch_buffer: &Retained<ProtocolObject<dyn MTLBuffer>> }, quote! {}, quote! { __dsl_indirect_dispatch_buffer }))
+ Some((Some(quote! { '__dsl_indirect_dispatch_buffer }), quote! { __dsl_indirect_dispatch_buffer: impl crate::backends::common::kernel::BufferArg<'__dsl_indirect_dispatch_buffer, Retained<ProtocolObject<dyn MTLBuffer>>> }, quote! {}, quote! { __dsl_indirect_dispatch_buffer }))
}
_ => None,
}
})
.multiunzip();
+ let encode_generics =
+ encode_generics.into_iter().flatten().collect::<Vec<_>>();
+
let (dispatch, elements) = if kernel.has_axis() {
if kernel.has_groups() || kernel.has_threads() {
anyhow::bail!("mixing groups/threads and axis is not supported");
@@ -220,9 +227,10 @@ pub fn bindgen(
(
quote! {
+ let (__dsl_buffer, __dsl_offset) = __dsl_indirect_dispatch_buffer.into_parts();
compute_encoder.dispatch_threadgroups_indirect(
- __dsl_indirect_dispatch_buffer,
- 0,
+ __dsl_buffer,
+ __dsl_offset,
MTLSize::new(#((#threads) as usize, )*),
);
},
@@ -296,21 +304,28 @@ pub fn bindgen(
Ok(Self { pipeline })
}
- fn encode(&self, #(#encode_args_defs, )* compute_encoder: &ProtocolObject<dyn MTLComputeCommandEncoder>) {
+ fn encode<#(#encode_generics, )* 'encoder>(&self, #(#encode_args_defs, )* compute_encoder: &'encoder ProtocolObject<dyn MTLComputeCommandEncoder>) {
#empty_dispatch_guards
compute_encoder.set_compute_pipeline_state(&self.pipeline);
#(#encode_args_sets)*
#dispatch
}
- fn encode_if(&self, #(#encode_args_defs, )* compute_encoder: &ProtocolObject<dyn MTLComputeCommandEncoder>, predicate: Option<&Retained<ProtocolObject<dyn MTLBuffer>>>) {
+ fn encode_if<#(#encode_generics, )* 'encoder, 'predicate>(&self, #(#encode_args_defs, )* compute_encoder: &'encoder ProtocolObject<dyn MTLComputeCommandEncoder>, predicate: Option<impl crate::backends::common::kernel::BufferArg<'predicate, Retained<ProtocolObject<dyn MTLBuffer>>>>) {
#empty_dispatch_guards
- compute_encoder.condition(
- predicate,
- || {
- self.encode(#(#encode_args_names, )* compute_encoder);
- },
- None::<fn()>,
- );
+
+ if let Some(predicate) = predicate {
+ let (__dsl_buffer, __dsl_offset) = predicate.into_parts();
+ compute_encoder.condition(
+ __dsl_buffer,
+ __dsl_offset,
+ || {
+ self.encode(#(#encode_args_names, )* compute_encoder);
+ },
+ None::<fn()>,
+ );
+ } else {
+ self.encode(#(#encode_args_names, )* compute_encoder);
+ }
}
}
};
diff --git a/crates/uzu/src/backends/common/kernel/sampling.rs b/crates/uzu/src/backends/common/kernel/sampling.rs
index 5fc3cffb5..82bef77ba 100644
--- a/crates/uzu/src/backends/common/kernel/sampling.rs
+++ b/crates/uzu/src/backends/common/kernel/sampling.rs
@@ -202,8 +202,7 @@ impl<B: Backend> SamplingKernel<B> {
if let Some(bitmask_buffer) = bitmask_buffer {
self.bitmask.encode(
logits_buffer,
- bitmask_buffer,
- bitmask_offset as u32,
+ (bitmask_buffer, bitmask_offset),
logits_buffer,
batch_size as u32,
vocab_size as u32,
@@ -264,11 +263,13 @@ impl<B: Backend> SamplingKernel<B> {
self.gumbel.encode(
logits_buffer,
- seeds_buffer.ok_or(SamplingError::StochasticWithoutSeed)?,
+ (
+ seeds_buffer.ok_or(SamplingError::StochasticWithoutSeed)?,
+ seeds_offset,
+ ),
logits_buffer,
batch_size as u32,
vocab_size as u32,
- (seeds_offset / size_of::<u64>()) as u32,
compute_encoder,
);
}
diff --git a/crates/uzu/src/backends/metal/encodable_block/linear/quantized.rs b/crates/uzu/src/backends/metal/encodable_block/linear/quantized.rs
index 357700ced..58e96ca3f 100644
--- a/crates/uzu/src/backends/metal/encodable_block/linear/quantized.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/linear/quantized.rs
@@ -272,7 +272,7 @@ impl EncodableBlock<Metal> for QuantizedLinear {
let total_len = batch_size * self.output_dim;
bias_add.encode_if(
output_buffer,
- &bias_buf,
+ bias_buf,
output_buffer,
self.output_dim as u32,
total_len as u32,
@@ -329,7 +329,7 @@ impl EncodableBlock<Metal> for QuantizedLinear {
let total_len = batch_size * self.output_dim;
bias_add.encode_if(
output_buffer,
- &bias_buf,
+ bias_buf,
output_buffer,
self.output_dim as u32,
total_len as u32,
diff --git a/crates/uzu/src/backends/metal/encodable_block/normalization/layer_norm.rs b/crates/uzu/src/backends/metal/encodable_block/normalization/layer_norm.rs
index b4fe040d8..c06b3df89 100644
--- a/crates/uzu/src/backends/metal/encodable_block/normalization/layer_norm.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/normalization/layer_norm.rs
@@ -127,9 +127,9 @@ impl EncodableBlock<Metal> for LayerNorm {
};
self.kernel.encode(
- &input_buffer,
+ input_buffer,
&self.scales_buffer,
- &output_buffer,
+ output_buffer,
batch_size,
model_dim,
self.config.epsilon,
diff --git a/crates/uzu/src/backends/metal/encodable_block/short_conv_mixer.rs b/crates/uzu/src/backends/metal/encodable_block/short_conv_mixer.rs
index 8e53a1bc1..10e253d05 100644
--- a/crates/uzu/src/backends/metal/encodable_block/short_conv_mixer.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/short_conv_mixer.rs
@@ -349,14 +349,15 @@ impl ShortConvMixer {
let state_stride = kernel_size.saturating_sub(1);
let in_proj_stride = self.model_dim * 3;
- let in_proj_offset = in_proj.offset()
- + sampling_start * in_proj_stride * elem_bytes;
- let out_offset = out.offset() + sampling_start * self.model_dim * elem_bytes;
+ let in_proj_offset =
+ in_proj.offset() + sampling_start * in_proj_stride * elem_bytes;
+ let out_offset =
+ out.offset() + sampling_start * self.model_dim * elem_bytes;
let suffix_state_offset = suffix_state.offset()
+ sampling_start * self.model_dim * state_stride * elem_bytes;
let base_state_offset = conv_state.offset();
- let parents_offset = parents.offset()
- + sampling_start * std::mem::size_of::<i32>();
+ let parents_offset =
+ parents.offset() + sampling_start * std::mem::size_of::<i32>();
let conv_weight = self.conv_weight.clone();
let weight_buf = conv_weight.buffer().clone();
diff --git a/crates/uzu/src/backends/metal/encodable_block/tensor_add_swap.rs b/crates/uzu/src/backends/metal/encodable_block/tensor_add_swap.rs
index 4a8abc476..d6e29b2f2 100644
--- a/crates/uzu/src/backends/metal/encodable_block/tensor_add_swap.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/tensor_add_swap.rs
@@ -74,8 +74,8 @@ impl EncodableBlock<Metal> for TensorAddSwap {
let main_mtl_buffer = main_array.buffer();
self.kernel.encode(
- &skip_mtl_buffer,
- &main_mtl_buffer,
+ skip_mtl_buffer,
+ main_mtl_buffer,
length as u32,
encoder,
);
diff --git a/crates/uzu/src/backends/metal/encodable_block/tensor_copy.rs b/crates/uzu/src/backends/metal/encodable_block/tensor_copy.rs
index 916b5a1e6..d2663b814 100644
--- a/crates/uzu/src/backends/metal/encodable_block/tensor_copy.rs
+++ b/crates/uzu/src/backends/metal/encodable_block/tensor_copy.rs
@@ -74,8 +74,8 @@ impl EncodableBlock<Metal> for TensorCopy {
let destination_mtl_buffer = destination_array.buffer();
self.kernel.encode(
- &source_mtl_buffer,
- &destination_mtl_buffer,
+ source_mtl_buffer,
+ destination_mtl_buffer,
length as u32,
encoder,
);
diff --git a/crates/uzu/src/backends/metal/kernel/kv_cache_update/kv_cache_update.metal b/crates/uzu/src/backends/metal/kernel/kv_cache_update/kv_cache_update.metal
index 84564431c..80fe21813 100644
--- a/crates/uzu/src/backends/metal/kernel/kv_cache_update/kv_cache_update.metal
+++ b/crates/uzu/src/backends/metal/kernel/kv_cache_update/kv_cache_update.metal
@@ -27,8 +27,10 @@ KERNEL(KVCacheUpdate) (
// [headIdx: 0..num_heads, tokenIdx: 0..max_sequence_length, channelIdx:
// 0..head_dim]
const uint head_offset = head_idx * max_sequence_length * head_dim;
- const uint sourceIdx = head_offset + swaps[i].source * head_dim + channel_idx;
- const uint destIdx = head_offset + swaps[i].destination * head_dim + channel_idx;
+ const uint sourceIdx =
+ head_offset + swaps[i].source * head_dim + channel_idx;
+ const uint destIdx =
+ head_offset + swaps[i].destination * head_dim + channel_idx;
swap(in_place_keys, sourceIdx, destIdx);
swap(in_place_values, sourceIdx, destIdx);
diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemv/shaders/gemv.metal b/crates/uzu/src/backends/metal/kernel/matmul/gemv/shaders/gemv.metal
index 4946439ca..c71737961 100644
--- a/crates/uzu/src/backends/metal/kernel/matmul/gemv/shaders/gemv.metal
+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemv/shaders/gemv.metal
@@ -69,7 +69,7 @@ struct GEMVKernel {
// * The last thread that partially overlaps with the matrix is shifted
// inwards such that the thread block fits exactly in the matrix
- MTL_CONST short tgp_mem_size = BN > 1 ? BN*(blockM + TM)*BP : 0;
+ MTL_CONST short tgp_mem_size = BN > 1 ? BN*(blockM + TM) * BP : 0;
MTL_CONST bool needs_tgp_reduction = BN > 1;
template <typename U = T>
@@ -236,8 +236,7 @@ struct GEMVKernel {
MTL_PRAGMA_UNROLL
for (int bp = 0; bp < BP; bp++) {
threadgroup AccT* tgp_results =
- tgp_memory + bp * (BN * (blockM + TM)) +
- sgN * (blockM + TM) + bm;
+ tgp_memory + bp * (BN * (blockM + TM)) + sgN * (blockM + TM) + bm;
MTL_PRAGMA_UNROLL
for (int tm = 0; tm < TM; tm++) {
tgp_results[tm] = result[bp][tm];
@@ -276,8 +275,7 @@ struct GEMVKernel {
static_cast<T>(alpha) * static_cast<T>(result[bp][tm]) +
static_cast<T>(beta) * bias[(out_row + tm) * bias_stride];
} else {
- out_row_ptr[out_row + tm] =
- static_cast<T>(result[bp][tm]);
+ out_row_ptr[out_row + tm] = static_cast<T>(result[bp][tm]);
}
}
}
@@ -573,8 +571,8 @@ template <
bp \
) \
instantiate_kernel( \
- "gemv_bp_" #name "_bm" #bm "_bn" #bn "_sm" #sm "_sn" #sn \
- "_tm" #tm "_tn4_nc" #nc "_axpby" #axpby "_bp" #bp, \
+ "gemv_bp_" #name "_bm" #bm "_bn" #bn "_sm" #sm "_sn" #sn "_tm" #tm \
+ "_tn4_nc" #nc "_axpby" #axpby "_bp" #bp, \
gemv_bp, \
itype, \
bm, \
@@ -698,11 +696,32 @@ template <
instantiate_gemv_helper(name, itype, bm, bn, sm, sn, tm, tn, 1, 0) \
instantiate_gemv_helper(name, itype, bm, bn, sm, sn, tm, tn, 1, 1) // clang-format on
-#define instantiate_gemv_bp(name, itype, bm, bn, sm, sn, tm, bp) \
- instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 0, 0, bp) \
- instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 0, 1, bp) \
- instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 1, 0, bp) \
- instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 1, 1, bp) // clang-format on
+#define instantiate_gemv_bp(name, itype, bm, bn, sm, sn, tm, bp) \
+ instantiate_gemv_bp_helper( \
+ name, \
+ itype, \
+ bm, \
+ bn, \
+ sm, \
+ sn, \
+ tm, \
+ 0, \
+ 0, \
+ bp \
+ ) instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 0, 1, bp) \
+ instantiate_gemv_bp_helper(name, itype, bm, bn, sm, sn, tm, 1, 0, bp) \
+ instantiate_gemv_bp_helper( \
+ name, \
+ itype, \
+ bm, \
+ bn, \
+ sm, \
+ sn, \
+ tm, \
+ 1, \
+ 1, \
+ bp \
+ ) // clang-format on
// clang-format off
#define instantiate_gemv_blocks(name, itype) \
@@ -714,14 +733,23 @@ template <
instantiate_gemv(name, itype, 4, 1, 1, 32, 4, 4) \
instantiate_gemv(name, itype, 8, 1, 1, 32, 4, 4) // clang-format on
-#define instantiate_gemv_bp_blocks(name, itype, bp) \
- instantiate_gemv_bp(name, itype, 1, 8, 1, 32, 4, bp) \
- instantiate_gemv_bp(name, itype, 1, 8, 1, 32, 1, bp) \
- instantiate_gemv_bp(name, itype, 1, 1, 8, 4, 4, bp) \
- instantiate_gemv_bp(name, itype, 1, 1, 8, 4, 1, bp) \
- instantiate_gemv_bp(name, itype, 4, 1, 1, 32, 1, bp) \
- instantiate_gemv_bp(name, itype, 4, 1, 1, 32, 4, bp) \
- instantiate_gemv_bp(name, itype, 8, 1, 1, 32, 4, bp) // clang-format on
+#define instantiate_gemv_bp_blocks(name, itype, bp) \
+ instantiate_gemv_bp(name, itype, 1, 8, 1, 32, 4, bp) \
+ instantiate_gemv_bp(name, itype, 1, 8, 1, 32, 1, bp) \
+ instantiate_gemv_bp(name, itype, 1, 1, 8, 4, 4, bp) \
+ instantiate_gemv_bp(name, itype, 1, 1, 8, 4, 1, bp) \
+ instantiate_gemv_bp(name, itype, 4, 1, 1, 32, 1, bp) \
+ instantiate_gemv_bp(name, itype, 4, 1, 1, 32, 4, bp) \
+ instantiate_gemv_bp( \
+ name, \
+ itype, \
+ 8, \
+ 1, \
+ 1, \
+ 32, \
+ 4, \
+ bp \
+ ) // clang-format on
instantiate_gemv_blocks(float32, float);
instantiate_gemv_blocks(float16, half);
@@ -734,7 +762,6 @@ instantiate_gemv_bp_blocks(float32, float, 4);
instantiate_gemv_bp_blocks(float16, half, 4);
instantiate_gemv_bp_blocks(bfloat16, bfloat16_t, 4);
-
template <
typename T,
const int BM, /* Threadgroup rows (in simdgroups) */
diff --git a/crates/uzu/src/backends/metal/kernel/sampling/bitmask.metal b/crates/uzu/src/backends/metal/kernel/sampling/bitmask.metal
index 0b1b017d0..df0c27902 100644
--- a/crates/uzu/src/backends/metal/kernel/sampling/bitmask.metal
+++ b/crates/uzu/src/backends/metal/kernel/sampling/bitmask.metal
@@ -10,7 +10,6 @@ VARIANTS(T, float, half, bfloat)
KERNEL(Bitmask) (
device const T* logits,
device const uint32_t* bitmask,
- constant uint& bitmask_offset,
device T* processed_logits,
constant uint& batch_size,
constant uint& vocab_size,
@@ -18,7 +17,6 @@ KERNEL(Bitmask) (
uint batch_idx GROUPS(batch_size),
uint thread_idx THREADS(BLOCK_SIZE)
) {
- bitmask += bitmask_offset / sizeof(uint32_t);
uint bitmask_size = (vocab_size + (BITS_IN_U32 - 1)) / BITS_IN_U32;
uint base_idx =
batch_idx * vocab_size + group_idx * BLOCK_SIZE * GRAIN_SIZE + thread_idx;
diff --git a/crates/uzu/src/backends/metal/kernel/sampling/gumbel.metal b/crates/uzu/src/backends/metal/kernel/sampling/gumbel.metal
index f298730dd..b8d4f2254 100644
--- a/crates/uzu/src/backends/metal/kernel/sampling/gumbel.metal
+++ b/crates/uzu/src/backends/metal/kernel/sampling/gumbel.metal
@@ -14,7 +14,6 @@ KERNEL(Gumbel) (
device T* processed_logits,
constant uint& batch_size,
constant uint& vocab_size,
- constant uint& seeds_offset,
uint group_idx GROUPS(vocab_size.div_ceil(BLOCK_SIZE * GRAIN_SIZE)),
uint batch_idx GROUPS(batch_size),
uint thread_idx THREADS(BLOCK_SIZE)
@@ -26,7 +25,7 @@ KERNEL(Gumbel) (
uint grain_offset = batch_start + grain_offset_in_batch;
- uint64_t rng_seed = batch_seeds[seeds_offset + batch_idx];
+ uint64_t rng_seed = batch_seeds[batch_idx];
uint64_t rng_offset = (group_idx * BLOCK_SIZE + thread_idx) *
(GRAIN_SIZE + WORDS_PER_OFFSET - 1) / WORDS_PER_OFFSET;
PhiloxState rng;
diff --git a/crates/uzu/src/backends/metal/kernel/short_conv/mod.rs b/crates/uzu/src/backends/metal/kernel/short_conv/mod.rs
index bf42ba391..8f60fdfb6 100644
--- a/crates/uzu/src/backends/metal/kernel/short_conv/mod.rs
+++ b/crates/uzu/src/backends/metal/kernel/short_conv/mod.rs
@@ -30,12 +30,18 @@ fn make_function_constants(
pub struct ShortConvKernel {
pack_pipeline: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- prefill_pipeline_no_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- prefill_pipeline_with_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- decode_pipeline_no_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- decode_pipeline_with_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- trie_pipeline_no_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
- trie_pipeline_with_bias: Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ prefill_pipeline_no_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ prefill_pipeline_with_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ decode_pipeline_no_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ decode_pipeline_with_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ trie_pipeline_no_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
+ trie_pipeline_with_bias:
+ Retained<ProtocolObject<dyn MTLComputePipelineState>>,
}
pub struct ShortConvPackArguments<'a> {
@@ -355,11 +361,7 @@ impl ShortConvKernel {
args.base_state_offset,
3,
);
- compute_encoder.set_buffer(
- Some(args.parents),
- args.parents_offset,
- 4,
- );
+ compute_encoder.set_buffer(Some(args.parents), args.parents_offset, 4);
compute_encoder.set_buffer(Some(args.out), args.out_offset, 5);
compute_encoder.set_buffer(
Some(args.suffix_state),
diff --git a/crates/uzu/src/backends/metal/kernel/short_conv/short_conv.metal b/crates/uzu/src/backends/metal/kernel/short_conv/short_conv.metal
index 6c5ee6878..13d8b55b8 100644
--- a/crates/uzu/src/backends/metal/kernel/short_conv/short_conv.metal
+++ b/crates/uzu/src/backends/metal/kernel/short_conv/short_conv.metal
@@ -211,9 +211,11 @@ void short_conv_trie_kernel(
// Select parent state (root uses base_state)
const int parent = parents[node];
- const device T* parent_state = (parent < 0)
- ? (base_state + base_state_offset)
- : (suffix_state + (size_t(parent) * model_dim + size_t(channel_idx)) * state_stride);
+ const device T* parent_state =
+ (parent < 0) ? (base_state + base_state_offset)
+ : (suffix_state +
+ (size_t(parent) * model_dim + size_t(channel_idx)) *
+ state_stride);
float acc = 0.0f;
if (has_bias) {
@@ -236,7 +238,8 @@ void short_conv_trie_kernel(
// Write post-state for this node
if (tap_count > 0) {
device T* dst_state =
- suffix_state + (node * model_dim + size_t(channel_idx)) * state_stride;
+ suffix_state +
+ (node * model_dim + size_t(channel_idx)) * state_stride;
for (int tap = 0; tap < tap_count - 1; ++tap) {
dst_state[size_t(tap)] = parent_state[size_t(tap + 1)];
}
diff --git a/crates/uzu/src/backends/metal/metal_extensions/compute_command_encoder_extensions_conditional.rs b/crates/uzu/src/backends/metal/metal_extensions/compute_command_encoder_extensions_conditional.rs
index e3035cb59..d68a37b57 100644
--- a/crates/uzu/src/backends/metal/metal_extensions/compute_command_encoder_extensions_conditional.rs
+++ b/crates/uzu/src/backends/metal/metal_extensions/compute_command_encoder_extensions_conditional.rs
@@ -25,7 +25,8 @@ trait ComputeEncoderRawConditional {
pub trait ComputeEncoderConditional {
fn condition<IfBlock, ElseBlock>(
&self,
- predicate: Option<&Retained<ProtocolObject<dyn MTLBuffer>>>,
+ predicate: &Retained<ProtocolObject<dyn MTLBuffer>>,
+ offset: usize,
if_block: IfBlock,
else_block: Option<ElseBlock>,
) where
@@ -70,29 +71,20 @@ where
{
fn condition<IfBlock, ElseBlock>(
&self,
- predicate: Option<&Retained<ProtocolObject<dyn MTLBuffer>>>,
+ predicate: &Retained<ProtocolObject<dyn MTLBuffer>>,
+ offset: usize,
if_block: IfBlock,
else_block: Option<ElseBlock>,
) where
IfBlock: FnOnce(),
ElseBlock: FnOnce(),
{
- match (predicate, else_block) {
- (Some(p), Some(else_fn)) => {
- self.encode_start_if(p, 0, MTLCompareFunction::Equal, 0);
- if_block();
- self.encode_start_else();
- else_fn();
- self.encode_end_if();
- },
- (Some(p), None) => {
- self.encode_start_if(p, 0, MTLCompareFunction::Equal, 0);
- if_block();
- self.encode_end_if();
- },
- (None, _) => {
- if_block();
- },
+ self.encode_start_if(predicate, offset, MTLCompareFunction::Equal, 0);
+ if_block();
+ if let Some(else_block) = else_block {
+ self.encode_start_else();
+ else_block();
}
+ self.encode_end_if();
}
}
diff --git a/crates/uzu/src/forward_pass/short_conv_layer.rs b/crates/uzu/src/forward_pass/short_conv_layer.rs
index d6fe83e6a..b01cfeea4 100644
--- a/crates/uzu/src/forward_pass/short_conv_layer.rs
+++ b/crates/uzu/src/forward_pass/short_conv_layer.rs
@@ -95,9 +95,8 @@ impl<B: Backend> ShortConvLayer<B> {
);
let elem_bytes = conv.data_type().size_in_bytes();
- let bytes_per_token = model_dim
- .saturating_mul(state_stride)
- .saturating_mul(elem_bytes);
+ let bytes_per_token =
+ model_dim.saturating_mul(state_stride).saturating_mul(elem_bytes);
let src_start = commit_index.saturating_mul(bytes_per_token);
let src_end = src_start.saturating_add(bytes_per_token);
diff --git a/crates/uzu/tests/sampling_test.rs b/crates/uzu/tests/sampling_test.rs
index 5d34cb4e5..2341f5594 100644
--- a/crates/uzu/tests/sampling_test.rs
+++ b/crates/uzu/tests/sampling_test.rs
@@ -1743,7 +1743,6 @@ fn test_gumbel_gpu_cpu_match() {
&gumbel_logits_buffer,
BATCH as u32,
VOCAB as u32,
- 0,
&compute_encoder,
);
compute_encoder.end_encoding();