diff --git a/tools/mtmd/mtmd-ios.cpp b/tools/mtmd/mtmd-ios.cpp index 6c75e6b27bd..bf8de2ac1cb 100644 --- a/tools/mtmd/mtmd-ios.cpp +++ b/tools/mtmd/mtmd-ios.cpp @@ -149,6 +149,7 @@ void mtmd_ios_free(mtmd_ios_context* ctx) { } int mtmd_ios_prefill_image(mtmd_ios_context* ctx, const std::string& image_path) { + if (!ctx || image_path.empty()) { return -1; } @@ -292,3 +293,18 @@ mtmd_ios_token mtmd_ios_loop(mtmd_ios_context* ctx) { const char* mtmd_ios_get_last_error(mtmd_ios_context* ctx) { return ctx ? ctx->last_error.c_str() : nullptr; } + +bool mtmd_ios_clean_kv_cache(mtmd_ios_context* ctx) { + if (!ctx) { + return false; + } + + // 清理 kv-cache 并重置序列位置 + ctx->n_past = 0; + llama_kv_self_seq_rm(ctx->lctx, 0, 0, -1); + + // 清理batch状态 + common_batch_clear(ctx->batch); + + return true; +} \ No newline at end of file diff --git a/tools/mtmd/mtmd-ios.h b/tools/mtmd/mtmd-ios.h index b71453b4b9e..92a1f0e47c6 100644 --- a/tools/mtmd/mtmd-ios.h +++ b/tools/mtmd/mtmd-ios.h @@ -8,14 +8,10 @@ #include "mtmd-helper.h" #include "mtmd.h" -#ifdef __cplusplus -extern "C" { -#endif - // Context structure typedef struct mtmd_ios_context mtmd_ios_context; -// Parameters structure +// Parameters structure (C++ only) typedef struct mtmd_ios_params { std::string model_path; std::string mmproj_path; @@ -28,6 +24,16 @@ typedef struct mtmd_ios_params { bool warmup; } mtmd_ios_params; +// Loop return value structure (C++ only) +typedef struct { + char * token; + bool is_end; +} mtmd_ios_token; + +#ifdef __cplusplus +extern "C" { +#endif + // Initialize, returns 0 on success, -1 on failure // Parameters: // params: parameters @@ -54,12 +60,6 @@ int mtmd_ios_prefill_image(mtmd_ios_context * ctx, const std::string & image_pat // role: role int mtmd_ios_prefill_text(mtmd_ios_context * ctx, const std::string & text, const std::string & role); -// Loop return value structure -typedef struct { - char * token; - bool is_end; -} mtmd_ios_token; - // Loop, returns 0 on success, -1 on failure // Parameters: // ctx: context @@ -75,6 +75,11 @@ const char * mtmd_ios_get_last_error(mtmd_ios_context * ctx); // str: string void mtmd_ios_string_free(char * str); +// Clean kv-cache +// Parameters: +// ctx: context +bool mtmd_ios_clean_kv_cache(mtmd_ios_context * ctx); + #ifdef __cplusplus } #endif