From b688cbe8c2eeddfc7bb48f167c7c02564acc9df0 Mon Sep 17 00:00:00 2001 From: noctrex Date: Tue, 25 Aug 2026 21:00:09 +0300 Subject: [PATCH 1/4] ggml : enable INT8 convrot on HIP for RDNA3/RDNA4/CDNA Remove the blanket GGML_USE_HIP exclusion from the INT8 tensorwise convrot ops and scope the capability check to GPUs with working integer GEMM support (turing_mma_available, amd_wmma_available, amd_mfma_available). RDNA2 stays excluded: hipBLASLt INT8 is broken there. vendors/hip.h: map CUDA_R_8I, CUDA_R_32I, CUBLAS_COMPUTE_32I and __shfl_down_sync to their HIP equivalents. HIP's masked shuffle templates require a 64-bit mask, so route to the maskless intrinsic like the other __shfl_*_sync mappings. Verified on gfx1100 (Windows ROCm 7.1): ggml tests/test-int8-convrot passes, and CPU-vs-GPU output is bit-exact at real model shapes (K=3840/10240, N up to 11520, 8104 rows). --- src/ggml-cuda/ggml-cuda.cu | 18 ++++++++++-------- src/ggml-cuda/vendors/hip.h | 5 +++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ggml-cuda/ggml-cuda.cu b/src/ggml-cuda/ggml-cuda.cu index 025f11e16e..2a3411af4e 100644 --- a/src/ggml-cuda/ggml-cuda.cu +++ b/src/ggml-cuda/ggml-cuda.cu @@ -1333,7 +1333,7 @@ typedef void (*ggml_cuda_op_mul_mat_t)( const int64_t src1_padded_row_size, cudaStream_t stream); -#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) +#if !defined(GGML_USE_MUSA) __global__ void quantize_rowwise_i8_cuda(const float * src, int8_t * dst, float * scales, int64_t k, int64_t rows) { const int64_t row = blockIdx.x; const int tid = threadIdx.x; @@ -2237,10 +2237,10 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor } if (src0->type == GGML_TYPE_I8) { -#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) +#if !defined(GGML_USE_MUSA) ggml_cuda_mul_mat_i8(ctx, src0, src1, dst); #else - GGML_ABORT("INT8 tensorwise matmul is only implemented for CUDA"); + GGML_ABORT("INT8 tensorwise matmul is only implemented for CUDA and HIP"); #endif return; } @@ -2646,10 +2646,10 @@ static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct gg ggml_cuda_mul_mat_id(ctx, dst); break; case GGML_OP_QUANTIZE_I8_CONVROT: -#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) +#if !defined(GGML_USE_MUSA) GGML_ASSERT(ggml_cuda_op_quantize_i8_convrot(ctx, dst->src[0], dst, ggml_get_op_params_i32(dst, 0))); #else - GGML_ABORT("INT8 convrot quantization is only implemented for CUDA"); + GGML_ABORT("INT8 convrot quantization is only implemented for CUDA and HIP"); #endif break; case GGML_OP_OUT_PROD: @@ -5236,7 +5236,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g return false; // TODO this could in principle be implemented though currently there is no use case. } if (a->type == GGML_TYPE_I8) { -#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) +#if !defined(GGML_USE_MUSA) const ggml_tensor * weight_scale = op->src[2]; const ggml_tensor * bias = op->src[3]; const int convrot_group_size = ggml_get_op_params_i32(op, 2); @@ -5260,7 +5260,9 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g (bias->type == GGML_TYPE_F32 && ggml_is_contiguous(bias) && ggml_nelements(bias) == a->ne[1])) && (convrot_group_size == 0 || (convrot_group_size == 256 && a->ne[0] % 256 == 0)) && - turing_mma_available(ggml_cuda_info().devices[dev_ctx->device].cc); + (turing_mma_available(ggml_cuda_info().devices[dev_ctx->device].cc) || + amd_wmma_available(ggml_cuda_info().devices[dev_ctx->device].cc) || + amd_mfma_available(ggml_cuda_info().devices[dev_ctx->device].cc)); #else return false; #endif @@ -5640,7 +5642,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g case GGML_OP_LIGHTNING_INDEXER: return ggml_cuda_lightning_indexer_supported(dev_ctx->device, op); case GGML_OP_QUANTIZE_I8_CONVROT: -#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) +#if !defined(GGML_USE_MUSA) { const ggml_tensor * src = op->src[0]; const int group_size = ggml_get_op_params_i32(op, 0); diff --git a/src/ggml-cuda/vendors/hip.h b/src/ggml-cuda/vendors/hip.h index 9aa558f3f4..e37c54c175 100644 --- a/src/ggml-cuda/vendors/hip.h +++ b/src/ggml-cuda/vendors/hip.h @@ -20,6 +20,8 @@ #define CUDA_R_16F HIPBLAS_R_16F #define CUDA_R_16BF HIPBLAS_R_16B #define CUDA_R_32F HIPBLAS_R_32F +#define CUDA_R_8I HIPBLAS_R_8I +#define CUDA_R_32I HIPBLAS_R_32I #define CUBLAS_SIDE_RIGHT HIPBLAS_SIDE_RIGHT #define CUBLAS_FILL_MODE_UPPER HIPBLAS_FILL_MODE_UPPER #define CUBLAS_DIAG_NON_UNIT HIPBLAS_DIAG_NON_UNIT @@ -32,6 +34,8 @@ #define __shfl_sync(mask, var, laneMask, width) __shfl(var, laneMask, width) #define __shfl_up_sync(mask, var, laneMask, width) __shfl_up(var, laneMask, width) #define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width) +// Variadic: call sites may omit the trailing width argument. +#define __shfl_down_sync(mask, ...) __shfl_down(__VA_ARGS__) #define __all_sync(mask, var) __all(var) #define __any_sync(mask, var) __any(var) #define cublasStrsmBatched hipblasStrsmBatched @@ -159,6 +163,7 @@ #if HIP_VERSION >= 60500000 #define CUBLAS_COMPUTE_16F HIPBLAS_COMPUTE_16F #define CUBLAS_COMPUTE_32F HIPBLAS_COMPUTE_32F +#define CUBLAS_COMPUTE_32I HIPBLAS_COMPUTE_32I #define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_COMPUTE_32F_FAST_16F #define cublasComputeType_t hipblasComputeType_t #define cudaDataType_t hipDataType From 0ec2d6a5677538300d1777588164d32c8938f5e3 Mon Sep 17 00:00:00 2001 From: noctrex Date: Tue, 25 Aug 2026 21:00:28 +0300 Subject: [PATCH 2/4] ggml : support convrot group size 64 in CUDA/HIP kernels Template the INT8 convrot quantization kernels on group_size and instantiate 64 alongside 256. The Hadamard normalization is now derived from the group size (2^-log4(group_size): 1/8 for G=64, 1/16 for G=256). The small-kernel max reduction uses a shared-memory tree instead of warp shuffles so it stays correct for 16-thread blocks. Capability gates accept group size 64 and 256. Z-Image int8_tensorwise exports in the wild use convrot_groupsize 64; without this they fall back to CPU on every GPU backend. Verified on gfx1100 (Windows ROCm 7.1): exact-reference tests pass for H64 (single-group and multi-group packed GEMM), H256 unchanged, and CPU-vs-GPU output is bit-exact at real Z-Image shapes. --- src/ggml-cuda/ggml-cuda.cu | 128 ++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 46 deletions(-) diff --git a/src/ggml-cuda/ggml-cuda.cu b/src/ggml-cuda/ggml-cuda.cu index 2a3411af4e..01852f1247 100644 --- a/src/ggml-cuda/ggml-cuda.cu +++ b/src/ggml-cuda/ggml-cuda.cu @@ -1377,12 +1377,14 @@ __global__ void quantize_rowwise_i8_cuda(const float * src, int8_t * dst, float } } +template __global__ void quantize_rowwise_i8_convrot_cuda( const float * src, int8_t * dst, float * scales, int64_t k, int64_t rows, int64_t dst_stride) { - constexpr int group_size = 256; constexpr int group_threads = group_size / 4; constexpr int groups_per_wave = 1024 / group_threads; + // H4 carries a 1/2 normalization, applied log4(group_size) times. + constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f); const int64_t row = blockIdx.x; const int tid = threadIdx.x; @@ -1396,7 +1398,7 @@ __global__ void quantize_rowwise_i8_convrot_cuda( int8_t * dst_row = dst + row * dst_stride; for (int64_t i = tid; i < k; i += blockDim.x) { - values[i] = src_row[i] * (1.0f / 16.0f); + values[i] = src_row[i] * hadamard_norm; } __syncthreads(); @@ -1466,27 +1468,32 @@ __global__ void quantize_rowwise_i8_convrot_cuda( } } +template __global__ void convrot_group_amax_i8_cuda( const float * src, float * partial_max, int64_t k, int64_t groups, int64_t total_groups) { + constexpr int group_threads = group_size / 4; + // H4 carries a 1/2 normalization, applied log4(group_size) times. + constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f); + const int64_t index = blockIdx.x; const int tid = threadIdx.x; if (index >= total_groups) { return; } - __shared__ float values[256]; - __shared__ float maxima[2]; + __shared__ float values[group_size]; + __shared__ float maxima[group_threads]; const int64_t row = index / groups; const int64_t group = index - row * groups; - const float * input = src + row * k + group * 256; - values[tid] = input[tid] * (1.0f / 16.0f); - values[tid + 64] = input[tid + 64] * (1.0f / 16.0f); - values[tid + 128] = input[tid + 128] * (1.0f / 16.0f); - values[tid + 192] = input[tid + 192] * (1.0f / 16.0f); + const float * input = src + row * k + group * group_size; +#pragma unroll + for (int j = 0; j < 4; ++j) { + values[tid + j * group_threads] = input[tid + j * group_threads] * hadamard_norm; + } __syncthreads(); #pragma unroll - for (int stride = 1; stride < 256; stride *= 4) { + for (int stride = 1; stride < group_size; stride *= 4) { const int base = (tid / stride) * 4 * stride + tid % stride; const int i0 = base; const int i1 = i0 + stride; @@ -1503,18 +1510,21 @@ __global__ void convrot_group_amax_i8_cuda( __syncthreads(); } - float value = fmaxf(fabsf(values[tid]), fabsf(values[tid + 64])); - value = fmaxf(value, fabsf(values[tid + 128])); - value = fmaxf(value, fabsf(values[tid + 192])); - for (int offset = 16; offset > 0; offset >>= 1) { - value = fmaxf(value, __shfl_down_sync(0xffffffff, value, offset)); - } - if ((tid & 31) == 0) { - maxima[tid >> 5] = value; + float value = 0.0f; +#pragma unroll + for (int j = 0; j < 4; ++j) { + value = fmaxf(value, fabsf(values[tid + j * group_threads])); } + maxima[tid] = value; __syncthreads(); + for (int stride = group_threads / 2; stride > 0; stride >>= 1) { + if (tid < stride) { + maxima[tid] = fmaxf(maxima[tid], maxima[tid + stride]); + } + __syncthreads(); + } if (tid == 0) { - partial_max[index] = fmaxf(maxima[0], maxima[1]); + partial_max[index] = maxima[0]; } } @@ -1544,28 +1554,33 @@ __global__ void reduce_convrot_row_amax_i8_cuda( } } +template __global__ void convrot_group_quantize_i8_cuda( const float * src, int8_t * dst, const float * scales, int64_t k, int64_t groups, int64_t total_groups, int64_t dst_stride) { + constexpr int group_threads = group_size / 4; + // H4 carries a 1/2 normalization, applied log4(group_size) times. + constexpr float hadamard_norm = group_size == 256 ? (1.0f / 16.0f) : (1.0f / 8.0f); + const int64_t index = blockIdx.x; const int tid = threadIdx.x; if (index >= total_groups) { return; } - __shared__ float values[256]; + __shared__ float values[group_size]; const int64_t row = index / groups; const int64_t group = index - row * groups; - const float * input = src + row * k + group * 256; - int8_t * output = dst + row * dst_stride + group * 256; - values[tid] = input[tid] * (1.0f / 16.0f); - values[tid + 64] = input[tid + 64] * (1.0f / 16.0f); - values[tid + 128] = input[tid + 128] * (1.0f / 16.0f); - values[tid + 192] = input[tid + 192] * (1.0f / 16.0f); + const float * input = src + row * k + group * group_size; + int8_t * output = dst + row * dst_stride + group * group_size; +#pragma unroll + for (int j = 0; j < 4; ++j) { + values[tid + j * group_threads] = input[tid + j * group_threads] * hadamard_norm; + } __syncthreads(); #pragma unroll - for (int stride = 1; stride < 256; stride *= 4) { + for (int stride = 1; stride < group_size; stride *= 4) { const int base = (tid / stride) * 4 * stride + tid % stride; const int i0 = base; const int i1 = i0 + stride; @@ -1584,12 +1599,12 @@ __global__ void convrot_group_quantize_i8_cuda( const float row_scale = scales[row]; const float inv_scale = row_scale == 0.0f ? 0.0f : 1.0f / row_scale; - const int indices[4] = { tid, tid + 64, tid + 128, tid + 192 }; #pragma unroll - for (int i = 0; i < 4; ++i) { - int value = row_scale == 0.0f ? 0 : __float2int_rn(values[indices[i]] * inv_scale); + for (int j = 0; j < 4; ++j) { + const int element = tid + j * group_threads; + int value = row_scale == 0.0f ? 0 : __float2int_rn(values[element] * inv_scale); value = max(-127, min(127, value)); - output[indices[i]] = (int8_t)value; + output[element] = (int8_t)value; } if (group == 0 && tid == 0 && dst_stride != k) { *((float *)(dst + row * dst_stride + k)) = row_scale; @@ -1623,7 +1638,8 @@ static void ggml_cuda_quantize_i8_convrot( int64_t k, int64_t rows, int64_t rows_padded, - int64_t dst_stride) { + int64_t dst_stride, + int group_size) { cudaStream_t stream = ctx.stream(); if (rows_padded > rows) { CUDA_CHECK(cudaMemsetAsync( @@ -1633,26 +1649,43 @@ static void ggml_cuda_quantize_i8_convrot( const size_t nbytes_shared = (size_t)k * sizeof(float); const size_t smpbo = ggml_cuda_info().devices[ggml_cuda_get_device()].smpbo; if (nbytes_shared + 128 <= smpbo) { - CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda, smpbo - 128); - quantize_rowwise_i8_convrot_cuda<<>>( - src, dst, scales, k, rows, dst_stride); + if (group_size == 64) { + CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda<64>, smpbo - 128); + quantize_rowwise_i8_convrot_cuda<64><<>>( + src, dst, scales, k, rows, dst_stride); + } else { + CUDA_SET_SHARED_MEMORY_LIMIT(quantize_rowwise_i8_convrot_cuda<256>, smpbo - 128); + quantize_rowwise_i8_convrot_cuda<256><<>>( + src, dst, scales, k, rows, dst_stride); + } return; } - const int64_t groups = k / 256; + const int64_t groups = k / group_size; const int64_t total_groups = rows * groups; + const int group_threads = group_size / 4; ggml_cuda_pool_alloc partial_max(ctx.pool(), total_groups); ggml_cuda_pool_alloc packed_scales(ctx.pool()); float * scales_d = scales; if (scales_d == nullptr) { scales_d = packed_scales.alloc(rows); } - convrot_group_amax_i8_cuda<<>>( - src, partial_max.get(), k, groups, total_groups); + if (group_size == 64) { + convrot_group_amax_i8_cuda<64><<>>( + src, partial_max.get(), k, groups, total_groups); + } else { + convrot_group_amax_i8_cuda<256><<>>( + src, partial_max.get(), k, groups, total_groups); + } reduce_convrot_row_amax_i8_cuda<<>>( partial_max.get(), scales_d, groups, rows); - convrot_group_quantize_i8_cuda<<>>( - src, dst, scales_d, k, groups, total_groups, dst_stride); + if (group_size == 64) { + convrot_group_quantize_i8_cuda<64><<>>( + src, dst, scales_d, k, groups, total_groups, dst_stride); + } else { + convrot_group_quantize_i8_cuda<256><<>>( + src, dst, scales_d, k, groups, total_groups, dst_stride); + } } static bool ggml_cuda_op_quantize_i8_convrot( @@ -1665,7 +1698,7 @@ static bool ggml_cuda_op_quantize_i8_convrot( const int64_t scale_rows = (rows * (int64_t)sizeof(float) + src->ne[0] - 1) / src->ne[0]; if (src->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_I8 || !ggml_is_contiguous(src) || !ggml_is_contiguous(dst) || - group_size != 256 || src->ne[0] % group_size != 0 || + (group_size != 64 && group_size != 256) || src->ne[0] % group_size != 0 || dst->ne[0] != src->ne[0] || dst->ne[1] != rows_padded + scale_rows) { return false; } @@ -1679,7 +1712,8 @@ static bool ggml_cuda_op_quantize_i8_convrot( src->ne[0], rows, rows_padded, - src->ne[0]); + src->ne[0], + group_size); return true; } @@ -1707,7 +1741,7 @@ static void ggml_cuda_mul_mat_i8( GGML_ASSERT(src1->ne[0] == k); GGML_ASSERT(!prequantized || (src1->op == GGML_OP_QUANTIZE_I8_CONVROT && src1->ne[1] == rows_padded + scale_rows)); - GGML_ASSERT(convrot_group_size == 0 || convrot_group_size == 256); + GGML_ASSERT(convrot_group_size == 0 || convrot_group_size == 64 || convrot_group_size == 256); ggml_cuda_pool_alloc qdata(ctx.pool()); ggml_cuda_pool_alloc scales(ctx.pool()); @@ -1727,7 +1761,7 @@ static void ggml_cuda_mul_mat_i8( (const float *)src1->data, qdata_d, scales_d, k, rows); } else { ggml_cuda_quantize_i8_convrot( - ctx, (const float *)src1->data, qdata_d, scales_d, k, rows, rows_padded, k); + ctx, (const float *)src1->data, qdata_d, scales_d, k, rows, rows_padded, k, convrot_group_size); } const float * weight_scales = dst->src[2] != nullptr ? (const float *)dst->src[2]->data : nullptr; @@ -5259,7 +5293,9 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g (bias == nullptr || (bias->type == GGML_TYPE_F32 && ggml_is_contiguous(bias) && ggml_nelements(bias) == a->ne[1])) && - (convrot_group_size == 0 || (convrot_group_size == 256 && a->ne[0] % 256 == 0)) && + (convrot_group_size == 0 || + ((convrot_group_size == 64 || convrot_group_size == 256) && + a->ne[0] % convrot_group_size == 0)) && (turing_mma_available(ggml_cuda_info().devices[dev_ctx->device].cc) || amd_wmma_available(ggml_cuda_info().devices[dev_ctx->device].cc) || amd_mfma_available(ggml_cuda_info().devices[dev_ctx->device].cc)); @@ -5653,7 +5689,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g : 0; return src != nullptr && src->type == GGML_TYPE_F32 && op->type == GGML_TYPE_I8 && ggml_is_contiguous(src) && ggml_is_contiguous(op) && - group_size == 256 && src->ne[0] % group_size == 0 && + (group_size == 64 || group_size == 256) && src->ne[0] % group_size == 0 && op->ne[0] == src->ne[0] && op->ne[1] == rows_padded + scale_rows; } #else From d61707b8e198fea994cd950a0b2f1bd1ed834135 Mon Sep 17 00:00:00 2001 From: noctrex Date: Thu, 27 Aug 2026 14:31:36 +0300 Subject: [PATCH 3/4] ggml : support convrot group size 64 in Vulkan backend Parameterize the quantize shader with a GROUP_SIZE macro (default 256, validated {64, 256}), register the G64 SPIR-V variant, relax the quantize and tensorwise mulmat gates to accept group size 64, and select the pipeline by the op param group size. The GEMM shaders are group-size agnostic and unchanged. The G256 variant compiles to byte-identical SPIR-V as before. --- src/ggml-vulkan/ggml-vulkan.cpp | 14 +++++--- .../vulkan-shaders/quantize_i8_convrot.comp | 36 +++++++++++++------ .../vulkan-shaders/vulkan-shaders-gen.cpp | 1 + 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/ggml-vulkan/ggml-vulkan.cpp b/src/ggml-vulkan/ggml-vulkan.cpp index 7ac745c762..10398a7094 100644 --- a/src/ggml-vulkan/ggml-vulkan.cpp +++ b/src/ggml-vulkan/ggml-vulkan.cpp @@ -912,6 +912,7 @@ struct vk_device_struct { vk_pipeline pipeline_matmul_split_k_reduce; vk_pipeline pipeline_quantize_q8_1_x4; vk_pipeline pipeline_quantize_i8_convrot; + vk_pipeline pipeline_quantize_i8_convrot_h64; vk_pipeline pipeline_mul_mat_i8_tensorwise; vk_pipeline pipeline_mul_mat_i8_tensorwise_cm1; @@ -5474,6 +5475,7 @@ static void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) { #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT) if (device->integer_dot_product) { ggml_vk_create_pipeline(device, device->pipeline_quantize_i8_convrot, "quantize_i8_convrot", quantize_i8_convrot_len, quantize_i8_convrot_data, "main", 3, sizeof(vk_quantize_i8_convrot_push_constants), {1, 1, 1}, {}, 1); + ggml_vk_create_pipeline(device, device->pipeline_quantize_i8_convrot_h64, "quantize_i8_convrot_h64", quantize_i8_convrot_h64_len, quantize_i8_convrot_h64_data, "main", 3, sizeof(vk_quantize_i8_convrot_push_constants), {1, 1, 1}, {}, 1); ggml_vk_create_pipeline(device, device->pipeline_mul_mat_i8_tensorwise, "mul_mat_i8_tensorwise", mul_mat_i8_tensorwise_len, mul_mat_i8_tensorwise_data, "main", 6, sizeof(vk_mul_mat_i8_tensorwise_push_constants), {16, 16, 1}, {}, 1); #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) if (device->coopmat_int_support) { @@ -9995,7 +9997,7 @@ static bool ggml_vk_can_use_quantize_i8_convrot(const vk_device & device, const const int64_t rows = ggml_nrows(src); const int64_t rows_padded = GGML_PAD(rows, 4); const int64_t scale_rows = (rows * (int64_t) sizeof(float) + src->ne[0] - 1) / src->ne[0]; - return group_size == 256 && src->ne[0] % 256 == 0 && + return (group_size == 64 || group_size == 256) && src->ne[0] % group_size == 0 && src->ne[0] <= std::numeric_limits::max() && rows <= std::numeric_limits::max() && dst->ne[0] == src->ne[0] && dst->ne[1] == rows_padded + scale_rows; @@ -10029,8 +10031,9 @@ static bool ggml_vk_can_use_mul_mat_i8_tensorwise(const vk_device & device, cons const int64_t rows = ggml_nrows(logical_input); const int64_t rows_padded = GGML_PAD(rows, 4); const int64_t scale_rows = (rows * (int64_t) sizeof(float) + weight->ne[0] - 1) / weight->ne[0]; - return convrot_group_size == 256 && ggml_get_op_params_i32(input, 0) == 256 && - weight->ne[0] % 256 == 0 && + return (convrot_group_size == 64 || convrot_group_size == 256) && + ggml_get_op_params_i32(input, 0) == convrot_group_size && + weight->ne[0] % convrot_group_size == 0 && input->ne[0] == weight->ne[0] && input->ne[1] == rows_padded + scale_rows && ggml_nrows(dst) == rows && weight_scale->type == GGML_TYPE_F32 && ggml_nelements(weight_scale) == weight->ne[1] && @@ -10051,7 +10054,10 @@ static void ggml_vk_quantize_i8_convrot( ggml_backend_vk_context * ctx, vk_context & subctx, const ggml_tensor * src, ggml_tensor * dst) { GGML_ASSERT(ggml_vk_can_use_quantize_i8_convrot(ctx->device, dst)); - vk_pipeline pipeline = ctx->device->pipeline_quantize_i8_convrot; + const int group_size = ggml_get_op_params_i32(dst, 0); + vk_pipeline pipeline = group_size == 64 + ? ctx->device->pipeline_quantize_i8_convrot_h64 + : ctx->device->pipeline_quantize_i8_convrot; ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1); const uint32_t rows = (uint32_t) ggml_nrows(src); diff --git a/src/ggml-vulkan/vulkan-shaders/quantize_i8_convrot.comp b/src/ggml-vulkan/vulkan-shaders/quantize_i8_convrot.comp index 1c69d9dde2..e9ab6bc550 100644 --- a/src/ggml-vulkan/vulkan-shaders/quantize_i8_convrot.comp +++ b/src/ggml-vulkan/vulkan-shaders/quantize_i8_convrot.comp @@ -2,7 +2,21 @@ #extension GL_EXT_control_flow_attributes : require -layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; +#ifndef GROUP_SIZE +#define GROUP_SIZE 256 +#endif +#if GROUP_SIZE != 64 && GROUP_SIZE != 256 +#error unsupported GROUP_SIZE +#endif +#define GROUP_THREADS (GROUP_SIZE / 4) + +#if GROUP_SIZE == 256 +#define HADAMARD_NORM (1.0 / 16.0) +#else +#define HADAMARD_NORM (1.0 / 8.0) +#endif + +layout(local_size_x = GROUP_THREADS, local_size_y = 1, local_size_z = 1) in; layout(push_constant) uniform parameter { @@ -14,22 +28,22 @@ layout(binding = 0, std430) readonly buffer A { float data_a[]; }; layout(binding = 1, std430) writeonly buffer D { uint data_d[]; }; layout(binding = 2, std430) writeonly buffer S { float data_s[]; }; -shared float values[256]; -shared float maxima[64]; +shared float values[GROUP_SIZE]; +shared float maxima[GROUP_THREADS]; void transform_group(const uint row, const uint group) { const uint tid = gl_LocalInvocationID.x; - const uint base_offset = row * p.k + group * 256; + const uint base_offset = row * p.k + group * GROUP_SIZE; [[unroll]] for (uint i = 0; i < 4; ++i) { - const uint index = tid + i * 64; - values[index] = data_a[base_offset + index] * (1.0 / 16.0); + const uint index = tid + i * GROUP_THREADS; + values[index] = data_a[base_offset + index] * HADAMARD_NORM; } barrier(); [[unroll]] - for (uint stride = 1; stride < 256; stride *= 4) { + for (uint stride = 1; stride < GROUP_SIZE; stride *= 4) { const uint base = (tid / stride) * 4 * stride + tid % stride; const uint i0 = base; const uint i1 = i0 + stride; @@ -54,14 +68,14 @@ uint quantize_value(const float value, const float inv_scale) { void quantize_row(const uint row) { const uint tid = gl_LocalInvocationID.x; - const uint groups = p.k / 256; + const uint groups = p.k / GROUP_SIZE; float local_max = 0.0; for (uint group = 0; group < groups; ++group) { transform_group(row, group); [[unroll]] for (uint i = 0; i < 4; ++i) { - local_max = max(local_max, abs(values[tid + i * 64])); + local_max = max(local_max, abs(values[tid + i * GROUP_THREADS])); } barrier(); } @@ -69,7 +83,7 @@ void quantize_row(const uint row) { maxima[tid] = local_max; barrier(); [[unroll]] - for (uint stride = 32; stride > 0; stride >>= 1) { + for (uint stride = GROUP_THREADS / 2; stride > 0; stride >>= 1) { if (tid < stride) { maxima[tid] = max(maxima[tid], maxima[tid + stride]); } @@ -90,7 +104,7 @@ void quantize_row(const uint row) { | (quantize_value(values[index + 1], inv_scale) << 8) | (quantize_value(values[index + 2], inv_scale) << 16) | (quantize_value(values[index + 3], inv_scale) << 24); - data_d[(row * p.k + group * 256) / 4 + tid] = packed; + data_d[(row * p.k + group * GROUP_SIZE) / 4 + tid] = packed; barrier(); } } diff --git a/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp index a0ae1c44cc..2dd7903fca 100644 --- a/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp +++ b/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp @@ -878,6 +878,7 @@ void process_shaders() { #if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT) string_to_spv("quantize_i8_convrot", "quantize_i8_convrot.comp", {}); + string_to_spv("quantize_i8_convrot_h64", "quantize_i8_convrot.comp", {{"GROUP_SIZE", "64"}}); string_to_spv("mul_mat_i8_tensorwise", "mul_mat_i8_tensorwise.comp", {}); #if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) string_to_spv("mul_mat_i8_tensorwise", "mul_mat_i8_tensorwise_cm1.comp", {}, true, true); From 7a91b8f8932863403fcbe7d233ae17bcabe90419 Mon Sep 17 00:00:00 2001 From: noctrex Date: Thu, 27 Aug 2026 16:09:03 +0300 Subject: [PATCH 4/4] ggml : support F32 input for int8 convrot tensorwise mulmat in Vulkan The tensorwise mulmat accepts src1 as either the packed QUANTIZE_I8_CONVROT output or raw F32, but the Vulkan backend only handled the packed form, so F32 inputs fell back to the CPU via the scheduler. Relax the supports_op gate to accept the F32 form, bounded by maxStorageBufferRange for the packed staging size. Quantize the F32 input on the fly into a grow-on-demand device prealloc buffer in the packed payload+scales layout, then run the existing tensorwise GEMM unchanged. Extend the Vulkan test with F32-input cases (GPU packed equivalence, exact CPU reference). --- src/ggml-vulkan/ggml-vulkan.cpp | 133 +++++++++++++++++++++++--------- tests/test-int8-convrot.cpp | 78 +++++++++++++++++-- 2 files changed, 168 insertions(+), 43 deletions(-) diff --git a/src/ggml-vulkan/ggml-vulkan.cpp b/src/ggml-vulkan/ggml-vulkan.cpp index 10398a7094..aa9db88478 100644 --- a/src/ggml-vulkan/ggml-vulkan.cpp +++ b/src/ggml-vulkan/ggml-vulkan.cpp @@ -2353,7 +2353,9 @@ struct ggml_backend_vk_context { size_t semaphore_idx, event_idx; ggml_vk_garbage_collector gc; size_t prealloc_size_x, prealloc_size_y, prealloc_size_split_k, prealloc_size_add_rms_partials, prealloc_size_add_rms_partials_offset; + size_t prealloc_size_qconvrot; vk_buffer prealloc_x, prealloc_y, prealloc_split_k, prealloc_add_rms_partials, sync_staging; + vk_buffer prealloc_qconvrot; vk::Fence fence, almost_ready_fence; bool submit_pending {}; bool almost_ready_fence_pending {}; @@ -2377,7 +2379,7 @@ struct ggml_backend_vk_context { // Track which prealloc buffers have pending reads that need to be synchronized. // These are checked before writing to the buffer (and call ggml_vk_sync_buffers if set), // and set to true after the buffer contents are consumed. - bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync; + bool prealloc_x_need_sync, prealloc_y_need_sync, prealloc_split_k_need_sync, prealloc_qconvrot_need_sync; vk_context_ref compute_ctx; @@ -3648,7 +3650,7 @@ static void ggml_vk_sync_buffers(ggml_backend_vk_context* ctx, vk_context& subct const bool transfer_queue = subctx->p->q->transfer_only; if (ctx) { - ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = false; + ctx->prealloc_x_need_sync = ctx->prealloc_y_need_sync = ctx->prealloc_split_k_need_sync = ctx->prealloc_qconvrot_need_sync = false; } subctx->s->buffer->buf.pipelineBarrier( @@ -7612,6 +7614,7 @@ static void ggml_vk_init(ggml_backend_vk_context * ctx, size_t idx) { ctx->prealloc_size_x = 0; ctx->prealloc_size_y = 0; ctx->prealloc_size_split_k = 0; + ctx->prealloc_size_qconvrot = 0; // Fixed size of 1KB, for deterministic behavior ctx->prealloc_size_add_rms_partials = 1024; @@ -10014,27 +10017,39 @@ static bool ggml_vk_can_use_mul_mat_i8_tensorwise(const vk_device & device, cons const ggml_tensor * input = dst->src[1]; const ggml_tensor * weight_scale = dst->src[2]; const ggml_tensor * bias = dst->src[3]; - const ggml_tensor * logical_input = input != nullptr ? input->src[0] : nullptr; if (!device->integer_dot_product || weight == nullptr || input == nullptr || - weight->type != GGML_TYPE_I8 || input->type != GGML_TYPE_I8 || - input->op != GGML_OP_QUANTIZE_I8_CONVROT || logical_input == nullptr || - logical_input->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 || - weight_scale == nullptr || + weight->type != GGML_TYPE_I8 || dst->type != GGML_TYPE_F32 || weight->ne[2] != 1 || weight->ne[3] != 1 || + weight_scale == nullptr || !ggml_is_contiguous(weight) || !ggml_is_contiguous(input) || - !ggml_is_contiguous(logical_input) || !ggml_is_contiguous(weight_scale) || - !ggml_is_contiguous(dst)) { + !ggml_is_contiguous(weight_scale) || !ggml_is_contiguous(dst)) { return false; } + // The input is either quantized on the fly from F32, or prequantized + // with ggml_quantize_i8_convrot into the packed payload+scales layout. + int64_t rows; + if (input->type == GGML_TYPE_F32) { + rows = ggml_nrows(input); + } else { + const ggml_tensor * logical_input = input->src[0]; + if (input->type != GGML_TYPE_I8 || input->op != GGML_OP_QUANTIZE_I8_CONVROT || + logical_input == nullptr || logical_input->type != GGML_TYPE_F32 || + !ggml_is_contiguous(logical_input)) { + return false; + } + rows = ggml_nrows(logical_input); + } + const int convrot_group_size = ggml_get_op_params_i32(dst, 2); - const int64_t rows = ggml_nrows(logical_input); const int64_t rows_padded = GGML_PAD(rows, 4); const int64_t scale_rows = (rows * (int64_t) sizeof(float) + weight->ne[0] - 1) / weight->ne[0]; return (convrot_group_size == 64 || convrot_group_size == 256) && - ggml_get_op_params_i32(input, 0) == convrot_group_size && + input->ne[0] == weight->ne[0] && weight->ne[0] % convrot_group_size == 0 && - input->ne[0] == weight->ne[0] && input->ne[1] == rows_padded + scale_rows && + (input->type == GGML_TYPE_F32 || + (ggml_get_op_params_i32(input, 0) == convrot_group_size && + input->ne[1] == rows_padded + scale_rows)) && ggml_nrows(dst) == rows && weight_scale->type == GGML_TYPE_F32 && ggml_nelements(weight_scale) == weight->ne[1] && (bias == nullptr || @@ -10042,7 +10057,10 @@ static bool ggml_vk_can_use_mul_mat_i8_tensorwise(const vk_device & device, cons ggml_nelements(bias) == weight->ne[1])) && weight->ne[0] / 4 <= std::numeric_limits::max() && weight->ne[1] <= std::numeric_limits::max() && - rows <= std::numeric_limits::max(); + rows <= std::numeric_limits::max() && + (input->type != GGML_TYPE_F32 || + (uint64_t) weight->ne[0] * rows_padded + (uint64_t) rows * sizeof(float) <= + device->properties.limits.maxStorageBufferRange); #else GGML_UNUSED(device); GGML_UNUSED(dst); @@ -10050,34 +10068,37 @@ static bool ggml_vk_can_use_mul_mat_i8_tensorwise(const vk_device & device, cons #endif } -static void ggml_vk_quantize_i8_convrot( +static void ggml_vk_quantize_i8_convrot_subbuf( ggml_backend_vk_context * ctx, vk_context & subctx, - const ggml_tensor * src, ggml_tensor * dst) { - GGML_ASSERT(ggml_vk_can_use_quantize_i8_convrot(ctx->device, dst)); - const int group_size = ggml_get_op_params_i32(dst, 0); + const vk_subbuffer & src_buf, const vk_subbuffer & quantized_buf, + const vk_subbuffer & scales_buf, uint32_t k, uint32_t rows, int group_size) { vk_pipeline pipeline = group_size == 64 ? ctx->device->pipeline_quantize_i8_convrot_h64 : ctx->device->pipeline_quantize_i8_convrot; ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1); + const vk_quantize_i8_convrot_push_constants pc = { k, rows }; + const uint32_t workgroups = std::min(rows, ctx->device->properties.limits.maxComputeWorkGroupCount[0]); + ggml_vk_dispatch_pipeline( + ctx, subctx, pipeline, { src_buf, quantized_buf, scales_buf }, pc, { workgroups, 1, 1 }); +} + +static void ggml_vk_quantize_i8_convrot( + ggml_backend_vk_context * ctx, vk_context & subctx, + const ggml_tensor * src, ggml_tensor * dst) { + GGML_ASSERT(ggml_vk_can_use_quantize_i8_convrot(ctx->device, dst)); + const int group_size = ggml_get_op_params_i32(dst, 0); + const uint32_t rows = (uint32_t) ggml_nrows(src); const uint64_t rows_padded = GGML_PAD((uint64_t) rows, 4); const uint64_t scale_offset = (uint64_t) src->ne[0] * rows_padded; const vk_subbuffer src_buf = ggml_vk_tensor_subbuffer(ctx, src); const vk_subbuffer dst_buf = ggml_vk_tensor_subbuffer(ctx, dst); - const vk_subbuffer quantized_buf = { dst_buf.buffer, dst_buf.offset, scale_offset }; - const vk_subbuffer scales_buf = { - dst_buf.buffer, - dst_buf.offset + scale_offset, - (uint64_t) rows * sizeof(float), - }; - const vk_quantize_i8_convrot_push_constants pc = { - (uint32_t) src->ne[0], - rows, - }; - const uint32_t workgroups = std::min(rows, ctx->device->properties.limits.maxComputeWorkGroupCount[0]); - ggml_vk_dispatch_pipeline( - ctx, subctx, pipeline, { src_buf, quantized_buf, scales_buf }, pc, { workgroups, 1, 1 }); + ggml_vk_quantize_i8_convrot_subbuf( + ctx, subctx, src_buf, + { dst_buf.buffer, dst_buf.offset, scale_offset }, + { dst_buf.buffer, dst_buf.offset + scale_offset, (uint64_t) rows * sizeof(float) }, + (uint32_t) src->ne[0], rows, group_size); } static void ggml_vk_mul_mat_i8_tensorwise( @@ -10100,13 +10121,38 @@ static void ggml_vk_mul_mat_i8_tensorwise( const uint64_t rows_padded = GGML_PAD((uint64_t) rows, 4); const uint64_t scale_offset = (uint64_t) weight->ne[0] * rows_padded; const vk_subbuffer weight_buf = ggml_vk_tensor_subbuffer(ctx, weight); - const vk_subbuffer input_buf = ggml_vk_tensor_subbuffer(ctx, input); - const vk_subbuffer quantized_buf = { input_buf.buffer, input_buf.offset, scale_offset }; - const vk_subbuffer activation_scales_buf = { - input_buf.buffer, - input_buf.offset + scale_offset, - (uint64_t) rows * sizeof(float), - }; + vk_subbuffer quantized_buf; + vk_subbuffer activation_scales_buf; + if (input->type == GGML_TYPE_F32) { + // Quantize the F32 input on the fly into the preallocated packed layout. + const uint64_t qconvrot_size = scale_offset + (uint64_t) rows * sizeof(float); + if (ctx->prealloc_size_qconvrot < qconvrot_size) { + ctx->prealloc_size_qconvrot = qconvrot_size; + ggml_vk_preallocate_buffers(ctx, subctx); + } + if (ctx->prealloc_qconvrot_need_sync) { + ggml_vk_sync_buffers(ctx, subctx); + } + quantized_buf = { ctx->prealloc_qconvrot, 0, scale_offset }; + activation_scales_buf = { + ctx->prealloc_qconvrot, + scale_offset, + (uint64_t) rows * sizeof(float), + }; + ggml_vk_quantize_i8_convrot_subbuf( + ctx, subctx, ggml_vk_tensor_subbuffer(ctx, input), + quantized_buf, activation_scales_buf, + (uint32_t) input->ne[0], rows, ggml_get_op_params_i32(dst, 2)); + ggml_vk_sync_buffers(ctx, subctx); + } else { + const vk_subbuffer input_buf = ggml_vk_tensor_subbuffer(ctx, input); + quantized_buf = { input_buf.buffer, input_buf.offset, scale_offset }; + activation_scales_buf = { + input_buf.buffer, + input_buf.offset + scale_offset, + (uint64_t) rows * sizeof(float), + }; + } const vk_subbuffer weight_scales_buf = ggml_vk_tensor_subbuffer(ctx, weight_scale); const vk_subbuffer bias_buf = bias != nullptr ? ggml_vk_tensor_subbuffer(ctx, bias) @@ -10134,6 +10180,9 @@ static void ggml_vk_mul_mat_i8_tensorwise( dispatch(ctx->device->pipeline_mul_mat_i8_tensorwise_cm1, 0, coop_rows); } dispatch(ctx->device->pipeline_mul_mat_i8_tensorwise, coop_rows, rows - coop_rows); + if (input->type == GGML_TYPE_F32) { + ctx->prealloc_qconvrot_need_sync = true; + } } static void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx) { @@ -15316,6 +15365,14 @@ static void ggml_vk_preallocate_buffers(ggml_backend_vk_context * ctx, vk_contex } ctx->prealloc_split_k = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_split_k); } + if (ctx->prealloc_qconvrot == nullptr || (ctx->prealloc_size_qconvrot > 0 && ctx->prealloc_qconvrot->size < ctx->prealloc_size_qconvrot)) { + VK_LOG_MEMORY("ggml_vk_preallocate_buffers(qconvrot_size: " << ctx->prealloc_size_qconvrot << ")"); + // Resize buffer + if (ctx->prealloc_qconvrot != nullptr) { + ggml_vk_destroy_buffer(ctx->prealloc_qconvrot); + } + ctx->prealloc_qconvrot = ggml_vk_create_buffer_device(ctx->device, ctx->prealloc_size_qconvrot); + } if (ctx->prealloc_add_rms_partials == nullptr || (ctx->prealloc_size_add_rms_partials > 0 && ctx->prealloc_add_rms_partials->size < ctx->prealloc_size_add_rms_partials)) { VK_LOG_MEMORY("ggml_vk_preallocate_buffers(add_partials_size: " << ctx->prealloc_add_rms_partials << ")"); // Resize buffer @@ -15969,6 +16026,7 @@ static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) { ggml_vk_destroy_buffer(ctx->prealloc_x); ggml_vk_destroy_buffer(ctx->prealloc_y); ggml_vk_destroy_buffer(ctx->prealloc_split_k); + ggml_vk_destroy_buffer(ctx->prealloc_qconvrot); ggml_vk_destroy_buffer(ctx->prealloc_add_rms_partials); ggml_vk_destroy_buffer(ctx->sync_staging); @@ -15979,6 +16037,7 @@ static void ggml_vk_cleanup(ggml_backend_vk_context * ctx) { ctx->prealloc_size_x = 0; ctx->prealloc_size_y = 0; ctx->prealloc_size_split_k = 0; + ctx->prealloc_size_qconvrot = 0; for (auto& event : ctx->gc.events) { ctx->device->device.destroyEvent(event); diff --git a/tests/test-int8-convrot.cpp b/tests/test-int8-convrot.cpp index c0d69b6f26..baf361cd2e 100644 --- a/tests/test-int8-convrot.cpp +++ b/tests/test-int8-convrot.cpp @@ -66,11 +66,23 @@ int main(int argc, char ** argv) { ggml_tensor * q_large = ggml_quantize_i8_convrot(ctx, x_large, 256); ggml_tensor * fused_large = ggml_mul_mat_i8_tensorwise(ctx, w_large, q_large, ws_large, b_large, 256); ggml_tensor * fused_large_no_bias = ggml_mul_mat_i8_tensorwise(ctx, w_large, q_large, ws_large, nullptr, 256); + ggml_tensor * q_large_g64 = ggml_quantize_i8_convrot(ctx, x_large, 64); + ggml_tensor * fused_g64_packed = ggml_mul_mat_i8_tensorwise(ctx, w_large, q_large_g64, ws_large, b_large, 64); + // F32-input form: the input is quantized on the fly inside the op. + ggml_tensor * fused256_f32 = ggml_mul_mat_i8_tensorwise(ctx, w256, x256, ws256, b256, 256); + ggml_tensor * fused_f32_large = ggml_mul_mat_i8_tensorwise(ctx, w_large, x_large, ws_large, b_large, 256); + ggml_tensor * fused_f32_large_no_bias = ggml_mul_mat_i8_tensorwise(ctx, w_large, x_large, ws_large, nullptr, 256); + ggml_tensor * fused_f32_g64 = ggml_mul_mat_i8_tensorwise(ctx, w_large, x_large, ws_large, b_large, 64); ggml_cgraph * graph = ggml_new_graph(ctx); ggml_build_forward_expand(graph, fused256); ggml_build_forward_expand(graph, fused_large); ggml_build_forward_expand(graph, fused_large_no_bias); + ggml_build_forward_expand(graph, fused_g64_packed); + ggml_build_forward_expand(graph, fused256_f32); + ggml_build_forward_expand(graph, fused_f32_large); + ggml_build_forward_expand(graph, fused_f32_large_no_bias); + ggml_build_forward_expand(graph, fused_f32_g64); ggml_backend_t backend = nullptr; if (use_cuda || use_vulkan) { @@ -93,9 +105,14 @@ int main(int argc, char ** argv) { ggml_backend_cpu_set_n_threads(backend, 2); } if (!ggml_backend_supports_op(backend, q_large) || + !ggml_backend_supports_op(backend, q_large_g64) || !ggml_backend_supports_op(backend, fused_large) || - !ggml_backend_supports_op(backend, fused_large_no_bias)) { - std::fprintf(stderr, "backend does not report packed INT8 convrot matmul support\n"); + !ggml_backend_supports_op(backend, fused_large_no_bias) || + !ggml_backend_supports_op(backend, fused_g64_packed) || + !ggml_backend_supports_op(backend, fused_f32_large) || + !ggml_backend_supports_op(backend, fused_f32_large_no_bias) || + !ggml_backend_supports_op(backend, fused_f32_g64)) { + std::fprintf(stderr, "backend does not report INT8 convrot matmul support\n"); return 1; } ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx, backend); @@ -167,20 +184,47 @@ int main(int argc, char ** argv) { } std::vector fused_data(4); ggml_backend_tensor_get(fused256, fused_data.data(), 0, ggml_nbytes(fused256)); - for (size_t i = 0; i < expected_fused.size(); ++i) { - if (!nearly_equal(fused_data[i], expected_fused[i])) { - std::fprintf(stderr, "fused INT8 convrot mismatch at %zu: %.8f != %.8f\n", i, fused_data[i], expected_fused[i]); - return 1; + // The F32-input form must match the packed form: same quantizer, same GEMM. + { + std::vector fused256_f32_data(4); + ggml_backend_tensor_get(fused256_f32, fused256_f32_data.data(), 0, ggml_nbytes(fused256_f32)); + for (size_t i = 0; i < fused_data.size(); ++i) { + if (!nearly_equal(fused256_f32_data[i], fused_data[i])) { + std::fprintf(stderr, "F32-input vs packed mismatch at %zu: %.8f != %.8f\n", i, fused256_f32_data[i], fused_data[i]); + return 1; + } + } + } + if (!use_cuda && !use_vulkan) { + // On CPU the F32-input path quantizes exactly like the reference above, + // so the packed-form expected values apply unchanged. + for (size_t i = 0; i < expected_fused.size(); ++i) { + if (!nearly_equal(fused_data[i], expected_fused[i])) { + std::fprintf(stderr, "fused INT8 convrot mismatch at %zu: %.8f != %.8f\n", i, fused_data[i], expected_fused[i]); + return 1; + } } } std::vector q_large_data(ggml_nbytes(q_large)); std::vector fused_large_data(ggml_nelements(fused_large)); std::vector fused_large_no_bias_data(ggml_nelements(fused_large_no_bias)); + std::vector fused_g64_packed_data(ggml_nelements(fused_g64_packed)); + std::vector fused_f32_large_data(ggml_nelements(fused_f32_large)); + std::vector fused_f32_large_no_bias_data(ggml_nelements(fused_f32_large_no_bias)); + std::vector fused_f32_g64_data(ggml_nelements(fused_f32_g64)); ggml_backend_tensor_get(q_large, q_large_data.data(), 0, ggml_nbytes(q_large)); ggml_backend_tensor_get(fused_large, fused_large_data.data(), 0, ggml_nbytes(fused_large)); ggml_backend_tensor_get( fused_large_no_bias, fused_large_no_bias_data.data(), 0, ggml_nbytes(fused_large_no_bias)); + ggml_backend_tensor_get( + fused_g64_packed, fused_g64_packed_data.data(), 0, ggml_nbytes(fused_g64_packed)); + ggml_backend_tensor_get( + fused_f32_large, fused_f32_large_data.data(), 0, ggml_nbytes(fused_f32_large)); + ggml_backend_tensor_get( + fused_f32_large_no_bias, fused_f32_large_no_bias_data.data(), 0, ggml_nbytes(fused_f32_large_no_bias)); + ggml_backend_tensor_get( + fused_f32_g64, fused_f32_g64_data.data(), 0, ggml_nbytes(fused_f32_g64)); const int large_rows_padded = (large_rows + 3) & ~3; std::vector large_activation_scales(large_rows); std::memcpy( @@ -216,6 +260,28 @@ int main(int argc, char ** argv) { } } + // The F32-input form must match the packed form: same quantizer, same GEMM. + for (size_t i = 0; i < fused_large_data.size(); ++i) { + if (!nearly_equal(fused_f32_large_data[i], fused_large_data[i])) { + std::fprintf( + stderr, "F32-input vs packed mismatch at %zu: %.8f != %.8f\n", + i, fused_f32_large_data[i], fused_large_data[i]); + return 1; + } + if (!nearly_equal(fused_f32_large_no_bias_data[i], fused_large_no_bias_data[i])) { + std::fprintf( + stderr, "F32-input no-bias vs packed mismatch at %zu: %.8f != %.8f\n", + i, fused_f32_large_no_bias_data[i], fused_large_no_bias_data[i]); + return 1; + } + if (!nearly_equal(fused_f32_g64_data[i], fused_g64_packed_data[i])) { + std::fprintf( + stderr, "F32-input G64 vs packed G64 mismatch at %zu: %.8f != %.8f\n", + i, fused_f32_g64_data[i], fused_g64_packed_data[i]); + return 1; + } + } + ggml_backend_buffer_free(buffer); ggml_backend_free(backend); ggml_free(ctx);