* OpenVINO Backend: Fuse IM2COL + MatMul convolution into OpenVINO convolution * ci:ggml-ov: Skip recurrent state rollback tests * ci:ggml-ov: Skip recurrent state rollback tests * Update OPENVINO.md * ggml-openvino : add env-var gated op support debugging * Fix ggml_rope_set_offset case * OpenVINO backend: Support Whisper.cpp * Fix code style * openvino : enable qwen35 on NPU Static shapes: - get_graph_input_shape() left the s_copy / s_copy-leaf inputs dynamic ([1,1,1,-1]) even in static mode, which propagated a dynamic slot dim through GET_ROWS into the conv/GDN state, the state reshapes and the GDN output. - With -np 1 the s_copy defrag remainder gathers zero rows; short-circuit that CPY to the untouched cache instead of emitting a degenerate Slice/Concat, and skip binding its zero-byte ggml tensor as an output (the dynamic path already did the latter, the static path wrote the full cache over a 0-byte buffer). Token-count independence: - In static mode the compiled model's token count is the prefill chunk size or 1, not the captured cgraph's. Offsets derived from the captured count were therefore wrong. Anchor the GDN state slice at the end of the packed [attn | state] output and drop the rs_src_begin runtime inputs, and make VIEWs over the GDN output / conv_input pass through so the consumer does the slicing. - CONT could not identify its token axis when the graph was captured with a single token (every trailing dim has the same stride and size 1) and baked the captured shape into the prefill model. Chunked prefill: - The last chunk is padded with fabricated tokens. Attention masks them, but the recurrent path folded them into cache_r/cache_s permanently. Add a chunk_valid_len runtime input, use it to zero g and beta for padded steps (making the recurrence an exact identity) and to end the conv snapshot window at the last valid token, and disable the recurrent-cache reset after the first chunk so earlier chunks are not wiped. - get_is_prefill() and the chunk loop bound read inp_pos->ne[0] directly, but IMROPE stacks 4 position planes, so every decode step was run through the padded prefill model and the loop ran extra out-of-bounds chunks. cache_rs_reset_idx/len now stay runtime Parameters in static mode, since can_reuse_statically() does not invalidate the cached model on ComputeParams changes. Add GGML_OPENVINO_FORCE_STATIC to exercise the static path on CPU. * Update to OpenVINO 2026.3.1 * ggml-openvino: forward NPU compilation mode parameters Add GGML_OPENVINO_NPU_COMPILE_CONFIG to the backend's cached environment so callers can configure the NPU compiler without using the generic property escape hatch. When the value is non-empty, pass it to OpenVINO as NPU_COMPILATION_MODE_PARAMS. This enables settings such as optimization-level=3 for NPU compilation while preserving the existing behavior when the variable is unset and leaving CPU and GPU configuration unchanged. Document the variable, its NPU-only scope, and the optimization-level=3 example in the OpenVINO backend runtime configuration table. * ggml-openvino : support RELU, POOL_2D, QUICK_GEGLU, and ROLL ops * reorder op table * exclude GPU/NPU failing POOL_2D case * move op type detection to compute_op_case * Relax rope supported cases * Fix pool case * Update openvino doc, gpu driver in ov docker * openvino: remove unused static remote context branch * openvino: parallelize static model build * Apply editorconfig --------- Co-authored-by: Mostafa Faheem <mostafaaafaheem@gmail.com> Co-authored-by: Ravi Panchumarthy <ravi.panchumarthy@intel.com> Co-authored-by: zhaixuejun1993 <xuejun.zhai@intel.com>
196 lines
7.9 KiB
C++
196 lines
7.9 KiB
C++
#include "ggml-decoder.h"
|
|
#include "ggml-impl.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <openvino/runtime/core.hpp>
|
|
#include <openvino/runtime/infer_request.hpp>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
struct graph_key {
|
|
int n_nodes;
|
|
std::string first_node_name;
|
|
std::string last_node_name;
|
|
std::vector<std::string> input_src_names;
|
|
|
|
graph_key(const ggml_cgraph * cgraph) : n_nodes(cgraph->n_nodes) {
|
|
if (n_nodes > 0) {
|
|
first_node_name = cgraph->nodes[0]->name;
|
|
last_node_name = cgraph->nodes[n_nodes - 1]->name;
|
|
}
|
|
|
|
auto get_input_key_name = [](const ggml_cgraph * graph, const ggml_tensor * tensor) {
|
|
std::string name = tensor->name;
|
|
const size_t hash_pos = ggml_hash_find(&graph->visited_hash_set, tensor);
|
|
if (((tensor->flags & GGML_TENSOR_FLAG_COMPUTE) || GgmlOvDecoder::is_kvcache(tensor, nullptr)) &&
|
|
hash_pos != GGML_HASHSET_FULL && ggml_bitset_get(graph->visited_hash_set.used, hash_pos)) {
|
|
name += "#" + std::to_string(hash_pos);
|
|
}
|
|
return name;
|
|
};
|
|
|
|
std::vector<std::string> node_names;
|
|
node_names.reserve(cgraph->n_nodes);
|
|
for (int node_idx = 0; node_idx < cgraph->n_nodes; node_idx++) {
|
|
node_names.emplace_back(cgraph->nodes[node_idx]->name);
|
|
}
|
|
|
|
for (int node_idx = 0; node_idx < cgraph->n_nodes; node_idx++) {
|
|
const ggml_tensor * node = cgraph->nodes[node_idx];
|
|
for (int src_idx = 0; src_idx < GGML_MAX_SRC; src_idx++) {
|
|
const ggml_tensor * src = node->src[src_idx];
|
|
if (src == nullptr || src->name[0] == '\0') {
|
|
continue;
|
|
}
|
|
|
|
const std::string src_name = get_input_key_name(cgraph, src);
|
|
if (std::find(node_names.begin(), node_names.end(), src_name) != node_names.end()) {
|
|
continue;
|
|
}
|
|
if (src_name.find("weight") != std::string::npos) {
|
|
continue;
|
|
}
|
|
|
|
input_src_names.push_back(std::to_string(node_idx) + ":" + std::to_string(src_idx) + ":" + src_name);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool operator==(const graph_key & other) const {
|
|
return n_nodes == other.n_nodes && first_node_name == other.first_node_name &&
|
|
last_node_name == other.last_node_name && input_src_names == other.input_src_names;
|
|
}
|
|
};
|
|
|
|
struct graph_key_hash {
|
|
size_t operator()(const graph_key & key) const {
|
|
size_t hash = std::hash<int>{}(key.n_nodes);
|
|
if (key.n_nodes > 0) {
|
|
hash ^= std::hash<std::string>{}(key.first_node_name) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
|
hash ^= std::hash<std::string>{}(key.last_node_name) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
|
}
|
|
for (const auto & input_src_name : key.input_src_names) {
|
|
hash ^= std::hash<std::string>{}(input_src_name) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
|
}
|
|
return hash;
|
|
}
|
|
};
|
|
|
|
struct decoder_runtime_ctx {
|
|
decoder_runtime_ctx(std::shared_ptr<std::mutex> mutex) : mutex(std::move(mutex)) {}
|
|
|
|
std::shared_ptr<std::mutex> mutex;
|
|
std::shared_ptr<GgmlOvDecoder> ptr;
|
|
};
|
|
|
|
struct ov_runtime_context {
|
|
mutable std::mutex ctx_mutex;
|
|
std::string device;
|
|
bool stateful;
|
|
std::unordered_map<graph_key, std::shared_ptr<decoder_runtime_ctx>, graph_key_hash> decoder_cache;
|
|
std::unordered_map<graph_key, std::shared_ptr<ov::InferRequest>, graph_key_hash> infer_request_cache;
|
|
std::unordered_map<graph_key, std::shared_ptr<ov::InferRequest>, graph_key_hash> infer_request_cache_prefill;
|
|
std::unordered_map<graph_key, std::vector<std::string>, graph_key_hash> ov_input_names_cache;
|
|
std::unordered_map<graph_key, std::vector<std::string>, graph_key_hash> ov_output_names_cache;
|
|
//TODO: Stateful is only supported for single request at a time.
|
|
// Simultanous stateful inference request support to be added.
|
|
size_t stateful_kv_size;
|
|
std::map<std::string, std::string> kv_state_input_name_map;
|
|
std::atomic<int> backend_count;
|
|
|
|
ov_runtime_context() : device("CPU"), stateful(false), stateful_kv_size(0), backend_count(0) {}
|
|
|
|
void clear_caches_locked() {
|
|
decoder_cache.clear();
|
|
infer_request_cache.clear();
|
|
infer_request_cache_prefill.clear();
|
|
ov_input_names_cache.clear();
|
|
ov_output_names_cache.clear();
|
|
kv_state_input_name_map.clear();
|
|
stateful_kv_size = 0;
|
|
}
|
|
|
|
void clear_caches() {
|
|
std::lock_guard<std::mutex> lock(ctx_mutex);
|
|
clear_caches_locked();
|
|
}
|
|
};
|
|
|
|
enum ggml_status ov_graph_compute(struct ggml_cgraph * cgraph, ggml_backend_t backend);
|
|
|
|
enum ggml_status ov_graph_compute_dynamic(struct ggml_cgraph * cgraph, std::shared_ptr<ov_runtime_context> r_ctx);
|
|
enum ggml_status ov_graph_compute_static(struct ggml_cgraph * cgraph, std::shared_ptr<ov_runtime_context> r_ctx);
|
|
|
|
size_t checksum(const void * data, size_t size);
|
|
|
|
bool save_ggml_tensor_data_to_txt(const ggml_tensor * tensor, const std::string & file_path);
|
|
|
|
void print_input_tensor_info(const std::string & name, const ov::Tensor & tensor);
|
|
|
|
void print_output_tensor_info(const std::string & name, const ov::Tensor & tensor, const void * output_dst);
|
|
|
|
template <typename T>
|
|
std::vector<T> pad_input(const T * data,
|
|
size_t rows,
|
|
size_t cols,
|
|
size_t padded_rows,
|
|
size_t padded_cols,
|
|
T pad_value) {
|
|
std::vector<T> padded(padded_rows * padded_cols, pad_value);
|
|
|
|
for (size_t i = 0; i < std::min(rows, padded_rows); ++i) {
|
|
for (size_t j = 0; j < std::min(cols, padded_cols); ++j) {
|
|
padded[i * padded_cols + j] = data[i * cols + j];
|
|
}
|
|
}
|
|
|
|
return padded;
|
|
}
|
|
|
|
template <typename T>
|
|
std::vector<T> pad_input(const ggml_tensor * tensor, size_t padded_rows, size_t padded_cols, T pad_value) {
|
|
return pad_input<T>(reinterpret_cast<const T *>(tensor->data),
|
|
static_cast<size_t>(tensor->ne[1]), // rows
|
|
static_cast<size_t>(tensor->ne[0]), // cols
|
|
padded_rows, padded_cols, pad_value);
|
|
}
|
|
|
|
const ggml_tensor * get_inp_pos_tensor(struct ggml_cgraph * cgraph);
|
|
|
|
int64_t get_inp_pos_n_tokens(struct ggml_cgraph * cgraph, const ggml_tensor * inp_pos);
|
|
|
|
bool get_is_prefill(struct ggml_cgraph * cgraph, const ggml_tensor * inp_pos);
|
|
|
|
ov::Tensor get_ov_input_tensor(std::shared_ptr<GgmlOvDecoder> ggml_decoder, const std::string & param_name);
|
|
ov::Tensor get_ov_input_tensor_static_decode(std::shared_ptr<GgmlOvDecoder> ggml_decoder,
|
|
const std::string & param_name);
|
|
ov::Tensor get_ov_input_tensor_static_prefill(std::shared_ptr<GgmlOvDecoder> ggml_decoder,
|
|
const std::string & param_name,
|
|
int chunk_index);
|
|
|
|
ov::Tensor create_ov_output_tensor(std::shared_ptr<GgmlOvDecoder> ggml_decoder,
|
|
std::shared_ptr<ov::InferRequest> infer_request,
|
|
int output_index,
|
|
const ggml_tensor * ggml_tensor);
|
|
|
|
bool is_naive(struct ggml_cgraph * cgraph);
|
|
|
|
/**
|
|
* @brief Heuristically checks whether the given computation graph is a split-model fragment.
|
|
* @param cgraph Pointer to the GGML computation graph to analyze.
|
|
* @return true if the graph is identified as split; otherwise false.
|
|
*/
|
|
bool is_model_splitted(struct ggml_cgraph * cgraph);
|
|
|
|
enum ggml_status naive_compute(struct ggml_cgraph * cgraph,
|
|
ov::Core & core,
|
|
const std::string & device,
|
|
const ov::AnyMap & config);
|