Skip to content

Commit e468802

Browse files
authored
Merge pull request #39 from tc-mb/feat_ios_0721
feat ios: add clean kv cache
2 parents 93f4086 + ea69103 commit e468802

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

tools/mtmd/mtmd-ios.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void mtmd_ios_free(mtmd_ios_context* ctx) {
149149
}
150150

151151
int mtmd_ios_prefill_image(mtmd_ios_context* ctx, const std::string& image_path) {
152+
152153
if (!ctx || image_path.empty()) {
153154
return -1;
154155
}
@@ -292,3 +293,18 @@ mtmd_ios_token mtmd_ios_loop(mtmd_ios_context* ctx) {
292293
const char* mtmd_ios_get_last_error(mtmd_ios_context* ctx) {
293294
return ctx ? ctx->last_error.c_str() : nullptr;
294295
}
296+
297+
bool mtmd_ios_clean_kv_cache(mtmd_ios_context* ctx) {
298+
if (!ctx) {
299+
return false;
300+
}
301+
302+
// 清理 kv-cache 并重置序列位置
303+
ctx->n_past = 0;
304+
llama_kv_self_seq_rm(ctx->lctx, 0, 0, -1);
305+
306+
// 清理batch状态
307+
common_batch_clear(ctx->batch);
308+
309+
return true;
310+
}

tools/mtmd/mtmd-ios.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88
#include "mtmd-helper.h"
99
#include "mtmd.h"
1010

11-
#ifdef __cplusplus
12-
extern "C" {
13-
#endif
14-
1511
// Context structure
1612
typedef struct mtmd_ios_context mtmd_ios_context;
1713

18-
// Parameters structure
14+
// Parameters structure (C++ only)
1915
typedef struct mtmd_ios_params {
2016
std::string model_path;
2117
std::string mmproj_path;
@@ -28,6 +24,16 @@ typedef struct mtmd_ios_params {
2824
bool warmup;
2925
} mtmd_ios_params;
3026

27+
// Loop return value structure (C++ only)
28+
typedef struct {
29+
char * token;
30+
bool is_end;
31+
} mtmd_ios_token;
32+
33+
#ifdef __cplusplus
34+
extern "C" {
35+
#endif
36+
3137
// Initialize, returns 0 on success, -1 on failure
3238
// Parameters:
3339
// params: parameters
@@ -54,12 +60,6 @@ int mtmd_ios_prefill_image(mtmd_ios_context * ctx, const std::string & image_pat
5460
// role: role
5561
int mtmd_ios_prefill_text(mtmd_ios_context * ctx, const std::string & text, const std::string & role);
5662

57-
// Loop return value structure
58-
typedef struct {
59-
char * token;
60-
bool is_end;
61-
} mtmd_ios_token;
62-
6363
// Loop, returns 0 on success, -1 on failure
6464
// Parameters:
6565
// ctx: context
@@ -75,6 +75,11 @@ const char * mtmd_ios_get_last_error(mtmd_ios_context * ctx);
7575
// str: string
7676
void mtmd_ios_string_free(char * str);
7777

78+
// Clean kv-cache
79+
// Parameters:
80+
// ctx: context
81+
bool mtmd_ios_clean_kv_cache(mtmd_ios_context * ctx);
82+
7883
#ifdef __cplusplus
7984
}
8085
#endif

0 commit comments

Comments
 (0)