Compare commits

..
Author SHA1 Message Date
Ruben Ortlam 779b413a67 vulkan: add fp8 and scaled matmul support (#29791)
* vulkan: add fp8 and scaled matmul support

* remove supports_op rejection, reject unaligned src, fix get_rows issue

* add f8 copy pipelines

* fix test issue

* handle strided scale-views

* fixes after review

* use get_rows, not quant, use fastdiv for scaling shader

* fix test issue

* fix missing initializer
2026-11-30 19:22:22 +01:00
Albert Lie 이영덕 7dfa241048 jinja : use python semantics for the modulo operator (#33336)
Assisted-by: Claude
2026-11-30 19:15:12 +01:00
neoliandXuan-Son Nguyen 49f972e139 llama: Fix the Clef-Flash model loading crash on arm cpus (#32962)
* fix crash while loading clef model on arm cpu

* Add has_lm_head() judgement in qwen35 for clef models

* add comment for LLM_TENSOR_OUTPUT_LEXICAL tensor type

* Update src/llama-arch.cpp

---------

Co-authored-by: Xuan-Son Nguyen <thichthat@gmail.com>
2026-11-30 19:09:20 +01:00
Pascal 019951ec44 CUDA: one thread per row for soft_max and sum_rows on short rows (#33187)
* CUDA: one thread per row for soft_max and sum_rows on short rows

A row shorter than a warp left most of the block idle, and pooling the
Hy4 flash indexer keys softmaxes and sums millions of rows of 4. With
ncols <= 8 each thread now handles a whole row. On 3.4M rows of 4,
soft_max goes from 1550 to 24 us and sum_rows from 1549 to 11 us.
Hy4 flash decode goes from 66 to 87 t/s at 8k context, from 39 to 73
t/s at 32k and from 17 to 48 t/s at 107k.

* CUDA: use fastdiv for the short row soft_max indices

The one thread per row soft_max spent more time in its 64-bit row
index divisions than on the row itself. On 3.4M rows of 4 without
mask, soft_max goes from 23.9 to 18.2 us.

* tests : cover a broadcast mask on the short row soft_max

The short row cases only used a mask of the full shape, so the
fastmodulo on the mask indices of dims 2 and 3 never ran on a
broadcast mask.
2026-11-30 18:32:15 +01:00
37 changed files with 696 additions and 1034 deletions
+26 -7
View File
@@ -216,14 +216,33 @@ static value exec_binary_op(context & ctx, const token & op, value left_val, val
}
return mk_val<value_float>(std::floor(a / b));
} else if (op.value == "%") {
double rem = std::fmod(a, b);
JJ_DEBUG("Modulo operation: %f %% %f = %f", a, b, rem);
bool is_float = is_val<value_float>(left_val) || is_val<value_float>(right_val);
if (is_float) {
return mk_val<value_float>(rem);
} else {
return mk_val<value_int>(float_to_int(rem));
// python semantics: result has the sign of the divisor
if (is_val<value_int>(left_val) && is_val<value_int>(right_val)) {
const int64_t x = left_val->as_int();
const int64_t y = right_val->as_int();
if (y == 0) {
throw std::runtime_error("integer modulo by zero");
}
if (y == -1) {
// INT64_MIN % -1 is UB
return mk_val<value_int>(0);
}
int64_t r = x % y;
if (r != 0 && (r < 0) != (y < 0)) {
r += y;
}
return mk_val<value_int>(r);
}
double rem = std::fmod(a, b);
if (rem != 0) {
if ((rem < 0) != (b < 0)) {
rem += b;
}
} else {
rem = std::copysign(0.0, b);
}
JJ_DEBUG("Modulo operation: %f %% %f = %f", a, b, rem);
return mk_val<value_float>(rem);
} else if (op.value == "<") {
JJ_DEBUG("Comparison operation: %f < %f is %d", a, b, a < b);
return mk_val<value_bool>(a < b);
+21
View File
@@ -33,6 +33,27 @@ static __device__ __forceinline__ float reduce_row_f32(const float * x, const in
return sum;
}
#define CUDA_REDUCE_ROWS_SHORT_NCOLS 8
template <bool norm>
static __global__ void reduce_rows_f32_short(const float * x_ptr, float * dst_ptr, const int ncols, const int64_t nrows) {
const int64_t row = (int64_t) blockIdx.x*blockDim.x + threadIdx.x;
ggml_cuda_pdl_sync();
if (row >= nrows) {
return;
}
const float * GGML_CUDA_RESTRICT x = x_ptr + row*ncols;
float sum = 0.0f;
for (int i = 0; i < ncols; ++i) {
sum += x[i];
}
dst_ptr[row] = norm ? sum / ncols : sum;
}
// Row reduction kernel template - compute sum (norm=false) or mean (norm=true)
template <bool norm>
static __global__ void reduce_rows_f32(const float * x_ptr, float * dst_ptr, const int ncols) {
+67
View File
@@ -142,6 +142,64 @@ static __global__ void soft_max_f32(
}
}
#define CUDA_SOFT_MAX_SHORT_NCOLS 8
template <typename T>
static __global__ void soft_max_f32_short(
const float * x, const T * mask, const float * sinks, float * dst, const soft_max_params p,
const uint3 ne01_fdv, const uint3 ne02_fdv, const uint3 ne12_fdv, const uint3 ne13_fdv) {
const uint32_t rowx = blockIdx.x*blockDim.x + threadIdx.x;
if (rowx >= p.nrows_x) {
return;
}
const uint32_t i012 = fastdiv(rowx, ne01_fdv);
const uint32_t i01 = rowx - i012*ne01_fdv.z;
const uint32_t i03 = fastdiv(i012, ne02_fdv);
const uint32_t i02 = i012 - i03*ne02_fdv.z;
const uint32_t i12 = fastmodulo(i02, ne12_fdv);
const uint32_t i13 = fastmodulo(i03, ne13_fdv);
const int ncols = p.ncols;
x += (int64_t) rowx*ncols;
mask += (i01*p.nb11 + i12*p.nb12 + i13*p.nb13) / sizeof(T) * (mask != nullptr);
dst += (int64_t) rowx*ncols;
const float slope = get_alibi_slope(p.max_bias, i02, p.n_head_log2, p.m0, p.m1);
float vals[CUDA_SOFT_MAX_SHORT_NCOLS];
float max_val = sinks ? sinks[i02] : -INFINITY;
#pragma unroll
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
if (col < ncols) {
vals[col] = x[col]*p.scale + (mask ? slope*t2f32(mask[col]) : 0.0f);
max_val = max(max_val, vals[col]);
}
}
float sum = sinks ? expf(sinks[i02] - max_val) : 0.0f;
#pragma unroll
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
if (col < ncols) {
vals[col] = expf(vals[col] - max_val);
sum += vals[col];
}
}
const float inv_sum = 1.0f / sum;
#pragma unroll
for (int col = 0; col < CUDA_SOFT_MAX_SHORT_NCOLS; ++col) {
if (col < ncols) {
dst[col] = vals[col]*inv_sum;
}
}
}
template <int n_elem_per_thread = 0>
static __device__ void soft_max_f32_parallelize_cols_single_row(const float * __restrict__ x,
float * __restrict__ dst,
@@ -392,6 +450,15 @@ static void soft_max_f32_cuda(const float * x,
int nth = WARP_SIZE;
const int64_t ncols_x = params.ncols;
if (ncols_x <= CUDA_SOFT_MAX_SHORT_NCOLS && params.nrows_x <= INT_MAX) {
const uint3 ne01_fdv = init_fastdiv_values(params.ne01);
const uint3 ne02_fdv = init_fastdiv_values(params.ne02);
const uint3 ne12_fdv = init_fastdiv_values(params.ne12);
const uint3 ne13_fdv = init_fastdiv_values(params.ne13);
soft_max_f32_short<<<(params.nrows_x + 255) / 256, 256, 0, stream>>>(x, mask, sinks, dst, params, ne01_fdv, ne02_fdv, ne12_fdv, ne13_fdv);
return;
}
while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
const dim3 block_dims(nth, 1, 1);
const dim3 block_nums(params.ne01, params.ne02, params.ne03);
+7
View File
@@ -4,6 +4,13 @@
void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
const int id = ggml_cuda_get_device();
const int nsm = ggml_cuda_info().devices[id].nsm;
if (ncols <= CUDA_REDUCE_ROWS_SHORT_NCOLS) {
const dim3 block_dims(256, 1, 1);
const dim3 block_nums((nrows + 255) / 256, 1, 1);
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
ggml_cuda_kernel_launch(reduce_rows_f32_short</*norm=*/false>, launch_params, x, dst, ncols, (int64_t) nrows);
return;
}
const dim3 block_nums(nrows, 1, 1);
if ((nrows / nsm) < 2) {
const dim3 block_dims(512, 1, 1);
+4 -3
View File
@@ -1019,9 +1019,11 @@ void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph
ggml_flash_attn_ext_set_sinks(tensor_clone, src_clone[4]);
}
} else if (tensor->op == GGML_OP_MUL_MAT) {
tensor_clone = ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
tensor_clone = src_clone[2] ? ggml_mul_mat_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], nullptr)
: ggml_mul_mat(ggml_ctx, src_clone[0], src_clone[1]);
} else if (tensor->op == GGML_OP_MUL_MAT_ID) {
tensor_clone = ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
tensor_clone = src_clone[3] ? ggml_mul_mat_id_ext(ggml_ctx, src_clone[0], src_clone[1], src_clone[2], src_clone[3], nullptr)
: ggml_mul_mat_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
} else if (tensor->op == GGML_OP_SUB) {
tensor_clone = ggml_sub(ggml_ctx, src_clone[0], src_clone[1]);
} else if (tensor->op == GGML_OP_MUL) {
@@ -1612,4 +1614,3 @@ void ggml_vk_check_results_1(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph
VK_LOG_DEBUG("END ggml_vk_check_results_1(" << tensor->name << ")");
}
#endif
@@ -45,6 +45,18 @@ struct vk_mat_mat_stream_k_fixup_push_constants {
static_assert(sizeof(vk_mat_mat_push_constants) <= 128);
struct vk_op_mul_mat_scale_push_constants {
uint32_t ne0; uint32_t ne1; uint32_t ne2; uint32_t ne3;
uint32_t sne0; uint32_t sne1; uint32_t sne2; uint32_t sne3;
uint32_t snb0; uint32_t snb1; uint32_t snb2; uint32_t snb3;
uint32_t per_expert;
uint32_t has_ids;
uint32_t ids_s0; uint32_t ids_s1; uint32_t ids_offset;
uint32_t ne_012mp; uint32_t ne_012L;
uint32_t ne_01mp; uint32_t ne_01L;
uint32_t ne_0mp; uint32_t ne_0L;
};
struct vk_mat_vec_push_constants {
uint32_t ncols;
uint32_t stride_a;
@@ -1255,4 +1267,3 @@ static vk_op_binary_push_constants ggml_vk_rms_norm_push_constants(
eps, 0.0f, (int32_t)num_partials,
};
}
+7 -2
View File
@@ -864,6 +864,7 @@ struct vk_device_struct {
vk_pipeline pipeline_matmul_split_k_reduce;
vk_pipeline pipeline_matmul_stream_k_fixup;
vk_pipeline pipeline_mul_mat_scale_f32;
vk_pipeline pipeline_quantize_q8_1_x4;
vk_pipeline pipeline_dequant[GGML_TYPE_COUNT];
@@ -921,8 +922,8 @@ struct vk_device_struct {
vk_pipeline pipeline_roll_f32;
vk_pipeline pipeline_repeat_i32, pipeline_repeat_back_f32;
vk_pipeline pipeline_repeat_i16;
vk_pipeline pipeline_cpy_32, pipeline_cpy_f32_f16, pipeline_cpy_16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16, pipeline_cpy_bf16_f32, pipeline_cpy_bf16_f16, pipeline_cpy_f32_i32, pipeline_cpy_i32_f32;
vk_pipeline pipeline_contig_cpy_32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16, pipeline_contig_cpy_bf16_f32, pipeline_contig_cpy_bf16_f16, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32;
vk_pipeline pipeline_cpy_32, pipeline_cpy_f32_f16, pipeline_cpy_16, pipeline_cpy_f16_f32, pipeline_cpy_f32_bf16, pipeline_cpy_bf16_f32, pipeline_cpy_bf16_f16, pipeline_cpy_8, pipeline_cpy_f8_e4m3_f16, pipeline_cpy_f32_i32, pipeline_cpy_i32_f32;
vk_pipeline pipeline_contig_cpy_32, pipeline_contig_cpy_f32_f16, pipeline_contig_cpy_16, pipeline_contig_cpy_f16_f32, pipeline_contig_cpy_f32_bf16, pipeline_contig_cpy_bf16_f32, pipeline_contig_cpy_bf16_f16, pipeline_contig_cpy_8, pipeline_contig_cpy_f8_e4m3_f16, pipeline_contig_cpy_f32_i32, pipeline_contig_cpy_i32_f32;
vk_pipeline pipeline_cpy_f32_quant[GGML_TYPE_COUNT];
vk_pipeline pipeline_cpy_quant_f32[GGML_TYPE_COUNT];
vk_pipeline pipeline_cpy_transpose_16, pipeline_cpy_transpose_32;
@@ -1175,6 +1176,10 @@ typedef std::vector<vk_submission> vk_sequence;
#define MAT_VEC_FUSION_FLAGS_SCALE1 0x8
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE 0x10
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC 0x20
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_2D 0x40
struct vk_staging_memcpy {
vk_staging_memcpy(void * _dst, const void * _src, size_t _n) : dst(_dst), src(_src), n(_n) {}
+155 -11
View File
@@ -2776,6 +2776,9 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
}
#undef X_CM2
create_mm_pipelines({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, false, true}, tc_mmq, "matmul_f8_e4m3_f16_f16acc", matmul_f8_e4m3_f16_f16acc_cm2_len, matmul_f8_e4m3_f16_f16acc_cm2_data, sizeof(vk_mat_mat_push_constants), 3, cm2_spec, true);
create_mm_pipelines({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, false, false}, tc_mmq, "matmul_f8_e4m3_f16", matmul_f8_e4m3_f16_cm2_len, matmul_f8_e4m3_f16_cm2_data, sizeof(vk_mat_mat_push_constants), 3, cm2_spec, true);
GGML_ASSERT(device->subgroup_ballot);
create_mm_pipelines({GGML_TYPE_F16, GGML_TYPE_F16, true, true}, tc_mm, "matmul_id_subgroup_f16_f16acc", matmul_id_subgroup_f16_f16acc_cm2_len, matmul_id_subgroup_f16_f16acc_cm2_data, sizeof(vk_mat_mat_id_push_constants), 5, cm2_spec, true);
@@ -2820,6 +2823,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
FOR_EACH_LUT_FP4_TYPE(X_CM2_ID)
}
#undef X_CM2_ID
create_mm_pipelines({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, true, true}, tc_mmqid, "matmul_id_subgroup_f8_e4m3_f16_f16acc", matmul_id_subgroup_f8_e4m3_f16_f16acc_cm2_len, matmul_id_subgroup_f8_e4m3_f16_f16acc_cm2_data, sizeof(vk_mat_mat_id_push_constants), 5, cm2_spec, true);
create_mm_pipelines({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, true, false}, tc_mmqid, "matmul_id_subgroup_f8_e4m3_f16", matmul_id_subgroup_f8_e4m3_f16_cm2_len, matmul_id_subgroup_f8_e4m3_f16_cm2_data, sizeof(vk_mat_mat_id_push_constants), 5, cm2_spec, true);
} else
#endif // defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
#if defined(VK_KHR_cooperative_matrix) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
@@ -2943,6 +2948,14 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
cm1_create_mmq({GGML_TYPE_Q6_K, GGML_TYPE_Q8_1, false, false}, tc_mmq_cm1_int_k, "matmul_q6_k_q8_1", matmul_q6_k_q8_1_cm1_len, matmul_q6_k_q8_1_cm1_data, sizeof(vk_mat_mat_push_constants), 3);
if (!rdna4) { cm1_create_mmq({GGML_TYPE_NVFP4, GGML_TYPE_Q8_1, false, false}, tc_mmq_cm1_int_k, "matmul_nvfp4_q8_1", matmul_nvfp4_q8_1_cm1_len, matmul_nvfp4_q8_1_cm1_data, sizeof(vk_mat_mat_push_constants), 3); }
}
if (device->coopmat_acc_f16_support) {
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, false, true}, tc_mmq, "matmul_f8_e4m3_f32_f16acc", matmul_f8_e4m3_f32_f16acc_cm1_len, matmul_f8_e4m3_f32_f16acc_cm1_data, sizeof(vk_mat_mat_push_constants), 3);
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, false, true}, tc_mmq, "matmul_f8_e4m3_f16_f16acc", matmul_f8_e4m3_f16_f16acc_cm1_len, matmul_f8_e4m3_f16_f16acc_cm1_data, sizeof(vk_mat_mat_push_constants), 3);
}
if (device->coopmat_acc_f32_support) {
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, false, false}, tc_mmq, "matmul_f8_e4m3_f32", matmul_f8_e4m3_f32_cm1_len, matmul_f8_e4m3_f32_cm1_data, sizeof(vk_mat_mat_push_constants), 3);
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, false, false}, tc_mmq, "matmul_f8_e4m3_f16", matmul_f8_e4m3_f16_cm1_len, matmul_f8_e4m3_f16_cm1_data, sizeof(vk_mat_mat_push_constants), 3);
}
GGML_ASSERT(device->subgroup_ballot);
@@ -3000,6 +3013,14 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
FOR_EACH_LUT_FP4_TYPE(X_CM1_ID)
}
#undef X_CM1_ID
if (device->coopmat_acc_f16_support) {
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, true}, tc_mmq_id, "matmul_id_subgroup_f8_e4m3_f32_f16acc", matmul_id_subgroup_f8_e4m3_f32_f16acc_cm1_len, matmul_id_subgroup_f8_e4m3_f32_f16acc_cm1_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, true, true}, tc_mmq_id, "matmul_id_subgroup_f8_e4m3_f16_f16acc", matmul_id_subgroup_f8_e4m3_f16_f16acc_cm1_len, matmul_id_subgroup_f8_e4m3_f16_f16acc_cm1_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
}
if (device->coopmat_acc_f32_support) {
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, false}, tc_mmq_id, "matmul_id_subgroup_f8_e4m3_f32", matmul_id_subgroup_f8_e4m3_f32_cm1_len, matmul_id_subgroup_f8_e4m3_f32_cm1_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
cm1_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F16, true, false}, tc_mmq_id, "matmul_id_subgroup_f8_e4m3_f16", matmul_id_subgroup_f8_e4m3_f16_cm1_len, matmul_id_subgroup_f8_e4m3_f16_cm1_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
}
if (device->coopmat_int_support && (rdna3 || rdna4)) {
cm1_create_mmq({GGML_TYPE_Q4_0, GGML_TYPE_Q8_1, true, false}, tc_mmq_cm1_int, "matmul_id_subgroup_q4_0_q8_1", matmul_id_subgroup_q4_0_q8_1_cm1_len, matmul_id_subgroup_q4_0_q8_1_cm1_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
@@ -3080,6 +3101,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, false, false}, tc_mmq, "matmul_" #tstr "_f32", SPV_DOT2(matmul_##tstr##_f32), sizeof(vk_mat_mat_push_constants), 3);
FOR_EACH_LUT_TYPE(X_SG)
#undef X_SG
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, false, true}, tc_mmq, "matmul_f8_e4m3_f32_f16acc", SPV_DOT2_F16ACC(matmul_f8_e4m3_f32), sizeof(vk_mat_mat_push_constants), 3);
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, false, false}, tc_mmq, "matmul_f8_e4m3_f32", SPV_DOT2(matmul_f8_e4m3_f32), sizeof(vk_mat_mat_push_constants), 3);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
@@ -3119,6 +3142,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_subgroup_" #tstr "_f32", SPV_DOT2(matmul_id_subgroup_##tstr##_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count, mul_mat_subgroup_size);
FOR_EACH_LUT_TYPE(X_SG_ID_SUB)
#undef X_SG_ID_SUB
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, true}, tc_mmqid, "matmul_id_subgroup_f8_e4m3_f32_f16acc", SPV_DOT2_F16ACC(matmul_id_subgroup_f8_e4m3_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count, mul_mat_subgroup_size);
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_subgroup_f8_e4m3_f32", SPV_DOT2(matmul_id_subgroup_f8_e4m3_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count, mul_mat_subgroup_size);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
std::vector<vk_tile_config> tc_mmqid_int = {{s_warptile_mmqid_int, s_mmq_wg_denoms, s_align}, {m_warptile_mmqid_int, m_mmq_wg_denoms, m_align}, {l_warptile_mmqid_int, l_mmq_wg_denoms, l_align}};
@@ -3156,6 +3181,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_" #tstr "_f32", SPV_DOT2(matmul_id_##tstr##_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
FOR_EACH_LUT_TYPE(X_SG_ID)
#undef X_SG_ID
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, true}, tc_mmqid, "matmul_id_f8_e4m3_f32_f16acc", SPV_DOT2_F16ACC(matmul_id_f8_e4m3_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_f8_e4m3_f32", SPV_DOT2(matmul_id_f8_e4m3_f32), sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
std::vector<vk_tile_config> tc_mmqid_int = {{s_warptile_mmqid_int, s_mmq_wg_denoms, s_align}, {m_warptile_mmqid_int, m_mmq_wg_denoms, m_align}, {l_warptile_mmqid_int, l_mmq_wg_denoms, l_align}};
@@ -3194,6 +3221,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, false, false}, tc_mmq, "matmul_" #tstr "_f32", matmul_##tstr##_f32_fp32_len, matmul_##tstr##_f32_fp32_data, sizeof(vk_mat_mat_push_constants), 3);
FOR_EACH_LUT_TYPE(X_SG_FP32)
#undef X_SG_FP32
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, false, false}, tc_mmq, "matmul_f8_e4m3_f32", matmul_f8_e4m3_f32_fp32_len, matmul_f8_e4m3_f32_fp32_data, sizeof(vk_mat_mat_push_constants), 3);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
@@ -3227,6 +3255,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_subgroup_" #tstr "_f32", matmul_id_subgroup_##tstr##_f32_fp32_len, matmul_id_subgroup_##tstr##_f32_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count, mul_mat_subgroup_size);
FOR_EACH_LUT_TYPE(X_SG_ID_SUB_FP32)
#undef X_SG_ID_SUB_FP32
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_subgroup_f8_e4m3_f32", matmul_id_subgroup_f8_e4m3_f32_fp32_len, matmul_id_subgroup_f8_e4m3_f32_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count, mul_mat_subgroup_size);
} else {
sg_create({GGML_TYPE_F32, GGML_TYPE_F32, true, false}, tc_mm, "matmul_id_f32_f32", matmul_id_f32_f32_fp32_len, matmul_id_f32_f32_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
sg_create({GGML_TYPE_F16, GGML_TYPE_F16, true, false}, tc_mm, "matmul_id_f16", matmul_id_f16_fp32_len, matmul_id_f16_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
@@ -3239,6 +3268,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
sg_create({TYPE, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_" #tstr "_f32", matmul_id_##tstr##_f32_fp32_len, matmul_id_##tstr##_f32_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
FOR_EACH_LUT_TYPE(X_SG_ID_FP32)
#undef X_SG_ID_FP32
sg_create({GGML_TYPE_F8_E4M3, GGML_TYPE_F32, true, false}, tc_mmqid, "matmul_id_f8_e4m3_f32", matmul_id_f8_e4m3_f32_fp32_len, matmul_id_f8_e4m3_f32_fp32_data, sizeof(vk_mat_mat_id_push_constants), mul_mat_id_param_count);
}
}
}
@@ -3419,6 +3449,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f32_f32", arr_dmmv_iq4_nl_f32_f32_len[reduc16], arr_dmmv_iq4_nl_f32_f32_data[reduc16], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f32_f32", OCP_DMMV_LEN(arr_dmmv_mxfp4_f32_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_mxfp4_f32_f32, reduc16), "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_NVFP4][i], "mul_mat_vec_nvfp4_f32_f32", OCP_DMMV_LEN(arr_dmmv_nvfp4_f32_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_nvfp4_f32_f32, reduc16), "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f32_f32[w][GGML_TYPE_F8_E4M3][i], "mul_mat_vec_f8_e4m3_f32_f32", arr_dmmv_f8_e4m3_f32_f32_len[reduc16], arr_dmmv_f8_e4m3_f32_f32_data[reduc16], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F32 ][i], "mul_mat_vec_f32_f16_f32", arr_dmmv_f32_f16_f32_len[reduc], arr_dmmv_f32_f16_f32_data[reduc], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {1, 1, 1}, {wg_size_subgroup, 1, i+1}, 1, false, use_subgroups, force_subgroup_size);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F16 ][i], "mul_mat_vec_f16_f16_f32", arr_dmmv_f16_f16_f32_len[reduc], arr_dmmv_f16_f16_f32_data[reduc], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {2, 1, 1}, {wg_size_subgroup, 2, i+1}, 1, false, use_subgroups, force_subgroup_size);
@@ -3448,6 +3479,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_IQ4_NL][i], "mul_mat_vec_iq4_nl_f16_f32", arr_dmmv_iq4_nl_f16_f32_len[reduc16], arr_dmmv_iq4_nl_f16_f32_data[reduc16], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_MXFP4][i], "mul_mat_vec_mxfp4_f16_f32", OCP_DMMV_LEN(arr_dmmv_mxfp4_f16_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_mxfp4_f16_f32, reduc16), "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_NVFP4][i], "mul_mat_vec_nvfp4_f16_f32", OCP_DMMV_LEN(arr_dmmv_nvfp4_f16_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_nvfp4_f16_f32, reduc16), "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_f16_f32[w][GGML_TYPE_F8_E4M3][i], "mul_mat_vec_f8_e4m3_f16_f32", arr_dmmv_f8_e4m3_f16_f32_len[reduc16], arr_dmmv_f8_e4m3_f16_f32_data[reduc16], "main", mul_mat_vec_num_bindings, sizeof(vk_mat_vec_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq, i+1}, 1, true, use_subgroups16, force_subgroup_size16);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
@@ -3523,6 +3555,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[w][GGML_TYPE_IQ4_NL], "mul_mat_vec_id_iq4_nl_f32", arr_dmmv_id_iq4_nl_f32_f32_len[reduc16], arr_dmmv_id_iq4_nl_f32_f32_data[reduc16], "main", mul_mat_vec_id_num_bindings, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[w][GGML_TYPE_MXFP4], "mul_mat_vec_id_mxfp4_f32", OCP_DMMV_LEN(arr_dmmv_id_mxfp4_f32_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_id_mxfp4_f32_f32, reduc16), "main", mul_mat_vec_id_num_bindings, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[w][GGML_TYPE_NVFP4], "mul_mat_vec_id_nvfp4_f32", OCP_DMMV_LEN(arr_dmmv_id_nvfp4_f32_f32, reduc16), OCP_DMMV_DATA(arr_dmmv_id_nvfp4_f32_f32, reduc16), "main", mul_mat_vec_id_num_bindings, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq}, 1, true, use_subgroups16, force_subgroup_size16);
ggml_vk_create_pipeline(device, device->pipeline_dequant_mul_mat_vec_id_f32[w][GGML_TYPE_F8_E4M3], "mul_mat_vec_id_f8_e4m3_f32", arr_dmmv_id_f8_e4m3_f32_f32_len[reduc16], arr_dmmv_id_f8_e4m3_f32_f32_data[reduc16], "main", mul_mat_vec_id_num_bindings, sizeof(vk_mat_vec_id_push_constants), {rm_iq, 1, 1}, {wg_size_subgroup16, rm_iq}, 1, true, use_subgroups16, force_subgroup_size16);
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (device->integer_dot_product) {
@@ -3590,6 +3623,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_IQ4_NL], "dequant_iq4_nl", dequant_iq4_nl_len, dequant_iq4_nl_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_MXFP4], "dequant_mxfp4", dequant_mxfp4_len, dequant_mxfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_NVFP4], "dequant_nvfp4", dequant_nvfp4_len, dequant_nvfp4_data, "main", 2, 5 * sizeof(uint32_t), {256 * 16, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_dequant[GGML_TYPE_F8_E4M3], "dequant_f8_e4m3", dequant_f8_e4m3_len, dequant_f8_e4m3_data, "main", 2, 5 * sizeof(uint32_t), {256, 1, 1}, {}, 1);
// get_rows
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F32 ], "get_rows_f32", get_rows_f32_len, get_rows_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
@@ -3620,6 +3654,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl", get_rows_iq4_nl_len, get_rows_iq4_nl_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_MXFP4], "get_rows_mxfp4", get_rows_mxfp4_len, get_rows_mxfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_NVFP4], "get_rows_nvfp4", get_rows_nvfp4_len, get_rows_nvfp4_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_F8_E4M3], "get_rows_f8_e4m3", get_rows_f8_e4m3_len, get_rows_f8_e4m3_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows[GGML_TYPE_I32], "get_rows_i32", get_rows_i32_len, get_rows_i32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F32 ], "get_rows_f32_f32", get_rows_f32_f32_len, get_rows_f32_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
@@ -3650,6 +3685,7 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_IQ4_NL], "get_rows_iq4_nl_f32", get_rows_iq4_nl_f32_len, get_rows_iq4_nl_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_MXFP4], "get_rows_mxfp4_f32", get_rows_mxfp4_f32_len, get_rows_mxfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_NVFP4], "get_rows_nvfp4_f32", get_rows_nvfp4_f32_len, get_rows_nvfp4_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1024, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows_f32[GGML_TYPE_F8_E4M3], "get_rows_f8_e4m3_f32", get_rows_f8_e4m3_f32_len, get_rows_f8_e4m3_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), { 512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_get_rows_back_f32, "get_rows_back_f32", get_rows_back_f32_len, get_rows_back_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {256, 1, 1}, {}, 1, true);
ggml_vk_create_pipeline(device, device->pipeline_matmul_split_k_reduce, "split_k_reduce", split_k_reduce_len, split_k_reduce_data, "main", 2, 2 * sizeof(uint32_t), {256 * 4, 1, 1}, {}, 1);
@@ -3778,6 +3814,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_bf16,"cpy_f32_bf16",cpy_f32_bf16_len,cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_bf16_f32,"cpy_bf16_f32",cpy_bf16_f32_len,cpy_bf16_f32_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_bf16_f16,"cpy_bf16_f16",cpy_bf16_f16_len,cpy_bf16_f16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_8, "cpy_8", cpy_8_len, cpy_8_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_f8_e4m3_f16, "cpy_f8_e4m3_f16", cpy_f8_e4m3_f16_len, cpy_f8_e4m3_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_i32_f32, "cpy_i32_f32", cpy_i32_f32_len, cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_cpy_f32_i32, "cpy_f32_i32", cpy_f32_i32_len, cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
@@ -3788,6 +3826,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_bf16,"contig_cpy_f32_bf16",contig_cpy_f32_bf16_len,contig_cpy_f32_bf16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_bf16_f32,"contig_cpy_bf16_f32",contig_cpy_bf16_f32_len,contig_cpy_bf16_f32_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_bf16_f16,"contig_cpy_bf16_f16",contig_cpy_bf16_f16_len,contig_cpy_bf16_f16_data,"main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_8, "contig_cpy_8", contig_cpy_8_len, contig_cpy_8_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f8_e4m3_f16, "contig_cpy_f8_e4m3_f16", contig_cpy_f8_e4m3_f16_len, contig_cpy_f8_e4m3_f16_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_i32_f32, "contig_cpy_i32_f32", contig_cpy_i32_f32_len, contig_cpy_i32_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_contig_cpy_f32_i32, "contig_cpy_f32_i32", contig_cpy_f32_i32_len, contig_cpy_f32_i32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
@@ -3902,6 +3942,8 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_upscale_bicubic_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BICUBIC}, 1);
ggml_vk_create_pipeline(device, device->pipeline_upscale_bilinear_antialias_f32, "upscale_f32", upscale_f32_len, upscale_f32_data, "main", 2, sizeof(vk_op_upscale_push_constants), {512, 1, 1}, {GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ANTIALIAS}, 1);
ggml_vk_create_pipeline(device, device->pipeline_mul_mat_scale_f32, "mul_mat_scale_f32", mul_mat_scale_f32_len, mul_mat_scale_f32_data, "main", 3, sizeof(vk_op_mul_mat_scale_push_constants), {256, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_pad_f32, "pad_f32", pad_f32_len, pad_f32_data, "main", 2, sizeof(vk_op_pad_push_constants), {512, 1, 1}, {}, 1);
ggml_vk_create_pipeline(device, device->pipeline_pad_reflect_1d_f32, "pad_reflect_1d_f32", pad_reflect_1d_f32_len, pad_reflect_1d_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {512, 1, 1}, {}, 1);
@@ -6340,6 +6382,7 @@ vk_pipeline ggml_vk_get_to_fp16(ggml_backend_vk_context * ctx, ggml_type type) {
case GGML_TYPE_NVFP4:
case GGML_TYPE_TQ1_0:
case GGML_TYPE_TQ2_0:
case GGML_TYPE_F8_E4M3:
break;
default:
return nullptr;
@@ -6405,6 +6448,7 @@ static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec(ggml_backend_vk_context *
case GGML_TYPE_NVFP4:
case GGML_TYPE_TQ1_0:
case GGML_TYPE_TQ2_0:
case GGML_TYPE_F8_E4M3:
break;
default:
return nullptr;
@@ -6493,6 +6537,7 @@ static vk_pipeline ggml_vk_get_dequantize_mul_mat_vec_id(ggml_backend_vk_context
case GGML_TYPE_NVFP4:
case GGML_TYPE_TQ1_0:
case GGML_TYPE_TQ2_0:
case GGML_TYPE_F8_E4M3:
break;
default:
return nullptr;
@@ -7094,6 +7139,13 @@ vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_t
return ctx->device->pipeline_cpy_bf16_f16;
}
}
if (src->type == GGML_TYPE_F8_E4M3 && to == GGML_TYPE_F16) {
if (contig) {
return ctx->device->pipeline_contig_cpy_f8_e4m3_f16;
} else {
return ctx->device->pipeline_cpy_f8_e4m3_f16;
}
}
if (src->type == GGML_TYPE_F32 && to == GGML_TYPE_I32) {
if (contig) {
return ctx->device->pipeline_contig_cpy_f32_i32;
@@ -7140,6 +7192,9 @@ vk_pipeline ggml_vk_get_cpy_pipeline(ggml_backend_vk_context * ctx, const ggml_t
}
}
if (src->type == to && !ggml_is_quantized(to) && ggml_type_size(to) == 1) {
return contig ? ctx->device->pipeline_contig_cpy_8 : ctx->device->pipeline_cpy_8;
}
if (src->type == to) {
// Copy two or four bytes at a time, depending on block size.
// For quantized types, we scale by block size/type size. But
@@ -7280,7 +7335,7 @@ static void ggml_vk_mul_mat_q_f16(ggml_backend_vk_context * ctx, vk_context& sub
std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << ggml_type_name(src1->type) << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << ggml_type_name(dst->type) << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
std::cerr << "))");
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || src0->type == GGML_TYPE_F8_E4M3); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
const uint64_t ne00 = src0->ne[0];
@@ -7657,6 +7712,10 @@ static bool ggml_vk_should_use_mmvq(const vk_device& device, uint32_t m, uint32_
GGML_UNUSED(m);
}
static bool ggml_vk_mat_vec_folds_weight_scale(const ggml_tensor * scale, bool swap_inputs = false) {
return scale != nullptr && !swap_inputs && ggml_is_contiguous(scale);
}
static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const struct ggml_cgraph * cgraph, int node_idx, bool swap_inputs = false) {
ggml_tensor * dst = cgraph->nodes[node_idx];
const ggml_tensor * src0 = dst->src[swap_inputs ? 1 : 0];
@@ -7666,7 +7725,7 @@ static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context&
std::cerr << "), (" << src1 << ", name=" << src1->name << ", type=" << src1->type << ", ne0=" << src1->ne[0] << ", ne1=" << src1->ne[1] << ", ne2=" << src1->ne[2] << ", ne3=" << src1->ne[3] << ", nb0=" << src1->nb[0] << ", nb1=" << src1->nb[1] << ", nb2=" << src1->nb[2] << ", nb3=" << src1->nb[3];
std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
std::cerr << ")),)");
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || src0->type == GGML_TYPE_F8_E4M3); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
const uint64_t ne00 = src0->ne[0];
@@ -7872,6 +7931,15 @@ static void ggml_vk_mul_mat_vec_q_f16(ggml_backend_vk_context * ctx, vk_context&
fusion_flags |= MAT_VEC_FUSION_FLAGS_BIAS1;
}
// fold weight scale (src[2]) via the F0 slot, free since scaled matmuls don't fuse
if (ggml_vk_mat_vec_folds_weight_scale(dst->src[2], swap_inputs)) {
d_F0 = ggml_vk_tensor_subbuffer(ctx, dst->src[2]);
fusion_flags |= MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE;
if (ggml_nelements(dst->src[2]) > 1) {
fusion_flags |= MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC;
}
}
ggml_pipeline_request_descriptor_sets(ctx, dmmv, CEIL_DIV(ne12 * ne13, ctx->device->properties.limits.maxComputeWorkGroupCount[1]));
uint32_t base_work_group_y = 0;
@@ -8167,6 +8235,48 @@ static uint32_t ggml_vk_nb_elem(const ggml_tensor * t, int i) {
return (uint32_t)(t->nb[i] / ggml_type_size(t->type));
}
static void ggml_vk_mul_mat_scale(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
const bool is_id = dst->op == GGML_OP_MUL_MAT_ID;
const ggml_tensor * scale = is_id ? dst->src[3] : dst->src[2];
const ggml_tensor * ids = is_id ? dst->src[2] : nullptr;
GGML_ASSERT(scale != nullptr && scale->type == GGML_TYPE_F32);
GGML_ASSERT(dst->type == GGML_TYPE_F32 && ggml_is_contiguous(dst));
const bool per_channel_expert = is_id && scale->ne[0] == dst->ne[0] && scale->ne[1] == dst->src[0]->ne[2];
const bool per_expert = is_id && (ggml_nelements(scale) == dst->src[0]->ne[2] || per_channel_expert);
vk_pipeline pipeline = ctx->device->pipeline_mul_mat_scale_f32;
ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
ggml_vk_sync_buffers(ctx, subctx);
vk_subbuffer d_D = ggml_vk_tensor_subbuffer(ctx, dst);
vk_subbuffer d_S = ggml_vk_tensor_subbuffer(ctx, scale);
vk_subbuffer d_I = is_id ? ggml_vk_tensor_subbuffer(ctx, ids, true) : d_S;
vk_op_mul_mat_scale_push_constants pc = {
(uint32_t)dst->ne[0], (uint32_t)dst->ne[1], (uint32_t)dst->ne[2], (uint32_t)dst->ne[3],
(uint32_t)scale->ne[0], (uint32_t)scale->ne[1], (uint32_t)scale->ne[2], (uint32_t)scale->ne[3],
ggml_vk_nb_elem(scale, 0), ggml_vk_nb_elem(scale, 1), ggml_vk_nb_elem(scale, 2), ggml_vk_nb_elem(scale, 3),
per_channel_expert ? 2u : per_expert ? 1u : 0u,
is_id ? 1u : 0u,
is_id ? (uint32_t)(ids->nb[0] / sizeof(int32_t)) : 0u,
is_id ? (uint32_t)(ids->nb[1] / sizeof(int32_t)) : 0u,
is_id ? get_misalign_bytes(ctx, ids) / (uint32_t)sizeof(int32_t) : 0u,
0, 0, 0, 0, 0, 0,
};
init_fastdiv_values(pc.ne0 * pc.ne1 * pc.ne2, pc.ne_012mp, pc.ne_012L);
init_fastdiv_values(pc.ne0 * pc.ne1, pc.ne_01mp, pc.ne_01L);
init_fastdiv_values(pc.ne0, pc.ne_0mp, pc.ne_0L);
const uint64_t total = (uint64_t)ggml_nelements(dst);
const uint64_t max_elems = (uint64_t)ctx->device->properties.limits.maxComputeWorkGroupCount[0] * 256;
const uint32_t elements0 = (uint32_t)std::min(total, max_elems);
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, { d_D, d_S, d_I }, pc, { elements0, 1, 1 });
}
void ggml_vk_dsv4_hc_comb(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * mixes, const ggml_tensor * scale, const ggml_tensor * base, ggml_tensor * dst) {
VK_LOG_DEBUG("ggml_vk_dsv4_hc_comb(" << mixes << ", " << scale << ", " << base << ", " << dst << ")");
@@ -8310,6 +8420,8 @@ void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const st
ggml_tensor * src1 = dst->src[1];
VK_LOG_DEBUG("ggml_vk_mul_mat(" << src0 << ", " << src1 << ", " << dst << ")");
bool folded = false;
// Handle huge A matrix by splitting the M dimensions. This works well for convolution use cases
// where the M dimension is very large.
// Split_k doesn't work with M splitting.
@@ -8371,11 +8483,16 @@ void ggml_vk_mul_mat(ggml_backend_vk_context * ctx, vk_context& subctx, const st
// mul_mat_vec supports batching ne12*ne13 when ne11==1, or treating ne11 as the batch size (up to four)
// when ne12 and ne13 are one.
} else if ((dst->ne[1] == 1 || (dst->ne[1] <= ggml_vk_mul_mat_vec_max_cols(ctx->device) && src1->ne[2] * src1->ne[3] == 1)) &&
(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || ggml_is_quantized(src0->type))) {
(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || src0->type == GGML_TYPE_F8_E4M3 || ggml_is_quantized(src0->type))) {
ggml_vk_mul_mat_vec_q_f16(ctx, subctx, cgraph, node_idx);
folded = ggml_vk_mat_vec_folds_weight_scale(dst->src[2]);
} else {
ggml_vk_mul_mat_q_f16(ctx, subctx, src0, src1, dst, false);
}
if (dst->src[2] != nullptr && !folded) {
ggml_vk_mul_mat_scale(ctx, subctx, dst);
}
}
static void ggml_vk_mul_mat_id_q_f16(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst) {
@@ -8741,7 +8858,7 @@ static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_conte
std::cerr << "), (" << ids << ", name=" << ids->name << ", type=" << ids->type << ", ne0=" << ids->ne[0] << ", ne1=" << ids->ne[1] << ", ne2=" << ids->ne[2] << ", ne3=" << ids->ne[3] << ", nb0=" << ids->nb[0] << ", nb1=" << ids->nb[1] << ", nb2=" << ids->nb[2] << ", nb3=" << ids->nb[3];
std::cerr << "), (" << dst << ", name=" << dst->name << ", type=" << dst->type << ", ne0=" << dst->ne[0] << ", ne1=" << dst->ne[1] << ", ne2=" << dst->ne[2] << ", ne3=" << dst->ne[3] << ", nb0=" << dst->nb[0] << ", nb1=" << dst->nb[1] << ", nb2=" << dst->nb[2] << ", nb3=" << dst->nb[3];
std::cerr << "))");
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src0) || src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 || src0->type == GGML_TYPE_F8_E4M3); // NOLINT
GGML_ASSERT(ggml_vk_dim01_contiguous(src1) || src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); // NOLINT
GGML_ASSERT(ids->type == GGML_TYPE_I32);
@@ -8940,6 +9057,18 @@ static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_conte
fusion_flags |= MAT_VEC_FUSION_FLAGS_SCALE1;
}
// fold weight scale (src[3]) via the F0 slot, free since scaled matmuls don't fuse
if (ggml_vk_mat_vec_folds_weight_scale(dst->src[3])) {
d_F0 = ggml_vk_tensor_subbuffer(ctx, dst->src[3]);
fusion_flags |= MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE;
if (ggml_nelements(dst->src[3]) > 1) {
fusion_flags |= MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC;
}
if (dst->src[3]->ne[0] == dst->ne[0] && dst->src[3]->ne[1] == src0->ne[2]) {
fusion_flags |= MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_2D;
}
}
// Loop over the batch dimension
for (uint32_t expert_i1 = 0; expert_i1 < nei1; ++expert_i1) {
const vk_mat_vec_id_push_constants pc = {
@@ -8971,7 +9100,7 @@ static void ggml_vk_mul_mat_vec_id_q_f16(ggml_backend_vk_context * ctx, vk_conte
static bool ggml_vk_use_mul_mat_vec_id(const ggml_tensor * dst) {
const ggml_tensor * src0 = dst->src[0];
const ggml_tensor * ids = dst->src[2];
return ids->ne[1] <= 8 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type));
return ids->ne[1] <= 8 && (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F8_E4M3 || ggml_is_quantized(src0->type));
}
bool ggml_vk_use_mul_mat_vec_id(const struct ggml_cgraph * cgraph, int node_idx) {
@@ -8984,11 +9113,17 @@ void ggml_vk_mul_mat_id(ggml_backend_vk_context * ctx, vk_context& subctx, const
ggml_tensor * src1 = dst->src[1];
ggml_tensor * src2 = dst->src[2];
VK_LOG_DEBUG("ggml_vk_mul_mat_id(" << src0 << ", " << src1 << ", " << src2 << ", " << dst << ")");
bool folded = false;
if (ggml_vk_use_mul_mat_vec_id(cgraph, node_idx)) {
ggml_vk_mul_mat_vec_id_q_f16(ctx, subctx, cgraph, node_idx);
folded = ggml_vk_mat_vec_folds_weight_scale(dst->src[3]);
} else {
ggml_vk_mul_mat_id_q_f16(ctx, subctx, src0, src1, src2, dst);
}
if (dst->src[3] != nullptr && !folded) {
ggml_vk_mul_mat_scale(ctx, subctx, dst);
}
}
bool ggml_vk_flash_attn_scalar_shmem_support(const vk_device& device, const vk_fa_tuning_params& params, uint32_t hsk, uint32_t hsv, bool f32acc, ggml_type k_type, ggml_type v_type) {
@@ -15280,6 +15415,12 @@ bool ggml_vk_can_fuse(const ggml_backend_vk_context * ctx, const struct ggml_cgr
return false;
}
const ggml_tensor * base = cgraph->nodes[node_idx];
if ((base->op == GGML_OP_MUL_MAT && base->src[2] != nullptr) ||
(base->op == GGML_OP_MUL_MAT_ID && base->src[3] != nullptr)) {
return false;
}
if ((ops.size() == 2 || ops.size() == 3 || ops.size() == 4) &&
ops.begin()[0] == GGML_OP_RMS_NORM && ops.begin()[1] == GGML_OP_MUL) {
// additional constraints specific to this fusion
@@ -17158,10 +17299,6 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
const vk_device& device = ggml_vk_get_device(ctx->device);
if (ggml_get_src_scale(op, 0)) {
return false;
}
const bool uses_bda = (op->op == GGML_OP_IM2COL || op->op == GGML_OP_IM2COL_3D) &&
device->shader_int64 && device->buffer_device_address;
@@ -17235,6 +17372,12 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
case GGML_OP_MUL_MAT_ID:
{
ggml_type src0_type = op->src[0]->type;
// matmul paths bind these at their raw offset
for (const ggml_tensor * t : std::initializer_list<const ggml_tensor *>{op->src[0], op->src[1], ggml_get_src_scale(op, 0)}) {
if (t && (vk_tensor_offset(t) + t->view_offs) % device->properties.limits.minStorageBufferOffsetAlignment != 0) {
return false;
}
}
if (op->op == GGML_OP_MUL_MAT_ID) {
if (!ggml_vk_use_mul_mat_vec_id(op)) {
const ggml_tensor * ids = op->src[2];
@@ -17280,6 +17423,7 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
case GGML_TYPE_NVFP4:
case GGML_TYPE_TQ1_0:
case GGML_TYPE_TQ2_0:
case GGML_TYPE_F8_E4M3:
break;
default:
return false;
@@ -17288,7 +17432,7 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
if (op->op == GGML_OP_MUL_MAT_ID && op->src[2]->ne[3] != op->src[1]->ne[3]) {
return false;
}
if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16) ||
if (!(ggml_vk_dim01_contiguous(op->src[0]) || op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16 || op->src[0]->type == GGML_TYPE_F8_E4M3) ||
!(ggml_vk_dim01_contiguous(op->src[1]) || op->src[1]->type == GGML_TYPE_F32 || op->src[1]->type == GGML_TYPE_F16)) {
return false;
}
@@ -17393,6 +17537,7 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
case GGML_TYPE_NVFP4:
case GGML_TYPE_TQ1_0:
case GGML_TYPE_TQ2_0:
case GGML_TYPE_F8_E4M3:
case GGML_TYPE_I32:
return true;
default:
@@ -18477,4 +18622,3 @@ void ggml_vk_debug_label::begin(vk_context & ctx, const std::string & name) {
subctx->debug_labels.push_back(name);
ggml_vk_cmd_label_begin(subctx->s->buffer->buf, subctx->debug_labels.back().c_str());
}
@@ -1,5 +1,7 @@
#version 450
#extension GL_EXT_shader_8bit_storage : require
#include "types.glsl"
#include "generic_unary_head.glsl"
@@ -19,7 +21,9 @@ void main() {
if (idx + (num_iter-1)*num_threads < p.ne) {
[[unroll]] for (uint i = 0; i < num_iter; ++i) {
#if defined(DATA_A_BF16)
#if defined(DATA_A_F8_E4M3)
data_d[get_doffset() + idx] = D_TYPE(e4m3_to_fp32(data_a[get_aoffset() + idx]));
#elif defined(DATA_A_BF16)
data_d[get_doffset() + idx] = D_TYPE(bf16_to_fp32(uint32_t(data_a[get_aoffset() + idx])));
#elif defined(DATA_D_BF16)
float f = float(data_a[get_aoffset() + idx]);
@@ -37,7 +41,9 @@ void main() {
continue;
}
#if defined(DATA_A_BF16)
#if defined(DATA_A_F8_E4M3)
data_d[get_doffset() + idx] = D_TYPE(e4m3_to_fp32(data_a[get_aoffset() + idx]));
#elif defined(DATA_A_BF16)
data_d[get_doffset() + idx] = D_TYPE(bf16_to_fp32(uint32_t(data_a[get_aoffset() + idx])));
#elif defined(DATA_D_BF16)
float f = float(data_a[get_aoffset() + idx]);
@@ -1,5 +1,7 @@
#version 450
#extension GL_EXT_shader_8bit_storage : require
#include "types.glsl"
#include "generic_unary_head.glsl"
@@ -12,7 +14,9 @@ void main() {
return;
}
#if defined(DATA_A_BF16)
#if defined(DATA_A_F8_E4M3)
data_d[get_doffset() + dst_idx(idx)] = D_TYPE(e4m3_to_fp32(data_a[get_aoffset() + src0_idx(idx)]));
#elif defined(DATA_A_BF16)
data_d[get_doffset() + dst_idx(idx)] = D_TYPE(bf16_to_fp32(uint32_t(data_a[get_aoffset() + src0_idx(idx)])));
#elif defined(DATA_D_BF16)
float f = float(data_a[get_aoffset() + src0_idx(idx)]);
@@ -0,0 +1,18 @@
#version 450
#include "dequant_head.glsl"
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout (binding = 0) readonly buffer A {uint8_t data_a[];};
layout (binding = 1) writeonly buffer D {D_TYPE data_b[];};
void main() {
const uint i = gl_GlobalInvocationID.x;
if (i >= p.nel) {
return;
}
data_b[i] = D_TYPE(e4m3_to_fp32(data_a[i]));
}
@@ -544,12 +544,38 @@ vec4 dequantize4(uint ib, uint iqs, uint a_offset) {
}
#endif
#if defined(DATA_A_F8_E4M3)
FLOAT_TYPE dequantize1(uint ib, uint iqs, uint a_offset) {
return FLOAT_TYPE(e4m3_to_fp32(data_a[a_offset + ib + iqs]));
}
vec2 dequantize(uint ib, uint iqs, uint a_offset) {
const uint idx = a_offset + ib + iqs;
return vec2(e4m3_to_fp32(data_a[idx]), e4m3_to_fp32(data_a[idx + 1]));
}
vec4 dequantize4(uint ib, uint iqs, uint a_offset) {
const uint idx = a_offset + ib + iqs;
return vec4(e4m3_to_fp32(data_a[idx ]), e4m3_to_fp32(data_a[idx + 1]),
e4m3_to_fp32(data_a[idx + 2]), e4m3_to_fp32(data_a[idx + 3]));
}
vec4 dequantize4_2aligned(uint ib, uint iqs, uint a_offset) {
const uint idx = a_offset + ib + iqs;
return vec4(e4m3_to_fp32(data_a[idx ]), e4m3_to_fp32(data_a[idx + 1]),
e4m3_to_fp32(data_a[idx + 2]), e4m3_to_fp32(data_a[idx + 3]));
}
#endif
#if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16)
vec2 get_dm(uint ib, uint a_offset) {
return vec2(0, 0);
}
#endif
#if defined(DATA_A_F8_E4M3)
vec2 get_dm(uint ib, uint a_offset) {
return vec2(1.0, 0.0);
}
#endif
#if defined(DATA_A_IQ1_M)
vec2 get_dm(uint ib, uint a_offset) {
const uint16_t[4] scales = data_a[a_offset + ib].scales;
@@ -1412,6 +1412,17 @@ f16vec4 dequantFuncNVFP4_v(const in decodeBufNVFP4 bl, const in uint blockCoords
}
#endif
#if defined(DATA_A_F8_E4M3)
layout(buffer_reference, std430, buffer_reference_align = 1) buffer decodeBufF8_E4M3 {
uint8_t block;
};
float16_t dequantFuncF8_E4M3(const in decodeBufF8_E4M3 bl, const in uint blockCoords[2], const in uint coordInBlock[2])
{
return float16_t(e4m3_to_fp32(bl.block));
}
#endif
#if defined(DATA_A_Q1_0)
#define dequantFuncA dequantFuncQ1_0
#define dequantFuncA_v dequantFuncQ1_0_v
@@ -1492,4 +1503,6 @@ f16vec4 dequantFuncNVFP4_v(const in decodeBufNVFP4 bl, const in uint blockCoords
#define dequantFuncA_v dequantFuncNVFP4_v
#elif defined(DATA_A_F32)
#define dequantFuncA dequantFuncF32
#elif defined(DATA_A_F8_E4M3)
#define dequantFuncA dequantFuncF8_E4M3
#endif
@@ -27,6 +27,8 @@ void main() {
#if defined(DATA_A_BF16)
TEMP_TYPE v = TEMP_TYPE(bf16_to_fp32(data_a[a_offset + i00]));
#elif defined(DATA_A_F8_E4M3)
TEMP_TYPE v = TEMP_TYPE(e4m3_to_fp32(data_a[a_offset + i00]));
#else
TEMP_TYPE v = TEMP_TYPE(data_a[a_offset + i00]);
#endif
@@ -0,0 +1,57 @@
#version 450
#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require
#include "utils.glsl"
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0) buffer D { float data_d[]; };
layout(binding = 1) readonly buffer S { float data_s[]; };
layout(binding = 2) readonly buffer I { int32_t data_ids[]; };
layout(push_constant) uniform parameter {
uint ne0; uint ne1; uint ne2; uint ne3;
uint sne0; uint sne1; uint sne2; uint sne3;
uint snb0; uint snb1; uint snb2; uint snb3;
uint per_expert;
uint has_ids;
uint ids_s0; uint ids_s1; uint ids_offset;
uint ne_012mp; uint ne_012L;
uint ne_01mp; uint ne_01L;
uint ne_0mp; uint ne_0L;
} p;
void main() {
const uint total = p.ne0 * p.ne1 * p.ne2 * p.ne3;
const uint stride = gl_NumWorkGroups.x * gl_WorkGroupSize.x;
for (uint i = gl_GlobalInvocationID.x; i < total; i += stride) {
const uint i3 = fastdiv(i, p.ne_012mp, p.ne_012L);
const uint r3 = i - i3 * p.ne0 * p.ne1 * p.ne2;
const uint i2 = fastdiv(r3, p.ne_01mp, p.ne_01L);
const uint r2 = r3 - i2 * p.ne0 * p.ne1;
const uint i1 = fastdiv(r2, p.ne_0mp, p.ne_0L);
const uint i0 = r2 - i1 * p.ne0;
int32_t expert = 0;
if (p.has_ids != 0) {
expert = data_ids[p.ids_offset + i1 * p.ids_s0 + i2 * p.ids_s1];
if (expert < 0) {
continue;
}
}
uint si;
if (p.per_expert != 0) {
si = (p.per_expert == 2) ? i0 * p.snb0 + uint(expert) * p.snb1 : uint(expert) * p.snb0;
} else {
si = (i0 % p.sne0) * p.snb0
+ (i1 % p.sne1) * p.snb1
+ (i2 % p.sne2) * p.snb2
+ (i3 % p.sne3) * p.snb3;
}
data_d[i] *= data_s[si];
}
}
@@ -7,7 +7,7 @@
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
#if !defined(DATA_A_F32) && !defined(DATA_A_F16) && !defined(DATA_A_BF16)
#if !defined(DATA_A_F32) && !defined(DATA_A_F16) && !defined(DATA_A_BF16) && !defined(DATA_A_F8_E4M3)
#define K_PER_ITER 8
#else
#define K_PER_ITER 4
@@ -114,7 +114,7 @@ void iter(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const uint first_row, const
}
}
#if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16)
#if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16) || defined(DATA_A_F8_E4M3)
void iter_aligned_nonquant(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const uint first_row, const uint num_rows, const uint tid, const uint i)
{
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
@@ -93,6 +93,16 @@ layout (constant_id = 0) const uint BLOCK_SIZE = 32;
layout (constant_id = 1) const uint NUM_ROWS = 1;
layout (constant_id = 2) const uint NUM_COLS = 1;
#ifdef MUL_MAT_ID
#define WEIGHT_SCALE_INDEX(row) (((p.fusion_flags & MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_2D) != 0) ? (expert_id * p.stride_d + (row)) : (((p.fusion_flags & MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC) != 0) ? expert_id : 0u))
#else
#define WEIGHT_SCALE_INDEX(row) (((p.fusion_flags & MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC) != 0) ? (row) : 0u)
#endif
#define APPLY_WEIGHT_SCALE(acc, row) \
if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE) != 0) { \
(acc) *= FLOAT_TYPE(data_fuse0[WEIGHT_SCALE_INDEX(row)]); \
}
#ifdef USE_SUBGROUP_ADD_NO_SHMEM
void reduce_result(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t d_offset, const in uint32_t first_row, const in uint32_t num_rows, const in uint32_t tid) {
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
@@ -104,6 +114,7 @@ void reduce_result(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t
if (tid == 0) {
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
[[unroll]] for (uint n = 0; n < num_rows; ++n) {
APPLY_WEIGHT_SCALE(temp[j][n], first_row + n);
#ifdef MUL_MAT_ID
if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) {
temp[j][n] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]);
@@ -162,6 +173,7 @@ void reduce_result(FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t d_offs
[[unroll]] for (uint s = 0; s < gl_NumSubgroups; ++s) {
temp[j][n] += tmpsh[j][n][s];
}
APPLY_WEIGHT_SCALE(temp[j][n], first_row + n);
#ifdef MUL_MAT_ID
if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) {
temp[j][n] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]);
@@ -210,6 +222,7 @@ void reduce_result(FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t d_offs
if (tid == 0) {
[[unroll]] for (uint j = 0; j < NUM_COLS; ++j) {
[[unroll]] for (uint n = 0; n < num_rows; ++n) {
APPLY_WEIGHT_SCALE(tmpsh[j][n][0], first_row + n);
#ifdef MUL_MAT_ID
if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) {
tmpsh[j][n][0] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]);
@@ -4,6 +4,11 @@
#define MAT_VEC_FUSION_FLAGS_BIAS1 0x2
#define MAT_VEC_FUSION_FLAGS_SCALE0 0x4
#define MAT_VEC_FUSION_FLAGS_SCALE1 0x8
// weight scale in data_fuse0; _VEC: per-output (dense) / per-expert (id), else scalar;
// _2D (id only): per-channel-per-expert [n_out, n_expert]
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE 0x10
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_VEC 0x20
#define MAT_VEC_FUSION_FLAGS_WEIGHT_SCALE_2D 0x40
layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
#if defined(A_TYPEV4)
@@ -87,6 +87,8 @@ layout (binding = 0) readonly buffer A_SCALAR {float data_a_scalar[];};
layout (binding = 0) readonly buffer A_SCALAR {float16_t data_a_scalar[];};
#elif defined(DATA_A_BF16)
layout (binding = 0) readonly buffer A_SCALAR {uint16_t data_a_scalar[];};
#elif defined(DATA_A_F8_E4M3)
layout (binding = 0) readonly buffer A_SCALAR {uint8_t data_a_scalar[];};
#endif
#if defined(A_TYPE_PACKED16)
layout (binding = 0) readonly buffer A_PACKED16 {A_TYPE_PACKED16 data_a_packed16[];};
@@ -283,7 +285,7 @@ void main() {
const uint warp_r = warp_i % (BM / WM);
const uint warp_c = warp_i / (BM / WM);
#if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16)
#if defined(DATA_A_F32) || defined(DATA_A_F16) || defined(DATA_A_BF16) || defined(DATA_A_F8_E4M3)
const uint LOAD_VEC_A_EFF = (ALIGNED != 0) ? LOAD_VEC_A : 1;
const uint LOAD_VEC_BATCH_A = (ALIGNED != 0) ? 1 : 2;
#elif !defined(MULMAT_QUANT)
@@ -124,7 +124,7 @@ layout (binding = 3) writeonly buffer S {D_TYPE data_s[];};
layout (binding = 1) readonly buffer B4 {B_TYPEV4 data_b_v4[];};
#endif
#if defined(MULMAT_QUANT) || QUANT_K > 1
#if defined(MULMAT_QUANT) || QUANT_K > 1 || defined(DATA_A_F8_E4M3)
#include "dequant_funcs_cm2.glsl"
#ifndef MULMAT_QUANT
// Per-type path: use the alias set by dequant_funcs_cm2.glsl
@@ -345,6 +345,26 @@ void load_a_to_shmem(const uint pos_a, const uint row, const uint col, const uin
store_a(col, eff_row + 4, FLOAT_TYPEV2(kvalues_mxfp4[vui >> 4] * d,
kvalues_mxfp4[vui2 >> 4] * d));
#endif
#elif defined(DATA_A_F8_E4M3)
#if LOAD_VEC_A == 4
if (ALIGNED != 0) {
const uint idx = pos_a + col * p.stride_a / LOAD_VEC_A + row;
const uint k_pair = row * LOAD_VEC_A / 2;
FLOAT_TYPEV4 aa = FLOAT_TYPEV4(e4m3_to_fp32(data_a[idx]));
store_a(col, k_pair, aa.xy);
store_a(col, k_pair + 1, aa.zw);
return;
}
#endif
const uint idx = pos_a + col * p.stride_a + row * 2;
if (idx_m < p.M && block + row * 2 + 1 < end_k) {
store_a(col, row, FLOAT_TYPEV2(e4m3_to_fp32(data_a_scalar[idx]),
e4m3_to_fp32(data_a_scalar[idx + 1])));
} else if (idx_m < p.M && block + row * 2 < end_k) {
store_a(col, row, FLOAT_TYPEV2(e4m3_to_fp32(data_a_scalar[idx]), 0.0f));
} else {
store_a(col, row, FLOAT_TYPEV2(0.0f));
}
#else
if (MmTypeA == GGML_TYPE_Q4_0) {
const uint idx = pos_a + col * p.stride_a / mm_load_vec_a() + row;
@@ -12,6 +12,10 @@
#extension GL_EXT_float_e4m3 : require
#endif
#if defined(DATA_A_F8_E4M3)
#extension GL_EXT_shader_8bit_storage : require
#endif
#if defined(DATA_A_F32)
#define QUANT_K 1
#define QUANT_R 1
@@ -54,6 +58,18 @@
#define A_TYPE_PACKED32 uint32_t
#endif
#if defined(DATA_A_F8_E4M3)
#define QUANT_K 1
#define QUANT_R 1
#if LOAD_VEC_A == 4
#define A_TYPE u8vec4
#else
#define A_TYPE uint8_t
#endif
#define A_TYPE_PACKED32 uint32_t
#endif
#define QUANT_K_Q4_0 32
#define QUANT_R_Q4_0 2
@@ -1971,6 +1987,33 @@ float bf16_to_fp32(uint32_t u)
return uintBitsToFloat(u << 16);
}
// OCP E4M3: 1 sign, 4 exponent (bias 7), 3 mantissa. No infinities; S.1111.111 is NaN.
float e4m3_to_fp32(uint x) {
const uint s = (x >> 7) & 1u;
const uint e = (x >> 3) & 0xFu;
const uint m = x & 0x7u;
float val;
if (e == 0xFu && m == 0x7u) {
return uintBitsToFloat(0x7FC00000u | (s << 31)); // NaN
} else if (e == 0u) {
val = float(m) * (1.0 / 512.0); // 2^-6 * m/8
} else {
val = uintBitsToFloat(((e + 120u) << 23) | (m << 20));
}
return s == 1u ? -val : val;
}
#if defined(DATA_A_F8_E4M3)
float e4m3_to_fp32(uint8_t x) {
return e4m3_to_fp32(uint(x));
}
vec4 e4m3_to_fp32(u8vec4 x) {
return vec4(e4m3_to_fp32(uint(x.x)), e4m3_to_fp32(uint(x.y)),
e4m3_to_fp32(uint(x.z)), e4m3_to_fp32(uint(x.w)));
}
#endif
vec4 bf16_to_fp32(uvec4 u)
{
return vec4(bf16_to_fp32(u.x), bf16_to_fp32(u.y), bf16_to_fp32(u.z), bf16_to_fp32(u.w));
@@ -75,6 +75,7 @@ const std::vector<std::string> type_names = {
"tq1_0",
"tq2_0",
"bf16",
"f8_e4m3",
};
enum MatMulIdType {
@@ -628,6 +629,15 @@ void matmul_shaders(bool fp16, MatMulIdType matmul_id_type, bool coopmat, bool c
{"FLOAT_TYPEV8", FLOAT_TYPE(8, tname)},
};
if (tname == "f8_e4m3") {
const std::string load_vec_a = coopmat2 ? "1" : "4";
if (!coopmat2) {
string_to_spv(shader_name + "_f8_e4m3_f32" + dot2_sfx, source_name, merge_maps(merge_maps(base_dict, float_type_dict), {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f32}, {"B_TYPE_SCALAR", "float"}, {"B_TYPEV4", "vec4"}, {"D_TYPE", "float"}}), fp16, coopmat, coopmat2, f16acc);
}
string_to_spv(shader_name + "_f8_e4m3_f16" + dot2_sfx, source_name, merge_maps(merge_maps(base_dict, float_type_dict), {{data_a_key, "1"}, {"LOAD_VEC_A", load_vec_a}, {"LOAD_VEC_B", load_vec}, {"B_TYPE", aligned_b_type_f16}, {"B_TYPE_SCALAR", "float16_t"}, {"B_TYPEV4", "f16vec4"}, {"D_TYPE", "float"}}), fp16, coopmat, coopmat2, f16acc);
continue;
}
#if defined(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
if (!f16acc && !coopmat && !coopmat2 && !dot2 && (is_legacy_quant(tname) || is_k_quant(tname) || tname == "mxfp4" || tname == "iq3_s" || tname == "iq4_xs")) {
string_to_spv(shader_name + "_" + tname + "_q8_1", "mul_mmq.comp", merge_maps(merge_maps(base_dict, float_type_dict), {{data_a_key, "1"}, {"D_TYPE", "float"},}), fp16, coopmat, coopmat2, f16acc);
@@ -850,7 +860,7 @@ void process_shaders() {
string_to_spv("dequant_" + tname + "_transpose", "dequant_" + tname + ".comp", merge_maps(base_dict, {{data_a_key, "1"}, {"D_TYPE", "float16_t"}, {"DEQUANT_TRANSPOSE", "1"}}));
}
shader = (tname == "f32" || tname == "f16" || tname == "bf16") ? "get_rows.comp" : "get_rows_quant.comp";
shader = (tname == "f32" || tname == "f16" || tname == "bf16" || tname == "f8_e4m3") ? "get_rows.comp" : "get_rows_quant.comp";
if (tname == "f16") {
string_to_spv("get_rows_" + tname, shader, merge_maps(base_dict, {{"TEMP_TYPE", "FLOAT_TYPE"}, {data_a_key, "1"}, {"B_TYPE", "int"}, {"D_TYPE", "float16_t"}, {"OPTIMIZATION_ERROR_WORKAROUND", "1"}}));
@@ -895,6 +905,8 @@ void process_shaders() {
string_to_spv("cpy_f32_bf16","copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "uint16_t"}, {"DATA_D_BF16", "1"}});
string_to_spv("cpy_bf16_f32","copy.comp", {{"A_TYPE", "uint16_t"}, {"D_TYPE", "float"}, {"DATA_A_BF16", "1"}});
string_to_spv("cpy_bf16_f16","copy.comp", {{"A_TYPE", "uint16_t"}, {"D_TYPE", "float16_t"}, {"DATA_A_BF16", "1"}});
string_to_spv("cpy_8", "copy.comp", {{"A_TYPE", "uint8_t"}, {"D_TYPE", "uint8_t"}});
string_to_spv("cpy_f8_e4m3_f16", "copy.comp", {{"A_TYPE", "uint8_t"}, {"D_TYPE", "float16_t"}, {"DATA_A_F8_E4M3", "1"}});
string_to_spv("contig_cpy_32", "contig_copy.comp", {{"A_TYPE", "uint"}, {"D_TYPE", "uint"}});
string_to_spv("contig_cpy_f32_i32", "contig_copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "int"}});
string_to_spv("contig_cpy_i32_f32", "contig_copy.comp", {{"A_TYPE", "int"}, {"D_TYPE", "float"}});
@@ -904,6 +916,8 @@ void process_shaders() {
string_to_spv("contig_cpy_f32_bf16","contig_copy.comp",{{"A_TYPE", "float"}, {"D_TYPE", "uint16_t"}, {"DATA_D_BF16", "1"}});
string_to_spv("contig_cpy_bf16_f32","contig_copy.comp",{{"A_TYPE", "uint16_t"}, {"D_TYPE", "float"}, {"DATA_A_BF16", "1"}});
string_to_spv("contig_cpy_bf16_f16","contig_copy.comp",{{"A_TYPE", "uint16_t"}, {"D_TYPE", "float16_t"}, {"DATA_A_BF16", "1"}});
string_to_spv("contig_cpy_8", "contig_copy.comp", {{"A_TYPE", "uint8_t"}, {"D_TYPE", "uint8_t"}});
string_to_spv("contig_cpy_f8_e4m3_f16", "contig_copy.comp", {{"A_TYPE", "uint8_t"}, {"D_TYPE", "float16_t"}, {"DATA_A_F8_E4M3", "1"}});
string_to_spv("cpy_f32_i32", "copy.comp", {{"A_TYPE", "float"}, {"D_TYPE", "int"}});
string_to_spv("cpy_i32_f32", "copy.comp", {{"A_TYPE", "int"}, {"D_TYPE", "float"}});
@@ -987,6 +1001,8 @@ void process_shaders() {
string_to_spv("repeat_i16", "repeat.comp", {{"A_TYPE", "int16_t"}, {"D_TYPE", "int16_t"}});
string_to_spv("mul_mat_scale_f32", "mul_mat_scale.comp", {});
string_to_spv("pad_f32", "pad.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
string_to_spv("pad_reflect_1d_f32", "pad_reflect_1d.comp", {{"A_TYPE", "float"}, {"D_TYPE", "float"}});
+2
View File
@@ -966,6 +966,7 @@ static const std::map<llm_tensor, const char *> LLM_TENSOR_NAMES = {
{ LLM_TENSOR_ROUTER_OUT, "blk.%d.ffn_router_out" },
{ LLM_TENSOR_ATTN_V_GATE, "blk.%d.attn_v_gate" },
{ LLM_TENSOR_ATTN_V_EXPS, "blk.%d.attn_v_exps" },
{ LLM_TENSOR_OUTPUT_LEXICAL, "output" },
};
// declare information about the model weight tensors:
@@ -1408,6 +1409,7 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
{LLM_TENSOR_ATTN_V_GATE, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
{LLM_TENSOR_ATTN_V_EXPS, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT_ID}},
{LLM_TENSOR_OUTPUT_LEXICAL, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_GET_ROWS}},
};
LLM_KV::LLM_KV(llm_arch arch, const char * suffix) : arch(arch), suffix(suffix) {}
+1
View File
@@ -969,6 +969,7 @@ enum llm_tensor {
LLM_TENSOR_ROUTER_OUT,
LLM_TENSOR_ATTN_V_GATE,
LLM_TENSOR_ATTN_V_EXPS,
LLM_TENSOR_OUTPUT_LEXICAL, // Clef: Maps to the same tensor as LLM_TENSOR_OUTPUT but uses GET_ROWS operation
};
+7 -1
View File
@@ -82,6 +82,12 @@ void llama_model_clef::load_arch_tensors(llama_model_loader & ml) {
load_norm(field_norm, LLM_TENSOR_DECISION_FIELD_NORM, n_embd_h);
load_norm(option_norm, LLM_TENSOR_DECISION_OPTION_NORM, n_embd_h);
output_lexical = create_tensor(tn(LLM_TENSOR_OUTPUT_LEXICAL, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
if (output_lexical == nullptr) {
output_lexical = tok_embd;
}
proj_memory = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_MEMORY, "weight"), {n_embd, n_embd_h}, 0);
proj_question = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_QUESTION, "weight"), {n_embd, n_embd_h}, 0);
proj_option_question = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION, "weight"), {n_embd, n_embd_h}, 0);
@@ -559,7 +565,7 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
ggml_tensor * opt_ctx = ggml_mul_mat(ctx0, hidden_t, inp->pool_o); // [n_embd, n_options]
// mean of the output embeddings of the tokens of each option
ggml_tensor * lexical = ggml_get_rows(ctx0, model.output, inp->tokens);
ggml_tensor * lexical = ggml_get_rows(ctx0, model.output_lexical, inp->tokens);
lexical = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, lexical)), inp->pool_o); // [n_embd, n_options]
// one query per option
+4
View File
@@ -2999,6 +2999,8 @@ struct llama_model_qwen35 : public llama_model_base {
graph_mtp(const llm_graph_params & params);
};
// if false, the derived model does not use the LM head and builds its own output
virtual bool has_lm_head() const { return true; }
std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
};
@@ -3060,6 +3062,7 @@ struct llama_model_clef : public llama_model_qwen35 {
ggml_tensor * scorer_b = nullptr;
ggml_tensor * scorer_out = nullptr;
ggml_tensor * scorer_out_b = nullptr;
ggml_tensor * output_lexical = nullptr;
class input_decision;
@@ -3090,6 +3093,7 @@ struct llama_model_clef : public llama_model_qwen35 {
const llama_model_clef & model;
};
bool has_lm_head() const override { return false; }
std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
};
+9 -6
View File
@@ -41,17 +41,20 @@ void llama_model_qwen35::load_arch_tensors(llama_model_loader & ml) {
// output
output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
if (has_lm_head()) {
output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
// if output is NULL, init from the input tok embed
if (output == NULL) {
output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
}
}
// optional projection of the embeddings output
cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), { n_embd, hparams.n_embd_out() }, TENSOR_NOT_REQUIRED);
cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), { hparams.n_embd_out() }, TENSOR_NOT_REQUIRED);
// if output is NULL, init from the input tok embed
if (output == NULL) {
output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
}
auto load_block_trunk = [&](int il, int flags) {
auto & layer = layers[il];
+38 -3
View File
@@ -6850,7 +6850,7 @@ struct test_mul_mat_id : public test_case {
const int64_t m_v; // rows of as in memory, the experts of as are strided for m_v > m, no view for m_v == 0
const bool ids_offset;
const bool skip; // set some ids to a negative value
const int scale_gran; // weight scale granularity: 0=default, 1=per-expert scalar, 2=per-channel-per-expert
const int scale_gran; // weight scale granularity: 0=default, 1=per-expert scalar, 2=per-channel-per-expert, 3=per-tensor +inf
const bool scale_view;
std::string vars() override {
@@ -6908,12 +6908,14 @@ struct test_mul_mat_id : public test_case {
ggml_set_name(b, "b");
ggml_tensor * s = nullptr;
if (scale_gran == 2) {
if (scale_gran == 3) {
s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
} else if (scale_gran == 2) {
s = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, m, n_mats);
} else if (scale_gran == 1 || ggml_needs_scale(type_a)) {
s = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, n_mats);
}
if (s && skip && !scale_view) {
if (s && skip && !scale_view && scale_gran != 3) {
const int64_t n_channels = scale_gran == 2 ? m : 1;
ggml_tensor * storage = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_channels, n_mats + 1);
ggml_set_name(storage, "scale_storage");
@@ -6942,6 +6944,14 @@ struct test_mul_mat_id : public test_case {
std::vector<float> prefix(storage->ne[0], NAN);
ggml_backend_tensor_set(storage, prefix.data(), 0, prefix.size()*sizeof(float));
}
if (scale_gran == 3) {
// skipped slots stay zero with an infinite scale, positive inputs keep the other slots at +inf
for (const char * name : {"as", "b"}) {
init_tensor_uniform(ggml_get_tensor(ctx, name), 0.1f, 1.0f);
}
const float inf = INFINITY;
ggml_backend_tensor_set(ggml_get_tensor(ctx, "s"), &inf, 0, sizeof(float));
}
}
void reinit_perf_iter(ggml_context * ctx) override {
@@ -13601,6 +13611,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
}
for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F8_E4M3}) {
for (int64_t n : {1, 9}) {
test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 2, false, 32, n, 32, 0, false, true, 3));
}
test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 2, false, 32, 9, 32, 0, true, true, 3));
}
for (int scale_gran : {1, 2}) {
for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F8_E4M3}) {
test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 1, 1, false, 32, 4, 32, 0, false, false, scale_gran));
@@ -13609,6 +13626,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
}
test_cases.emplace_back(new test_mul_mat(type, GGML_TYPE_F32, 32, 4, 32, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran, true));
test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 2, false, 32, 4, 32, 0, false, false, scale_gran, true));
// misaligned ids view
test_cases.emplace_back(new test_mul_mat_id(type, GGML_TYPE_F32, 4, 2, false, 32, 9, 32, 0, true, true, scale_gran));
}
for (int64_t n : {1, 8}) {
test_cases.emplace_back(new test_mul_mat(GGML_TYPE_Q4_K, GGML_TYPE_F32, 32, n, 256, {1, 1}, {1, 1}, {0, 1, 2, 3}, 0, 1, false, 0, 0, 0, false, scale_gran));
@@ -14330,6 +14349,18 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
exponent <<= 1;
}
#endif
for (int64_t ne0 : {1, 3, 4, 8}) {
for (bool mask : {false, true}) {
for (bool sinks : {false, true}) {
for (float max_bias : {0.0f, 8.0f}) {
if (!mask && max_bias > 0.0f) continue;
test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {ne0, 37, 2, 3}, mask, sinks, GGML_TYPE_F16, {1, 1}, 0.1f, max_bias));
}
}
}
}
test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 37, 2, 3}, true, true, GGML_TYPE_F16, {2, 3}, 0.1f, 8.0f));
test_cases.emplace_back(new test_soft_max(GGML_TYPE_F32, {4, 300000, 1, 1}, false, false, GGML_TYPE_F32, {1, 1}, 1.0f, 0.0f));
for (bool mask : {false, true}) {
for (bool sinks : {false, true}) {
for (float max_bias : {0.0f, 8.0f}) {
@@ -14722,6 +14753,10 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1, 1, 1 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 1024, 1, 1 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 33, 256, 1, 1 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 1, 37, 2, 3 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 4, 37, 2, 3 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 8, 37, 2, 3 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 4, 300000, 1, 1 }));
test_cases.emplace_back(new test_sum_rows(GGML_TYPE_F32, { 17, 65537, 1, 1 }));
test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {64, 64, 320, 1}));
test_cases.emplace_back(new test_group_norm(GGML_TYPE_F32, {9, 9, 1280, 1}));
+12
View File
@@ -706,6 +706,18 @@ static void test_expressions(testing & t) {
"3,-4,-4,3.0,3"
);
test_template(t, "modulo",
"{{ 7 % 3 }},{{ a % 3 }},{{ 7 % b }},{{ a % b }},{{ (a // 2) * 2 + a % 2 }},{{ 9007199254740993 % 2 }}",
{{"a", -7}, {"b", -3}},
"1,2,-2,-1,-7,1"
);
test_template(t, "float modulo",
"{{ 7.5 % 2 }},{{ c % 2 }},{{ 7.5 % b }},{{ c % 3 }},{{ 6.0 % b }}",
{{"b", -2}, {"c", -7.5}},
"1.5,0.5,-0.5,1.5,-0.0"
);
test_template(t, "string comparison",
"{{ 'b' > 'a' }},{{ 'VBD' > 'VB' }},{{ 'a' <= 'a' }},{{ 'Z' < 'a' }},{{ 'ab' >= 'b' }}",
json::object(),
-16
View File
@@ -264,22 +264,6 @@ or, if `invoke()` threw:
There is no `[DONE]` sentinel (unlike `/chat/completions`), the stream ends after the `done`
**Background tasks**
`exec_shell_command` runs every command as a task. With an `X-Conversation-Id` header, set by the client and never by the model, a task can move to the background: owned by that conversation, it keeps running after its call returns. Without the header, commands run and block as before.
- `params.background: true` starts the command in the background, the call returns after about 1 s with the first output and a `task_id`
- `params.detach_on_timeout: true` moves the command to the background when it reaches its timeout, instead of killing it
- with `stream: true`, the first event carries the id of the command: `{"chunk": "", "exec_id": 3}`
Routes, all scoped to the conversation the request names:
- `POST /tools/tasks` with `{"conv_id": "c", "from_exec_id": 3}`: move a running command to the background, its call returns at once and the task keeps the same id
- `GET /tools/tasks/events?conv_ids=a,b`: SSE, a `snapshot` of the background tasks of these conversations, then `task_started`, `task_output` (`id` and `offset`, at most one every 500 ms per task) and `task_exited`
- `GET /tools/tasks/output?conv_id=c&id=3&from=0`: the task, its `output` since `from`, `next` for the next read, and `skipped` when older output was dropped (256 KB kept per task)
- `DELETE /tools/tasks?conv_id=c&id=3`: stop a task and everything it started
The model gets the `background_task` tool (`list`, `output`, `stop`) for the tasks of its conversation. Everything still running is stopped when the server exits.
### Router mode: how child <--> router communicates
Upon spawning a new child process using `subprocess`, both child and router listen to the stdout/stderr (combined)
+41 -10
View File
@@ -8,6 +8,11 @@
#include <utility>
#include <shared_mutex>
enum class stream_read_status {
OK,
OFFSET_LOST,
};
namespace {
constexpr int64_t STREAM_SESSION_TTL_SECONDS = 300;
constexpr size_t STREAM_SESSION_MAX_BYTES = 4 * 1024 * 1024;
@@ -67,6 +72,42 @@ void server_stream_session_manager_stop() {
g_stream_sessions.stop_gc();
}
struct stream_session {
std::string conversation_id;
int64_t started_ts; // unix seconds at construction
stream_session(std::string conversation_id_, size_t max_bytes_);
stream_session(const stream_session &) = delete;
stream_session & operator=(const stream_session &) = delete;
bool append(const char * data, size_t len);
void finalize();
// drain from offset into sink, blocking for more bytes or finalize. OFFSET_LOST if offset
// fell below the dropped prefix
stream_read_status read_from(size_t offset,
const std::function<bool(const char *, size_t)> & sink,
const std::function<bool()> & should_stop);
bool is_done() const;
bool is_cancelled() const;
size_t total_size() const; // bytes that ever entered the session
size_t dropped_prefix() const; // bytes evicted from the front due to cap
int64_t completed_at() const; // 0 while alive, unix seconds after finalize
void cancel();
private:
mutable std::mutex mu;
std::condition_variable cv;
std::vector<char> buffer;
size_t prefix_dropped;
size_t cap_bytes;
bool done;
std::atomic<bool> cancelled; // polled lock-free by the should_stop closure, no mu
int64_t completed_ts;
};
stream_session::stream_session(std::string conversation_id_, size_t max_bytes_)
: conversation_id(std::move(conversation_id_))
, started_ts(now_seconds())
@@ -153,16 +194,6 @@ stream_read_status stream_session::read_from(size_t offset,
}
}
stream_read_status stream_session::read_available(size_t offset, std::string & out) const {
std::lock_guard<std::mutex> lock(mu);
if (offset < prefix_dropped) {
return stream_read_status::OFFSET_LOST;
}
const size_t local_off = std::min(offset - prefix_dropped, buffer.size());
out.append(buffer.begin() + local_off, buffer.end());
return stream_read_status::OK;
}
bool stream_session::is_done() const {
std::lock_guard<std::mutex> lock(mu);
return done;
+1 -48
View File
@@ -3,62 +3,15 @@
#include "server-http.h"
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
// streaming buffer for one generation, survives HTTP disconnect. the producer appends SSE bytes,
// readers drain from any offset via read_from. keyed by conversation_id, one conv = one live session
enum class stream_read_status {
OK,
OFFSET_LOST,
};
struct stream_session {
std::string conversation_id;
int64_t started_ts; // unix seconds at construction
stream_session(std::string conversation_id_, size_t max_bytes_);
stream_session(const stream_session &) = delete;
stream_session & operator=(const stream_session &) = delete;
bool append(const char * data, size_t len);
void finalize();
// drain from offset into sink, blocking for more bytes or finalize. OFFSET_LOST if offset
// fell below the dropped prefix
stream_read_status read_from(size_t offset,
const std::function<bool(const char *, size_t)> & sink,
const std::function<bool()> & should_stop);
// append what is buffered from offset to out without waiting. OFFSET_LOST if offset fell below the dropped prefix
stream_read_status read_available(size_t offset, std::string & out) const;
bool is_done() const;
bool is_cancelled() const;
size_t total_size() const; // bytes that ever entered the session
size_t dropped_prefix() const; // bytes evicted from the front due to cap
int64_t completed_at() const; // 0 while alive, unix seconds after finalize
void cancel();
private:
mutable std::mutex mu;
std::condition_variable cv;
std::vector<char> buffer;
size_t prefix_dropped;
size_t cap_bytes;
bool done;
std::atomic<bool> cancelled; // polled lock-free by the should_stop closure, no mu
int64_t completed_ts;
};
struct stream_session;
using stream_session_ptr = std::shared_ptr<stream_session>;
File diff suppressed because it is too large Load Diff
-12
View File
@@ -25,7 +25,6 @@ struct server_tool {
int id;
std::function<bool()> alive;
void push(const std::string & chunk);
void push_data(const json & data);
};
// should_stop: true once the caller stopped waiting, e.g. the client dropped the connection; a null should_stop means "never stop"
virtual json invoke(json params, stream * st = nullptr, const std::function<bool()> & should_stop = nullptr) const = 0;
@@ -34,7 +33,6 @@ struct server_tool {
};
struct server_tools_runtime; // impl detail, defined in server-tools.cpp
struct tools_tasks; // impl detail, defined in server-tools.cpp
struct server_tools {
std::vector<std::unique_ptr<server_tool>> tools;
@@ -46,23 +44,13 @@ struct server_tools {
// set when --tools-runtime is configured; routes every tool call through an isolate
std::unique_ptr<server_tools_runtime> runtime;
// background tasks started by exec_shell_command, stopped with everything they started when this is destroyed
std::unique_ptr<tools_tasks> tasks;
void setup(const std::vector<std::string> & enabled_tools,
server_mcp & mcp_mgr,
const std::string & tools_runtime);
server_http_context::handler_t handle_get;
server_http_context::handler_t handle_post;
server_http_context::handler_t handle_tasks_events; // GET /tools/tasks/events?conv_ids=a,b
server_http_context::handler_t handle_tasks_create; // POST /tools/tasks {"conv_id": c, "from_exec_id": n}
server_http_context::handler_t handle_tasks_output; // GET /tools/tasks/output?conv_id=c&id=n&from=offset
server_http_context::handler_t handle_tasks_stop; // DELETE /tools/tasks?conv_id=c&id=n
server_tools();
~server_tools();
private:
void setup_tasks_routes();
};
-8
View File
@@ -308,10 +308,6 @@ static bool server_setup_features(
}
ctx_http.get ("/tools", ex_wrapper(tools.handle_get));
ctx_http.post("/tools", ex_wrapper(tools.handle_post));
ctx_http.get ("/tools/tasks/events", ex_wrapper(tools.handle_tasks_events));
ctx_http.get ("/tools/tasks/output", ex_wrapper(tools.handle_tasks_output));
ctx_http.post("/tools/tasks", ex_wrapper(tools.handle_tasks_create));
ctx_http.del ("/tools/tasks", ex_wrapper(tools.handle_tasks_stop));
if (!params.server_tools.empty()) {
warn_names.push_back("server tools (experimental)");
}
@@ -324,10 +320,6 @@ static bool server_setup_features(
} else {
ctx_http.get ("/tools", ex_wrapper(res_403));
ctx_http.post("/tools", ex_wrapper(res_403));
ctx_http.get ("/tools/tasks/events", ex_wrapper(res_403));
ctx_http.get ("/tools/tasks/output", ex_wrapper(res_403));
ctx_http.post("/tools/tasks", ex_wrapper(res_403));
ctx_http.del ("/tools/tasks", ex_wrapper(res_403));
}
if (!warn_names.empty()) {
@@ -1,4 +1,3 @@
import json
import os
import re
import shutil
@@ -7,7 +6,6 @@ import sys
import time
import pytest
import requests
from utils import *
server: ServerProcess
@@ -23,9 +21,6 @@ CONTAINER_IMAGE = "busybox"
COMMAND_LEAVING_A_PROCESS = "start /b ping -n 30 127.0.0.1 & echo started" if os.name == "nt" else "sleep 30 & echo started"
COMMAND_OUTLIVING_THE_TIMEOUT = "ping -n 30 127.0.0.1 & echo done" if os.name == "nt" else "sleep 30; echo done"
COMMAND_RUNNING_UNTIL_STOPPED = "ping -n 30 127.0.0.1 > nul" if os.name == "nt" else "sleep 30"
BACKGROUND_CONV_ID = "conv-background"
@pytest.fixture(autouse=True)
@@ -267,89 +262,6 @@ def test_tools_builtin_exec_shell_command_timeout_ends_the_command():
assert "done" not in res["plain_text_response"]
def call_tool_in_conversation(name: str, params: dict) -> dict:
return call_tool(name, params, headers={"X-Conversation-Id": f"{BACKGROUND_CONV_ID}::some/model"})
def test_tools_builtin_background_task_needs_a_conversation():
global server
server.start()
err = call_tool_expect_error("exec_shell_command", {"command": "echo hello", "background": True})
assert "X-Conversation-Id" in err
def test_tools_builtin_background_task_output_and_events():
global server
server.start()
events = requests.get(server.make_url(f"/tools/tasks/events?conv_ids={BACKGROUND_CONV_ID}"), stream=True, timeout=30)
res = call_tool_in_conversation("exec_shell_command", {"command": "echo hello", "background": True})
assert "hello" in res["plain_text_response"]
assert "exit code: 0" in res["plain_text_response"]
task_id = res["task_id"]
seen = []
for line in events.iter_lines():
if line.startswith(b"data: "):
seen.append(json.loads(line[6:])["event"])
if seen[-1] == "task_exited":
break
events.close()
assert seen[0] == "snapshot"
assert "task_started" in seen
assert f"{task_id}: exited" in call_tool_in_conversation("background_task", {"action": "list"})["plain_text_response"]
other = server.make_request("POST", "/tools", data={"tool": "background_task", "params": {"action": "output", "task_id": task_id}},
headers={"X-Conversation-Id": "conv-other"})
assert "error" in other.body
output = server.make_request("GET", f"/tools/tasks/output?conv_id={BACKGROUND_CONV_ID}&id={task_id}&from=0")
assert "hello" in output.body["output"]
def test_tools_builtin_background_task_stop():
global server
server.start()
res = call_tool_in_conversation("exec_shell_command", {"command": COMMAND_RUNNING_UNTIL_STOPPED, "background": True})
assert "running" in res["plain_text_response"]
start = time.monotonic()
stopped = server.make_request("DELETE", f"/tools/tasks?conv_id={BACKGROUND_CONV_ID}&id={res['task_id']}")
assert time.monotonic() - start < 10
assert stopped.body["status"] == "stopped"
def test_tools_builtin_exec_moved_to_the_background_while_running():
global server
server.start()
events = server.make_stream_request("POST", "/tools", data={
"tool": "exec_shell_command",
"params": {"command": COMMAND_RUNNING_UNTIL_STOPPED, "timeout": 20},
"stream": True,
}, headers={"X-Conversation-Id": BACKGROUND_CONV_ID})
exec_id = next(events)["exec_id"]
res = server.make_request("POST", "/tools/tasks", data={"conv_id": BACKGROUND_CONV_ID, "from_exec_id": exec_id})
assert res.status_code == 200, res.body
assert "moved to the background" in "".join(e.get("chunk", "") for e in events)
stopped = server.make_request("DELETE", f"/tools/tasks?conv_id={BACKGROUND_CONV_ID}&id={exec_id}")
assert stopped.body["status"] == "stopped"
def test_tools_builtin_exec_moved_to_the_background_on_timeout():
global server
server.start()
res = call_tool_in_conversation("exec_shell_command", {"command": COMMAND_RUNNING_UNTIL_STOPPED, "timeout": 1, "detach_on_timeout": True})
assert "moved to the background" in res["plain_text_response"]
stopped = server.make_request("DELETE", f"/tools/tasks?conv_id={BACKGROUND_CONV_ID}&id={res['task_id']}")
assert stopped.body["status"] == "stopped"
def test_tools_builtin_cwd_header(tmp_path):
global server
server.start()