Compare commits

..
4 Commits
Author SHA1 Message Date
Mark Cockram 9a278b02b8 server : include timings in Anthropic /v1/messages responses (#30244)
Add the timings object to the non-streaming response and to the final
message_delta event of the stream, as the OpenAI chat and Responses
paths already do. Clients such as llama-swap read prompt and generation
speed from it and otherwise report -1 for /v1/messages requests.

Update test_compat_anthropic.py to expect timings there; message_stop
still carries none.
2026-11-30 02:31:01 +01:00
Evan Huus 9fcc40d3d4 common : share the kv arg parser with llama-bench (#32831)
Assisted-by: Codex
2026-11-30 01:31:06 +01:00
Aldehir Rojas 57577675aa server : pass in --json-schema to chat parsing (#31862)
* server : apply --json-schema through the chat template

* server : update comment

* cont : move field
2026-11-30 00:29:32 +01:00
thelittlefireman bb5aacd61d ggml-cuda: Add GCN HIP sparse fattn (#30370)
Assisted-by: ChatGPT
2026-11-30 00:02:48 +01:00
15 changed files with 214 additions and 53 deletions
+14 -2
View File
@@ -291,13 +291,21 @@ const std::vector<ggml_type> kv_cache_types = {
GGML_TYPE_Q5_1,
};
static ggml_type kv_cache_type_from_str(const std::string & s) {
ggml_type common_parse_kv_cache_type(const std::string & s) {
for (const auto & type : kv_cache_types) {
if (ggml_type_name(type) == s) {
return type;
}
}
throw std::runtime_error("Unsupported cache type: " + s);
return GGML_TYPE_COUNT;
}
static ggml_type kv_cache_type_from_str(const std::string & s) {
const ggml_type type = common_parse_kv_cache_type(s);
if (type == GGML_TYPE_COUNT) {
throw std::runtime_error("Unsupported cache type: " + s);
}
return type;
}
static std::string get_all_kv_cache_types() {
@@ -2253,6 +2261,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
"BNF-like grammar to constrain generations (see samples in grammars/ dir)",
[](common_params & params, const std::string & value) {
params.sampling.grammar = {COMMON_GRAMMAR_TYPE_USER, value};
params.sampling.json_schema.clear();
}
).set_sampling());
add_opt(common_arg(
@@ -2260,6 +2269,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
"file to read grammar from",
[](common_params & params, const std::string & value) {
params.sampling.grammar = {COMMON_GRAMMAR_TYPE_USER, read_file(value)};
params.sampling.json_schema.clear();
}
).set_sampling());
add_opt(common_arg(
@@ -2267,6 +2277,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
"JSON schema to constrain generations (https://json-schema.org/), e.g. `{\"type\": \"object\"}` for any JSON object",
[](common_params & params, const std::string & value) {
params.sampling.grammar = {COMMON_GRAMMAR_TYPE_OUTPUT_FORMAT, json_schema_to_grammar(json::parse(value))};
params.sampling.json_schema = value;
}
).set_sampling());
add_opt(common_arg(
@@ -2284,6 +2295,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
std::back_inserter(schema)
);
params.sampling.grammar = {COMMON_GRAMMAR_TYPE_OUTPUT_FORMAT, json_schema_to_grammar(json::parse(schema))};
params.sampling.json_schema = schema;
}
).set_sampling());
add_opt(common_arg(
+2
View File
@@ -19,6 +19,8 @@
// CLI argument parsing
//
ggml_type common_parse_kv_cache_type(const std::string & value);
struct common_arg {
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
std::set<enum llama_example> excludes = {};
+3
View File
@@ -2034,6 +2034,9 @@ void common_chat_session::apply_sampling(common_params_sampling & sampling) cons
}
if (!grammar_text.empty()) {
sampling.grammar = {COMMON_GRAMMAR_TYPE_TOOL_CALLS, grammar_text};
} else if (sampling.grammar.type == COMMON_GRAMMAR_TYPE_OUTPUT_FORMAT) {
// drop the schema grammar when the template does not use it, since it would reject the generation prompt
sampling.grammar = {};
}
sampling.grammar_lazy = grammar_lazy;
sampling.generation_prompt = generation_prompt_text;
+1
View File
@@ -280,6 +280,7 @@ struct common_params_sampling {
common_grammar grammar; // optional grammar constraint (user / output-format / tool-calls)
bool grammar_lazy = false;
std::vector<common_grammar_trigger> grammar_triggers; // optional triggers (for lazy grammars)
std::string json_schema; // schema the output-format grammar was built from, applied through the chat template
std::set<llama_token> preserved_tokens;
std::vector<llama_logit_bias> logit_bias; // logit biases to apply
+50 -14
View File
@@ -373,9 +373,21 @@ static constexpr __device__ int ggml_cuda_fattn_tile_get_nbatch_K(const int DKQ,
return (ggml_cuda_fattn_tile_get_config(DKQ, DV, ncols) >> 23) & ((1 << 9) - 1);
}
#ifdef GGML_USE_HIP
// The sparse launch pins ncols1 == 1, so the ncols2 parameter is the total number of Q columns per block.
static constexpr bool ggml_cuda_flash_attn_ext_tile_may_use_sparse(
const int DKQ, const int DV, const int ncols2, const bool use_logit_softcap) {
return DKQ > 64 && ncols2 > 1 && !use_logit_softcap &&
ggml_cuda_fattn_tile_get_config_amd(DKQ, DV, ncols2) != 0 &&
ggml_cuda_fattn_tile_get_config_amd_rdna(DKQ, DV, ncols2) != 0;
}
bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_tensor * dst, const int DKQ, const int DV);
#endif // GGML_USE_HIP
// Function that performs a single iteration in for the KQ matrix multiplication:
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int nbatch_fa, int nbatch_K,
bool use_logit_softcap, bool oob_check, bool indexed_bias, typename T_vec_dot, typename T_K>
bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_K>
static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
T_vec_dot * const Q_tmp,
const T_K * const __restrict__ K_h2,
@@ -394,7 +406,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
constexpr int np = nwarps > ncols ? nwarps/ncols : 1; // number of parallel warps per Q column
constexpr int stride_tile_K = std::is_same_v<T_vec_dot, half2> ? nbatch_K/2 + cpy_ne : nbatch_K + cpy_ne;
flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, false, indexed_bias, false, T_K, T_vec_dot>
flash_attn_load_tile<warp_size, nwarps, nbatch_fa, nbatch_K, stride_tile_K, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_K, T_vec_dot>
(K_h2 + k_KQ_0/2, KV_tmp, nbatch_K/2, stride_K2, k_VKQ_0, k_VKQ_sup, rows);
__syncthreads();
@@ -452,7 +464,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter_KQ(
// Function that performs a single iteration of the main loop over up to nbatch_fa tokens.
template <int warp_size, int nwarps, int ncols1, int ncols2, int DKQ, int DV, int nbatch_fa, int nbatch_K,
bool use_logit_softcap, bool oob_check, bool indexed_bias, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
bool use_logit_softcap, bool oob_check, bool indexed_bias, bool use_sparse, typename T_vec_dot, typename T_KQ, typename T_acc, typename T_K, typename T_V>
static __device__ __forceinline__ void flash_attn_tile_iter(
T_vec_dot * const Q_tmp,
const T_K * const __restrict__ K_h2,
@@ -505,12 +517,12 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
constexpr int nbatch_K_last = DKQ % nbatch_K;
#pragma unroll
for (int k_KQ_0 = 0; k_KQ_0 < DKQ - nbatch_K_last; k_KQ_0 += nbatch_K) {
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias>(
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
}
if (nbatch_K_last > 0) {
constexpr int k_KQ_0 = DKQ - nbatch_K_last;
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, indexed_bias>(
flash_attn_tile_iter_KQ<warp_size, nwarps, ncols1, ncols2, DKQ, nbatch_fa, nbatch_K_last, use_logit_softcap, oob_check, indexed_bias, use_sparse>(
Q_tmp, K_h2, KV_tmp, stride_K2, k_VKQ_0, k_VKQ_sup, k_KQ_0, KQ_acc, rows);
}
@@ -535,7 +547,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
if (!oob_check || i_KQ < k_VKQ_sup) {
if (indexed_bias || ncols2 > 1 || mask) {
const int k = k_VKQ_0 + i_KQ;
const int k = use_sparse ? rows[k_VKQ_0 + i_KQ] : k_VKQ_0 + i_KQ;
__builtin_assume(k >= 0); // avoid sign extension and register spills on RDNA
const float m = __half2float(mask[int64_t(j)*stride_mask + k]);
KQ_acc[(i_KQ_0/(np*warp_size))*cpw + jc0] += slope*(indexed_bias ? bias.add(m, j, k) : m);
@@ -621,7 +633,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
constexpr int stride_tile_V = std::is_same_v<T_vec_dot, half2> ? DV/2 : DV;
#pragma unroll
for (int k0 = 0; k0 < nbatch_fa; k0 += nbatch_V) {
flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, false, indexed_bias, false, T_V, T_vec_dot>
flash_attn_load_tile<warp_size, nwarps, nbatch_V, DV, stride_tile_V, false, oob_check, use_sparse, indexed_bias || use_sparse, false, T_V, T_vec_dot>
(V_h2, KV_tmp, DV/2, stride_V2, k_VKQ_0 + k0, k_VKQ_sup - k0, rows);
__syncthreads();
@@ -691,7 +703,7 @@ static __device__ __forceinline__ void flash_attn_tile_iter(
}
}
template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool indexed_bias, ggml_type type_K, ggml_type type_V, bool use_kv_rows> // D == head size
template<int DKQ, int DV, int ncols1, int ncols2, bool use_logit_softcap, bool indexed_bias, ggml_type type_K, ggml_type type_V, bool use_kv_rows, bool use_sparse = false> // D == head size
__launch_bounds__(ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols1*ncols2), ggml_cuda_fattn_tile_get_occupancy(DKQ, DV, ncols1*ncols2))
static __global__ void flash_attn_tile(
const float2 * Q_ptr,
@@ -732,6 +744,15 @@ static __global__ void flash_attn_tile(
// Skip unused kernel variants for faster compilation:
#ifndef GCN
if constexpr (use_sparse) {
NO_DEVICE_CODE;
return;
}
#endif // GCN
static_assert(!use_sparse || (ncols1 == 1 && !indexed_bias && !use_kv_rows), "bad sparse configuration");
if ((use_logit_softcap && !(DV == 128 || DV == 256 || DV == 512))) {
GGML_UNUSED_VARS(Q, K, V, mask, sinks, KV_max, dst, dst_meta, scale,
max_bias, m0, m1, n_head_log2, logit_softcap,
@@ -767,7 +788,10 @@ static __global__ void flash_attn_tile(
const int sequence = blockIdx.z / (ne02/ncols2);
const int head0 = blockIdx.z*ncols2 - sequence*ne02; // == blockIdx.z % (ne02/ncols2)
const int32_t * rows = use_kv_rows ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
// For sparse attention, ne11 is the capacity of each index list, not the KV cache length.
const int64_t list = use_sparse ? int64_t(sequence % ne33)*gridDim.x + blockIdx.x : 0;
const int32_t * rows = use_sparse ? KV_max + list*ne11 :
use_kv_rows ? KV_rows_ptr + int64_t(sequence)*ne11 : nullptr;
__builtin_assume(!use_kv_rows || rows != nullptr);
const fattn_indexed_bias head_bias = indexed_bias ? bias.for_head(int64_t(sequence)*ne01.z, head0) : bias;
const int gqa_ratio = ne02 / ne12; // With grouped query attention there are > 1 Q matrices per K, V matrix.
@@ -868,20 +892,21 @@ static __global__ void flash_attn_tile(
__syncthreads();
// Main loop over KV cache:
const int k_VKQ_max = KV_max ? KV_max[sequence*gridDim.x + blockIdx.x] : ne11;
if (ncols2 == 1) {
const int k_VKQ_max = use_sparse ? KV_max[int64_t(gridDim.x)*ne33*ne11 + list] :
KV_max ? KV_max[sequence*gridDim.x + blockIdx.x] : ne11;
if (ncols2 == 1 || use_sparse) {
// Branch with out-of-bounds checks.
int k_VKQ_0 = blockIdx.y*nbatch_fa;
while (k_VKQ_0 <= k_VKQ_max - nbatch_fa) {
constexpr bool oob_check = false;
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias>
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
k_VKQ_0 += gridDim.y*nbatch_fa;
}
if (k_VKQ_0 < k_VKQ_max) {
constexpr bool oob_check = true;
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias>
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
}
@@ -889,7 +914,7 @@ static __global__ void flash_attn_tile(
// Branch without out-of-bounds checks.
for (int k_VKQ_0 = blockIdx.y*nbatch_fa; k_VKQ_0 < k_VKQ_max; k_VKQ_0 += gridDim.y*nbatch_fa) {
constexpr bool oob_check = false;
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias>
flash_attn_tile_iter<warp_size, nwarps, ncols1, ncols2, DKQ, DV, nbatch_fa, nbatch_K, use_logit_softcap, oob_check, indexed_bias, use_sparse>
(Q_tmp, K_h2, V_h2, maskh, ne01, logit_softcap, slope, KQ, KV_tmp,
stride_K2, stride_V2, stride_mask, KQ_max, KQ_sum, VKQ, k_VKQ_0, k_VKQ_max, col_Q_0, head_bias, rows);
}
@@ -1077,6 +1102,17 @@ static void launch_fattn_tile_switch_ncols1(ggml_backend_cuda_context & ctx, ggm
constexpr size_t nbytes_shared = 0;
#ifdef GGML_USE_HIP
if constexpr (!indexed_bias && !use_kv_rows && ggml_cuda_flash_attn_ext_tile_may_use_sparse(DKQ, DV, ncols2, use_logit_softcap)) {
if (ggml_cuda_flash_attn_ext_tile_shall_use_sparse(cc, dst, DKQ, DV)) {
const int nwarps = ggml_cuda_fattn_tile_get_nthreads(DKQ, DV, ncols2, cc) / warp_size;
const int nbatch_fa = ggml_cuda_fattn_tile_get_nbatch_fa(DKQ, DV, ncols2, cc);
fattn_kernel_t fattn_kernel = flash_attn_tile<DKQ, DV, 1, ncols2, use_logit_softcap, false, type_K, type_V, false, true>;
launch_fattn<DV, 1, ncols2, T_K, T_V>
(ctx, dst, fattn_kernel, nwarps, nbytes_shared, nbatch_fa, true, true, false, true, warp_size);
return;
}
}
// Limit indexed bias tile widths to avoid spilling registers on AMD.
// TODO: select the tile width in one function.
constexpr int max_cols = !indexed_bias ? 64 : DKQ == 64 ? 32 : use_kv_rows ? 16 : 8;
+65
View File
@@ -127,6 +127,71 @@ void ggml_cuda_flash_attn_ext_compact_mask(
#endif // !defined(GGML_USE_MUSA)
}
#ifdef GGML_USE_HIP
bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_tensor * dst, const int DKQ, const int DV) {
const ggml_tensor * Q = dst->src[0];
const ggml_tensor * K = dst->src[1];
const ggml_tensor * mask = dst->src[3];
const int n_kv_max = ggml_get_op_params_i32(dst, 4);
const int64_t n_kv = K->ne[1];
float max_bias = 0.0f;
memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
if (!mask || mask->ne[0] != n_kv || mask->ne[1] < Q->ne[1] ||
mask->ne[2] != 1 || dst->src[5] || n_kv_max <= 0 || max_bias != 0.0f) {
return false;
}
if (GGML_CUDA_CC_IS_GCN(cc)) {
// Sparse compaction is not beneficial when the selected positions exceed 25% of the KV cache.
if (n_kv_max > n_kv/4) {
return false;
}
const int gqa_ratio = Q->ne[2] / K->ne[2];
if (DKQ == 72 && DV == 72) {
return n_kv >= 8192;
}
if (DKQ == 80 && DV == 80) {
return n_kv >= 16384;
}
if (DKQ == 96 && DV == 96) {
return n_kv >= 16384;
}
if (DKQ == 112 && DV == 112) {
return n_kv >= 16384;
}
if (DKQ == 128 && DV == 128) {
return n_kv >= 8192;
}
if (DKQ == 192 && DV == 128) {
return n_kv >= 16384;
}
if (DKQ == 256 && DV == 256) {
if (gqa_ratio == 2) {
return (n_kv_max <= DKQ && n_kv >= 16384) ||
(n_kv >= 32768 && (Q->ne[1] == 1 || n_kv_max <= 2*DKQ));
}
return n_kv >= 4096 && (Q->ne[1] == 1 || n_kv_max < n_kv/8);
}
if (DKQ == 320 && DV == 256) {
return n_kv >= 65536 && gqa_ratio >= 64;
}
if (DKQ == 512 && DV == 512) {
return n_kv >= 16384 && gqa_ratio >= 16;
}
if (DKQ == 576 && DV == 512) {
return n_kv >= 32768 && gqa_ratio >= 32;
}
}
return false;
}
#endif // GGML_USE_HIP
bool ggml_cuda_flash_attn_ext_mma_f16_shall_use_sparse(const int cc, const ggml_tensor * dst, const int ncols1, const int ncols2) {
#ifdef GGML_USE_MUSA
GGML_UNUSED_VARS(cc, dst, ncols1, ncols2);
+31
View File
@@ -15053,6 +15053,30 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
// KV not a multiple of the compaction workgroup size.
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 1, { 8, 1}, 5003, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512));
// Long-context sparse attention selection: sparse tiles and dense fallbacks.
test_cases.emplace_back(new test_flash_attn_ext( 72, 72, 2, {12, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext( 80, 80, 2, {12, 1}, 16384, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 2, {12, 1}, 16384, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 2, {12, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(192, 128, 2, {32, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 256));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, { 2, 1}, 16384, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, { 2, 1}, 32768, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(320, 256, 1, {32, 1}, 65536, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(320, 256, 1, {64, 1}, 65536, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(512, 512, 1, {16, 1}, 65536, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 512));
test_cases.emplace_back(new test_flash_attn_ext(576, 512, 1, {32, 1}, 65536, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, true, 512));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 2}, 4096, 1, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 256));
// GCN sparse attention with independent masks for multiple query tokens.
for (int64_t nq : {2, 4, 8, 9, 16}) {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 16384, nq, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
}
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 8192, 8, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 8, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 2, {12, 1}, 32768, 8, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 2}, 16384, 4, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
// Sparse gather: native block sizes, padded slots, and head/batch strides.
for (ggml_type type : {GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q4_0, GGML_TYPE_Q4_1, GGML_TYPE_Q5_0, GGML_TYPE_Q5_1, GGML_TYPE_Q8_0, GGML_TYPE_IQ4_NL}) {
test_cases.emplace_back(new test_flash_attn_ext(128, 96, 2, {8, 2}, 5003, 3, true, false, 0, 0, GGML_PREC_F32, type, type, {0, 1, 2, 3}, true, false, 257));
@@ -15870,6 +15894,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
// Same non-causal vision-encoder shape for the other head size padded in-kernel by the CUDA MMA kernels
test_cases.emplace_back(new test_flash_attn_ext(40, 40, 16, {1, 1}, 5776, 5776, false, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
// GCN sparse attention break-even for multiple query tokens.
for (int64_t kv : {16384, 32768, 65536}) {
for (int64_t nq : {1, 2, 4, 8, 16, 32, 64}) {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, kv, nq, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16, {0, 1, 2, 3}, true, false, 2048));
}
}
// Sparse flash attention (n_kv_max hint) decode across KV depths.
// Shapes: 576/512 DeepSeek MLA, 512/512 DeepSeek-V4/GLM-5.2, 256/256 gqa12 Qwen QSA.
for (int64_t kv : {4096, 16384, 32768}) {
+2 -34
View File
@@ -493,38 +493,6 @@ static void print_usage(int /* argc */, char ** argv) {
"'first-last' or 'first-last+step' or 'first-last*mult'.\n");
}
static ggml_type ggml_type_from_name(const std::string & s) {
if (s == "f16") {
return GGML_TYPE_F16;
}
if (s == "bf16") {
return GGML_TYPE_BF16;
}
if (s == "f8_e4m3") {
return GGML_TYPE_F8_E4M3;
}
if (s == "q8_0") {
return GGML_TYPE_Q8_0;
}
if (s == "q4_0") {
return GGML_TYPE_Q4_0;
}
if (s == "q4_1") {
return GGML_TYPE_Q4_1;
}
if (s == "q5_0") {
return GGML_TYPE_Q5_0;
}
if (s == "q5_1") {
return GGML_TYPE_Q5_1;
}
if (s == "iq4_nl") {
return GGML_TYPE_IQ4_NL;
}
return GGML_TYPE_COUNT;
}
static cmd_params parse_cmd_params(int argc, char ** argv) {
cmd_params params;
std::string arg;
@@ -644,7 +612,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
ggml_type gt = ggml_type_from_name(t);
ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;
@@ -664,7 +632,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
std::vector<ggml_type> types;
for (const auto & t : p) {
ggml_type gt = ggml_type_from_name(t);
ggml_type gt = common_parse_kv_cache_type(t);
if (gt == GGML_TYPE_COUNT) {
invalid_param = true;
break;
+10
View File
@@ -1542,6 +1542,12 @@ json oaicompat_chat_params_parse(
}
}
// fall back to --json-schema if the request doesn't overwrite it
bool use_tools = has_tools && tool_choice != "none";
if (!opt.json_schema.empty() && !use_tools && json_schema.is_null() && grammar.empty() && !body.contains("response_format")) {
json_schema = json::parse(opt.json_schema);
}
// an absent or empty schema means any object
if (json_schema.is_object() && json_schema.empty()) {
json_schema["type"] = "object";
@@ -1620,6 +1626,10 @@ json oaicompat_chat_params_parse(
if (body.contains("grammar")) {
throw std::invalid_argument("Cannot use custom grammar constraints with tools.");
}
if (!json_schema.is_null()) {
// only a few models natively support response_format and tools, but none have support yet
throw std::invalid_argument("Cannot use response_format or json_schema with tools.");
}
}
// merge the template args provided from command line with the args provided in the user request
+1
View File
@@ -393,6 +393,7 @@ struct server_chat_params {
std::string media_path;
bool media_allow_private_urls = false;
bool force_pure_content = false;
std::string json_schema;
};
// used by /completions endpoint
+2 -1
View File
@@ -1758,7 +1758,8 @@ private:
/* reasoning_budget_msg */ params_base.sampling.reasoning_budget_message,
/* media_path */ params_base.media_path,
/* media_allow_private */ params_base.media_allow_private_urls,
/* force_pure_content */ params_base.force_pure_content_parser
/* force_pure_content */ params_base.force_pure_content_parser,
/* json_schema */ params_base.sampling.json_schema
};
{
+8
View File
@@ -794,6 +794,10 @@ json server_task_result_cmpl_final::to_json_anthropic() {
}}
};
if (stats.is_set()) {
res["timings"] = stats.to_json();
}
return res;
}
@@ -975,6 +979,10 @@ json server_task_result_cmpl_final::to_json_anthropic_stream() {
}}
});
if (stats.is_set()) {
events.back().at("data")["timings"] = stats.to_json();
}
events.push_back({
{"event", "message_stop"},
{"data", {
@@ -291,6 +291,23 @@ def test_completion_with_json_schema(jinja: bool, json_schema: dict, n_predicted
assert match_regex(re_content, choice["message"]["content"]), f'Expected {re_content}, got {choice["message"]["content"]}'
@pytest.mark.parametrize("jinja", [False, True])
def test_completion_with_server_json_schema(jinja: bool):
global server
server.jinja = jinja
server.json_schema = '{"const": "42"}'
server.start()
res = server.make_request("POST", "/chat/completions", data={
"max_tokens": 6,
"messages": [
{"role": "system", "content": "You are a coding assistant."},
{"role": "user", "content": "Write an example"},
],
})
assert res.status_code == 200, f'Expected 200, got {res.status_code}'
assert match_regex("\"42\"", res.body["choices"][0]["message"]["content"])
@pytest.mark.parametrize("jinja,grammar,n_predicted,re_content", [
(False, 'root ::= "a"{5,5}', 6, "a{5,5}"),
(True, 'root ::= "a"{5,5}', 6, "a{5,5}"),
@@ -77,8 +77,9 @@ def test_anthropic_messages_basic():
assert isinstance(res.body["usage"]["input_tokens"], int), "input_tokens should be integer"
assert isinstance(res.body["usage"]["output_tokens"], int), "output_tokens should be integer"
assert res.body["usage"]["output_tokens"] > 0, "Should have generated some tokens"
# Anthropic API should NOT include timings
assert "timings" not in res.body, "Anthropic API should not include timings field"
# llama-server extension: timings are included so clients can read generation speed
assert "timings" in res.body, "Anthropic API should include timings field"
assert res.body["timings"]["predicted_n"] > 0
def test_anthropic_messages_with_system():
@@ -206,6 +207,8 @@ def test_anthropic_messages_streaming():
assert message_delta["delta"]["stop_reason"] in ["end_turn", "max_tokens"]
assert "usage" in message_delta
assert message_delta["usage"]["output_tokens"] > 0
assert "timings" in message_delta, "message_delta should include timings"
assert message_delta["timings"]["predicted_n"] > 0
# Check message_stop
message_stop = next(e for e in events if e["type"] == "message_stop")
+3
View File
@@ -120,6 +120,7 @@ class ServerProcess:
reasoning: Literal['on', 'off', 'auto'] | None = None
chat_template: str | None = None
chat_template_file: str | None = None
json_schema: str | None = None
server_path: str | None = None
mmproj_url: str | None = None
no_mmproj: bool | None = None
@@ -300,6 +301,8 @@ class ServerProcess:
server_args.extend(["--chat-template", self.chat_template])
if self.chat_template_file:
server_args.extend(["--chat-template-file", self.chat_template_file])
if self.json_schema:
server_args.extend(["--json-schema", self.json_schema])
if self.mmproj_url:
server_args.extend(["--mmproj-url", self.mmproj_url])
if self.no_mmproj: