From fc660808e697d37387cf375720a7fe0df94f394b Mon Sep 17 00:00:00 2001 From: Amin Ya Date: Tue, 31 Mar 2026 13:37:57 -0700 Subject: [PATCH] fix: add TurboQuant for Windows This fixes the compilation on Windows tested with Clang and MSVC. - The global C variables need to be declared via GGML_API to be accessible correctly from C++ on Windows for dlls - Cuda functions should be imported via GGML_API on Windows - Pi needs to be declared before including `cmath` --- ggml/src/ggml-cpu/ops.cpp | 3 ++- ggml/src/ggml-cuda/turbo-innerq.cuh | 8 +++++--- ggml/src/ggml-turbo-quant.c | 8 +++++--- src/llama-kv-cache.cpp | 8 ++++---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp index cec4855f2d0f..c24ed48e7889 100644 --- a/ggml/src/ggml-cpu/ops.cpp +++ b/ggml/src/ggml-cpu/ops.cpp @@ -4894,6 +4894,8 @@ void ggml_compute_forward_get_rows( //} } +extern "C" GGML_API int turbo3_cpu_wht_group_size; + template static void ggml_compute_forward_set_rows_f32( const ggml_compute_params * params, @@ -4928,7 +4930,6 @@ static void ggml_compute_forward_set_rows_f32( // For turbo types: communicate WHT group size to the quantize function via global if (dst->type == GGML_TYPE_TURBO3_0 || dst->type == GGML_TYPE_TURBO4_0 || dst->type == GGML_TYPE_TURBO2_0) { - extern int turbo3_cpu_wht_group_size; int gs = 0; memcpy(&gs, dst->op_params, sizeof(int)); turbo3_cpu_wht_group_size = (gs == 64 || gs == 128) ? gs : 0; diff --git a/ggml/src/ggml-cuda/turbo-innerq.cuh b/ggml/src/ggml-cuda/turbo-innerq.cuh index 90103aee3391..9b8c84d543b5 100644 --- a/ggml/src/ggml-cuda/turbo-innerq.cuh +++ b/ggml/src/ggml-cuda/turbo-innerq.cuh @@ -1,5 +1,7 @@ #pragma once +#include "ggml.h" + // TurboQuant InnerQ per-channel equalization — cross-TU shared state // The host-side state lives in turbo-innerq.cu; device-side state is per-TU // in turbo-quant.cuh (only set-rows.cu needs device access). @@ -8,14 +10,14 @@ // Host-side shared state (defined in turbo-innerq.cu) extern bool g_innerq_finalized; -extern float g_innerq_scale_inv_host[INNERQ_MAX_CHANNELS]; +GGML_API float g_innerq_scale_inv_host[INNERQ_MAX_CHANNELS]; // Called from set-rows.cu after InnerQ finalization to publish scale_inv void turbo_innerq_publish(const float * scale_inv, int group_size); // Called from llama-kv-cache.cpp (or equivalent) to check if tensor needs update // Returns true if there are new scale_inv values to upload -bool turbo_innerq_needs_tensor_update(void); +GGML_API bool turbo_innerq_needs_tensor_update(void); // Called after tensor update to clear the flag -void turbo_innerq_mark_tensor_updated(void); +GGML_API void turbo_innerq_mark_tensor_updated(void); diff --git a/ggml/src/ggml-turbo-quant.c b/ggml/src/ggml-turbo-quant.c index 0fc19a2e61fe..3eeb70f88d79 100644 --- a/ggml/src/ggml-turbo-quant.c +++ b/ggml/src/ggml-turbo-quant.c @@ -10,13 +10,17 @@ #include "ggml-common.h" #include "ggml-impl.h" +#if defined(_WIN32) && !defined(_USE_MATH_DEFINES) +#define _USE_MATH_DEFINES +#endif + #include #include #include #include /* Global: WHT group size for CPU quantize path (set by CPU SET_ROWS handler) */ -int turbo3_cpu_wht_group_size = 0; +GGML_API int turbo3_cpu_wht_group_size = 0; /* ---------- constants ---------- */ @@ -246,7 +250,6 @@ void quantize_row_turbo3_0_ref(const float * GGML_RESTRICT x, block_turbo3_0 * G // Read WHT group size from global (set by CPU SET_ROWS handler before each call). // Fallback: 128 if row is 128-aligned, else 64. - extern int turbo3_cpu_wht_group_size; int group_size = turbo3_cpu_wht_group_size; if (group_size != 64 && group_size != 128) { group_size = (k % 128 == 0) ? 128 : 64; @@ -341,7 +344,6 @@ size_t quantize_turbo3_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT d void quantize_row_turbo2_0_ref(const float * GGML_RESTRICT x, block_turbo2_0 * GGML_RESTRICT y, int64_t k) { assert(k % QK_TURBO2 == 0); - extern int turbo3_cpu_wht_group_size; int group_size = turbo3_cpu_wht_group_size; if (group_size != 64 && group_size != 128) { group_size = (k % 128 == 0) ? 128 : 64; diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index d5abb82e45aa..450781506d9a 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -21,10 +21,10 @@ #endif #ifdef GGML_USE_CUDA -extern bool g_innerq_finalized; -extern float g_innerq_scale_inv_host[INNERQ_MAX_CHANNELS]; -extern bool turbo_innerq_needs_tensor_update(void); -extern void turbo_innerq_mark_tensor_updated(void); +GGML_API bool g_innerq_finalized; +GGML_API float g_innerq_scale_inv_host[INNERQ_MAX_CHANNELS]; +GGML_API bool turbo_innerq_needs_tensor_update(void); +GGML_API void turbo_innerq_mark_tensor_updated(void); #else static bool g_innerq_finalized = false; static float g_innerq_scale_inv_host[INNERQ_MAX_CHANNELS] = {};