Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Apply GPU constant generation to weaver (#633)

CCYehc7e754769d03
diff --git a/crates/backend-uzu/src/backends/common/gpu_types/mod.rs b/crates/backend-uzu/src/backends/common/gpu_types/mod.rs
index 4be07fa23..ddfd65d65 100644
--- a/crates/backend-uzu/src/backends/common/gpu_types/mod.rs
+++ b/crates/backend-uzu/src/backends/common/gpu_types/mod.rs
@@ -14,6 +14,7 @@ pub mod quantization;
pub mod quantization_method;
pub mod ring;
pub mod trie;
+pub mod weaver;
pub use activation_type::*;
pub use argmax::*;
diff --git a/crates/backend-uzu/src/backends/common/gpu_types/weaver.rs b/crates/backend-uzu/src/backends/common/gpu_types/weaver.rs
new file mode 100644
index 000000000..e6b058188
--- /dev/null
+++ b/crates/backend-uzu/src/backends/common/gpu_types/weaver.rs
@@ -0,0 +1,32 @@
+pub const CANDIDATES_MAX: usize = 512;
+pub const TOP_CHILDREN_THREADS: usize = 256;
+pub const TOP_CHILDREN_SIMDGROUPS: usize = 8;
+
+pub const FRONTIER_LANE_TOKEN: usize = 0;
+pub const FRONTIER_LANE_PARENT: usize = 1;
+pub const FRONTIER_LANE_DEPTH: usize = 2;
+pub const FRONTIER_LANE_CUM: usize = 3;
+pub const FRONTIER_LANE_LOGPROB: usize = 4;
+pub const FRONTIER_LANE_KEY: usize = 5;
+pub const FRONTIER_LANE_ACTIVE: usize = 6;
+pub const FRONTIER_LANE_COUNT: usize = FRONTIER_LANE_ACTIVE + 1;
+
+pub const TREE_LANE_TOKEN: usize = 0;
+pub const TREE_LANE_PARENT: usize = 1;
+pub const TREE_LANE_DEPTH: usize = 2;
+pub const TREE_LANE_CUM: usize = 3;
+pub const TREE_LANE_LOGPROB: usize = 4;
+pub const TREE_LANE_MASK: usize = 5;
+pub const TREE_LANE_COUNT: usize = TREE_LANE_MASK + 1;
+
+pub const METADATA_LANE_DEPTH: usize = 0;
+pub const METADATA_LANE_ANCESTOR_COUNT: usize = 1;
+pub const METADATA_LANE_NODE_INDEX: usize = 2;
+pub const METADATA_LANE_COUNT: usize = METADATA_LANE_NODE_INDEX + 1;
+
+pub const FRONTIER_NO_WINNER: u32 = !0;
+pub const FRONTIER_SELECT_THREADS: usize = 256;
+pub const FRONTIER_SELECT_SIMDGROUPS: usize = 8;
+pub const FRONTIER_ENTRIES_PER_THREAD: usize = 8;
+pub const FRONTIER_MAX_SLOTS: usize = 2048;
+pub const FRONTIER_MAX_WIDTH: usize = 32;
diff --git a/crates/backend-uzu/src/backends/common/kernel/mod.rs b/crates/backend-uzu/src/backends/common/kernel/mod.rs
index d23a882ec..b96024331 100644
--- a/crates/backend-uzu/src/backends/common/kernel/mod.rs
+++ b/crates/backend-uzu/src/backends/common/kernel/mod.rs
@@ -5,7 +5,6 @@ pub mod delta_net_chunked_prefill;
pub mod delta_net_tree_verify;
pub mod matmul;
pub mod radix_top_k_small;
-pub(crate) mod weaver;
include!(concat!(env!("OUT_DIR"), "/traits.rs"));
diff --git a/crates/backend-uzu/src/backends/common/kernel/weaver/mod.rs b/crates/backend-uzu/src/backends/common/kernel/weaver/mod.rs
deleted file mode 100644
index 480024288..000000000
--- a/crates/backend-uzu/src/backends/common/kernel/weaver/mod.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-pub(crate) const MAX_CANDIDATES: usize = 512;
-
-pub(crate) const FRONTIER_LANE_TOKEN: usize = 0;
-pub(crate) const FRONTIER_LANE_PARENT: usize = 1;
-pub(crate) const FRONTIER_LANE_DEPTH: usize = 2;
-pub(crate) const FRONTIER_LANE_CUM: usize = 3;
-pub(crate) const FRONTIER_LANE_LOGPROB: usize = 4;
-pub(crate) const FRONTIER_LANE_KEY: usize = 5;
-pub(crate) const FRONTIER_LANE_ACTIVE: usize = 6;
-pub(crate) const FRONTIER_LANE_COUNT: usize = FRONTIER_LANE_ACTIVE + 1;
-
-pub(crate) const TREE_LANE_TOKEN: usize = 0;
-pub(crate) const TREE_LANE_PARENT: usize = 1;
-pub(crate) const TREE_LANE_DEPTH: usize = 2;
-pub(crate) const TREE_LANE_CUM: usize = 3;
-pub(crate) const TREE_LANE_LOGPROB: usize = 4;
-pub(crate) const TREE_LANE_MASK: usize = 5;
-pub(crate) const TREE_LANE_COUNT: usize = TREE_LANE_MASK + 1;
-
-pub(crate) const METADATA_LANE_DEPTH: usize = 0;
-pub(crate) const METADATA_LANE_ANCESTOR_COUNT: usize = 1;
-pub(crate) const METADATA_LANE_NODE_INDEX: usize = 2;
-pub(crate) const METADATA_LANE_COUNT: usize = METADATA_LANE_NODE_INDEX + 1;
-
-pub(crate) const NO_WINNER: u32 = u32::MAX;
-pub(crate) const MAX_FRONTIER_SLOTS: usize = 2048;
-pub(crate) const MAX_FRONTIER_WIDTH: usize = 32;
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_scatter.rs b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_scatter.rs
index 2acfe32c2..b6cab65a3 100644
--- a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_scatter.rs
+++ b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_scatter.rs
@@ -1,6 +1,6 @@
use proc_macros::kernel;
-use crate::backends::common::kernel::weaver::*;
+use crate::backends::common::gpu_types::weaver;
const F32_SIGN_BIT: u32 = 1 << (u32::BITS - 1);
@@ -30,13 +30,13 @@ pub fn weaver_frontier_scatter(
return;
}
let (capacity, tree_slots, rows, fanout) = (capacity as usize, tree_slots as usize, rows as usize, fanout as usize);
- let tree = unsafe { std::slice::from_raw_parts(tree, TREE_LANE_COUNT * tree_slots) };
+ let tree = unsafe { std::slice::from_raw_parts(tree, weaver::TREE_LANE_COUNT * tree_slots) };
let parent_indices =
- unsafe { std::slice::from_raw_parts(round_metadata.add(METADATA_LANE_NODE_INDEX * rows), rows) };
+ unsafe { std::slice::from_raw_parts(round_metadata.add(weaver::METADATA_LANE_NODE_INDEX * rows), rows) };
let round_valid = unsafe { std::slice::from_raw_parts(round_valid, rows) };
let child_ids = unsafe { std::slice::from_raw_parts(child_ids, rows * fanout) };
let child_logprobs = unsafe { std::slice::from_raw_parts(child_logprobs, rows * fanout) };
- let frontier = unsafe { std::slice::from_raw_parts_mut(frontier, FRONTIER_LANE_COUNT * capacity) };
+ let frontier = unsafe { std::slice::from_raw_parts_mut(frontier, weaver::FRONTIER_LANE_COUNT * capacity) };
for index in 0..rows * fanout {
let row = index / fanout;
@@ -49,11 +49,11 @@ pub fn weaver_frontier_scatter(
continue;
}
let logprob = child_logprobs[index];
- let cumulative_logprob = f32::from_bits(tree[TREE_LANE_CUM * tree_slots + parent]) + logprob;
+ let cumulative_logprob = f32::from_bits(tree[weaver::TREE_LANE_CUM * tree_slots + parent]) + logprob;
let values = [
child_ids[index],
parent as u32,
- tree[TREE_LANE_DEPTH * tree_slots + parent] + 1,
+ tree[weaver::TREE_LANE_DEPTH * tree_slots + parent] + 1,
cumulative_logprob.to_bits(),
logprob.to_bits(),
top_k_score_key(cumulative_logprob),
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_select.rs b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_select.rs
index b8f58767d..7e178879a 100644
--- a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_select.rs
+++ b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_frontier_select.rs
@@ -1,6 +1,6 @@
use proc_macros::kernel;
-use crate::backends::common::kernel::weaver::*;
+use crate::backends::common::gpu_types::weaver;
#[kernel(WeaverFrontierSelect)]
pub fn weaver_frontier_select(
@@ -26,9 +26,9 @@ pub fn weaver_frontier_select(
candidate_pool_size: u32,
) {
if capacity == 0
- || capacity as usize > MAX_FRONTIER_SLOTS
+ || capacity as usize > weaver::FRONTIER_MAX_SLOTS
|| width == 0
- || width as usize > MAX_FRONTIER_WIDTH
+ || width as usize > weaver::FRONTIER_MAX_WIDTH
|| ancestor_stride == 0
|| max_depth == 0
|| tree_slots == 0
@@ -42,11 +42,11 @@ pub fn weaver_frontier_select(
(capacity as usize, tree_slots as usize, width as usize, ancestor_stride as usize);
let (candidate_pool_rows, candidate_pool_size) = (candidate_pool_rows as usize, candidate_pool_size as usize);
let pool_len = candidate_pool_rows * candidate_pool_size;
- let frontier = unsafe { std::slice::from_raw_parts_mut(frontier, FRONTIER_LANE_COUNT * capacity) };
- let tree = unsafe { std::slice::from_raw_parts_mut(tree, TREE_LANE_COUNT * slots) };
+ let frontier = unsafe { std::slice::from_raw_parts_mut(frontier, weaver::FRONTIER_LANE_COUNT * capacity) };
+ let tree = unsafe { std::slice::from_raw_parts_mut(tree, weaver::TREE_LANE_COUNT * slots) };
let slot_ancestors = unsafe { std::slice::from_raw_parts_mut(slot_ancestors, slots * stride) };
let round_token_ids = unsafe { std::slice::from_raw_parts_mut(round_token_ids, width) };
- let round_metadata = unsafe { std::slice::from_raw_parts_mut(round_metadata, METADATA_LANE_COUNT * width) };
+ let round_metadata = unsafe { std::slice::from_raw_parts_mut(round_metadata, weaver::METADATA_LANE_COUNT * width) };
let round_ancestors = unsafe { std::slice::from_raw_parts_mut(round_ancestors, width * stride) };
let round_valid = unsafe { std::slice::from_raw_parts_mut(round_valid, width) };
let candidate_pool_ids = unsafe { std::slice::from_raw_parts(candidate_pool_ids, pool_len) };
@@ -57,21 +57,22 @@ pub fn weaver_frontier_select(
unsafe { std::slice::from_raw_parts_mut(round_candidate_scores, width * candidate_pool_size) };
for row in 0..width {
- let (mut key, mut parent, mut token, mut winner) = (0, NO_WINNER, NO_WINNER, NO_WINNER);
+ let (mut key, mut parent, mut token, mut winner) =
+ (0, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER);
for slot in 0..capacity {
- if frontier[FRONTIER_LANE_ACTIVE * capacity + slot] == 0 {
+ if frontier[weaver::FRONTIER_LANE_ACTIVE * capacity + slot] == 0 {
continue;
}
let next = (
- frontier[FRONTIER_LANE_KEY * capacity + slot],
- frontier[FRONTIER_LANE_PARENT * capacity + slot],
- frontier[FRONTIER_LANE_TOKEN * capacity + slot],
+ frontier[weaver::FRONTIER_LANE_KEY * capacity + slot],
+ frontier[weaver::FRONTIER_LANE_PARENT * capacity + slot],
+ frontier[weaver::FRONTIER_LANE_TOKEN * capacity + slot],
);
if next.0 > key || (next.0 == key && (next.1 < parent || (next.1 == parent && next.2 < token))) {
(key, parent, token, winner) = (next.0, next.1, next.2, slot as u32);
}
}
- let real = winner != NO_WINNER;
+ let real = winner != weaver::FRONTIER_NO_WINNER;
let winner = if real {
winner as usize
} else {
@@ -79,24 +80,24 @@ pub fn weaver_frontier_select(
};
let tree_slot = slot_start as usize + row;
let lane_value = |lane: usize| u32::from(real) * frontier[lane * capacity + winner];
- let token = lane_value(FRONTIER_LANE_TOKEN);
- let depth = lane_value(FRONTIER_LANE_DEPTH);
- let cumulative_logprob = lane_value(FRONTIER_LANE_CUM);
- let logprob = lane_value(FRONTIER_LANE_LOGPROB);
+ let token = lane_value(weaver::FRONTIER_LANE_TOKEN);
+ let depth = lane_value(weaver::FRONTIER_LANE_DEPTH);
+ let cumulative_logprob = lane_value(weaver::FRONTIER_LANE_CUM);
+ let logprob = lane_value(weaver::FRONTIER_LANE_LOGPROB);
- tree[TREE_LANE_TOKEN * slots + tree_slot] = token;
- tree[TREE_LANE_PARENT * slots + tree_slot] = if real {
+ tree[weaver::TREE_LANE_TOKEN * slots + tree_slot] = token;
+ tree[weaver::TREE_LANE_PARENT * slots + tree_slot] = if real {
parent
} else {
- NO_WINNER
+ weaver::FRONTIER_NO_WINNER
};
- tree[TREE_LANE_DEPTH * slots + tree_slot] = depth;
- tree[TREE_LANE_CUM * slots + tree_slot] = cumulative_logprob;
- tree[TREE_LANE_LOGPROB * slots + tree_slot] = logprob;
- tree[TREE_LANE_MASK * slots + tree_slot] = u32::from(real);
+ tree[weaver::TREE_LANE_DEPTH * slots + tree_slot] = depth;
+ tree[weaver::TREE_LANE_CUM * slots + tree_slot] = cumulative_logprob;
+ tree[weaver::TREE_LANE_LOGPROB * slots + tree_slot] = logprob;
+ tree[weaver::TREE_LANE_MASK * slots + tree_slot] = u32::from(real);
if real {
- frontier[FRONTIER_LANE_ACTIVE * capacity + winner] = 0;
+ frontier[weaver::FRONTIER_LANE_ACTIVE * capacity + winner] = 0;
}
let parent_slot = if real && (parent as usize) < slots {
@@ -117,9 +118,9 @@ pub fn weaver_frontier_select(
}
round_token_ids[row] = token;
- round_metadata[METADATA_LANE_DEPTH * width + row] = depth.min(max_depth - 1);
- round_metadata[METADATA_LANE_ANCESTOR_COUNT * width + row] = depth.min(stride as u32);
- round_metadata[METADATA_LANE_NODE_INDEX * width + row] = tree_slot as u32;
+ round_metadata[weaver::METADATA_LANE_DEPTH * width + row] = depth.min(max_depth - 1);
+ round_metadata[weaver::METADATA_LANE_ANCESTOR_COUNT * width + row] = depth.min(stride as u32);
+ round_metadata[weaver::METADATA_LANE_NODE_INDEX * width + row] = tree_slot as u32;
round_valid[row] = u32::from(real && depth < lookahead_count && depth < max_depth);
// Every row of a round expands the pool for its own depth, so the winner's pool row is
diff --git a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_top_children.rs b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_top_children.rs
index c6027f6ed..55b713f3f 100644
--- a/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_top_children.rs
+++ b/crates/backend-uzu/src/backends/cpu/kernel/weaver/weaver_top_children.rs
@@ -1,7 +1,7 @@
use half::bf16;
use proc_macros::kernel;
-use crate::backends::common::kernel::weaver::MAX_CANDIDATES;
+use crate::backends::common::gpu_types::weaver;
#[kernel(WeaverTopChildren)]
pub fn weaver_top_children(
@@ -17,7 +17,7 @@ pub fn weaver_top_children(
let rows = rows as usize;
let candidates = candidates as usize;
let children = children as usize;
- if candidates == 0 || candidates > MAX_CANDIDATES || children == 0 || children > candidates {
+ if candidates == 0 || candidates > weaver::CANDIDATES_MAX || children == 0 || children > candidates {
return;
}
for row in 0..rows {
diff --git a/crates/backend-uzu/src/backends/metal/kernel/generated/weaver.h b/crates/backend-uzu/src/backends/metal/kernel/generated/weaver.h
new file mode 100644
index 000000000..b49c08d42
--- /dev/null
+++ b/crates/backend-uzu/src/backends/metal/kernel/generated/weaver.h
@@ -0,0 +1,63 @@
+// Auto-generated from gpu_types/weaver - do not edit manually
+#pragma once
+
+#include <metal_stdlib>
+using namespace metal;
+
+namespace uzu::weaver {
+static constant constexpr size_t CANDIDATES_MAX = 512;
+
+static constant constexpr size_t TOP_CHILDREN_THREADS = 256;
+
+static constant constexpr size_t TOP_CHILDREN_SIMDGROUPS = 8;
+
+static constant constexpr size_t FRONTIER_LANE_TOKEN = 0;
+
+static constant constexpr size_t FRONTIER_LANE_PARENT = 1;
+
+static constant constexpr size_t FRONTIER_LANE_DEPTH = 2;
+
+static constant constexpr size_t FRONTIER_LANE_CUM = 3;
+
+static constant constexpr size_t FRONTIER_LANE_LOGPROB = 4;
+
+static constant constexpr size_t FRONTIER_LANE_KEY = 5;
+
+static constant constexpr size_t FRONTIER_LANE_ACTIVE = 6;
+
+static constant constexpr size_t FRONTIER_LANE_COUNT = FRONTIER_LANE_ACTIVE + 1;
+
+static constant constexpr size_t TREE_LANE_TOKEN = 0;
+
+static constant constexpr size_t TREE_LANE_PARENT = 1;
+
+static constant constexpr size_t TREE_LANE_DEPTH = 2;
+
+static constant constexpr size_t TREE_LANE_CUM = 3;
+
+static constant constexpr size_t TREE_LANE_LOGPROB = 4;
+
+static constant constexpr size_t TREE_LANE_MASK = 5;
+
+static constant constexpr size_t TREE_LANE_COUNT = TREE_LANE_MASK + 1;
+
+static constant constexpr size_t METADATA_LANE_DEPTH = 0;
+
+static constant constexpr size_t METADATA_LANE_ANCESTOR_COUNT = 1;
+
+static constant constexpr size_t METADATA_LANE_NODE_INDEX = 2;
+
+static constant constexpr size_t METADATA_LANE_COUNT = METADATA_LANE_NODE_INDEX + 1;
+
+static constant constexpr uint32_t FRONTIER_NO_WINNER = ~0;
+
+static constant constexpr size_t FRONTIER_SELECT_THREADS = 256;
+
+static constant constexpr size_t FRONTIER_SELECT_SIMDGROUPS = 8;
+
+static constant constexpr size_t FRONTIER_ENTRIES_PER_THREAD = 8;
+
+static constant constexpr size_t FRONTIER_MAX_SLOTS = 2048;
+
+static constant constexpr size_t FRONTIER_MAX_WIDTH = 32;
+} // namespace uzu::weaver
diff --git a/crates/backend-uzu/src/backends/metal/kernel/mod.rs b/crates/backend-uzu/src/backends/metal/kernel/mod.rs
index 86353cfb9..f2285865d 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/mod.rs
+++ b/crates/backend-uzu/src/backends/metal/kernel/mod.rs
@@ -1,7 +1,10 @@
use crate::backends::{
common::{
Kernels,
- gpu_types::gemm::{gemm_tiling_simdgroups_per_column, gemm_tiling_simdgroups_per_row},
+ gpu_types::{
+ gemm::{gemm_tiling_simdgroups_per_column, gemm_tiling_simdgroups_per_row},
+ weaver,
+ },
},
metal::Metal,
};
diff --git a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier.h b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier.h
index 47a803776..9cfecf384 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier.h
+++ b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier.h
@@ -1,29 +1,5 @@
#pragma once
-using namespace metal;
+#include "../generated/weaver.h"
-constant uint WEAVER_FRONTIER_LANE_TOKEN = 0;
-constant uint WEAVER_FRONTIER_LANE_PARENT = 1;
-constant uint WEAVER_FRONTIER_LANE_DEPTH = 2;
-constant uint WEAVER_FRONTIER_LANE_CUM = 3;
-constant uint WEAVER_FRONTIER_LANE_LOGPROB = 4;
-constant uint WEAVER_FRONTIER_LANE_KEY = 5;
-constant uint WEAVER_FRONTIER_LANE_ACTIVE = 6;
-
-constant uint WEAVER_TREE_LANE_TOKEN = 0;
-constant uint WEAVER_TREE_LANE_PARENT = 1;
-constant uint WEAVER_TREE_LANE_DEPTH = 2;
-constant uint WEAVER_TREE_LANE_CUM = 3;
-constant uint WEAVER_TREE_LANE_LOGPROB = 4;
-constant uint WEAVER_TREE_LANE_MASK = 5;
-
-constant uint WEAVER_METADATA_LANE_DEPTH = 0;
-constant uint WEAVER_METADATA_LANE_ANCESTOR_COUNT = 1;
-constant uint WEAVER_METADATA_LANE_NODE_INDEX = 2;
-
-constant uint WEAVER_FRONTIER_NO_WINNER = 0xffffffffu;
-constant uint WEAVER_FRONTIER_SELECT_THREADS = 256;
-constant uint WEAVER_FRONTIER_SELECT_SIMDGROUPS = 8;
-constant uint WEAVER_FRONTIER_ENTRIES_PER_THREAD = 8;
-constant uint WEAVER_FRONTIER_MAX_SLOTS = 2048;
-constant uint WEAVER_FRONTIER_MAX_WIDTH = 32;
+namespace weaver = uzu::weaver;
diff --git a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_scatter.metal b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_scatter.metal
index 13756a247..667591ca7 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_scatter.metal
+++ b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_scatter.metal
@@ -27,7 +27,7 @@ PUBLIC KERNEL(WeaverFrontierScatter)(
return;
}
- const uint parent = round_metadata[WEAVER_METADATA_LANE_NODE_INDEX * rows + row];
+ const uint parent = round_metadata[weaver::METADATA_LANE_NODE_INDEX * rows + row];
if (parent >= tree_slots) {
return;
}
@@ -37,13 +37,13 @@ PUBLIC KERNEL(WeaverFrontierScatter)(
}
const float logprob = child_logprobs[row * fanout + child];
- const float cumulative_logprob = as_type<float>(tree[WEAVER_TREE_LANE_CUM * tree_slots + parent]) + logprob;
+ const float cumulative_logprob = as_type<float>(tree[weaver::TREE_LANE_CUM * tree_slots + parent]) + logprob;
- frontier[WEAVER_FRONTIER_LANE_TOKEN * capacity + slot] = child_ids[row * fanout + child];
- frontier[WEAVER_FRONTIER_LANE_PARENT * capacity + slot] = parent;
- frontier[WEAVER_FRONTIER_LANE_DEPTH * capacity + slot] = tree[WEAVER_TREE_LANE_DEPTH * tree_slots + parent] + 1u;
- frontier[WEAVER_FRONTIER_LANE_CUM * capacity + slot] = as_type<uint>(cumulative_logprob);
- frontier[WEAVER_FRONTIER_LANE_LOGPROB * capacity + slot] = as_type<uint>(logprob);
- frontier[WEAVER_FRONTIER_LANE_KEY * capacity + slot] = top_k_score_key(cumulative_logprob);
- frontier[WEAVER_FRONTIER_LANE_ACTIVE * capacity + slot] = 1u;
+ frontier[weaver::FRONTIER_LANE_TOKEN * capacity + slot] = child_ids[row * fanout + child];
+ frontier[weaver::FRONTIER_LANE_PARENT * capacity + slot] = parent;
+ frontier[weaver::FRONTIER_LANE_DEPTH * capacity + slot] = tree[weaver::TREE_LANE_DEPTH * tree_slots + parent] + 1u;
+ frontier[weaver::FRONTIER_LANE_CUM * capacity + slot] = as_type<uint>(cumulative_logprob);
+ frontier[weaver::FRONTIER_LANE_LOGPROB * capacity + slot] = as_type<uint>(logprob);
+ frontier[weaver::FRONTIER_LANE_KEY * capacity + slot] = top_k_score_key(cumulative_logprob);
+ frontier[weaver::FRONTIER_LANE_ACTIVE * capacity + slot] = 1u;
}
diff --git a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_select.metal b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_select.metal
index ac8968116..b58b5b192 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_select.metal
+++ b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_frontier_select.metal
@@ -27,34 +27,34 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
constant uint& lookahead_count,
constant uint& candidate_pool_rows,
constant uint& candidate_pool_size,
- threadgroup uint4 reduce[WEAVER_FRONTIER_SELECT_SIMDGROUPS],
- threadgroup uint winner_slot[WEAVER_FRONTIER_MAX_WIDTH],
- threadgroup uint row_candidate_pool_row[WEAVER_FRONTIER_MAX_WIDTH],
+ threadgroup uint4 reduce[weaver::FRONTIER_SELECT_SIMDGROUPS],
+ threadgroup uint winner_slot[weaver::FRONTIER_MAX_WIDTH],
+ threadgroup uint row_candidate_pool_row[weaver::FRONTIER_MAX_WIDTH],
const ThreadContext thread_context,
const uint group_index GROUPS(1),
- const uint lid THREADS(256)
+ const uint lid THREADS(weaver::FRONTIER_SELECT_THREADS)
) {
(void)group_index;
- if (capacity == 0 || capacity > WEAVER_FRONTIER_MAX_SLOTS || width == 0 || width > WEAVER_FRONTIER_MAX_WIDTH ||
+ if (capacity == 0 || capacity > weaver::FRONTIER_MAX_SLOTS || width == 0 || width > weaver::FRONTIER_MAX_WIDTH ||
ancestor_stride == 0 || max_depth == 0 || tree_slots == 0 || slot_start + width > tree_slots ||
candidate_pool_rows == 0 || candidate_pool_size == 0) {
return;
}
- bool entry_active[WEAVER_FRONTIER_ENTRIES_PER_THREAD];
- for (uint entry = 0; entry < WEAVER_FRONTIER_ENTRIES_PER_THREAD; ++entry) {
- const uint slot = lid + entry * WEAVER_FRONTIER_SELECT_THREADS;
- entry_active[entry] = slot < capacity && frontier[WEAVER_FRONTIER_LANE_ACTIVE * capacity + slot] != 0u;
+ bool entry_active[weaver::FRONTIER_ENTRIES_PER_THREAD];
+ for (uint entry = 0; entry < weaver::FRONTIER_ENTRIES_PER_THREAD; ++entry) {
+ const uint slot = lid + entry * weaver::FRONTIER_SELECT_THREADS;
+ entry_active[entry] = slot < capacity && frontier[weaver::FRONTIER_LANE_ACTIVE * capacity + slot] != 0u;
}
for (uint child = 0; child < width; ++child) {
- uint4 local = uint4(0u, WEAVER_FRONTIER_NO_WINNER, WEAVER_FRONTIER_NO_WINNER, WEAVER_FRONTIER_NO_WINNER);
- for (uint entry = 0; entry < WEAVER_FRONTIER_ENTRIES_PER_THREAD; ++entry) {
- const uint slot = lid + entry * WEAVER_FRONTIER_SELECT_THREADS;
+ uint4 local = uint4(0u, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER);
+ for (uint entry = 0; entry < weaver::FRONTIER_ENTRIES_PER_THREAD; ++entry) {
+ const uint slot = lid + entry * weaver::FRONTIER_SELECT_THREADS;
if (entry_active[entry]) {
- const uint key = frontier[WEAVER_FRONTIER_LANE_KEY * capacity + slot];
- const uint parent = frontier[WEAVER_FRONTIER_LANE_PARENT * capacity + slot];
- const uint token = frontier[WEAVER_FRONTIER_LANE_TOKEN * capacity + slot];
+ const uint key = frontier[weaver::FRONTIER_LANE_KEY * capacity + slot];
+ const uint parent = frontier[weaver::FRONTIER_LANE_PARENT * capacity + slot];
+ const uint token = frontier[weaver::FRONTIER_LANE_TOKEN * capacity + slot];
if (key > local.x || (key == local.x && (parent < local.y || (parent == local.y && token < local.z)))) {
local = uint4(key, parent, token, slot);
}
@@ -63,9 +63,9 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
uint4 simd;
simd.x = simd_max(local.x);
- simd.y = simd_min(local.x == simd.x ? local.y : WEAVER_FRONTIER_NO_WINNER);
- simd.z = simd_min(all(local.xy == simd.xy) ? local.z : WEAVER_FRONTIER_NO_WINNER);
- simd.w = simd_min(all(local.xyz == simd.xyz) ? local.w : WEAVER_FRONTIER_NO_WINNER);
+ simd.y = simd_min(local.x == simd.x ? local.y : weaver::FRONTIER_NO_WINNER);
+ simd.z = simd_min(all(local.xy == simd.xy) ? local.z : weaver::FRONTIER_NO_WINNER);
+ simd.w = simd_min(all(local.xyz == simd.xyz) ? local.w : weaver::FRONTIER_NO_WINNER);
if (thread_context.simd_lane_id == 0) {
reduce[thread_context.simdgroup_index] = simd;
}
@@ -73,14 +73,14 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
if (thread_context.simdgroup_index == 0) {
const uint4 partial =
- thread_context.simd_lane_id < WEAVER_FRONTIER_SELECT_SIMDGROUPS
+ thread_context.simd_lane_id < weaver::FRONTIER_SELECT_SIMDGROUPS
? reduce[thread_context.simd_lane_id]
- : uint4(0u, WEAVER_FRONTIER_NO_WINNER, WEAVER_FRONTIER_NO_WINNER, WEAVER_FRONTIER_NO_WINNER);
+ : uint4(0u, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER, weaver::FRONTIER_NO_WINNER);
uint4 selected;
selected.x = simd_max(partial.x);
- selected.y = simd_min(partial.x == selected.x ? partial.y : WEAVER_FRONTIER_NO_WINNER);
- selected.z = simd_min(all(partial.xy == selected.xy) ? partial.z : WEAVER_FRONTIER_NO_WINNER);
- selected.w = simd_min(all(partial.xyz == selected.xyz) ? partial.w : WEAVER_FRONTIER_NO_WINNER);
+ selected.y = simd_min(partial.x == selected.x ? partial.y : weaver::FRONTIER_NO_WINNER);
+ selected.z = simd_min(all(partial.xy == selected.xy) ? partial.z : weaver::FRONTIER_NO_WINNER);
+ selected.w = simd_min(all(partial.xyz == selected.xyz) ? partial.w : weaver::FRONTIER_NO_WINNER);
if (thread_context.simd_lane_id == 0) {
winner_slot[child] = selected.w;
}
@@ -88,32 +88,32 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
threadgroup_barrier(mem_flags::mem_threadgroup);
const uint selected = winner_slot[child];
- for (uint entry = 0; entry < WEAVER_FRONTIER_ENTRIES_PER_THREAD; ++entry) {
- entry_active[entry] = entry_active[entry] && (lid + entry * WEAVER_FRONTIER_SELECT_THREADS) != selected;
+ for (uint entry = 0; entry < weaver::FRONTIER_ENTRIES_PER_THREAD; ++entry) {
+ entry_active[entry] = entry_active[entry] && (lid + entry * weaver::FRONTIER_SELECT_THREADS) != selected;
}
}
if (lid < width) {
const uint row = lid;
const uint slot = winner_slot[row];
- const bool real = slot != WEAVER_FRONTIER_NO_WINNER;
+ const bool real = slot != weaver::FRONTIER_NO_WINNER;
const uint tree_slot = slot_start + row;
- const uint token = real ? frontier[WEAVER_FRONTIER_LANE_TOKEN * capacity + slot] : 0u;
- const uint parent = real ? frontier[WEAVER_FRONTIER_LANE_PARENT * capacity + slot] : WEAVER_FRONTIER_NO_WINNER;
- const uint depth = real ? frontier[WEAVER_FRONTIER_LANE_DEPTH * capacity + slot] : 0u;
- const uint cumulative_logprob = real ? frontier[WEAVER_FRONTIER_LANE_CUM * capacity + slot] : 0u;
- const uint logprob = real ? frontier[WEAVER_FRONTIER_LANE_LOGPROB * capacity + slot] : 0u;
+ const uint token = real ? frontier[weaver::FRONTIER_LANE_TOKEN * capacity + slot] : 0u;
+ const uint parent = real ? frontier[weaver::FRONTIER_LANE_PARENT * capacity + slot] : weaver::FRONTIER_NO_WINNER;
+ const uint depth = real ? frontier[weaver::FRONTIER_LANE_DEPTH * capacity + slot] : 0u;
+ const uint cumulative_logprob = real ? frontier[weaver::FRONTIER_LANE_CUM * capacity + slot] : 0u;
+ const uint logprob = real ? frontier[weaver::FRONTIER_LANE_LOGPROB * capacity + slot] : 0u;
- tree[WEAVER_TREE_LANE_TOKEN * tree_slots + tree_slot] = token;
- tree[WEAVER_TREE_LANE_PARENT * tree_slots + tree_slot] = parent;
- tree[WEAVER_TREE_LANE_DEPTH * tree_slots + tree_slot] = depth;
- tree[WEAVER_TREE_LANE_CUM * tree_slots + tree_slot] = cumulative_logprob;
- tree[WEAVER_TREE_LANE_LOGPROB * tree_slots + tree_slot] = logprob;
- tree[WEAVER_TREE_LANE_MASK * tree_slots + tree_slot] = real ? 1u : 0u;
+ tree[weaver::TREE_LANE_TOKEN * tree_slots + tree_slot] = token;
+ tree[weaver::TREE_LANE_PARENT * tree_slots + tree_slot] = parent;
+ tree[weaver::TREE_LANE_DEPTH * tree_slots + tree_slot] = depth;
+ tree[weaver::TREE_LANE_CUM * tree_slots + tree_slot] = cumulative_logprob;
+ tree[weaver::TREE_LANE_LOGPROB * tree_slots + tree_slot] = logprob;
+ tree[weaver::TREE_LANE_MASK * tree_slots + tree_slot] = real ? 1u : 0u;
if (real) {
- frontier[WEAVER_FRONTIER_LANE_ACTIVE * capacity + slot] = 0u;
+ frontier[weaver::FRONTIER_LANE_ACTIVE * capacity + slot] = 0u;
}
const uint parent_slot = real && parent < tree_slots ? parent : 0u;
@@ -127,9 +127,9 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
}
round_token_ids[row] = token;
- round_metadata[WEAVER_METADATA_LANE_DEPTH * width + row] = min(depth, max_depth - 1u);
- round_metadata[WEAVER_METADATA_LANE_ANCESTOR_COUNT * width + row] = min(depth, ancestor_stride);
- round_metadata[WEAVER_METADATA_LANE_NODE_INDEX * width + row] = tree_slot;
+ round_metadata[weaver::METADATA_LANE_DEPTH * width + row] = min(depth, max_depth - 1u);
+ round_metadata[weaver::METADATA_LANE_ANCESTOR_COUNT * width + row] = min(depth, ancestor_stride);
+ round_metadata[weaver::METADATA_LANE_NODE_INDEX * width + row] = tree_slot;
round_valid[row] = real && depth < lookahead_count && depth < max_depth ? 1u : 0u;
row_candidate_pool_row[row] = min(depth, candidate_pool_rows - 1u);
@@ -140,7 +140,7 @@ PUBLIC KERNEL(WeaverFrontierSelect)(
for (uint row = 0; row < width; ++row) {
const uint source = row_candidate_pool_row[row] * candidate_pool_size;
const uint destination = row * candidate_pool_size;
- for (uint candidate = lid; candidate < candidate_pool_size; candidate += WEAVER_FRONTIER_SELECT_THREADS) {
+ for (uint candidate = lid; candidate < candidate_pool_size; candidate += weaver::FRONTIER_SELECT_THREADS) {
round_candidate_ids[destination + candidate] = candidate_pool_ids[source + candidate];
round_candidate_scores[destination + candidate] = candidate_pool_scores[source + candidate];
}
diff --git a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_top_children.metal b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_top_children.metal
index ef7360872..84e101ffc 100644
--- a/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_top_children.metal
+++ b/crates/backend-uzu/src/backends/metal/kernel/weaver/weaver_top_children.metal
@@ -2,13 +2,10 @@
#include "../common/dsl.h"
#include "../common/thread_context.h"
#include "../common/top_k.h"
+#include "weaver_frontier.h"
using namespace metal;
-constant uint WEAVER_CANDIDATES_MAX = 512;
-constant uint WEAVER_TOP_CHILDREN_THREADS = 256;
-constant uint WEAVER_TOP_CHILDREN_SIMDGROUPS = 8;
-
METAL_FUNC bool weaver_better(uint score, uint token, uint index, uint best_score, uint best_token, uint best_index) {
return score > best_score ||
(score == best_score && (token < best_token || (token == best_token && index < best_index)));
@@ -23,10 +20,10 @@ PUBLIC KERNEL(WeaverTopChildren)(
constant uint& rows,
constant uint& candidates,
constant uint& children,
- threadgroup float reduce_float[WEAVER_TOP_CHILDREN_SIMDGROUPS],
- threadgroup uint reduce_score[WEAVER_TOP_CHILDREN_SIMDGROUPS],
- threadgroup uint reduce_token[WEAVER_TOP_CHILDREN_SIMDGROUPS],
- threadgroup uint reduce_index[WEAVER_TOP_CHILDREN_SIMDGROUPS],
+ threadgroup float reduce_float[weaver::TOP_CHILDREN_SIMDGROUPS],
+ threadgroup uint reduce_score[weaver::TOP_CHILDREN_SIMDGROUPS],
+ threadgroup uint reduce_token[weaver::TOP_CHILDREN_SIMDGROUPS],
+ threadgroup uint reduce_index[weaver::TOP_CHILDREN_SIMDGROUPS],
threadgroup float& logit_max,
threadgroup float& log_sum,
threadgroup uint& winner_score,
@@ -34,15 +31,15 @@ PUBLIC KERNEL(WeaverTopChildren)(
threadgroup uint& winner_index,
const ThreadContext thread_context,
const uint row GROUPS(rows),
- const uint lid THREADS(256)
+ const uint lid THREADS(weaver::TOP_CHILDREN_THREADS)
) {
- if (candidates == 0 || candidates > WEAVER_CANDIDATES_MAX || children == 0 || children > candidates) {
+ if (candidates == 0 || candidates > weaver::CANDIDATES_MAX || children == 0 || children > candidates) {
return;
}
const uint base = row * candidates;
const uint first_index = lid;
- const uint second_index = lid + WEAVER_TOP_CHILDREN_THREADS;
+ const uint second_index = lid + weaver::TOP_CHILDREN_THREADS;
const bool first_valid = first_index < candidates;
const bool second_valid = second_index < candidates;
const float first_logit =
@@ -63,7 +60,7 @@ PUBLIC KERNEL(WeaverTopChildren)(
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (thread_context.simdgroup_index == 0) {
- const float group_value = thread_context.simd_lane_id < WEAVER_TOP_CHILDREN_SIMDGROUPS
+ const float group_value = thread_context.simd_lane_id < weaver::TOP_CHILDREN_SIMDGROUPS
? reduce_float[thread_context.simd_lane_id]
: -INFINITY;
const float maximum = simd_max(group_value);
@@ -81,8 +78,9 @@ PUBLIC KERNEL(WeaverTopChildren)(
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (thread_context.simdgroup_index == 0) {
- const float group_value =
- thread_context.simd_lane_id < WEAVER_TOP_CHILDREN_SIMDGROUPS ? reduce_float[thread_context.simd_lane_id] : 0.0f;
+ const float group_value = thread_context.simd_lane_id < weaver::TOP_CHILDREN_SIMDGROUPS
+ ? reduce_float[thread_context.simd_lane_id]
+ : 0.0f;
const float total = simd_sum(group_value);
if (thread_context.simd_lane_id == 0) {
log_sum = log(total) + logit_max;
@@ -118,7 +116,7 @@ PUBLIC KERNEL(WeaverTopChildren)(
threadgroup_barrier(mem_flags::mem_threadgroup);
if (thread_context.simdgroup_index == 0) {
- const bool lane_valid = thread_context.simd_lane_id < WEAVER_TOP_CHILDREN_SIMDGROUPS;
+ const bool lane_valid = thread_context.simd_lane_id < weaver::TOP_CHILDREN_SIMDGROUPS;
const uint group_score = lane_valid ? reduce_score[thread_context.simd_lane_id] : 0u;
const uint selected_score = simd_max(group_score);
const uint group_token =
diff --git a/crates/backend-uzu/src/encodable_block/weaver.rs b/crates/backend-uzu/src/encodable_block/weaver.rs
index ca5cd814a..a321226c8 100644
--- a/crates/backend-uzu/src/encodable_block/weaver.rs
+++ b/crates/backend-uzu/src/encodable_block/weaver.rs
@@ -4,10 +4,10 @@ use crate::{
array::size_for_shape,
backends::common::{
Allocation, AllocationType, Backend, Context, Encoder, Kernels,
+ gpu_types::weaver,
kernel::{
ActivationKernel, AttentionLastQueryKernel, AttentionPrepareKernel, TensorAddBiasKernel,
TensorAddSwapKernel, WeaverNodeCacheWriteKernel, WeaverTopChildrenKernel,
- weaver::{MAX_CANDIDATES, METADATA_LANE_COUNT},
},
},
config::{normalization::NormalizationConfig, weaver::WeaverConfig},
@@ -102,7 +102,7 @@ pub enum WeaverNewError<B: Backend> {
Backend(#[source] B::Error),
#[error("model_dim must be divisible by num_heads")]
InvalidHeadConfig,
- #[error("candidate_pool_size must be in 1..={MAX_CANDIDATES}, got {0}")]
+ #[error("candidate_pool_size must be in 1..={max}, got {0}", max = weaver::CANDIDATES_MAX)]
InvalidCandidatePoolSize(usize),
}
@@ -212,7 +212,7 @@ impl<B: Backend> Weaver<B> {
if config.num_heads == 0 || !config.model_dim.is_multiple_of(config.num_heads) {
return Err(WeaverNewError::InvalidHeadConfig);
}
- if config.candidate_pool_size == 0 || config.candidate_pool_size > MAX_CANDIDATES {
+ if config.candidate_pool_size == 0 || config.candidate_pool_size > weaver::CANDIDATES_MAX {
return Err(WeaverNewError::InvalidCandidatePoolSize(config.candidate_pool_size));
}
let norm = |dim, name: &str| -> Result<_, WeaverNewError<B>> {
@@ -364,11 +364,11 @@ impl<B: Backend> Weaver<B> {
let rows = input.row_count;
let candidates = input.candidate_count;
assert!(rows > 0);
- assert!(candidates > 0 && candidates <= MAX_CANDIDATES);
+ assert!(candidates > 0 && candidates <= weaver::CANDIDATES_MAX);
assert!(children > 0 && children <= candidates);
assert!(input.ancestor_stride > 0);
let word = DataType::U32.size_in_bytes();
- debug_assert!(input.node_metadata.size() >= METADATA_LANE_COUNT * rows * word);
+ debug_assert!(input.node_metadata.size() >= weaver::METADATA_LANE_COUNT * rows * word);
debug_assert!(input.parent_token_ids.size() >= rows * word);
debug_assert!(input.ancestor_indices.size() >= rows * input.ancestor_stride * word);
diff --git a/crates/backend-uzu/src/speculators/dflash_speculator.rs b/crates/backend-uzu/src/speculators/dflash_speculator.rs
index 33edb8212..0e4b58335 100644
--- a/crates/backend-uzu/src/speculators/dflash_speculator.rs
+++ b/crates/backend-uzu/src/speculators/dflash_speculator.rs
@@ -6,13 +6,8 @@ pub use crate::encodable_block::dflash::DFlashState;
use crate::{
backends::common::{
Allocation, AllocationType, Backend, Context, Encoder, Kernels,
- kernel::{
- WeaverFrontierScatterKernel, WeaverFrontierSelectKernel,
- weaver::{
- FRONTIER_LANE_COUNT, MAX_FRONTIER_SLOTS, METADATA_LANE_COUNT, NO_WINNER, TREE_LANE_COUNT,
- TREE_LANE_DEPTH, TREE_LANE_LOGPROB, TREE_LANE_MASK, TREE_LANE_PARENT, TREE_LANE_TOKEN,
- },
- },
+ gpu_types::weaver,
+ kernel::{WeaverFrontierScatterKernel, WeaverFrontierSelectKernel},
},
config::speculator::dflash::DFlashSpeculatorConfig,
data_type::DataType,
@@ -163,7 +158,7 @@ impl<B: Backend> DFlashSpeculator<B> {
let lookahead_count = max_depth.min(block_size.saturating_sub(1));
// The frontier holds one slot per (tree slot, child) pair; the select
// kernel silently no-ops past its capacity.
- if (options.budget + 1) * options.children_per_node > MAX_FRONTIER_SLOTS || lookahead_count == 0 {
+ if (options.budget + 1) * options.children_per_node > weaver::FRONTIER_MAX_SLOTS || lookahead_count == 0 {
return Err(DFlashTreeError::InvalidOptions);
}
let target_hidden = state.target_output_norm().ok_or(DFlashTreeError::MissingTargetHidden)?;
@@ -330,16 +325,16 @@ impl<B: Backend> DFlashSpeculator<B> {
let scatter =
<B::Kernels as Kernels>::WeaverFrontierScatterKernel::new(context).map_err(DFlashTreeError::Backend)?;
- let mut tree_values = vec![0u32; TREE_LANE_COUNT * slots];
+ let mut tree_values = vec![0u32; weaver::TREE_LANE_COUNT * slots];
for slot in 0..slots {
- tree_values[TREE_LANE_PARENT * slots + slot] = NO_WINNER;
+ tree_values[weaver::TREE_LANE_PARENT * slots + slot] = weaver::FRONTIER_NO_WINNER;
}
- tree_values[TREE_LANE_TOKEN * slots] = params.bonus_token;
- tree_values[TREE_LANE_MASK * slots] = 1;
+ tree_values[weaver::TREE_LANE_TOKEN * slots] = params.bonus_token;
+ tree_values[weaver::TREE_LANE_MASK * slots] = 1;
let mut tree = encoder.allocate_constant_from_slice(&tree_values).map_err(DFlashTreeError::Backend)?;
let mut frontier = encoder
- .allocate_constant_from_slice(&vec![0u32; FRONTIER_LANE_COUNT * capacity])
+ .allocate_constant_from_slice(&vec![0u32; weaver::FRONTIER_LANE_COUNT * capacity])
.map_err(DFlashTreeError::Backend)?;
let mut slot_ancestors =
encoder.allocate_constant_from_slice(&vec![0u32; slots * stride]).map_err(DFlashTreeError::Backend)?;
@@ -351,7 +346,7 @@ impl<B: Backend> DFlashSpeculator<B> {
let mut round_token_ids =
encoder.allocate_constant_from_slice(&round_token_id_values).map_err(DFlashTreeError::Backend)?;
let mut round_metadata = encoder
- .allocate_constant_from_slice(&vec![0u32; METADATA_LANE_COUNT * width])
+ .allocate_constant_from_slice(&vec![0u32; weaver::METADATA_LANE_COUNT * width])
.map_err(DFlashTreeError::Backend)?;
let mut round_ancestors =
encoder.allocate_constant_from_slice(&vec![0u32; width * stride]).map_err(DFlashTreeError::Backend)?;
@@ -439,16 +434,20 @@ impl<B: Backend> DFlashSpeculator<B> {
}
fn tree_from_slots(tree: &[u32]) -> Vec<HostTreeNode> {
- assert!(tree.len().is_multiple_of(TREE_LANE_COUNT), "tree array must contain {TREE_LANE_COUNT} equal-length lanes");
- let slots = tree.len() / TREE_LANE_COUNT;
+ assert!(
+ tree.len().is_multiple_of(weaver::TREE_LANE_COUNT),
+ "tree array must contain {} equal-length lanes",
+ weaver::TREE_LANE_COUNT
+ );
+ let slots = tree.len() / weaver::TREE_LANE_COUNT;
let lane = |lane: usize, slot: usize| tree[lane * slots + slot];
let mut slot_to_node = vec![usize::MAX; slots];
let mut nodes: Vec<HostTreeNode> = Vec::with_capacity(slots);
for slot in 0..slots {
- if lane(TREE_LANE_MASK, slot) == 0 {
+ if lane(weaver::TREE_LANE_MASK, slot) == 0 {
continue;
}
- let parent_slot = lane(TREE_LANE_PARENT, slot) as i32;
+ let parent_slot = lane(weaver::TREE_LANE_PARENT, slot) as i32;
let parent = (parent_slot >= 0).then(|| {
let parent = slot_to_node[parent_slot as usize];
assert_ne!(parent, usize::MAX, "tree slot {slot} names padding slot {parent_slot} as its parent");
@@ -460,10 +459,10 @@ fn tree_from_slots(tree: &[u32]) -> Vec<HostTreeNode> {
nodes[parent].children.push(index);
}
nodes.push(HostTreeNode {
- token: lane(TREE_LANE_TOKEN, slot),
+ token: lane(weaver::TREE_LANE_TOKEN, slot),
parent,
- depth: lane(TREE_LANE_DEPTH, slot) as usize,
- logprob: f32::from_bits(lane(TREE_LANE_LOGPROB, slot)),
+ depth: lane(weaver::TREE_LANE_DEPTH, slot) as usize,
+ logprob: f32::from_bits(lane(weaver::TREE_LANE_LOGPROB, slot)),
children: Vec::new(),
});
}
diff --git a/crates/backend-uzu/tests/unit/backends/common/kernel/weaver/weaver_frontier_test.rs b/crates/backend-uzu/tests/unit/backends/common/kernel/weaver/weaver_frontier_test.rs
index 38a255c7c..a03fdeaaf 100644
--- a/crates/backend-uzu/tests/unit/backends/common/kernel/weaver/weaver_frontier_test.rs
+++ b/crates/backend-uzu/tests/unit/backends/common/kernel/weaver/weaver_frontier_test.rs
@@ -5,13 +5,8 @@ use crate::{
backends::{
common::{
Backend, Encoder, Kernels,
- kernel::{
- WeaverFrontierScatterKernel, WeaverFrontierSelectKernel,
- weaver::{
- FRONTIER_LANE_COUNT, METADATA_LANE_COUNT, METADATA_LANE_NODE_INDEX, TREE_LANE_COUNT, TREE_LANE_CUM,
- TREE_LANE_DEPTH,
- },
- },
+ gpu_types::weaver,
+ kernel::{WeaverFrontierScatterKernel, WeaverFrontierSelectKernel},
},
cpu::Cpu,
},
@@ -20,7 +15,7 @@ use crate::{
fn select<B: Backend>() -> Vec<u32> {
let context = create_context::<B>();
- let mut frontier = vec![0; FRONTIER_LANE_COUNT * 8];
+ let mut frontier = vec![0; weaver::FRONTIER_LANE_COUNT * 8];
for (slot, (token, parent, depth, cum, key, active)) in [
(9, 1, 1, 0x3f00_0000, 100, 1),
(8, 0, 2, 0x3f00_0001, 100, 1),
@@ -39,7 +34,7 @@ fn select<B: Backend>() -> Vec<u32> {
}
}
let mut frontier = alloc_allocation_with_data::<B, u32>(&context, &frontier);
- let mut tree = alloc_allocation_with_data::<B, u32>(&context, &[55; TREE_LANE_COUNT * 7]);
+ let mut tree = alloc_allocation_with_data::<B, u32>(&context, &[55; weaver::TREE_LANE_COUNT * 7]);
let mut slot_ancestors = alloc_allocation_with_data::<B, u32>(&context, &(0u32..7 * 3).collect::<Vec<_>>());
let mut token = alloc_allocation_with_data::<B, u32>(&context, &[66; 4]);
let mut metadata = alloc_allocation_with_data::<B, u32>(&context, &[77; 3 * 4]);
@@ -85,17 +80,19 @@ fn select<B: Backend>() -> Vec<u32> {
fn scatter<B: Backend>() -> Vec<u32> {
let context = create_context::<B>();
- let mut tree = vec![0; TREE_LANE_COUNT * 4];
- tree[TREE_LANE_CUM * 4..(TREE_LANE_CUM + 1) * 4].copy_from_slice(&[0.5, -1.0, 2.0, 4.0].map(f32::to_bits));
- tree[TREE_LANE_DEPTH * 4..(TREE_LANE_DEPTH + 1) * 4].copy_from_slice(&[0, 2, 4, 6]);
+ let mut tree = vec![0; weaver::TREE_LANE_COUNT * 4];
+ tree[weaver::TREE_LANE_CUM * 4..(weaver::TREE_LANE_CUM + 1) * 4]
+ .copy_from_slice(&[0.5, -1.0, 2.0, 4.0].map(f32::to_bits));
+ tree[weaver::TREE_LANE_DEPTH * 4..(weaver::TREE_LANE_DEPTH + 1) * 4].copy_from_slice(&[0, 2, 4, 6]);
let tree = alloc_allocation_with_data::<B, u32>(&context, &tree);
- let mut metadata = vec![0; METADATA_LANE_COUNT * 3];
- metadata[METADATA_LANE_NODE_INDEX * 3..(METADATA_LANE_NODE_INDEX + 1) * 3].copy_from_slice(&[1, 3, 0]);
+ let mut metadata = vec![0; weaver::METADATA_LANE_COUNT * 3];
+ metadata[weaver::METADATA_LANE_NODE_INDEX * 3..(weaver::METADATA_LANE_NODE_INDEX + 1) * 3]
+ .copy_from_slice(&[1, 3, 0]);
let metadata = alloc_allocation_with_data::<B, u32>(&context, &metadata);
let valid = alloc_allocation_with_data::<B, u32>(&context, &[1, 0, 1]);
let ids = alloc_allocation_with_data::<B, u32>(&context, &(10..19).collect::<Vec<_>>());
let scores = alloc_allocation_with_data::<B, f32>(&context, &[-0.1, -0.2, -0.3, 8.0, 8.0, 8.0, 0.1, 0.2, 0.3]);
- let mut frontier = alloc_allocation_with_data::<B, u32>(&context, &[42; FRONTIER_LANE_COUNT * 16]);
+ let mut frontier = alloc_allocation_with_data::<B, u32>(&context, &[42; weaver::FRONTIER_LANE_COUNT * 16]);
let kernel = <B::Kernels as Kernels>::WeaverFrontierScatterKernel::new(&context).unwrap();
let mut encoder = Encoder::new(context.as_ref()).unwrap();
kernel.encode(&tree, &metadata, &valid, &ids, &scores, &mut frontier, 16, 4, 3, 3, &mut encoder);