interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-openvino / utils.h

196 lines · 7794 bytes Raw Blame History
1#include "ggml-decoder.h"
2#include "ggml-impl.h"
3#include "ggml-openvino-extra.h"
4
5#include <cstddef>
6#include <cstdint>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <openvino/runtime/core.hpp>
11#include <openvino/runtime/infer_request.hpp>
12#include <string>
13#include <unordered_map>
14#include <unordered_set>
15#include <utility>
16#include <vector>
17
18// Cache key for a translated/compiled graph. Node count plus the two end node names identify a
19// graph during inference, where the same few graphs repeat for the whole session. The list of
20// external input names below tells those apart more precisely, but it walks every node and every
21// src slot, so it is built only when GGML_OPENVINO_FULL_GRAPH_KEY is set. Op tests run many small
22// graphs that can share a node count and end names, and need the full key.
23struct graph_key {
24 int n_nodes;
25 int n_leaves;
26 std::string first_node_name;
27 std::string last_node_name;
28 // Each entry: "<node_idx>:<src_idx>:<op_type>"
29 std::vector<std::string> input_srcs;
30
31 graph_key(const ggml_cgraph * cgraph, bool include_inputs = false) : n_nodes(cgraph->n_nodes), n_leaves(cgraph->n_leafs) {
32 if (n_nodes > 0) {
33 first_node_name = cgraph->nodes[0]->name;
34 last_node_name = cgraph->nodes[n_nodes - 1]->name;
35 }
36
37 static const bool full_key = ggml_openvino_getenv_int("GGML_OPENVINO_FULL_GRAPH_KEY") != 0;
38 if (!full_key && !include_inputs) {
39 return;
40 }
41
42 std::unordered_set<const ggml_tensor *> node_set;
43 node_set.reserve(cgraph->n_nodes);
44 for (int i = 0; i < cgraph->n_nodes; i++) {
45 node_set.insert(cgraph->nodes[i]);
46 }
47
48 for (int node_idx = 0; node_idx < cgraph->n_nodes; node_idx++) {
49 const ggml_tensor * node = cgraph->nodes[node_idx];
50 for (int src_idx = 0; src_idx < GGML_MAX_SRC; src_idx++) {
51 const ggml_tensor * src = node->src[src_idx];
52 if (src == nullptr || src->buffer == nullptr || node_set.count(src) || src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
53 continue;
54 }
55
56 input_srcs.push_back(std::to_string(node_idx) + ":" + std::to_string(src_idx) + ":" +
57 GgmlOvDecoder::compute_op_type(node));
58 }
59 }
60 }
61
62 bool operator==(const graph_key & other) const {
63 return n_nodes == other.n_nodes && n_leaves == other.n_leaves &&
64 first_node_name == other.first_node_name &&
65 last_node_name == other.last_node_name && input_srcs == other.input_srcs;
66 }
67};
68
69struct graph_key_hash {
70 size_t operator()(const graph_key & key) const {
71 size_t hash = std::hash<int>{}(key.n_nodes);
72 hash ^= std::hash<int>{}(key.n_leaves) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
73 if (key.n_nodes > 0) {
74 hash ^= std::hash<std::string>{}(key.first_node_name) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
75 hash ^= std::hash<std::string>{}(key.last_node_name) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
76 }
77 for (const auto & s : key.input_srcs) {
78 hash ^= std::hash<std::string>{}(s) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
79 }
80 return hash;
81 }
82};
83
84struct decoder_runtime_ctx {
85 decoder_runtime_ctx(std::shared_ptr<std::mutex> mutex) : mutex(std::move(mutex)) {}
86
87 std::shared_ptr<std::mutex> mutex;
88 std::shared_ptr<GgmlOvDecoder> ptr;
89};
90
91struct ov_compiled_graph {
92 ov::CompiledModel decode;
93 ov::CompiledModel prefill;
94 std::vector<std::string> input_names;
95 std::vector<std::string> output_names;
96};
97
98// Only compilation and cache publication use this mutex. Requests, decoders and
99// sequence state belong to individual backend contexts and never enter this cache.
100struct ov_compiled_model_cache {
101 std::mutex mutex;
102 std::unordered_map<std::string, ov_compiled_graph> graphs;
103 size_t backend_count = 0;
104};
105
106// Private to one backend instance. Only compiled_cache is shared with other
107// instances; clearing these local caches cannot invalidate their requests.
108struct ov_runtime_context {
109 // Serializes calls on this backend only, not inference in other contexts.
110 std::mutex execution_mutex;
111 std::shared_ptr<ov_compiled_model_cache> compiled_cache;
112 mutable std::mutex ctx_mutex;
113 std::string device;
114 bool stateful;
115 std::unordered_map<graph_key, std::shared_ptr<decoder_runtime_ctx>, graph_key_hash> decoder_cache;
116 std::unordered_map<graph_key, std::shared_ptr<ov::InferRequest>, graph_key_hash> infer_request_cache;
117 std::unordered_map<graph_key, std::shared_ptr<ov::InferRequest>, graph_key_hash> infer_request_cache_prefill;
118 std::unordered_map<graph_key, std::vector<std::string>, graph_key_hash> ov_input_names_cache;
119 std::unordered_map<graph_key, std::vector<std::string>, graph_key_hash> ov_output_names_cache;
120 size_t stateful_kv_size;
121 std::map<std::string, std::string> kv_state_input_name_map;
122 // compute-only copies of graphs that carry unselected ggml_build_forward_select() branches
123 struct compute_graph {
124 ggml_cgraph graph;
125 std::vector<ggml_tensor *> nodes;
126 };
127 std::unordered_map<const ggml_cgraph *, compute_graph> compute_graphs;
128
129 ov_runtime_context() : device("CPU"), stateful(false), stateful_kv_size(0) {}
130
131 void clear_caches_locked() {
132 decoder_cache.clear();
133 infer_request_cache.clear();
134 infer_request_cache_prefill.clear();
135 ov_input_names_cache.clear();
136 ov_output_names_cache.clear();
137 kv_state_input_name_map.clear();
138 stateful_kv_size = 0;
139 }
140
141 void clear_caches() {
142 std::lock_guard<std::mutex> lock(ctx_mutex);
143 clear_caches_locked();
144 }
145};
146
147enum ggml_status ov_graph_compute(struct ggml_cgraph * cgraph, ggml_backend_t backend);
148
149size_t checksum(const void * data, size_t size);
150
151bool save_ggml_tensor_data_to_txt(const ggml_tensor * tensor, const std::string & file_path);
152
153void print_input_tensor_info(const std::string & name, const ov::Tensor & tensor);
154
155void print_output_tensor_info(const std::string & name, const ov::Tensor & tensor, const void * output_dst);
156
157template <typename T>
158std::vector<T> pad_input(const T * data,
159 size_t rows,
160 size_t cols,
161 size_t padded_rows,
162 size_t padded_cols,
163 T pad_value) {
164 std::vector<T> padded(padded_rows * padded_cols, pad_value);
165
166 for (size_t i = 0; i < std::min(rows, padded_rows); ++i) {
167 for (size_t j = 0; j < std::min(cols, padded_cols); ++j) {
168 padded[i * padded_cols + j] = data[i * cols + j];
169 }
170 }
171
172 return padded;
173}
174
175template <typename T>
176std::vector<T> pad_input(const ggml_tensor * tensor, size_t padded_rows, size_t padded_cols, T pad_value) {
177 return pad_input<T>(reinterpret_cast<const T *>(tensor->data),
178 static_cast<size_t>(tensor->ne[1]), // rows
179 static_cast<size_t>(tensor->ne[0]), // cols
180 padded_rows, padded_cols, pad_value);
181}
182
183const ggml_tensor * get_inp_pos_tensor(struct ggml_cgraph * cgraph);
184
185int64_t get_inp_pos_n_tokens(struct ggml_cgraph * cgraph, const ggml_tensor * inp_pos);
186
187bool get_is_prefill(struct ggml_cgraph * cgraph, const ggml_tensor * inp_pos);
188
189bool is_naive(struct ggml_cgraph * cgraph);
190
191/**
192 * @brief Heuristically checks whether the given computation graph is a split-model fragment.
193 * @param cgraph Pointer to the GGML computation graph to analyze.
194 * @return true if the graph is identified as split; otherwise false.
195 */
196bool is_model_splitted(struct ggml_cgraph * cgraph);