diff --git a/ggml/src/ggml-sycl/common.hpp b/ggml/src/ggml-sycl/common.hpp index 11fe86cc0..5e904bd2a 100644 --- a/ggml/src/ggml-sycl/common.hpp +++ b/ggml/src/ggml-sycl/common.hpp @@ -26,7 +26,6 @@ #include "presets.hpp" #include "type.hpp" #include "sycl_hw.hpp" -#include "fattn-buffers.hpp" #include "memtrace.hpp" namespace syclexp = sycl::ext::oneapi::experimental; @@ -408,8 +407,6 @@ struct ggml_backend_sycl_context { // pool std::unique_ptr pools[GGML_SYCL_MAX_DEVICES]; - std::unique_ptr fattn_bufs[GGML_SYCL_MAX_DEVICES]; - std::unique_ptr host_pools[GGML_SYCL_MAX_DEVICES]; std::vector mmid_row_mapping_host; @@ -418,8 +415,6 @@ struct ggml_backend_sycl_context { static std::unique_ptr new_pool_for_host(queue_ptr qptr, int device); - static std::unique_ptr new_fattn_kv_buffers(queue_ptr qptr, int device); - ggml_sycl_pool & pool(int device) { if (pools[device] == nullptr) { pools[device] = new_pool_for_device(stream(device,0), device); @@ -431,17 +426,6 @@ struct ggml_backend_sycl_context { return pool(device); } - ggml_sycl_fattn_kv_buffers & fattn_buffers(int device) { - if (fattn_bufs[device] == nullptr) { - fattn_bufs[device] = new_fattn_kv_buffers(stream(device, 0), device); - } - return *fattn_bufs[device]; - } - - ggml_sycl_fattn_kv_buffers & fattn_buffers() { - return fattn_buffers(device); - } - #ifdef GGML_SYCL_GRAPH std::unique_ptr> exec_graph = nullptr; #endif diff --git a/ggml/src/ggml-sycl/fattn-buffers.cpp b/ggml/src/ggml-sycl/fattn-buffers.cpp deleted file mode 100644 index 78a52d2ab..000000000 --- a/ggml/src/ggml-sycl/fattn-buffers.cpp +++ /dev/null @@ -1,60 +0,0 @@ -// -// MIT license -// Copyright (C) 2025 Intel Corporation -// SPDX-License-Identifier: MIT -// - -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// - -#include "common.hpp" - -sycl::half * ggml_sycl_fattn_kv_buffers::kv_buffer::ensure_half(size_t n_elems) { - const size_t need_bytes = n_elems * sizeof(sycl::half); - - if (capacity >= need_bytes) { - return ptr; - } - - if (ptr) { - SYCL_CHECK(CHECK_TRY_ERROR(qptr->wait())); - ggml_sycl_memtrace_del(ptr); - SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr))); - ptr = nullptr; - capacity = 0; - } - - size_t cap = 0; - while (cap < need_bytes) { - cap += CHUNK_SIZE; - } - - void * dev_ptr; - SYCL_CHECK( - CHECK_TRY_ERROR(dev_ptr = sycl::malloc_device( - cap, *qptr))); - - if (!dev_ptr) { - GGML_LOG_ERROR("%s: can't allocate %lu Bytes of memory on device\n", __func__, cap); - ggml_sycl_memtrace_fail(GGML_SYCL_MEM_FATTN_KV, cap); - GGML_ABORT("fattn buffer alloc failed"); - } - - ptr = static_cast(dev_ptr); - capacity = cap; - ggml_sycl_memtrace_add(GGML_SYCL_MEM_FATTN_KV, ptr, cap); - return ptr; -} - -ggml_sycl_fattn_kv_buffers::kv_buffer::~kv_buffer() { -#ifdef DEBUG_SYCL_POOL - GGML_LOG_INFO("ggml_sycl_fattn_kv_buffer[%d]: %.2f MiB\n", device, capacity / 1024.0 / 1024.0); -#endif - if (ptr) { - ggml_sycl_memtrace_del(ptr); - SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *qptr))); - } -} diff --git a/ggml/src/ggml-sycl/fattn-buffers.hpp b/ggml/src/ggml-sycl/fattn-buffers.hpp deleted file mode 100644 index c00461de6..000000000 --- a/ggml/src/ggml-sycl/fattn-buffers.hpp +++ /dev/null @@ -1,63 +0,0 @@ -// -// MIT license -// Copyright (C) 2025 Intel Corporation -// SPDX-License-Identifier: MIT -// - -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// - -#ifndef GGML_SYCL_FATTN_BUFFERS_HPP -#define GGML_SYCL_FATTN_BUFFERS_HPP - -#include - -typedef sycl::queue *queue_ptr; - -struct ggml_sycl_fattn_kv_buffers { - // buffers grow in chunks of this size - static constexpr size_t CHUNK_SIZE = 16ull << 20; // 16 MiB - - struct kv_buffer { - kv_buffer(queue_ptr qptr_, int device_) : qptr(qptr_), device(device_) {} - ~kv_buffer(); - - kv_buffer(const kv_buffer &) = delete; - kv_buffer & operator=(const kv_buffer &) = delete; - - sycl::half * ensure_half(size_t n_elems); - - private: - sycl::half * ptr = nullptr; - size_t capacity = 0; - queue_ptr qptr = nullptr; - [[maybe_unused]] int device = 0; - }; - - kv_buffer K; - kv_buffer V; - - ggml_sycl_fattn_kv_buffers(queue_ptr qptr, int device) : K(qptr, device), V(qptr, device) {} - - ggml_sycl_fattn_kv_buffers(const ggml_sycl_fattn_kv_buffers &) = delete; - ggml_sycl_fattn_kv_buffers & operator=(const ggml_sycl_fattn_kv_buffers &) = delete; -}; - -/** - * Imitates `ggml_sycl_pool_alloc` to keep the code calling alloc unchanged. - */ -struct ggml_sycl_fattn_alloc { - ggml_sycl_fattn_kv_buffers::kv_buffer & buf; - sycl::half * ptr = nullptr; - - explicit ggml_sycl_fattn_alloc(ggml_sycl_fattn_kv_buffers::kv_buffer & buf_) : buf(buf_) {} - - sycl::half * alloc(size_t n_elems) { - ptr = buf.ensure_half(n_elems); - return ptr; - } -}; -#endif diff --git a/ggml/src/ggml-sycl/fattn-common.hpp b/ggml/src/ggml-sycl/fattn-common.hpp index 3c2d1a776..2e8ac6af3 100644 --- a/ggml/src/ggml-sycl/fattn-common.hpp +++ b/ggml/src/ggml-sycl/fattn-common.hpp @@ -6,7 +6,6 @@ #include "common.hpp" #include "convert.hpp" #include "vecdotq.hpp" -#include "fattn-buffers.hpp" #include "fattn.hpp" #include "ggml.h" @@ -933,13 +932,10 @@ void launch_fattn( GGML_ASSERT(!mask || mask->type == GGML_TYPE_F16); ggml_sycl_pool & pool = ctx.pool(); - ggml_sycl_fattn_kv_buffers & fbuf = ctx.fattn_buffers(); dpct::queue_ptr main_stream = ctx.stream(); const int id = ggml_sycl_get_device(); const int nsm = ggml_sycl_info().devices[id].nsm; - ggml_sycl_fattn_alloc K_f16(fbuf.K); - ggml_sycl_fattn_alloc V_f16(fbuf.V); const ggml_sycl_fattn_extra extra = ggml_sycl_fattn_get_extra(dst); ggml_sycl_pool_alloc KV_max(pool); ggml_sycl_pool_alloc dst_tmp(pool); @@ -959,8 +955,8 @@ void launch_fattn( const size_t bs = ggml_blck_size(K->type); const size_t ts = ggml_type_size(K->type); - sycl::half * K_f16_ptr = extra.K_buffer_ptr ? (sycl::half *) extra.K_buffer_ptr - : K_f16.alloc(ggml_nelements(K)); + GGML_ASSERT(extra.K_buffer_ptr); + sycl::half * K_f16_ptr = (sycl::half *) extra.K_buffer_ptr; if (ggml_is_contiguously_allocated(K)) { to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(K->type, dst); to_fp16(K_data, K_f16_ptr, ggml_nelements(K), main_stream); @@ -993,8 +989,8 @@ void launch_fattn( const size_t bs = ggml_blck_size(V->type); const size_t ts = ggml_type_size(V->type); - sycl::half * V_f16_ptr = extra.V_buffer_ptr ? (sycl::half *) extra.V_buffer_ptr - : V_f16.alloc(ggml_nelements(V)); + GGML_ASSERT(extra.V_buffer_ptr); + sycl::half * V_f16_ptr = (sycl::half *) extra.V_buffer_ptr; if (ggml_is_contiguously_allocated(V)) { to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(V->type, dst); to_fp16(V_data, V_f16_ptr, ggml_nelements(V), main_stream); diff --git a/ggml/src/ggml-sycl/fattn-mkl.cpp b/ggml/src/ggml-sycl/fattn-mkl.cpp index 30947b17b..5a5cf69fd 100644 --- a/ggml/src/ggml-sycl/fattn-mkl.cpp +++ b/ggml/src/ggml-sycl/fattn-mkl.cpp @@ -8,7 +8,6 @@ #include "common.hpp" #include "fattn-common.hpp" -#include "fattn-buffers.hpp" #include "convert.hpp" #include "fattn.hpp" @@ -283,8 +282,10 @@ static mkl_fa_kv_desc mkl_fa_make_desc(const ggml_tensor * T, bool interleaved, d.ts = (int64_t)ggml_type_size(T->type); if (T->type == GGML_TYPE_F16) { - d.mode = interleaved ? MKL_FA_KV_MODE_F16_INTERLEAVED - : MKL_FA_KV_MODE_F16_DENSE; + // MLA's V cache is a 512-wide view of 576-wide K rows. Treat any + // padded row stride as strided even when there is only one KV head. + d.mode = interleaved || d.nb1 != d.D * (int64_t)sizeof(sycl::half) + ? MKL_FA_KV_MODE_F16_INTERLEAVED : MKL_FA_KV_MODE_F16_DENSE; } else if (ggml_is_contiguously_allocated(T) && !interleaved) { d.mode = MKL_FA_KV_MODE_QUANT_CONTIG; } else { @@ -413,7 +414,9 @@ void ggml_sycl_flash_attn_ext_mkl(ggml_backend_sycl_context & ctx, ggml_tensor * const int64_t q_row_stride = Q->nb[1] / sizeof(float); const int64_t q_head_stride = Q->nb[2] / sizeof(float); - const bool V_is_K_view = V->view_src + // Alias the dequantized buffers only when K and V expose the same values. + // MLA V is a narrower view of K and needs its own strided dequantization. + const bool V_is_K_view = V->ne[0] == K->ne[0] && V->view_src && (V->view_src == K || (V->view_src == K->view_src && V->view_offs == K->view_offs)); diff --git a/ggml/src/ggml-sycl/fattn-vec.hpp b/ggml/src/ggml-sycl/fattn-vec.hpp index 9ec88c287..5e40c88fb 100644 --- a/ggml/src/ggml-sycl/fattn-vec.hpp +++ b/ggml/src/ggml-sycl/fattn-vec.hpp @@ -589,10 +589,10 @@ void ggml_sycl_flash_attn_ext_vec_case_impl(ggml_backend_sycl_context & ctx, ggm const bool need_f16_V = type_V == GGML_TYPE_F16; constexpr size_t nbytes_shared = 0; + const auto arch = ggml_sycl_info().devices[ggml_sycl_get_device()].hw_info.arch; // D=512 does not fit the default register file; it spills up to 343 bytes per thread, against at most 57 for D <= 256. This kernel is decode only, so thread occupancy is not the limit. It is 1.9x faster at every KV depth on Battlemage. constexpr bool use_large_grf = D >= 512; - const auto arch = ggml_sycl_info().devices[ctx.device].hw_info.arch; const int nthreads = ggml_sycl_fattn_vec_get_nthreads_device(arch); if constexpr (D <= 256) { if (nthreads == 256) { diff --git a/ggml/src/ggml-sycl/fattn.cpp b/ggml/src/ggml-sycl/fattn.cpp index 541ae8a82..8bd946e09 100644 --- a/ggml/src/ggml-sycl/fattn.cpp +++ b/ggml/src/ggml-sycl/fattn.cpp @@ -146,15 +146,17 @@ static best_fattn_kernel ggml_sycl_get_best_fattn_kernel(const int device, const // Set GGML_SYCL_ENABLE_MKL_FA=0 to force TILE/VEC path for A/B testing. // Example: GGML_SYCL_ENABLE_MKL_FA=0 llama-cli -m model.gguf -fa -ngl 99 ... // Note: MKL GEMM calls are incompatible with SYCL graph capture replay. - // MKL is validated for the mainstream GQA envelope: grouped-query - // (gqa_ratio >= 2), head_dim a multiple of 64 in [64,512] with matching - // K/V head size, mask, no sinks/ALiBi/softcap. Gemma's global layers use - // head_dim 512, so the cap must include it. Head sizes not a multiple of - // 64 (72/80/96), MHA (gqa_ratio == 1), and MLA (DKQ != DV, e.g. 576/512) - // fall through to TILE/VEC; see follow-up work. + const bool standard_shape = Q->ne[0] >= 64 && Q->ne[0] <= 512 && + Q->ne[0] % 64 == 0 && Q->ne[0] == V->ne[0]; + // GLM-4.7 Flash's MLA shape is already expressible by the MKL pipeline: + // KQ GEMM uses DKQ=576 while VKQ and output use DV=512. Keep this narrow + // until other mismatched K/V shapes have independent correctness data. + const bool glm_mla_shape = Q->ne[0] == 576 && K->ne[0] == 576 && + V->ne[0] == 512 && gqa_ratio == 20 && + K->type == GGML_TYPE_F16 && V->type == GGML_TYPE_F16; + if (g_ggml_sycl_enable_mkl_fa == 1 && mask && !sinks && gqa_ratio >= 2 && - Q->ne[0] >= 64 && Q->ne[0] <= 512 && Q->ne[0] % 64 == 0 && - Q->ne[0] == V->ne[0] && + (standard_shape || glm_mla_shape) && Q->ne[1] >= 32 && K->ne[1] >= 1024 && max_bias == 0.0f && logit_softcap == 0.0f && (Q->ne[3] == K->ne[3] || K->ne[3] == 1)) { @@ -164,7 +166,10 @@ static best_fattn_kernel ggml_sycl_get_best_fattn_kernel(const int device, const // nb1=75 for ne0=40 fall through to TILE. bool kv_strides_ok = true; for (const ggml_tensor * t : {K, V}) { - if (t->type == GGML_TYPE_F16 && t->nb[1] % (t->ne[0] * 2) != 0) { + const bool glm_v_stride = glm_mla_shape && t == V && + V->view_src && V->nb[1] == K->nb[1]; + if (!glm_v_stride && t->type == GGML_TYPE_F16 && + t->nb[1] % (t->ne[0] * 2) != 0) { kv_strides_ok = false; break; } diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp index 49148d7c5..b4d200526 100644 --- a/ggml/src/ggml-sycl/ggml-sycl.cpp +++ b/ggml/src/ggml-sycl/ggml-sycl.cpp @@ -792,6 +792,14 @@ static bool ggml_sycl_is_l0_discrete_gpu(int device) { } #endif +static void memcpy_host_forward(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst, + const void *ptr_src, size_t size) { + char *host_buf = (char *)malloc(size); + q_src.memcpy(host_buf, (const char *)ptr_src, size).wait(); + q_dst.memcpy((char *)ptr_dst, host_buf, size).wait(); + free(host_buf); +} + static void dev2dev_memcpy(int device_dst, sycl::queue &q_dst, int device_src, sycl::queue &q_src, void *ptr_dst, const void *ptr_src, size_t size) { @@ -835,10 +843,7 @@ static void dev2dev_memcpy(int device_dst, sycl::queue &q_dst, int device_src, s } else { GGML_SYCL_DEBUG("[SYCL] dev2dev memcpy by host forward for SYCL/L0 fallback\n"); } - char *host_buf = (char *)malloc(size); - q_src.memcpy(host_buf, (const char *)ptr_src, size).wait(); - q_dst.memcpy((char *)ptr_dst, host_buf, size).wait(); - free(host_buf); + memcpy_host_forward(q_dst, q_src, ptr_dst, ptr_src, size); } static bool @@ -2064,11 +2069,6 @@ std::unique_ptr ggml_backend_sycl_context::new_pool_for_device(q return std::unique_ptr(new ggml_sycl_pool_leg(qptr, device)); } - -std::unique_ptr ggml_backend_sycl_context::new_fattn_kv_buffers(queue_ptr qptr, int device) { - return std::unique_ptr(new ggml_sycl_fattn_kv_buffers(qptr, device)); -} - /// kernels typedef void (*ggml_sycl_op_mul_mat_t)( ggml_backend_sycl_context & ctx, diff --git a/ggml/src/ggml-sycl/memtrace.cpp b/ggml/src/ggml-sycl/memtrace.cpp index 9c4f88539..ecd82ff6d 100644 --- a/ggml/src/ggml-sycl/memtrace.cpp +++ b/ggml/src/ggml-sycl/memtrace.cpp @@ -15,7 +15,6 @@ static const char * mem_type_name(ggml_sycl_mem_type type) { case GGML_SYCL_MEM_POOL_LEG: return "pool_leg"; case GGML_SYCL_MEM_POOL_VMM: return "pool_vmm"; case GGML_SYCL_MEM_ASYNC: return "async"; - case GGML_SYCL_MEM_FATTN_KV: return "fattn_kv"; case GGML_SYCL_MEM_DIRECT: return "direct"; default: GGML_ABORT("[%s] The type value %d is not supported\n", __func__, (int) type); } diff --git a/ggml/src/ggml-sycl/memtrace.hpp b/ggml/src/ggml-sycl/memtrace.hpp index 426d90963..c7da47f31 100644 --- a/ggml/src/ggml-sycl/memtrace.hpp +++ b/ggml/src/ggml-sycl/memtrace.hpp @@ -10,7 +10,6 @@ enum ggml_sycl_mem_type { GGML_SYCL_MEM_POOL_LEG, GGML_SYCL_MEM_POOL_VMM, GGML_SYCL_MEM_ASYNC, - GGML_SYCL_MEM_FATTN_KV, GGML_SYCL_MEM_DIRECT, GGML_SYCL_MEM_TYPE_COUNT, diff --git a/tests/test-recurrent-state-rollback.cpp b/tests/test-recurrent-state-rollback.cpp index 4043a229c..006486b26 100644 --- a/tests/test-recurrent-state-rollback.cpp +++ b/tests/test-recurrent-state-rollback.cpp @@ -131,7 +131,7 @@ static test_status test_multi_seq_split_replay(const common_params & params, lla constexpr uint32_t n_seqs = 2; constexpr uint32_t n_ubatch = 16; - constexpr uint32_t n_prompt = 19; + constexpr uint32_t n_prompt = 20; constexpr uint32_t n_rollback = 3; constexpr uint32_t n_replay = 40; // > n_ubatch so each seq spans multiple ubatches constexpr llama_pos p0 = n_prompt - n_rollback; @@ -174,13 +174,13 @@ static test_status test_multi_seq_split_replay(const common_params & params, lla return llama_process(ctx, LLAMA_PROCESS_TYPE_DECODE, batch.get()) == 0; }; - // both contexts decode the identical [0, p0) prefill; only ctx_roll decodes - // the tail, which is then rolled back so its restore is pending at replay + // Keep the anchor at p0 - 1 when rolling back the tail. for (uint32_t s = 0; s < n_seqs && ok; ++s) { - ok = ok && decode_range(ctx_roll.get(), s, 0, (llama_pos) p0); - ok = ok && decode_range(ctx_ref.get(), s, 0, (llama_pos) p0); + ok = ok && decode_range(ctx_roll.get(), s, 0, (llama_pos) p0 - 1); + ok = ok && decode_range(ctx_ref.get(), s, 0, (llama_pos) p0 - 1); - ok = ok && decode_range(ctx_roll.get(), s, (llama_pos) p0, (llama_pos) n_prompt); + ok = ok && decode_range(ctx_roll.get(), s, (llama_pos) p0 - 1, (llama_pos) n_prompt); + ok = ok && decode_range(ctx_ref.get(), s, (llama_pos) p0 - 1, (llama_pos) p0); ok = ok && llama_memory_seq_rm(llama_get_memory(ctx_roll.get()), (llama_seq_id) s, p0, -1);