A high-performance inference engine for AI models
map_coords cleanup (#972)
CCYehee5ec5708b2b
diff --git a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention.metal b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention.metalindex d61c7029c..3fb235c83 100644--- a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention.metal+++ b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention.metal@@ -7,7 +7,7 @@#include "../../matmul/common/mxu_fragment/ops.h"#include "../../generated/attention.h"#include "../../generated/trie.h"-#include "util.h"+#include "layout.h"using namespace metal;using namespace uzu::matmul;@@ -231,14 +231,15 @@ KERNEL(AttentionGemmGrouped)(const uint key_base = first_key + kb * uint(BK);const bool masked_block = IS_TAIL || ((is_causal || is_trie) && (key_base + uint(BK) > prefix_length));if (masked_block) {- map_lane_rows(score, score_position, [&](ushort row, short col, AccumType value) {+ score.map_coords(lane, [&](short row, short col, AccumType value) UZU_ALWAYS_INLINE {+ const ushort lane_row = ushort(row - score_position.y) / Ops::THREAD_ELEMENT_ROW_STRIDE;const uint key = key_base + uint(col);bool visible = key < key_length;if (is_trie) {- visible =- visible && (key < prefix_length || ((row_trie_mask[row] >> ulong(key - prefix_length)) & 1ul) != 0ul);+ visible = visible &&+ (key < prefix_length || ((row_trie_mask[lane_row] >> ulong(key - prefix_length)) & 1ul) != 0ul);} else if (is_causal) {- visible = visible && (key <= last_visible_key[row]);+ visible = visible && (key <= last_visible_key[lane_row]);}return visible ? value : masked_score_pre_scale;});diff --git a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention_int8.metal b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention_int8.metalindex 8fe859f61..1875a3f08 100644--- a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention_int8.metal+++ b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/attention_int8.metal@@ -6,7 +6,7 @@#include "../../generated/attention.h"#include "../../generated/trie.h"#include "../kv_layout.h"-#include "util.h"+#include "layout.h"using namespace metal;using namespace uzu::matmul;@@ -348,19 +348,14 @@ KERNEL(AttentionGemmGroupedInt8)();if (!mask_free && (is_tail_block || (key_base + uint(uzu::kv_layout::TILE_KEYS) > prefix_length))) {- ulong row_mask[ROWS_PER_LANE];- METAL_PRAGMA_UNROLL- for (ushort row = 0; row < ROWS_PER_LANE; ++row) {- row_mask[row] = row_masks[query_band_offset + lane_rows[row]];- }- map_lane_rows(scores, score_position, [&](ushort lane_row, short column, AccumType value) UZU_ALWAYS_INLINE {+ scores.map_coords(lane, [&](short row, short column, AccumType value) UZU_ALWAYS_INLINE {+ const ulong row_mask = row_masks[query_band_offset + uint(row)];const uint key = key_base + uint(column);bool visible = key < key_length;if (is_trie) {- visible =- visible && (key < prefix_length || ((row_mask[lane_row] >> ulong(key - prefix_length)) & 1ul) != 0ul);+ visible = visible && (key < prefix_length || ((row_mask >> ulong(key - prefix_length)) & 1ul) != 0ul);} else {- visible = visible && key <= uint(row_mask[lane_row]);+ visible = visible && key <= uint(row_mask);}return visible ? value : -metal::numeric_limits<AccumType>::infinity();});diff --git a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/layout.h b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/layout.hnew file mode 100644index 000000000..a9d312840--- /dev/null+++ b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/layout.h@@ -0,0 +1,23 @@+#pragma once++#include <metal_stdlib>+#include "../../common/defines.h"+#include "../../common/dsl.h"+#include "../../matmul/common/mxu_fragment/ops.h"++using namespace metal;++#define ATTENTION_GQA_SIMDGROUPS 8++template <uint BK, uint BD, uint D_SLICES>+struct AttentionGemmGroupedLayout {+ using Ops = uzu::matmul::MxuFragmentOps<>;++ UZU_CONST int SIMDGROUPS = ATTENTION_GQA_SIMDGROUPS;+ UZU_CONST int TILE_ROWS = Ops::FRAGMENT_ROWS;+ UZU_CONST int SLICE_COLS = int(BD) / int(D_SLICES);+ UZU_CONST int BANDS = SIMDGROUPS / int(D_SLICES);+ UZU_CONST int BLOCK_ROWS = BANDS * TILE_ROWS;+ UZU_CONST int SCORE_TILE_ELEMENTS = TILE_ROWS * int(BK);+ UZU_CONST int SCORE_EXCHANGE_SIZE = SIMDGROUPS * SCORE_TILE_ELEMENTS;+};diff --git a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/util.h b/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/util.hdeleted file mode 100644index 81777cea1..000000000--- a/crates/uzu-engine/src/backends/metal/kernel/attention/gemm_grouped/util.h+++ /dev/null@@ -1,50 +0,0 @@-#pragma once--#include <metal_stdlib>-#include "../../common/defines.h"-#include "../../common/dsl.h"-#include "../../matmul/common/mxu_fragment/ops.h"--using namespace metal;--#define ATTENTION_GQA_SIMDGROUPS 8--template <uint BK, uint BD, uint D_SLICES>-struct AttentionGemmGroupedLayout {- using Ops = uzu::matmul::MxuFragmentOps<>;-- UZU_CONST int SIMDGROUPS = ATTENTION_GQA_SIMDGROUPS;- UZU_CONST int TILE_ROWS = Ops::FRAGMENT_ROWS;- UZU_CONST int SLICE_COLS = int(BD) / int(D_SLICES);- UZU_CONST int BANDS = SIMDGROUPS / int(D_SLICES);- UZU_CONST int BLOCK_ROWS = BANDS * TILE_ROWS;- UZU_CONST int SCORE_TILE_ELEMENTS = TILE_ROWS * int(BK);- UZU_CONST int SCORE_EXCHANGE_SIZE = SIMDGROUPS * SCORE_TILE_ELEMENTS;-};--// `fn(row_in_lane, column, value)` over one lane's elements of `tile`.-template <class FragmentType, class Fn>-METAL_FUNC void map_lane_rows(thread FragmentType& tile, const short2 position, Fn fn) {- using Ops = typename FragmentType::FragmentOpsType;- using Element = typename FragmentType::ElementType;- static_assert(FragmentType::ROW_FRAGMENTS == 1, "requires one row fragment");- static_assert(- Ops::THREAD_ELEMENT_ROWS * Ops::THREAD_ELEMENT_COLS == Ops::ELEMENTS_PER_THREAD,- "requires row-major thread elements"- );-- thread Element* data = tile.elements();- METAL_PRAGMA_UNROLL- for (ushort fragment_col = 0; fragment_col < FragmentType::COL_FRAGMENTS; ++fragment_col) {- const ushort fragment_base = fragment_col * Ops::ELEMENTS_PER_THREAD;- const short col_base = position.x + short(fragment_col) * short(Ops::FRAGMENT_COLS);- METAL_PRAGMA_UNROLL- for (ushort row = 0; row < Ops::THREAD_ELEMENT_ROWS; ++row) {- METAL_PRAGMA_UNROLL- for (ushort col = 0; col < Ops::THREAD_ELEMENT_COLS; ++col) {- const ushort element = fragment_base + row * Ops::THREAD_ELEMENT_COLS + col;- data[element] = Element(fn(row, col_base + short(col), data[element]));- }- }- }-}