Skip to content

Commit 240692b

Browse files
authored
Removed static variables from utils.cpp
2 parents 214838e + 56e89f8 commit 240692b

5 files changed

Lines changed: 101 additions & 137 deletions

File tree

ggml/include/ggml-openvino.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#pragma once
22

33
#include "ggml-backend.h"
4-
#include "ggml.h"
54

6-
#include <array>
75
#include <cstring>
86

97
#ifdef __cplusplus
108
extern "C" {
119
#endif
1210

13-
#define GGML_OPENVINO_NAME "OPENVINO"
14-
#define GGML_OPENVINO_MAX_DEVICES 16
11+
#define GGML_OPENVINO_NAME "OPENVINO"
1512

1613
// backend API
1714
GGML_BACKEND_API ggml_backend_t ggml_backend_openvino_init(int device);
@@ -35,28 +32,6 @@ GGML_BACKEND_API int ggml_backend_openvino_get_device_count(void);
3532

3633
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void);
3734

38-
struct ggml_openvino_device_info {
39-
int device_count;
40-
41-
struct openvino_device_info {
42-
int cc; // compute capability
43-
int nsm; // number of streaming multiprocessors
44-
size_t smpb; // max. shared memory per block
45-
size_t smpbo; // max. shared memory per block (with opt-in)
46-
bool vmm; // virtual memory support
47-
size_t vmm_granularity; // granularity of virtual memory
48-
size_t total_vram;
49-
};
50-
51-
openvino_device_info devices[GGML_OPENVINO_MAX_DEVICES] = {};
52-
53-
std::array<float, GGML_OPENVINO_MAX_DEVICES> default_tensor_split = {};
54-
};
55-
5635
#ifdef __cplusplus
5736
}
5837
#endif
59-
60-
#ifdef __cplusplus
61-
const ggml_openvino_device_info & ggml_openvino_info();
62-
#endif

