diff --git a/Voxt/NOTICE b/Voxt/NOTICE index efb04949ef6..42d6df71835 100644 --- a/Voxt/NOTICE +++ b/Voxt/NOTICE @@ -129,9 +129,8 @@ The Omni native runtime (VoxtNew.app/Contents/Resources/omni) bin/qwen3_asr_server (and its copy bin/silero_vad_server) is built from sglang-omni's sglang_omni_mlx/native (this repository, Apache License 2.0) by scripts/build-omni.sh. It carries, linked in or beside it: - - MLX 0.32.5 at commit fc344c9 (libmlx.dylib, libjaccl.dylib, - mlx.metallib), Copyright 2023 Apple Inc., MIT - (LICENSES/omni/mlx.LICENSE.txt) + - MLX 0.32.5 (libmlx.dylib, libjaccl.dylib, mlx.metallib), Copyright + 2023 Apple Inc., MIT (LICENSES/omni/mlx.LICENSE.txt) - CivetWeb 1.16, Copyright 2013-2021 the CivetWeb developers, 2004-2013 Sergey Lyubka, MIT (LICENSES/omni/civetweb.LICENSE.md; only its own licence applies: none of the optional parts it lists is built) diff --git a/sglang_omni_mlx/native/CMakeLists.txt b/sglang_omni_mlx/native/CMakeLists.txt index c4e2ee3f128..72847218c03 100644 --- a/sglang_omni_mlx/native/CMakeLists.txt +++ b/sglang_omni_mlx/native/CMakeLists.txt @@ -7,8 +7,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -# Note (XXXXRT666): the version of the MLX commit scripts/build_runtime.sh pins; -# MLX's version does not tell commits of its main branch apart. +# Note (XXXXRT666): the MLX release scripts/build_runtime.sh builds. set(SGLANG_OMNI_MLX_VERSION 0.32.5) if(NOT MLX_DIR OR NOT EXISTS "${MLX_DIR}/MLXConfig.cmake") message(FATAL_ERROR "Set MLX_DIR to /share/cmake/MLX: scripts/build_runtime.sh builds it") diff --git a/sglang_omni_mlx/native/scripts/build_runtime.sh b/sglang_omni_mlx/native/scripts/build_runtime.sh index 037009ff86b..e7ddaacff32 100755 --- a/sglang_omni_mlx/native/scripts/build_runtime.sh +++ b/sglang_omni_mlx/native/scripts/build_runtime.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash # Builds the native Qwen3-ASR runtime into a self-contained directory. -# Note (XXXXRT666): MLX is built from a pinned main commit, since no release has -# ml-explore/mlx#4791 yet, with scripts/mlx-patches applied in name order: each -# is the MLX source change of the ml-explore/mlx pull request its name starts -# with. The build is kept per commit and patch set under $work/mlx. +# Note (XXXXRT666): MLX is built from the source of its v0.32.5 release, with +# scripts/mlx-patches applied in name order: each is the MLX source change of +# the ml-explore/mlx pull request its name starts with. The build is kept per +# commit and patch set under $work/mlx. set -euo pipefail output="${1:?usage: build_runtime.sh []}" @@ -14,7 +14,7 @@ test "$(uname -m)" = arm64 || { echo "Apple Silicon is required" >&2; exit 1; } command -v uv >/dev/null || { echo "uv is required" >&2; exit 1; } deployment_target="${MACOSX_DEPLOYMENT_TARGET:-15.0}" -mlx_commit=fc344c986c4169d99f76b5d95ae6b4a58a359a63 +mlx_commit=cfc675e9e59cb6ee29a647cf8fe87ef844eff879 # v0.32.5 shopt -s nullglob mlx_patches=("$native_dir"/scripts/mlx-patches/*.patch) shopt -u nullglob diff --git a/sglang_omni_mlx/native/scripts/mlx-patches/4852-reuse-in-flight-metal-buffers.patch b/sglang_omni_mlx/native/scripts/mlx-patches/4852-reuse-in-flight-metal-buffers.patch index 65e9d67023c..f32e19b590f 100644 --- a/sglang_omni_mlx/native/scripts/mlx-patches/4852-reuse-in-flight-metal-buffers.patch +++ b/sglang_omni_mlx/native/scripts/mlx-patches/4852-reuse-in-flight-metal-buffers.patch @@ -1,5 +1,5 @@ diff --git a/mlx/allocator.h b/mlx/allocator.h -index 82bc874..a26f3e4 100644 +index 760bf2086..98a360742 100644 --- a/mlx/allocator.h +++ b/mlx/allocator.h @@ -53,6 +53,12 @@ class MLX_API Allocator { @@ -15,9 +15,9 @@ index 82bc874..a26f3e4 100644 virtual Buffer make_buffer(void* ptr, size_t size) { return Buffer{nullptr}; }; -@@ -85,6 +91,23 @@ class MLX_API Allocator { - - MLX_API Allocator& allocator(); +@@ -88,6 +94,23 @@ MLX_API Allocator& allocator(); + // Whether the active memory is above the memory limit. + MLX_API bool over_memory_limit(); +class AllocationScope { + public: @@ -40,7 +40,7 @@ index 82bc874..a26f3e4 100644 // The allocator owns data from malloc diff --git a/mlx/array.h b/mlx/array.h -index 59ab941..bae6993 100644 +index 59ab94194..bae6993ec 100644 --- a/mlx/array.h +++ b/mlx/array.h @@ -284,7 +284,8 @@ class MLX_API array { @@ -62,7 +62,7 @@ index 59ab941..bae6993 100644 switch (dtype()) { case bool_: diff --git a/mlx/backend/common/common.cpp b/mlx/backend/common/common.cpp -index 81dc5c1..8d0009c 100644 +index e317fe858..97add8ede 100644 --- a/mlx/backend/common/common.cpp +++ b/mlx/backend/common/common.cpp @@ -88,6 +88,7 @@ void ExpandDims::eval(const std::vector& inputs, array& out) { @@ -74,7 +74,7 @@ index 81dc5c1..8d0009c 100644 double numel = 1; diff --git a/mlx/backend/cpu/eval.cpp b/mlx/backend/cpu/eval.cpp -index 354820f..d02efe1 100644 +index 354820f0f..d02efe113 100644 --- a/mlx/backend/cpu/eval.cpp +++ b/mlx/backend/cpu/eval.cpp @@ -22,6 +22,7 @@ void clear_streams() { @@ -86,10 +86,10 @@ index 354820f..d02efe1 100644 auto outputs = arr.outputs(); diff --git a/mlx/backend/metal/allocator.cpp b/mlx/backend/metal/allocator.cpp -index ff12293..55e771f 100644 +index 80a3f72ba..ddf442433 100644 --- a/mlx/backend/metal/allocator.cpp +++ b/mlx/backend/metal/allocator.cpp -@@ -42,6 +42,14 @@ bool can_reuse_alien_buffer(void* ptr) { +@@ -43,6 +43,14 @@ bool can_reuse_alien_buffer(void* ptr) { namespace metal { @@ -104,7 +104,7 @@ index ff12293..55e771f 100644 MetalAllocator::MetalAllocator(Device& d) : device_(d.mtl_device()), residency_sets_(d.residency_sets()), -@@ -127,6 +135,20 @@ Buffer MetalAllocator::malloc(size_t size) { +@@ -129,6 +137,20 @@ Buffer MetalAllocator::malloc(size_t size) { // Try the cache std::unique_lock lk(mutex_); @@ -124,8 +124,8 @@ index ff12293..55e771f 100644 + } MTL::Buffer* buf = buffer_cache_.reuse_from_cache(size); if (!buf) { - size_t mem_required = get_active_memory() + get_cache_memory() + size; -@@ -190,11 +212,15 @@ void MetalAllocator::free(Buffer buffer) { + auto cache_size = buffer_cache_.cache_size(); +@@ -198,11 +220,15 @@ void MetalAllocator::free(Buffer buffer) { return; } @@ -140,10 +140,10 @@ index ff12293..55e771f 100644 #endif - - std::unique_lock lk(mutex_); + assert(buf->length() <= active_memory_); active_memory_ -= buf->length(); - if (get_cache_memory() < max_pool_size_) { - buffer_cache_.recycle_to_cache(buf); -@@ -209,6 +235,78 @@ void MetalAllocator::free(Buffer buffer) { + if (buffer_cache_.cache_size() < max_pool_size_) { +@@ -218,6 +244,78 @@ void MetalAllocator::free(Buffer buffer) { } } @@ -220,10 +220,10 @@ index ff12293..55e771f 100644 +} + size_t MetalAllocator::size(Buffer buffer) const { - return static_cast(buffer.ptr())->length(); - } + if (const auto* p = buffer.ptr(); p) { + return static_cast(p)->length(); diff --git a/mlx/backend/metal/allocator.h b/mlx/backend/metal/allocator.h -index 8859513..572a950 100644 +index 251d67a2e..faf14b91a 100644 --- a/mlx/backend/metal/allocator.h +++ b/mlx/backend/metal/allocator.h @@ -20,6 +20,8 @@ class MetalAllocator : public allocator::Allocator { @@ -235,7 +235,7 @@ index 8859513..572a950 100644 virtual Buffer make_buffer(void* ptr, size_t size) override; virtual void release(Buffer buffer) override; -@@ -41,8 +43,20 @@ class MetalAllocator : public allocator::Allocator { +@@ -48,8 +50,20 @@ class MetalAllocator : public allocator::Allocator { size_t get_memory_limit(); size_t set_wired_limit(size_t limit); void clear_cache(); @@ -256,7 +256,7 @@ index 8859513..572a950 100644 MTL::Device* device_; // The size of allocations which go on the heap until it is full. This size -@@ -61,6 +75,8 @@ class MetalAllocator : public allocator::Allocator { +@@ -68,6 +82,8 @@ class MetalAllocator : public allocator::Allocator { // Caching allocator BufferCache buffer_cache_; @@ -266,7 +266,7 @@ index 8859513..572a950 100644 // Allocation stats size_t block_limit_; diff --git a/mlx/backend/metal/device.cpp b/mlx/backend/metal/device.cpp -index 6c0c92f..9151efc 100644 +index 100b74ae4..f98a3c973 100644 --- a/mlx/backend/metal/device.cpp +++ b/mlx/backend/metal/device.cpp @@ -15,6 +15,7 @@ @@ -538,10 +538,10 @@ index 6c0c92f..9151efc 100644 #endif diff --git a/mlx/backend/metal/device.h b/mlx/backend/metal/device.h -index ad5a1d4..ab9da8c 100644 +index c0ff6a3c5..a53cad0a5 100644 --- a/mlx/backend/metal/device.h +++ b/mlx/backend/metal/device.h -@@ -35,8 +35,9 @@ class MLX_API CommandEncoder { +@@ -36,8 +36,9 @@ class MLX_API CommandEncoder { } ~ConcurrentContext() { enc.concurrent_ = false; @@ -553,7 +553,7 @@ index ad5a1d4..ab9da8c 100644 enc.concurrent_outputs_.clear(); } -@@ -48,6 +49,7 @@ class MLX_API CommandEncoder { +@@ -49,6 +50,7 @@ class MLX_API CommandEncoder { void set_input_array(const array& a, int idx, int64_t offset = 0); void set_output_array(array& a, int idx, int64_t offset = 0); void register_output_array(const array& a); @@ -561,7 +561,7 @@ index ad5a1d4..ab9da8c 100644 void add_temporary(array arr); void add_temporaries(std::vector arrays); -@@ -101,10 +103,7 @@ class MLX_API CommandEncoder { +@@ -102,10 +104,7 @@ class MLX_API CommandEncoder { void commit(std::function completion = nullptr); void synchronize(); #ifndef NDEBUG @@ -573,7 +573,7 @@ index ad5a1d4..ab9da8c 100644 #endif MTL::CommandBuffer* get_command_buffer() const { -@@ -115,6 +114,7 @@ class MLX_API CommandEncoder { +@@ -116,6 +115,7 @@ class MLX_API CommandEncoder { MTL::ComputeCommandEncoder* get_command_encoder(); Device& device_; @@ -581,7 +581,7 @@ index ad5a1d4..ab9da8c 100644 bool exiting_{false}; // Buffer that stores encoded commands. -@@ -122,6 +122,8 @@ class MLX_API CommandEncoder { +@@ -123,6 +123,8 @@ class MLX_API CommandEncoder { NS::SharedPtr buffer_; int buffer_ops_{0}; size_t buffer_sizes_{0}; @@ -590,7 +590,7 @@ index ad5a1d4..ab9da8c 100644 // The residency set and how many of its sets this queue has attached. ResidencySets& residency_sets_; -@@ -141,20 +143,23 @@ class MLX_API CommandEncoder { +@@ -142,20 +144,23 @@ class MLX_API CommandEncoder { NS::SharedPtr fence_; bool needs_barrier_{false}; bool concurrent_{false}; @@ -619,7 +619,7 @@ index ad5a1d4..ab9da8c 100644 }; diff --git a/mlx/backend/metal/eval.cpp b/mlx/backend/metal/eval.cpp -index b53feae..5a37187 100644 +index b53feaea4..5a371879d 100644 --- a/mlx/backend/metal/eval.cpp +++ b/mlx/backend/metal/eval.cpp @@ -2,6 +2,7 @@ @@ -682,7 +682,7 @@ index b53feae..5a37187 100644 } diff --git a/mlx/backend/metal/fence.cpp b/mlx/backend/metal/fence.cpp -index 3c133e5..a75ba21 100644 +index 3c133e589..a75ba21ff 100644 --- a/mlx/backend/metal/fence.cpp +++ b/mlx/backend/metal/fence.cpp @@ -21,6 +21,7 @@ struct FenceImpl { @@ -694,7 +694,7 @@ index 3c133e5..a75ba21 100644 cpu_value()[0] = 0; } diff --git a/mlx/backend/metal/fft.cpp b/mlx/backend/metal/fft.cpp -index 952f829..4a3f934 100644 +index 100dc4fa6..4e2181b21 100644 --- a/mlx/backend/metal/fft.cpp +++ b/mlx/backend/metal/fft.cpp @@ -281,6 +281,7 @@ int primitive_root(int n) { diff --git a/sglang_omni_mlx/native/scripts/mlx-patches/4860-affine-qmv-mma-2-3-5-6-bits.patch b/sglang_omni_mlx/native/scripts/mlx-patches/4860-affine-qmv-mma-2-3-5-6-bits.patch deleted file mode 100644 index 2658655b44c..00000000000 --- a/sglang_omni_mlx/native/scripts/mlx-patches/4860-affine-qmv-mma-2-3-5-6-bits.patch +++ /dev/null @@ -1,156 +0,0 @@ -diff --git a/mlx/backend/metal/kernels/quantized.h b/mlx/backend/metal/kernels/quantized.h -index 5357baf..4ff8657 100644 ---- a/mlx/backend/metal/kernels/quantized.h -+++ b/mlx/backend/metal/kernels/quantized.h -@@ -1932,26 +1932,27 @@ template - static_assert(BN % 8 == 0, "BN should be divisible by 8"); - static_assert(TM == 1 || TM == 2, "qmv_mma takes at most 16 rows"); - static_assert( -- (bits == 4 || bits == 8) && -+ (bits >= 2 && bits <= 8 && bits != 7) && - (group_size == 32 || group_size == 64 || group_size == 128), -- "qmv_mma reads 4 and 8 bits at group sizes 32, 64 and 128"); -+ "qmv_mma reads 2 to 8 bits (not 7) at group sizes 32, 64 and 128"); - - using Frag = mlx::steel::BaseMMAFrag; - using frag_t = typename Frag::frag_type; - constexpr int BK = 128; - constexpr short TN = BN / 8; -- constexpr int pack_factor = 8 / bits; -- constexpr short loads = bits / 4; -+ // The lane's 32 values take bits words -+ constexpr short words = bits; -+ constexpr uint mask = (1 << bits) - 1; - - const short2 coord = Frag::get_coord(simd_lid); - const short fn = coord.x; - const short fm = coord.y; -- const int row_bytes = K / pack_factor; -+ const int row_bytes = K / 8 * bits; - const int row_scales = K / group_size; - - const int y_col = tid.x * BN; - auto wl = (const device uint8_t*)w + -- (y_col + fm) * static_cast(row_bytes) + 16 * fn / pack_factor; -+ (y_col + fm) * static_cast(row_bytes) + 2 * fn * bits; - const int64_t s_off = - (y_col + fm) * static_cast(row_scales) + 16 * fn / group_size; - scales += s_off; -@@ -1986,16 +1987,34 @@ template - } - } - } -- uint4 ws[TN][loads]; -+ uint ws[TN][words]; - float sc[TN]; - float bs[TN]; - STEEL_PRAGMA_UNROLL - for (short t = 0; t < TN; t++) { - const device uint8_t* wt = -- wl + 8 * t * static_cast(row_bytes) + k / pack_factor; -- STEEL_PRAGMA_UNROLL -- for (short l = 0; l < loads; l++) { -- ws[t][l] = *(const device uint4*)(wt + 16 * l); -+ wl + 8 * t * static_cast(row_bytes) + k / 8 * bits; -+ if constexpr (words % 4 == 0) { -+ STEEL_PRAGMA_UNROLL -+ for (short l = 0; l < words; l += 4) { -+ const uint4 v = *(const device uint4*)(wt + 4 * l); -+ ws[t][l] = v.x; -+ ws[t][l + 1] = v.y; -+ ws[t][l + 2] = v.z; -+ ws[t][l + 3] = v.w; -+ } -+ } else if constexpr (words % 2 == 0) { -+ STEEL_PRAGMA_UNROLL -+ for (short l = 0; l < words; l += 2) { -+ const uint2 v = *(const device uint2*)(wt + 4 * l); -+ ws[t][l] = v.x; -+ ws[t][l + 1] = v.y; -+ } -+ } else { -+ STEEL_PRAGMA_UNROLL -+ for (short l = 0; l < words; l++) { -+ ws[t][l] = ((const device uint*)wt)[l]; -+ } - } - const int64_t si = - 8 * t * static_cast(row_scales) + k / group_size; -@@ -2021,19 +2040,37 @@ template - // each half reads as 1024 + q, and taking 1024 off again is exact. - uint p[4]; - if constexpr (bits == 4) { -- const uint lo = ws[t][0][g / 2]; -- const uint hi = ws[t][0][2 + g / 2]; -+ const uint lo = ws[t][g / 2]; -+ const uint hi = ws[t][2 + g / 2]; - const uint q = (g % 2) == 0 ? (lo & 0xffff) | (hi << 16) - : (lo >> 16) | (hi & 0xffff0000); - STEEL_PRAGMA_UNROLL - for (short i = 0; i < 4; i++) { - p[i] = (q >> (4 * i)) & 0x000f000f; - } -+ } else if constexpr (bits == 8) { -+ STEEL_PRAGMA_UNROLL -+ for (short i = 0; i < 4; i++) { -+ p[i] = ((ws[t][g] >> (8 * i)) & 0xff) | -+ (((ws[t][4 + g] >> (8 * i)) & 0xff) << 16); -+ } - } else { -+ // Values 4g to 4g+3 and 16+4g to 16+4g+3, which can span two words -+ uint q[2]; -+ STEEL_PRAGMA_UNROLL -+ for (short h = 0; h < 2; h++) { -+ const short bit = (16 * h + 4 * g) * bits; -+ const short word = bit / 32; -+ const short shift = bit % 32; -+ q[h] = ws[t][word] >> shift; -+ if (shift + 4 * bits > 32) { -+ q[h] |= ws[t][word + 1] << (32 - shift); -+ } -+ } - STEEL_PRAGMA_UNROLL - for (short i = 0; i < 4; i++) { -- p[i] = ((ws[t][0][g] >> (8 * i)) & 0xff) | -- (((ws[t][1][g] >> (8 * i)) & 0xff) << 16); -+ p[i] = ((q[0] >> (bits * i)) & mask) | -+ (((q[1] >> (bits * i)) & mask) << 16); - } - } - STEEL_PRAGMA_UNROLL -diff --git a/mlx/backend/metal/kernels/quantized.metal b/mlx/backend/metal/kernels/quantized.metal -index fa945c9..d33f3dd 100644 ---- a/mlx/backend/metal/kernels/quantized.metal -+++ b/mlx/backend/metal/kernels/quantized.metal -@@ -237,7 +237,11 @@ - instantiate_quantized_all(type, group_size, 5) \ - instantiate_quantized_all(type, group_size, 6) \ - instantiate_quantized_all(type, group_size, 8) \ -+ instantiate_qmv_mma_all(type, group_size, 2) \ -+ instantiate_qmv_mma_all(type, group_size, 3) \ - instantiate_qmv_mma_all(type, group_size, 4) \ -+ instantiate_qmv_mma_all(type, group_size, 5) \ -+ instantiate_qmv_mma_all(type, group_size, 6) \ - instantiate_qmv_mma_all(type, group_size, 8) - - #define instantiate_quantized_all_groups(type) \ -diff --git a/mlx/backend/metal/quantized.cpp b/mlx/backend/metal/quantized.cpp -index f41c2fd..f748602 100644 ---- a/mlx/backend/metal/quantized.cpp -+++ b/mlx/backend/metal/quantized.cpp -@@ -669,10 +669,11 @@ inline bool use_qmv_mma( - if (d.get_architecture().back() == 'd') { - return false; - } -- // Only 4 and 8 bits are instantiated; affine pays off from a smaller N * -- // K than fp. -- return (bits == 4 || bits == 8) && (N >= 512) && (K >= 512) && -- (int64_t(N) * K >= (int64_t(1) << 21)) && (N % 16 == 0); -+ // qmv_mma reads 2 to 8 bits (not 7). Affine pays off from a smaller -+ // N * K than fp, and from 2^20 on gen 15+ (M4). -+ int nk_log2 = d.get_architecture_gen() >= 15 ? 20 : 21; -+ return (bits >= 2 && bits != 7) && (N >= 512) && (K >= 512) && -+ (int64_t(N) * K >= (int64_t(1) << nk_log2)) && (N % 16 == 0); - } - } else { // fp quantization - if (nax) {