99#include " ggml.h"
1010
1111#include < atomic>
12+ #include < cstdlib>
1213#include < cstdint>
1314#include < cstring>
1415#include < memory>
@@ -627,7 +628,7 @@ static const ggml_backend_i ggml_backend_openvino_interface = {
627628};
628629
629630int ggml_backend_openvino_get_device_count () {
630- return ggml_openvino_info (). device_count ;
631+ return 1 ;
631632}
632633
633634static ggml_guid_t ggml_backend_openvino_guid (void ) {
@@ -649,6 +650,17 @@ GGML_BACKEND_API ggml_backend_t ggml_backend_openvino_init(int device) {
649650 return nullptr ;
650651 }
651652
653+ ctx->runtime_context = std::make_shared<ov_runtime_context>();
654+ if (ctx->runtime_context == nullptr ) {
655+ GGML_LOG_ERROR (" %s: failed to allocate runtime context\n " , __func__);
656+ delete ctx;
657+ return nullptr ;
658+ }
659+
660+ std::shared_ptr<ov_runtime_context> r_ctx = std::static_pointer_cast<ov_runtime_context>(ctx->runtime_context );
661+ r_ctx->device = ggml_openvino_get_device_name ();
662+ r_ctx->stateful = getenv (" GGML_OPENVINO_STATEFUL_EXECUTION" ) && !ggml_openvino_is_npu ();
663+
652664 ggml_backend_t openvino_backend = new ggml_backend{
653665 /* .guid = */ ggml_backend_openvino_guid (),
654666 /* .interface = */ ggml_backend_openvino_interface,
@@ -1026,7 +1038,7 @@ static const char * ggml_backend_openvino_reg_get_name(ggml_backend_reg_t reg) {
10261038
10271039static size_t ggml_backend_openvino_reg_get_device_count (ggml_backend_reg_t reg) {
10281040 GGML_UNUSED (reg);
1029- return ggml_openvino_info (). device_count ;
1041+ return ( size_t ) ggml_backend_openvino_get_device_count () ;
10301042}
10311043
10321044static ggml_backend_dev_t ggml_backend_openvino_reg_get_device (ggml_backend_reg_t reg, size_t index) {
@@ -1035,36 +1047,17 @@ static ggml_backend_dev_t ggml_backend_openvino_reg_get_device(ggml_backend_reg_
10351047 return ctx->devices [index];
10361048}
10371049
1038- static void * ggml_backend_openvino_get_proc_address (ggml_backend_reg_t reg, const char * name) {
1039- GGML_UNUSED (reg);
1040- GGML_UNUSED (name);
1041- return nullptr ;
1042- }
1043-
10441050static const struct ggml_backend_reg_i ggml_backend_openvino_reg_interface = {
10451051 /* .get_name = */ ggml_backend_openvino_reg_get_name,
10461052 /* .get_device_count = */ ggml_backend_openvino_reg_get_device_count,
10471053 /* .get_device = */ ggml_backend_openvino_reg_get_device,
1048- /* .get_proc_address = */ ggml_backend_openvino_get_proc_address ,
1054+ /* .get_proc_address = */ NULL ,
10491055};
10501056
1051- static int get_openvino_device_count () {
1052- return 1 ;
1053- }
1054-
1055- static ggml_openvino_device_info ggml_openvino_init () {
1057+ static void ggml_openvino_init () {
10561058 // Initialize device config singleton from env var
10571059 ggml_openvino_init_device_config ();
10581060 GGML_LOG_INFO (" OpenVINO: using device %s\n " , ggml_openvino_get_device_name ().c_str ());
1059-
1060- ggml_openvino_device_info info = {};
1061- info.device_count = get_openvino_device_count ();
1062- return info;
1063- }
1064-
1065- const ggml_openvino_device_info & ggml_openvino_info () {
1066- static ggml_openvino_device_info info = ggml_openvino_init ();
1067- return info;
10681061}
10691062
10701063GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg (void ) {
@@ -1075,9 +1068,11 @@ GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void) {
10751068 static std::mutex mutex;
10761069 std::lock_guard<std::mutex> lock (mutex);
10771070 if (!initialized) {
1071+ ggml_openvino_init ();
1072+
10781073 ggml_backend_openvino_reg_context * ctx = new ggml_backend_openvino_reg_context;
10791074
1080- for (int i = 0 ; i < ggml_openvino_info (). device_count ; i++) {
1075+ for (int i = 0 ; i < ggml_backend_openvino_get_device_count () ; i++) {
10811076 ggml_backend_openvino_device_context * dev_ctx = new ggml_backend_openvino_device_context;
10821077 dev_ctx->device = i;
10831078 dev_ctx->name = GGML_OPENVINO_NAME + std::to_string (i);
0 commit comments