Skip to content

Commit fa21872

Browse files
committed
Merge branch 'upstream' into concedo_experimental
# Conflicts: # docs/backend/SYCL.md # ggml/src/ggml-cpu/ggml-cpu.c # ggml/src/ggml-opencl/CMakeLists.txt # ggml/src/ggml-opencl/ggml-opencl.cpp # ggml/src/ggml-opencl/kernels/cvt.cl # ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32_ns.cl # ggml/src/ggml-opencl/kernels/gemv_moe_q4_k_f32_ns.cl # ggml/src/ggml-sycl/backend.hpp # ggml/src/ggml-sycl/common.hpp # ggml/src/ggml-sycl/dmmv.cpp # ggml/src/ggml-sycl/ggml-sycl.cpp # scripts/sync_vendor.py # tests/CMakeLists.txt # tests/test-alloc.cpp # tests/test-backend-ops.cpp # tests/test-chat.cpp # tests/test-gguf.cpp # tests/test-save-load-state.cpp
2 parents 1f9dd9c + f4253ef commit fa21872

76 files changed

Lines changed: 1404 additions & 1115 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/chat-auto-parser-generator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ common_peg_arena autoparser::build_parser(const generation_params & inputs, cons
147147
} else {
148148
parser = content.build_parser(ctx);
149149
}
150-
return pure_content ? p.prefix(generation_prompt, reasoning.start) + parser : p.prefix(generation_prompt, reasoning.start) << parser;
150+
const std::string reasoning_start = trim_whitespace(reasoning.start);
151+
return pure_content ? p.prefix(generation_prompt, reasoning_start) + parser : p.prefix(generation_prompt, reasoning_start) << parser;
151152
});
152153
}
153154

