From 31bd816426d42a27896d8ec66689852707e24c4f Mon Sep 17 00:00:00 2001 From: yumengbo Date: Sat, 23 Nov 2024 06:03:08 +0800 Subject: [PATCH] Add GGML_OV_FRONTEND option. Add readme. --- .../ggml-openvino/ggml-ov-frontend-utils.cpp | 108 ------------------ .../ggml-openvino/ggml-ov-frontend-utils.h | 6 - 2 files changed, 114 deletions(-) delete mode 100644 ggml/src/ggml-openvino/ggml-ov-frontend-utils.cpp delete mode 100644 ggml/src/ggml-openvino/ggml-ov-frontend-utils.h diff --git a/ggml/src/ggml-openvino/ggml-ov-frontend-utils.cpp b/ggml/src/ggml-openvino/ggml-ov-frontend-utils.cpp deleted file mode 100644 index 10107cbfd0..0000000000 --- a/ggml/src/ggml-openvino/ggml-ov-frontend-utils.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "ggml-ov-frontend-utils.h" -#include "ggml-backend-impl.h" -#include -#include - -using ov::frontend::tensorflow::ggml::GgmlOvGraphIterator; - -std::shared_ptr get_ggml_graph_iterator(struct ggml_cgraph * cgraph) { - return std::make_shared(cgraph); -} - -std::vector get_ggml_graph_input_tensors(std::shared_ptr ggml_graph_iterator) { - std::vector input_tensors; - auto input_names = ggml_graph_iterator->get_input_names(); - ggml_graph_iterator->reset(); - for (; !ggml_graph_iterator->is_end(); ggml_graph_iterator->next()) { - auto decoder = std::dynamic_pointer_cast(ggml_graph_iterator->get_decoder()); - for (size_t inp = 0; inp < decoder->get_input_size(); ++inp) { - if (std::find(input_names.begin(), input_names.end(), decoder->get_input_name(inp)) != input_names.end()) { - auto input_data = decoder->get_input_ggml_tensor(inp)->data; - ov::Tensor input_tensor = ov::Tensor(decoder->get_input_type(inp), decoder->get_input_shape(inp).to_shape(), input_data); - input_tensors.push_back(input_tensor); - } - } - } - return input_tensors; -} - -static ov::frontend::FrontEnd::Ptr get_ggml_frontend() { - ov::frontend::FrontEnd::Ptr front_end = nullptr; - auto fem = ov::frontend::FrontEndManager(); - // std::string fe_so_path = "/home/yumeng/Code/test/openvino/bin/intel64/Release/libopenvino_ggml_frontend.so"; - std::string fe_so_path = "/home/yumeng/Code/ov-ggml-frontend/openvino/bin/intel64/Release/libopenvino_ggml_frontend.so"; - fem.register_front_end("ggml", fe_so_path); - front_end = fem.load_by_framework("ggml"); - return front_end; -} - -enum ggml_status openvino_frontend_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph) { - ov::Core core; - auto devices = core.get_available_devices(); - #ifdef GGML_OPENVINO_DEBUG - GGML_LOG_INFO("Device numbers: %d\n", devices.size()); - #endif - // Get GGML Frontend - auto front_end = get_ggml_frontend(); - if (!front_end) { - GGML_LOG_ERROR("GGML FrontEnd is not initialized \n"); - return GGML_STATUS_FAILED; - } else { - #ifdef GGML_OPENVINO_DEBUG - GGML_LOG_INFO("GGML FrontEnd is initialized \n"); - #endif - } - - auto ggml_graph_iterator = get_ggml_graph_iterator(cgraph); - std::shared_ptr graph_iterator = ggml_graph_iterator; - - // Load GraphIterator -> InputModel - ov::frontend::InputModel::Ptr input_model = front_end->load(graph_iterator); - if (!input_model) { - GGML_LOG_ERROR("Input Model is not loaded \n"); - return GGML_STATUS_FAILED; - } else { - #ifdef GGML_OPENVINO_DEBUG - GGML_LOG_INFO("Input Model loaded \n"); - #endif - } - - // TODO: Convert InputModel -> ov::Model - std::shared_ptr model = front_end->convert(input_model); - if (!model) { - GGML_LOG_ERROR("Model is not converted \n"); - } else { - #ifdef GGML_OPENVINO_DEBUG - GGML_LOG_INFO("Model converted \n"); - #endif - } - - - // Loading a model to the device - ov::CompiledModel compiled_model = core.compile_model(model); - - // Create infer request - ov::InferRequest infer_request = compiled_model.create_infer_request(); - - // Get input tensor - auto input_tensor = get_ggml_graph_input_tensors(ggml_graph_iterator); - - // Set input tensor - for (size_t i = 0; i < input_tensor.size(); i++) { - infer_request.set_input_tensor(i, input_tensor[i]); - } - - infer_request.infer(); - - ov::Tensor output_tensor = infer_request.get_output_tensor(); - // Put data in output tensor to the last node -> data in cgraph - // Get output type - ggml_tensor* dst = cgraph->nodes[cgraph->n_nodes - 1]; - std::memcpy(dst->data, output_tensor.data(), output_tensor.get_byte_size()); - #ifdef GGML_OPENVINO_DEBUG - GGML_LOG_INFO("%f\n", *output_tensor.data()); - #endif - - return GGML_STATUS_SUCCESS; - GGML_UNUSED(backend); -} diff --git a/ggml/src/ggml-openvino/ggml-ov-frontend-utils.h b/ggml/src/ggml-openvino/ggml-ov-frontend-utils.h deleted file mode 100644 index 15dd46ed4e..0000000000 --- a/ggml/src/ggml-openvino/ggml-ov-frontend-utils.h +++ /dev/null @@ -1,6 +0,0 @@ -#include "ggml-graph-iterator.h" -#include "ggml-backend-impl.h" - -std::shared_ptr get_ggml_graph_iterator(struct ggml_cgraph * cgraph); - -enum ggml_status openvino_frontend_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);