mirror of
https://github.com/opencv/opencv.git
synced 2026-12-05 14:35:09 -06:00
Added support for Granite Docling 258M and PaddleOCR-VL - #29618 Adds `com.microsoft::GroupQueryAttention` to the engine-new ONNX importer, required by GraniteDocling-258M's ,end-to-end samples for GraniteDocling-258M and PaddleOCR-VL-1.5. Verified on Linux x86_64 / GCC / CPU **companion opencv_extra PR** [Vlm ocr support engine new opencv/opencv_extra#1412](https://github.com/opencv/opencv_extra/pull/1412) for test data upstream/5.x already lowers `GroupQueryAttention` to `AttentionOnnxAi`, but that path rejects `do_rotary=1`: ```cpp CV_CheckEQ(params.get<int>("do_rotary", 0), 0, "GroupQueryAttention: do_rotary=1 is not supported"); ``` GraniteDocling-258M's export sets it. Confirmed two ways: building a Llama checkpoint from its published `text_config` (9 heads / 3 KV heads, `rope_theta=100000`) and running onnxruntime-genai's `model_builder`; and reading the community export at `onnx-community/granite-docling-258M-ONNX`, which carries 30 `GroupQueryAttention` nodes with `do_rotary=1`, `num_heads=9`, `kv_num_heads=3`, `head_dim=64`. ## Changes **Importer** (`onnx_importer2.cpp`, +49) Routing as above, plus the input validation the op needs: packed QKV rejected, past_key / past_value required as a pair, `do_rotary=1` requires cos/sin, unsupported trailing inputs rejected, and `seqlens_k` shorter than the buffer recognised as a shared-buffer export rather than silently mis-attended. **Layer** (`group_query_attention_layer.cpp`, +380) Attention is built on `fastGemmBatch` and `fused_softmax_softcap_mask`, the same kernels `attention_onnxai_layer.cpp` uses. Grouping falls out of the batched-GEMM offset table (query head → `h / groupSize`), so no broadcast copy is needed, and the second GEMM writes straight into the `[B, S, num_heads*D]` output. Supports rotary (full and partial), sliding window, softcap, growing and preallocated caches, and optional `present_*` outputs. **Core** (`persistence_json.cpp`, +92) `\uXXXX` escapes are UTF-16 code units, so anything above the BMP arrives as a surrogate pair. These were encoded as two separate 3-byte sequences CESU-8, which is not valid UTF-8 - for keys and values alike. Now recombined into one 4-byte sequence, with unpaired surrogates rejected. **Tests / perf / samples** `test_layer_group_query_attention.cpp` (+93), `test_tokenizer.cpp` (+28), `test_tokenizer.py` (+42), `perf_layer.cpp` (+125: `Layer_GroupQueryAttention` MHA_ShortCache and Grouped_WithCache, plus `Layer_Sign`), and two samples (`granite_docling_inference.py`, `paddleocr_vl_inference.py`). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
343 lines
11 KiB
C++
343 lines
11 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
#include "precomp.hpp"
|
|
#include "kv_cache_manager.hpp"
|
|
#include "net_impl.hpp"
|
|
|
|
#include <memory>
|
|
#include <opencv2/core/utils/tls.hpp>
|
|
#include "layers/cpu_kernels/fast_gemm.hpp"
|
|
namespace cv { namespace dnn {
|
|
CV__DNN_INLINE_NS_BEGIN
|
|
|
|
|
|
|
|
void initKVDataRecursively(const Ptr<Graph>& graph, std::map<std::string, KCache>& kData, std::map<std::string, VCache>& vData, FastGemmOpt& opt) {
|
|
for (const auto& layer : graph->prog()) {
|
|
|
|
for (const auto& subgraph : layer->subgraphs() ? *layer->subgraphs() : std::vector<Ptr<Graph>>()) {
|
|
initKVDataRecursively(subgraph, kData, vData, opt);
|
|
}
|
|
|
|
if (layer->type == "AttentionOnnxAi") {
|
|
Ptr<AttentionOnnxAiLayer> attn = layer.dynamicCast<AttentionOnnxAiLayer>();
|
|
// Leaving it out of kData/vData also routes its forward() to the graph inputs.
|
|
if (!attn->paged_cache_supported)
|
|
continue;
|
|
int kvNumHeads = attn->kv_num_heads;
|
|
|
|
if (kvNumHeads > 0)
|
|
{
|
|
kData.emplace(layer->name, KCache(opt, kvNumHeads));
|
|
vData.emplace(layer->name, VCache(opt, kvNumHeads));
|
|
}
|
|
else
|
|
{
|
|
kData.emplace(layer->name, KCache(opt));
|
|
vData.emplace(layer->name, VCache(opt));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setKVCacheManager(Ptr<Net::Impl> netimpl)
|
|
{
|
|
CV_Assert(netimpl != nullptr);
|
|
|
|
// readNetFromONNX() returns an empty Net on unsupported ops; don't walk a null graph.
|
|
CV_CheckTrue(!netimpl->mainGraph.empty(),
|
|
"enableKVCache() requires a successfully loaded model");
|
|
CV_Assert(!netimpl->layers.empty());
|
|
|
|
auto manager = KVCacheManager();
|
|
manager.netimpl = netimpl;
|
|
manager.opt.init();
|
|
initKVDataRecursively(netimpl->mainGraph, manager.kData, manager.vData, manager.opt);
|
|
manager.buildRoutes();
|
|
|
|
manager.isInitialized = true;
|
|
netimpl->useKVCache = true;
|
|
netimpl->kvCacheManager = std::move(manager);
|
|
}
|
|
|
|
void KVCacheManager::init()
|
|
{
|
|
// Construction of per-layer caches happens in setKVCacheManager.
|
|
// This method is retained as a callable hook for deferred re-init if needed.
|
|
CV_Assert(isInitialized);
|
|
}
|
|
|
|
void KVCacheManager::buildRoutes()
|
|
{
|
|
presentToPastRoutes.clear();
|
|
hasRoutes = false;
|
|
|
|
if (!netimpl || !netimpl->mainGraph)
|
|
return;
|
|
|
|
const std::vector<Arg>& gr_outputs = netimpl->mainGraph->outputs();
|
|
for (const Arg& out_arg : gr_outputs)
|
|
{
|
|
const ArgData& out_adata = netimpl->args.at(out_arg.idx);
|
|
const std::string& out_name = out_adata.name;
|
|
|
|
if (out_name.compare(0, 8, "present.") != 0)
|
|
continue;
|
|
|
|
std::string past_name = "past_key_values." + out_name.substr(8);
|
|
auto it = netimpl->argnames.find(past_name);
|
|
if (it == netimpl->argnames.end())
|
|
continue;
|
|
|
|
Arg past_arg((int)it->second);
|
|
if (netimpl->args.at(past_arg.idx).kind != DNN_ARG_INPUT)
|
|
continue;
|
|
|
|
presentToPastRoutes.emplace_back(out_arg.idx, past_arg.idx);
|
|
}
|
|
|
|
hasRoutes = !presentToPastRoutes.empty();
|
|
if (hasRoutes)
|
|
initPastTensors();
|
|
}
|
|
|
|
void KVCacheManager::initPastTensors()
|
|
{
|
|
for (const auto& route : presentToPastRoutes)
|
|
{
|
|
const ArgData& past_adata = netimpl->args.at(route.second);
|
|
const MatShape& decl_shape = past_adata.shape;
|
|
if (decl_shape.dims <= 0)
|
|
continue;
|
|
|
|
// Replace symbolic dims (stored as <=0): batch (dim 0) -> 1, all others -> 0 for empty-sequence state.
|
|
std::vector<int> shape_vec(decl_shape.dims);
|
|
for (int d = 0; d < decl_shape.dims; d++)
|
|
shape_vec[d] = (decl_shape[d] > 0) ? decl_shape[d] : (d == 0 ? 1 : 0);
|
|
|
|
int dtype = past_adata.type;
|
|
if (dtype < 0)
|
|
dtype = CV_32F;
|
|
|
|
Mat& past_t = netimpl->__tensors__.at(route.second);
|
|
past_t = Mat(shape_vec, dtype, Scalar(0));
|
|
// The consumer's signature changed; the op loop re-finalizes it.
|
|
}
|
|
}
|
|
|
|
void KVCacheManager::applyRoutes()
|
|
{
|
|
for (const auto& route : presentToPastRoutes)
|
|
{
|
|
const Mat& present_t = netimpl->argTensor(Arg(route.first));
|
|
Mat& past_t = netimpl->__tensors__.at(route.second);
|
|
if (present_t.empty())
|
|
continue;
|
|
// Grown buffer changes the consumer's signature; the op loop re-finalizes it.
|
|
present_t.copyTo(past_t);
|
|
}
|
|
}
|
|
|
|
void KVCacheManager::reserve(int maxTokens)
|
|
{
|
|
for (auto& kv : kData) kv.second.reserve(maxTokens);
|
|
for (auto& kv : vData) kv.second.reserve(maxTokens);
|
|
}
|
|
|
|
void KVCache::grow(const Mat& newData) {
|
|
CV_Assert(newData.dims == 4 || newData.dims == 3);
|
|
|
|
if (nHeads == -1) {
|
|
CV_Assert(newData.dims == 4); // to derive shape from data, we need 4D
|
|
headDim = newData.size[3];
|
|
nHeads = newData.size[1];
|
|
batchSize = newData.size[0];
|
|
} else {
|
|
if (headDim == -1) {
|
|
if (newData.dims == 4)
|
|
headDim = newData.size[3];
|
|
else{
|
|
CV_Assert(newData.dims == 3);
|
|
CV_Assert(newData.size[2] % nHeads == 0);
|
|
headDim = newData.size[2] / nHeads;
|
|
}
|
|
} else {
|
|
if (newData.dims == 4)
|
|
CV_Assert(newData.size[3] == headDim);
|
|
else
|
|
CV_Assert(newData.size[2] == headDim * nHeads);
|
|
}
|
|
if (batchSize == -1)
|
|
batchSize = newData.size[0];
|
|
}
|
|
|
|
int T = newData.dims == 4 ? newData.size[2] : newData.size[1];
|
|
|
|
if (pages.empty()) {
|
|
// Prefill: allocate the page pool, honouring any reserved capacity.
|
|
int neededPages = (T + pageSize - 1) / pageSize;
|
|
int reservedPages = (int)(((int64_t)reservedTokens + pageSize - 1) / pageSize);
|
|
int totalPages = std::max(neededPages, reservedPages);
|
|
for (int i = 0; i < totalPages; i++) {
|
|
int page_size = isKCache ?
|
|
(int)fastGemmPackBSize(pageSize, headDim, opt):
|
|
(int)fastGemmPackBSize(headDim, pageSize, opt);
|
|
|
|
pages.push_back(
|
|
Mat({batchSize, nHeads, page_size}, CV_32F, Scalar(0))
|
|
);
|
|
}
|
|
growPrefill(newData, T);
|
|
} else {
|
|
// Non-empty cache: append T (>=1) tokens (T>1 = chunked prefill / speculative decode).
|
|
appendTokens(newData, T);
|
|
}
|
|
|
|
}
|
|
|
|
void KVCache::appendTokens(const Mat& newData, int T) {
|
|
if (T == 1) {
|
|
growGenerate(newData);
|
|
return;
|
|
}
|
|
const int seqAxis = newData.dims == 4 ? 2 : 1;
|
|
std::vector<Range> ranges(newData.dims, Range::all());
|
|
for (int t = 0; t < T; t++) {
|
|
ranges[seqAxis] = Range(t, t + 1);
|
|
Mat token = newData(ranges).clone(); // contiguous single-token slice
|
|
growGenerate(token);
|
|
}
|
|
}
|
|
|
|
void KVCache::growPrefill(const Mat& newData, int T){
|
|
int totalPages = (T + pageSize - 1) / pageSize;
|
|
int ps = isKCache ? (int)fastGemmPackBSize(pageSize, headDim, opt)
|
|
: (int)fastGemmPackBSize(headDim, pageSize, opt);
|
|
|
|
bool is3Dlayout = newData.dims == 3;
|
|
|
|
int total = totalPages * batchSize * nHeads;
|
|
|
|
cv::TLSData<std::vector<float>> tls_temp_buf;
|
|
|
|
auto fn = [&](const Range& range) {
|
|
for (int i = range.start; i < range.end; i++) {
|
|
int page = i / (batchSize * nHeads);
|
|
int b = (i - page * batchSize * nHeads) / nHeads;
|
|
int h = i % nHeads;
|
|
|
|
// source
|
|
size_t step_source = b * nHeads * T * headDim;
|
|
if(is3Dlayout)
|
|
step_source += page * pageSize * nHeads * headDim +
|
|
h * headDim;
|
|
else
|
|
step_source += h * headDim * T + page * pageSize * headDim;
|
|
const auto* source = newData.ptr<float>() + step_source;
|
|
|
|
int chunk_T = std::min(pageSize, T - page * pageSize);
|
|
const float* actual_source = source;
|
|
|
|
int lds = is3Dlayout ? headDim * nHeads : headDim;
|
|
|
|
if (chunk_T < pageSize) {
|
|
std::vector<float>& temp_buf = *tls_temp_buf.get();
|
|
temp_buf.assign(pageSize * headDim, 0.0f);
|
|
for (int i = 0; i < chunk_T; i++) {
|
|
std::memcpy(temp_buf.data() + i * headDim, source + i * lds, headDim * sizeof(float));
|
|
}
|
|
actual_source = temp_buf.data();
|
|
}
|
|
|
|
// dst
|
|
size_t step_dst = b * nHeads * ps +
|
|
h * ps;
|
|
auto* dst = pages[page].ptr<float>() + step_dst;
|
|
|
|
const int N = headDim;
|
|
const int K = pageSize;
|
|
|
|
fastGemmPackB(
|
|
isKCache,
|
|
N, K,
|
|
actual_source, chunk_T < pageSize ? headDim : lds,
|
|
dst,
|
|
opt
|
|
);
|
|
}
|
|
};
|
|
parallel_for_(Range(0, total), fn);
|
|
nTokens += T;
|
|
}
|
|
|
|
void KCache::growGenerate(const Mat& newData){
|
|
int cur_page = nTokens / pageSize;
|
|
const int Ps = fastGemmPackBSize(pageSize, headDim, opt);
|
|
int t0 = nTokens % pageSize;
|
|
const int batch_size = newData.size[0];
|
|
|
|
if (cur_page >= (int)pages.size()) {
|
|
pages.push_back(Mat({batchSize, nHeads, Ps}, CV_32F, Scalar(0)));
|
|
}
|
|
|
|
auto* page = pages[cur_page].ptr<float>();
|
|
const auto* data = newData.ptr<float>();
|
|
|
|
const int nstripes = batch_size * nHeads;
|
|
parallel_for_(Range(0, nstripes), [&](const Range& r) {
|
|
for (int i = r.start; i < r.end; i++) {
|
|
int b = i / nHeads, h = i % nHeads;
|
|
const float* src = data + (b * nHeads + h) * headDim;
|
|
float* dst = page + b * nHeads * Ps + h * Ps + t0;
|
|
for (int j = 0; j < headDim; j++)
|
|
dst[j * pageSize] = src[j];
|
|
}
|
|
});
|
|
|
|
nTokens += 1;
|
|
}
|
|
|
|
void VCache::growGenerate(const Mat& newData){
|
|
const int batch_size = newData.size[0];
|
|
const int Nr = fastGemmNR(opt);
|
|
const int Ps = fastGemmPackBSize(headDim, pageSize, opt);
|
|
const int t0 = nTokens % pageSize;
|
|
const int step_packed = pageSize * Nr;
|
|
int cur_page = nTokens / pageSize;
|
|
|
|
if (cur_page >= (int)pages.size()) {
|
|
pages.push_back(Mat({batchSize, nHeads, Ps}, CV_32F, Scalar(0)));
|
|
}
|
|
|
|
auto* page = pages[cur_page].ptr<float>();
|
|
const auto* data = newData.ptr<float>();
|
|
|
|
const int nstripes = batch_size * nHeads;
|
|
parallel_for_(Range(0, nstripes), [&](const Range& r) {
|
|
for (int i = r.start; i < r.end; i++) {
|
|
int b = i / nHeads, h = i % nHeads;
|
|
for (int j = 0; j <= (headDim - 1) / Nr; j++) {
|
|
int step = b * nHeads * headDim + h * headDim + j * Nr;
|
|
int copy_size = std::min(Nr, headDim - j * Nr);
|
|
|
|
auto* cur_page_ptr = page + b * nHeads * Ps + h * Ps + t0 * Nr + j * step_packed;
|
|
const float* src_ptr = data + step;
|
|
std::memcpy(cur_page_ptr, src_ptr, copy_size * sizeof(float));
|
|
if (copy_size < Nr) {
|
|
float replication_val = src_ptr[0];
|
|
for (int k = copy_size; k < Nr; k++) {
|
|
cur_page_ptr[k] = replication_val;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
nTokens += 1;
|
|
}
|
|
|
|
CV__DNN_INLINE_NS_END
|
|
}}
|