Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8263e97
sync : ggml
ggerganov May 27, 2025
525d51b
Vulkan: Add f32 accumulator support to quantized mul mat to fix GLM4 …
0cc4m May 19, 2025
380aae7
sycl : Overcoming workaround for mmap() allocation on Windows (llama/…
s-Nick May 20, 2025
2192394
metal : fix typo in FA kernel comments (llama/13651)
ggerganov May 20, 2025
1e7f745
sycl: disable reorder for sycl mulmat (llama/13536)
sgeor255 May 20, 2025
5e90842
CUDA: skip fully masked-out KV in FA vec kernel (llama/13584)
JohannesGaessler May 20, 2025
e5f0301
vulkan: fix warnings (llama/13626)
netrunnereve May 20, 2025
55050e4
musa: Upgrade MUSA SDK version to rc4.0.1 and use mudnn::Unary::IDENT…
yeahdongcn May 21, 2025
39a2783
ggml : add ggml_gelu_erf() (llama/13667)
ngxson May 21, 2025
547c3cd
opencl: fix couple crashes (llama/12795)
May 21, 2025
0079d89
opencl: Add support for multiple devices (llama/12622)
May 21, 2025
09499e6
SYCL: Avoid using with SYCL-Graph for unsupported nodes (llama/13587)
May 22, 2025
28c7ab8
sycl : Remove waits from function calls (llama/13702)
s-Nick May 22, 2025
c10deb2
use LOG_WARN to replace `std::cerr` (llama/13657)
foldl May 23, 2025
8e2a08b
vulkan: Disable coopmat/coopmat2/bfloat extensions if glslc doesn't s…
jeffbolznv May 23, 2025
1fecf05
vulkan: support CPY from any type to itself (llama/13695)
jeffbolznv May 23, 2025
39dc9dd
ggml : fix the order of ggml_unary_op (llama/13718)
ngxson May 23, 2025
85c583d
CANN: Support MUL_MAT_ID for q8_0 and q4_0 (llama/13705)
noemotiovon May 23, 2025
d3b5380
CUDA: fix race condition in FA vector kernels (llama/13742)
JohannesGaessler May 24, 2025
093dfaa
ggml : add ggml_gelu_erf() CUDA kernel (llama/13719)
ngxson May 24, 2025
3df6086
ggml-cpu : set openmp wait time if not set (llama/13758)
slaren May 24, 2025
4d4a5d7
SYCL: revert "sycl: simplify bin_bcast_kernel (ggml/13383)" (llama/13…
qnixsynapse May 25, 2025
e2ba135
CANN: Add the basic supports of Flash Attention kernel (llama/13627)
shibizhao May 26, 2025
6370037
vulkan: mark IM2COL as supporting non-contig (llama/13783)
jeffbolznv May 26, 2025
02ed80e
sycl: Add more debug prints (llama/13640)
Rbiessy May 26, 2025
a26a34b
SYCL: Add non contiguous support in RMS_NORM and NORM kernels (llama/…
qnixsynapse May 26, 2025
45f2e0f
cuda : avoid cuGetErrorString (llama/13791)
ggerganov May 26, 2025
5eabeb7
ggml : allow CUDA graphs when using pipeline parallelism (llama/13814)
slaren May 27, 2025
4e61025
ggml-cpu: x86 feature detection is specific to x86 (llama/13811)
ckastner May 27, 2025
65206c2
ggml : riscv: add xtheadvector support (llama/13720)
xctan May 27, 2025
4575811
sync : ggml
ggerganov May 27, 2025
e2ac490
talk-llama : sync llama.cpp
ggerganov May 27, 2025
255eac6
sync : fix builds - musa, ruby
ggerganov May 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/talk-llama/llama-batch.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "llama-batch.h"

#include <cassert>
#include <cstring>
#include <algorithm>

Expand Down Expand Up @@ -281,9 +282,10 @@ llama_batch_allocr::llama_batch_allocr(struct llama_batch in_batch, llama_pos p0
batch = in_batch;
GGML_ASSERT(batch.n_tokens > 0);
if (!batch.pos) {
assert(p0 >= 0);
pos.resize(batch.n_tokens);
for (int32_t i = 0; i < batch.n_tokens; i++) {
pos[i] = i + p0;
pos[i] = p0 + i;
}
batch.pos = pos.data();
}
Expand Down
192 changes: 79 additions & 113 deletions examples/talk-llama/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ llama_context::llama_context(

const auto & hparams = model.hparams;

cparams.n_seq_max = std::max(1u, params.n_seq_max);
cparams.n_seq_max = std::max(1u, params.n_seq_max);
if (cparams.n_seq_max > LLAMA_MAX_PARALLEL_SEQUENCES) {
throw std::runtime_error("n_seq_max must be <= " + std::to_string(LLAMA_MAX_PARALLEL_SEQUENCES));
}

cparams.n_threads = params.n_threads;
cparams.n_threads_batch = params.n_threads_batch;
cparams.yarn_ext_factor = params.yarn_ext_factor;
Expand Down Expand Up @@ -93,6 +97,7 @@ llama_context::llama_context(
}

cparams.n_ubatch = std::min(cparams.n_batch, params.n_ubatch == 0 ? params.n_batch : params.n_ubatch);

cparams.op_offload = params.op_offload;

const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
Expand Down Expand Up @@ -176,8 +181,9 @@ llama_context::llama_context(
// init the memory module
if (!hparams.vocab_only) {
llama_memory_params params_mem = {
/*.type_k =*/ params.type_k,
/*.type_v =*/ params.type_v,
/*.type_k =*/ params.type_k,
/*.type_v =*/ params.type_v,
/*.swa_full =*/ params.swa_full,
};

memory.reset(model.create_memory(params_mem, cparams));
Expand Down Expand Up @@ -687,12 +693,18 @@ int llama_context::encode(llama_batch & inp_batch) {

GGML_ASSERT((!batch.token && batch.embd) || (batch.token && !batch.embd)); // NOLINT

// TODO: move the validation to the llama_batch_allocr
if (batch.token) {
for (int32_t i = 0; i < n_tokens; ++i) {
if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= model.vocab.n_tokens()) {
LLAMA_LOG_ERROR("%s: invalid token[%d] = %d\n", __func__, i, batch.token[i]);
return -1;
}

if (batch.seq_id && (batch.seq_id[i][0] < 0 || batch.seq_id[i][0] >= LLAMA_MAX_PARALLEL_SEQUENCES)) {
LLAMA_LOG_ERROR("%s: invalid seq_id[%d] = %d > %d\n", __func__, i, batch.seq_id[i][0], LLAMA_MAX_PARALLEL_SEQUENCES);
throw -1;
}
}
}

Expand Down Expand Up @@ -846,7 +858,7 @@ int llama_context::encode(llama_batch & inp_batch) {

int llama_context::decode(llama_batch & inp_batch) {
if (!memory) {
LLAMA_LOG_WARN("%s: cannot decode batches with this context (use llama_encode() instead)\n", __func__);
LLAMA_LOG_DEBUG("%s: cannot decode batches with this context (calling encode() instead)\n", __func__);
return encode(inp_batch);
}

Expand All @@ -855,11 +867,17 @@ int llama_context::decode(llama_batch & inp_batch) {
return -1;
}

if (!inp_batch.pos) {
if (inp_batch.seq_id) {
LLAMA_LOG_ERROR("%s: pos == NULL, but seq_id != NULL\n", __func__);
return -1;
}
}

llama_kv_cache * kv_self = static_cast<llama_kv_cache *>(memory.get());

// temporary allocate memory for the input batch if needed
// TODO: this is incorrect for multiple sequences because get_pos_max() is the maximum across all sequences
llama_batch_allocr batch_allocr(inp_batch, inp_batch.pos ? -1 : kv_self->get_pos_max() + 1);
llama_batch_allocr batch_allocr(inp_batch, inp_batch.pos ? -1 : kv_self->seq_pos_max(0) + 1);

const llama_batch & batch = batch_allocr.batch;

Expand All @@ -875,11 +893,17 @@ int llama_context::decode(llama_batch & inp_batch) {

GGML_ASSERT((!batch.token && batch.embd) || (batch.token && !batch.embd)); // NOLINT

// TODO: move the validation to the llama_batch_allocr
if (batch.token) {
for (int64_t i = 0; i < n_tokens_all; ++i) {
if (batch.token[i] < 0 || (uint32_t) batch.token[i] >= model.vocab.n_tokens()) {
LLAMA_LOG_ERROR("%s: invalid token[%" PRId64 "] = %d\n", __func__, i, batch.token[i]);
throw std::runtime_error("invalid token");
return -1;
}

if (batch.seq_id && (batch.seq_id[i][0] < 0 || batch.seq_id[i][0] >= LLAMA_MAX_PARALLEL_SEQUENCES)) {
LLAMA_LOG_ERROR("%s: invalid seq_id[%" PRId64 "] = %d >= %d\n", __func__, i, batch.seq_id[i][0], LLAMA_MAX_PARALLEL_SEQUENCES);
return -1;
}
}
}
Expand Down Expand Up @@ -947,8 +971,6 @@ int llama_context::decode(llama_batch & inp_batch) {

// find KV slot
if (!kv_self->find_slot(ubatch)) {
LLAMA_LOG_WARN("%s: failed to find KV cache slot for ubatch of size %d\n", __func__, ubatch.n_tokens);

return 1;
}

Expand Down Expand Up @@ -2093,6 +2115,7 @@ llama_context_params llama_context_default_params() {
/*.flash_attn =*/ false,
/*.no_perf =*/ true,
/*.op_offload =*/ true,
/*.swa_full =*/ true,
};

return result;
Expand Down Expand Up @@ -2287,65 +2310,51 @@ int32_t llama_apply_adapter_cvec(
return res ? 0 : -1;
}

//
// kv cache view
//

llama_kv_cache_view llama_kv_cache_view_init(const llama_context * ctx, int32_t n_seq_max) {
const auto * kv = ctx->get_kv_self();
if (kv == nullptr) {
LLAMA_LOG_WARN("%s: the context does not have a KV cache\n", __func__);
return {};
}

return llama_kv_cache_view_init(*kv, n_seq_max);
}

void llama_kv_cache_view_update(const llama_context * ctx, llama_kv_cache_view * view) {
const auto * kv = ctx->get_kv_self();
if (kv == nullptr) {
LLAMA_LOG_WARN("%s: the context does not have a KV cache\n", __func__);
return;
}

llama_kv_cache_view_update(view, kv);
}

//
// kv cache
//

// deprecated
int32_t llama_get_kv_cache_token_count(const llama_context * ctx) {
return llama_kv_self_n_tokens(ctx);
}

int32_t llama_kv_self_n_tokens(const llama_context * ctx) {
const auto * kv = ctx->get_kv_self();
if (!kv) {
return 0;
}

return kv->get_n_tokens();
}
int32_t res = 0;

// deprecated
int32_t llama_get_kv_cache_used_cells(const llama_context * ctx) {
return llama_kv_self_used_cells(ctx);
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
const llama_pos p0 = kv->seq_pos_min(s);
const llama_pos p1 = kv->seq_pos_max(s);

if (p0 >= 0) {
res += (p1 - p0) + 1;
}
}

return res;
}

// deprecated
// note: this is the same as above - will be removed anyway, so it's ok
int32_t llama_kv_self_used_cells(const llama_context * ctx) {
const auto * kv = ctx->get_kv_self();
if (!kv) {
return 0;
}

return kv->get_used_cells();
}
int32_t res = 0;

// deprecated
void llama_kv_cache_clear(llama_context * ctx) {
llama_kv_self_clear(ctx);
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
const llama_pos p0 = kv->seq_pos_min(s);
const llama_pos p1 = kv->seq_pos_max(s);

if (p0 >= 0) {
res += (p1 - p0) + 1;
}
}

return res;
}

void llama_kv_self_clear(llama_context * ctx) {
Expand All @@ -2357,15 +2366,6 @@ void llama_kv_self_clear(llama_context * ctx) {
kv->clear();
}

// deprecated
bool llama_kv_cache_seq_rm(
llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1) {
return llama_kv_self_seq_rm(ctx, seq_id, p0, p1);
}

bool llama_kv_self_seq_rm(
llama_context * ctx,
llama_seq_id seq_id,
Expand All @@ -2379,16 +2379,6 @@ bool llama_kv_self_seq_rm(
return kv->seq_rm(seq_id, p0, p1);
}

// deprecated
void llama_kv_cache_seq_cp(
llama_context * ctx,
llama_seq_id seq_id_src,
llama_seq_id seq_id_dst,
llama_pos p0,
llama_pos p1) {
llama_kv_self_seq_cp(ctx, seq_id_src, seq_id_dst, p0, p1);
}

void llama_kv_self_seq_cp(
llama_context * ctx,
llama_seq_id seq_id_src,
Expand All @@ -2403,13 +2393,6 @@ void llama_kv_self_seq_cp(
kv->seq_cp(seq_id_src, seq_id_dst, p0, p1);
}

// deprecated
void llama_kv_cache_seq_keep(
llama_context * ctx,
llama_seq_id seq_id) {
llama_kv_self_seq_keep(ctx, seq_id);
}

void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
auto * kv = ctx->get_kv_self();
if (!kv) {
Expand All @@ -2419,16 +2402,6 @@ void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
kv->seq_keep(seq_id);
}

// deprecated
void llama_kv_cache_seq_add(
llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1,
llama_pos delta) {
llama_kv_self_seq_add(ctx, seq_id, p0, p1, delta);
}

void llama_kv_self_seq_add(
llama_context * ctx,
llama_seq_id seq_id,
Expand All @@ -2443,16 +2416,6 @@ void llama_kv_self_seq_add(
kv->seq_add(seq_id, p0, p1, delta);
}

// deprecated
void llama_kv_cache_seq_div(
llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1,
int d) {
llama_kv_self_seq_div(ctx, seq_id, p0, p1, d);
}

void llama_kv_self_seq_div(
llama_context * ctx,
llama_seq_id seq_id,
Expand All @@ -2467,25 +2430,24 @@ void llama_kv_self_seq_div(
kv->seq_div(seq_id, p0, p1, d);
}

// deprecated
llama_pos llama_kv_cache_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
return llama_kv_self_seq_pos_max(ctx, seq_id);
llama_pos llama_kv_self_seq_pos_min(llama_context * ctx, llama_seq_id seq_id) {
const auto * kv = ctx->get_kv_self();
if (!kv) {
return -1;
}

return kv->seq_pos_min(seq_id);
}

llama_pos llama_kv_self_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
const auto * kv = ctx->get_kv_self();
if (!kv) {
return 0;
return -1;
}

return kv->seq_pos_max(seq_id);
}

// deprecated
void llama_kv_cache_defrag(llama_context * ctx) {
llama_kv_self_defrag(ctx);
}

void llama_kv_self_defrag(llama_context * ctx) {
auto * kv = ctx->get_kv_self();
if (!kv) {
Expand All @@ -2496,11 +2458,6 @@ void llama_kv_self_defrag(llama_context * ctx) {
kv->defrag_sched(-1.0f);
}

// deprecated
bool llama_kv_cache_can_shift(const llama_context * ctx) {
return llama_kv_self_can_shift(ctx);
}

bool llama_kv_self_can_shift(const llama_context * ctx) {
const auto * kv = ctx->get_kv_self();
if (!kv) {
Expand All @@ -2510,11 +2467,6 @@ bool llama_kv_self_can_shift(const llama_context * ctx) {
return kv->get_can_shift();
}

// deprecated
void llama_kv_cache_update(llama_context * ctx) {
llama_kv_self_update(ctx);
}

// llama state API

// deprecated
Expand Down Expand Up @@ -2637,7 +2589,21 @@ int32_t llama_encode(
int32_t llama_decode(
llama_context * ctx,
llama_batch batch) {
const int ret = ctx->decode(batch);
int ret = ctx->decode(batch);

// defrag and try again
// TODO: distinguish return code when we are sure that even after defrag there is no space available
if (ret == 1) {
llama_kv_self_defrag(ctx);
ret = ctx->decode(batch);

if (ret == 1) {
LLAMA_LOG_WARN("%s: failed to find KV cache slot for batch of size %d\n", __func__, batch.n_tokens);

return ret;
}
}

if (ret != 0) {
LLAMA_LOG_ERROR("%s: failed to decode, ret = %d\n", __func__, ret);
}
Expand Down
4 changes: 4 additions & 0 deletions examples/talk-llama/llama-cparams.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#include "llama-cparams.h"

size_t llama_max_parallel_sequences(void) {
return LLAMA_MAX_PARALLEL_SEQUENCES;
}
Loading
Loading