#pragma once
#include "ggml.h"
#include "openvino/runtime/core.hpp"
#define CL_TARGET_OPENCL_VERSION 300
#include <CL/cl.h>
#include <cstdlib>
#include <map>
#include <memory>
#include <openvino/core/node.hpp>
#include <openvino/runtime/remote_context.hpp>
#include <openvino/runtime/tensor.hpp>
#include <optional>
#include <string>
enum class ExtraQuantType { F16, Q4_0_C, Q8_1_C, Q4_0_128, Q4_0_64, Q8_0_C, Q8_0_32, Q4_1_64 };
ov::Core & ov_singleton_core();
std::optional<ov::RemoteContext> ggml_openvino_get_remote_context(const std::string & device);
const ov::AnyMap & ggml_openvino_get_compile_config();
cl_command_queue ggml_openvino_get_cl_queue(const std::string & device);
typedef cl_int(CL_API_CALL * clEnqueueMemFillINTEL_fn)(cl_command_queue queue,
void * dst_ptr,
const void * pattern,
size_t pattern_size,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event * event_wait_list,
cl_event * event);
typedef cl_int(CL_API_CALL * clEnqueueMemcpyINTEL_fn)(cl_command_queue queue,
cl_bool blocking,
void * dst_ptr,
const void * src_ptr,
size_t size,
cl_uint num_events_in_wait_list,
const cl_event * event_wait_list,
cl_event * event);
clEnqueueMemFillINTEL_fn ggml_openvino_get_clEnqueueMemFillINTEL();
clEnqueueMemcpyINTEL_fn ggml_openvino_get_clEnqueueMemcpyINTEL();
struct ggml_openvino_gpu_context {
ov::RemoteContext remote_context;
cl_command_queue cl_queue = nullptr;
size_t max_alloc_size = SIZE_MAX;
};
struct ggml_openvino_device_config {
std::string device_name = "CPU";
std::vector<std::string> available_devices;
std::vector<std::string> selected_devices;
bool is_npu = false;
bool initialized = false;
std::map<std::string, ggml_openvino_gpu_context> gpu_contexts;
ov::AnyMap compile_config;
std::unordered_map<std::string, std::string> environment_variables;
clEnqueueMemFillINTEL_fn cl_mem_fill_fn = nullptr;
clEnqueueMemcpyINTEL_fn cl_mem_cpy_fn = nullptr;
void init();
~ggml_openvino_device_config();
};
ggml_openvino_device_config & ggml_openvino_get_device_config();
void ggml_openvino_init_device_config();
const std::string & ggml_openvino_get_device_name();
const std::vector<std::string> & ggml_openvino_get_available_devices();
bool ggml_openvino_is_selected_device(const std::string & device);
std::string ggml_openvino_get_device_description(const std::string & device_name);
const char * ggml_openvino_getenv_str(const char * var, const char * default_value = nullptr);
int ggml_openvino_getenv_int(const char * var, int default_value = 0);
void ggml_openvino_apply_config_overrides(ov::AnyMap & config, const char * spec, const char * device);
std::string ggml_openvino_config_overrides_in_use();
bool ggml_openvino_reduce_compile_mem_enabled();
bool ggml_openvino_release_weights_enabled();
bool ggml_openvino_is_npu();
bool ggml_openvino_is_static();
bool ggml_openvino_kv_slice_enabled();
bool ggml_openvino_npu_l0_host_tensors_enabled();
bool ggml_openvino_npu_fast_mask_enabled();
bool ggml_openvino_npu_hfa_prefill_enabled();
bool ggml_openvino_npu_decode_unfold_enabled();
bool ggml_openvino_npu_parallel_compile_enabled();
int ggml_openvino_npu_decode_bucket();
int ggml_openvino_static_kv_window(int rows, int ctx);
bool ggml_openvino_npu_kv_on_demand_enabled();
bool ggml_openvino_npu_kv_head_major_enabled();
bool ggml_openvino_npu_raw_weight_cache_enabled();
bool ggml_openvino_npu_weightless_bank_enabled();
void ggml_openvino_kv_storage_register(void * data, size_t row_bytes, size_t rows);
void ggml_openvino_kv_storage_unregister(const void * base, size_t size);
void * ggml_openvino_kv_storage(const void * ptr, size_t bytes);
bool ggml_openvino_kv_storage_reserve(const void * data, size_t rows, size_t heads = 0);
size_t ggml_openvino_kv_storage_capacity(const void * data);
enum ggml_openvino_kv_copy_op { GGML_OPENVINO_KV_READ, GGML_OPENVINO_KV_WRITE, GGML_OPENVINO_KV_FILL };
bool ggml_openvino_kv_storage_copy(const void * ptr, void * host, size_t size, ggml_openvino_kv_copy_op op);
bool ggml_openvino_kv_storage_write_rows(const void * data, const void * src, size_t src_stride, size_t src_col,
size_t dst_row, size_t n);
void ggml_openvino_kv_storage_clear(const void * base, size_t size, uint8_t value);
bool ggml_openvino_is_gpu();
bool ggml_openvino_shape_from_mask_enabled();
bool ggml_openvino_manual_gqa_enabled();
bool ggml_openvino_gdn_prescale_enabled();
float ggml_openvino_gdn_q_scale();
bool ggml_openvino_gdn_native_state_enabled();
bool ggml_openvino_npu_is_4000();
bool ggml_openvino_gdn_decompose_enabled();
int ggml_openvino_gdn_decompose_chunk();
size_t ggml_openvino_max_alloc_size(const std::string & device);
void ggml_openvino_register_weight_buffer(void * data, size_t size);
void ggml_openvino_unregister_weight_buffer(const void * data);
void ggml_openvino_release_weight_buffers();
bool ggml_openvino_weight_buffers_released();
std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor * tensor, bool no_requant = false);
bool ggml_openvino_use_exact_zero_point(const ggml_tensor * tensor);
struct ggml_openvino_extra_base {
enum class Type { WEIGHT, QUANTIZED_WEIGHT, TENSOR };
Type type;
virtual ~ggml_openvino_extra_base() = default;
protected:
explicit ggml_openvino_extra_base(Type t) : type(t) {}
};
struct ggml_openvino_weight_extra : public ggml_openvino_extra_base {
ov::Tensor weights; std::shared_ptr<ov::Node> weight_node;
ggml_openvino_weight_extra(ov::Tensor w, std::shared_ptr<ov::Node> n) :
ggml_openvino_extra_base(Type::WEIGHT),
weights(std::move(w)),
weight_node(std::move(n)) {}
};
struct ggml_openvino_quantized_weight_extra : public ggml_openvino_extra_base {
ov::Tensor weights; ov::Tensor scales; ov::Tensor zp; std::shared_ptr<ov::Node> weight_node;
ggml_openvino_quantized_weight_extra(ov::Tensor w, ov::Tensor s, ov::Tensor z, std::shared_ptr<ov::Node> n) :
ggml_openvino_extra_base(Type::QUANTIZED_WEIGHT),
weights(std::move(w)),
scales(std::move(s)),
zp(std::move(z)),
weight_node(std::move(n)) {}
};
struct ggml_openvino_tensor_extra : public ggml_openvino_extra_base {
std::shared_ptr<ov::Tensor> tensor; const void * data;
ggml_openvino_tensor_extra(std::shared_ptr<ov::Tensor> t, const void * data) :
ggml_openvino_extra_base(Type::TENSOR),
tensor(std::move(t)),
data(data) {}
};
struct ggml_openvino_extracted_layout {
size_t total_size = 0; size_t weights_offset = 0; size_t weights_size = 0; size_t scales_offset = 0; size_t scales_size = 0; size_t zp_offset = 0; size_t zp_size = 0; bool is_u4; int64_t weights_per_block; bool is_symmetric;
bool is_requant = false; std::optional<ExtraQuantType> requant_type; };
ggml_openvino_extracted_layout ggml_openvino_get_extracted_layout(const ggml_tensor * tensor, bool use_bias = false);
ggml_openvino_tensor_extra * ggml_openvino_create_tensor_extra(const ggml_tensor * tensor,
bool is_remote,
const std::string & device);
bool ggml_openvino_buffer_is_remote(const ggml_tensor * tensor);
void ggml_openvino_buffer_register_extra(ggml_tensor * tensor, ggml_openvino_extra_base * extra);
struct ggml_backend_openvino_context {
int device = 0;
std::string name = "OpenVINO";
std::string description = "OpenVINO Backend Context";
std::shared_ptr<void> runtime_context = nullptr;
ggml_backend_openvino_context() = default;
};