From 513243b9342f2a8a90aa3d997f368716f5123f9a Mon Sep 17 00:00:00 2001 From: Anoop Kapoor Date: Fri, 17 Oct 2025 21:26:07 -0700 Subject: [PATCH 1/2] @FIR-1032 - LLama.cpp: wrong model cause crash the llama-cli process --- ggml/include/ggml-tsavorite.h | 2 ++ ggml/src/ggml-tsavorite/ggml-tsavorite.cpp | 23 +++++++++++++++++++++- tools/main/main.cpp | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ggml/include/ggml-tsavorite.h b/ggml/include/ggml-tsavorite.h index 79a1ff155ec4c..90436b2d1920d 100644 --- a/ggml/include/ggml-tsavorite.h +++ b/ggml/include/ggml-tsavorite.h @@ -214,6 +214,8 @@ extern void ggml_tsi_log_tensor_data(tensor_log log_data); // GGML supports tensors with a maximum rank of 4 #define MEM_REF_DESCRIPTOR_RANK 4 #define TSI_TVU_MEM_ALIGN 128 +void +ggml_tsi_finalize(); // // backend API diff --git a/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp b/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp index 0de76aeb3f450..9ea9639e6b284 100644 --- a/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp +++ b/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp @@ -32,10 +32,10 @@ namespace tsirt = ::tsi::runtime; typedef struct _txe_device_t *txe_device_s; typedef struct _txe_compute_pipeline_state_t *txe_compute_pipeline_state_s; FILE *tsi_op_log_file; +bool runtime_initialized = false; uint64_t num_of_op; // Centralized TSI runtime initialization - called once globally static void ensure_tsi_runtime_initialized() { - static bool runtime_initialized = false; if (!runtime_initialized) { std::string mainProfilerName = "OPU "; tsirt::utils::TSIProfiler::initialize(); @@ -693,6 +693,8 @@ static struct ggml_backend_tsavorite_context *ggml_tsavorite_init(ggml_backend_d static void ggml_tsavorite_free(struct ggml_backend_tsavorite_context *ctx) { GGML_TSAVORITE_LOG_INFO("Start %s\n", __func__); + if (!ctx) + return; for (int i = 0; i < GGML_TSAVORITE_KERNEL_TYPE_COUNT; ++i) { if (ctx->kernels[i].pipeline) { @@ -724,6 +726,25 @@ static void ggml_tsavorite_free(struct ggml_backend_tsavorite_context *ctx) { std::cout << tsirt::utils::TSIProfiler::getFormattedResults( /*truncateFuncNames*/ true) << std::endl; + sleep(2); +} + +void +ggml_tsi_finalize() { + //if (tsi_op_log_file == NULL) + if (runtime_initialized != true) + return; + sleep(2); + tsi_finalize(); + GGML_TSAVORITE_LOG_INFO("Start %s\n", __func__); + tsirt::utils::TSIProfiler::finalize(); + std::cout << "\nOPU Profiling Results:" << std::endl; + std::cout << tsirt::utils::TSIProfiler::getFormattedResults( + /*truncateFuncNames*/ true) + << std::endl; + sleep(2); + GGML_TSAVORITE_LOG_INFO("End %s\n", __func__); + return; } #if 0 diff --git a/tools/main/main.cpp b/tools/main/main.cpp index dc90b08a05296..763f542a8dfbb 100644 --- a/tools/main/main.cpp +++ b/tools/main/main.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) #include @@ -153,6 +154,7 @@ int main(int argc, char ** argv) { if (model == NULL) { LOG_ERR("%s: error: unable to load model\n", __func__); + ggml_tsi_finalize(); return 1; } From 06fb3dfd63be037dfe782ae91120e349f4c583f8 Mon Sep 17 00:00:00 2001 From: Anoop Kapoor Date: Fri, 17 Oct 2025 22:08:39 -0700 Subject: [PATCH 2/2] Address Ashish's Comment --- ggml/src/ggml-tsavorite/ggml-tsavorite.cpp | 2 -- tools/main/main.cpp | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp b/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp index 9ea9639e6b284..1c4b69cdae509 100644 --- a/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp +++ b/ggml/src/ggml-tsavorite/ggml-tsavorite.cpp @@ -731,10 +731,8 @@ static void ggml_tsavorite_free(struct ggml_backend_tsavorite_context *ctx) { void ggml_tsi_finalize() { - //if (tsi_op_log_file == NULL) if (runtime_initialized != true) return; - sleep(2); tsi_finalize(); GGML_TSAVORITE_LOG_INFO("Start %s\n", __func__); tsirt::utils::TSIProfiler::finalize(); diff --git a/tools/main/main.cpp b/tools/main/main.cpp index 763f542a8dfbb..dc05b5445d6aa 100644 --- a/tools/main/main.cpp +++ b/tools/main/main.cpp @@ -154,7 +154,7 @@ int main(int argc, char ** argv) { if (model == NULL) { LOG_ERR("%s: error: unable to load model\n", __func__); - ggml_tsi_finalize(); + ggml_tsi_finalize(); return 1; }