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..1c4b69cdae509 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,23 @@ 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 (runtime_initialized != true) + return; + 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..dc05b5445d6aa 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; }