ggml/src/ggml-openvino/ggml-openvino-extra.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,16 @@ ggml_openvino_tensor_extra * ggml_openvino_create_tensor_extra(const ggml_tensor
167167
// Register an extra with the tensor's OpenVINO buffer context for proper lifetime management.
168168
// This sets tensor->extra and tracks the extra in the buffer context for cleanup.
169169
void ggml_openvino_buffer_register_extra(ggml_tensor * tensor, ggml_openvino_extra_base * extra);
170+
171+
// =====================================================
172+
// OpenVINO Backend Context and Interface
173+
// =====================================================
174+
struct ggml_backend_openvino_context {
175+
int device = 0;
176+
std::string name = "OpenVINO";
177+
std::string description = "OpenVINO Backend Context";
178+
179+
std::shared_ptr<void> runtime_context = nullptr;
180+
181+
ggml_backend_openvino_context() = default;
182+
};

ggml/src/ggml-openvino/ggml-openvino.cpp

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ggml.h"
1010

1111
#include <atomic>
12+
#include <cstdlib>
1213
#include <cstdint>
1314
#include <cstring>
1415
#include <memory>
@@ -593,36 +594,6 @@ bool ggml_backend_buft_is_openvino_host(ggml_backend_buffer_type_t buft) {
593594
return buft->iface.get_name == ggml_backend_openvino_host_buffer_type_get_name;
594595
}
595596

596-
// =====================================================
597-
// OpenVINO Backend Context and Interface
598-
// =====================================================
599-
600-
struct ggml_backend_openvino_context {
601-
int device; // the device ID currently in use
602-
std::string name; // context Name
603-
std::string description; // context description
604-
605-
// OpenVINO core components
606-
ov::Core core; // OpenVINO core interface
607-
std::shared_ptr<ov::CompiledModel> model; // compiled Model
608-
ov::InferRequest infer_request; // inference Request
609-
610-
// OpenVINO Multi-stream support
611-
static const int MAX_STREAMS = 8; // define the maximum number of flows
612-
std::vector<ov::InferRequest> streams; // used to support multi-stream reasoning
613-
int current_stream; // the currently active stream index
614-
615-
// state Management
616-
bool is_initialized; // initialize
617-
618-
ggml_backend_openvino_context() :
619-
device(0),
620-
name("OpenVINO"),
621-
description("OpenVINO Backend Context"),
622-
current_stream(0),
623-
is_initialized(false) {}
624-
};
625-
626597
static void ggml_backend_openvino_free(ggml_backend_t backend) {
627598
ggml_backend_openvino_context * ctx = (ggml_backend_openvino_context *) backend->context;
628599
delete ctx;
@@ -635,7 +606,7 @@ static const char * ggml_backend_openvino_get_name(ggml_backend_t backend) {
635606
}
636607

637608
static enum ggml_status ggml_backend_openvino_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
638-
return ov_graph_compute(cgraph);
609+
return ov_graph_compute(cgraph, backend);
639610
GGML_UNUSED(backend);
640611
}
641612

@@ -657,7 +628,7 @@ static const ggml_backend_i ggml_backend_openvino_interface = {
657628
};
658629

659630
int ggml_backend_openvino_get_device_count() {
660-
return ggml_openvino_info().device_count;
631+
return 1;
661632
}
662633

663634
static ggml_guid_t ggml_backend_openvino_guid(void) {
@@ -679,6 +650,17 @@ GGML_BACKEND_API ggml_backend_t ggml_backend_openvino_init(int device) {
679650
return nullptr;
680651
}
681652

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+
682664
ggml_backend_t openvino_backend = new ggml_backend{
683665
/* .guid = */ ggml_backend_openvino_guid(),
684666
/* .interface = */ ggml_backend_openvino_interface,
@@ -1059,7 +1041,7 @@ static const char * ggml_backend_openvino_reg_get_name(ggml_backend_reg_t reg) {
10591041

10601042
static size_t ggml_backend_openvino_reg_get_device_count(ggml_backend_reg_t reg) {
10611043
GGML_UNUSED(reg);
1062-
return ggml_openvino_info().device_count;
1044+
return (size_t) ggml_backend_openvino_get_device_count();
10631045
}
10641046

10651047
static ggml_backend_dev_t ggml_backend_openvino_reg_get_device(ggml_backend_reg_t reg, size_t index) {
@@ -1068,36 +1050,17 @@ static ggml_backend_dev_t ggml_backend_openvino_reg_get_device(ggml_backend_reg_
10681050
return ctx->devices[index];
10691051
}
10701052

1071-
static void * ggml_backend_openvino_get_proc_address(ggml_backend_reg_t reg, const char * name) {
1072-
GGML_UNUSED(reg);
1073-
GGML_UNUSED(name);
1074-
return nullptr;
1075-
}
1076-
10771053
static const struct ggml_backend_reg_i ggml_backend_openvino_reg_interface = {
10781054
/* .get_name = */ ggml_backend_openvino_reg_get_name,
10791055
/* .get_device_count = */ ggml_backend_openvino_reg_get_device_count,
10801056
/* .get_device = */ ggml_backend_openvino_reg_get_device,
1081-
/* .get_proc_address = */ ggml_backend_openvino_get_proc_address,
1057+
/* .get_proc_address = */ NULL,
10821058
};
10831059

1084-
static int get_openvino_device_count() {
1085-
return 1;
1086-
}
1087-
1088-
static ggml_openvino_device_info ggml_openvino_init() {
1060+
static void ggml_openvino_init() {
10891061
// Initialize device config singleton from env var
10901062
ggml_openvino_init_device_config();
10911063
GGML_LOG_INFO("OpenVINO: using device %s\n", ggml_openvino_get_device_name().c_str());
1092-
1093-
ggml_openvino_device_info info = {};
1094-
info.device_count = get_openvino_device_count();
1095-
return info;
1096-
}
1097-
1098-
const ggml_openvino_device_info & ggml_openvino_info() {
1099-
static ggml_openvino_device_info info = ggml_openvino_init();
1100-
return info;
11011064
}
11021065

11031066
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void) {
@@ -1108,9 +1071,11 @@ GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void) {
11081071
static std::mutex mutex;
11091072
std::lock_guard<std::mutex> lock(mutex);
11101073
if (!initialized) {
1074+
ggml_openvino_init();
1075+
11111076
ggml_backend_openvino_reg_context * ctx = new ggml_backend_openvino_reg_context;
11121077

1113-
for (int i = 0; i < ggml_openvino_info().device_count; i++) {
1078+
for (int i = 0; i < ggml_backend_openvino_get_device_count(); i++) {
11141079
ggml_backend_openvino_device_context * dev_ctx = new ggml_backend_openvino_device_context;
11151080
dev_ctx->device = i;
11161081
dev_ctx->name = GGML_OPENVINO_NAME + std::to_string(i);

0 commit comments

Comments
 (0)