A high-performance inference engine for AI models
update coms
eugenef17016d1ac1a
diff --git a/crates/uzu/src/backends/metal/context.rs b/crates/uzu/src/backends/metal/context.rsindex 659c8d03e..c5b7d08f0 100644--- a/crates/uzu/src/backends/metal/context.rs+++ b/crates/uzu/src/backends/metal/context.rs@@ -1,7 +1,7 @@#![allow(dead_code)]include!(concat!(env!("OUT_DIR"), "/metal_lib.rs"));-use std::{cell::RefCell, collections::HashMap, rc::Rc, env};+use std::{cell::RefCell, collections::HashMap, env, rc::Rc};use metal::{CommandQueue as MTLCommandQueue,@@ -98,15 +98,8 @@ impl DeviceArchitecture {}}- fn parse_architecture_info(- arch: &str,- ) -> (DeviceGeneration, DeviceClass) {- // Try to extract from GPU architecture string if available- // MLX uses device_->architecture()->name() which gives "applegpu_gXXY"- // where XX is generation and Y is class (p/g/d)- // The metal crate's device.name() gives the marketing name like "Apple M3 Pro"-- // For now, we'll use a heuristic based on the device name+ fn parse_architecture_info(arch: &str) -> (DeviceGeneration, DeviceClass) {+ // Parse generation and class from device name (e.g. "Apple M3 Pro")let generation = if arch.contains("M4") {DeviceGeneration::Gen17} else if arch.contains("M3") {@@ -205,7 +198,7 @@ impl MTLContext {self.architecture.device_class}- /// TF32 toggle similar to MLX env::enable_tf32().+ /// TF32 toggle via UZU_TF32 environment variable.pub fn tf32_enabled(&self) -> bool {env::var("UZU_TF32").map(|v| v == "1" || v.eq_ignore_ascii_case("true"))diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal b/crates/uzu/src/backends/metal/kernel/matmul/gemv.metalindex c3ff84e4f..1b2ff8cc5 100644--- a/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemv.metal@@ -94,9 +94,9 @@ DECL_GEMV(gemv_bf16_rows8, bfloat, float, 8, 128)#include <metal_stdlib>using namespace metal;-#define MLX_MTL_PRAGMA_UNROLL _Pragma("unroll")+#define UZU_PRAGMA_UNROLL _Pragma("unroll")-// Simplified MLX-style GEMV without bias/axpby; compact signature.+// Vectorized GEMV without bias/axpby; compact signature.template <typename T,const int BM,@@ -123,12 +123,12 @@ struct GEMVKernel {const int src_offset,const int src_size) {if (src_offset + TN <= src_size) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tn = 0; tn < TN; tn++) {dst[tn] = static_cast<U>(src[src_offset + tn]);}} else {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tn = 0; tn < TN; tn++) {dst[tn] = src_offset + tn < src_size? static_cast<U>(src[src_offset + tn])@@ -183,10 +183,10 @@ struct GEMVKernel {load_safe<float>(in_vec, v_coeff, bn, TN);int mat_offset = 0;- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {load_safe(mat, inter, mat_offset + bn, TN);- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tn = 0; tn < TN; tn++) {result[tm] += inter[tn] * v_coeff[tn];}@@ -199,19 +199,19 @@ struct GEMVKernel {if (leftover > 0) {load_safe<float>(in_vec, v_coeff, bn, in_size);- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {load_safe(&mat[tm * matrix_ld], inter, bn, in_size);- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tn = 0; tn < TN; tn++) {result[tm] += inter[tn] * v_coeff[tn];}}}- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (ushort sn = (SN / 2); sn >= 1; sn >>= 1) {result[tm] += simd_shuffle_down(result[tm], sn);}@@ -220,7 +220,7 @@ struct GEMVKernel {if (BN > 1) {threadgroup float* tgp_results = tgp_memory + sgN * (blockM + TM) + bm;if (thrN == 0) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {tgp_results[tm] = result[tm];}@@ -228,9 +228,9 @@ struct GEMVKernel {threadgroup_barrier(mem_flags::mem_none);if (sgN == 0) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int sgn = 1; sgn < BN; sgn++) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {result[tm] += tgp_results[sgn * (blockM + TM) + tm];}@@ -240,7 +240,7 @@ struct GEMVKernel {}if (simdN == 0 && thrN == 0) {- MLX_MTL_PRAGMA_UNROLL+ UZU_PRAGMA_UNROLLfor (int tm = 0; tm < TM; tm++) {out_vec[out_row + tm] = static_cast<T>(result[tm]);}diff --git a/crates/uzu/src/backends/metal/kernel/matmul/gemv_masked.metal b/crates/uzu/src/backends/metal/kernel/matmul/gemv_masked.metalindex c6bbe437b..e36180018 100644--- a/crates/uzu/src/backends/metal/kernel/matmul/gemv_masked.metal+++ b/crates/uzu/src/backends/metal/kernel/matmul/gemv_masked.metal@@ -1,6 +1,5 @@-// Masked and gather GEMV variants (simplified). Masks/indices are honored only-// for output write; data loads remain dense. This keeps parity with MLX kernel-// signatures while avoiding redundant specialization.+// Masked and gather GEMV variants. Masks/indices are honored only for output+// write; data loads remain dense.#include <metal_stdlib>using namespace metal;diff --git a/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs b/crates/uzu/src/backends/metal/kernel/matmul/kernel.rsindex 2e2f99419..76a7a03b6 100644--- a/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs+++ b/crates/uzu/src/backends/metal/kernel/matmul/kernel.rs@@ -146,7 +146,7 @@ impl MatmulKernel {let large_mat =(batch_size_out as i64) * (m as i64) * (n as i64) >= (1_i64 << 20);- // Prefer NAX tiles when available (mirrors MLX fused_nax path).+ // Prefer NAX tiles when available on M4+ hardware.if mtl.is_nax_available()&& (!matches!(self.dt, DataType::F32) || mtl.tf32_enabled()){@@ -368,7 +368,7 @@ impl MatmulKernel {// Paramslet tiles_n = (n + bn - 1) / bn;let tiles_m = (m + bm - 1) / bm;- // Swizzle like MLX: small tm -> no swizzle, otherwise simple 2-way.+ // Swizzle: small tm -> no swizzle, otherwise simple 2-way.let swizzle_log = if tiles_m <= 3 {0} else {diff --git a/crates/uzu/src/backends/metal/kernel/matmul/matmul.metal b/crates/uzu/src/backends/metal/kernel/matmul/matmul.metalindex de3e3dd5e..f1b70c726 100644--- a/crates/uzu/src/backends/metal/kernel/matmul/matmul.metal+++ b/crates/uzu/src/backends/metal/kernel/matmul/matmul.metal@@ -1,6 +1,5 @@// SPDX-License-Identifier: MIT// General matrix multiplication kernels for uzu-// Adapted from MLX steel GEMM implementation#include <metal_stdlib>#include "gemm.h"