common/chat-diff-analyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ static std::vector<std::function<void(const common_chat_template & tmpl, autopar
124124
analysis.tools.format.section_end = "";
125125
analysis.tools.format.per_call_start = "<TOOLCALL>";
126126
analysis.tools.format.per_call_end = "</TOOLCALL>";
127+
analysis.tools.format.tools_array_wrapped = true;
127128
analysis.content.mode = content_mode::PLAIN;
128129
analysis.content.start = "";
129130
analysis.content.end = "";
130131
analysis.reasoning.mode = reasoning_mode::TAG_BASED;
131-
analysis.reasoning.start = "<think>\n\n";
132+
analysis.reasoning.start = "<think>\n";
132133
analysis.reasoning.end = "</think>";
133134
analysis.assistant_start = "<SPECIAL_11>Assistant";
134135
analysis.user_start = "<SPECIAL_11>User";
135136
analysis.preserved_tokens.clear();
136-
analysis.preserved_tokens.push_back("<SPECIAL_12>");
137137
analysis.preserved_tokens.push_back("<SPECIAL_11>");
138138
analysis.preserved_tokens.push_back("</think>");
139139
analysis.preserved_tokens.push_back("<TOOLCALL>");

common/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,9 @@ enum ggml_opt_optimizer_type common_opt_get_optimizer(const char *);
10821082
struct common_prompt_checkpoint {
10831083
int64_t n_tokens;
10841084

1085+
// (optional) id of the task that created the checkpoint
1086+
int id_task = -1;
1087+
10851088
llama_pos pos_min;
10861089
llama_pos pos_max;
10871090

embd_res/klite.embd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7765,7 +7765,7 @@ Current version indicated by LITEVER below.
77657765
const codeContainer = btn.parentElement.querySelector('pre code');
77667766
let innercode = codeContainer.innerText;
77677767
//remove common language descriptiors from the start
7768-
let langsmatched = ["matlab","jsonc","powershell","ps1","haskell","hs","vbnet","vb","apache","apacheconf","makefile","mk","ini","protobuf","proto","typescript","tsx","markdown","md","mkdown","mkd","python","py","javascript","js","jsx","html","xhtml","xml","css","json","typescript","ts","tsx","bash","sh","zsh","java","csharp","cs","c","h","cpp","hpp","php","sql","ruby","rb","go","golang","kotlin","kt","swift","rust","rs","r","dart","scala","dockerfile","docker","yaml","yml","ini","toml","perl","pl","shell","console","powershell","ps1","lua","typescript","ts"];
7768+
let langsmatched = ["matlab","jsonc","powershell","ps1","haskell","hs","vbnet","vb","apache","apacheconf","makefile","mk","ini","protobuf","proto","typescript","tsx","markdown","md","mkdown","mkd","python","py","javascript","js","jsx","html","xhtml","xml","css","json","typescript","ts","tsx","bash","sh","zsh","java","csharp","cs","c","h","cpp","hpp","php","sql","ruby","rb","go","golang","kotlin","kt","swift","rust","rs","r","dart","scala","dockerfile","docker","yaml","yml","ini","toml","perl","pl","shell","console","powershell","ps1","lua","typescript","ts","patch","diff"];
77697769
for(let i = 0; i < langsmatched.length; ++i) {
77707770
let matcher = langsmatched[i]+"\n";
77717771
if (innercode.startsWith(matcher)) {

ggml/include/ggml-rpc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ extern "C" {
88

99
#define RPC_PROTO_MAJOR_VERSION 4
1010
#define RPC_PROTO_MINOR_VERSION 0
11-
#define RPC_PROTO_PATCH_VERSION 1
11+
#define RPC_PROTO_PATCH_VERSION 2
1212

1313
#ifdef __cplusplus
14-
static_assert(GGML_OP_COUNT == 97, "GGML_OP_COUNT has changed - update RPC_PROTO_PATCH_VERSION");
14+
static_assert(GGML_OP_COUNT == 98, "GGML_OP_COUNT has changed - update RPC_PROTO_PATCH_VERSION");
1515
#endif
1616

1717
#define GGML_RPC_MAX_SERVERS 16

ggml/include/ggml.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ extern "C" {
576576
GGML_OP_RWKV_WKV7,
577577
GGML_OP_SOLVE_TRI,
578578
GGML_OP_GATED_DELTA_NET,
579+
GGML_OP_LIGHTNING_INDEXER,
579580

580581
GGML_OP_UNARY,
581582

@@ -2593,6 +2594,24 @@ extern "C" {
25932594
struct ggml_tensor * state,
25942595
int64_t K);
25952596

2597+
// DSA lightning indexer
2598+
//
2599+
// q: [n_embd_idx, n_head_idx, n_batch, ne3 ]
2600+
// k: [n_embd_idx, 1, n_kv, ne3 ]
2601+
// weights: [n_head_idx, n_batch, 1, ne3 ] !! prescaled !!
2602+
// mask: [n_kv, n_batch, 1, ne33] !! f16 !!
2603+
// res: [n_kv, n_batch, 1, ne3 ]
2604+
//
2605+
// broadcast:
2606+
// ne3 % ne33 == 0
2607+
//
2608+
GGML_API struct ggml_tensor * ggml_lightning_indexer(
2609+
struct ggml_context * ctx,
2610+
struct ggml_tensor * q,
2611+
struct ggml_tensor * k,
2612+
struct ggml_tensor * weights,
2613+
struct ggml_tensor * mask);
2614+
25962615
// custom operators
25972616

25982617
typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);

ggml/include/gguf.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ extern "C" {
125125
// get ith C string from array with given key_id
126126
GGML_API const char * gguf_get_arr_str (const struct gguf_context * ctx, int64_t key_id, size_t i);
127127

128-
GGML_API int64_t gguf_get_n_tensors (const struct gguf_context * ctx);
129-
GGML_API int64_t gguf_find_tensor (const struct gguf_context * ctx, const char * name); // returns -1 if the tensor is not found
130-
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int64_t tensor_id);
131-
GGML_API const char * gguf_get_tensor_name (const struct gguf_context * ctx, int64_t tensor_id);
132-
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int64_t tensor_id);
133-
GGML_API size_t gguf_get_tensor_size (const struct gguf_context * ctx, int64_t tensor_id);
128+
GGML_API int64_t gguf_get_n_tensors (const struct gguf_context * ctx);
129+
GGML_API int64_t gguf_find_tensor (const struct gguf_context * ctx, const char * name); // returns -1 if the tensor is not found
130+
GGML_API size_t gguf_get_tensor_offset(const struct gguf_context * ctx, int64_t tensor_id);
131+
GGML_API const char * gguf_get_tensor_name (const struct gguf_context * ctx, int64_t tensor_id);
132+
GGML_API const int64_t * gguf_get_tensor_ne (const struct gguf_context * ctx, int64_t tensor_id); // returns ne, an array of GGML_MAX_DIMS elements; ne[dim] is 1 for dim >= n_dims
133+
GGML_API enum ggml_type gguf_get_tensor_type (const struct gguf_context * ctx, int64_t tensor_id);
134+
GGML_API size_t gguf_get_tensor_size (const struct gguf_context * ctx, int64_t tensor_id);
134135

135136
// removes key if it exists, returns id that the key had prior to removal (-1 if it didn't exist)
136137
GGML_API int64_t gguf_remove_key(struct gguf_context * ctx, const char * key);

ggml/src/ggml-cpu/ggml-cpu.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,6 +2832,10 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
28322832
{
28332833
ggml_compute_forward_gated_delta_net(params, tensor);
28342834
} break;
2835+
case GGML_OP_LIGHTNING_INDEXER:
2836+
{
2837+
ggml_compute_forward_lightning_indexer(params, tensor);
2838+
} break;
28352839
case GGML_OP_MAP_CUSTOM1:
28362840
{
28372841
ggml_compute_forward_map_custom1(params, tensor);
@@ -3193,6 +3197,7 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
31933197
case GGML_OP_FLASH_ATTN_BACK:
31943198
case GGML_OP_SSM_CONV:
31953199
case GGML_OP_SSM_SCAN:
3200+
case GGML_OP_LIGHTNING_INDEXER:
31963201
{
31973202
n_tasks = n_threads;
31983203
} break;
@@ -3814,6 +3819,12 @@ struct ggml_cplan ggml_graph_plan(
38143819
// to prepare magnitude and phase for inverted FFTs.
38153820
cur = ggml_type_size(node->type)*(n_tasks + node->ne[0] * n_tasks * 4);
38163821
} break;
3822+
case GGML_OP_LIGHTNING_INDEXER:
3823+
{
3824+
// temp buffer for dequantizing lightning indexer keys
3825+
const int64_t ne10 = node->src[1]->ne[0];
3826+
cur += sizeof(float)*ne10*n_tasks;
3827+
} break;
38173828
default:
38183829
break;
38193830
}

ggml/src/ggml-cpu/ops.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11568,3 +11568,87 @@ void ggml_compute_forward_fwht(const ggml_compute_params * params, ggml_tensor *
1156811568
}
1156911569
}
1157011570
}
11571+
11572+
// ggml_compute_forward_lightning_indexer
11573+
11574+
void ggml_compute_forward_lightning_indexer(
11575+
const ggml_compute_params * params,
11576+
ggml_tensor * dst) {
11577+
11578+
const ggml_tensor * q = dst->src[0];
11579+
const ggml_tensor * k = dst->src[1];
11580+
const ggml_tensor * w = dst->src[2]; // weights
11581+
const ggml_tensor * m = dst->src[3]; // mask
11582+
11583+
GGML_ASSERT(dst->type == GGML_TYPE_F32);
11584+
GGML_ASSERT( q->type == GGML_TYPE_F32);
11585+
GGML_ASSERT( w->type == GGML_TYPE_F32);
11586+
GGML_ASSERT( m->type == GGML_TYPE_F16);
11587+
11588+
GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
11589+
GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
11590+
GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
11591+
GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
11592+
GGML_TENSOR_LOCALS(int64_t, new, w, ne)
11593+
GGML_TENSOR_LOCALS(size_t, nbw, w, nb)
11594+
GGML_TENSOR_LOCALS(int64_t, nem, m, ne)
11595+
GGML_TENSOR_LOCALS(size_t, nbm, m, nb)
11596+
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
11597+
GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
11598+
11599+
GGML_ASSERT( nb0 == ggml_type_size(dst->type));
11600+
GGML_ASSERT(nbq0 == ggml_type_size( q->type));
11601+
GGML_ASSERT(nbk0 == ggml_type_size( k->type));
11602+
GGML_ASSERT(nbw0 == ggml_type_size( w->type));
11603+
GGML_ASSERT(nbm0 == ggml_type_size( m->type));
11604+
11605+
const int n_embd = q->ne[0];
11606+
const int n_head = q->ne[1];
11607+
const int n_tokens = q->ne[2];
11608+
const int n_stream = q->ne[3];
11609+
const int n_kv = k->ne[2];
11610+
11611+
ggml_to_float_t const k_to_float = ggml_get_type_traits(k->type)->to_float;
11612+
GGML_ASSERT((k->type == GGML_TYPE_F32 || k_to_float) && "lightning indexer: unsupported K-type");
11613+
11614+
const int nr = n_kv;
11615+
const int ith = params->ith;
11616+
const int nth = params->nth;
11617+
11618+
// (temporary) buffer for K converted to float
11619+
float * k_row_f32 = (float *) params->wdata + ith*(1*n_embd + CACHE_LINE_SIZE_F32);
11620+
11621+
// rows per thread
11622+
const int dr = (nr + nth - 1)/nth;
11623+
11624+
// row range for this thread
11625+
const int ir0 = dr*ith;
11626+
const int ir1 = MIN(ir0 + dr, nr);
11627+
11628+
for (int s = 0; s < n_stream; ++s) {
11629+
for (int t = 0; t < n_tokens; ++t) {
11630+
const float * w_row = (float *) ((char *) w->data + t*nbw1 + s*nbw3);
11631+
const ggml_fp16_t * m_row = (ggml_fp16_t *) ((char *) m->data + t*nbm1 + (s%nem3)*nbm3);
11632+
float * dst_row = (float *) ((char *) dst->data + t*nb1 + s*nb3 );
11633+
for (int ik = ir0; ik < ir1; ++ik) {
11634+
char * k_row = (char *) k->data + ik*nbk2 + s*nbk3;
11635+
if (k_to_float) {
11636+
k_to_float(k_row, k_row_f32, n_embd);
11637+
} else {
11638+
k_row_f32 = (float *) k_row;
11639+
}
11640+
float score = 0.0f;
11641+
for (int h = 0; h < n_head; ++h) {
11642+
// dot product of q and k for head h
11643+
float qk = 0.0f;
11644+
const float * q_row = (float *) ((char *) q->data + h*nbq1 + t*nbq2 + s*nbq3);
11645+
ggml_vec_dot_f32(n_embd, &qk, 0, q_row, 0, k_row_f32, 0, 1);
11646+
// ReLU and weights (prescaled)
11647+
score += MAX(qk, 0.0f) * w_row[h];
11648+
}
11649+
// apply mask
11650+
dst_row[ik] = score + GGML_CPU_FP16_TO_FP32(m_row[ik]);
11651+
}
11652+
}
11653+
}
11654+
}

ggml/src/ggml-cpu/ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void ggml_compute_forward_rwkv_wkv7(const struct ggml_compute_params * params, s
105105
void ggml_compute_forward_solve_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst);
106106
void ggml_compute_forward_gla(const struct ggml_compute_params * params, struct ggml_tensor * dst);
107107
void ggml_compute_forward_gated_delta_net(const struct ggml_compute_params * params, struct ggml_tensor * dst);
108+
void ggml_compute_forward_lightning_indexer(const struct ggml_compute_params * params, struct ggml_tensor * dst);
108109
void ggml_compute_forward_map_custom1(const struct ggml_compute_params * params, struct ggml_tensor * dst);
109110
void ggml_compute_forward_map_custom2(const struct ggml_compute_params * params, struct ggml_tensor * dst);
110111
void ggml_compute_forward_map_custom3(const struct ggml_compute_params * params, struct ggml_tensor * dst);

0 commit comments

Comments
 (0)