Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-openvino/ggml-openvino-extra.h # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-openvino / ggml-openvino-extra.h` 235 lines ยท 10929 bytes Raw Blame History 1 #pragma once 2 3 #include "ggml.h" 4 #include "openvino/runtime/core.hpp" 5 6 #define CL_TARGET_OPENCL_VERSION 300 7 #include 8 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 17 // ExtraQuantType enum - defines requantization target formats 18 // Q4_1_64: u4, group 64, *true* asymmetric (per-group scale and zero point). Note that 19 // Q4_0_128/Q4_0_C are symmetric despite taking the unsigned branch of quantize_q4_0 -- that branch 20 // pins zp to 8 with d = max/-8, which is algebraically symmetric. 21 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 }; 22 23 ov::Core & ov_singleton_core(); 24 25 // Get the remote context for the current device (returns empty optional for CPU) 26 std::optional ggml_openvino_get_remote_context(); 27 28 // Get the compile config for the current device 29 const ov::AnyMap & ggml_openvino_get_compile_config(); 30 31 // Get the OpenCL command queue for GPU operations (returns nullptr for CPU/NPU) 32 cl_command_queue ggml_openvino_get_cl_queue(); 33 34 // Intel USM extension function type 35 typedef cl_int(CL_API_CALL * clEnqueueMemFillINTEL_fn)(cl_command_queue queue, 36 void * dst_ptr, 37 const void * pattern, 38 size_t pattern_size, 39 size_t size, 40 cl_uint num_events_in_wait_list, 41 const cl_event * event_wait_list, 42 cl_event * event); 43 44 typedef cl_int(CL_API_CALL * clEnqueueMemcpyINTEL_fn)(cl_command_queue queue, 45 cl_bool blocking, 46 void * dst_ptr, 47 const void * src_ptr, 48 size_t size, 49 cl_uint num_events_in_wait_list, 50 const cl_event * event_wait_list, 51 cl_event * event); 52 53 // Get the clEnqueueMemFillINTEL function pointer (returns nullptr if not available) 54 clEnqueueMemFillINTEL_fn ggml_openvino_get_clEnqueueMemFillINTEL(); 55 56 // Get the clEnqueueMemcpyINTEL function pointer (returns nullptr if not available) 57 clEnqueueMemcpyINTEL_fn ggml_openvino_get_clEnqueueMemcpyINTEL(); 58 59 // ===================================================== 60 // Global Device Configuration (singleton) 61 // ===================================================== 62 // Initialized once during backend init from GGML_OPENVINO_DEVICE env var 63 64 struct ggml_openvino_device_config { 65 std::string device_name = "CPU"; 66 std::vector available_devices; 67 bool is_npu = false; 68 bool initialized = false; 69 std::optional remote_context; 70 size_t max_alloc_size = SIZE_MAX; 71 ov::AnyMap compile_config; 72 std::unordered_map environment_variables; 73 cl_command_queue cl_queue = nullptr; 74 clEnqueueMemFillINTEL_fn cl_mem_fill_fn = nullptr; 75 clEnqueueMemcpyINTEL_fn cl_mem_cpy_fn = nullptr; 76 77 void init(); 78 ~ggml_openvino_device_config(); 79 }; 80 81 // Get the global device config singleton 82 ggml_openvino_device_config & ggml_openvino_get_device_config(); 83 84 // Initialize device config (call during backend init) 85 void ggml_openvino_init_device_config(); 86 87 // Get the device name 88 const std::string & ggml_openvino_get_device_name(); 89 90 // Get all available physical OpenVINO devices 91 std::vector ggml_openvino_get_available_devices(); 92 93 // Human-readable device name, e.g. "Intel(R) AI Boost (NPU 4000)"; the device id if unavailable 94 std::string ggml_openvino_get_device_description(const std::string & device_name); 95 96 // Environment variable accessors. All GGML_OPENVINO_* env vars are read once 97 // during backend init and cached on the device config; consumers must go 98 // through these helpers (never call ::getenv directly) so behavior stays 99 // consistent and centralized. 100 // 101 // Use ggml_openvino_getenv_str() for string / path values 102 // (e.g. GGML_OPENVINO_DEVICE, GGML_OPENVINO_CACHE_DIR). The optional 103 // default_value is returned when the var is unset or empty. 104 // 105 // Use ggml_openvino_getenv_int() for boolean toggles and integer settings. 106 // It returns std::atoi(value) when set, otherwise default_value. For 107 // boolean use, `if (ggml_openvino_getenv_int(name))` is true iff the value 108 // is a non-zero integer (so "0" disables, "1" enables). 109 const char * ggml_openvino_getenv_str(const char * var, const char * default_value = nullptr); 110 int ggml_openvino_getenv_int(const char * var, int default_value = 0); 111 112 // Memory optimization toggles. GGML_OPENVINO_MEMORY_OPTIMIZE is an umbrella 113 // switch; the fine-grained env vars still override it when explicitly set. 114 bool ggml_openvino_reduce_compile_mem_enabled(); 115 bool ggml_openvino_release_weights_enabled(); 116 117 // Check if running on NPU 118 bool ggml_openvino_is_npu(); 119 120 // Check if running on a GPU (GPU, GPU.0, GPU.1, ...) 121 bool ggml_openvino_is_gpu(); 122 123 // Largest single memory object the device can allocate, SIZE_MAX when there is no known limit 124 size_t ggml_openvino_max_alloc_size(); 125 126 // Host weight-buffer release (GGML_OPENVINO_RELEASE_WEIGHTS, GPU only). 127 // register: record a host weight buffer (idempotent per data pointer). 128 // release: madvise(MADV_DONTNEED) all registered buffers, dropping their RSS. 129 // released: true once release has run (used to fail-fast on post-release recompile). 130 void ggml_openvino_register_weight_buffer(void * data, size_t size); 131 void ggml_openvino_release_weight_buffers(); 132 bool ggml_openvino_weight_buffers_released(); 133 134 // Get requantization type for a tensor type (returns nullopt if no requant needed) 135 std::optional ggml_openvino_get_requant_type(const ggml_tensor * tensor, bool no_requant = false); 136 137 // ===================================================== 138 // OpenVINO Tensor Extra Types 139 // ===================================================== 140 // These types are stored in tensor->extra by the OpenVINO backend buffer. 141 // They allow: 142 // 1. Pre-built ov::Constant nodes for weights (avoiding memcpy during graph construction) 143 // 2. ov::Tensor wrappers for KV cache / compute tensors (for direct use with infer_request) 144 145 // Base class for OpenVINO tensor extra data 146 struct ggml_openvino_extra_base { 147 enum class Type { WEIGHT, QUANTIZED_WEIGHT, TENSOR }; 148 Type type; 149 virtual ~ggml_openvino_extra_base() = default; 150 protected: 151 explicit ggml_openvino_extra_base(Type t) : type(t) {} 152 }; 153 154 // Extra data for F16/F32/BF16 weight tensors - stores the pre-built weight node 155 struct ggml_openvino_weight_extra : public ggml_openvino_extra_base { 156 ov::Tensor weights; // The underlying weight data tensor 157 std::shared_ptr weight_node; // Pre-built OpenVINO weight node 158 159 ggml_openvino_weight_extra(ov::Tensor w, std::shared_ptr n) : 160 ggml_openvino_extra_base(Type::WEIGHT), 161 weights(std::move(w)), 162 weight_node(std::move(n)) {} 163 }; 164 165 // Extra data for quantized weight tensors - stores extracted weights/scales/zp and weight node 166 struct ggml_openvino_quantized_weight_extra : public ggml_openvino_extra_base { 167 ov::Tensor weights; // U4 or U8 extracted weights 168 ov::Tensor scales; // F16 scales 169 ov::Tensor zp; // U4 or U8 zero points (same type as weights) 170 std::shared_ptr weight_node; // Pre-built OpenVINO weight subgraph 171 172 ggml_openvino_quantized_weight_extra(ov::Tensor w, ov::Tensor s, ov::Tensor z, std::shared_ptr n) : 173 ggml_openvino_extra_base(Type::QUANTIZED_WEIGHT), 174 weights(std::move(w)), 175 scales(std::move(s)), 176 zp(std::move(z)), 177 weight_node(std::move(n)) {} 178 }; 179 180 // Extra data for KV cache / compute tensors - stores ov::Tensor for infer_request 181 struct ggml_openvino_tensor_extra : public ggml_openvino_extra_base { 182 std::shared_ptr tensor; // For direct use with infer_request 183 184 explicit ggml_openvino_tensor_extra(std::shared_ptr t) : 185 ggml_openvino_extra_base(Type::TENSOR), 186 tensor(std::move(t)) {} 187 }; 188 189 // ===================================================== 190 // Extracted Size Calculation for Quantized Tensors 191 // ===================================================== 192 // For quantized tensors, we need extra space to store extracted weights, scales, and zero points. 193 // Returns the total size needed in the buffer for extracted data. 194 195 struct ggml_openvino_extracted_layout { 196 size_t total_size = 0; // Total bytes needed 197 size_t weights_offset = 0; // Offset to weights in buffer 198 size_t weights_size = 0; // Size of weights in bytes 199 size_t scales_offset = 0; // Offset to scales in buffer 200 size_t scales_size = 0; // Size of scales in bytes 201 size_t zp_offset = 0; // Offset to zero points in buffer 202 size_t zp_size = 0; // Size of zero points in bytes (U4 or U8) 203 bool is_u4; // true for U4 weights, false for U8 204 int64_t weights_per_block; // weights per scale/zp block 205 bool is_symmetric; // true for symmetric quantization 206 207 // Requantization info 208 bool is_requant = false; // true if this tensor needs requantization 209 std::optional requant_type; // target requant type if is_requant 210 }; 211 212 // Calculate the buffer layout for extracted quantized data 213 ggml_openvino_extracted_layout ggml_openvino_get_extracted_layout(const ggml_tensor * tensor, bool use_bias = false); 214 215 ggml_openvino_tensor_extra * ggml_openvino_create_tensor_extra(const ggml_tensor * tensor, bool is_remote); 216 217 // Check if a tensor's buffer uses remote (device) memory (e.g. GPU USM) 218 bool ggml_openvino_buffer_is_remote(const ggml_tensor * tensor); 219 220 // Register an extra with the tensor's OpenVINO buffer context for proper lifetime management. 221 // This sets tensor->extra and tracks the extra in the buffer context for cleanup. 222 void ggml_openvino_buffer_register_extra(ggml_tensor * tensor, ggml_openvino_extra_base * extra); 223 224 // ===================================================== 225 // OpenVINO Backend Context and Interface 226 // ===================================================== 227 struct ggml_backend_openvino_context { 228 int device = 0; 229 std::string name = "OpenVINO"; 230 std::string description = "OpenVINO Backend Context"; 231 232 std::shared_ptr runtime_context = nullptr; 233 234 ggml_backend_openvino_context() = default; 235 };