Compare commits

...
19 Commits
Author SHA1 Message Date
Pranesh GonegandlaandPranesh Gonegandla 8330e96967 spec : fix n-gram drafts rejected at temp > 0 after truncation (#29924)
Co-authored-by: Pranesh Gonegandla <pgonegandla@nvidia.com>
2026-10-04 11:45:22 +03:00
Adrien Gallouët 6716df694b common : prepare load_from_models_dir() for path conversion (#29674)
This is part of the fs::path modernization series.
That was also the opportunity to remove fs_list().

Signed-off-by: Adrien Gallouët <angt@huggingface.co>
2026-10-04 11:35:33 +03:00
Adrien Gallouët bf9a0ccce7 server : fix dead LLAMA_ARG_HF_REPO_FILE key in preset allow-list (#29938)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
2026-10-04 11:34:43 +03:00
Sigbjørn Skjæret 0faee50042 ci : pushing tag needs deploy key (#29937) 2026-10-04 09:45:49 +02:00
Sigbjørn Skjæret f98b31c67e ci : improve release flow (#29913)
* improve release flow

* fix copied typo

* fix permissions
2026-10-04 09:31:24 +02:00
Masashi Yoshimura 11fe02151f webgpu: add f16 support to fill/set_rows (#29897) 2026-10-04 09:07:47 +09:00
Adrien Gallouët 836d57176d mtmd : fix deprecated strdup warning on Windows (#29863)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
2026-10-03 19:47:54 +02:00
Alessandro de Oliveira Faria (A.K.A.CABELO) eec18f5d32 vendor : update cpp-httplib to 0.59.0 (#29886) 2026-10-03 19:08:47 +02:00
Nik Bogatyrev 1537a0a8b2 server : fix laya abort by limiting n_batch to n_ubatch (#29903)
* server : fix laya abort by limiting n_batch to n_ubatch

Fixes #29902

Assisted-by: Claude

* fix(review) : rm tests, embeddings cond
2026-10-03 17:19:13 +02:00
Adrien Gallouët edd6e2bbda common : add common_is_tty() helper and fix deprecated warnings on Windows (#29860)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
2026-10-03 15:48:24 +02:00
Yash Raj Pandey 9bf55f4a36 chat : honor json_schema in Ling 3.0 parser (#29813)
* chat : honor json_schema in Ling 3.0 parser

Ling 3.0 only built a grammar for tool calls and did not handle inputs.json_schema, so response_format requests were left unconstrained.

Add an eager response-format grammar path with precedence over tools, following the existing parser patterns. Require </think> before JSON when thinking is enabled and do not allow trailing prose after the JSON response.

Fixes #29652.

Assisted-by: Claude Opus 5.5

* chat : require Ling 3.0 think block for response formats
2026-10-03 15:40:50 +02:00
Pascal a55e952b85 ci: fix flaky ADD_ADD f16 by using the fused ADD tolerance (#29904) 2026-10-03 14:56:07 +02:00
Pascal 436f6f89e1 graph: gather the recurrent states once so the reserve covers every split (#29856)
build_rs gathered the extra states (n_rs - n_seqs rows) with their own
get_rows. The worst-case reserve has n_rs == n_seqs, so that node was
sized at zero rows, and any ubatch whose cells are not contiguous forced
a graph reallocation at an unchanged node count, which aborts under
GGML_SCHED_NO_REALLOC.

A single get_rows now gathers the n_rs states: the ubatch states and the
extra states are views of it, and its size only depends on n_rs, which
the reserve already sets to the maximum. A custom getter (mamba ssm_scan)
gathers from the second state, so a single sequence ubatch copies no
state. The views are built once per graph in the input to keep the host
overhead of the graph unchanged.
2026-10-03 14:02:05 +02:00
b92761a515 ggml-openvino: update to 2026.4.1, optimize performance, expand ops, improve device listing. (#29852)
* ggml-openvino : Qwen3.5 MoE perf (#312)

Squash of ravi9/llama.cpp#312:

- ggml-openvino: add detailed inference profiling (Yu, Zijun)
- ggml-openvino: use remote output tensors by default (Yu, Zijun)
- ggml-openvino: optimize single-sequence recurrent state (Yu, Zijun)
- opt1: remove recurrent reset for single sequence, opt2: direct gdn outputs (break parallel sequence) (Yu, Zijun)
- fix parallel sequences (Yu, Zijun)
- ggml-openvino: simplify graph cache key (ynimmaga)
- enable stateful for qwen35 single sequence (Yu, Zijun)
- Fix after rebasing (Yu, Zijun)
- Add k-requant option q4_asym64 (Yu, Zijun)
- Fix qwen35 llama-bench -p 0 (Yu, Zijun)
- Simplify RESHAPE translation (Yu, Zijun)
- openvino: fuse MoE routing (Yu, Zijun)
- openvino: fuse GDN qk normalization (Yu, Zijun)
- openvino: enable GPU MoE fusion by default (Yu, Zijun)
- ggml-openvino: add cache_only mode to import cached compiled model on disk directly (Yu, Zijun)
- openvino : report the device allocation limit to ggml (Łukasz Ślusarczyk)
- Fix windows build (Yu, Zijun)

Co-authored-by: ynimmaga <ynimmaga@users.noreply.github.com>
Co-authored-by: Łukasz Ślusarczyk <lukasz.slusarczyk@intel.com>

* ggml-openvino: Update doc of compiled model cache

* openvino: implement PRD-compliant device enumeration and memory reporting

* openvino: fix multi-device listing issues from review

- Only the device selected by GGML_OPENVINO_DEVICE reports as GPU; the
  other OpenVINO devices report as IGPU so llama.cpp does not offload to
  them. Initializing a non-selected device logs a warning.
- Name devices OPENVINO<i> again and show the OpenVINO id in the
  description. Raw "CPU" names shadowed the ggml CPU backend.
- Support GPU.N: create the OpenCL queue on OpenVINO's own context for
  the selected device, and replace "GPU"/"NPU" string comparisons with
  ggml_openvino_is_gpu()/ggml_openvino_is_npu().
- An unavailable GGML_OPENVINO_DEVICE is now an error that lists the
  available devices, instead of silently falling back to CPU.
- Memory: cap iGPU/NPU free memory at system available memory, fall back
  to system memory instead of 0/0 when the plugin lacks memory
  properties, and ignore host USM allocations in GPU usage.
- Initialize the device config once under a lock, even if OpenCL setup
  fails.
- Fix supports_op return type for non-selected devices (build error).

* openvino : take USM entry points from the selected device platform

clGetExtensionFunctionAddressForPlatform was called on the first platform
returned by clGetPlatformIDs. The address it returns is only valid for the
platform it was queried on, and the first platform is not always the one that
holds the device OpenVINO selected.

On a host whose first platform comes from another vendor the lookup returns
null, and then every read, write and memset on a GPU buffer fails with
"clEnqueueMemcpyINTEL not available".

Look both entry points up in init(), on the platform of the device OpenVINO
picked, and keep them in the device config next to the command queue.

Assisted-by: Claude Opus 5

* openvino: fuse MoE experts for models with a fused gate_up weight

FuseMoeCompressed only matches models whose gate and up projections are
separate GatherMatmul ops. gemma-4 packs both into one expert weight and
splits the result after the GEMM, so its MoE block stayed unfused and ran
the expert GEMMs as per-token GEMVs.

Add FuseMoeCompressedFusedGateUp, which matches that shape
(one GatherMatmul -> Slice/Slice -> Gelu(ERF) -> Multiply) and folds it into
the same MOECompressed op, using GEMM3_SWIGLU with GEGLU_ERF. The fused
weight, scale and zero point are split into gate/up halves by copying raw
bytes, since a graph Slice would be rewritten to StridedSlice and constant
folded, whose reference evaluator crashes on sub-byte types.

gemma-4 also applies a per-expert output scale to the down projection before
the router weights. MOECompressed takes only one per-expert weight, so that
scale is folded into the routing weights, which is exact.

The op reads the zero point straight off a weight port and needs an integer
Constant there, so the matcher requires one and leaves natively quantized
experts (exact f16 zp) to the unfused path.

gemma-4-26B-A4B on Arc B390, GGML_OPENVINO_REQUANT_KQUANT=q4_asym64_all,
llama-bench -p 512 -n 128 -r 2, against a GGML_OPENVINO_MOE_OP=0 baseline:
pp512 66.16 -> 1608.73 t/s, tg128 25.94 -> 26.46 t/s. Perplexity over 12
chunks is unchanged (1451.3 +/- 177.9 unfused vs 1427.6 +/- 175.1 fused).

No effect without that requant option, on models with separate gate/up
weights, or on CPU. test-backend-ops -b OPENVINO0 is unchanged by this
commit: two MUL_MAT_ID m_v cases fail, the same two on the unmodified base.

* openvino: fix rank-3 axis handling so MoE works under stateful execution

Stateful execution drops the leading size-1 batch dim, so OV tensors are rank
3 while GgmlOvDecoder::get_shape/get_stride still report GGML_MAX_DIMS=4
reversed entries. Several MoE ops derive OV axis indices straight from that
metadata, so they picked the wrong axis. A MoE model with
GGML_OPENVINO_STATEFUL_EXECUTION=1 aborts while building the graph:

  Check 'is_axis_valid(axis, r)' failed at src/core/src/validation_util.cpp:336
  While validating node 'opset11::TopK ... _ffn_moe_probs ...'
  Axis 3 out of the tensor rank range [-3, 2].

Fix idiom throughout: take the axis from the real OV rank, or shift a
metadata-derived axis down by metadata_rank - actual_rank.

  argsort.cpp    the router top-k axis is 2 on rank 3, not 3. This is the
                 abort quoted above.
  add.cpp        the MoE expert-sum bypass collapses the 8-ADD chain into one
                 ReduceSum on hardcoded axis 2, which on rank 3 reduces n_embd
                 instead of the expert axis. Now rank-2, with the following
                 Unsqueeze at rank-3.
  get_rows.cpp   squeezing a hardcoded {0,1} also strips the batch dim
                 whenever it is 1, which is every decode step. Squeeze down to
                 the trailing two dims instead.
  mul_mat_id.cpp pick the reshape dims by actual rank, and skip the trailing
                 Unsqueeze that re-adds the batch dim.
  view.cpp       the expert-plane slice had the Slice axis, dst_ov_axis, the
                 ShapeOf+Gather index and the Reshape target all rank-4.
  utils.cpp      process_view_input_new's "translate_view already resolved
                 this VIEW, skip re-slicing" shortcut required equal ranks. 4
                 vs 3 never matched, so every resolved expert plane got
                 re-sliced. Now compares the common trailing dims. Same axis
                 shift for the Slice in the view-chain walker.

Stateless is unchanged by construction: every edit is gated on the actual
rank, so axis_shift == 0 reproduces the previous code exactly. Checked on
OV-CPU by diffing greedy output against the unmodified base for dense
gemma-4-E2B, granite-1b-a400m and gemma-4-26B-A4B; all identical.

granite-1b-a400m on OV-CPU aborts with the error above before this change;
after it, it generates and is byte-identical to stateless. Dense gemma-4-E2B
is identical stateless vs stateful both before and after. test-backend-ops
-b OPENVINO0 is unchanged: two pre-existing MUL_MAT_ID m_v cases fail, the
same two on the unmodified base.

gemma-4-26B-A4B is a poor correctness vehicle here. On OV it already drifts
into degenerate repetition a few tokens in, in stateless as much as stateful,
and the two modes diverge somewhere inside that degenerate region instead of
matching token for token. Each mode is self-reproducible across runs.

Known limitation: FuseMoeCompressedFusedGateUp does not match the rank-3
graph, so a MoE model run with GGML_OPENVINO_STATEFUL_EXECUTION=1 loses the
prefill fusion while gaining decode. gemma-4-26B-A4B on Arc B390,
GGML_OPENVINO_REQUANT_KQUANT=q4_asym64_all, llama-bench -p 512 -n 128 -r 2:

  unfused (GGML_OPENVINO_MOE_OP=0)  pp512   66.16   tg128  25.94
  fused, stateless (default)        pp512 1608.73   tg128  26.46
  fused, stateful                   pp512   66.18   tg128  29.91

Stateful is opt-in and off by default, and MoE did not run there at all
before this, so nothing that previously worked regresses. Making the pass
match rank 3 is the follow-up.

* OpenVINO Backend: Upgrade graph cache to use node_idx, src_idx, node type

* ggml-openvino : enable more comprehensive conv fusion

* enable conv ops

* Reject kernel size 0 and support IM2COL_3D

* openvino : abort when the GPU remote context cannot be created

init() logged the error and returned, which left the device name a GPU but
remote_context empty. The remote buffer and tensor paths assert only on the
device being a GPU and then dereference that empty optional.

Those paths have no host fallback, and a device that OpenVINO listed should
have a working OpenCL context, so stop instead of continuing. An OpenCL stack
that is broken as a whole is still caught earlier by the device availability
check, which falls back to CPU.

Assisted-by: Claude Opus 5

* openvino : fix build warnings

The single-argument form of the OpenVINO RTTI macros is the intended one, but
their selector macro leaves __VA_ARGS__ empty, which -Wpedantic reports on
every pass and op header. Turn that warning off for this backend only, the
way ggml-cuda and ggml-sycl already do for their own third-party warnings.

Also drop a break and a dead assignment around a GGML_ABORT, which is noreturn.

Assisted-by: Claude Opus 5

* OpenVINO Backend: Support common MTMD ops

* ggml-openvino: give a reshaping view its own ov::Tensor

* ggml-openvino : compute HARDSIGMOID and EXPM1 in f32

HARDSIGMOID used a 1/6 constant in the input type, which is not exact
in bf16, and EXPM1 lost precision for small inputs in f16. Both now
compute in f32 and convert back, except on NPU where the f32 path
gives wrong results.

Fixes the HARDSIGMOID/EXPM1 test-backend-ops failures on GPU.

* ggml-openvino : update device selection and --list-devices

Show the selecting GGML_OPENVINO_DEVICE value and active device in
--list-devices, startup logs, and backend tests.

Clarify OpenVINO selection uses GGML_OPENVINO_DEVICE, not -dev.

* openvino : remove unreachable OpenCL queue checks

A remote buffer exists only on a GPU device, and init() aborts there if the
queue cannot be created, so the queue is never null at these call sites.

Assisted-by: Claude Opus 5

* openvino : update OpenVINO to 2026.4.1 and GPU drivers to 26.35.39758.10

* docs : update OpenVINO validated models and GPU driver version

* ggml-openvino : skip empty views when giving a reshaping view its own tensor

A zero-size view can sit at the end of a GPU USM buffer (Qwen3.5 recurrent cache). Wrapping it as a remote tensor throws "shared USM buffer has smaller size (0)".

Assisted-by: Claude

* ggml-openvino : rebind the cached decoder when llama passes a different graph

llama keeps separate graphs for batches with and without outputs. llama-server splits the prompt into chunks for context checkpoints, so a cached decoder could be reused with a graph built in other memory and bind the previous chunk's input tensors. SWA and recurrent models then lost most of the prompt in llama-cli and llama-server.

Assisted-by: Claude

* docs : update OpenVINO validated models

Smoke test on Lunar Lake (32 GB) with the two fixes above. Re-add the Qwen3.5 and gemma models.

Assisted-by: Claude

---------

Co-authored-by: Yu, Zijun <zijun.yu@intel.com>
Co-authored-by: ynimmaga <ynimmaga@users.noreply.github.com>
Co-authored-by: Łukasz Ślusarczyk <lukasz.slusarczyk@intel.com>
Co-authored-by: haarika-madaka <haarika.madaka@intel.com>
Co-authored-by: Mustafa Cavus <mustafa.cavus@intel.com>
Co-authored-by: Mostafa Faheem <mostafaaafaheem@gmail.com>
2026-10-03 11:59:25 +03:00
Tarek Dakhran cb7934c52c model : Add LFM2.5-Encoder-350M and LFM2.5-Encoder-230M (#29862)
Register `Lfm2BidirectionalForMaskedLM` architecture for LFM2.5-Encoder
models.
2026-10-03 08:44:45 +02:00
PascalandRuben Ortlam 889edf43dd qwen4exp : halve the indexer score memory (#29825)
* qwen4exp : halve the indexer score memory

The indexer scored all heads in one product and rectified a copy of it,
so two [n_pool, n_idx_h, n_tokens] f32 tensors were live at once, the
largest buffers of the graph at long context. Each head now gets its
own product, rectified and summed in place into one [n_pool, n_tokens]
score.

* qwen4exp: let the allocator reuse the indexer score buffers

Address review from CISC: use plain ggml_add and ggml_relu in the
indexer head loop. The graph allocator already runs them in place when
their source has no other consumer, so the _inplace variants are not
needed. The compute buffer and the speed are unchanged.

* cuda: support 4 heads in the lightning indexer

Dispatch 4 heads to the vector kernel, too few for a wmma tile, and
accept them in supports_op. test-backend-ops covers 4 heads.

* metal: take the lightning indexer head count as a function constant

The kernel reads the head count from a function constant and zero fills
the last head tile, so any head count runs and 64 heads is unchanged.

* qwen4exp: compute the indexer score with the lightning indexer

Address review from am17an: the unweighted sum of the rectified head
scores scaled by 1/sqrt(head_dim) is the lightning indexer with every
head weight set to that scale, so the indexer calls
ggml_lightning_indexer on the pooled keys with an f16 pool mask. The
keys are read once for all heads and no per head score is
materialized.

* vulkan: tile the lightning indexer over keys and tokens

A workgroup scores 64 keys against 8 tokens: the keys are staged once
in shared memory, the queries one head at a time, and each invocation
owns one key for two tokens, so no dot product needs a cross invocation
reduction. The subgroup variant and the flat dispatch are gone, the grid
is keys x tokens x streams.

* vectorize vulkan loads and use fp16 dot product

---------

Co-authored-by: Ruben Ortlam <rortlam@redhat.com>
2026-10-03 07:19:00 +02:00
Xuan-Son NguyenandSigbjørn Skjæret 99b95488ca model: add support for clef decision model (text-only) (#29831)
* init support for clef (text only)

* more static graph

* clean up

* nits

* nits 2

* Update gguf-py/gguf/constants.py

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>

---------

Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@huggingface.co>
2026-10-03 02:50:48 +02:00
Aman Gupta bed0a85660 CUDA: fuse shared experts into MMVQ (#29184)
* CUDA: fuse shared experts into MMVQ

* check if buffer is null

* move stride_col_dst to fusion args
2026-10-02 21:26:27 +03:00
Sigbjørn Skjæret 4ebdf2c74a ci : use t4-medium for cuda jobs (#29842)
[no ci]
2026-10-02 17:31:19 +02:00
112 changed files with 6160 additions and 1589 deletions
+6 -6
View File
@@ -1,12 +1,12 @@
ARG OPENVINO_VERSION_MAJOR=2026.4
ARG OPENVINO_VERSION_FULL=2026.4.0.22959.99c81491cc3
ARG OPENVINO_VERSION_MAJOR=2026.4.1
ARG OPENVINO_VERSION_FULL=2026.4.1.22982.07f9c262b05
ARG UBUNTU_VERSION=24.04
# Intel GPU driver versions. https://github.com/intel/compute-runtime/releases
ARG IGC_VERSION=v2.40.13
ARG IGC_VERSION_FULL=2_2.40.13+22418
ARG COMPUTE_RUNTIME_VERSION=26.31.39395.13
ARG COMPUTE_RUNTIME_VERSION_FULL=26.31.39395.13-0
ARG IGC_VERSION=v2.41.5
ARG IGC_VERSION_FULL=2_2.41.5+22716
ARG COMPUTE_RUNTIME_VERSION=26.35.39758.10
ARG COMPUTE_RUNTIME_VERSION_FULL=26.35.39758.10-0
ARG IGDGMM_VERSION=22.10.0
# Intel NPU driver versions. https://github.com/intel/linux-npu-driver/releases
+4 -4
View File
@@ -41,8 +41,8 @@ jobs:
env:
# Sync versions in build-openvino.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Clone
@@ -69,8 +69,8 @@ jobs:
env:
# Sync versions in build.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Clone
+4 -4
View File
@@ -41,8 +41,8 @@ jobs:
env:
# Sync versions in build-openvino.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Clone
@@ -96,8 +96,8 @@ jobs:
env:
# Sync versions in build-openvino.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Clone
+1 -1
View File
@@ -45,7 +45,7 @@ env:
jobs:
gpu-cuda:
runs-on: "hf-jobs-t4-small:cuda13"
runs-on: "hf-jobs-t4-medium:cuda13"
steps:
- name: Clone
@@ -47,8 +47,8 @@ jobs:
env:
# Sync versions in build.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Clone
+239
View File
@@ -0,0 +1,239 @@
name: Publish Release
on:
workflow_run:
workflows:
- Release
types:
- completed
branches:
- master
env:
GH_TOKEN: ${{ github.token }}
BRANCH_NAME: master
jobs:
publish:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
# Fine-grained permission
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
permissions:
actions: read
contents: write # for creating release
id-token: write
attestations: write
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.check.outputs.should_release }}
tag_name: ${{ steps.tag.outputs.name }}
steps:
- id: check
env:
COMMIT_MESSAGE: ${{ github.event.workflow_run.head_commit.message }}
run: |
if echo "$COMMIT_MESSAGE" | grep -q '\[no release\]'; then
echo "should_release=false" >> $GITHUB_OUTPUT
else
echo "should_release=true" >> $GITHUB_OUTPUT
fi
- name: Clone
if: ${{ steps.check.outputs.should_release == 'true' }}
id: checkout
uses: actions/checkout@v6
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
ssh-key: ${{ secrets.DEPLOY_KEY_RELEASE }}
- name: Determine tag name
if: ${{ steps.check.outputs.should_release == 'true' }}
id: tag
uses: ./.github/actions/get-tag-name
- name: Download artifacts
if: ${{ steps.check.outputs.should_release == 'true' }}
id: download-artifact
uses: actions/download-artifact@v8
with:
path: ./artifact
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
merge-multiple: true
skip-decompress: true
- name: Merge artifacts
if: ${{ steps.check.outputs.should_release == 'true' }}
id: move_artifacts
run: |
mkdir -p release
# the windows-cpu zip contains the full toolset (llama-server with the embedded
# UI, ggml-cpu) - inject it into the other windows zips so that every archive
# ships the same binaries, only with a different backend library on top
echo "Injecting windows-cpu binaries (llama-server + CPU backend) into the backend zips..."
for arch in x64 arm64; do
cpu_zip="artifact/llama-bin-win-cpu-${arch}.zip"
temp_dir=$(mktemp -d)
echo "Extracting windows-cpu-${arch} package..."
unzip "$cpu_zip" -d "$temp_dir"
echo "Merging into $arch zips..."
for target_zip in artifact/llama-bin-win-*-${arch}.zip; do
if [[ "$target_zip" == "$cpu_zip" ]]; then
continue
fi
echo "Injecting into $(basename "$target_zip")"
realpath_target_zip=$(realpath "$target_zip")
(cd "$temp_dir" && zip -r "$realpath_target_zip" .)
done
rm -rf "$temp_dir"
done
echo "Renaming and moving zips to release..."
for zip_file in artifact/llama-bin-win-*.zip; do
base_name=$(basename "$zip_file" .zip)
zip_name="llama-${{ steps.tag.outputs.name }}-${base_name#llama-}.zip"
echo "Moving $zip_file to release/$zip_name"
mv "$zip_file" "release/$zip_name"
done
echo "Moving other artifacts..."
rm -f artifact/llama-ui.zip
mv -v artifact/*.zip release
mv -v artifact/*.tar.gz release
- name: Download UI build
if: ${{ steps.check.outputs.should_release == 'true' }}
id: download_ui
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: ./ui-dist
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Package UI
if: ${{ steps.check.outputs.should_release == 'true' }}
id: package_ui
run: |
tar -czvf release/llama-${{ steps.tag.outputs.name }}-ui.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./ui-dist .
- name: Attest release artifacts
if: ${{ steps.check.outputs.should_release == 'true' }}
id: attest
uses: actions/attest@v4
with:
subject-path: 'release/*'
- name: Create release
if: ${{ steps.check.outputs.should_release == 'true' }}
id: create_release
uses: ggml-org/action-create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag.outputs.name }}
commitish: ${{ github.event.workflow_run.head_sha }}
prerelease: true
body: |
<details open>
${{ github.event.workflow_run.head_commit.message }}
</details>
**Website:**
- <https://llama.app>
**Attestations:**
- <${{ steps.attest.outputs.attestation-url }}>
**macOS/iOS:**
- [macOS Apple Silicon (arm64)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-macos-arm64.tar.gz)
- macOS Apple Silicon (arm64, KleidiAI enabled) [DISABLED](https://github.com/ggml-org/llama.cpp/pull/23780)
- [macOS Intel (x64)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-macos-x64.tar.gz)
- [iOS XCFramework](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-xcframework.zip)
**Linux:**
- [Ubuntu x64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-x64.tar.gz)
- [Ubuntu arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-arm64.tar.gz)
- [Ubuntu s390x (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-s390x.tar.gz)
- [Ubuntu x64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-x64.tar.gz)
- [Ubuntu arm64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-arm64.tar.gz)
- [Ubuntu x64 (CUDA 12)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-12.8-x64.tar.gz) - [CUDA 12.8 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-12.8-x64.tar.gz)
- [Ubuntu x64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-x64.tar.gz) - [CUDA 13.4 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-x64.tar.gz)
- [Ubuntu arm64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-arm64.tar.gz) - [CUDA 13.4 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-arm64.tar.gz)
- [Ubuntu x64 (ROCm 10.0)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-rocm-10.0-x64.tar.gz)
- [Ubuntu x64 (OpenVINO)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-openvino-${{ needs.ubuntu-24-openvino.outputs.openvino_version }}-x64.tar.gz)
- [Ubuntu x64 (SYCL FP32)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-fp32-x64.tar.gz)
- [Ubuntu x64 (SYCL FP16)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-fp16-x64.tar.gz)
- [Linux arm64 (Snapdragon: CPU, Adreno GPU, Hexagon NPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-linux-arm64-snapdragon.tar.gz) - [setup guide](https://github.com/ggml-org/llama.cpp/blob/master/docs/backend/snapdragon/linux.md)
**Android:**
- [Android arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-android-arm64.tar.gz)
- [Android arm64 (Snapdragon: CPU, Adreno GPU, Hexagon NPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-android-arm64-snapdragon.tar.gz) - [setup guide](https://github.com/ggml-org/llama.cpp/blob/master/docs/backend/snapdragon/README.md)
**Windows:**
- [Windows x64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cpu-x64.zip)
- [Windows arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cpu-arm64.zip)
- [Windows arm64 (OpenCL Adreno)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-opencl-adreno-arm64.zip)
- [Windows x64 (CUDA 12)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-12.4-x64.zip) - [CUDA 12.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-12.4-x64.zip)
- [Windows x64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-13.4-x64.zip) - [CUDA 13.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-13.4-x64.zip)
- [Windows arm64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-13.4-arm64.zip) - [CUDA 13.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-13.4-arm64.zip)
- [Windows x64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-vulkan-x64.zip)
- [Windows x64 (OpenVINO)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-openvino-${{ needs.windows-openvino.outputs.openvino_version }}-x64.zip)
- [Windows x64 (SYCL)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-sycl-x64.zip)
- [Windows x64 (ROCm 10.0)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-rocm-10.0-x64.zip)
**openEuler:**
- [DISABLED](https://github.com/ggml-org/llama.cpp/pull/23705)
- openEuler x86 (310p)
- openEuler x86 (910b, ACL Graph)
- openEuler aarch64 (310p)
- openEuler aarch64 (910b, ACL Graph)
**UI:**
- [UI](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-ui.tar.gz)
- name: Upload release
if: ${{ steps.check.outputs.should_release == 'true' }}
id: upload_release
uses: actions/github-script@v8
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const path = require('path');
const fs = require('fs');
const release_id = '${{ steps.create_release.outputs.id }}';
for (let file of await fs.readdirSync('./release')) {
if (path.extname(file) === '.zip' || file.endsWith('.tar.gz')) {
console.log('uploadReleaseAsset', file);
await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release_id,
name: file,
data: await fs.readFileSync(`./release/${file}`)
});
}
}
ui-publish:
if: ${{ needs.publish.outputs.should_release == 'true' }}
needs:
- publish
uses: ./.github/workflows/ui-publish.yml
with:
version_tag: ${{ needs.publish.outputs.tag_name }}
run_id: ${{ github.event.workflow_run.id }}
secrets:
hf_token: ${{ secrets.HF_TOKEN_UI_STATIC_OUTPUT }}
+138 -369
View File
@@ -41,8 +41,12 @@ jobs:
check-release:
runs-on: ubuntu-slim
permissions:
contents: write
outputs:
should_release: ${{ steps.check.outputs.should_release }}
tag_name: ${{ steps.tag.outputs.name }}
steps:
- id: check
@@ -61,6 +65,30 @@ jobs:
echo "should_release=false" >> $GITHUB_OUTPUT
fi
- name: Clone
if: ${{ steps.check.outputs.should_release == 'true' }}
id: checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
ssh-key: ${{ secrets.DEPLOY_KEY_RELEASE }}
- name: Determine tag name
if: ${{ steps.check.outputs.should_release == 'true' }}
id: tag
uses: ./.github/actions/get-tag-name
- name: Create and push git tag
if: ${{ steps.check.outputs.should_release == 'true' }}
run: |
TAG="${{ steps.tag.outputs.name }}"
if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null 2>&1; then
echo "Tag ${TAG} already exists, skipping creation"
else
git tag "${TAG}"
git push origin "${TAG}"
fi
macos-cpu:
needs: [check-release, ui-build]
if: ${{ needs.check-release.outputs.should_release == 'true' }}
@@ -97,7 +125,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -121,21 +149,17 @@ jobs:
${{ env.CMAKE_ARGS }}
cmake --build build --config Release -j $(sysctl -n hw.logicalcpu)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-macos-${{ matrix.build }}.tar.gz -s ",^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-macos-${{ matrix.build }}.tar.gz -s ",^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-macos-${{ matrix.build }}.tar.gz
name: llama-bin-macos-${{ matrix.build }}.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-macos-${{ matrix.build }}.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -168,7 +192,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -206,21 +230,17 @@ jobs:
${{ env.CMAKE_ARGS }}
cmake --build build --config Release -j $(nproc)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-${{ matrix.build }}.tar.gz
name: llama-bin-ubuntu-${{ matrix.build }}.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-${{ matrix.build }}.tar.gz
archive: false
- name: ccache-clear
if: ${{ matrix.build != 's390x' }}
@@ -253,7 +273,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -292,21 +312,17 @@ jobs:
${{ env.CMAKE_ARGS }}
cmake --build build --config Release -j $(nproc)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-vulkan-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-${{ matrix.build }}.tar.gz
name: llama-bin-ubuntu-vulkan-${{ matrix.build }}.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-vulkan-${{ matrix.build }}.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -347,8 +363,7 @@ jobs:
actions: write
steps:
# the container has no git; install it before checkout so that a real git
# repository is created (the get-tag-name action and the build both need it)
# the container has no git; install it before checkout so that a real git repository is created
- name: Install git
run: |
apt-get update
@@ -368,7 +383,7 @@ jobs:
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -411,21 +426,17 @@ jobs:
${{ env.CMAKE_ARGS }} ${{ matrix.defines }}
cmake --build build --config Release -j $(nproc)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
name: llama-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
archive: false
# ship the CUDA runtime libraries the backend links against, mirroring
# the windows-cuda cudart zip - extract next to the binaries ($ORIGIN rpath)
@@ -440,13 +451,13 @@ jobs:
cp -L /usr/local/cuda/lib64/libcudart.so.${major} ./cudart/
cp -L /usr/local/cuda/lib64/libcublas.so.${major} ./cudart/
cp -L /usr/local/cuda/lib64/libcublasLt.so.${major} ./cudart/
tar -czvf cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz --transform "s,^\.,cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}," -C ./cudart .
tar -czvf cudart-llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz --transform "s,^\.,cudart-llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}," -C ./cudart .
- name: Upload CUDA runtime
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
name: cudart-llama-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
path: cudart-llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-cuda-${{ matrix.label }}-${{ matrix.build }}.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -459,8 +470,8 @@ jobs:
runs-on: ubuntu-24.04 # previously ubuntu-latest
#permissions:
# actions: write
permissions:
actions: write
env:
NDK_VERSION: "29.0.14206865"
@@ -473,7 +484,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -529,21 +540,17 @@ jobs:
# with:
# key: release-android-arm64
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-android-arm64.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-android-arm64.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-android-arm64.tar.gz
name: llama-bin-android-arm64.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-android-arm64.tar.gz
archive: false
android-arm64-snapdragon:
needs: [check-release, ui-build]
@@ -552,6 +559,9 @@ jobs:
runs-on: ubuntu-latest
container: 'ghcr.io/snapdragon-toolchain/arm64-android:v0.7'
permissions:
actions: write
defaults:
run:
shell: bash
@@ -569,7 +579,7 @@ jobs:
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -586,21 +596,17 @@ jobs:
cmake --build build -j $(nproc)
cmake --install build --prefix pkg-snapdragon/llama.cpp
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE pkg-snapdragon/llama.cpp/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-android-arm64-snapdragon.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C pkg-snapdragon/llama.cpp .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-android-arm64-snapdragon.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C pkg-snapdragon/llama.cpp .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-android-arm64-snapdragon.tar.gz
name: llama-bin-android-arm64-snapdragon.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-android-arm64-snapdragon.tar.gz
archive: false
linux-arm64-snapdragon:
needs: [check-release, ui-build]
@@ -609,6 +615,9 @@ jobs:
runs-on: ubuntu-latest
container: 'ghcr.io/snapdragon-toolchain/arm64-linux:v0.7'
permissions:
actions: write
defaults:
run:
shell: bash
@@ -626,7 +635,7 @@ jobs:
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -643,21 +652,17 @@ jobs:
cmake --build build -j $(nproc)
cmake --install build --prefix pkg-snapdragon/llama.cpp
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE pkg-snapdragon/llama.cpp/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-linux-arm64-snapdragon.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C pkg-snapdragon/llama.cpp .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-linux-arm64-snapdragon.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C pkg-snapdragon/llama.cpp .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-linux-arm64-snapdragon.tar.gz
name: llama-bin-linux-arm64-snapdragon.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-linux-arm64-snapdragon.tar.gz
archive: false
ubuntu-24-openvino:
needs: [check-release, ui-build]
@@ -673,8 +678,8 @@ jobs:
env:
# Sync versions in build-openvino.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Set OpenVINO version output
@@ -688,7 +693,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -738,10 +743,6 @@ jobs:
${{ env.CMAKE_ARGS }}
cmake --build build/ReleaseOV --config Release --parallel
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
@@ -764,13 +765,13 @@ jobs:
cp -r "$OPENVINO_ROOT"/docs/licensing "$dest"/openvino-licensing
cp LICENSE "$dest"
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C "$dest" .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C "$dest" .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.tar.gz
name: llama-bin-ubuntu-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -783,13 +784,16 @@ jobs:
runs-on: windows-2022
permissions:
actions: write
outputs:
openvino_version: ${{ steps.openvino_version.outputs.value }}
env:
# Sync versions in build-openvino.yml, build-self-hosted.yml, release.yml, build-cache.yml, .devops/openvino.Dockerfile
OPENVINO_VERSION_MAJOR: "2026.4"
OPENVINO_VERSION_FULL: "2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR: "2026.4.1"
OPENVINO_VERSION_FULL: "2026.4.1.22982.07f9c262b05"
steps:
- name: Set OpenVINO version output
@@ -804,7 +808,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -862,10 +866,6 @@ jobs:
cmake --build build\ReleaseOV --config Release -- /m
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
shell: powershell
@@ -892,13 +892,13 @@ jobs:
Copy-Item -Path (Join-Path $OPENVINO_ROOT 'docs\licensing\*') -Destination $licensingDest -Recurse -Force
Copy-Item LICENSE $dest
7z a -snl llama-${{ steps.tag.outputs.name }}-bin-win-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.zip $dest\*
7z a -snl llama-${{ needs.check-release.outputs.tag_name }}-bin-win-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.zip $dest\*
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-win-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.zip
name: llama-bin-win-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.zip
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-win-openvino-${{ env.OPENVINO_VERSION_MAJOR }}-x64.zip
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -928,7 +928,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -964,10 +964,10 @@ jobs:
7z a -snl llama-bin-win-cpu-${{ matrix.arch }}.zip .\build\bin\Release\*
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-bin-win-cpu-${{ matrix.arch }}.zip
name: llama-bin-win-cpu-${{ matrix.arch }}.zip
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -982,6 +982,9 @@ jobs:
runs-on: windows-2022
permissions:
actions: write
strategy:
matrix:
include:
@@ -1080,10 +1083,6 @@ jobs:
Write-Host "HIP backend artifact found:"
$hipDll | Format-Table FullName, Length -AutoSize
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Get ROCm short version
run: |
$rocmVersionShort = ('${{ matrix.ROCM_VERSION }}'.Split('.')[0..1] -join '.')
@@ -1125,10 +1124,10 @@ jobs:
.\build\bin\Release\amd_comgr.dll
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-bin-win-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.zip
name: llama-bin-win-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.zip
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -1225,10 +1224,10 @@ jobs:
7z a -snl llama-bin-win-${{ matrix.backend }}-${{ matrix.arch }}.zip .\build\bin\Release\${{ matrix.target }}.dll
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-bin-win-${{ matrix.backend }}-${{ matrix.arch }}.zip
name: llama-bin-win-${{ matrix.backend }}-${{ matrix.arch }}.zip
archive: false
# note: builds only the ggml-cuda backend - llama-server is injected from the
# windows-cpu zip during the release "Merge artifacts" step
@@ -1298,10 +1297,10 @@ jobs:
7z a -snl llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip .\build\bin\Release\ggml-cuda.dll
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip
name: llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip
archive: false
- name: Copy and pack Cuda runtime (x64)
if: ${{ matrix.arch == 'x64' }}
@@ -1322,10 +1321,10 @@ jobs:
7z a cudart-llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip $dst\*
- name: Upload Cuda runtime
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: cudart-llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip
name: cudart-llama-bin-win-cuda-${{ matrix.cuda }}-${{ matrix.arch }}.zip
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -1340,6 +1339,9 @@ jobs:
runs-on: windows-2022
permissions:
actions: write
defaults:
run:
shell: bash
@@ -1428,10 +1430,10 @@ jobs:
7z a -snl llama-bin-win-sycl-x64.zip ./build/bin/*
- name: Upload the release package
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-bin-win-sycl-x64.zip
name: llama-bin-win-sycl-x64.zip
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -1453,6 +1455,9 @@ jobs:
runs-on: ubuntu-24.04
permissions:
actions: write
env:
ONEAPI_ROOT: /opt/intel/oneapi/
ONEAPI_INSTALLER_VERSION: "2026.1"
@@ -1482,7 +1487,7 @@ jobs:
sudo apt-get install -y ./libze1.deb ./libze-dev.deb
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -1510,21 +1515,17 @@ jobs:
-DGGML_SYCL_F16=${{ matrix.fp16 }}
time cmake --build build --config Release -j $(nproc)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-${{ matrix.build }}-x64.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-sycl-${{ matrix.build }}-x64.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-${{ matrix.build }}-x64.tar.gz
name: llama-bin-ubuntu-sycl-${{ matrix.build }}-x64.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-sycl-${{ matrix.build }}-x64.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -1555,7 +1556,7 @@ jobs:
fetch-depth: 0
- name: Download UI build
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist
@@ -1634,10 +1635,6 @@ jobs:
${{ env.CMAKE_ARGS }}
cmake --build build --config Release -j $(nproc)
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Get ROCm short version
run: echo "ROCM_VERSION_SHORT=$(echo '${{ matrix.ROCM_VERSION }}' | cut -d '.' -f 1,2)" >> $GITHUB_ENV
@@ -1645,13 +1642,13 @@ jobs:
id: pack_artifacts
run: |
cp LICENSE ./build/bin/
tar -czvf llama-${{ steps.tag.outputs.name }}-bin-ubuntu-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-bin-ubuntu-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.tar.gz
name: llama-bin-ubuntu-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.tar.gz
path: llama-${{ needs.check-release.outputs.tag_name }}-bin-ubuntu-rocm-${{ env.ROCM_VERSION_SHORT }}-${{ matrix.build }}.tar.gz
archive: false
- name: ccache-clear
uses: ./.github/actions/ccache-clear
@@ -1663,6 +1660,9 @@ jobs:
if: ${{ needs.check-release.outputs.should_release == 'true' }}
runs-on: macos-26
permissions:
actions: write
steps:
- name: Checkout code
uses: actions/checkout@v6
@@ -1700,22 +1700,18 @@ jobs:
- name: Build Xcode project
run: xcodebuild -project examples/llama.swiftui/llama.swiftui.xcodeproj -scheme llama.swiftui -sdk iphoneos CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= -destination 'generic/platform=iOS' FRAMEWORK_FOLDER_PATH=./build-ios build
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Pack artifacts
id: pack_artifacts
run: |
# Zip file is required for Swift Package Manager, which does not support tar.gz for binary targets.
# For more details, see https://developer.apple.com/documentation/xcode/distributing-binary-frameworks-as-swift-packages
zip -r -y llama-${{ steps.tag.outputs.name }}-xcframework.zip build-apple/llama.xcframework
zip -r -y llama-${{ needs.check-release.outputs.tag_name }}-xcframework.zip build-apple/llama.xcframework
- name: Upload artifacts
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
path: llama-${{ steps.tag.outputs.name }}-xcframework.zip
name: llama-${{ steps.tag.outputs.name }}-xcframework.zip
path: llama-${{ needs.check-release.outputs.tag_name }}-xcframework.zip
archive: false
# TODO: this build is disabled to save Github Actions resources (https://github.com/ggml-org/llama.cpp/pull/23705)
# in order to enable it again, we have to provision dedicated runners to run it
@@ -1742,6 +1738,8 @@ jobs:
# build: 'Release'
# use_acl_graph: 'off'
# runs-on: ${{ matrix.arch == 'aarch64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }}
# permissions:
# actions: write
# steps:
# - name: Checkout
# uses: actions/checkout@v6
@@ -1794,247 +1792,18 @@ jobs:
# chown -R '"${HOST_UID}"':'"${HOST_GID}"' /workspace/build
# '
#
# - name: Determine tag name
# id: tag
# uses: ./.github/actions/get-tag-name
#
# - name: Pack artifacts
# run: |
# cp LICENSE ./build/bin/
# tar -czvf llama-${{ steps.tag.outputs.name }}-bin-${{ matrix.chip_type }}-openEuler-${{ matrix.arch }}${{ matrix.use_acl_graph == 'on' && '-aclgraph' || '' }}.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./build/bin .
# tar -czvf llama-${{ needs.check-release.outputs.tag_name }}-bin-${{ matrix.chip_type }}-openEuler-${{ matrix.arch }}${{ matrix.use_acl_graph == 'on' && '-aclgraph' || '' }}.tar.gz --transform "s,^\.,llama-${{ needs.check-release.outputs.tag_name }}," -C ./build/bin .
#
# - name: Upload artifacts
# uses: actions/upload-artifact@v6
# uses: actions/upload-artifact@v7
# with:
# path: llama-${{ steps.tag.outputs.name }}-bin-${{ matrix.chip_type }}-openEuler-${{ matrix.arch }}${{ matrix.use_acl_graph == 'on' && '-aclgraph' || '' }}.tar.gz
# name: llama-bin-${{ matrix.chip_type }}-openEuler-${{ matrix.arch }}${{ matrix.use_acl_graph == 'on' && '-aclgraph' || '' }}.tar.gz
# path: llama-${{ needs.check-release.outputs.tag_name }}-bin-${{ matrix.chip_type }}-openEuler-${{ matrix.arch }}${{ matrix.use_acl_graph == 'on' && '-aclgraph' || '' }}.tar.gz
# archive: false
ui-build:
needs: [check-release]
if: ${{ needs.check-release.outputs.should_release == 'true' }}
uses: ./.github/workflows/ui-build.yml
release:
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
# Fine-grant permission
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
permissions:
contents: write # for creating release
id-token: write
attestations: write
runs-on: ubuntu-slim
needs:
- windows
- windows-cpu
- windows-cuda
- windows-sycl
- windows-rocm
- windows-openvino
- ubuntu-24-rocm
- ubuntu-cpu
- ubuntu-vulkan
- ubuntu-cuda
- ubuntu-24-openvino
- ubuntu-24-sycl
- android-arm64
- android-arm64-snapdragon
- linux-arm64-snapdragon
- macos-cpu
- ios-xcode
#- openEuler-cann
- ui-build
outputs:
tag_name: ${{ steps.tag.outputs.name }}
steps:
- name: Clone
id: checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
ssh-key: ${{ secrets.DEPLOY_KEY_RELEASE }}
- name: Determine tag name
id: tag
uses: ./.github/actions/get-tag-name
- name: Download artifacts
id: download-artifact
uses: actions/download-artifact@v7
with:
path: ./artifact
merge-multiple: true
- name: Merge artifacts
id: move_artifacts
run: |
mkdir -p release
# the windows-cpu zip contains the full toolset (llama-server with the embedded
# UI, ggml-cpu) - inject it into the other windows zips so that every archive
# ships the same binaries, only with a different backend library on top
echo "Injecting windows-cpu binaries (llama-server + CPU backend) into the backend zips..."
for arch in x64 arm64; do
cpu_zip="artifact/llama-bin-win-cpu-${arch}.zip"
temp_dir=$(mktemp -d)
echo "Extracting windows-cpu-${arch} package..."
unzip "$cpu_zip" -d "$temp_dir"
echo "Merging into $arch zips..."
for target_zip in artifact/llama-bin-win-*-${arch}.zip; do
if [[ "$target_zip" == "$cpu_zip" ]]; then
continue
fi
echo "Injecting into $(basename "$target_zip")"
realpath_target_zip=$(realpath "$target_zip")
(cd "$temp_dir" && zip -r "$realpath_target_zip" .)
done
rm -rf "$temp_dir"
done
echo "Renaming and moving zips to release..."
for zip_file in artifact/llama-bin-win-*.zip; do
base_name=$(basename "$zip_file" .zip)
zip_name="llama-${{ steps.tag.outputs.name }}-${base_name#llama-}.zip"
echo "Moving $zip_file to release/$zip_name"
mv "$zip_file" "release/$zip_name"
done
echo "Moving other artifacts..."
mv -v artifact/*.zip release
mv -v artifact/*.tar.gz release
- name: Download UI build
id: download_ui
uses: actions/download-artifact@v7
with:
name: llama-ui.zip
path: ./ui-dist
- name: Package UI
id: package_ui
run: |
tar -czvf release/llama-${{ steps.tag.outputs.name }}-ui.tar.gz --transform "s,^\.,llama-${{ steps.tag.outputs.name }}," -C ./ui-dist .
- name: Attest release artifacts
id: attest
uses: actions/attest@v4
with:
subject-path: 'release/*'
- name: Create and push git tag
run: |
TAG="${{ steps.tag.outputs.name }}"
if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null 2>&1; then
echo "Tag ${TAG} already exists, skipping creation"
else
git tag "${TAG}"
git push origin "${TAG}"
fi
- name: Create release
id: create_release
uses: ggml-org/action-create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag.outputs.name }}
prerelease: true
body: |
<details open>
${{ github.event.head_commit.message }}
</details>
**Website:**
- <https://llama.app>
**Attestations:**
- <${{ steps.attest.outputs.attestation-url }}>
**macOS/iOS:**
- [macOS Apple Silicon (arm64)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-macos-arm64.tar.gz)
- macOS Apple Silicon (arm64, KleidiAI enabled) [DISABLED](https://github.com/ggml-org/llama.cpp/pull/23780)
- [macOS Intel (x64)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-macos-x64.tar.gz)
- [iOS XCFramework](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-xcframework.zip)
**Linux:**
- [Ubuntu x64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-x64.tar.gz)
- [Ubuntu arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-arm64.tar.gz)
- [Ubuntu s390x (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-s390x.tar.gz)
- [Ubuntu x64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-x64.tar.gz)
- [Ubuntu arm64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-vulkan-arm64.tar.gz)
- [Ubuntu x64 (CUDA 12)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-12.8-x64.tar.gz) - [CUDA 12.8 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-12.8-x64.tar.gz)
- [Ubuntu x64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-x64.tar.gz) - [CUDA 13.4 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-x64.tar.gz)
- [Ubuntu arm64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-arm64.tar.gz) - [CUDA 13.4 libraries](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-${{ steps.tag.outputs.name }}-bin-ubuntu-cuda-13.4-arm64.tar.gz)
- [Ubuntu x64 (ROCm 10.0)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-rocm-10.0-x64.tar.gz)
- [Ubuntu x64 (OpenVINO)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-openvino-${{ needs.ubuntu-24-openvino.outputs.openvino_version }}-x64.tar.gz)
- [Ubuntu x64 (SYCL FP32)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-fp32-x64.tar.gz)
- [Ubuntu x64 (SYCL FP16)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-ubuntu-sycl-fp16-x64.tar.gz)
- [Linux arm64 (Snapdragon: CPU, Adreno GPU, Hexagon NPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-linux-arm64-snapdragon.tar.gz) - [setup guide](https://github.com/ggml-org/llama.cpp/blob/master/docs/backend/snapdragon/linux.md)
**Android:**
- [Android arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-android-arm64.tar.gz)
- [Android arm64 (Snapdragon: CPU, Adreno GPU, Hexagon NPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-android-arm64-snapdragon.tar.gz) - [setup guide](https://github.com/ggml-org/llama.cpp/blob/master/docs/backend/snapdragon/README.md)
**Windows:**
- [Windows x64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cpu-x64.zip)
- [Windows arm64 (CPU)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cpu-arm64.zip)
- [Windows arm64 (OpenCL Adreno)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-opencl-adreno-arm64.zip)
- [Windows x64 (CUDA 12)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-12.4-x64.zip) - [CUDA 12.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-12.4-x64.zip)
- [Windows x64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-13.4-x64.zip) - [CUDA 13.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-13.4-x64.zip)
- [Windows arm64 (CUDA 13)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-cuda-13.4-arm64.zip) - [CUDA 13.4 DLLs](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/cudart-llama-bin-win-cuda-13.4-arm64.zip)
- [Windows x64 (Vulkan)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-vulkan-x64.zip)
- [Windows x64 (OpenVINO)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-openvino-${{ needs.windows-openvino.outputs.openvino_version }}-x64.zip)
- [Windows x64 (SYCL)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-sycl-x64.zip)
- [Windows x64 (ROCm 10.0)](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-bin-win-rocm-10.0-x64.zip)
**openEuler:**
- [DISABLED](https://github.com/ggml-org/llama.cpp/pull/23705)
- openEuler x86 (310p)
- openEuler x86 (910b, ACL Graph)
- openEuler aarch64 (310p)
- openEuler aarch64 (910b, ACL Graph)
**UI:**
- [UI](https://github.com/ggml-org/llama.cpp/releases/download/${{ steps.tag.outputs.name }}/llama-${{ steps.tag.outputs.name }}-ui.tar.gz)
- name: Upload release
id: upload_release
uses: actions/github-script@v8
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const path = require('path');
const fs = require('fs');
const release_id = '${{ steps.create_release.outputs.id }}';
for (let file of await fs.readdirSync('./release')) {
if (path.extname(file) === '.zip' || file.endsWith('.tar.gz')) {
console.log('uploadReleaseAsset', file);
await github.rest.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release_id,
name: file,
data: await fs.readFileSync(`./release/${file}`)
});
}
}
ui-publish:
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
needs:
- release
uses: ./.github/workflows/ui-publish.yml
with:
version_tag: ${{ needs.release.outputs.tag_name }}
secrets:
hf_token: ${{ secrets.HF_TOKEN_UI_STATIC_OUTPUT }}
+1 -1
View File
@@ -102,7 +102,7 @@ jobs:
PYTEST_WORKERS=1 ./tests.sh
server-cuda:
runs-on: "hf-jobs-t4-small:cuda13"
runs-on: "hf-jobs-t4-medium:cuda13"
steps:
- name: Clone
+5 -3
View File
@@ -11,8 +11,10 @@ on:
jobs:
build:
runs-on: ubuntu-slim
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
permissions:
actions: write
contents: read
steps:
- name: Checkout code
@@ -52,7 +54,7 @@ jobs:
working-directory: tools/ui
- name: Upload built UI
uses: actions/upload-artifact@v6
uses: actions/upload-artifact@v7
with:
name: llama-ui.zip
path: tools/ui/dist/
+7 -11
View File
@@ -7,38 +7,34 @@ on:
description: 'Version tag to publish under (e.g., b1234)'
required: true
type: string
run_id:
required: true
type: number
secrets:
hf_token:
description: 'Hugging Face token with write access'
required: true
jobs:
build:
name: Build static output
uses: ./.github/workflows/ui-build.yml
publish:
name: Publish UI Static Output
needs: build
runs-on: ubuntu-slim
permissions:
actions: read
contents: read
env:
HF_BUCKET_NAME: ${{ vars.HF_BUCKET_UI_STATIC_OUTPUT }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 1
- name: Download UI build artifact
uses: actions/download-artifact@v7
uses: actions/download-artifact@v8
with:
name: llama-ui.zip
path: tools/ui/dist/
run-id: ${{ inputs.run_id }}
github-token: ${{ github.token }}
- name: Create distribution archive
run: |
+26 -47
View File
@@ -3,6 +3,9 @@
#include "build-info.h"
#include "common.h"
#include "../src/llama-ext.h"
#include "fit.h"
#include "log.h"
#include "llama.h"
@@ -1030,51 +1033,18 @@ std::filesystem::path fs_get_cache_file(const std::string & filename) {
return cache_directory / std::filesystem::u8path(filename);
}
std::vector<common_file_info> fs_list(const std::string & path, bool include_directories) {
std::vector<common_file_info> files;
if (path.empty()) return files;
std::filesystem::path dir(path);
if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) {
return files;
}
for (const auto & entry : std::filesystem::directory_iterator(dir)) {
try {
// Only include regular files (skip directories)
const auto & p = entry.path();
if (std::filesystem::is_regular_file(p)) {
common_file_info info;
info.path = p.string();
info.name = p.filename().string();
info.is_dir = false;
try {
info.size = static_cast<size_t>(std::filesystem::file_size(p));
} catch (const std::filesystem::filesystem_error &) {
info.size = 0;
}
files.push_back(std::move(info));
} else if (include_directories && std::filesystem::is_directory(p)) {
common_file_info info;
info.path = p.string();
info.name = p.filename().string();
info.size = 0; // Directories have no size
info.is_dir = true;
files.push_back(std::move(info));
}
} catch (const std::filesystem::filesystem_error &) {
// skip entries we cannot inspect
continue;
}
}
return files;
}
//
// TTY utils
//
bool common_is_tty(FILE * file) {
#if defined(_WIN32)
return _isatty(_fileno(file));
#else
return isatty(fileno(file));
#endif
}
bool tty_can_use_colors() {
// Check NO_COLOR environment variable (https://no-color.org/)
if (const char * no_color = std::getenv("NO_COLOR")) {
@@ -1092,10 +1062,7 @@ bool tty_can_use_colors() {
// Check if stdout and stderr are connected to a terminal
// We check both because log messages can go to either
bool stdout_is_tty = isatty(fileno(stdout));
bool stderr_is_tty = isatty(fileno(stderr));
return stdout_is_tty || stderr_is_tty;
return common_is_tty(stdout) || common_is_tty(stderr);
}
//
@@ -1186,6 +1153,7 @@ static const std::map<common_decision_type, std::string> COMMON_DECISION_TYPE_NA
{ COMMON_DECISION_TYPE_KEV, "kev" },
{ COMMON_DECISION_TYPE_NIMBLE, "nimble" },
{ COMMON_DECISION_TYPE_LAYA, "laya" },
{ COMMON_DECISION_TYPE_CLEF, "clef" },
};
static common_decision_type common_decision_type_from_string(const std::string & str) {
@@ -1261,10 +1229,10 @@ common_init_result::common_init_result(common_params & params, bool model_only)
const llama_vocab * vocab = llama_model_get_vocab(model);
// this decision model returns a score for each token via the embeddings output
// these decision models return a score for each token via the embeddings output
// TODO: maybe improve this in the future
const auto decision_type = common_get_decision_type(model);
if (decision_type == COMMON_DECISION_TYPE_LAYA || decision_type == COMMON_DECISION_TYPE_KEV) {
if (decision_type == COMMON_DECISION_TYPE_LAYA || decision_type == COMMON_DECISION_TYPE_KEV || decision_type == COMMON_DECISION_TYPE_CLEF) {
params.embedding = true;
params.pooling_type = LLAMA_POOLING_TYPE_NONE;
@@ -1276,6 +1244,14 @@ common_init_result::common_init_result(common_params & params, bool model_only)
LOG_INF("%s", "decision model reads the embeddings output, enabling embedding mode\n");
}
// embeddings need the whole batch in one ubatch, so n_batch must not be larger than n_ubatch
// (server.cpp does this check for --embedding, but before the model is loaded)
if (cparams.embeddings && cparams.n_batch > cparams.n_ubatch) {
LOG_WRN("embeddings enabled: setting n_batch = n_ubatch = %u\n", cparams.n_ubatch);
cparams.n_batch = cparams.n_ubatch;
params.n_batch = params.n_ubatch;
}
// load and optionally apply lora adapters
for (auto & la : params.lora_adapters) {
llama_adapter_lora_ptr lora;
@@ -2210,6 +2186,9 @@ llama_batch_ext * common_batch::get_sub_batch(int32_t off, int32_t n) {
if (t.output) {
llama_batch_ext_set_output_logits(res, idx, true);
}
if (t.decision_order != 0) {
llama_batch_ext_set_decision_order(res, idx, (llama_decision_order) t.decision_order);
}
}
return res;
+6 -8
View File
@@ -19,6 +19,7 @@
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <cstdio>
#if defined(_WIN32) && !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x0A00
@@ -929,14 +930,6 @@ std::filesystem::path fs_get_cache_directory();
std::filesystem::path fs_get_cache_file(const std::string & filename);
std::filesystem::path fs_get_config_directory();
struct common_file_info {
std::string path;
std::string name;
size_t size = 0; // in bytes
bool is_dir = false;
};
std::vector<common_file_info> fs_list(const std::string & path, bool include_directories);
void fs_write_atomic(const std::filesystem::path & path, const std::string & data);
//
@@ -946,6 +939,9 @@ void fs_write_atomic(const std::filesystem::path & path, const std::string & dat
// Auto-detect if colors can be enabled based on terminal and environment
bool tty_can_use_colors();
// Check if the given file is attached to a terminal
bool common_is_tty(FILE * file);
//
// Model utils
//
@@ -960,6 +956,7 @@ enum common_decision_type {
COMMON_DECISION_TYPE_KEV, // dot product of the hidden states of the last token and of one end token per option
COMMON_DECISION_TYPE_NIMBLE, // same as openjev, the prompt lists all the questions of the request
COMMON_DECISION_TYPE_LAYA, // score of one marker token per option, read from the embeddings output
COMMON_DECISION_TYPE_CLEF, // all questions in one prompt, score of option i read from the embeddings output at row i
COMMON_DECISION_TYPE_UNKNOWN, // a decision model of a type that is not supported
};
@@ -1062,6 +1059,7 @@ struct common_batch {
bool output;
llama_embd embd; // non-owning view of the data passed to add_embd()/set_embd(), data == NULL if none
std::vector<llama_seq_id> seq_ids_extra; // see add_seq()
int32_t decision_order = 0; // see llama_batch_ext_set_decision_order()
};
std::vector<token> tokens; // mirror of the entries, tokens[i] describes batch index i
+1 -12
View File
@@ -35,13 +35,6 @@
#endif
#endif
// isatty
#if defined(_WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif
//
// downloader
//
@@ -97,11 +90,7 @@ class ProgressBar : public common_download_callback {
}
static bool is_output_a_tty() {
#if defined(_WIN32)
return _isatty(_fileno(stdout));
#else
return isatty(1);
#endif
return common_is_tty(stdout);
}
public:
-13
View File
@@ -14,19 +14,6 @@
#include <vector>
#include <algorithm>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <io.h>
# include <windows.h>
# define isatty _isatty
# define fileno _fileno
#else
# include <unistd.h>
#endif // defined(_WIN32)
int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
int common_log_get_verbosity_thold(void) {
+12 -4
View File
@@ -75,9 +75,10 @@ common_chat_params common_chat_params_init_ling3(const common_chat_template &
(last_close == std::string::npos || last_open > last_close);
}
auto has_tools = inputs.tools.is_array() && !inputs.tools.empty();
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
auto include_grammar = has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE;
auto has_tools = inputs.tools.is_array() && !inputs.tools.empty();
auto has_response_format = inputs.json_schema.is_object() && !inputs.json_schema.empty();
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
auto include_grammar = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
auto parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
auto end = p.end();
@@ -101,6 +102,13 @@ common_chat_params common_chat_params_init_ling3(const common_chat_template &
// a trailing end-of-turn token is consumed instead of leaking into content
auto tail = p.optional(p.content(p.until(ROLE_END))) + p.optional(p.literal(ROLE_END));
// the think block must close before the JSON, so the turn cannot end inside the reasoning
if (has_response_format) {
auto closed_reasoning = p.literal(THINK_START) + think_body + p.literal(THINK_END);
auto response_format = p.content(p.schema(p.json(), "response-format", inputs.json_schema));
return opener + (closed_reasoning << response_format) + end;
}
if (!has_tools || inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_NONE) {
return opener + reasoning + tail + end;
}
@@ -180,7 +188,7 @@ common_chat_params common_chat_params_init_ling3(const common_chat_template &
data.parser = parser.save();
if (include_grammar) {
data.grammar_lazy = inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
data.grammar_lazy = !has_response_format && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder, data.grammar_lazy);
});
+57 -45
View File
@@ -385,63 +385,75 @@ static bool is_draft_file(const std::string & fname) {
}
common_presets common_preset_context::load_from_models_dir(const std::string & models_dir) const {
if (!std::filesystem::exists(models_dir) || !std::filesystem::is_directory(models_dir)) {
const std::filesystem::path dir = std::filesystem::u8path(models_dir);
if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir)) {
throw std::runtime_error(string_format("error: '%s' does not exist or is not a directory\n", models_dir.c_str()));
}
std::vector<local_model> models;
auto scan_subdir = [&models](const std::string & subdir_path, const std::string & name) {
auto files = fs_list(subdir_path, false);
common_file_info model_file;
common_file_info first_shard_file;
common_file_info mmproj_file;
common_file_info draft_file;
for (const auto & file : files) {
if (string_ends_with(file.name, ".gguf")) {
if (is_mmproj_file(file.name)) {
mmproj_file = file;
} else if (is_draft_file(file.name)) {
if (draft_file.path.empty()) {
draft_file = file; // first sidecar found wins
}
} else if (file.name.find("-00001-of-") != std::string::npos) {
first_shard_file = file;
} else {
model_file = file;
auto scan_subdir = [&models](const std::filesystem::path & subdir_path, const std::string & name) {
std::filesystem::path model_file;
std::filesystem::path first_shard_file;
std::filesystem::path mmproj_file;
std::filesystem::path draft_file;
std::error_code ec;
for (const auto & entry : std::filesystem::directory_iterator(subdir_path)) {
if (!entry.is_regular_file(ec)) {
continue;
}
const std::string fname = fs_path_to_utf8(entry.path().filename());
if (!string_ends_with(fname, ".gguf")) {
continue;
}
if (is_mmproj_file(fname)) {
mmproj_file = entry.path();
} else if (is_draft_file(fname)) {
if (draft_file.empty()) {
draft_file = entry.path(); // first sidecar found wins
}
} else if (fname.find("-00001-of-") != std::string::npos) {
first_shard_file = entry.path();
} else {
model_file = entry.path();
}
}
// single file model
local_model model{
/* name */ name,
/* path */ first_shard_file.path.empty() ? model_file.path : first_shard_file.path,
/* path_mmproj */ mmproj_file.path, // can be empty
/* path_draft */ draft_file.path // can be empty
};
if (!model.path.empty()) {
models.push_back(model);
const std::filesystem::path & path = first_shard_file.empty() ? model_file : first_shard_file;
if (!path.empty()) {
models.push_back({
/* name */ name,
/* path */ fs_path_to_utf8(path),
/* path_mmproj */ fs_path_to_utf8(mmproj_file), // can be empty
/* path_draft */ fs_path_to_utf8(draft_file) // can be empty
});
}
};
auto files = fs_list(models_dir, true);
for (const auto & file : files) {
if (file.is_dir) {
scan_subdir(file.path, file.name);
} else if (string_ends_with(file.name, ".gguf")) {
if (is_mmproj_file(file.name) || is_draft_file(file.name)) {
continue; // companion file, cannot be loaded as a model on its own
}
// single file model
std::string name = file.name;
string_replace_all(name, ".gguf", "");
local_model model{
/* name */ name,
/* path */ file.path,
/* path_mmproj */ "",
/* path_draft */ ""
};
models.push_back(model);
for (const auto & entry : std::filesystem::directory_iterator(dir)) {
std::error_code ec;
if (entry.is_directory(ec)) {
scan_subdir(entry.path(), fs_path_to_utf8(entry.path().filename()));
continue;
}
if (!entry.is_regular_file(ec)) {
continue;
}
const std::string fname = fs_path_to_utf8(entry.path().filename());
if (!string_ends_with(fname, ".gguf")) {
continue;
}
if (is_mmproj_file(fname) || is_draft_file(fname)) {
continue; // companion file, cannot be loaded as a model on its own
}
// single file model
std::string name = fname;
string_replace_all(name, ".gguf", "");
models.push_back({
/* name */ name,
/* path */ fs_path_to_utf8(entry.path()),
/* path_mmproj */ "",
/* path_draft */ ""
});
}
// convert local models to presets
+2 -2
View File
@@ -2915,8 +2915,8 @@ void common_speculative_draft(common_speculative * spec) {
SPC_DBG("truncating draft to %d tokens\n", dp.n_max);
result.resize(dp.n_max);
// the candidates are one per drafted token and must be cut with them
if (dp.result_q) {
// trim the candidates only if the drafter produced them (n-gram drafters do not)
if (dp.result_q && !dp.result_q->empty()) {
dp.result_q->resize(dp.n_max);
}
}
+3
View File
@@ -42,6 +42,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
"ChameleonForConditionalGeneration": "chameleon",
"ChatGLMForConditionalGeneration": "chatglm",
"ChatGLMModel": "chatglm",
"ClefModel": "clef",
"CodeShellForCausalLM": "codeshell",
"CogVLMForCausalLM": "cogvlm",
"Cohere2MoeForCausalLM": "command_r",
@@ -154,6 +155,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
"LevModel": "lev",
"NimbleModel": "lev",
"Lfm25AudioTokenizer": "lfm2",
"Lfm2BidirectionalForMaskedLM": "lfm2",
"Lfm2BidirectionalModel": "lfm2",
"Lfm2ForCausalLM": "lfm2",
"Lfm2Model": "lfm2",
@@ -296,6 +298,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
MMPROJ_MODEL_MAP: dict[str, str] = {
"AudioFlamingo3ForConditionalGeneration": "ultravox",
"ClefModel": "clef",
"CogVLMForCausalLM": "cogvlm",
"DeepseekOCR2ForCausalLM": "deepseek",
"DeepseekOCRForCausalLM": "deepseek",
+150
View File
@@ -0,0 +1,150 @@
from __future__ import annotations
import json
import math
from pathlib import Path
from typing import Any, Iterable, Iterator, TYPE_CHECKING
import torch
if TYPE_CHECKING:
from torch import Tensor
from .base import MmprojModel, ModelBase, gguf, logger
from .qwen import Qwen3_5TextModel
def _is_clef_checkpoint(dir_model: Path) -> bool:
return (dir_model / "joint_head_config.json").is_file() and (dir_model / "config.json").is_file()
@ModelBase.register_hparams_loader(_is_clef_checkpoint)
def _load_clef_hparams(dir_model: Path) -> dict[str, Any]:
logger.info("gguf: detected Clef checkpoint")
hparams = ModelBase.load_hparams(dir_model, False, guess=False)
hparams["architectures"] = ["ClefModel"]
with open(dir_model / "joint_head_config.json", encoding="utf-8") as f:
hparams["decision"] = json.load(f)
return hparams
@ModelBase.register("ClefModel")
class ClefModel(Qwen3_5TextModel):
model_arch = gguf.MODEL_ARCH.CLEF
no_mtp = True # the checkpoint has no MTP head
# prompt follows joint_schema_model.py of the model repo
_SYSTEM_PROMPT = (
"Read the complete state and schema. Decide every field jointly. Each answer "
"must be exactly one of that field's allowed options."
)
# torch.nn.LayerNorm default, used by the head
_HEAD_NORM_EPS = 1e-5
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
head = self.hparams["decision"]
self._n_routing = head["routing_layers"]
# the head blocks are named dec.blk.N, routing blocks first
self.tensor_map = gguf.get_tensor_name_map(self.model_arch, max(self.block_count, self._n_routing + head["layers"]))
self._scales: dict[str, float] = {}
def set_vocab(self):
super().set_vocab()
self.gguf_writer.add_chat_template([{"name": "systemone", "template": self._systemone_template()}])
@classmethod
def _systemone_template(cls) -> str:
def text(value: str) -> str:
return "{{ " + json.dumps(value) + " }}"
def render(name: str) -> str:
# strings are used as is, other values are compact JSON
return "{{ " + name + " if " + name + " is string else " + name + " | tojson(separators=[',', ':']) }}"
# the pieces of the prompt are tokenized one by one, the server gives the text that separates them (sep)
# and the text that starts the span of a question or of an option (mark_question, mark_option)
# the keys of JSON objects are given in sorted order
option = (
"{% set d = o.description %}"
"{% if q.type == 'noul' and d is none %}"
"{% set d = 'The proposition is true or the answer is yes.' if o.key == 'true' else 'The proposition is false or the answer is no.' %}"
"{% endif %}"
"{{ ({'option_id': o.key} if d is none else {'description': d, 'option_id': o.key}) | tojson(separators=[',', ':']) }}"
)
return (
text(f"<|im_start|>system\n{cls._SYSTEM_PROMPT}<|im_end|>\n<|im_start|>user\nSTATE:\n")
+ "{{ sep }}" + render("state")
+ "{{ sep }}" + text("\n\nSCHEMA FIELDS:\n")
+ "{% for q in questions %}"
+ "{{ sep }}" + text("\nFIELD ") + "{{ loop.index }}" + text("\nID: ") + "{{ q.id }}"
+ text("\nTYPE: ") + "{{ q.type }}" + text("\nINSTRUCTION: ")
+ "{{ sep }}{{ mark_question }}" + render("q.instructions")
+ "{{ sep }}" + text("\nALLOWED OPTIONS:\n")
+ "{% for o in q.options %}"
+ "{{ sep }}" + text("OPTION ") + "{{ loop.index }}" + text(": ")
+ "{{ sep }}{{ mark_option }}" + option
+ "{{ sep }}" + text("\n")
+ "{% endfor %}"
+ "{{ sep }}" + text("END FIELD\n")
+ "{% endfor %}"
+ "{{ sep }}" + text("\n<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\nJOINT SCHEMA DECISIONS:")
)
def set_gguf_parameters(self):
super().set_gguf_parameters()
head = self.hparams["decision"]
self.gguf_writer.add_decision_type(gguf.DecisionType.CLEF)
self.gguf_writer.add_decision_routing_block_count(head["routing_layers"])
self.gguf_writer.add_decision_block_count(head["layers"])
self.gguf_writer.add_decision_head_count(head["heads"])
self.gguf_writer.add_layer_norm_eps(self._HEAD_NORM_EPS)
def get_tensors(self) -> Iterator[tuple[str, Tensor]]:
yield from super().get_tensors()
from safetensors.torch import load_file
for name, data in load_file(self.dir_model / "joint_head.safetensors").items():
yield "joint_head." + name, data
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
if not name.startswith("joint_head."):
yield from super().modify_tensors(data_torch, name, bid)
return
parts = name.split(".")
# learned scalars, stored as the values used at inference
if len(parts) == 2 and data_torch.ndim == 0:
value = float(data_torch)
if parts[1] == "residual_gate":
self._scales[parts[1]] = 1.0 / (1.0 + math.exp(-value))
else:
self._scales[parts[1]] = math.exp(min(value, math.log(100.0)))
if len(self._scales) == 3:
scales = [self._scales[k] for k in ("prior_logit_scale", "joint_logit_scale", "residual_gate")]
yield self.format_tensor_name(gguf.MODEL_TENSOR.DECISION_SCALES, suffix=""), torch.tensor(scales, dtype=torch.float32)
return
# routing blocks come first
if parts[1] == "layers":
parts[2] = str(int(parts[2]) + self._n_routing)
name = ".".join(parts)
# nn.MultiheadAttention keeps q, k, v in one tensor
for suffix in ("weight", "bias"):
if name.endswith(".in_proj_" + suffix):
prefix = name[:-len("in_proj_" + suffix)]
for x, data in zip("qkv", data_torch.chunk(3, dim=0)):
yield self.map_tensor_name(prefix + x + "." + suffix), data
return
yield self.map_tensor_name(name), data_torch
@ModelBase.register("ClefModel")
class ClefVisionModel(MmprojModel):
def __init__(self, *args, **kwargs):
del args, kwargs
raise NotImplementedError(
"multimodal input is not supported yet for Clef, requires https://github.com/ggml-org/llama.cpp/pull/29622 to be merged first")
+5 -3
View File
@@ -65,19 +65,21 @@ class LFM2Model(TextModel):
yield from super().modify_tensors(data_torch, name, bid)
@ModelBase.register("Lfm2Model", "Lfm2BidirectionalModel")
@ModelBase.example("LiquidAI/LFM2.5-ColBERT-350M", "LiquidAI/LFM2.5-Embedding-350M")
@ModelBase.register("Lfm2Model", "Lfm2BidirectionalModel", "Lfm2BidirectionalForMaskedLM")
@ModelBase.example("LiquidAI/LFM2.5-ColBERT-350M", "LiquidAI/LFM2.5-Embedding-350M", "LiquidAI/LFM2.5-Encoder-350M", "LiquidAI/LFM2.5-Encoder-230M")
class LFM2ColBertModel(LFM2Model):
model_arch = gguf.MODEL_ARCH.LFM2
dense_tensor_name = "dense_2"
def set_gguf_parameters(self):
super().set_gguf_parameters()
if self.hf_arch == "Lfm2BidirectionalModel":
if self.hf_arch in ("Lfm2BidirectionalModel", "Lfm2BidirectionalForMaskedLM"):
self.gguf_writer.add_causal_attention(False)
self._try_set_pooling_type()
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
# masked LM checkpoints use "lfm2." prefix
name = name.removeprefix("lfm2.")
if not name.startswith(self.dense_tensor_name):
name = "model." + name
+30 -26
View File
@@ -52,8 +52,8 @@ Although OpenVINO supports a wide range of [Intel hardware](https://docs.openvin
- `Q4_1`
- `Q4_K`
- `Q4_K_M`
- `Q5_K` (converted to `Q8_0_C` at runtime)
- `Q6_K` (converted to `Q8_0_C` at runtime)
- `Q5_K` (converted to `Q8_0_C` at runtime by default)
- `Q6_K` (converted to `Q8_0_C` at runtime by default)
> [!NOTE]
> Accuracy validation and performance optimizations for quantized models are a work in progress.
@@ -93,12 +93,12 @@ Although, the validated models below were tested with `llama-cli` using the `Q4_
> Extensive accuracy validation, performance optimizations, and broader architecture coverage are work in progress.
**Legend & Test Configuration:**
- **Status:** ✓ = Passed | ✗ = Failed or Unsupported
- **Status:** ✓ = Passed | ~ = Accuracy issues | ✗ = Failed or Unsupported
- **Execution Modes:**
- **SL** = Stateless (`GGML_OPENVINO_STATEFUL_EXECUTION=0`)
- **SF** = Stateful (`GGML_OPENVINO_STATEFUL_EXECUTION=1`)
- Note: The NPU operates in stateless mode only.
- **Validation system:** Intel® Core™ Ultra 5 238V (Lunar Lake) | 32 GB RAM | Ubuntu 24.04 | Intel Graphics Compiler 2.41.5 | Intel OpenCL GPU Driver 26.31.39395.13-0 | Intel NPU Driver 1.38.0.
- **Validation system:** Intel® Core™ Ultra 5 238V (Lunar Lake) | 32 GB RAM | Ubuntu 24.04 | Intel Graphics Compiler 2.41.5 | Intel OpenCL GPU Driver 26.35.39758.10-0 | Intel NPU Driver 1.38.0.
- See [Known Limitations](#known-limitations) for context on observed failures.
| Model | CPU (SL / SF) | GPU (SL / SF) | NPU (SL) |
@@ -113,14 +113,14 @@ Although, the validated models below were tested with `llama-cli` using the `Q4_
| [bartowski/Qwen_Qwen3-1.7B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3-1.7B-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [Qwen/Qwen3-4B-Q4_K_M](https://huggingface.co/Qwen/Qwen3-4B-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [lm-kit/Qwen3-8B-Q4_K_M](https://huggingface.co/lm-kit/qwen-3-8b-instruct-gguf) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [bartowski/Qwen_Qwen3.5-0.8B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-0.8B-GGUF) | ✓ / ✗ | ✓ / ✗ | ✗ |
| [bartowski/Qwen_Qwen3.5-2B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-2B-GGUF) | ✓ / ✗ | ✓ / ✗ | ✗ |
| [bartowski/Qwen_Qwen3.5-4B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-4B-GGUF) | ✓ / ✗ | ✓ / ✗ | ✗ |
| [lmstudio-community/Qwen3.5-9B-Q4_K_M](https://huggingface.co/lmstudio-community/Qwen3.5-9B-GGUF) | ✓ / ✗ | ✓ / ✗ | ✗ |
| [bartowski/Qwen_Qwen3.5-0.8B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-0.8B-GGUF) | ✓ / ✓ | ✓ / ~ | ✗ |
| [bartowski/Qwen_Qwen3.5-2B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-2B-GGUF) | ✓ / ✓ | ✓ / ~ | ✗ |
| [bartowski/Qwen_Qwen3.5-4B-Q4_K_M](https://huggingface.co/bartowski/Qwen_Qwen3.5-4B-GGUF) | ✓ / ✓ | ✓ / ~ | ✗ |
| [lmstudio-community/Qwen3.5-9B-Q4_K_M](https://huggingface.co/lmstudio-community/Qwen3.5-9B-GGUF) | ✓ / ✓ | ✓ / ~ | ✗ |
| | | | |
| [unsloth/gemma-3-4b-it-Q4_K_M](https://huggingface.co/unsloth/gemma-3-4b-it-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [bartowski/google_gemma-4-E2B-it-Q4_K_M](https://huggingface.co/bartowski/google_gemma-4-E2B-it-GGUF) | ✓ / ✓ | ✓ / ✓ | ✗ |
| [bartowski/google_gemma-4-E4B-it-Q4_K_M](https://huggingface.co/bartowski/google_gemma-4-E4B-it-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [bartowski/google_gemma-4-E2B-it-Q4_K_M](https://huggingface.co/bartowski/google_gemma-4-E2B-it-GGUF) | ✓ / ✓ | ✓ / ~ | ~ |
| [bartowski/google_gemma-4-E4B-it-Q4_K_M](https://huggingface.co/bartowski/google_gemma-4-E4B-it-GGUF) | ✓ / ✓ | ✗ / ✗ | ✓ |
| [bartowski/gemma-4-12B-it-Q4_K_M](https://huggingface.co/bartowski/gemma-4-12B-it-GGUF) | ✓ / ✓ | ✓ / ✓ | ✗ |
| | | | |
| [bartowski/Phi-3-mini-4k-instruct-Q4_K_M](https://huggingface.co/bartowski/Phi-3-mini-4k-instruct-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
@@ -134,9 +134,9 @@ Although, the validated models below were tested with `llama-cli` using the `Q4_
| [bartowski/DeepSeek-R1-Distill-Llama-8B-Q4_K_M](https://huggingface.co/bartowski/DeepSeek-R1-Distill-Llama-8B-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [bartowski/DeepSeek-R1-Distill-Qwen-7B-Q4_K_M](https://huggingface.co/bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| | | | |
| [ibm-granite/granite-4.0-350m-Q4_K_M](https://huggingface.co/ibm-granite/granite-4.0-350m-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [ibm-granite/granite-4.0-350m-Q4_K_M](https://huggingface.co/ibm-granite/granite-4.0-350m-GGUF) | ✓ / ✓ | ~ / ~ | ✓ |
| [ibm-granite/granite-4.0-micro-Q4_K_M](https://huggingface.co/ibm-granite/granite-4.0-micro-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| [ibm-granite/granite-4.0-1b-Q4_K_M](https://huggingface.co/ibm-granite/granite-4.0-1b-GGUF) | ✓ / ✓ | ✓ / ✓ | ✗ |
| [ibm-granite/granite-4.0-1b-Q4_K_M](https://huggingface.co/ibm-granite/granite-4.0-1b-GGUF) | ✓ / ✓ | ~ / ~ | ~ |
| [ibm-research/granite-3.2-8b-instruct-Q4_K_M](https://huggingface.co/ibm-research/granite-3.2-8b-instruct-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
| | | | |
| [HuggingFaceTB/smollm2-1.7b-instruct-q4_k_m](https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct-GGUF) | ✓ / ✓ | ✓ / ✓ | ✓ |
@@ -244,8 +244,8 @@ chmod +x build-llamacpp-ov.sh
# ============================================
set -euo pipefail
OPENVINO_VERSION_MAJOR="2026.4"
OPENVINO_VERSION_FULL="2026.4.0.22959.99c81491cc3"
OPENVINO_VERSION_MAJOR="2026.4.1"
OPENVINO_VERSION_FULL="2026.4.1.22982.07f9c262b05"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OPENVINO_INSTALL_DIR="/opt/intel/openvino_${OPENVINO_VERSION_MAJOR}"
@@ -342,7 +342,7 @@ echo " ./build/ReleaseOV/bin/llama-cli -m model.gguf"
```
> [!NOTE]
> The script pins OpenVINO `2026.4` via the `OPENVINO_VERSION_MAJOR` / `OPENVINO_VERSION_FULL` variables at the top — edit them to track a different release.
> The script pins OpenVINO `2026.4.1` via the `OPENVINO_VERSION_MAJOR` / `OPENVINO_VERSION_FULL` variables at the top — edit them to track a different release.
</details>
@@ -372,8 +372,8 @@ REM ============================================
REM llama.cpp OpenVINO Build Script (Ninja)
REM ============================================
set "OPENVINO_VERSION_MAJOR=2026.4"
set "OPENVINO_VERSION_FULL=2026.4.0.22959.99c81491cc3"
set "OPENVINO_VERSION_MAJOR=2026.4.1"
set "OPENVINO_VERSION_FULL=2026.4.1.22982.07f9c262b05"
set "SCRIPT_DIR=%~dp0"
set "VCPKG_DIR=C:\vcpkg"
@@ -552,7 +552,7 @@ endlocal
```
> [!NOTE]
> The script pins OpenVINO `2026.4` via the `OPENVINO_VERSION_MAJOR` / `OPENVINO_VERSION_FULL` variables at the top — edit them to track a different release. From any new shell, source the matching `setupvars` script via the junction — `call "C:\Intel\openvino\setupvars.bat"` from `cmd`, or `& "C:\Intel\openvino\setupvars.ps1"` from PowerShell. If `winget` cannot register Visual Studio Build Tools on first run, install them once manually and re-run the script from an elevated **Developer Command Prompt for VS 2022**.
> The script pins OpenVINO `2026.4.1` via the `OPENVINO_VERSION_MAJOR` / `OPENVINO_VERSION_FULL` variables at the top — edit them to track a different release. From any new shell, source the matching `setupvars` script via the junction — `call "C:\Intel\openvino\setupvars.bat"` from `cmd`, or `& "C:\Intel\openvino\setupvars.ps1"` from PowerShell. If `winget` cannot register Visual Studio Build Tools on first run, install them once manually and re-run the script from an elevated **Developer Command Prompt for VS 2022**.
</details>
@@ -625,7 +625,7 @@ $env:GGML_OPENVINO_DEVICE = "NPU"
build\ReleaseOV\bin\llama-cli.exe -m "C:\models\Llama-3.2-1B-Instruct-Q4_K_M.gguf" -c 512
```
> [!NOTE]
> On systems with multiple GPUs, use `GPU.0` or `GPU.1` to explicitly target specific GPU. See [OpenVINO GPU Device](https://docs.openvino.ai/2026/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.html) for more details.
> On systems with multiple GPUs, use `GPU.0` or `GPU.1` to explicitly target specific GPU. A device that is not available is an error (no fallback to CPU), and the error message lists the available OpenVINO devices with their names. Run `llama-cli --list-devices` to see the valid values: each OpenVINO device shows the `GGML_OPENVINO_DEVICE=<value>` to set, and `(selected)` marks the active one. Select the OpenVINO device with this variable, not with `-dev`. See [OpenVINO GPU Device](https://docs.openvino.ai/2026/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.html) for more details.
### 5. Docker Build
@@ -713,12 +713,13 @@ Boolean flags follow a uniform convention: set to a **positive integer** (e.g. `
| Variable | Type | Default | Description |
|-----------------------------------|-----------|------------|-------------------------------------------------------------------------------------------------------------|
| `GGML_OPENVINO_DEVICE` | String | `CPU` | Specify the target device (CPU, GPU, NPU). On systems with multiple GPUs, use `GPU.0` or `GPU.1` to explicitly target specific GPU. See [OpenVINO GPU Device](https://docs.openvino.ai/2026/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.html). When set to **NPU**, static compilation mode is enabled for optimal performance. |
| `GGML_OPENVINO_CACHE_DIR` | String | `not set` | Directory for OpenVINO model caching (recommended: `/tmp/ov_cache`). Enables model caching when set. **Not supported on NPU devices.** |
| `GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR` | String | `not set` | Directory for the frontend compiled-model cache. When set, OpenVINO compiled models are exported as blobs and imported on later runs to skip weight requantization, graph conversion, and compilation for matching single-graph models. |
| `GGML_OPENVINO_DEVICE` | String | `CPU` | Specify the target device (CPU, GPU, NPU). On systems with multiple GPUs, use `GPU.0` or `GPU.1` to explicitly target specific GPU. A device that is not available is an error (no fallback to CPU), and the error message lists the available OpenVINO devices with their names. See [OpenVINO GPU Device](https://docs.openvino.ai/2026/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.html). When set to **NPU**, static compilation mode is enabled for optimal performance. |
| `GGML_OPENVINO_CACHE_DIR` | String | `not set` | Directory for OpenVINO's separate plugin cache. On NPU, this sets `NPUW_CACHE_DIR`. |
| `GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR` | String | `not set` | Directory for standalone compiled blobs with weights. Dynamic CPU/GPU graphs can import matching blobs on later runs. |
| `GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY` | Boolean | `0` | Require an existing compiled blob and skip weight uploads and compilation. Requires Linux or Windows mmap loading and a full dynamic CPU/GPU graph on OpenVINO. |
| `GGML_OPENVINO_PREFILL_CHUNK_SIZE`| Integer | `256` | Token chunk size for **NPU** prefill (NPU-only; ignored on CPU/GPU). Must be a positive integer; otherwise the default is used. |
| `GGML_OPENVINO_NPU_COMPILE_CONFIG` | String | `not set` | NPU-only compiler mode parameters forwarded to OpenVINO as `NPU_COMPILATION_MODE_PARAMS`, for example `optimization-level=3`. |
| `GGML_OPENVINO_STATEFUL_EXECUTION`| Boolean | `0` | Enable stateful KV cache for better performance. Recommended on CPU, GPU. |
| `GGML_OPENVINO_STATEFUL_EXECUTION`| Boolean | `0` | Keep KV and supported recurrent caches inside the model. Single-slot CPU/GPU execution only. |
| `GGML_OPENVINO_DISABLE_CACHE` | Boolean | `0` | Disable the in-process compiled-model / decoder cache (cache is on by default). Set to `1` to disable. |
| `GGML_OPENVINO_DISABLE_KV_SLICE` | Boolean | `0` | Disable the KV-cache input-tensor slicing optimization (slicing is on by default on CPU/GPU). Set to `1` to disable. |
| `GGML_OPENVINO_DISABLE_KV_STATE_RELAYOUT` | Boolean | `0` | Disable the stateful KV-state sequence-axis relayout (relayout is on by default). It moves the KV state sequence axis from dim 1 to dim 2, so the GPU plugin can append new tokens in place instead of copying the whole state every token, and the reader side no longer transposes the whole accumulated state. Set to `1` to disable. |
@@ -727,8 +728,10 @@ Boolean flags follow a uniform convention: set to a **positive integer** (e.g. `
| `GGML_OPENVINO_REDUCE_COMPILE_MEM`| Boolean | inherits from `GGML_OPENVINO_MEMORY_OPTIMIZE` | Reduce compile-time host memory use by streaming weight requantization and avoiding extra weight-node materialization where possible. Set explicitly to override the umbrella switch. |
| `GGML_OPENVINO_RELEASE_WEIGHTS` | Boolean | inherits from `GGML_OPENVINO_MEMORY_OPTIMIZE` on GPU | GPU-only. Release host weight buffers after the compiled model cache can reuse the device/plugin copy. Requires stable graph shapes; dynamic workloads that need recompilation should leave this disabled. |
| `GGML_OPENVINO_SPILL_DIR` | String | `not set` | Directory for a disk-backed weight buffer. When set, the repacked weight buffer is mapped from an unlinked file on this path instead of anonymous memory, so its pages are reclaimable under memory pressure instead of staying pinned, cutting the load-time host memory peak. Must point at real storage; a tmpfs mount (e.g. `/tmp` on many systems) backs it with RAM and makes the peak worse. |
| `GGML_OPENVINO_REQUANT_KQUANT` | String | `not set` | Requantize Q6_K/Q5_K weights (and matching MoE expert weights) to a 4-bit target instead of the default Q8_0_C, trading accuracy for less memory traffic. One of `q4_sym128` (Q6_K/Q5_K only), `q4_sym128_all` (Q4_K too, drops its per-group zero point), `q4_asym64_all` (Q6_K/Q5_K/Q4_K, keeps a real zero point at group 64), or `native` (no requantization). |
| `GGML_OPENVINO_PROFILING` | Boolean | `0` | Enable execution-time profiling. |
| `GGML_OPENVINO_REQUANT_KQUANT` | String | `not set` | Requantize Q6_K/Q5_K weights (and matching MoE expert weights) to a 4-bit target instead of the default Q8_0_C, trading accuracy for less memory traffic. One of `q4_asym64` (Q6_K/Q5_K only, keeps a real zero point at group 64), `q4_asym64_all` (also requantizes Q4_K), `q4_sym128` (Q6_K/Q5_K only), `q4_sym128_all` (Q4_K too, drops its per-group zero point), or `native` (no requantization). |
| `GGML_OPENVINO_PROFILING` | Integer | `0` | `1` logs execution timing; `2` or higher also enables OpenVINO and OpenCL profiling. |
| `GGML_OPENVINO_DEBUG_NODE` | String | `not set` | Add the named graph nodes as compiled outputs for debugging. Separate multiple names with commas. |
| `GGML_OPENVINO_MOE_OP` | Boolean | `1` | On GPU, set to `0` to keep the unfused GatherMatmul path. |
| `GGML_OPENVINO_DUMP_CGRAPH` | Boolean | `0` | Dump the GGML compute graph to `cgraph_ov.txt`. |
| `GGML_OPENVINO_DUMP_IR` | Boolean | `0` | Serialize OpenVINO IR files with timestamps. |
| `GGML_OPENVINO_DEBUG_INPUT` | Boolean | `0` | Enable input debugging and print input tensor info. |
@@ -737,8 +740,9 @@ Boolean flags follow a uniform convention: set to a **positive integer** (e.g. `
| `GGML_OPENVINO_LOG_UNSUPPORTED_OPS`| Boolean | `0` | Log warning messages with tensor details and rejection reasons for any ops not supported by the OpenVINO backend. Emits at `WARN` level (requires `--log-verbosity >= 2`, enabled by default). |
> [!NOTE]
> - `GGML_OPENVINO_STATEFUL_EXECUTION` is an **Experimental** feature to allow stateful execution for managing the KV cache internally inside the OpenVINO model, improving performance on CPUs and GPUs. Stateful execution is not effective on NPUs, and not all models currently support this feature. This feature is experimental and has been validated only with the llama-simple, llama-cli, llama-bench, and llama-run applications and is recommended to enable for the best performance. Other applications, such as llama-server and llama-perplexity, are not yet supported.
> - `GGML_OPENVINO_STATEFUL_EXECUTION` is an **Experimental** feature for managing caches internally inside the OpenVINO model on CPUs and GPUs. Use a single slot (`-np 1`). KV caches retain the append-based state layout and sequence-axis optimization. Qwen3.5 adds recurrent cache states in their GGML layouts. Qwen3.5 requires an unsplit graph with model caching enabled and no recurrent rollback. A prompt starting at position 0 resets all states. State save/restore, sequence rewind, context shift, and mid-sequence graph replacement are unsupported. Stateful execution is not effective on NPUs.
> - `GGML_OPENVINO_LOG_UNSUPPORTED_OPS` emits logs at `WARN` level (`GGML_LOG_WARN`), which requires application log verbosity `--log-verbosity >= 2` (or `-lv 2`).
> - With `GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY=1`, use the same compilation settings as the export run. One directory can hold blobs for different models and settings; `GGML_OPENVINO_SPILL_DIR` does not affect the cache key and is ignored in cache-only mode. See [Compiled model cache](../../ggml/src/ggml-openvino/README.md) for the workflow and restrictions.
### Example Usage
+7
View File
@@ -1571,6 +1571,9 @@ struct ggml_cuda_mm_fusion_args_host {
const ggml_tensor * gate_scale = nullptr;
ggml_glu_op glu_op;
float glu_limit = 0.0f;
const ggml_tensor * shared_up = nullptr;
const ggml_tensor * shared_gate = nullptr;
ggml_tensor * shared_dst = nullptr;
};
struct ggml_cuda_mm_fusion_args_device {
const void * x_bias = nullptr;
@@ -1580,6 +1583,10 @@ struct ggml_cuda_mm_fusion_args_device {
const void * gate_scale = nullptr;
ggml_glu_op glu_op;
float glu_limit = 0.0f;
const void * shared_up = nullptr;
const void * shared_gate = nullptr;
float * shared_dst = nullptr;
uint32_t shared_stride_col_dst = 0;
};
struct ggml_cuda_kernel_launch_params {
+89
View File
@@ -1823,6 +1823,55 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
return use_mul_mat_vec_q;
}
static bool ggml_cuda_match_shared_expert(const ggml_cgraph * graph, int routed_idx, int shared_idx) {
if (routed_idx + 2 >= graph->n_nodes || shared_idx + 2 >= graph->n_nodes || shared_idx < routed_idx + 3) {
return false;
}
const int nodes[] = { routed_idx, routed_idx + 1, routed_idx + 2, shared_idx, shared_idx + 1, shared_idx + 2 };
const ggml_op ops[] = { GGML_OP_MUL_MAT_ID, GGML_OP_MUL_MAT_ID, GGML_OP_GLU,
GGML_OP_MUL_MAT, GGML_OP_MUL_MAT, GGML_OP_GLU };
const int outputs[] = { routed_idx + 2, shared_idx + 2 };
if (!ggml_can_fuse_subgraph_ext(graph, nodes, 6, ops, outputs, 2)) {
return false;
}
const ggml_tensor * routed = graph->nodes[routed_idx + 2];
const ggml_tensor * shared = graph->nodes[shared_idx + 2];
const ggml_tensor * gate = routed->src[0];
const ggml_tensor * up = routed->src[1];
const ggml_tensor * shared_gate = shared->src[0];
const ggml_tensor * shared_up = shared->src[1];
const auto is_pair = [&](const ggml_tensor * a, const ggml_tensor * b, int idx) {
return (a == graph->nodes[idx] && b == graph->nodes[idx + 1]) ||
(b == graph->nodes[idx] && a == graph->nodes[idx + 1]);
};
if (!is_pair(gate, up, routed_idx) || !is_pair(shared_gate, shared_up, shared_idx) ||
!ggml_cuda_should_fuse_mul_mat(up, gate, routed) ||
!ggml_cuda_should_fuse_mul_mat(shared_up, shared_gate, shared) ||
!up->src[0]->buffer ||
!ggml_cuda_should_fuse_mul_mat_vec_q(up)) {
return false;
}
const ggml_tensor * input = up->src[1];
const ggml_tensor * weight = up->src[0];
const ggml_tensor * shared_weight = shared_up->src[0];
if (input->op != GGML_OP_RESHAPE || input->src[0] != shared_up->src[1] ||
input->ne[1] != 1 || input->ne[3] != 1 || !ggml_is_contiguous(input) ||
!ggml_is_contiguous(shared_up->src[1]) || !ggml_is_matrix(shared_up->src[1]) ||
weight->type != shared_weight->type || weight->ne[0] != shared_weight->ne[0] ||
weight->ne[1] != shared_weight->ne[1] || weight->nb[1] != shared_weight->nb[1] || weight->ne[3] != 1 ||
!ggml_is_matrix(shared_weight) || !ggml_is_contiguous(shared_weight) ||
!ggml_is_contiguous(shared_gate->src[0]) || !ggml_is_contiguous(routed) || !ggml_is_contiguous(shared)) {
return false;
}
if (shared_weight->op != GGML_OP_NONE || shared_gate->src[0]->op != GGML_OP_NONE ||
ggml_get_glu_op(routed) != ggml_get_glu_op(shared) ||
ggml_get_op_params_f32(routed, 3) != ggml_get_op_params_f32(shared, 3)) {
return false;
}
return true;
}
static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
GGML_TENSOR_BINARY_OP_LOCALS
@@ -3459,6 +3508,25 @@ static int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph
ggml_tensor * node = cgraph->nodes[i];
if (node->op == GGML_OP_MUL_MAT_ID && cuda_ctx->stream_context().concurrent_events.empty() &&
ggml_cuda_match_shared_expert(cgraph, i, i + 3)) {
const int outputs[] = { i + 2, i + 5 };
if (ggml_cuda_check_fusion_memory_ranges(cgraph, i, 6, outputs, 2)) {
ggml_tensor * routed = cgraph->nodes[i + 2];
ggml_tensor * shared = cgraph->nodes[i + 5];
const ggml_tensor * up = routed->src[1];
ggml_cuda_mm_fusion_args_host fusion{};
fusion.gate = routed->src[0]->src[0];
fusion.glu_op = ggml_get_glu_op(routed);
fusion.glu_limit = ggml_get_op_params_f32(routed, 3);
fusion.shared_up = shared->src[1]->src[0];
fusion.shared_gate = shared->src[0]->src[0];
fusion.shared_dst = shared;
ggml_cuda_mul_mat_vec_q(*cuda_ctx, up->src[0], up->src[1], up->src[2], routed, &fusion);
return 5;
}
}
if (node->op == GGML_OP_MUL) {
ggml_cuda_moe_weighted_reduction_match match;
if (ggml_cuda_match_moe_weighted_reduction(cgraph, i, match)) {
@@ -4549,6 +4617,27 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph
if (!disable_fusion) {
// add alloc deps for performance positive fusions. This may increase the overall compute buffer size.
// TODO: consolidate fusion paths in graph_optimize and graph_compute
ggml_cuda_set_device(cuda_ctx->device);
for (int i = 0; i + 5 < cgraph->n_nodes; ++i) {
if (cgraph->nodes[i]->op != GGML_OP_MUL_MAT_ID) {
continue;
}
for (int j = i + 3; j + 2 < cgraph->n_nodes; ++j) {
if (cgraph->nodes[j]->op == GGML_OP_MUL_MAT_ID && cgraph->nodes[j + 1]->op == GGML_OP_MUL_MAT_ID) {
break;
}
if (cgraph->nodes[j]->op != GGML_OP_MUL_MAT || !ggml_cuda_match_shared_expert(cgraph, i, j)) {
continue;
}
// Group both outputs before allocation so the shared result cannot alias intervening nodes.
std::rotate(cgraph->nodes + i + 3, cgraph->nodes + j, cgraph->nodes + j + 3);
ggml_tensor * up = cgraph->nodes[i + 2]->src[1];
params->add_alloc_dep(params->user_data, up->src[1], cgraph->nodes[i + 5]);
params->add_alloc_dep(params->user_data, up->src[2], cgraph->nodes[i + 5]);
i += 5;
break;
}
}
for (int i = 0; i < cgraph->n_nodes; ++i) {
ggml_cuda_moe_weighted_reduction_match match;
if (ggml_cuda_match_moe_weighted_reduction(cgraph, i, match)) {
+20 -1
View File
@@ -528,6 +528,25 @@ void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor *
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 32, k, GGML_TYPE_F32)
GGML_ABORT("fatal error");
}
} else if (n_embd == 128 && n_head == 4) {
// too few heads for a wmma tile, use vector kernel
constexpr int K_VECS_PER_WARP = 8;
constexpr int WARPS_PER_BLOCK = 8;
constexpr int K_VECS_PER_BLOCK = K_VECS_PER_WARP * WARPS_PER_BLOCK;
dim3 block(32, WARPS_PER_BLOCK);
int num_kv_blocks = (n_kv + (K_VECS_PER_BLOCK) - 1) / (K_VECS_PER_BLOCK);
dim3 grid(num_kv_blocks, n_batch, n_stream);
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_F16)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q4_0)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q4_1)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q5_0)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q5_1)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_Q8_0)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_BF16)
LIGHTNING_INDEXER_CASE(lightning_indexer_kernel_vec, 128, 4, k, GGML_TYPE_F32)
GGML_ABORT("fatal error");
} else {
GGML_ABORT("fatal error");
}
@@ -556,7 +575,7 @@ bool ggml_cuda_lightning_indexer_supported(int device, const ggml_tensor * dst)
return false;
}
if (neq1 != 64 && neq1 != 32) {
if (neq1 != 64 && neq1 != 32 && neq1 != 4) {
return false;
}
+40 -10
View File
@@ -601,7 +601,7 @@ __launch_bounds__(calc_nwarps(type, ncols_dst, get_device_table_id(), small_k, h
static __global__ void mul_mat_vec_q(
const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion, float * dst_ptr,
const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t stride_row_x, const uint32_t stride_col_y,
const uint32_t stride_col_dst, const uint3 channel_ratio, const uint32_t stride_channel_x,
uint32_t stride_col_dst, const uint3 channel_ratio, const uint32_t stride_channel_x,
const uint32_t stride_channel_y, const uint32_t stride_channel_dst, const uint3 sample_ratio,
const uint32_t stride_sample_x, const uint32_t stride_sample_y, const uint32_t stride_sample_dst,
const uint32_t ids_stride) {
@@ -625,14 +625,20 @@ static __global__ void mul_mat_vec_q(
const int blocks_per_row_x = ncols_x / qk;
constexpr int blocks_per_iter = vdr * nwarps*warp_size / qi;
const uint32_t channel_dst = blockIdx.y;
const bool shared_expert = has_fusion && fusion.shared_up && blockIdx.y == gridDim.y - 1;
const uint32_t channel_dst = shared_expert ? 0 : blockIdx.y;
if (shared_expert) {
vx = fusion.shared_up;
dst = fusion.shared_dst;
stride_col_dst = fusion.shared_stride_col_dst;
}
uint32_t channel_x;
uint32_t channel_y;
uint32_t sample_dst;
ggml_cuda_pdl_sync();
channel_x = ncols_dst == 1 && ids ? ids[channel_dst] : fastdiv(channel_dst, channel_ratio);
channel_x = shared_expert ? 0 : ncols_dst == 1 && ids ? ids[channel_dst] : fastdiv(channel_dst, channel_ratio);
channel_y = ncols_dst == 1 && ids ? fastmodulo(channel_dst, nchannels_y) : channel_dst;
sample_dst = blockIdx.z;
@@ -656,7 +662,7 @@ static __global__ void mul_mat_vec_q(
use_gate = fusion.gate != nullptr;
use_bias = fusion.x_bias != nullptr;
use_gate_bias = fusion.gate_bias != nullptr && use_gate;
vgate = fusion.gate;
vgate = shared_expert ? fusion.shared_gate : fusion.gate;
x_bias = (const float *) fusion.x_bias;
gate_bias = (const float *) fusion.gate_bias;
active_glu = fusion.glu_op;
@@ -854,7 +860,7 @@ static __global__ void mul_mat_vec_q_moe(
const void * vx_ptr, const void * vy_ptr, const int32_t * ids_ptr, const ggml_cuda_mm_fusion_args_device fusion,
float * dst_ptr,
const uint32_t ncols_x, const uint3 nchannels_y, const uint32_t nrows_x,
const uint32_t stride_row_x, const uint32_t stride_col_y, const uint32_t stride_col_dst,
const uint32_t stride_row_x, const uint32_t stride_col_y, uint32_t stride_col_dst,
const uint32_t stride_channel_x, const uint32_t stride_channel_y, const uint32_t stride_channel_dst,
const uint32_t ncols_dst, const uint32_t ids_stride) {
const void * GGML_CUDA_RESTRICT vx = vx_ptr;
@@ -869,6 +875,13 @@ static __global__ void mul_mat_vec_q_moe(
constexpr vec_dot_q_cuda_t vec_dot_q_cuda = get_vec_dot_q_cuda(type);
const bool shared_expert = has_fusion && fusion.shared_up && blockIdx.y == gridDim.y - 1;
if (shared_expert) {
vx = fusion.shared_up;
dst = fusion.shared_dst;
stride_col_dst = fusion.shared_stride_col_dst;
}
// fuse gate, bias, scales, and glu_op into the up projection
bool use_gate = false;
const void * vgate = nullptr;
@@ -881,7 +894,7 @@ static __global__ void mul_mat_vec_q_moe(
if constexpr (has_fusion) {
use_gate = fusion.gate != nullptr;
vgate = fusion.gate;
vgate = shared_expert ? fusion.shared_gate : fusion.gate;
x_bias = (const float *) fusion.x_bias;
gate_bias = (const float *) fusion.gate_bias;
active_glu = fusion.glu_op;
@@ -897,14 +910,14 @@ static __global__ void mul_mat_vec_q_moe(
const int blocks_per_row_x = ncols_x / qk;
constexpr int blocks_per_iter = vdr * warp_size / qi;
const uint32_t channel_dst = blockIdx.y;
const uint32_t channel_dst = shared_expert ? 0 : blockIdx.y;
if (token_idx >= ncols_dst) {
return;
}
ggml_cuda_pdl_sync();
const uint32_t channel_x = ids[channel_dst + token_idx * ids_stride];
const uint32_t channel_x = shared_expert ? 0 : ids[channel_dst + token_idx * ids_stride];
const uint32_t channel_y = fastmodulo(channel_dst, nchannels_y);
const block_q8_1 * y = ((const block_q8_1 *) vy) + channel_y*stride_channel_y + token_idx*stride_col_y;
@@ -1050,7 +1063,7 @@ static void mul_mat_vec_q_moe_launch(
constexpr int rows_per_block = 2; // 2 gives best perf based on tuning
const int64_t nblocks_rows = (nrows_x + rows_per_block - 1) / rows_per_block;
const dim3 block_nums(nblocks_rows, nchannels_dst);
const dim3 block_nums(nblocks_rows, nchannels_dst + (fusion.shared_up != nullptr));
const dim3 block_dims(warp_size, ncols_dst);
const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(block_nums, block_dims, 0, stream);
@@ -1187,7 +1200,7 @@ static void mul_mat_vec_q_switch_ncols_dst(
constexpr bool c_halve_iters = decltype(halve_iters_tag)::value && c_promoted;
const std::pair<dim3, dim3> dims = calc_launch_params<type>(c_ncols_dst, nrows_x, nchannels_dst,
const std::pair<dim3, dim3> dims = calc_launch_params<type>(c_ncols_dst, nrows_x, nchannels_dst + (fusion.shared_up != nullptr),
nsamples_dst, warp_size, table_id, c_small_k, c_halve_iters);
mul_mat_vec_q_switch_fusion<type, c_ncols_dst, c_small_k, c_halve_iters>(
vx, vy, ids, fusion, dst, ncols_x, nchannels_y_fd, stride_row_x, stride_col_y, stride_col_dst,
@@ -1454,6 +1467,23 @@ void ggml_cuda_mul_mat_vec_q(
// non-negligible for some models such as gpt-oss-20b
GGML_ASSERT((fusion->x_scale == nullptr && fusion->gate_scale == nullptr) || src0->type == GGML_TYPE_NVFP4);
if (fusion->shared_up) {
GGML_ASSERT(ids && fusion->gate && fusion->shared_gate && fusion->shared_dst);
GGML_ASSERT(!fusion->x_bias && !fusion->gate_bias && !fusion->x_scale && !fusion->gate_scale);
GGML_ASSERT(ne11 == 1 && ne03 == 1 && ne13 == 1);
GGML_ASSERT(fusion->shared_up->type == src0->type && fusion->shared_gate->type == src0->type);
GGML_ASSERT(ggml_are_same_shape(fusion->shared_up, fusion->shared_gate));
GGML_ASSERT(ggml_is_contiguous(fusion->shared_up) && ggml_is_contiguous(fusion->shared_gate));
GGML_ASSERT(fusion->shared_up->ne[0] == ne00 && fusion->shared_up->ne[1] == ne01);
GGML_ASSERT(fusion->shared_up->nb[1] == nb01 && ggml_is_matrix(fusion->shared_up));
GGML_ASSERT(fusion->shared_dst->type == GGML_TYPE_F32 && ggml_is_contiguous(fusion->shared_dst));
GGML_ASSERT(fusion->shared_dst->ne[0] == ne0 && fusion->shared_dst->ne[1] == ne2);
fusion_local.shared_up = fusion->shared_up->data;
fusion_local.shared_gate = fusion->shared_gate->data;
fusion_local.shared_dst = (float *) fusion->shared_dst->data;
fusion_local.shared_stride_col_dst = fusion->shared_dst->nb[1] / ts_dst;
}
if (fusion->x_bias) {
GGML_ASSERT(fusion->x_bias->type == GGML_TYPE_F32);
GGML_ASSERT(fusion->x_bias->ne[0] == dst->ne[0]);
+12 -2
View File
@@ -484,13 +484,23 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_lightning_indexe
const ggml_tensor * op) {
GGML_ASSERT(op->op == GGML_OP_LIGHTNING_INDEXER);
char base[256];
char name[256];
snprintf(name, 256, "kernel_lightning_indexer_%s", ggml_type_name(op->src[1]->type));
const int16_t nh = op->src[0]->ne[1];
snprintf(base, 256, "kernel_lightning_indexer_%s", ggml_type_name(op->src[1]->type));
snprintf(name, 256, "%s_nh=%d", base, nh);
ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
if (!res.pipeline) {
res = ggml_metal_library_compile_pipeline(lib, name, name, nullptr);
ggml_metal_cv_t cv = ggml_metal_cv_init();
ggml_metal_cv_set_int16(cv, nh, FC_LIGHTNING_INDEXER + 0);
res = ggml_metal_library_compile_pipeline(lib, base, name, cv);
ggml_metal_cv_free(cv);
}
return res;
+1 -2
View File
@@ -1770,8 +1770,7 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
}
return has_simdgroup_mm; // TODO: over-restricted for vec-kernels
case GGML_OP_LIGHTNING_INDEXER:
if (op->src[0]->ne[0] != OP_LIGHTNING_INDEXER_DK ||
op->src[0]->ne[1] != OP_LIGHTNING_INDEXER_NH) {
if (op->src[0]->ne[0] != OP_LIGHTNING_INDEXER_DK) {
return false;
}
if (!has_simdgroup_mm ||
+1 -1
View File
@@ -122,6 +122,7 @@
#define FC_DSV4_HC 2000
#define FC_PAD 2100
#define FC_FLASH_ATTN_EXT_TENSOR 2200
#define FC_LIGHTNING_INDEXER 2200
// op-specific constants
#define OP_FLASH_ATTN_EXT_NQPSG 8
@@ -136,7 +137,6 @@
#define OP_FLASH_ATTN_EXT_VEC_NCPSG 32
#define OP_LIGHTNING_INDEXER_DK 128
#define OP_LIGHTNING_INDEXER_NH 64
#define OP_LIGHTNING_INDEXER_NHPTG 8
#define OP_LIGHTNING_INDEXER_NKPSG 8
#define OP_LIGHTNING_INDEXER_NSG 8
-1
View File
@@ -1372,7 +1372,6 @@ int ggml_metal_op_lightning_indexer(ggml_metal_op_t ctx, int idx) {
GGML_ASSERT(op->type == GGML_TYPE_F32);
GGML_ASSERT(q->ne[0] == OP_LIGHTNING_INDEXER_DK);
GGML_ASSERT(q->ne[1] == OP_LIGHTNING_INDEXER_NH);
ggml_metal_kargs_lightning_indexer args = {
/*.n_kv =*/ (int32_t) k->ne[2],
+11 -5
View File
@@ -329,6 +329,8 @@ kernel void kernel_flash_attn_ext_vec_reduce(
#undef DV
}
constant short FC_lightning_indexer_nh [[function_constant(FC_LIGHTNING_INDEXER + 0)]];
template<
typename kd4x4_t,
short nl_k,
@@ -345,7 +347,7 @@ kernel void kernel_lightning_indexer(
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
constexpr short DK = OP_LIGHTNING_INDEXER_DK;
constexpr short NH = OP_LIGHTNING_INDEXER_NH;
const short NH = FC_lightning_indexer_nh;
constexpr short NHPTG = OP_LIGHTNING_INDEXER_NHPTG;
constexpr short NKPSG = OP_LIGHTNING_INDEXER_NKPSG;
constexpr short NSG = OP_LIGHTNING_INDEXER_NSG;
@@ -411,18 +413,22 @@ kernel void kernel_lightning_indexer(
float score = 0.0f;
FOR_UNROLL (short i_head = 0; i_head < NH; i_head += NHPTG) {
// stage the Q tile [DK, NHPTG] and the (prescaled) head weights
// stage the Q tile [DK, NHPTG] and the (prescaled) head weights, heads past NH are zero
for (short i = tiitg; i < NHPTG*DK4; i += NTG) {
const short ih = i/DK4;
const short i4 = i%DK4;
device const float4 * q4 = (device const float4 *) (pq + (i_head + ih)*args.nbq1);
if (i_head + ih < NH) {
device const float4 * q4 = (device const float4 *) (pq + (i_head + ih)*args.nbq1);
sq4[ih*DK4 + i4] = half4(q4[i4]);
sq4[ih*DK4 + i4] = half4(q4[i4]);
} else {
sq4[ih*DK4 + i4] = half4(0.0h);
}
}
if (tiitg < NHPTG) {
sw[tiitg] = ((device const float *) pw)[i_head + tiitg];
sw[tiitg] = i_head + tiitg < NH ? ((device const float *) pw)[i_head + tiitg] : 0.0f;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
+11
View File
@@ -13,6 +13,17 @@ ggml_add_backend_library(ggml-openvino
target_link_libraries(ggml-openvino PRIVATE openvino::runtime openvino::threading OpenCL::OpenCL)
# the OpenVINO RTTI macros take one argument and leave __VA_ARGS__ empty, which -Wpedantic reports
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
target_compile_options(ggml-openvino PRIVATE -Wno-gnu-zero-variadic-macro-arguments)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(ggml-openvino PRIVATE -Wno-pedantic)
endif()
if (WIN32)
target_link_libraries(ggml-openvino PRIVATE psapi)
endif()
if (GGML_OPENVINO)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
+44
View File
@@ -0,0 +1,44 @@
# Compiled model cache
`GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR` exports compiled CPU/GPU graphs with their weights. It bypasses the plugin-level `GGML_OPENVINO_CACHE_DIR` and uses `OPTIMIZE_SPEED`, so weightless caching is disabled.
One directory can hold blobs for different models and compilation settings. Run each intended workload once to export its dynamic graph:
```sh
GGML_OPENVINO_DEVICE=GPU \
GGML_OPENVINO_NATIVE_SOFTPLUS=1 \
GGML_OPENVINO_DISABLE_KV_SLICE=1 \
GGML_OPENVINO_REQUANT_KQUANT=q4_asym64_all \
GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR=/path/to/qwen-cache \
./build/ReleaseOV/bin/llama-bench -m /path/to/model.gguf -r 1
```
`GGML_OPENVINO_SPILL_DIR` remains optional for this first run. Wait for the `model cache WROTE` message and completion of the workload before stopping it. Compatible prefill and decode graphs share one blob and manifest. A graph with different ports or incompatible shapes gets an exact entry instead; interrupted exports are not cache hits.
On later runs, supply the same compilation settings and enable `GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY=1`:
```sh
GGML_OPENVINO_DEVICE=GPU \
GGML_OPENVINO_NATIVE_SOFTPLUS=1 \
GGML_OPENVINO_DISABLE_KV_SLICE=1 \
GGML_OPENVINO_REQUANT_KQUANT=q4_asym64_all \
GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR=/path/to/qwen-cache \
GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY=1 \
./build/ReleaseOV/bin/llama-bench -m /path/to/model.gguf -r 1
```
Cache-only mode allocates backend address space without filling weight pages. On Windows, this also uses system commit capacity. The model-buffer size in the loader log is this virtual size. Weight uploads only record source identity; they do not read or requantize the weights. Graph conversion and compilation are skipped. Runtime buffers are still allocated and populated normally.
Cache-only mode uses the settings provided by the current process. Keep these values exactly the same, including set versus unset: `GGML_OPENVINO_REQUANT_KQUANT`, `GGML_OPENVINO_NATIVE_SOFTPLUS`, `GGML_OPENVINO_DISABLE_KV_SLICE`, `GGML_OPENVINO_MANUAL_GQA_ATTN`, `GGML_OPENVINO_STATEFUL_EXECUTION`, `GGML_OPENVINO_DISABLE_KV_STATE_RELAYOUT`, `GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS`, `GGML_OPENVINO_REDUCE_COMPILE_MEM`, `GGML_OPENVINO_MEMORY_OPTIMIZE`, `GGML_OPENVINO_PROFILING`, and `GGML_OPENVINO_DEBUG_NODE`. On GPU, also repeat `GGML_OPENVINO_MOE_OP=0` if used. `GGML_OPENVINO_SPILL_DIR` is optional on the first run and ignored in cache-only mode; host-weight release is disabled in cache-only mode.
A missing or incompatible graph fails with an error instead of compiling with absent weights. The fingerprint uses the dynamic graph's topology, ports, model parameters, weights, settings, and OpenVINO version; changing only dynamic token or KV sizes does not require a new entry. A different workload can still require another graph; populate it first without cache-only mode.
## Restrictions
- Cache-only mode requires Linux or Windows and mmap loading (`--load-mode mmap`, or the default when all selected devices support mmap). Do not use tensor validation or mlock when trying to avoid weight reads.
- On Windows, the backend commits virtual memory for its buffers without touching weight pages. Large models can still reach the system commit limit.
- The model must execute entirely on OpenVINO, with dynamic CPU/GPU graphs and in-process caching enabled. Static/NPU execution and CPU fallback are unsupported in cache-only mode.
- GGUF metadata, tokenizer data, tensor descriptors, and graph construction are still needed. The llama.cpp loader is unchanged: depending on its prefetch settings, it may request pages with `MAP_POPULATE` or read-ahead on Linux, or `PrefetchVirtualMemory` on Windows. Non-mmap loading also reads the payload before the backend sees it.
- File identity, size, modification/change timestamps, tensor offsets, graph structure, settings, and OpenVINO version identify cache entries. Linux uses device/inode and Windows uses volume serial/file index. Replacing, copying, or modifying a GGUF invalidates its entries. This avoids reading weight bytes and ties the cache to the local source files. Keep those files unchanged throughout loading and inference.
- Use the same target device and compatible OpenVINO/plugin installation. Import support depends on the plugin; the tested CPU plugin cannot import MoE graphs containing `GatherMatmulCompressed`. GPU MoE and CPU dense graph imports were tested.
- Blobs contain weights and can approach model size for each compiled graph. Import still reads those blobs and initializes the device.
+208 -20
View File
@@ -87,6 +87,14 @@ void GgmlOvDecoder::update_io(ggml_cgraph * cgraph) {
compute_model_outputs();
}
// llama keeps separate graphs for batches with and without outputs, so a cache hit can come from a
// graph built in other memory. The decoder then still points at the old graph's tensors.
bool GgmlOvDecoder::is_bound_to(const ggml_cgraph * cgraph) const {
return m_cgraph == cgraph && cgraph->n_nodes > 0 && m_node_info_list.size() == (size_t) cgraph->n_nodes &&
m_node_info_list.front().node == cgraph->nodes[0] &&
m_node_info_list.back().node == cgraph->nodes[cgraph->n_nodes - 1];
}
GgmlOvDecoder::GgmlOvDecoder(ggml_cgraph * cgraph, std::map<std::string, std::shared_ptr<ov::Node>> & model_weights) {
m_cgraph = cgraph;
m_model_weights = model_weights;
@@ -117,6 +125,12 @@ bool is_same_shape(const ggml_tensor * a, const ggml_tensor * b) {
bool is_conv_states_all_tensor(const ggml_tensor * tensor) {
return tensor != nullptr && strncmp(tensor->name, "conv_states_all", strlen("conv_states_all")) == 0;
}
bool is_full_single_slot_writeback(const ggml_tensor * node) {
return node->view_src != nullptr && node->view_src->ne[1] == 1 && node->src[1] != nullptr &&
node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src == node->view_src &&
node->src[1]->view_offs == 0 && ggml_nbytes(node->src[1]) == ggml_nbytes(node->view_src);
}
} // namespace
// MoE expert aggregation (build_moe_ffn in llama-graph.cpp): each expert plane is
@@ -274,8 +288,34 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
int op_case = 0;
switch (node->op) {
case GGML_OP_RESHAPE: {
if (m_naive) {
break;
}
auto name = std::string(node->name);
auto * src = node->src[0];
// Identify recurrent sequence reshapes before size checks, which are ambiguous for one token.
bool recurrent_sequence = false;
for (int i = 0; i < m_cgraph->n_nodes && !recurrent_sequence; ++i) {
const auto * consumer = m_cgraph->nodes[i];
if (consumer->op == GGML_OP_MUL_MAT_ID && consumer->src[1] == node) {
return 1;
} else if (consumer->op == GGML_OP_SSM_CONV) {
const auto * concat = consumer->src[0];
if (concat->op == GGML_OP_CONCAT) {
const auto * transposed = concat->src[1];
recurrent_sequence = transposed->op == GGML_OP_TRANSPOSE && transposed->src[0] == node;
}
} else if (consumer->op == GGML_OP_UNARY && ggml_get_unary_op(consumer) == GGML_UNARY_OP_SOFTPLUS) {
const auto * biased = consumer->src[0];
recurrent_sequence = biased->op == GGML_OP_ADD && biased->src[0] == node;
}
}
if (recurrent_sequence && node->ne[0] == src->ne[0] && node->ne[3] == 1) {
return 6;
}
if (node->ne[0] == src->ne[0] && node->ne[2] == 1 && node->ne[3] == 1) {
return 5;
}
if (src->op == GGML_OP_RESHAPE && src->src[0]->ne[0] == node->ne[0] && src->src[0]->ne[1] == node->ne[1]) {
op_case = 4;
} else if (node->ne[0] * node->ne[1] == src->ne[0]) {
@@ -285,7 +325,7 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
if (src->ne[2] * src->ne[3] == node->ne[1]) {
op_case = 5;
}
} else if (src->ne[0] * src->ne[1] * src->ne[2] == node->ne[1]) {
} else if (node->ne[0] == 1 && src->ne[0] * src->ne[1] * src->ne[2] == node->ne[1]) {
op_case = 3;
} else if (name.find("linear_attn_qkv_mixed") == 0 || name.find("alpha") == 0) {
op_case = 6;
@@ -294,6 +334,38 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
} else if (name.find("state_predelta") == 0) {
op_case = 8;
}
if (op_case == 1 && m_is_stateful) {
// Recurrent convolution and GDN gates retain their rank-4 layout.
bool recurrent = src->op == GGML_OP_GET_ROWS && is_recurrent_cache(src->src[0]);
for (int i = 0; i < m_cgraph->n_nodes && !recurrent; ++i) {
const auto * consumer = m_cgraph->nodes[i];
if (consumer->op == GGML_OP_GATED_DELTA_NET) {
for (int j : {3, 4}) {
const auto * gate = consumer->src[j];
if (gate->op == GGML_OP_UNARY) {
gate = gate->src[0];
}
recurrent = recurrent || gate == node;
}
} else if (consumer->op == GGML_OP_MUL) {
for (int j = 0; j < 2; ++j) {
const auto * gate = consumer->src[j];
const auto * norm = consumer->src[1 - j];
if (gate->op != GGML_OP_UNARY || gate->src[0] != node) {
continue;
}
if (norm->op == GGML_OP_MUL) {
norm = norm->src[0];
}
recurrent = recurrent || (norm->op == GGML_OP_RMS_NORM && norm->src[0]->op == GGML_OP_VIEW &&
norm->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET);
}
}
}
if (recurrent) {
op_case = 9;
}
}
break;
}
case GGML_OP_PERMUTE: {
@@ -342,11 +414,12 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
if (node->src[1]->op == GGML_OP_VIEW) {
// GET_ROWS gathering recurrent state cache rows via the inp->s_copy index list:
// src[0] is a reshape of cache_r/cache_s, src[1] is a view of the s_copy leaf.
// op_case 3: main view (active sequences, view offset 0)
// op_case 4: extra view (defrag remainder, nonzero view offset)
// op_case 1/2: active/extra rows of a multi-slot cache
// op_case 3/4: active/extra rows of a single-slot cache
if (node->src[0]->op == GGML_OP_RESHAPE && node->src[0]->src[0] != nullptr &&
is_kvcache(node->src[0]->src[0], nullptr)) {
op_case = node->src[1]->view_offs == 0 ? 1 : 2;
is_recurrent_cache(node->src[0]->src[0])) {
const bool single_slot = node->src[0]->src[0]->ne[1] == 1;
op_case = (node->src[1]->view_offs == 0 ? 1 : 2) + (single_slot ? 2 : 0);
}
}
break;
@@ -362,6 +435,14 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
op_case = 2;
break;
}
case GGML_ROPE_TYPE_VISION: {
op_case = 3;
break;
}
case GGML_ROPE_TYPE_MROPE: {
op_case = 4;
break;
}
default:
op_case = 0;
break;
@@ -369,6 +450,12 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
break;
}
case GGML_OP_VIEW: {
if (!m_model_params.has_rs_rollback && node->src[0] != nullptr &&
node->src[0]->op == GGML_OP_GATED_DELTA_NET) {
// The GDN translator publishes native attention/state outputs under these VIEW names.
op_case = 2;
break;
}
if (m_is_static && node->src[0] != nullptr &&
(node->src[0]->op == GGML_OP_GATED_DELTA_NET || node->src[0]->op == GGML_OP_CONCAT)) {
// VIEW slicing a GATED_DELTA_NET combined [attn|state] output, or the conv_input
@@ -426,6 +513,10 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
if (node->src[0]->op == GGML_OP_VIEW) {
if (is_same_shape(node->src[0]->src[0], node->src[0])) {
op_case = 1;
} else if (!m_model_params.has_rs_rollback &&
node->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET) {
// GDN attention is routed directly to this VIEW by get_output_names().
op_case = 3;
} else if (node->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET) {
op_case = 2;
}
@@ -449,12 +540,40 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
}
break;
}
case GGML_OP_UPSCALE: {
const int32_t mode_flags = node->op_params[0];
const ggml_scale_mode scale_mode = static_cast<ggml_scale_mode>(mode_flags & 0xFF);
switch (scale_mode) {
case GGML_SCALE_MODE_NEAREST: {
op_case = 1;
break;
}
case GGML_SCALE_MODE_BILINEAR: {
op_case = 2;
break;
}
case GGML_SCALE_MODE_BICUBIC: {
op_case = 3;
break;
}
default:
op_case = 0;
break;
}
break;
}
case GGML_OP_CPY: {
if (node->src[0]->op == GGML_OP_VIEW) {
if (node->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET) {
op_case = 1;
if (!m_model_params.has_rs_rollback) {
// op_case 7 replaces a single-slot cache; op_case 10 writes native GDN state
// into an active range of a larger non-rollback cache.
op_case = is_full_single_slot_writeback(node) ? 7 : 10;
} else {
op_case = 1;
}
} else if (GgmlOvDecoder::is_conv_state_writeback(node)) {
op_case = 2;
op_case = is_full_single_slot_writeback(node) ? 8 : 2;
break;
} else if (is_conv_states_all_tensor(node->view_src) && node->src[1] != nullptr &&
node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src == node->view_src) {
@@ -463,9 +582,9 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
}
} else if (node->src[0]->op == GGML_OP_GET_ROWS && node->src[1] != nullptr &&
node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src != nullptr &&
is_kvcache(node->src[1]->view_src, nullptr)) {
is_recurrent_cache(node->src[1]->view_src)) {
// s_copy defrag remainder writeback: gathered extra state rows copied back into the cache
op_case = 3;
op_case = node->src[1]->view_src->ne[1] == 1 ? 9 : 3;
} else if (node->src[1] != nullptr && node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src != nullptr) {
// op_case 5: KV write for decoder self-attention (dynamic write offset)
// op_case 6: KV write for encoder self-attn or cross-attn (static offset)
@@ -504,7 +623,7 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
}
case GGML_OP_SCALE: {
if (node->view_src && node->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY) {
op_case = 1;
op_case = node->view_src->ne[1] == 1 ? 2 : 1;
}
break;
}
@@ -858,35 +977,48 @@ std::pair<ModelParams, ComputeParams> GgmlOvDecoder::compute_llm_params(ggml_cgr
if (node->op == GGML_OP_GATED_DELTA_NET) {
model_params.state_size = node->src[0]->ne[0];
}
if (node->op == GGML_OP_SCALE && node->view_src != nullptr && is_kvcache(node->view_src, nullptr)) {
if (node->op == GGML_OP_SCALE && node->view_src != nullptr && is_recurrent_cache(node->view_src)) {
if (model_params.n_rs_slots == -1) {
model_params.n_rs_slots = node->view_src->ne[1];
} else {
GGML_ASSERT(model_params.n_rs_slots == node->view_src->ne[1]);
}
compute_params.cache_rs_reset_len = ggml_nelements(node) / node->view_src->ne[0];
compute_params.cache_rs_reset_idx = node->src[0]->view_offs / node->view_src->ne[0];
}
// Capture the destination slot block of every recurrent state cache writeback, plus the
// conv_input window the conv state writeback copies. The active sequences occupy a
// contiguous slot block [begin, begin + n_seqs) of the cache; the block and the window move
// source window needed by conv state and packed GDN rollback writes. The active sequences
// occupy a contiguous slot block [begin, begin + n_seqs) of the cache; these offsets move
// with the batch, so they are fed to the cached model as runtime inputs.
if (node->op == GGML_OP_CPY && node->view_src != nullptr && is_kvcache(node->view_src, nullptr) &&
if (node->op == GGML_OP_CPY && node->view_src != nullptr && is_recurrent_cache(node->view_src) &&
node->src[1] != nullptr && node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src == node->view_src) {
const bool is_conv = is_conv_state_writeback(node);
const bool is_gdn = node->src[0]->op == GGML_OP_VIEW && node->src[0]->src[0] != nullptr &&
node->src[0]->src[0]->op == GGML_OP_GATED_DELTA_NET;
const bool is_extra = node->src[0]->op == GGML_OP_GET_ROWS;
const bool is_gdn_rollback = is_gdn && is_same_shape(node->src[0], node->src[1]);
const ggml_tensor * dest_view = node->src[1];
const ggml_tensor * cache = node->view_src;
const size_t row_bytes = cache->ne[0] * ggml_type_size(cache->type);
if (row_bytes > 0 && (is_conv || is_gdn || is_extra)) {
if (is_gdn_rollback) {
// Rollback GDN exposes an already-flattened [state, seq, snapshot] VIEW and copies
// it to an identically-shaped cache VIEW. Non-rollback copies native 4-D state
// [value, key, head, seq] into flattened cache rows, so the shapes differ. This
// signature is local to the CPY and still works when fallback splits the graph.
model_params.has_rs_rollback = true;
}
if (row_bytes > 0 && (is_conv || is_gdn || is_extra) && !is_full_single_slot_writeback(node)) {
ComputeParams::RsWriteback writeback;
writeback.slot_begin = (int) (dest_view->view_offs / row_bytes);
if (is_conv) {
writeback.src_begin = (int) (node->src[0]->view_offs / node->src[0]->view_src->nb[0]);
} else if (is_gdn) {
} else if (is_gdn_rollback) {
writeback.src_begin = (int) (node->src[0]->view_offs / node->src[0]->view_src->nb[1]);
}
compute_params.rs_writebacks[get_tensor_ov_name(cgraph, node)] = writeback;
}
if (is_conv || is_gdn) {
if ((is_conv || is_gdn) && !is_full_single_slot_writeback(node)) {
compute_params.s_copy_active_slot_len = (int) dest_view->ne[1];
}
}
@@ -975,6 +1107,12 @@ ov::PartialShape GgmlOvDecoder::get_graph_input_shape(const ggml_tensor * op,
input_shape = ov::PartialShape{-1, 1, -1, -1};
}
} else if (is_recurrent_cache(input)) {
input_shape = ov::PartialShape{get_shape(input)};
if (!m_is_static && !m_is_stateful && input->ne[1] > 1) {
input_shape[2] = -1;
}
} else if (is_kvcache(input, op)) {
// kvcache
input_shape = ov::PartialShape{get_shape(input)};
@@ -1057,7 +1195,7 @@ bool GgmlOvDecoder::is_s_copy_leaf(const ggml_tensor * tensor) const {
while (data != nullptr && (data->op == GGML_OP_VIEW || data->op == GGML_OP_RESHAPE)) {
data = data->src[0];
}
if (data != nullptr && is_kvcache(data, nullptr)) {
if (data != nullptr && is_recurrent_cache(data)) {
return true;
}
}
@@ -1095,7 +1233,7 @@ void GgmlOvDecoder::add_extra_inputs() {
}
// create_1d_input("token_len", m_compute_params.token_len_per_seq * m_compute_params.n_seq_active);
if (m_compute_params.cache_rs_reset_idx != -1) {
if (m_compute_params.cache_rs_reset_idx != -1 && m_model_params.n_rs_slots != 1) {
// Whether/which cache slot to reset varies per compute call (e.g. a new sequence starting
// vs. continued decoding). can_reuse_statically() does not invalidate the cached static
// model on ComputeParams changes, so these must stay runtime Parameters even when static
@@ -1119,7 +1257,7 @@ void GgmlOvDecoder::add_extra_inputs() {
for (const auto & [node_name, writeback] : m_compute_params.rs_writebacks) {
create_1d_input("rs_slot_begin_" + node_name, writeback.slot_begin);
if (!m_is_static) {
if (!m_is_static && writeback.src_begin >= 0) {
create_1d_input("rs_src_begin_" + node_name, writeback.src_begin);
}
}
@@ -1216,6 +1354,9 @@ void GgmlOvDecoder::compute_model_outputs() {
if (cur_node->op == GGML_OP_NONE || cur_node->op == GGML_OP_VIEW || cur_node->op == GGML_OP_RESHAPE) {
continue;
}
if (::is_inplace_op(cur_node) && ggml_nbytes(cur_node) == 0) {
continue;
}
auto cur_node_use_count = m_cgraph->use_counts[ggml_hash_find(&m_cgraph->visited_hash_set, cur_node)];
if (cur_node_use_count == 0) {
// The output of in-place ops is the view_src tensor, which is updated in place. We should use the view_src name as the output name to make sure it can be correctly matched with the later ops that use the view_src.
@@ -1822,6 +1963,27 @@ std::vector<size_t> GgmlOvDecoder::get_output_stride(int node_idx) const {
}
std::vector<std::string> GgmlOvDecoder::get_output_names(int node_idx) const {
auto * node = m_node_info_list[node_idx].node;
if (node->op == GGML_OP_GATED_DELTA_NET && !m_model_params.has_rs_rollback) {
std::string attn_name;
std::string state_name;
for (int i = node_idx + 1; i < m_cgraph->n_nodes; i++) {
auto * consumer = m_cgraph->nodes[i];
if (consumer->op != GGML_OP_VIEW || consumer->src[0] != node) {
continue;
}
// GGML packs [attention | state]. The attention VIEW starts at offset 0 and the
// state VIEW starts after the token-dependent attention segment.
auto & name = consumer->view_offs == 0 ? attn_name : state_name;
if (!name.empty()) {
return {m_node_info_list[node_idx].node_name};
}
name = get_tensor_ov_name(m_cgraph, consumer);
}
if (!attn_name.empty() && !state_name.empty()) {
return {attn_name, state_name};
}
}
return {m_node_info_list[node_idx].node_name};
}
@@ -2154,6 +2316,11 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
case GGML_OP_DIV:
case GGML_OP_CLAMP:
case GGML_OP_PAD:
case GGML_OP_UPSCALE:
case GGML_OP_SIN:
case GGML_OP_COS:
case GGML_OP_LOG:
case GGML_OP_ROLL:
m_node_dynamic_dims[node] = m_node_dynamic_dims[node->src[0]];
break;
case GGML_OP_SUM_ROWS:
@@ -2168,6 +2335,8 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
break;
case GGML_OP_CPY:
case GGML_OP_SET_ROWS:
case GGML_OP_SUM:
case GGML_OP_MEAN:
m_node_dynamic_dims[node] = -1;
break;
case GGML_OP_IM2COL: {
@@ -2198,6 +2367,25 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
}
break;
}
case GGML_OP_IM2COL_3D: {
m_node_dynamic_dims[node] = -1;
if (m_node_dynamic_dims[node->src[1]] != -1) {
const int src_dyn = m_node_dynamic_dims[node->src[1]];
if (src_dyn == 0) {
m_node_dynamic_dims[node] = 1; // IW -> OW
} else if (src_dyn == 1) {
m_node_dynamic_dims[node] = 2; // IH -> OH
} else if (src_dyn == 3) {
m_node_dynamic_dims[node] = 3; // N -> N
}
if (m_node_dynamic_dims[node] != -1) {
OPENVINO_ASSERT(node->src[1]->ne[src_dyn] == node->ne[m_node_dynamic_dims[node]],
"Dynamic dim value mismatch for IM2COL_3D node: " + std::string(node->name) +
" and its src[1]: " + std::string(node->src[1]->name));
}
}
break;
}
default:
GGML_LOG_DEBUG("ggml-openvino: compute_node_dynamic_dims: unhandled op %s for node '%s'\n",
ggml_op_name(node->op), node->name);
+30 -11
View File
@@ -28,7 +28,9 @@ struct ModelParams {
std::map<int, int> n_heads_kv_per_layer;
int head_size = -1;
int state_size = -1; // for SSM molels, eg qwen35
int32_t rope_params[16];
int32_t rope_params[16] = {};
int n_rs_slots = -1;
bool has_rs_rollback = false;
bool mixed_rope_params = false;
bool is_cacheless_attn = false;
std::vector<int> swa_layers;
@@ -45,9 +47,15 @@ struct ModelParams {
memcmp(rope_params, other.rope_params, sizeof(int32_t) * 16) == 0;
}
bool can_reuse_dynamically(const ModelParams & other) const { return same_rope_params(other); }
bool can_reuse_dynamically(const ModelParams & other) const {
return same_rope_params(other) && n_rs_slots == other.n_rs_slots &&
has_rs_rollback == other.has_rs_rollback;
}
bool can_reuse_statically(const ModelParams & other) const { return same_rope_params(other) && ctx == other.ctx; }
bool can_reuse_statically(const ModelParams & other) const {
return same_rope_params(other) && ctx == other.ctx && n_rs_slots == other.n_rs_slots &&
has_rs_rollback == other.has_rs_rollback;
}
bool kv_buffer_changed(const ModelParams & other) const { return kv_buffer_ctx_id != other.kv_buffer_ctx_id; }
};
@@ -100,7 +108,7 @@ struct ComputeParams {
struct RsWriteback {
int slot_begin = 0; // first cache slot written by the CPY
int src_begin = 0; // first source row or column copied by the CPY
int src_begin = -1; // first source column copied by a conv-state CPY
};
std::map<std::string, RsWriteback> rs_writebacks;
@@ -353,6 +361,7 @@ public:
void add_extra_inputs();
void update_io(ggml_cgraph * cgraph);
bool is_bound_to(const ggml_cgraph * cgraph) const;
static bool is_inp_tok(const ggml_tensor * tensor, const ggml_tensor * op) {
return op->op == GGML_OP_GET_ROWS && tensor == op->src[1] && op->src[0]->op == GGML_OP_NONE;
@@ -362,10 +371,11 @@ public:
return op->op == GGML_OP_ROPE && tensor == op->src[1];
}
// IMROPE packs 4 stacked position planes (t/h/w/e) into inp_pos, each of length
// IMROPE and VISION pack 4 stacked position planes (t/h/w/e) into inp_pos, each of length
// n_tokens; other modes carry a single position per token.
static int get_inp_pos_n_planes(const ggml_tensor * op) {
return op->op_params[2] == GGML_ROPE_TYPE_IMROPE ? 4 : 1;
const int mode = op->op_params[2];
return (mode == GGML_ROPE_TYPE_IMROPE || mode == GGML_ROPE_TYPE_VISION || (mode & GGML_ROPE_TYPE_MROPE)) ? 4 : 1;
}
static bool is_inp_emb(const ggml_tensor * tensor, const ggml_tensor * op) {
@@ -387,17 +397,26 @@ public:
return op->op == GGML_OP_ROPE && tensor == op->src[2];
}
// also returns true for cache_s and cache_r in SSM/DeltaNet models
static bool is_kvcache(const ggml_tensor * tensor, const ggml_tensor * op) {
if (tensor == nullptr) {
inline static bool is_recurrent_cache(const ggml_tensor * tensor) {
return tensor != nullptr && (strncmp(tensor->name, "cache_r_l", strlen("cache_r_l")) == 0 ||
strncmp(tensor->name, "cache_s_l", strlen("cache_s_l")) == 0 ||
strncmp(tensor->name, "cache_ple_r_l", strlen("cache_ple_r_l")) == 0);
}
inline static bool is_cache(const ggml_tensor * tensor, const ggml_tensor * op) {
return is_recurrent_cache(tensor) || is_kvcache(tensor, op);
}
inline static bool is_kvcache(const ggml_tensor * tensor, const ggml_tensor * op) {
if (tensor == nullptr || is_recurrent_cache(tensor)) {
return false;
}
return (tensor->buffer != nullptr && tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY) ||
(op != nullptr && op->op == GGML_OP_SET_ROWS && op->src[2] == tensor);
}
static bool is_conv_state_writeback(const ggml_tensor * node) {
return node->op == GGML_OP_CPY && node->view_src != nullptr && is_kvcache(node->view_src, nullptr) &&
inline static bool is_conv_state_writeback(const ggml_tensor * node) {
return node->op == GGML_OP_CPY && node->view_src != nullptr && is_recurrent_cache(node->view_src) &&
node->src[0] != nullptr && node->src[0]->op == GGML_OP_VIEW && node->src[0]->src[0] != nullptr &&
node->src[0]->src[0]->op == GGML_OP_CONCAT && node->src[1] != nullptr &&
node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src == node->view_src;
+162 -71
View File
@@ -2,12 +2,15 @@
#include "ggml-impl.h"
#include "ggml.h"
#include "model-cache.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <openvino/runtime/intel_gpu/ocl/ocl.hpp>
#include <openvino/runtime/intel_npu/level_zero/level_zero.hpp>
#include <openvino/runtime/properties.hpp>
#include <mutex>
#include <optional>
ov::Core & ov_singleton_core() {
@@ -15,14 +18,88 @@ ov::Core & ov_singleton_core() {
return core;
}
static bool has_prefix(const std::string & s, const std::string & prefix) {
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
}
static bool is_virtual_routing_device(const std::string & device_name) {
return has_prefix(device_name, "AUTO") || has_prefix(device_name, "MULTI") || has_prefix(device_name, "HETERO");
}
static std::vector<std::string> ov_enumerate_devices() {
std::vector<std::string> result;
for (const auto & device : ov_singleton_core().get_available_devices()) {
if (!is_virtual_routing_device(device)) {
result.push_back(device);
}
}
if (result.empty()) {
result.push_back("CPU");
}
std::sort(result.begin(), result.end());
result.erase(std::unique(result.begin(), result.end()), result.end());
return result;
}
std::string ggml_openvino_get_device_description(const std::string & device_name) {
std::string description = device_name;
try {
description = ov_singleton_core().get_property(device_name, ov::device::full_name);
} catch (...) {
return device_name;
}
if (has_prefix(device_name, "NPU")) {
try {
const std::string arch = ov_singleton_core().get_property(device_name, "DEVICE_ARCHITECTURE").as<std::string>();
if (!arch.empty()) {
description += " (NPU " + arch + ")";
}
} catch (...) {
}
}
return description;
}
// requested: GGML_OPENVINO_DEVICE, nullptr if unset. available_devices is never empty (see ov_enumerate_devices)
static std::string resolve_openvino_device_name(const std::vector<std::string> & available_devices,
const char * requested) {
auto available = [&](const std::string & name) {
return std::find(available_devices.begin(), available_devices.end(), name) != available_devices.end();
};
if (requested == nullptr) {
return available("CPU") ? "CPU" : available_devices.front();
}
if (!available(requested)) {
// No fallback to CPU (easy to miss) and no GPU -> GPU.0 alias (with iGPU + dGPU, GPU.0 is often the
// wrong one). List the devices here: --list-devices initializes this backend and would abort too.
std::string list;
for (const std::string & name : available_devices) {
list += "\n " + name + ": " + ggml_openvino_get_device_description(name);
}
GGML_ABORT("GGML OpenVINO Backend: GGML_OPENVINO_DEVICE=%s is not available. "
"Set it to one of the available OpenVINO devices:%s",
requested, list.c_str());
}
return requested;
}
// =====================================================
// Device Configuration Implementations
// =====================================================
void ggml_openvino_device_config::init() {
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
if (initialized) {
return;
}
// Set up front: a failed OpenCL setup below is not retried on every call
initialized = true;
// All recognized GGML_OPENVINO_* env vars. Their values are cached here
// once at backend init time and read back via ggml_openvino_getenv_str()
@@ -34,6 +111,7 @@ void ggml_openvino_device_config::init() {
"GGML_OPENVINO_SPILL_DIR",
"GGML_OPENVINO_DEBUG_NODE",
"GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR",
"GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY",
"GGML_OPENVINO_NPU_COMPILE_CONFIG",
// Integer values (use ggml_openvino_getenv_int)
"GGML_OPENVINO_PREFILL_CHUNK_SIZE",
@@ -53,6 +131,7 @@ void ggml_openvino_device_config::init() {
"GGML_OPENVINO_DISABLE_KV_SLICE",
"GGML_OPENVINO_ENABLE_FALLBACK",
"GGML_OPENVINO_MANUAL_GQA_ATTN",
"GGML_OPENVINO_MOE_OP",
"GGML_OPENVINO_MEMORY_OPTIMIZE",
"GGML_OPENVINO_RELEASE_WEIGHTS",
"GGML_OPENVINO_REDUCE_COMPILE_MEM",
@@ -62,6 +141,8 @@ void ggml_openvino_device_config::init() {
"GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS",
"GGML_OPENVINO_REQUANT_KQUANT",
"GGML_OPENVINO_DISABLE_KV_STATE_RELAYOUT",
// Build the precise (but O(n_nodes)) graph cache key. Needed by op tests.
"GGML_OPENVINO_FULL_GRAPH_KEY",
};
for (const char * const & env_var : env_var_names) {
@@ -71,16 +152,14 @@ void ggml_openvino_device_config::init() {
}
}
device_name = ggml_openvino_getenv_str("GGML_OPENVINO_DEVICE", "CPU");
auto available_devices = ov_singleton_core().get_available_devices();
if (std::find(available_devices.begin(), available_devices.end(), device_name) == available_devices.end()) {
GGML_LOG_WARN("GGML OpenVINO Backend: device %s is not available, fallback to CPU\n", device_name.c_str());
device_name = "CPU";
}
is_npu = (device_name == "NPU");
available_devices = ov_enumerate_devices();
device_name = resolve_openvino_device_name(available_devices, ggml_openvino_getenv_str("GGML_OPENVINO_DEVICE"));
is_npu = has_prefix(device_name, "NPU");
ggml_openvino_model_cache_init();
const char * cache_dir = ggml_openvino_getenv_str("GGML_OPENVINO_CACHE_DIR");
if (device_name == "NPU") {
if (has_prefix(device_name, "NPU")) {
compile_config = {
{"NPU_COMPILER_DYNAMIC_QUANTIZATION", "YES" },
{"NPU_USE_NPUW", "YES" },
@@ -106,48 +185,69 @@ void ggml_openvino_device_config::init() {
compile_config.insert(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
}
if (ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING") >= 2) {
compile_config.insert(ov::enable_profiling(true));
}
// Initialize remote context with queue sharing for GPU
if (device_name == "GPU") {
// Create OpenCL context and queue
if (has_prefix(device_name, "GPU")) {
// Use the OpenCL context OpenVINO created for this device, so GPU.N gets its own device
cl_context cl_ctx;
try {
auto ov_ctx = ov_singleton_core().get_default_context(device_name).as<ov::intel_gpu::ocl::ClContext>();
cl_ctx = ov_ctx.get();
} catch (const std::exception & e) {
// The consumers of the remote context have no host fallback, and OpenVINO
// already reported the device as present.
GGML_ABORT("ggml-openvino: failed to get the OpenCL context for %s: %s", device_name.c_str(), e.what());
}
cl_int err;
cl_platform_id platform;
err = clGetPlatformIDs(1, &platform, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("Failed to get OpenCL platform: %d\n", err);
return;
}
cl_device_id cl_device;
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &cl_device, nullptr);
err = clGetContextInfo(cl_ctx, CL_CONTEXT_DEVICES, sizeof(cl_device), &cl_device, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("Failed to get OpenCL device: %d\n", err);
return;
GGML_ABORT("ggml-openvino: failed to get the OpenCL device for %s: %d", device_name.c_str(), err);
}
cl_context cl_ctx = clCreateContext(nullptr, 1, &cl_device, nullptr, nullptr, &err);
cl_platform_id cl_platform;
err = clGetDeviceInfo(cl_device, CL_DEVICE_PLATFORM, sizeof(cl_platform), &cl_platform, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("Failed to create OpenCL context: %d\n", err);
return;
GGML_ABORT("ggml-openvino: failed to get the OpenCL platform for %s: %d", device_name.c_str(), err);
}
cl_queue = clCreateCommandQueueWithProperties(cl_ctx, cl_device, nullptr, &err);
cl_mem_fill_fn =
(clEnqueueMemFillINTEL_fn) clGetExtensionFunctionAddressForPlatform(cl_platform, "clEnqueueMemFillINTEL");
cl_mem_cpy_fn =
(clEnqueueMemcpyINTEL_fn) clGetExtensionFunctionAddressForPlatform(cl_platform, "clEnqueueMemcpyINTEL");
cl_ulong device_max_alloc = 0;
err = clGetDeviceInfo(cl_device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(device_max_alloc), &device_max_alloc,
nullptr);
if (err == CL_SUCCESS) {
max_alloc_size = device_max_alloc;
} else {
// not fatal, ggml then allocates one buffer
GGML_LOG_WARN("Failed to get OpenCL max allocation size: %d\n", err);
}
const cl_queue_properties profiling_properties[] = {
CL_QUEUE_PROPERTIES,
CL_QUEUE_PROFILING_ENABLE,
0,
};
const cl_queue_properties * queue_properties =
ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING") >= 2 ? profiling_properties : nullptr;
cl_queue = clCreateCommandQueueWithProperties(cl_ctx, cl_device, queue_properties, &err);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("Failed to create OpenCL command queue: %d\n", err);
clReleaseContext(cl_ctx);
return;
GGML_ABORT("ggml-openvino: failed to create the OpenCL queue for %s: %d", device_name.c_str(), err);
}
// Create OpenVINO remote context with queue sharing
remote_context = ov::intel_gpu::ocl::ClContext(ov_singleton_core(), cl_queue);
// Release the context (queue keeps a reference)
clReleaseContext(cl_ctx);
} else if (device_name == "NPU") {
} else if (has_prefix(device_name, "NPU")) {
// remote tensor is not used for NPU yet
// remote_context = ov_singleton_core().get_default_context(device_name);
}
initialized = true;
}
ggml_openvino_device_config::~ggml_openvino_device_config() {
@@ -173,6 +273,12 @@ const std::string & ggml_openvino_get_device_name() {
return ggml_openvino_get_device_config().device_name;
}
std::vector<std::string> ggml_openvino_get_available_devices() {
auto & config = ggml_openvino_get_device_config();
config.init();
return config.available_devices;
}
// Get the value of a GGML_OPENVINO_* env var as a string. Returns
// default_value when the var is unset or set to an empty string.
const char * ggml_openvino_getenv_str(const char * var, const char * default_value) {
@@ -198,12 +304,12 @@ bool ggml_openvino_reduce_compile_mem_enabled() {
return ggml_openvino_getenv_int("GGML_OPENVINO_MEMORY_OPTIMIZE") != 0;
}
bool ggml_openvino_release_weights_enabled(const std::string & device) {
bool ggml_openvino_release_weights_enabled() {
const char * release_weights = ggml_openvino_getenv_str("GGML_OPENVINO_RELEASE_WEIGHTS");
if (release_weights != nullptr) {
return device == "GPU" && ggml_openvino_getenv_int("GGML_OPENVINO_RELEASE_WEIGHTS") != 0;
return ggml_openvino_is_gpu() && ggml_openvino_getenv_int("GGML_OPENVINO_RELEASE_WEIGHTS") != 0;
}
return device == "GPU" && ggml_openvino_getenv_int("GGML_OPENVINO_MEMORY_OPTIMIZE") != 0;
return ggml_openvino_is_gpu() && ggml_openvino_getenv_int("GGML_OPENVINO_MEMORY_OPTIMIZE") != 0;
}
// Check if running on NPU
@@ -211,6 +317,14 @@ bool ggml_openvino_is_npu() {
return ggml_openvino_get_device_config().is_npu;
}
bool ggml_openvino_is_gpu() {
return has_prefix(ggml_openvino_get_device_name(), "GPU");
}
size_t ggml_openvino_max_alloc_size() {
return ggml_openvino_get_device_config().max_alloc_size;
}
// Get the remote context for the current device (returns empty optional for CPU)
std::optional<ov::RemoteContext> ggml_openvino_get_remote_context() {
return ggml_openvino_get_device_config().remote_context;
@@ -226,32 +340,14 @@ cl_command_queue ggml_openvino_get_cl_queue() {
return ggml_openvino_get_device_config().cl_queue;
}
// Get the clEnqueueMemFillINTEL function pointer (lazy load)
// Get the clEnqueueMemFillINTEL function pointer
clEnqueueMemFillINTEL_fn ggml_openvino_get_clEnqueueMemFillINTEL() {
static clEnqueueMemFillINTEL_fn fn = nullptr;
static bool loaded = false;
if (!loaded) {
loaded = true;
cl_platform_id platform;
if (clGetPlatformIDs(1, &platform, nullptr) == CL_SUCCESS) {
fn = (clEnqueueMemFillINTEL_fn) clGetExtensionFunctionAddressForPlatform(platform, "clEnqueueMemFillINTEL");
}
}
return fn;
return ggml_openvino_get_device_config().cl_mem_fill_fn;
}
// Get the clEnqueueMemcpyINTEL function pointer (lazy load)
// Get the clEnqueueMemcpyINTEL function pointer
clEnqueueMemcpyINTEL_fn ggml_openvino_get_clEnqueueMemcpyINTEL() {
static clEnqueueMemcpyINTEL_fn fn = nullptr;
static bool loaded = false;
if (!loaded) {
loaded = true;
cl_platform_id platform;
if (clGetPlatformIDs(1, &platform, nullptr) == CL_SUCCESS) {
fn = (clEnqueueMemcpyINTEL_fn) clGetExtensionFunctionAddressForPlatform(platform, "clEnqueueMemcpyINTEL");
}
}
return fn;
return ggml_openvino_get_device_config().cl_mem_cpy_fn;
}
// Get requantization type for a tensor type (returns nullopt if no requant needed)
@@ -280,14 +376,11 @@ std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor *
// Q6_K/Q5_K are touched):
// q4_sym128 Q6_K/Q5_K -> Q4_0_128 (u4, group 128, symmetric)
// q4_sym128_all and Q4_K too -- drops Q4_K's per-32 zero point, which costs some accuracy
// q4_asym64_all Q6_K/Q5_K and Q4_K -> Q4_1_64 (u4, group 64, asymmetric) -- most of the
// metadata saving while keeping a real zero point
// q4_asym64 Q6_K/Q5_K -> Q4_1_64 (u4, group 64, asymmetric)
// q4_asym64_all Q6_K/Q5_K and Q4_K -> Q4_1_64 (u4, group 64, asymmetric)
// native no requantization at all (keep Q6_K/Q5_K as they are)
//
// The asymmetric target is only offered in its _all form: leaving Q4_K at its native group 32
// while Q6_K/Q5_K move to group 64 gives the Q/K/V projections different group counts, and the
// GPU plugin's FullyConnectedHorizontalFusion concatenates their scale constants, which then
// fails shape inference. Requantizing all three keeps the group size uniform.
// q4_asym64 leaves Q4_K at its native group 32. Use q4_asym64_all to keep the group size uniform.
const char * rq = ggml_openvino_getenv_str("GGML_OPENVINO_REQUANT_KQUANT");
auto is_opt = [rq](const char * name) {
return rq && strcmp(rq, name) == 0;
@@ -295,6 +388,7 @@ std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor *
const bool sym128 = is_opt("q4_sym128");
const bool sym128_all = is_opt("q4_sym128_all");
const bool asym64_all = is_opt("q4_asym64_all");
const bool asym64 = is_opt("q4_asym64");
if (tensor->type == GGML_TYPE_Q4_K) {
if (sym128_all) {
@@ -313,7 +407,7 @@ std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor *
if (sym128 || sym128_all) {
return ExtraQuantType::Q4_0_64;
}
if (asym64_all) {
if (asym64 || asym64_all) {
return ExtraQuantType::Q4_1_64;
}
// TODO: temporary workaround for a known OpenVINO GPU-plugin bug -- remove once the
@@ -328,7 +422,7 @@ std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor *
// already requantize to per-channel Q8_0_C (grouped=0). Sending these to grouped 4 bit
// avoids the broken layout and restores correct output.
// Opt out with GGML_OPENVINO_REQUANT_KQUANT=native.
if (ggml_openvino_get_device_name() == "GPU" && !is_opt("native")) {
if (ggml_openvino_is_gpu() && !is_opt("native")) {
return ExtraQuantType::Q4_0_64;
}
}
@@ -338,7 +432,7 @@ std::optional<ExtraQuantType> ggml_openvino_get_requant_type(const ggml_tensor *
if (sym128 || sym128_all) {
return ExtraQuantType::Q4_0_128;
}
if (asym64_all) {
if (asym64 || asym64_all) {
return ExtraQuantType::Q4_1_64;
}
if (is_opt("native")) {
@@ -439,9 +533,7 @@ ggml_openvino_extracted_layout ggml_openvino_get_extracted_layout(const ggml_ten
layout.weights_per_block = tensor->ne[0];
break;
default:
layout.weights_per_block = -1;
GGML_ABORT("Code of re-quantizing to channel-wise is not updated");
break;
}
if (layout.is_requant) {
@@ -560,12 +652,11 @@ ggml_openvino_tensor_extra * ggml_openvino_create_tensor_extra(const ggml_tensor
return nullptr;
}
const auto & device_name = ggml_openvino_get_device_name();
auto remote_context = ggml_openvino_get_remote_context();
std::shared_ptr<ov::Tensor> ov_tensor;
if (is_remote) {
GGML_ASSERT(device_name == "GPU");
GGML_ASSERT(ggml_openvino_is_gpu());
auto gpu_context = remote_context->as<ov::intel_gpu::ocl::ClContext>();
auto usm_tensor = gpu_context.create_tensor(element_type, shape, tensor->data);
ov_tensor = std::make_shared<ov::intel_gpu::ocl::USMTensor>(std::move(usm_tensor));
+17 -1
View File
@@ -63,12 +63,16 @@ clEnqueueMemcpyINTEL_fn ggml_openvino_get_clEnqueueMemcpyINTEL();
struct ggml_openvino_device_config {
std::string device_name = "CPU";
std::vector<std::string> available_devices;
bool is_npu = false;
bool initialized = false;
std::optional<ov::RemoteContext> remote_context;
size_t max_alloc_size = SIZE_MAX;
ov::AnyMap compile_config;
std::unordered_map<std::string, std::string> environment_variables;
cl_command_queue cl_queue = nullptr;
clEnqueueMemFillINTEL_fn cl_mem_fill_fn = nullptr;
clEnqueueMemcpyINTEL_fn cl_mem_cpy_fn = nullptr;
void init();
~ggml_openvino_device_config();
@@ -83,6 +87,12 @@ void ggml_openvino_init_device_config();
// Get the device name
const std::string & ggml_openvino_get_device_name();
// Get all available physical OpenVINO devices
std::vector<std::string> ggml_openvino_get_available_devices();
// Human-readable device name, e.g. "Intel(R) AI Boost (NPU 4000)"; the device id if unavailable
std::string ggml_openvino_get_device_description(const std::string & device_name);
// Environment variable accessors. All GGML_OPENVINO_* env vars are read once
// during backend init and cached on the device config; consumers must go
// through these helpers (never call ::getenv directly) so behavior stays
@@ -102,11 +112,17 @@ int ggml_openvino_getenv_int(const char * var, int default_value = 0);
// Memory optimization toggles. GGML_OPENVINO_MEMORY_OPTIMIZE is an umbrella
// switch; the fine-grained env vars still override it when explicitly set.
bool ggml_openvino_reduce_compile_mem_enabled();
bool ggml_openvino_release_weights_enabled(const std::string & device);
bool ggml_openvino_release_weights_enabled();
// Check if running on NPU
bool ggml_openvino_is_npu();
// Check if running on a GPU (GPU, GPU.0, GPU.1, ...)
bool ggml_openvino_is_gpu();
// Largest single memory object the device can allocate, SIZE_MAX when there is no known limit
size_t ggml_openvino_max_alloc_size();
// Host weight-buffer release (GGML_OPENVINO_RELEASE_WEIGHTS, GPU only).
// register: record a host weight buffer (idempotent per data pointer).
// release: madvise(MADV_DONTNEED) all registered buffers, dropping their RSS.
+382 -87
View File
@@ -8,6 +8,7 @@
#include "ggml-openvino/utils.h"
#include "ggml-quants.h"
#include "ggml.h"
#include "model-cache.h"
#include <algorithm>
#include <atomic>
@@ -24,7 +25,10 @@
#include <openvino/runtime/allocator.hpp>
#include <openvino/runtime/intel_gpu/ocl/ocl.hpp>
#include <openvino/runtime/intel_npu/level_zero/level_zero.hpp>
#include <openvino/runtime/properties.hpp>
#include <openvino/runtime/tensor.hpp>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
@@ -69,8 +73,7 @@ struct ggml_backend_openvino_buffer_context {
size_t size;
bool is_remote;
// Set when the buffer is a file-backed spill mapping (GGML_OPENVINO_SPILL_DIR); it must be
// munmap'd rather than freed.
// File-backed spill or cache-only virtual memory.
void * spill_mapping = nullptr;
size_t spill_size = 0;
@@ -79,6 +82,8 @@ struct ggml_backend_openvino_buffer_context {
// Track all extras for cleanup
std::map<ggml_tensor *, ggml_openvino_extra_base *> tensor_extras;
std::map<const void *, uint64_t> weight_fingerprints;
std::vector<ggml_openvino_source_mapping> source_mappings;
// Used for re-allocation on device for kvcache
void * data_prev;
@@ -100,7 +105,7 @@ struct ggml_backend_openvino_buffer_context {
const auto & device_name = ggml_openvino_get_device_name();
if (is_remote) {
GGML_ASSERT(device_name == "GPU");
GGML_ASSERT(ggml_openvino_is_gpu());
auto remote_context = ggml_openvino_get_remote_context();
auto gpu_context = remote_context->as<ov::intel_gpu::ocl::ClContext>();
ov::intel_gpu::ocl::USMTensor usm_tensor =
@@ -108,8 +113,25 @@ struct ggml_backend_openvino_buffer_context {
data = usm_tensor.get();
ov_buffer = std::make_shared<ov::intel_gpu::ocl::USMTensor>(std::move(usm_tensor));
} else {
#ifndef _WIN32
if (const char * spill_dir = ggml_openvino_getenv_str("GGML_OPENVINO_SPILL_DIR")) {
#ifdef _WIN32
if (ggml_openvino_model_cache_only()) {
data = spill_mapping = VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (data == nullptr) {
return;
}
spill_size = size;
ov_buffer = std::make_shared<ov::Tensor>(ov::element::u8, ov::Shape{size}, data);
} else
#else
if (ggml_openvino_model_cache_only()) {
void * m = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m == MAP_FAILED) {
return;
}
data = spill_mapping = m;
spill_size = size;
ov_buffer = std::make_shared<ov::Tensor>(ov::element::u8, ov::Shape{size}, data);
} else if (const char * spill_dir = ggml_openvino_getenv_str("GGML_OPENVINO_SPILL_DIR")) {
// Disk-backed weight buffer: back the repacked weights with a temp file via MAP_SHARED
// instead of anonymous memory. Anonymous pages can only be evicted to swap, so the
// repacked buffer stays pinned alongside the mmap'd source and both are resident at once
@@ -180,7 +202,11 @@ struct ggml_backend_openvino_buffer_context {
delete pair.second;
}
tensor_extras.clear();
#ifndef _WIN32
#ifdef _WIN32
if (spill_mapping != nullptr) {
VirtualFree(spill_mapping, 0, MEM_RELEASE);
} else
#else
if (spill_mapping != nullptr) {
munmap(spill_mapping, spill_size);
} else
@@ -295,7 +321,7 @@ static enum ggml_status ggml_backend_openvino_buffer_init_tensor(ggml_backend_bu
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
// Put kvcache on device memory for GPU (NPU memory is too small even for kvcache)
if (strncmp(tensor->name, "cache_", 6) == 0 && !ctx->is_remote && ggml_openvino_get_device_name() == "GPU" &&
if (strncmp(tensor->name, "cache_", 6) == 0 && !ctx->is_remote && ggml_openvino_is_gpu() &&
!is_stateful_enabled()) {
GGML_ASSERT(ctx->tensor_extras.empty());
auto device = ctx->device;
@@ -311,6 +337,26 @@ static enum ggml_status ggml_backend_openvino_buffer_init_tensor(ggml_backend_bu
if (tensor->view_src != nullptr) {
GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft);
if (tensor->view_src->extra != nullptr) {
// The cached ov::Tensor carries the shape it was built with, so sharing view_src's
// extra hands out the wrong shape for a reshaping view (e.g. Vcur reshaped from
// [n_embd, n_tokens] to [head_size, n_heads_kv, n_tokens]). When such a view is a
// graph input, binding it fails the shape check. Give it its own extra instead;
// ggml_openvino_create_tensor_extra reads ne and data off the view, so the offset is
// handled too. Only safe for a contiguous view - the ov::Tensor assumes dense strides.
// Skip empty views: they have no data, and on GPU one can sit at the end of the USM buffer.
if (!ggml_are_same_shape(tensor, tensor->view_src) && ggml_is_contiguous(tensor) &&
!ggml_is_quantized(tensor->type) && tensor->data != nullptr && ggml_nbytes(tensor) > 0) {
if (ggml_openvino_tensor_extra * extra =
ggml_openvino_create_tensor_extra(tensor, ctx->is_remote)) {
auto it = ctx->tensor_extras.find(tensor);
if (it != ctx->tensor_extras.end()) {
delete it->second;
}
ctx->tensor_extras[tensor] = extra;
tensor->extra = extra;
return GGML_STATUS_SUCCESS;
}
}
tensor->extra = tensor->view_src->extra;
}
return GGML_STATUS_SUCCESS;
@@ -346,7 +392,7 @@ static void ggml_backend_openvino_buffer_memset_tensor(ggml_backend_buffer_t buf
// For remote (device) buffers, use OpenCL USM memfill
cl_command_queue queue = ggml_openvino_get_cl_queue();
auto mem_fill_fn = ggml_openvino_get_clEnqueueMemFillINTEL();
if (queue != nullptr && mem_fill_fn != nullptr) {
if (mem_fill_fn != nullptr) {
uint8_t pattern = value;
cl_int err = mem_fill_fn(queue, (char *) tensor->data + offset, &pattern, sizeof(pattern), size, 0, nullptr,
nullptr);
@@ -355,7 +401,7 @@ static void ggml_backend_openvino_buffer_memset_tensor(ggml_backend_buffer_t buf
}
clFinish(queue);
} else {
GGML_LOG_ERROR("%s: no OpenCL queue or clEnqueueMemFillINTEL not available for GPU buffer\n", __func__);
GGML_LOG_ERROR("%s: clEnqueueMemFillINTEL not available for GPU buffer\n", __func__);
}
} else {
memset((char *) tensor->data + offset, value, size);
@@ -375,6 +421,17 @@ static void ggml_backend_openvino_buffer_set_tensor(ggml_backend_buffer_t buffer
bool is_weight_buffer = (buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
// Full tensor set: offset=0, full size, not a view
bool is_full_tensor_set = (offset == 0 && size == ggml_nbytes(tensor) && tensor->view_src == nullptr);
if (is_weight_buffer && ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR")) {
if (is_full_tensor_set) {
ctx->weight_fingerprints[tensor->data] = ggml_openvino_source_fingerprint(data, size, ctx->source_mappings);
}
if (ggml_openvino_model_cache_only()) {
if (!is_full_tensor_set) {
GGML_ABORT("ggml-openvino: cache-only mode requires whole mmap weight uploads");
}
return;
}
}
// 2D tensor (typical weight shape), or a 3D quantized MoE expert weight (MUL_MAT_ID). Dense 3D
// expert weights are handled later in create_weight_node instead.
bool is_2d = (tensor->ne[2] == 1 && tensor->ne[3] == 1);
@@ -441,14 +498,14 @@ static void ggml_backend_openvino_buffer_set_tensor(ggml_backend_buffer_t buffer
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue();
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (queue != nullptr && mem_cpy_fn != nullptr) {
if (mem_cpy_fn != nullptr) {
cl_int err =
mem_cpy_fn(queue, CL_TRUE, (char *) tensor->data + offset, data, size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL failed with error %d\n", __func__, err);
}
} else {
GGML_LOG_ERROR("%s: no OpenCL queue or clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
}
} else {
memcpy((char *) tensor->data + offset, data, size);
@@ -478,18 +535,22 @@ static void ggml_backend_openvino_buffer_get_tensor(ggml_backend_buffer_t buffer
GGML_ASSERT(tensor != nullptr && tensor->data != nullptr);
ggml_backend_openvino_buffer_context * ctx = (ggml_backend_openvino_buffer_context *) buffer->context;
if (ggml_openvino_model_cache_only() && buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
GGML_ABORT("ggml-openvino: cannot read unloaded weights in cache-only mode");
}
if (ctx->is_remote) {
// For remote (device) buffers, use OpenCL USM memcpy (device-to-host)
cl_command_queue queue = ggml_openvino_get_cl_queue();
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (queue != nullptr && mem_cpy_fn != nullptr) {
if (mem_cpy_fn != nullptr) {
cl_int err =
mem_cpy_fn(queue, CL_TRUE, data, (const char *) tensor->data + offset, size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL failed with error %d\n", __func__, err);
}
} else {
GGML_LOG_ERROR("%s: no OpenCL queue or clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
}
} else {
memcpy(data, (const char *) tensor->data + offset, size);
@@ -507,8 +568,8 @@ static bool ggml_backend_openvino_buffer_cpy_tensor(ggml_backend_buffer_t buffer
// For remote (device) buffers, use OpenCL USM memcpy
cl_command_queue queue = ggml_openvino_get_cl_queue();
auto mem_cpy_fn = ggml_openvino_get_clEnqueueMemcpyINTEL();
if (queue == nullptr || mem_cpy_fn == nullptr) {
GGML_LOG_ERROR("%s: no OpenCL queue or clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
if (mem_cpy_fn == nullptr) {
GGML_LOG_ERROR("%s: clEnqueueMemcpyINTEL not available for GPU buffer\n", __func__);
return false;
}
// Can copy from host to device
@@ -550,7 +611,7 @@ static void ggml_backend_openvino_buffer_clear(ggml_backend_buffer_t buffer, uin
if (ctx->is_remote) {
cl_command_queue queue = ggml_openvino_get_cl_queue();
auto mem_fill_fn = ggml_openvino_get_clEnqueueMemFillINTEL();
if (queue != nullptr && mem_fill_fn != nullptr) {
if (mem_fill_fn != nullptr) {
uint8_t pattern = value;
cl_int err = mem_fill_fn(queue, ctx->data, &pattern, sizeof(pattern), ctx->size, 0, nullptr, nullptr);
if (err != CL_SUCCESS) {
@@ -558,8 +619,7 @@ static void ggml_backend_openvino_buffer_clear(ggml_backend_buffer_t buffer, uin
}
clFinish(queue);
} else {
GGML_LOG_WARN("%s: no OpenCL queue or clEnqueueMemFillINTEL not available for GPU buffer clear\n",
__func__);
GGML_LOG_WARN("%s: clEnqueueMemFillINTEL not available for GPU buffer clear\n", __func__);
}
} else {
memset(ctx->data, value, ctx->size);
@@ -609,7 +669,8 @@ static size_t ggml_backend_openvino_buffer_type_get_alignment(ggml_backend_buffe
static size_t ggml_backend_openvino_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
GGML_UNUSED(buft);
return SIZE_MAX;
// A GPU caps a single memory object, so let ggml split a large buffer into parts that fit
return ggml_openvino_max_alloc_size();
}
static size_t ggml_backend_openvino_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft,
@@ -617,7 +678,7 @@ static size_t ggml_backend_openvino_buffer_type_get_alloc_size(ggml_backend_buff
GGML_UNUSED(buft);
// For quantized weight tensors, we need extra space for extracted data.
if (ggml_is_quantized(tensor->type) && tensor->ne[3] == 1) {
if (!ggml_openvino_model_cache_only() && ggml_is_quantized(tensor->type) && tensor->ne[3] == 1) {
ggml_openvino_extracted_layout layout = ggml_openvino_get_extracted_layout(tensor);
if (layout.total_size > 0) {
// GGML_LOG_DEBUG("%s: tensor %s needs %zu bytes (original %zu, extracted: weights=%zu scales=%zu zp=%zu)\n",
@@ -772,6 +833,19 @@ bool ggml_backend_buft_is_openvino_host(ggml_backend_buffer_type_t buft) {
return buft->iface.get_name == ggml_backend_openvino_host_buffer_type_get_name;
}
uint64_t ggml_backend_openvino_weight_fingerprint(const ggml_tensor * tensor) {
if (ggml_backend_buffer_is_openvino(tensor->buffer)) {
auto * ctx = static_cast<ggml_backend_openvino_buffer_context *>(tensor->buffer->context);
auto it = ctx->weight_fingerprints.find(tensor->data);
if (it != ctx->weight_fingerprints.end()) {
return it->second;
}
GGML_ABORT("ggml-openvino: missing source identity for weight %s", tensor->name);
}
std::vector<ggml_openvino_source_mapping> mappings;
return ggml_openvino_source_fingerprint(tensor->data, ggml_nbytes(tensor), mappings);
}
static void ggml_backend_openvino_free(ggml_backend_t backend) {
ggml_backend_openvino_context * ctx = (ggml_backend_openvino_context *) backend->context;
@@ -825,7 +899,7 @@ static const ggml_backend_i ggml_backend_openvino_interface = {
};
int ggml_backend_openvino_get_device_count() {
return 1;
return (int) ggml_openvino_get_available_devices().size();
}
static ggml_guid_t ggml_backend_openvino_guid(void) {
@@ -884,10 +958,122 @@ namespace {
struct ggml_backend_openvino_device_context {
int device;
std::string name;
std::string ov_name; // OpenVINO device id: CPU, GPU, GPU.1, NPU, ...
std::string description;
size_t total_memory;
};
}
static bool ov_device_has_prefix(const std::string & s, const std::string & prefix) {
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
}
static bool ov_try_get_size_t_property(const std::string & device, const std::string & property, size_t & out) {
try {
const ov::Any value = ov_singleton_core().get_property(device, property);
if (value.is<size_t>()) {
out = value.as<size_t>();
return true;
}
if (value.is<uint64_t>()) {
out = (size_t) value.as<uint64_t>();
return true;
}
if (value.is<unsigned long long>()) {
out = (size_t) value.as<unsigned long long>();
return true;
}
if (value.is<int64_t>()) {
const int64_t v = value.as<int64_t>();
if (v >= 0) {
out = (size_t) v;
return true;
}
}
} catch (...) {
}
return false;
}
// System memory available to new allocations (MemAvailable on Linux), SIZE_MAX if unknown
static size_t ov_system_available_memory() {
#ifdef _WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (GlobalMemoryStatusEx(&status)) {
return (size_t) status.ullAvailPhys;
}
#else
if (FILE * f = fopen("/proc/meminfo", "r")) {
char line[256];
unsigned long long kb = 0;
bool found = false;
while (!found && fgets(line, sizeof(line), f)) {
found = sscanf(line, "MemAvailable: %llu kB", &kb) == 1;
}
fclose(f);
if (found) {
return (size_t) std::min<unsigned long long>(kb * 1024, SIZE_MAX);
}
}
#endif
return SIZE_MAX;
}
// iGPU and NPU allocate from system RAM, so their free memory can't exceed what the OS has available
static bool ov_device_shares_system_memory(const std::string & device) {
if (ov_device_has_prefix(device, "NPU")) {
return true;
}
if (!ov_device_has_prefix(device, "GPU")) {
return false;
}
try {
return ov_singleton_core().get_property(device, ov::device::type) == ov::device::Type::INTEGRATED;
} catch (...) {
return false;
}
}
// usm_host / usm_shared allocations live in system RAM on a discrete GPU
static bool ov_gpu_stat_is_host_memory(const std::string & key) {
return key == "usm_host" || key == "usm_shared";
}
static bool ov_try_get_gpu_used_memory(const std::string & device, size_t & out) {
out = 0;
try {
const ov::Any stats_any = ov_singleton_core().get_property(device, "GPU_MEMORY_STATISTICS");
if (stats_any.is<std::map<std::string, uint64_t>>()) {
const auto stats = stats_any.as<std::map<std::string, uint64_t>>();
for (const auto & kv : stats) {
if (!ov_gpu_stat_is_host_memory(kv.first)) {
out += (size_t) kv.second;
}
}
return true;
}
if (stats_any.is<ov::AnyMap>()) {
const auto stats = stats_any.as<ov::AnyMap>();
for (const auto & kv : stats) {
if (ov_gpu_stat_is_host_memory(kv.first)) {
continue;
}
if (kv.second.is<size_t>()) {
out += kv.second.as<size_t>();
} else if (kv.second.is<uint64_t>()) {
out += (size_t) kv.second.as<uint64_t>();
} else if (kv.second.is<unsigned long long>()) {
out += (size_t) kv.second.as<unsigned long long>();
}
}
return true;
}
} catch (...) {
}
return false;
}
static const char * ggml_backend_openvino_device_get_name(ggml_backend_dev_t dev) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
return ctx->name.c_str();
@@ -899,27 +1085,45 @@ static const char * ggml_backend_openvino_device_get_description(ggml_backend_de
}
static void ggml_backend_openvino_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
// total_memory is only set for GPU/NPU; used = this process's OpenVINO allocations on the device
size_t used = 0;
const bool known = ctx->total_memory > 0 &&
(ov_device_has_prefix(ctx->ov_name, "GPU") ?
ov_try_get_gpu_used_memory(ctx->ov_name, used) :
ov_try_get_size_t_property(ctx->ov_name, "NPU_DEVICE_ALLOC_MEM_SIZE", used));
if (known) {
*total = ctx->total_memory;
*free = (used >= *total) ? 0 : (*total - used);
} else {
// CPU, or a plugin without memory properties: report system memory
#ifdef _WIN32
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
*total = status.ullTotalPhys;
*free = status.ullAvailPhys;
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
*total = status.ullTotalPhys;
*free = status.ullAvailPhys;
#else
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
*total = pages * page_size;
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
*total = pages * page_size;
// "free" system memory is ill-defined, for practical purposes assume that all of it is free:
*free = *total;
// "free" system memory is ill-defined, for practical purposes assume that all of it is free:
*free = *total;
#endif // _WIN32
}
GGML_UNUSED(dev);
if (ov_device_shares_system_memory(ctx->ov_name)) {
*free = std::min(*free, ov_system_available_memory());
}
}
static enum ggml_backend_dev_type ggml_backend_openvino_device_get_type(ggml_backend_dev_t dev) {
GGML_UNUSED(dev);
return GGML_BACKEND_DEVICE_TYPE_GPU;
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
// Only the device selected by GGML_OPENVINO_DEVICE is offered for offload. The others are
// registered for discovery (--list-devices) only; llama.cpp skips IGPU devices when a GPU exists.
return ctx->ov_name == ggml_openvino_get_device_name() ? GGML_BACKEND_DEVICE_TYPE_GPU : GGML_BACKEND_DEVICE_TYPE_IGPU;
}
static void ggml_backend_openvino_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) {
@@ -940,6 +1144,12 @@ static void ggml_backend_openvino_device_get_props(ggml_backend_dev_t dev, ggml_
static ggml_backend_t ggml_backend_openvino_device_init(ggml_backend_dev_t dev, const char * params) {
GGML_UNUSED(params);
ggml_backend_openvino_device_context * ctx = (ggml_backend_openvino_device_context *) dev->context;
if (ctx->ov_name != ggml_openvino_get_device_name()) {
// Not an error: test-backend-ops initializes every device
GGML_LOG_WARN("%s: %s (OpenVINO %s) is not the selected device, no ops will run on it; "
"set GGML_OPENVINO_DEVICE=%s to use it\n",
__func__, ctx->name.c_str(), ctx->ov_name.c_str(), ctx->ov_name.c_str());
}
return ggml_backend_openvino_init(ctx->device);
}
@@ -1159,7 +1369,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
if (op->type == GGML_TYPE_I64) {
return {false, "CONCAT with I64 type is not supported"};
}
if (ggml_openvino_get_device_name() == "GPU" && op->type == GGML_TYPE_BF16 && has_view_op_input(op)) {
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_BF16 && has_view_op_input(op)) {
return {false, "CONCAT with BF16 type and VIEW input is not supported on GPU"};
}
break;
@@ -1183,7 +1393,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
if (op->ne[3] != 1) {
return {false, "GET_ROWS/SET_ROWS with ne[3] != 1 (ne[3]=" + std::to_string(op->ne[3]) + ") is not supported"};
}
if (op->op == GGML_OP_GET_ROWS && ggml_openvino_get_device_name() == "GPU" &&
if (op->op == GGML_OP_GET_ROWS && ggml_openvino_is_gpu() &&
op->src[0]->type == GGML_TYPE_BF16) {
return {false, "GET_ROWS with BF16 src0 is not supported on GPU"};
}
@@ -1246,26 +1456,37 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
// The GPU plugin can fuse broadcast DIV into the preceding FFN GEMM path
// and produce infs for per-channel scale vectors. Keep those DIVs on CPU
// until the fused GPU kernel is reliable. (falied case llama-arch-test mpt)
if (ggml_openvino_get_device_name() == "GPU" && op->src[1]->ne[0] == op->ne[0] &&
if (ggml_openvino_is_gpu() && op->src[1]->ne[0] == op->ne[0] &&
op->src[1]->ne[1] == 1 && op->src[1]->ne[2] == 1 && op->src[1]->ne[3] == 1) {
return {false, "DIV per-channel scale broadcast is not supported on GPU"};
}
break;
}
case GGML_OP_POOL_2D: {
const auto& name = ggml_openvino_get_device_name();
if (name == "GPU") {
if (ggml_openvino_is_gpu()) {
const int32_t * params = op->op_params;
const int k0 = params[1];
const int k1 = params[2];
const int p0 = params[5];
const int p1 = params[6];
if ((p0 > 0 || p1 > 0) && (k0 < 3 || k1 < 3)) {
return {false, "POOL_2D with padding and kernel size < 3 is not supported on " + name};
return {false, "POOL_2D with padding and kernel size < 3 is not supported on " + ggml_openvino_get_device_name()};
}
}
break;
}
case GGML_OP_SUM: {
if (op->src[0]->op == GGML_OP_PERMUTE) {
return {false, "SUM with PERMUTE input is not supported"};
}
break;
}
case GGML_OP_MEAN: {
if (op->src[0]->op == GGML_OP_PERMUTE && op->src[0]->src[0] != nullptr && op->src[0]->src[0]->op == GGML_OP_VIEW) {
return {false, "MEAN with PERMUTE of VIEW input is not supported"};
}
break;
}
case GGML_OP_SUM_ROWS: {
if (op->src[0]->op == GGML_OP_PERMUTE) {
return {false, "SUM_ROWS with PERMUTE input is not supported"};
@@ -1303,7 +1524,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
break;
}
case GGML_OP_PERMUTE: {
if (op->type == GGML_TYPE_BF16 && ggml_openvino_get_device_name() == "GPU") {
if (op->type == GGML_TYPE_BF16 && ggml_openvino_is_gpu()) {
return {false, "PERMUTE with BF16 type is not supported on GPU"};
}
break;
@@ -1312,7 +1533,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
if (op->src[0]->type != GGML_TYPE_BF16 && op->src[1]->type == GGML_TYPE_BF16) {
return {false, "CPY with BF16 src[1] type is not supported"};
}
if (ggml_openvino_get_device_name() == "NPU" && (op->src[0]->type == GGML_TYPE_BF16 || op->src[1]->type == GGML_TYPE_BF16)) {
if (ggml_openvino_is_npu() && (op->src[0]->type == GGML_TYPE_BF16 || op->src[1]->type == GGML_TYPE_BF16)) {
return {false, "CPY with BF16 is not supported is not supported on NPU"};
}
// CPY to a quantized destination (e.g. f32 -> q4_0) is numerically unstable with OpenVINO backend.
@@ -1337,13 +1558,13 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
break;
}
case GGML_OP_MUL_MAT: {
if (ggml_openvino_get_device_name() == "GPU" && op->src[0] != nullptr && op->src[1] != nullptr &&
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[1] != nullptr &&
ggml_is_quantized(op->src[0]->type) && strcmp(op->src[0]->name, "a") == 0 &&
strcmp(op->src[1]->name, "b") == 0 && op->src[0]->ne[1] == 1 && op->src[1]->ne[1] == 64 &&
op->src[0]->ne[0] == 256 && op->src[1]->ne[0] == 256) {
return {false, "MUL_MAT quantized benchmark test case on GPU is not supported"};
}
if (ggml_openvino_get_device_name() == "GPU" && op->type == GGML_TYPE_F32 && op->ne[0] == 1 && op->ne[1] == 1 &&
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_F32 && op->ne[0] == 1 && op->ne[1] == 1 &&
(op->src[0]->buffer == nullptr || op->src[0]->buffer->usage != GGML_BACKEND_BUFFER_USAGE_WEIGHTS)) {
return {false, "MUL_MAT scalar dot product with non-weight src[0] on GPU is not supported"};
}
@@ -1363,7 +1584,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
return {false, "MUL_MAT_ID with single-expert or empty ne[2] <= 1 (ne[2]=" +
std::to_string(op->src[0]->ne[2]) + ") is not supported"};
}
if (ggml_openvino_get_device_name() == "GPU" && op->src[0] != nullptr && !ggml_is_quantized(op->src[0]->type)) {
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && !ggml_is_quantized(op->src[0]->type)) {
return {false, "MUL_MAT_ID with non-quantized weights on GPU is not supported"};
}
// The GPU plugin's GatherMatmul returns wrong values for the layouts test-backend-ops
@@ -1372,55 +1593,21 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
// The same graph is correct on the CPU plugin, and correct on GPU for every real model,
// which always feeds experts from a bound tensor buffer. Standalone op-test tensors have
// no buffer at all, so use that to exclude them and let the scheduler run them on CPU.
if (ggml_openvino_get_device_name() == "GPU" && op->src[0] != nullptr && op->src[0]->buffer == nullptr) {
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[0]->buffer == nullptr) {
return {false, "MUL_MAT_ID with unbound expert tensors on GPU is not supported"};
}
// Only MXFP4 still needs the large-temporary guard; every other quantized type goes
// through GatherMatmul, which never materializes the selected expert weights.
if (ggml_openvino_get_device_name() == "GPU" && op->src[0] != nullptr && op->src[0]->type == GGML_TYPE_MXFP4 &&
if (ggml_openvino_is_gpu() && op->src[0] != nullptr && op->src[0]->type == GGML_TYPE_MXFP4 &&
mul_mat_id_requires_large_tmp(op)) {
return {false, "MUL_MAT_ID with MXFP4 weights requires large temporary on GPU"};
}
break;
}
case GGML_OP_ROPE: {
const int32_t * op_params = op->op_params;
const int n_dims = op_params[1];
const int mode = op_params[2];
const int64_t n_offs = op_params[15];
if (mode != GGML_ROPE_TYPE_NORMAL && mode != GGML_ROPE_TYPE_NEOX && mode != GGML_ROPE_TYPE_IMROPE) {
return {false, "ROPE with mode " + std::to_string(mode) + " is not supported"};
}
if (n_offs < 0 || (n_offs % 2) != 0) {
return {false, "ROPE with invalid n_offs=" + std::to_string(n_offs)};
}
const int64_t head_dim = op->src[0]->ne[0];
const int64_t rope_dims = n_dims == 0 ? head_dim : n_dims;
if (rope_dims <= 0 || rope_dims + n_offs > head_dim || (rope_dims % 2) != 0) {
return {false, "ROPE with n_dims=" + std::to_string(n_dims) + ", n_offs=" + std::to_string(n_offs) +
", head_dim=" + std::to_string(head_dim) + " is not supported"};
}
if (op->type != GGML_TYPE_F32 && op->type != GGML_TYPE_F16) {
return {false, "ROPE with type " + std::string(ggml_type_name(op->type)) + " is not supported"};
}
if (op->view_src != nullptr && !ggml_is_contiguous(op->src[0])) {
return {false, "ROPE on VIEW / non-contiguous input is not supported"};
}
if (op->src[0]->ne[3] > 1) {
// translate_rope's cos/sin tables cover one sequence only; ne[3] > 1 fails to broadcast.
return {false, "ROPE with multiple sequences (ne[3]=" + std::to_string(op->src[0]->ne[3]) +
") is not supported"};
}
float freq_scale;
float ext_factor;
float attn_factor;
memcpy(&freq_scale, op_params + 6, sizeof(float));
memcpy(&ext_factor, op_params + 7, sizeof(float));
memcpy(&attn_factor, op_params + 8, sizeof(float));
if (mode == GGML_ROPE_TYPE_IMROPE &&
(op->src[2] != nullptr || freq_scale != 1.0f || ext_factor != 0.0f || attn_factor != 1.0f)) {
return {false, "IMROPE with freq_factors, freq_scale, ext_factor, or attn_factor is not supported"};
}
break;
}
case GGML_OP_TRANSPOSE: {
@@ -1430,7 +1617,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
break;
}
case GGML_OP_REPEAT: {
if (ggml_openvino_get_device_name() == "GPU" && op->type == GGML_TYPE_BF16) {
if (ggml_openvino_is_gpu() && op->type == GGML_TYPE_BF16) {
return {false, "REPEAT with BF16 type is not supported on GPU"};
}
break;
@@ -1438,7 +1625,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
case GGML_OP_GATED_DELTA_NET: {
// enable after https://github.com/openvinotoolkit/openvino/pull/35917 is included in OV release
// return true;
// if (ggml_openvino_get_device_name() == "GPU" && op->src[0]->ne[2] > 1) {
// if (ggml_openvino_is_gpu() && op->src[0]->ne[2] > 1) {
// // CVS-186471
// return true;
// }
@@ -1469,6 +1656,84 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
}
break;
}
case GGML_OP_CONV_2D:
case GGML_OP_CONV_2D_DW: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "CONV_2D kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_2D with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_2D with non-contiguous view input is not supported"};
}
const int32_t * params = op->op_params;
const int p0 = params[2];
const int p1 = params[3];
const int d0 = params[4];
const int d1 = params[5];
const int64_t dilated_kw = (int64_t) d0 * (op->src[0]->ne[0] - 1) + 1;
const int64_t dilated_kh = (int64_t) d1 * (op->src[0]->ne[1] - 1) + 1;
const int64_t padded_w = op->src[1]->ne[0] + 2 * p0;
const int64_t padded_h = op->src[1]->ne[1] + 2 * p1;
if (padded_w < dilated_kw || padded_h < dilated_kh) {
return {false, "CONV_2D padded input is smaller than kernel"};
}
break;
}
case GGML_OP_CONV_3D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0 || op->src[0]->ne[2] <= 0) {
return {false, "CONV_3D kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_3D with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_3D with non-contiguous view input is not supported"};
}
const int32_t * params = op->op_params;
const int p0 = params[3];
const int p1 = params[4];
const int p2 = params[5];
const int d0 = params[6];
const int d1 = params[7];
const int d2 = params[8];
const int64_t dilated_kw = (int64_t) d0 * (op->src[0]->ne[0] - 1) + 1;
const int64_t dilated_kh = (int64_t) d1 * (op->src[0]->ne[1] - 1) + 1;
const int64_t dilated_kd = (int64_t) d2 * (op->src[0]->ne[2] - 1) + 1;
const int64_t padded_w = op->src[1]->ne[0] + 2 * p0;
const int64_t padded_h = op->src[1]->ne[1] + 2 * p1;
const int64_t padded_d = op->src[1]->ne[2] + 2 * p2;
if (padded_w < dilated_kw || padded_h < dilated_kh || padded_d < dilated_kd) {
return {false, "CONV_3D padded input is smaller than kernel"};
}
break;
}
case GGML_OP_CONV_TRANSPOSE_1D:
case GGML_OP_CONV_TRANSPOSE_2D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "CONV_TRANSPOSE kernel size must be positive"};
}
if (op->src[0]->op == GGML_OP_PERMUTE || op->src[1]->op == GGML_OP_PERMUTE) {
return {false, "CONV_TRANSPOSE with PERMUTE input is not supported"};
}
if (has_non_contiguous_view_input(op)) {
return {false, "CONV_TRANSPOSE with non-contiguous view input is not supported"};
}
break;
}
case GGML_OP_IM2COL: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0) {
return {false, "IM2COL kernel size must be positive"};
}
break;
}
case GGML_OP_IM2COL_3D: {
if (op->src[0]->ne[0] <= 0 || op->src[0]->ne[1] <= 0 || op->src[0]->ne[2] <= 0) {
return {false, "IM2COL_3D kernel size must be positive"};
}
break;
}
default:
break;
}
@@ -1478,6 +1743,24 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
static ggml_openvino_op_support ggml_backend_openvino_device_supports_op_impl(ggml_backend_dev_t dev, const ggml_tensor * op) {
GGML_ASSERT(dev->reg != nullptr);
ggml_backend_openvino_device_context * dev_ctx = (ggml_backend_openvino_device_context *) dev->context;
if (dev_ctx->ov_name != ggml_openvino_get_device_name()) {
// Data placed on a non-selected device (e.g. with -dev) can never run here; stop with a hint
// instead of the generic scheduler abort. Unallocated tensors (test-backend-ops) pass through.
for (int i = -1; i < GGML_MAX_SRC; i++) {
const ggml_tensor * t = i < 0 ? op : op->src[i];
ggml_backend_buffer_t buf = t == nullptr ? nullptr : (t->view_src ? t->view_src->buffer : t->buffer);
if (buf != nullptr &&
(ggml_backend_buft_is_openvino(buf->buft) || ggml_backend_buft_is_openvino_host(buf->buft)) &&
((ggml_backend_openvino_buffer_type_context *) buf->buft->context)->device == dev_ctx->device) {
GGML_ABORT("%s is not the selected OpenVINO device (%s). The OpenVINO device is chosen with the "
"GGML_OPENVINO_DEVICE environment variable, not -dev: set GGML_OPENVINO_DEVICE=%s",
dev_ctx->name.c_str(), ggml_openvino_get_device_name().c_str(), dev_ctx->ov_name.c_str());
}
}
return {false, "device is not the selected OpenVINO device"};
}
static std::unordered_set<ggml_type> supported_types{
GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_I64, GGML_TYPE_I32, GGML_TYPE_Q4_0,
GGML_TYPE_Q4_1, GGML_TYPE_Q4_K, GGML_TYPE_Q5_1, GGML_TYPE_Q5_K, GGML_TYPE_Q8_0, GGML_TYPE_Q6_K,
@@ -1527,8 +1810,9 @@ static ggml_openvino_op_support ggml_backend_openvino_device_supports_op_impl(gg
if (!supported) {
return {false, "unary op " + std::string(ggml_unary_op_name(ggml_get_unary_op(op))) + " has no op translator"};
}
if (ggml_get_unary_op(op) == GGML_UNARY_OP_EXP && op->type == GGML_TYPE_F32) {
return {false, "UNARY_EXP with F32 type is not supported"};
if (op->type == GGML_TYPE_F32 && (ggml_get_unary_op(op) == GGML_UNARY_OP_EXP ||
ggml_get_unary_op(op) == GGML_UNARY_OP_EXPM1)) {
return {false, "UNARY_EXP / UNARY_EXPM1 with F32 type is not supported"};
}
break;
}
@@ -1665,15 +1949,26 @@ GGML_BACKEND_API ggml_backend_reg_t ggml_backend_openvino_reg(void) {
std::lock_guard<std::mutex> lock(mutex);
if (!initialized) {
ggml_openvino_init();
const std::vector<std::string> openvino_devices = ggml_openvino_get_available_devices();
ggml_backend_openvino_reg_context * ctx = new ggml_backend_openvino_reg_context;
for (int i = 0; i < ggml_backend_openvino_get_device_count(); i++) {
ggml_backend_openvino_device_context * dev_ctx = new ggml_backend_openvino_device_context;
dev_ctx->device = i;
// Not the raw OpenVINO id: "CPU" would shadow the ggml CPU backend in ggml_backend_dev_by_name
dev_ctx->name = GGML_OPENVINO_NAME + std::to_string(i);
dev_ctx->description = ov::get_openvino_version().description;
dev_ctx->ov_name = openvino_devices[i];
// The device is chosen with GGML_OPENVINO_DEVICE, not -dev, so show the value to set
dev_ctx->description = "GGML_OPENVINO_DEVICE=" + dev_ctx->ov_name +
(dev_ctx->ov_name == ggml_openvino_get_device_name() ? " (selected)" : "") +
" - " + ggml_openvino_get_device_description(dev_ctx->ov_name);
dev_ctx->total_memory = 0;
if (ov_device_has_prefix(dev_ctx->ov_name, "GPU")) {
ov_try_get_size_t_property(dev_ctx->ov_name, "GPU_DEVICE_TOTAL_MEM_SIZE", dev_ctx->total_memory);
} else if (ov_device_has_prefix(dev_ctx->ov_name, "NPU")) {
ov_try_get_size_t_property(dev_ctx->ov_name, "NPU_DEVICE_TOTAL_MEM_SIZE", dev_ctx->total_memory);
}
ggml_backend_dev_t dev =
new ggml_backend_device{/* .interface = */ ggml_backend_openvino_device_interface,
+232 -21
View File
@@ -6,9 +6,13 @@
#include "ggml-openvino-extra.h"
#include <cerrno>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <limits>
#include <sstream>
#include <openvino/core/version.hpp>
#include <string>
#include <sys/stat.h>
@@ -16,7 +20,19 @@
#include <vector>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# include <psapi.h>
# include <direct.h>
# include <process.h>
#else
# include <unistd.h>
#endif
#ifdef __linux__
# include <sys/sysmacros.h>
#endif
namespace {
@@ -37,10 +53,7 @@ inline uint64_t fnv1a_u64(uint64_t h, uint64_t v) {
constexpr uint64_t FNV_OFFSET = 0xcbf29ce484222325ull;
// Bytes sampled from each end of a weight tensor for the sampled hash. The whole
// model is never hashed (that would cost seconds every run); instead we sample a
// bounded window from the head and tail of each weight's bytes. The manifest
// re-verify (same sample) guards the residual collision risk.
// Fallback when source-file identity is unavailable outside cache-only mode.
constexpr size_t WEIGHT_SAMPLE_BYTES = 4096;
// Is this src a model weight, mirroring create_weight_nodes()'s selection:
@@ -52,8 +65,7 @@ bool is_weight_src(const ggml_tensor * src) {
return src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS || ggml_is_quantized(src->type);
}
// Per-weight sampled fingerprint: identity (name/shape/type) + a bounded byte
// sample. Returns FNV offset basis if data is unavailable (kept deterministic).
// Weight metadata and source identity; do not read repacked or unloaded buffers.
uint64_t weight_fingerprint(const ggml_tensor * t) {
uint64_t h = FNV_OFFSET;
h = fnv1a(h, t->name, strlen(t->name));
@@ -63,15 +75,7 @@ uint64_t weight_fingerprint(const ggml_tensor * t) {
h = fnv1a_u64(h, static_cast<uint64_t>(t->type));
const size_t nbytes = ggml_nbytes(t);
h = fnv1a_u64(h, nbytes);
if (t->data != nullptr && nbytes > 0) {
const size_t head = nbytes < WEIGHT_SAMPLE_BYTES ? nbytes : WEIGHT_SAMPLE_BYTES;
h = fnv1a(h, t->data, head);
if (nbytes > WEIGHT_SAMPLE_BYTES) {
const size_t tail = nbytes < 2 * WEIGHT_SAMPLE_BYTES ? nbytes - WEIGHT_SAMPLE_BYTES : WEIGHT_SAMPLE_BYTES;
h = fnv1a(h, static_cast<const uint8_t *>(t->data) + (nbytes - tail), tail);
}
}
return h;
return fnv1a_u64(h, ggml_backend_openvino_weight_fingerprint(t));
}
// Walk the cgraph and invoke fn(weight_tensor) for each distinct weight, in node
@@ -156,12 +160,162 @@ bool make_dirs(const std::string & path) {
} // namespace
bool ggml_openvino_model_cache_only() {
return ggml_openvino_getenv_int("GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY") != 0;
}
static const char * cache_settings[] = {
"GGML_OPENVINO_REQUANT_KQUANT",
"GGML_OPENVINO_NATIVE_SOFTPLUS",
"GGML_OPENVINO_DISABLE_KV_SLICE",
"GGML_OPENVINO_MANUAL_GQA_ATTN",
"GGML_OPENVINO_STATEFUL_EXECUTION",
"GGML_OPENVINO_DISABLE_KV_STATE_RELAYOUT",
"GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS",
"GGML_OPENVINO_REDUCE_COMPILE_MEM",
"GGML_OPENVINO_MEMORY_OPTIMIZE",
"GGML_OPENVINO_PROFILING",
};
void ggml_openvino_model_cache_init() {
const bool cache_only = ggml_openvino_model_cache_only();
const std::string dir = ggml_openvino_model_cache_dir();
if (dir.empty()) {
if (cache_only) {
GGML_ABORT("ggml-openvino: cache-only mode requires GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR");
}
return;
}
if (cache_only && (ggml_openvino_is_npu() || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC") ||
ggml_openvino_getenv_int("GGML_OPENVINO_DISABLE_CACHE") ||
ggml_openvino_getenv_int("GGML_OPENVINO_ENABLE_FALLBACK"))) {
GGML_ABORT("ggml-openvino: cache-only mode requires dynamic CPU/GPU execution with caching and without fallback");
}
#if !defined(__linux__) && !defined(_WIN32)
if (cache_only) {
GGML_ABORT("ggml-openvino: cache-only mmap identification requires Linux or Windows");
}
#endif
if (cache_only) {
auto & config = ggml_openvino_get_device_config();
config.environment_variables.erase("GGML_OPENVINO_SPILL_DIR");
config.environment_variables["GGML_OPENVINO_RELEASE_WEIGHTS"] = "0";
}
}
uint64_t ggml_openvino_source_fingerprint(const void * data, size_t size, std::vector<ggml_openvino_source_mapping> & mappings) {
const uintptr_t address = reinterpret_cast<uintptr_t>(data);
auto contains = [&](const ggml_openvino_source_mapping & m) {
return address >= m.begin && address < m.end && size <= m.end - address;
};
auto fingerprint = [&](const ggml_openvino_source_mapping & m) {
return fnv1a_u64(m.identity, m.offset + address - m.begin);
};
for (const auto & m : mappings) {
if (contains(m)) {
return fingerprint(m);
}
}
#ifdef __linux__
std::ifstream maps("/proc/self/maps");
std::string line;
while (std::getline(maps, line)) {
unsigned long long begin, end, offset, inode;
unsigned int dev_major, dev_minor;
char permissions[5];
int path_start = 0;
if (sscanf(line.c_str(), "%llx-%llx %4s %llx %x:%x %llu %n", &begin, &end, permissions,
&offset, &dev_major, &dev_minor, &inode, &path_start) != 7 || inode == 0) {
continue;
}
ggml_openvino_source_mapping m{uintptr_t(begin), uintptr_t(end), offset, FNV_OFFSET};
if (!contains(m)) {
continue;
}
struct stat st;
const std::string path = line.substr(path_start);
if (stat(path.c_str(), &st) != 0 || !S_ISREG(st.st_mode) || uint64_t(st.st_ino) != inode ||
major(st.st_dev) != dev_major || minor(st.st_dev) != dev_minor) {
break;
}
m.identity = fnv1a_u64(m.identity, st.st_dev);
m.identity = fnv1a_u64(m.identity, st.st_ino);
m.identity = fnv1a_u64(m.identity, st.st_size);
m.identity = fnv1a_u64(m.identity, st.st_mtim.tv_sec);
m.identity = fnv1a_u64(m.identity, st.st_mtim.tv_nsec);
m.identity = fnv1a_u64(m.identity, st.st_ctim.tv_sec);
m.identity = fnv1a_u64(m.identity, st.st_ctim.tv_nsec);
mappings.push_back(m);
return fingerprint(m);
}
#elif defined(_WIN32)
MEMORY_BASIC_INFORMATION memory;
if (VirtualQuery(data, &memory, sizeof(memory)) == sizeof(memory) && memory.Type == MEM_MAPPED) {
std::wstring name(MAX_PATH, L'\0');
DWORD length = 0;
while (name.size() <= 32768) {
length = GetMappedFileNameW(GetCurrentProcess(), const_cast<void *>(data), name.data(), static_cast<DWORD>(name.size()));
if (length == 0 || length < name.size() - 1) {
break;
}
name.resize(name.size() * 2);
}
if (length > 0 && length < name.size() - 1) {
name.resize(length);
const std::wstring path = L"\\\\?\\GLOBALROOT" + name;
HANDLE file = CreateFileW(path.c_str(), FILE_READ_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file != INVALID_HANDLE_VALUE) {
BY_HANDLE_FILE_INFORMATION info;
FILE_BASIC_INFO basic;
const bool valid = GetFileInformationByHandle(file, &info) &&
GetFileInformationByHandleEx(file, FileBasicInfo, &basic, sizeof(basic));
CloseHandle(file);
if (valid) {
const uint64_t file_size = (uint64_t(info.nFileSizeHigh) << 32) | info.nFileSizeLow;
const uintptr_t begin = reinterpret_cast<uintptr_t>(memory.AllocationBase);
if (file_size <= std::numeric_limits<uintptr_t>::max() - begin) {
ggml_openvino_source_mapping m{begin, begin + static_cast<uintptr_t>(file_size), 0,
fnv1a(FNV_OFFSET, "win32", 5)};
if (contains(m)) {
m.identity = fnv1a_u64(m.identity, info.dwVolumeSerialNumber);
m.identity = fnv1a_u64(m.identity, (uint64_t(info.nFileIndexHigh) << 32) | info.nFileIndexLow);
m.identity = fnv1a_u64(m.identity, file_size);
m.identity = fnv1a_u64(m.identity, (uint64_t(info.ftLastWriteTime.dwHighDateTime) << 32) |
info.ftLastWriteTime.dwLowDateTime);
m.identity = fnv1a_u64(m.identity, static_cast<uint64_t>(basic.ChangeTime.QuadPart));
mappings.push_back(m);
return fingerprint(m);
}
}
}
}
}
}
#endif
if (ggml_openvino_model_cache_only()) {
GGML_ABORT("ggml-openvino: could not identify mapped GGUF weight; use --load-mode mmap");
}
uint64_t h = FNV_OFFSET;
const size_t head = std::min(size, WEIGHT_SAMPLE_BYTES);
h = fnv1a(h, data, head);
if (size > head) {
const size_t tail = std::min(size - head, WEIGHT_SAMPLE_BYTES);
h = fnv1a(h, static_cast<const uint8_t *>(data) + size - tail, tail);
}
return h;
}
std::string ggml_openvino_model_cache_dir() {
const char * dir = ggml_openvino_getenv_str("GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR");
if (!dir || strlen(dir) == 0) {
return std::string();
}
std::string path(dir);
if (ggml_openvino_model_cache_only()) {
return path;
}
// Create the cache directory (and parents) on first use so callers don't
// have to pre-create it; a missing dir would otherwise silently disable the
// cache (manifest/blob writes fail with no directory to write into).
@@ -173,13 +327,36 @@ std::string ggml_openvino_model_cache_dir() {
return path;
}
std::string ggml_openvino_model_cache_temp_path(const std::string & path) {
#ifdef _WIN32
const int pid = _getpid();
#else
const int pid = getpid();
#endif
return path + ".tmp." + std::to_string(pid) + "." + std::to_string(ggml_time_us());
}
uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
const std::string & device,
bool fa,
const int32_t * rope_params,
int rope_len,
uint64_t extra_cfg) {
uint64_t extra_cfg,
const std::string & graph_signature) {
uint64_t h = FNV_OFFSET;
h = fnv1a_u64(h, 2);
h = fnv1a(h, graph_signature.data(), graph_signature.size());
for (const char * name : cache_settings) {
const char * value = ggml_openvino_getenv_str(name, "");
h = fnv1a(h, value, strlen(value) + 1);
}
if (const char * debug_nodes = ggml_openvino_getenv_str("GGML_OPENVINO_DEBUG_NODE")) {
h = fnv1a(h, "GGML_OPENVINO_DEBUG_NODE", sizeof("GGML_OPENVINO_DEBUG_NODE"));
h = fnv1a(h, debug_nodes, strlen(debug_nodes) + 1);
}
if (ggml_openvino_is_gpu() && ggml_openvino_getenv_int("GGML_OPENVINO_MOE_OP", 1) == 0) {
h = fnv1a(h, "GGML_OPENVINO_MOE_OP=0", sizeof("GGML_OPENVINO_MOE_OP=0"));
}
// Topology: node count + each node's op and name (cheap, and distinguishes
// graphs that share weights but differ structurally).
@@ -193,7 +370,7 @@ uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
// Weights: the model identity.
for_each_weight(cgraph, [&](const ggml_tensor * t) { h = fnv1a_u64(h, weight_fingerprint(t)); });
// Config that changes the produced blob.
// Device, model parameters, and backend configuration.
h = fnv1a(h, device.data(), device.size());
h = fnv1a_u64(h, fa ? 1u : 0u);
if (rope_params && rope_len > 0) {
@@ -216,7 +393,9 @@ std::string ggml_openvino_model_cache_manifest_path(const std::string & dir, uin
bool ggml_openvino_model_cache_write_manifest(const std::string & path,
const ggml_cgraph * cgraph,
uint64_t fingerprint) {
uint64_t fingerprint,
const std::vector<std::string> & inputs,
const std::vector<std::string> & outputs) {
std::ofstream f(path, std::ios::trunc);
if (!f.is_open()) {
return false;
@@ -227,12 +406,21 @@ bool ggml_openvino_model_cache_write_manifest(const std::string & path,
f << t->name << " " << t->ne[0] << " " << t->ne[1] << " " << t->ne[2] << " " << t->ne[3] << " "
<< static_cast<int>(t->type) << " " << hex64(weight_fingerprint(t)) << "\n";
});
f << "ports\n";
for (const auto * names : { &inputs, &outputs }) {
f << names->size() << '\n';
for (const auto & name : *names) {
f << std::quoted(name) << '\n';
}
}
return f.good();
}
bool ggml_openvino_model_cache_verify_manifest(const std::string & path,
const ggml_cgraph * cgraph,
uint64_t fingerprint) {
uint64_t fingerprint,
std::vector<std::string> & inputs,
std::vector<std::string> & outputs) {
std::ifstream f(path);
if (!f.is_open()) {
return false;
@@ -260,7 +448,7 @@ bool ggml_openvino_model_cache_verify_manifest(const std::string & path,
size_t idx = 0;
std::string line;
std::getline(f, line); // consume rest of ov_version line
while (std::getline(f, line)) {
while (idx < expected.size() && std::getline(f, line)) {
if (line.empty()) {
continue;
}
@@ -269,5 +457,28 @@ bool ggml_openvino_model_cache_verify_manifest(const std::string & path,
}
++idx;
}
return idx == expected.size();
if (idx != expected.size()) {
return false;
}
if (!std::getline(f, line)) {
return true;
}
if (line != "ports") {
return false;
}
for (auto * names : { &inputs, &outputs }) {
size_t count;
if (!(f >> count) || count > 100000) {
return false;
}
for (size_t i = 0; i < count; ++i) {
std::string name;
if (!(f >> std::quoted(name))) {
return false;
}
names->push_back(name);
}
}
f >> std::ws;
return f.eof();
}
+29 -28
View File
@@ -1,38 +1,39 @@
#pragma once
// Frontend-level compiled-model cache (GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR).
//
// The OpenVINO plugin's own ov::cache_dir caches the compiled blob keyed by the
// *OV model*, but producing that model still runs the full frontend every time:
// weight requantization (incl. the large token_embd F32 transient) and the
// ggml->OV graph conversion. This cache keys off a fingerprint computed directly
// from the ggml cgraph, so a hit skips requant + convert + compile entirely and
// instead imports a previously exported CompiledModel blob.
//
// Opt-in and independent from GGML_OPENVINO_CACHE_DIR. Default off.
// Compiled blobs include weights. Cache-only execution skips weight uploads and graph compilation.
#include "ggml.h"
#include <cstdint>
#include <string>
#include <vector>
// Returns the compiled-model cache directory from GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR,
// or empty if unset/disabled. When empty, callers must not use the cache.
bool ggml_openvino_model_cache_only();
void ggml_openvino_model_cache_init();
struct ggml_openvino_source_mapping {
uintptr_t begin;
uintptr_t end;
uint64_t offset;
uint64_t identity;
};
// Identify mmap weights without reading their pages. Cache mappings for one buffer lifetime.
uint64_t ggml_openvino_source_fingerprint(const void * data, size_t size, std::vector<ggml_openvino_source_mapping> & mappings);
uint64_t ggml_backend_openvino_weight_fingerprint(const ggml_tensor * tensor);
// Returns the compiled-model cache directory, or empty if unset.
std::string ggml_openvino_model_cache_dir();
std::string ggml_openvino_model_cache_temp_path(const std::string & path);
// Compute a stable 64-bit fingerprint identifying the model+config that a cgraph
// would compile to. Combines graph topology, a sampled hash of every weight
// tensor (name/shape/dtype + bounded byte sample), and the config that changes
// the produced blob (device, flash-attention, rope params, the compile-memory
// flags, stateful, and the OpenVINO version). `device` is the resolved device
// string; `fa` is the flash-attention flag; `rope_params`/`rope_len` cover the
// model's rope configuration; `extra_cfg` folds in any other blob-affecting bits.
// Hash graph structure, source weight identities, configuration, and OpenVINO version.
uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
const std::string & device,
bool fa,
const int32_t * rope_params,
int rope_len,
uint64_t extra_cfg);
uint64_t extra_cfg,
const std::string & graph_signature);
// Path to the compiled-blob file for a fingerprint (<dir>/<hex>.blob).
std::string ggml_openvino_model_cache_blob_path(const std::string & dir, uint64_t fingerprint);
@@ -41,16 +42,16 @@ std::string ggml_openvino_model_cache_blob_path(const std::string & dir, uint64_
// fingerprints, used to re-verify a hit before trusting the blob.
std::string ggml_openvino_model_cache_manifest_path(const std::string & dir, uint64_t fingerprint);
// Write/read the manifest. The manifest is a newline-separated list of
// "name ne0 ne1 ne2 ne3 type sample_hash" lines plus a header line with the
// fingerprint and OV version. Returns false on I/O error.
// Record weight metadata and source identities. Returns false on I/O error.
bool ggml_openvino_model_cache_write_manifest(const std::string & path,
const ggml_cgraph * cgraph,
uint64_t fingerprint);
uint64_t fingerprint,
const std::vector<std::string> & inputs,
const std::vector<std::string> & outputs);
// Verify that the cgraph's weights still match the stored manifest (guards the
// sampled-hash collision risk: a blob is only trusted if every weight's
// name/shape/type/sample-hash matches what was cached). Returns true on match.
// Require all weight metadata and source identities to match the manifest.
bool ggml_openvino_model_cache_verify_manifest(const std::string & path,
const ggml_cgraph * cgraph,
uint64_t fingerprint);
uint64_t fingerprint,
std::vector<std::string> & inputs,
std::vector<std::string> & outputs);
@@ -33,6 +33,8 @@ public:
const std::vector<std::string> & get_input_names() const { return m_input_names; }
const std::vector<std::string> & get_output_names() const { return m_output_names; }
size_t get_input_size() const override { return m_decoder->get_input_size(m_node_idx); }
ov::element::Type get_input_type(size_t index) const {
@@ -120,7 +122,10 @@ public:
auto view_it = m_tensor_map->find(m_input_names[idx]);
if (!base_name.empty() && view_it != m_tensor_map->end()) {
auto base_it = m_tensor_map->find(base_name);
if (base_it != m_tensor_map->end() &&
// A multi-output translator can publish a VIEW directly without materializing
// its packed parent (GatedDeltaNet attention/state). In that case the VIEW is the
// authoritative value. The node comparison retains the existing resolved-VIEW path.
if (base_it == m_tensor_map->end() ||
view_it->second.get_node_shared_ptr() != base_it->second.get_node_shared_ptr()) {
return view_it->second;
}
+11 -3
View File
@@ -27,10 +27,18 @@ OutputVector translate_add(const NodeContext & context) {
auto base_name = context.get_view_input_src_name(1, view_size - 1);
auto base = context.get_input(base_name);
// Stateful models drop the leading batch dim, so the base is rank 3 and both axes
// below shift down by one. Take them from the actual rank: the expert axis is always
// second from last, and the token axis is re-added just before it.
const auto base_rank = base.get_partial_shape().rank();
FRONT_END_OP_CONVERSION_CHECK(base_rank.is_static() && base_rank.get_length() >= 3,
"MoE expert sum needs a static rank of at least 3");
const int64_t rank = base_rank.get_length();
auto reduced = std::make_shared<ov::op::v1::ReduceSum>(
base, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2}), false);
auto res =
std::make_shared<ov::op::v0::Unsqueeze>(reduced, ov::op::v0::Constant::create(ov::element::i64, {1}, {1}));
base, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {rank - 2}), false);
auto res = std::make_shared<ov::op::v0::Unsqueeze>(
reduced, ov::op::v0::Constant::create(ov::element::i64, {1}, {rank - 3}));
return rename_outputs_with_suffix({res}, context.get_name());
}
@@ -32,10 +32,14 @@ OutputVector translate_argsort(const NodeContext & context) {
FRONT_END_OP_CONVERSION_CHECK(false, "Unsupported GGML_OP_ARGSORT order: ", order);
}
auto k = std::make_shared<ov::op::v0::Squeeze>(get_dimensions(input.get_node_shared_ptr(), {3}),
// Stateful models drop the leading size-1 batch dim, so the expert axis is 2 there
// instead of 3 (same rank-3-vs-rank-4 split as get_rows.cpp / process_view_input).
const int axis = (context.is_stateful() && input.get_partial_shape().rank() == 3) ? 2 : 3;
auto k = std::make_shared<ov::op::v0::Squeeze>(get_dimensions(input.get_node_shared_ptr(), {axis}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {0}));
auto topk = std::make_shared<ov::op::v11::TopK>(input, k, 3, mode, ov::op::v11::TopK::SortType::SORT_VALUES,
auto topk = std::make_shared<ov::op::v11::TopK>(input, k, axis, mode, ov::op::v11::TopK::SortType::SORT_VALUES,
context.get_output_type(), false);
return rename_outputs_with_suffix({topk->output(1)}, context.get_name());
+233
View File
@@ -0,0 +1,233 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/convolution.hpp>
#include <openvino/op/group_conv.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/unsqueeze.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_conv_2d(const NodeContext & context) {
num_inputs_check(context, 2, 2);
ov::Output<Node> kernel = process_view_input_new(context, 0);
ov::Output<Node> input = process_view_input_new(context, 1);
if (kernel.get_element_type() != input.get_element_type()) {
kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
}
const int32_t * params = context.get_output_op_params();
const int s0 = params[0];
const int s1 = params[1];
const int p0 = params[2];
const int p1 = params[3];
const int d0 = params[4];
const int d1 = params[5];
ov::Strides strides{static_cast<size_t>(s1), static_cast<size_t>(s0)};
ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::Strides dilations{static_cast<size_t>(d1), static_cast<size_t>(d0)};
ov::Output<Node> res = std::make_shared<ov::op::v1::Convolution>(
input, kernel, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
const auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_conv_2d_dw(const NodeContext & context) {
num_inputs_check(context, 2, 2);
ov::Output<Node> kernel = process_view_input_new(context, 0);
ov::Output<Node> input = process_view_input_new(context, 1);
if (kernel.get_element_type() != input.get_element_type()) {
kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
}
const int32_t * params = context.get_output_op_params();
const int s0 = params[0];
const int s1 = params[1];
const int p0 = params[2];
const int p1 = params[3];
const int d0 = params[4];
const int d1 = params[5];
// Reshape kernel from [C, 1, KH, KW] to [C, 1, 1, KH, KW] for 2D GroupConvolution
auto unsqueeze_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
auto kernel_5d = std::make_shared<ov::op::v0::Unsqueeze>(kernel, unsqueeze_axis);
ov::Strides strides{static_cast<size_t>(s1), static_cast<size_t>(s0)};
ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::Strides dilations{static_cast<size_t>(d1), static_cast<size_t>(d0)};
ov::Output<Node> res = std::make_shared<ov::op::v1::GroupConvolution>(
input, kernel_5d, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
const auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_conv_transpose_1d(const NodeContext & context) {
num_inputs_check(context, 2, 2);
ov::Output<Node> kernel = process_view_input_new(context, 0);
ov::Output<Node> input = process_view_input_new(context, 1);
if (kernel.get_element_type() != input.get_element_type()) {
kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
}
const int32_t * params = context.get_output_op_params();
const int s0 = params[0];
const int p0 = params[1];
const int d0 = params[2];
const auto kernel_shape = context.get_input_shape(0).to_shape(); // [1, Cin, Cout, K]
const int64_t Cin = kernel_shape[1];
const int64_t Cout = kernel_shape[2];
const int64_t K = kernel_shape[3];
const auto input_shape = context.get_input_shape(1).to_shape(); // [1, N, Cin, L]
const int64_t N = input_shape[0] * input_shape[1];
const int64_t L = input_shape[3];
auto kernel_3d = std::make_shared<ov::op::v1::Reshape>(
kernel, ov::op::v0::Constant::create(ov::element::i64, {3}, {Cin, Cout, K}), false);
auto input_3d = std::make_shared<ov::op::v1::Reshape>(
input, ov::op::v0::Constant::create(ov::element::i64, {3}, {N, Cin, L}), false);
ov::Strides strides{static_cast<size_t>(s0)};
ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p0)};
ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p0)};
ov::Strides dilations{static_cast<size_t>(d0)};
auto conv_tr = std::make_shared<ov::op::v1::ConvolutionBackpropData>(
input_3d, kernel_3d, strides, pads_begin, pads_end, dilations);
const auto out_shape = context.get_output_shape().to_shape();
auto out_shape_const = ov::op::v0::Constant::create(
ov::element::i64, {4}, {static_cast<int64_t>(out_shape[0]), static_cast<int64_t>(out_shape[1]),
static_cast<int64_t>(out_shape[2]), static_cast<int64_t>(out_shape[3])});
ov::Output<Node> res = std::make_shared<ov::op::v1::Reshape>(conv_tr, out_shape_const, false);
const auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_conv_transpose_2d(const NodeContext & context) {
num_inputs_check(context, 2, 2);
ov::Output<Node> kernel = process_view_input_new(context, 0);
ov::Output<Node> input = process_view_input_new(context, 1);
if (kernel.get_element_type() != input.get_element_type()) {
kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
}
const int32_t * params = context.get_output_op_params();
const int stride = params[0];
ov::Strides strides{static_cast<size_t>(stride), static_cast<size_t>(stride)};
ov::CoordinateDiff pads_begin{0, 0};
ov::CoordinateDiff pads_end{0, 0};
ov::Strides dilations{1, 1};
ov::Output<Node> res = std::make_shared<ov::op::v1::ConvolutionBackpropData>(
input, kernel, strides, pads_begin, pads_end, dilations);
const auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_conv_3d(const NodeContext & context) {
num_inputs_check(context, 2, 2);
ov::Output<Node> kernel = process_view_input_new(context, 0);
ov::Output<Node> input = process_view_input_new(context, 1);
if (kernel.get_element_type() != input.get_element_type()) {
kernel = std::make_shared<ov::op::v0::Convert>(kernel, input.get_element_type());
}
const int32_t * params = context.get_output_op_params();
const int s0 = params[0];
const int s1 = params[1];
const int s2 = params[2];
const int p0 = params[3];
const int p1 = params[4];
const int p2 = params[5];
const int d0 = params[6];
const int d1 = params[7];
const int d2 = params[8];
const int c = params[9];
const int n = params[10];
const int oc = params[11];
const auto kshape = context.get_input_shape(0).to_shape(); // [c*oc, KD, KH, KW]
const int64_t KD = kshape[1];
const int64_t KH = kshape[2];
const int64_t KW = kshape[3];
const auto inshape = context.get_input_shape(1).to_shape(); // [c*n, ID, IH, IW]
const int64_t ID = inshape[1];
const int64_t IH = inshape[2];
const int64_t IW = inshape[3];
auto kernel_5d = std::make_shared<ov::op::v1::Reshape>(
kernel, ov::op::v0::Constant::create(ov::element::i64, {5}, {static_cast<int64_t>(oc), static_cast<int64_t>(c), KD, KH, KW}), false);
auto input_5d = std::make_shared<ov::op::v1::Reshape>(
input, ov::op::v0::Constant::create(ov::element::i64, {5}, {static_cast<int64_t>(n), static_cast<int64_t>(c), ID, IH, IW}), false);
ov::Strides strides{static_cast<size_t>(s2), static_cast<size_t>(s1), static_cast<size_t>(s0)};
ov::CoordinateDiff pads_begin{static_cast<ptrdiff_t>(p2), static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::CoordinateDiff pads_end{static_cast<ptrdiff_t>(p2), static_cast<ptrdiff_t>(p1), static_cast<ptrdiff_t>(p0)};
ov::Strides dilations{static_cast<size_t>(d2), static_cast<size_t>(d1), static_cast<size_t>(d0)};
auto conv = std::make_shared<ov::op::v1::Convolution>(
input_5d, kernel_5d, strides, pads_begin, pads_end, dilations, ov::op::PadType::EXPLICIT);
const auto out_shape = context.get_output_shape().to_shape(); // [oc*n, OD, OH, OW]
auto out_shape_const = ov::op::v0::Constant::create(
ov::element::i64, {4}, {static_cast<int64_t>(out_shape[0]), static_cast<int64_t>(out_shape[1]),
static_cast<int64_t>(out_shape[2]), static_cast<int64_t>(out_shape[3])});
ov::Output<Node> res = std::make_shared<ov::op::v1::Reshape>(conv, out_shape_const, false);
const auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
+40 -21
View File
@@ -90,10 +90,24 @@ OutputVector translate_cpy(const NodeContext & context) {
return {context.get_input(1)};
}
}
// op_case 7/8/9 are the single-slot variants; op_case 10 writes native GDN state into a
// multi-slot cache without rollback snapshots.
const bool single_slot_assign = op_case >= 7 && op_case <= 9;
const bool direct_gdn_state = op_case == 7 || op_case == 10;
int writeback_case = op_case;
if (op_case == 10) {
writeback_case = 1;
} else if (single_slot_assign) {
writeback_case = op_case - 6;
}
const std::string slot_begin_name = "rs_slot_begin_" + context.get_name();
const bool slice_assign =
context.has_input(slot_begin_name) && !context.is_stateful() && (op_case >= 1 && op_case <= 3);
const bool slice_assign = writeback_case >= 1 && writeback_case <= 3 &&
(single_slot_assign || context.has_input(slot_begin_name));
if (slice_assign) {
if (single_slot_assign && writeback_case == 3) {
return {context.get_input(1)};
}
const int64_t slot_axis = 2;
auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
@@ -103,28 +117,25 @@ OutputVector translate_cpy(const NodeContext & context) {
std::vector<int64_t>{1, 1, -1, output_shape[3].get_length()});
ov::Output<ov::Node> src;
ov::Output<ov::Node> begin = context.get_input(slot_begin_name);
ov::Output<ov::Node> begin;
if (!single_slot_assign) {
begin = context.get_input(slot_begin_name);
}
auto base = context.get_input(1);
if (op_case == 1) {
ov::Output<ov::Node> state_begin;
const std::string src_begin_name = "rs_src_begin_" + context.get_name();
if (context.has_input(src_begin_name)) {
state_begin = context.get_input(src_begin_name);
if (writeback_case == 1) {
if (direct_gdn_state) {
// Non-rollback GDN publishes state directly as [active_slots, heads, value_dim,
// key_dim]. Flatten each active slot before replacing or updating the cache.
src = std::make_shared<ov::op::v1::Reshape>(context.get_input(0), feature, false);
} else {
auto ssm_state_size = context.get_ssm_state_size();
if (context.has_input("s_copy_active_slot_len")) {
auto len = context.get_input("s_copy_active_slot_len");
auto state_rows = std::make_shared<ov::op::v1::Multiply>(
ov::op::v0::Constant::create(ov::element::i64, {1}, {ssm_state_size}), len);
state_begin = std::make_shared<ov::op::v0::Negative>(state_rows);
} else {
state_begin = ov::op::v0::Constant::create(ov::element::i64, {1}, {-ssm_state_size});
}
// Multi-slot rollback still consumes GGML's packed [attention | state snapshots]
// layout. Slice the state block using the runtime source offset.
auto src_begin = context.get_input("rs_src_begin_" + context.get_name());
auto state_part =
std::make_shared<ov::op::v8::Slice>(context.get_input(0), src_begin, int_max, one, axis);
src = std::make_shared<ov::op::v1::Reshape>(state_part, feature, false);
}
auto state_part =
std::make_shared<ov::op::v8::Slice>(context.get_input(0), state_begin, int_max, one, axis);
src = std::make_shared<ov::op::v1::Reshape>(state_part, feature, false);
} else if (op_case == 2) {
} else if (writeback_case == 2) {
// conv_input is [previous conv state | new tokens]; the snapshot is the conv_kernel_size - 1
// columns ending at the last *valid* token. Gather (rather than Slice) keeps the output
// shape static even though the window start is a runtime value.
@@ -177,6 +188,10 @@ OutputVector translate_cpy(const NodeContext & context) {
src = std::make_shared<ov::op::v0::Convert>(src, context.get_output_type());
}
if (single_slot_assign) {
return rename_outputs_with_suffix({src}, context.get_name());
}
auto src_len = std::make_shared<ov::op::v8::Gather>(
std::make_shared<ov::op::v3::ShapeOf>(src, ov::element::i64), axis,
ov::op::v0::Constant::create(ov::element::i64, {}, {0}));
@@ -200,6 +215,10 @@ OutputVector translate_cpy(const NodeContext & context) {
src = std::make_shared<ov::op::v0::Convert>(src, context.get_output_type());
}
if (single_slot_assign) {
return rename_outputs_with_suffix({src}, context.get_name());
}
auto src_len =
std::make_shared<ov::op::v8::Gather>(std::make_shared<ov::op::v3::ShapeOf>(src, ov::element::i64), axis,
ov::op::v0::Constant::create(ov::element::i64, {}, {0}));
@@ -126,8 +126,7 @@ OutputVector translate_flash_attn_ext(const NodeContext & context) {
if (env != nullptr) {
return ggml_openvino_getenv_int("GGML_OPENVINO_MANUAL_GQA_ATTN") > 0;
}
const char * dev = ggml_openvino_getenv_str("GGML_OPENVINO_DEVICE");
return dev != nullptr && std::string(dev) == "GPU";
return ggml_openvino_is_gpu();
}();
const bool use_manual_gqa_attention =
manual_gqa_enabled && factor > 1 && num_heads_kv > 1 && !context.is_stateful();
@@ -3,6 +3,7 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include "ggml-openvino/ggml-openvino-extra.h"
#include <cmath>
#include <cstdint>
@@ -13,14 +14,17 @@
#include <openvino/op/concat.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/divide.hpp>
#include <openvino/op/exp.hpp>
#include <openvino/op/gather.hpp>
#include <openvino/op/less.hpp>
#include <openvino/op/loop.hpp>
#include <openvino/op/matmul.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/reduce_mean.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/squeeze.hpp>
#include <openvino/op/sqrt.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/op/tile.hpp>
#include <openvino/op/transpose.hpp>
@@ -34,6 +38,60 @@ namespace op {
static OutputVector translate_gated_delta_net_ref(const NodeContext & context);
static bool match_gdn_l2_norm(const Output<Node> & normalized, Output<Node> & input, float & eps) {
// Match the RMSNorm decomposition emitted by translate_rms_norm, followed by GGML SCALE.
const auto scale = ov::as_type_ptr<ov::op::v1::Multiply>(normalized.get_node_shared_ptr());
if (!scale) {
return false;
}
const auto factor = ov::as_type_ptr<ov::op::v0::Constant>(scale->get_input_node_shared_ptr(1));
const auto rms = ov::as_type_ptr<ov::op::v1::Multiply>(scale->get_input_node_shared_ptr(0));
if (!factor || ov::shape_size(factor->get_shape()) != 1 || !rms) {
return false;
}
const auto x = rms->input_value(0);
const auto & shape = x.get_partial_shape();
if (x.get_element_type() != ov::element::f32 || shape.rank() != 4 || shape[3].is_dynamic() ||
shape[3].get_length() <= 0 || normalized.get_partial_shape() != shape) {
return false;
}
const float dim = static_cast<float>(shape[3].get_length());
if (factor->cast_vector<float>()[0] != 1.0f / std::sqrt(dim)) {
return false;
}
const auto reciprocal = ov::as_type_ptr<ov::op::v1::Divide>(rms->get_input_node_shared_ptr(1));
if (!reciprocal) {
return false;
}
const auto one = ov::as_type_ptr<ov::op::v0::Constant>(reciprocal->get_input_node_shared_ptr(0));
const auto root = ov::as_type_ptr<ov::op::v0::Sqrt>(reciprocal->get_input_node_shared_ptr(1));
if (!one || ov::shape_size(one->get_shape()) != 1 || one->cast_vector<float>()[0] != 1.0f || !root) {
return false;
}
const auto add = ov::as_type_ptr<ov::op::v1::Add>(root->get_input_node_shared_ptr(0));
if (!add) {
return false;
}
const auto mean = ov::as_type_ptr<ov::op::v1::ReduceMean>(add->get_input_node_shared_ptr(0));
const auto rms_eps = ov::as_type_ptr<ov::op::v0::Constant>(add->get_input_node_shared_ptr(1));
if (!mean || !mean->get_keep_dims() || !rms_eps || ov::shape_size(rms_eps->get_shape()) != 1) {
return false;
}
const auto axes = ov::as_type_ptr<ov::op::v0::Constant>(mean->get_input_node_shared_ptr(1));
const auto square = ov::as_type_ptr<ov::op::v1::Multiply>(mean->get_input_node_shared_ptr(0));
if (!axes || axes->cast_vector<int64_t>() != std::vector<int64_t>{-1} || !square ||
square->input_value(0) != x || square->input_value(1) != x) {
return false;
}
// RMSNorm(x, rms_eps) / sqrt(D) = x / sqrt(sum(x*x) + D*rms_eps).
eps = dim * rms_eps->cast_vector<float>()[0];
if (!std::isfinite(eps) || eps <= 0.0f) {
return false;
}
input = x;
return true;
}
OutputVector translate_gated_delta_net(const NodeContext & context) {
auto v_shape = context.get_input_shape(2).to_shape(); // [B, T, H_v, S_v]
auto q_shape = context.get_input_shape(0).to_shape(); // [B, T, H_k, S_k]
@@ -53,11 +111,21 @@ OutputVector translate_gated_delta_net(const NodeContext & context) {
auto q = context.get_input(0);
auto k = context.get_input(1);
auto v = process_view_input(context, 2, H_v * S_v);
auto v = process_view_input(context, 2, H_v * S_v, 3);
auto g = context.get_input(3);
auto beta = context.get_input(4);
auto state = context.get_input(5);
Output<Node> raw_q, raw_k;
float q_eps = 1e-6f, k_eps = 1e-6f;
const bool fuse_qk_l2norm = ggml_openvino_is_gpu() &&
match_gdn_l2_norm(q, raw_q, q_eps) && match_gdn_l2_norm(k, raw_k, k_eps);
if (fuse_qk_l2norm) {
// Keep head tiling below; GDN applies normalization per head and keeps its attention scale.
q = raw_q;
k = raw_k;
}
// ggml maps GQA heads in tiled order, while OV GDN maps repeated heads in grouped order.
if (H_v != H_k) {
const int64_t repeat = H_v / H_k;
@@ -109,7 +177,7 @@ OutputVector translate_gated_delta_net(const NodeContext & context) {
// << ", v=" << v.get_partial_shape() << ", g=" << g.get_partial_shape()
// << ", beta=" << beta.get_partial_shape() << ", state=" << state.get_partial_shape() << std::endl;
auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, g, beta);
auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, g, beta, fuse_qk_l2norm, q_eps, k_eps);
auto attn_4d = gdn->output(0);
auto state_4d = gdn->output(1); // [B, H_v, key_dim, value_dim]
@@ -118,6 +186,13 @@ OutputVector translate_gated_delta_net(const NodeContext & context) {
// Transpose output state back to ggml layout [B, H_v, value_dim, key_dim]
auto state_transposed = std::make_shared<ov::op::v1::Transpose>(state_4d, state_perm);
if (context.get_output_names().size() == 2) {
// The canonical graph consumes the packed GGML result only through separate attention
// and state VIEWs. Publish the native outputs under those VIEW names to avoid
// flatten -> concat -> reshape -> slice -> reshape chains. This also works for B > 1.
return rename_outputs_with_suffix({attn_4d, state_transposed}, context.get_name());
}
auto flat_shape_1d = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
auto attn = std::make_shared<ov::op::v1::Reshape>(attn_4d, flat_shape_1d, false);
auto new_state = std::make_shared<ov::op::v1::Reshape>(state_transposed, flat_shape_1d, false);
@@ -310,6 +385,11 @@ static OutputVector translate_gated_delta_net_ref(const NodeContext & context) {
// state: [B*H_v, S_v, S_v] -> [B, H_v, S_v, S_v] -> flatten
auto state_4d_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{B, H_v, S_v, S_v});
auto state_4d = std::make_shared<ov::op::v1::Reshape>(final_state_out, state_4d_shape, false);
if (context.get_output_names().size() == 2) {
// Match the fused translator's direct attention/state contract.
return rename_outputs_with_suffix({attn_perm, state_4d}, context.get_name());
}
auto state_1d = std::make_shared<ov::op::v1::Reshape>(state_4d, flat_shape_1d, false);
// Concat [attn | state] and reshape to final output
@@ -44,6 +44,10 @@ OutputVector translate_get_rows(const NodeContext & context) {
}
auto op_case = context.get_op_case();
if (op_case == 3 || op_case == 4) {
return {data};
}
ov::Output<ov::Node> indices;
if ((op_case == 1 || op_case == 2) && context.has_input("s_copy_active_slot_len")) {
// Recurrent state reorder (inp->s_copy): slice the active (op_case 1) or extra (op_case 2)
@@ -66,8 +70,19 @@ OutputVector translate_get_rows(const NodeContext & context) {
// data[1,b,x,y] ind[1,1,b,x'] test-backend-ops case
// data[x,y] ind[1,1,1,x'] normal case
indices =
std::make_shared<ov::op::v0::Squeeze>(indices, ov::op::v0::Constant::create(ov::element::i64, {2}, {0, 1}));
// Squeeze the leading dims down to [b,x']. Stateful models drop one rank, so a hardcoded
// {0,1} would also strip the batch dim whenever b == 1 (every decode step).
const auto indices_rank = indices.get_partial_shape().rank();
FRONT_END_OP_CONVERSION_CHECK(indices_rank.is_static(), "Expected static rank for GET_ROWS indices");
std::vector<int64_t> indices_squeeze_axes;
for (int64_t i = 0; i + 2 < indices_rank.get_length(); ++i) {
indices_squeeze_axes.push_back(i);
}
if (!indices_squeeze_axes.empty()) {
indices = std::make_shared<ov::op::v0::Squeeze>(
indices, ov::op::v0::Constant::create(ov::element::i64, {indices_squeeze_axes.size()},
indices_squeeze_axes));
}
if (row_offset != 0) {
indices = std::make_shared<ov::op::v1::Add>(
indices, ov::op::v0::Constant::create(indices.get_element_type(), {}, {row_offset}));
@@ -6,11 +6,13 @@
#include <memory>
#include <openvino/core/shape.hpp>
#include <openvino/core/strides.hpp>
#include <openvino/op/concat.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/extractimagepatches.hpp>
#include <openvino/op/pad.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/slice.hpp>
#include <openvino/op/transpose.hpp>
#include <openvino/op/util/attr_types.hpp>
@@ -113,6 +115,124 @@ OutputVector translate_im2col(const NodeContext & context) {
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_im2col_3d(const NodeContext & context) {
num_inputs_check(context, 2, 2);
const int32_t * params = context.get_output_op_params();
int32_t s0 = params[0];
int32_t s1 = params[1];
int32_t s2 = params[2];
int32_t p0 = params[3];
int32_t p1 = params[4];
int32_t p2 = params[5];
int32_t d0 = params[6];
int32_t d1 = params[7];
int32_t d2 = params[8];
int32_t IC = params[9];
ov::Output<Node> image = process_view_input_new(context, 1);
const ov::Shape kernel_shape = context.get_input(0).get_shape();
const ov::Shape image_shape = image.get_shape();
const ov::Shape out_shape = context.get_output_shape().to_shape();
const size_t KD = kernel_shape[1];
const size_t KH = kernel_shape[2];
const size_t KW = kernel_shape[3];
const size_t N = image_shape[0] / static_cast<size_t>(IC);
const size_t ID = image_shape[1];
const size_t IH = image_shape[2];
const size_t IW = image_shape[3];
const size_t OD = (ID + 2 * p2 - d2 * (KD - 1) - 1) / s2 + 1;
const size_t OH = (IH + 2 * p1 - d1 * (KH - 1) - 1) / s1 + 1;
const size_t OW = (IW + 2 * p0 - d0 * (KW - 1) - 1) / s0 + 1;
if (N == 0 || OD == 0 || OH == 0 || OW == 0) {
auto output_type = context.get_output_type();
ov::Output<Node> res = ov::op::v0::Constant::create(
output_type, ov::Shape{N * OD, OH, OW, static_cast<size_t>(IC * KD * KH * KW)}, {});
return rename_outputs_with_suffix({res}, context.get_name());
}
const size_t IH_pad = IH + 2 * p1;
const size_t IW_pad = IW + 2 * p0;
auto image_5d_shape = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{5},
std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(IC), static_cast<int64_t>(ID),
static_cast<int64_t>(IH), static_cast<int64_t>(IW)});
auto image_5d = std::make_shared<ov::op::v1::Reshape>(image, image_5d_shape, false);
auto pads_begin = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{5}, std::vector<int64_t>{0, 0, p2, p1, p0});
auto pads_end = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{5}, std::vector<int64_t>{0, 0, p2, p1, p0});
auto pad_3d = std::make_shared<ov::op::v1::Pad>(image_5d, pads_begin, pads_end, ov::op::PadMode::CONSTANT);
const ov::Shape patch_sizes = {KH, KW};
const ov::Strides strides = {static_cast<size_t>(s1), static_cast<size_t>(s0)};
const ov::Shape rates = {static_cast<size_t>(d1), static_cast<size_t>(d0)};
ov::OutputVector kd_slices;
kd_slices.reserve(KD);
auto perm_nod = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 2, 1, 3, 4});
auto reshape_4d_shape = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{4},
std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(IC),
static_cast<int64_t>(IH_pad), static_cast<int64_t>(IW_pad)});
auto perm1 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 2, 3, 1});
auto r1_shape = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{5},
std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
static_cast<int64_t>(KH * KW), static_cast<int64_t>(IC)});
auto perm2 = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 1, 2, 4, 3});
auto r2_shape = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{6},
std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
static_cast<int64_t>(IC), 1, static_cast<int64_t>(KH * KW)});
auto step_c = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(s2)});
auto axes_c = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
for (size_t ikd = 0; ikd < KD; ++ikd) {
auto start_c = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(ikd * d2)});
auto stop_c = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{1}, {static_cast<int64_t>(ikd * d2 + OD * s2)});
auto depth_slice = std::make_shared<ov::op::v8::Slice>(pad_3d, start_c, stop_c, step_c, axes_c);
auto depth_slice_trans = std::make_shared<ov::op::v1::Transpose>(depth_slice, perm_nod);
auto depth_slice_4d = std::make_shared<ov::op::v1::Reshape>(depth_slice_trans, reshape_4d_shape, false);
auto patches = std::make_shared<ov::op::v3::ExtractImagePatches>(
depth_slice_4d, patch_sizes, strides, rates, ov::op::PadType::VALID);
auto t1 = std::make_shared<ov::op::v1::Transpose>(patches, perm1);
auto r1 = std::make_shared<ov::op::v1::Reshape>(t1, r1_shape, false);
auto t2 = std::make_shared<ov::op::v1::Transpose>(r1, perm2);
auto r2 = std::make_shared<ov::op::v1::Reshape>(t2, r2_shape, false);
kd_slices.push_back(r2);
}
ov::Output<Node> res;
if (KD == 1) {
res = kd_slices[0];
} else {
res = std::make_shared<ov::op::v0::Concat>(kd_slices, 4);
}
auto final_shape = ov::op::v0::Constant::create(
ov::element::i64, ov::Shape{4},
std::vector<int64_t>{static_cast<int64_t>(N * OD), static_cast<int64_t>(OH), static_cast<int64_t>(OW),
static_cast<int64_t>(IC * KD * KH * KW)});
res = std::make_shared<ov::op::v1::Reshape>(res, final_shape, false);
auto output_type = context.get_output_type();
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
@@ -28,7 +28,7 @@ OutputVector translate_l2_norm(const NodeContext & context) {
// 93: [ 128, 16, 1, 2] L2_NORM q_conv_predelta-1
// [ 128, 16, 1, 2] 0: VIEW q_conv-1
auto output_shape = context.get_output_shape().to_shape();
input_node = process_view_input(context, 0, output_shape[2] * output_shape[3]);
input_node = process_view_input(context, 0, output_shape[2] * output_shape[3], 3);
input_node =
std::make_shared<ov::op::v0::Squeeze>(input_node, ov::op::v0::Constant::create(ov::element::i64, {1}, {0}));
@@ -0,0 +1,27 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include <memory>
#include <openvino/op/constant.hpp>
#include <openvino/op/reduce_mean.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_mean(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});
auto res = std::make_shared<ov::op::v1::ReduceMean>(input, axis, true);
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -40,6 +40,21 @@ ov::Output<ov::Node> slice_axis(const ov::Output<ov::Node> & input, int64_t axis
const_i64({axis}));
}
// GGML tensors are rank 4, but stateful models drop the leading size-1 batch dim, so
// activations and ids arrive one rank lower. Pick the trailing dims by actual rank.
std::vector<int> trailing_dims(const ov::Output<ov::Node> & input, int count) {
const auto rank = input.get_partial_shape().rank();
FRONT_END_OP_CONVERSION_CHECK(rank.is_static(), "Expected static rank for MUL_MAT_ID input");
const int rank_len = static_cast<int>(rank.get_length());
FRONT_END_OP_CONVERSION_CHECK(rank_len >= count, "MUL_MAT_ID input rank is too low");
std::vector<int> dims;
for (int i = rank_len - count; i < rank_len; ++i) {
dims.push_back(i);
}
return dims;
}
ov::Output<ov::Node> static_shape_dims_or_shapeof(const ov::Output<ov::Node> & input,
const std::vector<int> & dims) {
const auto & partial_shape = input.get_partial_shape();
@@ -157,6 +172,13 @@ OutputVector translate_mul_mat_id(const NodeContext & context) {
auto activations = process_view_input_new(context, 1);
auto ids = process_view_input_new(context, 2);
if (activations.get_partial_shape().rank() == 3) {
activations = std::make_shared<ov::op::v0::Unsqueeze>(activations, const_i64({0}));
}
if (ids.get_partial_shape().rank() == 3) {
ids = std::make_shared<ov::op::v0::Unsqueeze>(ids, const_i64({0}));
}
if (expert_weights.get_element_type() == ov::element::u8 && expert_weights.get_partial_shape().rank().is_static() &&
expert_weights.get_partial_shape().rank().get_length() == 5) {
return rename_outputs_with_suffix({translate_mul_mat_id_mxfp4_packed(context, expert_weights, activations, ids)},
@@ -186,8 +208,8 @@ OutputVector translate_mul_mat_id(const NodeContext & context) {
expert_weights = std::make_shared<ov::op::v1::Reshape>(expert_weights, expert_weights_shape_3d, false);
}
auto activations_shape_3d = static_shape_dims_or_shapeof(activations, {1, 2, 3});
auto ids_shape_2d = static_shape_dims_or_shapeof(ids, {2, 3});
auto activations_shape_3d = static_shape_dims_or_shapeof(activations, trailing_dims(activations, 3));
auto ids_shape_2d = static_shape_dims_or_shapeof(ids, trailing_dims(ids, 2));
activations = std::make_shared<ov::op::v1::Reshape>(activations, activations_shape_3d, false);
ids = std::make_shared<ov::op::v1::Reshape>(ids, ids_shape_2d, false);
@@ -197,7 +219,7 @@ OutputVector translate_mul_mat_id(const NodeContext & context) {
}
const auto output_type = context.get_output_type();
const auto activations_type = ggml_openvino_get_device_name() == "GPU" ? ov::element::f16 : ov::element::f32;
const auto activations_type = ggml_openvino_is_gpu() ? ov::element::f16 : ov::element::f32;
if (activations.get_element_type() != activations_type) {
activations = std::make_shared<ov::op::v0::Convert>(activations, activations_type);
}
@@ -210,11 +232,14 @@ OutputVector translate_mul_mat_id(const NodeContext & context) {
ov::Output<ov::Node> result = std::make_shared<ov::op::internal::GatherMatmul>(activations_for_gather, expert_weights, ids);
// result is [n_used, n_tokens, m]; GGML expects [1, n_tokens, n_used, m].
// result is [n_used, n_tokens, m]; GGML expects [1, n_tokens, n_used, m], except on the
// stateful path where the leading batch dim is dropped.
auto result_transpose_order = const_i64({1, 0, 2});
result = std::make_shared<ov::op::v1::Transpose>(result, result_transpose_order);
auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
result = std::make_shared<ov::op::v0::Unsqueeze>(result, unsqueeze_axes);
if (!context.is_stateful()) {
auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
result = std::make_shared<ov::op::v0::Unsqueeze>(result, unsqueeze_axes);
}
if (result.get_element_type() != output_type) {
result = std::make_shared<ov::op::v0::Convert>(result, output_type);
+39 -66
View File
@@ -24,84 +24,57 @@ OutputVector translate_reshape(const NodeContext & context) {
return {context.get_input(0)};
}
int op_case = context.get_op_case();
auto output_shape = context.get_output_shape().to_shape();
const int op_case = context.get_op_case();
const auto output_shape = context.get_output_shape().to_shape();
std::vector<int64_t> shape(output_shape.begin(), output_shape.end());
std::shared_ptr<ov::Node> new_shape_node;
if (op_case == 0) {
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, context.get_output_shape().to_shape());
} else if (op_case == 1) {
if (context.is_stateful()) {
new_shape_node = ov::op::v0::Constant::create(
ov::element::i64, {3}, std::vector<int64_t>{-1, (int64_t) output_shape[2], (int64_t) output_shape[3]});
} else {
new_shape_node = ov::op::v0::Constant::create(
ov::element::i64, {4},
std::vector<int64_t>{(int64_t) output_shape[0], -1, (int64_t) output_shape[2],
(int64_t) output_shape[3]});
switch (op_case) {
case 0:
break;
case 1:
case 9:
shape[1] = -1;
if (context.is_stateful() && op_case == 1) {
shape.erase(shape.begin());
}
} else if (op_case == 2) {
new_shape_node = ov::op::v0::Constant::create(
ov::element::i64, {4},
std::vector<int64_t>{(int64_t) output_shape[0], (int64_t) output_shape[1], -1, (int64_t) output_shape[3]});
} else if (op_case == 3) {
// - 14: [ 1, 1024, 1, 1] RESHAPE Vcur-0 (reshaped) (reshaped)
// [ 512, 2, 1, 1] 0: RESHAPE Vcur-0 (reshaped)
// - 15: [ 1, 524288, 1, 1] RESHAPE cache_v_l0 (reshaped)
// [ 512, 1024, 1, 1] 0: NONE cache_v_l0
// - 16: [ 1, 524288, 1, 1] SET_ROWS cache_v_l0 (reshaped) (view)
// [ 1, 1024, 1, 1] 0: RESHAPE Vcur-0 (reshaped) (reshaped)
// [ 1024, 1, 1, 1] 1: NONE leaf_11
// [ 1, 524288, 1, 1] 2: RESHAPE cache_v_l0 (reshaped)
new_shape_node = ov::op::v0::Constant::create(
ov::element::i64, {4}, std::vector<int64_t>{(int64_t) output_shape[0], (int64_t) output_shape[1], -1, 1});
} else if (op_case == 4) {
break;
case 2:
case 3:
shape[2] = -1;
if (op_case == 3) {
shape[3] = 1;
}
break;
case 4:
return {context.get_input(0).get_node_shared_ptr()->input_value(0)};
} else if (op_case == 5) {
if (context.is_stateful()) {
std::vector<int64_t> shape_vec = {1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {3}, shape_vec);
} else {
std::vector<int64_t> shape_vec = {1, 1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
case 5:
case 7:
shape = {1, 1, -1, shape[3]};
if (context.is_stateful() && op_case == 5) {
shape.erase(shape.begin());
}
// // Alternative
// auto token_len = context.get_input("token_len");
// auto emb_size =
// ov::op::v0::Constant::create(ov::element::i64, {1}, {(int64_t) context.get_output_shape().to_shape()[3]});
// auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
// new_shape_node = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one, one, token_len, emb_size}, 0);
} else if (op_case == 6) {
// 14: [ 6144, 1, 2, 1] RESHAPE linear_attn_qkv_mixed-0
// [ 6144, 2, 1, 1] 0: MUL_MAT node_13
// reshape to [1, n_slot_active_len, -1, 6144]
break;
case 6:
// Recurrent inputs keep the active sequence count separate from the token count.
if (context.has_input("s_copy_active_slot_len")) {
auto n_slot_active_len = context.get_input("s_copy_active_slot_len");
auto emb_size = ov::op::v0::Constant::create(ov::element::i64, {1},
{(int64_t) context.get_output_shape().to_shape()[3]});
auto emb_size = ov::op::v0::Constant::create(ov::element::i64, {1}, {shape[3]});
auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
auto neg_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
new_shape_node =
std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one, n_slot_active_len, neg_one, emb_size}, 0);
} else {
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, context.get_output_shape().to_shape());
shape = {1, 1, -1, shape[3]};
}
} else if (op_case == 7) {
// 57: [ 2048, 2, 1, 1] RESHAPE linear_attn_out-0 (reshaped)
// [ 2048, 1, 2, 1] 0: MUL_MAT linear_attn_out-0
std::vector<int64_t> shape_vec = {1, 1, -1, (int64_t) context.get_output_shape().to_shape()[3]};
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
} else if (op_case == 8) {
// 106: [ 128, 128, 16, 2] RESHAPE state_predelta-1
// [ 262144, 2, 1, 1] 0: GET_ROWS node_86
auto output_shape = context.get_output_shape().to_shape();
std::vector<int64_t> shape_vec = {-1, (int64_t) output_shape[1], (int64_t) output_shape[2],
(int64_t) output_shape[3]};
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {4}, shape_vec);
break;
case 8:
shape[0] = -1;
break;
default:
FRONT_END_OP_CONVERSION_CHECK(false, "Unsupported RESHAPE case: ", op_case);
}
if (!new_shape_node) {
new_shape_node = ov::op::v0::Constant::create(ov::element::i64, {shape.size()}, shape);
}
auto res = std::make_shared<ov::op::v1::Reshape>(context.get_input(0), new_shape_node, false);
return rename_outputs_with_suffix({res}, context.get_name());
@@ -25,7 +25,17 @@ OutputVector translate_rms_norm(const NodeContext & context) {
auto op_case = context.get_op_case();
ov::Output<ov::Node> input_node;
if (op_case == 2) {
if (op_case == 3) {
// Flatten sequence and token dimensions to match the gate layout.
auto input_shape = context.get_input_shape(0).to_shape();
input_node = std::make_shared<ov::op::v1::Reshape>(
context.get_input(0),
ov::op::v0::Constant::create(
ov::element::i64, {4}, std::vector<int64_t>{1, -1, (int64_t) input_shape[2], (int64_t) input_shape[3]}),
false);
} else if (op_case == 1) {
input_node = process_view_input_new(context, 0);
} else if (op_case == 2) {
auto ssm_state_size = context.get_ssm_state_size();
// The GDN op packs [attn | new_state] along the row axis; the state occupies the last
// ssm_state_size * n_seqs rows. Slice it off (scaling by the active sequence count) to keep
+21 -14
View File
@@ -44,6 +44,8 @@ OutputVector translate_rope(const NodeContext & context) {
constexpr int TYPE_NORMAL = 0;
constexpr int TYPE_NEOX = 1;
constexpr int TYPE_IMROPE = 2;
constexpr int TYPE_VISION = 3;
constexpr int TYPE_MROPE = 4;
Output<Node> cos_theta_node;
Output<Node> sin_theta_node;
@@ -67,7 +69,7 @@ OutputVector translate_rope(const NodeContext & context) {
if (context.get_input_size() == 3) {
rope_freqs_weight = context.get_input(2).get_node_shared_ptr();
}
auto sin_cos = make_sin_cos(op_params, inp_pos, rope_freqs_weight, mode == TYPE_IMROPE, false);
auto sin_cos = make_sin_cos(op_params, inp_pos, rope_freqs_weight, mode, false, head_dim);
sin_theta_node = sin_cos.first;
cos_theta_node = sin_cos.second;
context.put_shared(cache_key + "_cos", cos_theta_node);
@@ -80,10 +82,11 @@ OutputVector translate_rope(const NodeContext & context) {
data_node = std::make_shared<ov::op::v0::Convert>(data_node, ov::element::f32);
}
const int64_t total_rope_dims = (mode == TYPE_VISION) ? (2 * n_dims) : n_dims;
FRONT_END_OP_CONVERSION_CHECK(n_offs >= 0 && (n_offs % 2 == 0),
"ROPE expects non-negative even n_offs");
FRONT_END_OP_CONVERSION_CHECK(n_dims > 0 && n_dims + n_offs <= head_dim && (n_dims % 2 == 0),
"ROPE expects even n_dims in [1, head_dim - n_offs]");
FRONT_END_OP_CONVERSION_CHECK(n_dims > 0 && total_rope_dims + n_offs <= head_dim && (n_dims % 2 == 0),
"ROPE expects even n_dims with total_rope_dims + n_offs <= head_dim");
// RoPEFusionFlux requires rank_equals(4) on x, t_cos and t_sin. The cos/sin
// tables are already built rank-4 ([1, S, 1, head_size/2]) for both modes. In
@@ -91,9 +94,10 @@ OutputVector translate_rope(const NodeContext & context) {
// to rank-4 ([1, S, n_heads, head_size]) here. Stateful RoPE already produced
// rank-4 output, so downstream attention is unaffected.
if (context.is_stateful()) {
const int64_t batch = static_cast<int64_t>(output_shape[0]);
auto r4_shape = ov::op::v0::Constant::create(
ov::element::i64, {4},
std::vector<int64_t>{1, -1, (int64_t) output_shape[2], (int64_t) output_shape[3]});
std::vector<int64_t>{batch, -1, (int64_t) output_shape[2], (int64_t) output_shape[3]});
data_node = std::make_shared<ov::op::v1::Reshape>(data_node, r4_shape, false);
}
// For TYPE_NORMAL rope (both stateful and stateless) we emit the Flux-style
@@ -103,6 +107,7 @@ OutputVector translate_rope(const NodeContext & context) {
auto axis_last = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
auto step_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
const int64_t batch = static_cast<int64_t>(output_shape[0]);
const int64_t n_heads = static_cast<int64_t>(output_shape[2]);
const int64_t half = n_dims / 2;
auto rot_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
@@ -112,7 +117,7 @@ OutputVector translate_rope(const NodeContext & context) {
auto neg_one_f = ov::op::v0::Constant::create(data_node->get_element_type(), ov::Shape{}, {-1.0f});
auto paired_shape = ov::op::v0::Constant::create(
ov::element::i64, {5}, std::vector<int64_t>{1, -1, n_heads, half, 2});
ov::element::i64, {5}, std::vector<int64_t>{batch, -1, n_heads, half, 2});
auto x_paired = std::make_shared<ov::op::v1::Reshape>(rot_data, paired_shape, false);
auto split_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {-1});
@@ -124,7 +129,7 @@ OutputVector translate_rope(const NodeContext & context) {
auto x_rotated_paired = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{x1_neg, x0}, -1);
auto flat_shape =
ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{1, -1, n_heads, n_dims});
ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{batch, -1, n_heads, n_dims});
auto x_rotated =
std::make_shared<ov::op::v1::Reshape>(x_rotated_paired, flat_shape, false);
@@ -167,10 +172,13 @@ OutputVector translate_rope(const NodeContext & context) {
} else {
res = std::make_shared<ov::op::v0::Concat>(concat_parts, -1);
}
} else if (mode == TYPE_NEOX || mode == TYPE_IMROPE) {
if (mode == TYPE_IMROPE) {
} else if (mode == TYPE_NEOX || mode == TYPE_IMROPE || mode == TYPE_MROPE || mode == TYPE_VISION) {
const int64_t half = (mode == TYPE_VISION) ? n_dims : (n_dims / 2);
const int64_t rot_dims = 2 * half;
if (mode != TYPE_NEOX) {
auto cos_sin_shape = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4},
std::vector<int64_t>{1, -1, 1, (n_dims >> 1)});
std::vector<int64_t>{1, -1, 1, half});
cos_theta_node = std::make_shared<ov::op::v1::Reshape>(cos_theta_node, cos_sin_shape, true);
sin_theta_node = std::make_shared<ov::op::v1::Reshape>(sin_theta_node, cos_sin_shape, true);
}
@@ -179,13 +187,12 @@ OutputVector translate_rope(const NodeContext & context) {
auto step_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
Output<Node> rot_data = data_node;
if (n_offs > 0 || n_offs + n_dims < head_dim) {
if (n_offs > 0 || n_offs + rot_dims < head_dim) {
auto rot_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
auto rot_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + n_dims});
auto rot_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + rot_dims});
rot_data = std::make_shared<ov::op::v8::Slice>(data_node, rot_start, rot_end, step_one, axis_last);
}
const int64_t half = n_dims / 2;
auto neg_one_f = ov::op::v0::Constant::create(data_node->get_element_type(), ov::Shape{}, {-1.0f});
auto split_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {3});
@@ -212,8 +219,8 @@ OutputVector translate_rope(const NodeContext & context) {
concat_parts.push_back(head);
}
concat_parts.push_back(rotated);
if (n_offs + n_dims < head_dim) {
auto tail_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + n_dims});
if (n_offs + rot_dims < head_dim) {
auto tail_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + rot_dims});
auto tail_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {head_dim});
auto tail = std::make_shared<ov::op::v8::Slice>(data_node, tail_start, tail_end, step_one, axis_last);
concat_parts.push_back(tail);
@@ -37,6 +37,10 @@ OutputVector translate_scale(const NodeContext & context) {
auto scale_node = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{}, std::vector<float>{scale});
if (context.get_op_case() == 2) {
return {context.get_input(0)};
}
if (context.get_op_case() == 1 && context.has_input("cache_rs_reset_len")) {
auto cache_rs_reset_idx = context.get_input("cache_rs_reset_idx");
auto cache_rs_reset_len = context.get_input("cache_rs_reset_len");
@@ -0,0 +1,30 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include <memory>
#include <openvino/op/constant.hpp>
#include <openvino/op/range.hpp>
#include <openvino/op/reduce_sum.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/shape_of.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_sum(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {0, 1, 2, 3});
auto res = std::make_shared<ov::op::v1::ReduceSum>(input, axes, true);
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -0,0 +1,138 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include "ggml-openvino/ggml-openvino-extra.h"
#include <memory>
#include <openvino/op/abs.hpp>
#include <openvino/op/add.hpp>
#include <openvino/op/clamp.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/elu.hpp>
#include <openvino/op/exp.hpp>
#include <openvino/op/gelu.hpp>
#include <openvino/op/greater.hpp>
#include <openvino/op/hard_sigmoid.hpp>
#include <openvino/op/log.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/negative.hpp>
#include <openvino/op/relu.hpp>
#include <openvino/op/round.hpp>
#include <openvino/op/sigmoid.hpp>
#include <openvino/op/softplus.hpp>
#include <openvino/op/subtract.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_unary_gelu(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto res = std::make_shared<ov::op::v7::Gelu>(input, ov::op::GeluApproximationMode::TANH);
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_gelu_erf(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto res = std::make_shared<ov::op::v7::Gelu>(input, ov::op::GeluApproximationMode::ERF);
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_gelu_quick(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto scale = ov::op::v0::Constant::create(input.get_element_type(), ov::Shape{}, {1.702f});
auto mul = std::make_shared<ov::op::v1::Multiply>(input, scale);
auto sig = std::make_shared<ov::op::v0::Sigmoid>(mul);
auto res = std::make_shared<ov::op::v1::Multiply>(input, sig);
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_elu(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto res = std::make_shared<ov::op::v0::Elu>(input, 1.0);
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_hardsigmoid(const NodeContext & context) {
num_inputs_check(context, 1, 1);
// compute in f32 like the ggml reference: 1/6 is not exact in f16/bf16 (NPU cannot take the f32 path)
auto input = process_view_input_new(context, 0);
const auto type = ggml_openvino_is_npu() ? input.get_element_type() : ov::element::f32;
ov::Output<ov::Node> x = input;
if (type != input.get_element_type()) {
x = std::make_shared<ov::op::v0::Convert>(input, type);
}
auto alpha = ov::op::v0::Constant::create(type, ov::Shape{}, {1.0f / 6.0f});
auto beta = ov::op::v0::Constant::create(type, ov::Shape{}, {0.5f});
ov::Output<ov::Node> res = std::make_shared<ov::op::v0::HardSigmoid>(x, alpha, beta);
if (type != input.get_element_type()) {
res = std::make_shared<ov::op::v0::Convert>(res, input.get_element_type());
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_step(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto zero = ov::op::v0::Constant::create(input.get_element_type(), ov::Shape{}, {0.0f});
auto cond = std::make_shared<ov::op::v1::Greater>(input, zero);
auto res = std::make_shared<ov::op::v0::Convert>(cond, input.get_element_type());
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_round(const NodeContext & context) {
num_inputs_check(context, 1, 1);
auto input = process_view_input_new(context, 0);
auto res = std::make_shared<ov::op::v5::Round>(input, ov::op::v5::Round::RoundMode::HALF_AWAY_FROM_ZERO);
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_expm1(const NodeContext & context) {
num_inputs_check(context, 1, 1);
// compute in f32 like the ggml reference: exp(x) - 1 in f16 loses the small-x digits (NPU cannot take the f32 path)
auto input = process_view_input_new(context, 0);
const auto type = ggml_openvino_is_npu() ? input.get_element_type() : ov::element::f32;
ov::Output<ov::Node> x = input;
if (type != input.get_element_type()) {
x = std::make_shared<ov::op::v0::Convert>(input, type);
}
auto exp = std::make_shared<ov::op::v0::Exp>(x);
auto one = ov::op::v0::Constant::create(type, ov::Shape{}, {1.0f});
ov::Output<ov::Node> res = std::make_shared<ov::op::v1::Subtract>(exp, one);
if (type != input.get_element_type()) {
res = std::make_shared<ov::op::v0::Convert>(res, input.get_element_type());
}
return rename_outputs_with_suffix({res}, context.get_name());
}
OutputVector translate_unary_softplus(const NodeContext & context) {
num_inputs_check(context, 1, 1);
if (ggml_openvino_getenv_int("GGML_OPENVINO_NATIVE_SOFTPLUS") != 0) {
return translate_1to1_match_1_input<ov::op::v4::SoftPlus>(context);
}
auto input = process_view_input_new(context, 0);
const auto element_type = input.get_element_type();
auto one = ov::op::v0::Constant::create(element_type, ov::Shape{}, {1.0f});
auto positive = std::make_shared<ov::op::v0::Relu>(input);
auto abs = std::make_shared<ov::op::v0::Abs>(input);
auto neg_abs = std::make_shared<ov::op::v0::Negative>(abs);
auto exp_neg_abs = std::make_shared<ov::op::v0::Exp>(neg_abs);
auto log_term = std::make_shared<ov::op::v0::Log>(std::make_shared<ov::op::v1::Add>(one, exp_neg_abs));
auto res = std::make_shared<ov::op::v1::Add>(positive, log_term);
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -1,44 +0,0 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include "ggml-openvino/ggml-openvino-extra.h"
#include <openvino/op/abs.hpp>
#include <openvino/op/add.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/exp.hpp>
#include <openvino/op/log.hpp>
#include <openvino/op/negative.hpp>
#include <openvino/op/relu.hpp>
#include <openvino/op/softplus.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_unary_softplus(const NodeContext & context) {
num_inputs_check(context, 1, 1);
if (ggml_openvino_getenv_int("GGML_OPENVINO_NATIVE_SOFTPLUS") != 0) {
return translate_1to1_match_1_input<ov::op::v4::SoftPlus>(context);
}
auto input = process_view_input_new(context, 0);
const auto element_type = input.get_element_type();
auto one = ov::op::v0::Constant::create(element_type, ov::Shape{}, {1.0f});
auto positive = std::make_shared<ov::op::v0::Relu>(input);
auto abs = std::make_shared<ov::op::v0::Abs>(input);
auto neg_abs = std::make_shared<ov::op::v0::Negative>(abs);
auto exp_neg_abs = std::make_shared<ov::op::v0::Exp>(neg_abs);
auto log_term = std::make_shared<ov::op::v0::Log>(std::make_shared<ov::op::v1::Add>(one, exp_neg_abs));
auto res = std::make_shared<ov::op::v1::Add>(positive, log_term);
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -0,0 +1,181 @@
#include "../node_context.h"
#include "../op_table.h"
#include "../utils.h"
#include "ggml.h"
#include <cmath>
#include <cstddef>
#include <memory>
#include <openvino/op/constant.hpp>
#include <openvino/op/gather.hpp>
#include <openvino/op/interpolate.hpp>
#include <openvino/op/matmul.hpp>
#include <vector>
namespace ov {
namespace frontend {
namespace ggml {
namespace op {
OutputVector translate_upscale(const NodeContext & context) {
num_inputs_check(context, 1, 1);
using Interpolate = ov::op::v4::Interpolate;
auto input = process_view_input_new(context, 0);
const auto input_shape = context.get_input_shape(0).to_shape();
const auto output_shape = context.get_output_shape().to_shape();
if (input_shape == output_shape) {
return rename_outputs_with_suffix({input}, context.get_name());
}
ov::Output<ov::Node> res = input;
// Resample batch / channel dimensions (ne[3] and ne[2], corresponding to OV axes 0 and 1)
// using nearest-neighbor index mapping: i0_d = floor(i_d * in_d / out_d).
for (size_t axis = 0; axis < 2; ++axis) {
const size_t in_dim = input_shape[axis];
const size_t out_dim = output_shape[axis];
if (in_dim != out_dim) {
std::vector<int64_t> indices(out_dim);
for (size_t i = 0; i < out_dim; ++i) {
indices[i] = static_cast<int64_t>((i * in_dim) / out_dim);
}
auto indices_node = ov::op::v0::Constant::create(ov::element::i64, {indices.size()}, indices);
auto axis_node = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {axis});
res = std::make_shared<ov::op::v8::Gather>(res, indices_node, axis_node);
}
}
// Spatial interpolation for ne[1] and ne[0] (OV axes 2 and 3)
if (input_shape[2] != output_shape[2] || input_shape[3] != output_shape[3]) {
const int32_t * op_params = context.get_output_op_params();
const int32_t mode_flags = op_params != nullptr ? op_params[0] : 0;
const int op_case = context.get_op_case();
const bool align_corners = (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) != 0;
const bool antialias = (mode_flags & GGML_SCALE_FLAG_ANTIALIAS) != 0;
if (op_case == 2 && antialias) { // GGML_SCALE_MODE_BILINEAR with antialias
const size_t H_in = input_shape[2];
const size_t W_in = input_shape[3];
const size_t H_out = output_shape[2];
const size_t W_out = output_shape[3];
const float pixel_offset = 0.5f;
// Height projection: [H_out, H_in]
std::vector<float> Wy_data(H_out * H_in, 0.0f);
const float sf1 = static_cast<float>(H_out) / H_in;
const float support1 = std::max(1.0f, 1.0f / sf1);
const float invscale1 = 1.0f / support1;
for (size_t i1 = 0; i1 < H_out; ++i1) {
const float y = (static_cast<float>(i1) + pixel_offset) / sf1;
const auto y_start = static_cast<int64_t>(y - support1 + pixel_offset);
const size_t y_min = y_start > 0 ? static_cast<size_t>(y_start) : 0;
const auto y_end = static_cast<int64_t>(y + support1 + pixel_offset);
const size_t y_max = y_end > 0 ? std::min<size_t>(static_cast<size_t>(y_end), H_in) : 0;
float total_weight = 0.0f;
for (size_t sy = y_min; sy < y_max; ++sy) {
float diff = std::abs((static_cast<float>(sy) - y + pixel_offset) * invscale1);
float weight = std::max(1.0f - diff, 0.0f);
Wy_data[i1 * H_in + sy] = weight;
total_weight += weight;
}
if (total_weight > 0.0f) {
for (size_t sy = y_min; sy < y_max; ++sy) {
Wy_data[i1 * H_in + sy] /= total_weight;
}
}
}
// Width projection: [W_in, W_out]
std::vector<float> Wx_data(W_in * W_out, 0.0f);
const float sf0 = static_cast<float>(W_out) / W_in;
const float support0 = std::max(1.0f, 1.0f / sf0);
const float invscale0 = 1.0f / support0;
for (size_t i0 = 0; i0 < W_out; ++i0) {
const float x = (static_cast<float>(i0) + pixel_offset) / sf0;
const auto x_start = static_cast<int64_t>(x - support0 + pixel_offset);
const size_t x_min = x_start > 0 ? static_cast<size_t>(x_start) : 0;
const auto x_end = static_cast<int64_t>(x + support0 + pixel_offset);
const size_t x_max = x_end > 0 ? std::min<size_t>(static_cast<size_t>(x_end), W_in) : 0;
float total_weight = 0.0f;
for (size_t sx = x_min; sx < x_max; ++sx) {
float diff = std::abs((static_cast<float>(sx) - x + pixel_offset) * invscale0);
float weight = std::max(1.0f - diff, 0.0f);
Wx_data[sx * W_out + i0] = weight;
total_weight += weight;
}
if (total_weight > 0.0f) {
for (size_t sx = x_min; sx < x_max; ++sx) {
Wx_data[sx * W_out + i0] /= total_weight;
}
}
}
auto Wy_node = ov::op::v0::Constant::create(ov::element::f32, {H_out, H_in}, Wy_data);
auto Wx_node = ov::op::v0::Constant::create(ov::element::f32, {W_in, W_out}, Wx_data);
auto res_y = std::make_shared<ov::op::v0::MatMul>(Wy_node, res);
res = std::make_shared<ov::op::v0::MatMul>(res_y, Wx_node);
} else {
Interpolate::InterpolateAttrs attrs;
attrs.shape_calculation_mode = Interpolate::ShapeCalcMode::SIZES;
attrs.antialias = antialias;
attrs.pads_begin = {0, 0, 0, 0};
attrs.pads_end = {0, 0, 0, 0};
switch (op_case) {
case 1: // GGML_SCALE_MODE_NEAREST
attrs.mode = Interpolate::InterpolateMode::NEAREST;
attrs.nearest_mode = Interpolate::NearestMode::FLOOR;
attrs.coordinate_transformation_mode = Interpolate::CoordinateTransformMode::ASYMMETRIC;
break;
case 2: // GGML_SCALE_MODE_BILINEAR
attrs.mode = Interpolate::InterpolateMode::LINEAR;
attrs.coordinate_transformation_mode = align_corners
? Interpolate::CoordinateTransformMode::ALIGN_CORNERS
: Interpolate::CoordinateTransformMode::HALF_PIXEL;
break;
case 3: // GGML_SCALE_MODE_BICUBIC
attrs.mode = Interpolate::InterpolateMode::CUBIC;
attrs.cube_coeff = -0.75;
attrs.coordinate_transformation_mode = align_corners
? Interpolate::CoordinateTransformMode::ALIGN_CORNERS
: Interpolate::CoordinateTransformMode::HALF_PIXEL;
break;
default:
FRONT_END_OP_CONVERSION_CHECK(false, "Unsupported upscale op_case: ", op_case);
}
std::vector<int64_t> target_shape_vec = {
static_cast<int64_t>(output_shape[2]),
static_cast<int64_t>(output_shape[3]),
};
std::vector<float> scales_vec = {
static_cast<float>(output_shape[2]) / static_cast<float>(input_shape[2]),
static_cast<float>(output_shape[3]) / static_cast<float>(input_shape[3]),
};
auto target_shape_node = ov::op::v0::Constant::create(ov::element::i64, {2}, target_shape_vec);
auto scales_node = ov::op::v0::Constant::create(ov::element::f32, {2}, scales_vec);
auto axes_node = ov::op::v0::Constant::create(ov::element::i64, {2}, {2, 3});
res = std::make_shared<Interpolate>(
res, target_shape_node, scales_node, axes_node, attrs);
}
}
return rename_outputs_with_suffix({res}, context.get_name());
}
} // namespace op
} // namespace ggml
} // namespace frontend
} // namespace ov
+23 -9
View File
@@ -44,7 +44,18 @@ OutputVector translate_view(const NodeContext & context) {
auto ss = src_ps.to_shape();
auto dd = dst_ps.to_shape();
const size_t nd = ss.size();
if (sst.size() == nd && dst.size() == nd) {
// Stateful models drop the leading size-1 batch dim, so the real OV tensor can
// be one rank lower than the ggml shape metadata above. Axis indices derived
// from that metadata must be shifted down by the difference before they are
// used as OV axes. Bail out if the dims we would drop are not all 1.
const auto in_rank = context.get_input(0).get_partial_shape().rank();
const int axis_shift =
in_rank.is_static() ? (int) nd - (int) in_rank.get_length() : 0;
bool shift_ok = axis_shift >= 0 && (size_t) axis_shift < nd;
for (int a = 0; a < axis_shift && shift_ok; ++a) {
shift_ok = (ss[a] == 1 && dd[a] == 1);
}
if (shift_ok && sst.size() == nd && dst.size() == nd) {
// Map each dst axis of size>1 to a src axis with equal (size,stride);
// the unmatched src axis of size>1 is the indexed expert axis.
// dst_to_src[d] records which src axis each dst axis came from, so we can
@@ -93,7 +104,8 @@ OutputVector translate_view(const NodeContext & context) {
ov::op::v0::Constant::create(ov::element::i64, {1}, {sel}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {sel + 1}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {1}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {dropped}));
ov::op::v0::Constant::create(ov::element::i64, {1},
{dropped - axis_shift}));
// Build the reshape target from the (concrete) dst shape, but
// keep the dynamic token axis dynamic instead of freezing it
// to the captured n_tokens. Without this the constant dst
@@ -106,20 +118,22 @@ OutputVector translate_view(const NodeContext & context) {
// dynamic dim from the correct SOURCE axis via ShapeOf+Gather
// and place it at the dst token position.
const int32_t dyn = context.get_op_dynamic_dim(); // output ggml axis, -1 if none
int dst_ov_axis = (dyn != -1) ? (3 - (int) dyn) : -1; // get_shape() reverses ggml order
int src_ov_axis = (dst_ov_axis >= 0 && dst_ov_axis < (int) nd)
// still in ggml metadata axis space; get_shape() reverses ggml order
int dst_ov_axis = (dyn != -1) ? ((int) nd - 1 - (int) dyn) : -1;
int src_ov_axis = (dst_ov_axis >= axis_shift && dst_ov_axis < (int) nd)
? dst_to_src[dst_ov_axis]
: -1;
if (dst_ov_axis >= 0 && src_ov_axis >= 0) {
if (dst_ov_axis >= 0 && src_ov_axis >= axis_shift) {
// target = concat of per-axis scalars; the token axis is a
// runtime Gather of the slice's shape, the rest are constants.
auto sl_shape = std::make_shared<ov::op::v3::ShapeOf>(sl, ov::element::i64);
auto tok_dim = std::make_shared<ov::op::v8::Gather>(
sl_shape,
ov::op::v0::Constant::create(ov::element::i64, {1}, {src_ov_axis}),
ov::op::v0::Constant::create(ov::element::i64, {1},
{src_ov_axis - axis_shift}),
ov::op::v0::Constant::create(ov::element::i64, {}, {0}));
ov::OutputVector parts;
for (int a = 0; a < (int) nd; ++a) {
for (int a = axis_shift; a < (int) nd; ++a) {
if (a == dst_ov_axis) {
parts.push_back(tok_dim);
} else {
@@ -131,8 +145,8 @@ OutputVector translate_view(const NodeContext & context) {
auto rs = std::make_shared<ov::op::v1::Reshape>(sl, dc, false);
return rename_outputs_with_suffix({rs}, context.get_name());
}
auto dc = ov::op::v0::Constant::create(
ov::element::i64, {nd}, std::vector<int64_t>(dd.begin(), dd.end()));
std::vector<int64_t> dd_ov(dd.begin() + axis_shift, dd.end());
auto dc = ov::op::v0::Constant::create(ov::element::i64, {dd_ov.size()}, dd_ov);
auto rs = std::make_shared<ov::op::v1::Reshape>(sl, dc, false);
return rename_outputs_with_suffix({rs}, context.get_name());
}
+35 -2
View File
@@ -2,16 +2,25 @@
#include "utils.h"
#include <openvino/op/abs.hpp>
#include <openvino/op/add.hpp>
#include <openvino/op/ceiling.hpp>
#include <openvino/op/cos.hpp>
#include <openvino/op/divide.hpp>
#include <openvino/op/exp.hpp>
#include <openvino/op/floor.hpp>
#include <openvino/op/gather.hpp>
#include <openvino/op/gelu.hpp>
#include <openvino/op/hswish.hpp>
#include <openvino/op/log.hpp>
#include <openvino/op/matmul.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/negative.hpp>
#include <openvino/op/relu.hpp>
#include <openvino/op/sigmoid.hpp>
#include <openvino/op/sign.hpp>
#include <openvino/op/sin.hpp>
#include <openvino/op/softplus.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/op/swish.hpp>
#include <openvino/op/tanh.hpp>
@@ -32,6 +41,7 @@ std::unordered_map<std::string, CreatorFunction> get_supported_ops() {
{"GGML_OP_FILL", op::translate_fill },
{"GGML_OP_GET_ROWS", op::translate_get_rows },
{"GGML_OP_IM2COL", op::translate_im2col },
{"GGML_OP_IM2COL_3D", op::translate_im2col_3d },
{"GGML_OP_MUL", op::translate_1to1_match_2_inputs<v1::Multiply>},
{"GGML_OP_MUL_MAT", op::translate_mulmat },
{"GGML_OP_MUL_MAT_ID", op::translate_mul_mat_id },
@@ -49,7 +59,24 @@ std::unordered_map<std::string, CreatorFunction> get_supported_ops() {
{"GGML_OP_ARGSORT", op::translate_argsort },
{"GGML_OP_SUB", op::translate_1to1_match_2_inputs<v1::Subtract>},
{"GGML_OP_TRANSPOSE", op::translate_transpose },
{"GGML_UNARY_OP_GELU", op::translate_1to1_match_1_input<v7::Gelu> },
{"GGML_OP_SIN", op::translate_1to1_match_1_input<v0::Sin> },
{"GGML_OP_COS", op::translate_1to1_match_1_input<v0::Cos> },
{"GGML_OP_LOG", op::translate_1to1_match_1_input<v0::Log> },
{"GGML_OP_MEAN", op::translate_mean },
{"GGML_OP_SUM", op::translate_sum },
{"GGML_UNARY_OP_GELU", op::translate_unary_gelu },
{"GGML_UNARY_OP_GELU_ERF", op::translate_unary_gelu_erf },
{"GGML_UNARY_OP_GELU_QUICK", op::translate_unary_gelu_quick },
{"GGML_UNARY_OP_ELU", op::translate_unary_elu },
{"GGML_UNARY_OP_HARDSWISH", op::translate_1to1_match_1_input<v4::HSwish> },
{"GGML_UNARY_OP_HARDSIGMOID", op::translate_unary_hardsigmoid },
{"GGML_UNARY_OP_STEP", op::translate_unary_step },
{"GGML_UNARY_OP_ABS", op::translate_1to1_match_1_input<v0::Abs> },
{"GGML_UNARY_OP_SGN", op::translate_1to1_match_1_input<v0::Sign> },
{"GGML_UNARY_OP_FLOOR", op::translate_1to1_match_1_input<v0::Floor> },
{"GGML_UNARY_OP_CEIL", op::translate_1to1_match_1_input<v0::Ceiling> },
{"GGML_UNARY_OP_ROUND", op::translate_unary_round },
{"GGML_UNARY_OP_EXPM1", op::translate_unary_expm1 },
{"GGML_UNARY_OP_SIGMOID", op::translate_1to1_match_1_input<v0::Sigmoid> },
{"GGML_UNARY_OP_SILU", op::translate_1to1_match_1_input<v4::Swish> },
{"GGML_UNARY_OP_SOFTPLUS", op::translate_unary_softplus },
@@ -60,7 +87,7 @@ std::unordered_map<std::string, CreatorFunction> get_supported_ops() {
{"GGML_OP_VIEW", op::translate_view },
{"GGML_GLU_OP_SWIGLU", op::translate_glu_swiglu },
{"GGML_GLU_OP_SWIGLU_OAI", op::translate_glu_swiglu_oai },
{"GGML_GLU_OP_SWIGLU_CLAMP", op::translate_glu_swiglu_clamp },
{"GGML_GLU_OP_SWIGLU_CLAMP", op::translate_glu_swiglu_clamp },
{"GGML_GLU_OP_GEGLU", op::translate_glu_geglu },
{"GGML_GLU_OP_GEGLU_QUICK", op::translate_glu_geglu_quick },
{"GGML_OP_SET_ROWS", op::translate_set_rows },
@@ -78,6 +105,12 @@ std::unordered_map<std::string, CreatorFunction> get_supported_ops() {
{"GGML_OP_SET", op::translate_set },
{"GGML_OP_POOL_2D", op::translate_pool_2d },
{"GGML_OP_ROLL", op::translate_roll },
{"GGML_OP_UPSCALE", op::translate_upscale },
{"GGML_OP_CONV_2D", op::translate_conv_2d },
{"GGML_OP_CONV_2D_DW", op::translate_conv_2d_dw },
{"GGML_OP_CONV_TRANSPOSE_1D", op::translate_conv_transpose_1d },
{"GGML_OP_CONV_TRANSPOSE_2D", op::translate_conv_transpose_2d },
{"GGML_OP_CONV_3D", op::translate_conv_3d },
// solve_tri has accuracy issues on GPU
// {"GGML_OP_SOLVE_TRI", op::translate_solve_tri },
};
@@ -18,6 +18,7 @@ GGML_OP_CONVERTER(translate_div);
GGML_OP_CONVERTER(translate_fill);
GGML_OP_CONVERTER(translate_get_rows);
GGML_OP_CONVERTER(translate_im2col);
GGML_OP_CONVERTER(translate_im2col_3d);
GGML_OP_CONVERTER(translate_mulmat);
GGML_OP_CONVERTER(translate_mul_mat_id);
GGML_OP_CONVERTER(translate_permute);
@@ -56,6 +57,22 @@ GGML_OP_CONVERTER(translate_tri);
GGML_OP_CONVERTER(translate_solve_tri);
GGML_OP_CONVERTER(translate_pool_2d);
GGML_OP_CONVERTER(translate_roll);
GGML_OP_CONVERTER(translate_upscale);
GGML_OP_CONVERTER(translate_mean);
GGML_OP_CONVERTER(translate_sum);
GGML_OP_CONVERTER(translate_unary_gelu);
GGML_OP_CONVERTER(translate_unary_gelu_erf);
GGML_OP_CONVERTER(translate_unary_gelu_quick);
GGML_OP_CONVERTER(translate_unary_elu);
GGML_OP_CONVERTER(translate_unary_hardsigmoid);
GGML_OP_CONVERTER(translate_unary_step);
GGML_OP_CONVERTER(translate_unary_round);
GGML_OP_CONVERTER(translate_unary_expm1);
GGML_OP_CONVERTER(translate_conv_2d);
GGML_OP_CONVERTER(translate_conv_2d_dw);
GGML_OP_CONVERTER(translate_conv_transpose_1d);
GGML_OP_CONVERTER(translate_conv_transpose_2d);
GGML_OP_CONVERTER(translate_conv_3d);
} // namespace op
@@ -0,0 +1,70 @@
#include "fuse_argsort_topk.h"
#include <openvino/op/constant.hpp>
#include <openvino/op/slice.hpp>
#include <openvino/op/topk.hpp>
#include <openvino/pass/pattern/op/wrap_type.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace pass {
FuseArgsortTopK::FuseArgsortTopK() {
// GGML argsort materializes the full ordering before a VIEW keeps the prefix.
// Let TopK produce only that prefix when all index consumers are such views.
auto pattern = ov::pass::pattern::wrap_type<ov::op::v11::TopK>();
const auto callback = [](ov::pass::pattern::Matcher & m) {
auto topk = ov::as_type_ptr<ov::op::v11::TopK>(m.get_match_root());
if (!topk->output(0).get_target_inputs().empty() || topk->output(1).get_target_inputs().empty() ||
topk->get_sort_type() != ov::op::v11::TopK::SortType::SORT_VALUES) {
return false;
}
const auto shape = topk->get_output_partial_shape(1);
if (shape.rank().is_dynamic()) {
return false;
}
const int64_t rank = shape.rank().get_length();
const int64_t axis = topk->get_axis();
if (shape[axis].is_dynamic()) {
return false;
}
int64_t prefix = -1;
for (const auto & input : topk->output(1).get_target_inputs()) {
const auto * slice = ov::as_type<ov::op::v8::Slice>(input.get_node());
if (!slice || input.get_index() != 0 || slice->get_input_size() != 5) {
return false;
}
int64_t values[4];
for (size_t i = 0; i < 4; ++i) {
auto value = ov::as_type_ptr<ov::op::v0::Constant>(slice->get_input_node_shared_ptr(i + 1));
if (!value || ov::shape_size(value->get_shape()) != 1) {
return false;
}
values[i] = value->cast_vector<int64_t>()[0];
}
const int64_t slice_axis = values[3] < 0 ? values[3] + rank : values[3];
if (values[0] != 0 || values[2] != 1 || slice_axis != axis || values[1] <= 0 ||
values[1] >= shape[axis].get_length() || (prefix != -1 && prefix != values[1])) {
return false;
}
prefix = values[1];
}
// ggml_argsort_top_k sorts the full row, then exposes only its first k indices.
// Keep other TopK forms unchanged because their ordering can be observable.
topk->set_argument(1, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {prefix}));
topk->validate_and_infer_types();
return true;
};
register_matcher(std::make_shared<ov::pass::pattern::Matcher>(pattern, "ov::frontend::ggml::pass::FuseArgsortTopK"), callback);
}
} // namespace pass
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -0,0 +1,19 @@
#pragma once
#include <openvino/pass/matcher_pass.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace pass {
class FuseArgsortTopK : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("ov::frontend::ggml::pass::FuseArgsortTopK")
FuseArgsortTopK();
};
} // namespace pass
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -1,5 +1,6 @@
#include "fuse_moe_compressed.h"
#include <cstring>
#include <limits>
#include <set>
#include <memory>
@@ -7,9 +8,11 @@
#include <openvino/core/rt_info.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/gelu.hpp>
#include <openvino/op/multiply.hpp>
#include <openvino/op/reduce_sum.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/slice.hpp>
#include <openvino/op/squeeze.hpp>
#include <openvino/op/subtract.hpp>
#include <openvino/op/swish.hpp>
@@ -84,6 +87,44 @@ size_t logical_k(const ov::Shape & shape) {
return shape.size() == 4 ? shape[2] * shape[3] : shape.back();
}
// Slice a [n_expert, m, ...] compressed weight/scale/zp Constant along axis 1 (m), [begin, end).
// Built by copying raw bytes directly instead of a graph Slice op: CommonOptimizations rewrites
// v8::Slice into v1::StridedSlice for constant folding regardless of disable_constant_folding
// (the rewrite doesn't carry the marking over), and that reference evaluator crashes on
// sub-byte (u4/i4) element types. Each axis-1 "row" (the trailing dims) is confirmed
// byte-aligned here -- weight/scale/zp trailing sizes are always whole groups -- so a
// byte-range memcpy per row is exact for both regular and sub-byte element types.
ov::Output<ov::Node> slice_experts_dim1(const ov::Output<ov::Node> & tensor, int64_t begin, int64_t end) {
auto constant = ov::as_type_ptr<ov::op::v0::Constant>(tensor.get_node_shared_ptr());
OPENVINO_ASSERT(constant, "slice_experts_dim1 expects a Constant input");
const auto & shape = constant->get_shape();
OPENVINO_ASSERT(shape.size() >= 2, "slice_experts_dim1 expects rank >= 2");
const auto & type = constant->get_element_type();
size_t row_elems = 1;
for (size_t i = 2; i < shape.size(); ++i) {
row_elems *= shape[i];
}
const size_t row_bits = row_elems * type.bitwidth();
OPENVINO_ASSERT(row_bits % 8 == 0, "slice_experts_dim1: row is not byte-aligned");
const size_t row_bytes = row_bits / 8;
ov::Shape out_shape = shape;
out_shape[1] = static_cast<size_t>(end - begin);
std::vector<uint8_t> out_data(shape[0] * static_cast<size_t>(end - begin) * row_bytes);
const auto * src = static_cast<const uint8_t *>(constant->get_data_ptr());
const size_t src_row_bytes = shape[1] * row_bytes;
for (size_t e = 0; e < shape[0]; ++e) {
std::memcpy(out_data.data() + e * static_cast<size_t>(end - begin) * row_bytes,
src + e * src_row_bytes + static_cast<size_t>(begin) * row_bytes,
static_cast<size_t>(end - begin) * row_bytes);
}
return std::make_shared<ov::op::v0::Constant>(type, out_shape, out_data.data());
}
} // namespace
FuseMoeCompressed::FuseMoeCompressed() {
@@ -267,6 +308,208 @@ FuseMoeCompressed::FuseMoeCompressed() {
register_matcher(std::make_shared<Matcher>(root_m, "ov::frontend::ggml::pass::FuseMoeCompressed"), callback);
}
FuseMoeCompressedFusedGateUp::FuseMoeCompressedFusedGateUp() {
using namespace ov::pass::pattern;
// A single GatherMatmul computes the fused gate+up projection; the gate/up split happens
// AFTER the GEMM via two Slice ops on the last (feature) axis (see get_glu_inputs in
// op/glu_geglu.cpp), unlike FuseMoeCompressed's two-separate-GatherMatmul models.
auto hidden_m = any_input();
auto a_reshape_m = wrap_type<ov::op::v1::Reshape>({ hidden_m, any_input() });
auto a_m = wrap_type<ov::op::v1::Transpose>({ optional<ov::op::v0::Convert>({ a_reshape_m }), any_input() });
auto gate_up_w_m = any_input();
auto ids_gate_up_m = any_input();
auto bgm_fused_m = wrap_type<ov::op::internal::GatherMatmul>({ a_m, gate_up_w_m, ids_gate_up_m, any_input() });
auto gu_u_m = optional<ov::op::v0::Convert>({ wrap_type<ov::op::v0::Unsqueeze>(
{ wrap_type<ov::op::v1::Transpose>({ bgm_fused_m, any_input() }), any_input() }) });
auto gate_slice_m = wrap_type<ov::op::v8::Slice>({ gu_u_m, any_input(), any_input(), any_input(), any_input() });
auto up_slice_m = wrap_type<ov::op::v8::Slice>({ gu_u_m, any_input(), any_input(), any_input(), any_input() });
auto gelu_m = wrap_type<ov::op::v7::Gelu>({ gate_slice_m });
auto geglu_m = wrap_type<ov::op::v1::Multiply>({ gelu_m, up_slice_m });
auto d_t_m = wrap_type<ov::op::v1::Transpose>(
{ optional<ov::op::v0::Convert>({ wrap_type<ov::op::v1::Reshape>({ geglu_m, any_input() }) }),
any_input() });
auto down_w_m = any_input();
auto ids_down_m = any_input();
auto bgm_down_m = wrap_type<ov::op::internal::GatherMatmul>({ d_t_m, down_w_m, ids_down_m, any_input() });
auto down_u_m = optional<ov::op::v0::Convert>({ wrap_type<ov::op::v0::Unsqueeze>(
{ wrap_type<ov::op::v1::Transpose>({ bgm_down_m, any_input() }), any_input() }) });
// gemma-4 applies an extra per-expert output scale to the down projection before the
// router-weight multiply (llama-graph.cpp's ffn_down_exps.scale); FuseMoeCompressed's
// Qwen-shaped models have no such scale, so this node is specific to this pattern.
auto down_scale_m = any_input();
auto down_scaled_m = wrap_type<ov::op::v1::Multiply>({ down_u_m, down_scale_m });
auto routing_m = any_input();
auto weighted_m = wrap_type<ov::op::v1::Multiply>({ down_scaled_m, routing_m });
auto root_m = wrap_type<ov::op::v1::ReduceSum>({ weighted_m, any_input() });
const auto callback = [=](Matcher & m) {
auto & pm = m.get_pattern_value_map();
const auto gate_up = unwrap_dequant(pm.at(gate_up_w_m));
const auto down = unwrap_dequant(pm.at(down_w_m));
if (!gate_up.ok || !down.ok) {
return false;
}
// oneDNN's weight-decompression GEMM needs a real zero point, so a symmetric (no zp)
// projection cannot be fused here -- e.g. the GPU-only Q4_0 fallback in
// ggml_openvino_get_extracted_layout for grouped 8-bit expert weights.
if (!gate_up.has_zp || !down.has_zp) {
return false;
}
const auto gate_up_shape = gate_up.weight.get_shape();
const auto down_shape = down.weight.get_shape();
if (gate_up_shape.size() < 3 || down_shape.size() < 3 || gate_up_shape.size() != down_shape.size()) {
return false;
}
// The op reads the zero point straight off a weight port, so it must already be an
// integer Constant. Requantized experts (GGML_OPENVINO_REQUANT_KQUANT=q4_asym64_all)
// are; a native Q4_K expert keeps an exact f16 zp and is left to the unfused path
// rather than rounded here -- rounding would put a live node on that port and the
// expert GEMM would read the wrong zero point.
static const std::set<ov::element::Type> int_types = { ov::element::u4, ov::element::i4,
ov::element::u8, ov::element::i8 };
if (int_types.count(gate_up.weight.get_element_type()) == 0 ||
int_types.count(down.weight.get_element_type()) == 0 ||
int_types.count(gate_up.zp.get_element_type()) == 0 ||
int_types.count(down.zp.get_element_type()) == 0) {
return false;
}
const auto group_of = [](const dequant_inputs & w) {
const auto s = w.weight.get_shape();
return s.size() == 4 ? s[3] : logical_k(s);
};
if (group_of(gate_up) != group_of(down)) {
return false;
}
auto gelu_node = ov::as_type_ptr<ov::op::v7::Gelu>(pm.at(gelu_m).get_node_shared_ptr());
if (!gelu_node || gelu_node->get_approximation_mode() != ov::op::GeluApproximationMode::ERF) {
return false;
}
// Read the actual gate/up split point from the matched Slice nodes instead of assuming
// inter_size/2, so this stays correct if the model's ffn_dim convention ever changes.
auto gate_slice = ov::as_type_ptr<ov::op::v8::Slice>(pm.at(gate_slice_m).get_node_shared_ptr());
auto up_slice = ov::as_type_ptr<ov::op::v8::Slice>(pm.at(up_slice_m).get_node_shared_ptr());
auto gate_end_c = ov::as_type_ptr<ov::op::v0::Constant>(gate_slice->input_value(2).get_node_shared_ptr());
auto up_begin_c = ov::as_type_ptr<ov::op::v0::Constant>(up_slice->input_value(1).get_node_shared_ptr());
if (!gate_end_c || !up_begin_c) {
return false;
}
const auto gate_end = gate_end_c->cast_vector<int64_t>().at(0);
const auto up_begin = up_begin_c->cast_vector<int64_t>().at(0);
if (gate_end != up_begin || gate_end <= 0 || gate_end >= static_cast<int64_t>(gate_up_shape[1])) {
return false;
}
const int64_t mid = gate_end;
const int64_t full = static_cast<int64_t>(gate_up_shape[1]);
auto ids = pm.at(ids_down_m);
const auto ids_pshape = ids.get_partial_shape();
if (ids_pshape.rank().is_dynamic() || ids_pshape[ids_pshape.rank().get_length() - 1].is_dynamic()) {
return false;
}
const size_t top_k = ids_pshape[ids_pshape.rank().get_length() - 1].get_length();
// routing weights arrive as [1, n_tokens, top_k, 1]; the op wants [..., top_k]
auto routing = pm.at(routing_m);
const auto routing_pshape = routing.get_partial_shape();
if (routing_pshape.rank().is_dynamic() || routing_pshape.rank().get_length() != 4 ||
routing_pshape[3] != 1) {
return false;
}
// Fold gemma-4's per-expert output scale into the routing weights: the reduction is
// sum_e(routing[e] * scale[e] * down_out[e]), and MOECompressed only takes one
// per-expert weight, so pre-multiply it into routing here (same [.., top_k, 1] shape).
auto down_scale = pm.at(down_scale_m);
if (down_scale.get_partial_shape() != routing_pshape) {
return false;
}
routing = std::make_shared<ov::op::v1::Multiply>(routing, down_scale);
routing = std::make_shared<ov::op::v0::Squeeze>(
routing, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ 1 }, { 3 }));
if (ids_pshape.rank().get_length() == 2) {
ids = std::make_shared<ov::op::v0::Unsqueeze>(
ids, ov::op::v0::Constant::create(ov::element::i64, ov::Shape{ 1 }, { 0 }));
}
if (routing.get_partial_shape() != ids.get_partial_shape()) {
return false;
}
const size_t down_k = logical_k(down_shape);
const auto down_scale_shape = down.scale.get_shape();
const size_t down_groups = down_scale_shape.size() >= 3 ? down_scale_shape[2] : 1;
// Split the fused gate_up weight/scale/zp on the output axis. slice_experts_dim1 copies
// raw bytes out of the Constant, so each half stays a Constant -- which is what the op
// needs on its weight ports.
const ov::Output<ov::Node> gate_weight = slice_experts_dim1(gate_up.weight, 0, mid);
const ov::Output<ov::Node> up_weight = slice_experts_dim1(gate_up.weight, mid, full);
const ov::Output<ov::Node> gate_scale = slice_experts_dim1(gate_up.scale, 0, mid);
const ov::Output<ov::Node> up_scale = slice_experts_dim1(gate_up.scale, mid, full);
const ov::Output<ov::Node> gate_zp = slice_experts_dim1(gate_up.zp, 0, mid);
const ov::Output<ov::Node> up_zp = slice_experts_dim1(gate_up.zp, mid, full);
ov::op::internal::MOECompressed::Config config;
config.expert_type = ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU;
config.activation_type = ov::op::internal::MOE::Activation_type::GEGLU_ERF;
config.expert_alpha = 0.0f;
config.expert_beta = 1.0f;
config.gate_idx = 0;
config.hidden_size = logical_k(gate_up_shape);
config.inter_size = static_cast<size_t>(mid);
config.num_expert = gate_up_shape[0];
config.num_shared_expert = 0;
config.top_k = top_k;
config.group_size = down_groups <= 1 ? std::numeric_limits<size_t>::max() : down_k / down_groups;
config.has_batch_dim = true;
config.has_zp = true;
config.out_type = ov::element::dynamic;
// Rebuild the activations Transpose before mul_mat_id's f16 GPU Convert, so the op
// stays f32 like the block it replaces (mirrors FuseMoeCompressed).
const auto a_transpose = pm.at(a_m).get_node_shared_ptr();
ov::Output<ov::Node> hidden =
std::make_shared<ov::op::v1::Transpose>(pm.at(a_reshape_m), a_transpose->input_value(1));
// w0 is the activated (gate) lane and w1 the multiplied (up) lane -- see
// moe_3gemm_swiglu_opt.cpp: "scratch.up = up(x) * silu(gate(x))".
const ov::OutputVector args = {
hidden, routing, ids,
gate_weight, gate_scale, gate_zp,
up_weight, up_scale, up_zp,
down.weight, down.scale, down.zp,
};
auto moe = std::make_shared<ov::op::internal::MOECompressed>(args, config);
ov::Output<ov::Node> result = moe->output(0);
const auto root_type = m.get_match_root()->get_output_element_type(0);
if (result.get_element_type() != root_type) {
result = std::make_shared<ov::op::v0::Convert>(result, root_type);
}
result.get_node_shared_ptr()->set_friendly_name(m.get_match_root()->get_friendly_name());
ov::copy_runtime_info(m.get_matched_nodes(), result.get_node_shared_ptr());
ov::replace_node(m.get_match_root(), result.get_node_shared_ptr());
register_new_node(moe);
return true;
};
register_matcher(
std::make_shared<Matcher>(root_m, "ov::frontend::ggml::pass::FuseMoeCompressedFusedGateUp"), callback);
}
} // namespace pass
} // namespace ggml
} // namespace frontend
@@ -13,6 +13,17 @@ public:
FuseMoeCompressed();
};
// Folds the MoE expert block emitted for a model whose gate and up projections share one
// fused MUL_MAT_ID weight (gemma-4: one GatherMatmul + Slice/Slice split, GEGLU activation)
// into a single ov::op::internal::MOECompressed, GEMM3_SWIGLU/GEGLU_ERF. Splits the fused
// weight/scale/zp into gate/up halves so it lands on the same 3-GEMM expert-grouped kernel
// FuseMoeCompressed uses for models with separate gate/up weights.
class FuseMoeCompressedFusedGateUp : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("ov::frontend::ggml::pass::FuseMoeCompressedFusedGateUp")
FuseMoeCompressedFusedGateUp();
};
} // namespace pass
} // namespace ggml
} // namespace frontend
@@ -0,0 +1,153 @@
#include "fuse_moe_router.h"
#include <openvino/core/graph_util.hpp>
#include <openvino/core/rt_info.hpp>
#include <openvino/op/broadcast.hpp>
#include <openvino/op/clamp.hpp>
#include <openvino/op/concat.hpp>
#include <openvino/op/constant.hpp>
#include <openvino/op/divide.hpp>
#include <openvino/op/gather.hpp>
#include <openvino/op/matmul.hpp>
#include <openvino/op/reduce_sum.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/shape_of.hpp>
#include <openvino/op/slice.hpp>
#include <openvino/op/softmax.hpp>
#include <openvino/op/squeeze.hpp>
#include <openvino/op/tile.hpp>
#include <openvino/op/topk.hpp>
#include <openvino/op/unsqueeze.hpp>
#include <openvino/pass/pattern/op/wrap_type.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace pass {
namespace {
bool constant_is(const ov::Output<ov::Node> & output, const std::vector<int64_t> & values) {
auto node = ov::as_type_ptr<ov::op::v0::Constant>(output.get_node_shared_ptr());
return node && node->get_element_type().is_integral_number() && node->cast_vector<int64_t>() == values;
}
} // namespace
FuseMoeRouter::FuseMoeRouter() {
using namespace ov::pass::pattern;
using namespace ov::op;
// Match the GGML rank-4 routing chain. The GPU plugin recognizes the resulting
// Softmax -> TopK -> ReduceSum -> Divide subgraph as MoERouterFused.
auto logits = wrap_type<v0::MatMul>();
auto softmax = wrap_type<v8::Softmax>({logits});
auto topk = wrap_type<v11::TopK>({softmax, any_input()});
topk->set_output_size(2);
auto ids = wrap_type<v8::Slice>({topk->output(1), any_input(), any_input(), any_input(), any_input()});
auto ids_2d = wrap_type<v0::Squeeze>({ids, any_input()});
auto probs = wrap_type<v1::Reshape>({softmax, any_input()});
auto data = wrap_type<v0::Squeeze>({probs, any_input()});
auto data_batch = wrap_type<v8::Gather>({wrap_type<v3::ShapeOf>({data}), any_input(), any_input()});
auto ids_count = wrap_type<v8::Gather>({wrap_type<v3::ShapeOf>({ids_2d}), any_input(), any_input()});
auto target = wrap_type<v0::Concat>({data_batch, ids_count});
auto broadcast = wrap_type<v3::Broadcast>({ids_2d, target});
auto gather = wrap_type<v8::Gather>({data, broadcast, any_input()});
auto weights_4d = wrap_type<v0::Unsqueeze>({gather, any_input()});
auto weights = wrap_type<v1::Reshape>({weights_4d, any_input()});
auto sum = wrap_type<v1::ReduceSum>({weights, any_input()});
auto clamp = wrap_type<v0::Clamp>({sum});
auto tile = wrap_type<v0::Tile>({clamp, any_input()});
auto norm = wrap_type<v1::Divide>({weights, tile});
const auto callback = [=](Matcher & m) {
const auto & pm = m.get_pattern_value_map();
const auto node = [&](const std::shared_ptr<ov::Node> & p) { return pm.at(p).get_node_shared_ptr(); };
const auto input_is = [&](const std::shared_ptr<ov::Node> & p, size_t i, const std::vector<int64_t> & v) {
return constant_is(node(p)->input_value(i), v);
};
auto mm = ov::as_type_ptr<v0::MatMul>(node(logits));
auto sm = ov::as_type_ptr<v8::Softmax>(node(softmax));
auto tk = ov::as_type_ptr<v11::TopK>(node(topk));
auto reduce = ov::as_type_ptr<v1::ReduceSum>(node(sum));
auto limit = ov::as_type_ptr<v0::Clamp>(node(clamp));
const auto shape = mm->get_output_partial_shape(0);
if (shape.rank() != 4 || shape[0] != 1 || shape[1] != 1 || shape[3].is_dynamic() ||
mm->get_transpose_a() || mm->get_input_partial_shape(0).rank() != 4 ||
mm->get_input_partial_shape(1).rank() != 2 || (sm->get_axis() != -1 && sm->get_axis() != 3) ||
tk->get_axis() != 3 || tk->get_mode() != v11::TopK::Mode::MAX ||
tk->get_sort_type() != v11::TopK::SortType::SORT_VALUES || tk->get_stable() ||
tk->get_index_element_type() != ov::element::i32 ||
!tk->output(0).get_target_inputs().empty()) {
return false;
}
const int64_t experts = shape[3].get_length();
auto end = ov::as_type_ptr<v0::Constant>(node(ids)->get_input_node_shared_ptr(2));
if (!end || !end->get_element_type().is_integral_number() || ov::shape_size(end->get_shape()) != 1) {
return false;
}
const int64_t k = end->cast_vector<int64_t>()[0];
const auto sorted_shape = tk->get_output_partial_shape(1);
if (k <= 0 || k > experts || sorted_shape[3].is_dynamic() || sorted_shape[3].get_length() < k ||
!input_is(probs, 1, {1, -1, experts, 1}) ||
!input_is(data, 1, {0}) || !input_is(ids_2d, 1, {0, 1}) ||
!input_is(weights_4d, 1, {0}) || !input_is(weights, 1, {1, 1, -1, k}) ||
ov::as_type_ptr<v1::Reshape>(node(probs))->get_special_zero() ||
ov::as_type_ptr<v1::Reshape>(node(weights))->get_special_zero() ||
!input_is(gather, 2, {1}) || ov::as_type_ptr<v8::Gather>(node(gather))->get_batch_dims() != 1 ||
!input_is(data_batch, 1, {0}) || !input_is(data_batch, 2, {0}) ||
!input_is(ids_count, 1, {1}) || !input_is(ids_count, 2, {0}) ||
ov::as_type_ptr<v8::Gather>(node(data_batch))->get_batch_dims() != 0 ||
ov::as_type_ptr<v8::Gather>(node(ids_count))->get_batch_dims() != 0 ||
ov::as_type_ptr<v0::Concat>(node(target))->get_axis() != 0 ||
ov::as_type_ptr<v3::Broadcast>(node(broadcast))->get_broadcast_spec().m_type != ov::op::BroadcastType::BIDIRECTIONAL ||
!reduce->get_keep_dims() || (!input_is(sum, 1, {-1}) && !input_is(sum, 1, {3})) ||
!input_is(tile, 1, {1, 1, 1, k}) ||
ov::as_type_ptr<v1::Divide>(node(norm))->get_autob().m_type != ov::op::AutoBroadcastType::NUMPY) {
return false;
}
// The top-k softmax sum is at least k / experts. Leave margin for rounding.
// This guard is only valid for unbiased softmax routing.
if (!(limit->get_min() <= 0.5 * double(k) / experts && limit->get_max() >= 2.0)) {
return false;
}
std::vector<std::shared_ptr<ov::Node>> slices;
for (const auto & input : tk->output(1).get_target_inputs()) {
auto slice = ov::as_type_ptr<v8::Slice>(input.get_node()->shared_from_this());
if (!slice || input.get_index() != 0 || slice->get_input_size() != 5 ||
!constant_is(slice->input_value(1), {0}) || !constant_is(slice->input_value(2), {k}) ||
!constant_is(slice->input_value(3), {1}) ||
(!constant_is(slice->input_value(4), {3}) && !constant_is(slice->input_value(4), {-1}))) {
return false;
}
slices.push_back(slice);
}
// Remove GGML's leading singleton dimension while building the plugin pattern,
// then restore it on both outputs for the following GGML nodes.
auto axis0 = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});
auto hidden = std::make_shared<v0::Squeeze>(mm->input_value(0), axis0);
auto routing = std::make_shared<v0::MatMul>(hidden, mm->input_value(1), false, mm->get_transpose_b());
auto probabilities = std::make_shared<v8::Softmax>(routing, -1);
auto selected = std::make_shared<v11::TopK>(probabilities,
v0::Constant::create(ov::element::i64, ov::Shape{}, {k}), 2,
v11::TopK::Mode::MAX, v11::TopK::SortType::SORT_VALUES, tk->get_index_element_type(), false);
auto total = std::make_shared<v1::ReduceSum>(selected->output(0),
v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1}), true);
auto normalized = std::make_shared<v1::Divide>(selected->output(0), total);
auto weights_out = std::make_shared<v0::Unsqueeze>(normalized, axis0);
auto ids_out = std::make_shared<v0::Unsqueeze>(selected->output(1), axis0);
ov::copy_runtime_info({mm, sm, tk, node(norm)}, {hidden, routing, probabilities, selected, total, normalized, weights_out, ids_out});
ov::replace_node(node(norm), weights_out);
for (const auto & slice : slices) {
ov::replace_node(slice, ids_out);
}
return true;
};
register_matcher(std::make_shared<Matcher>(norm, "ov::frontend::ggml::pass::FuseMoeRouter"), callback);
}
} // namespace pass
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -0,0 +1,19 @@
#pragma once
#include <openvino/pass/matcher_pass.hpp>
namespace ov {
namespace frontend {
namespace ggml {
namespace pass {
class FuseMoeRouter : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("ov::frontend::ggml::pass::FuseMoeRouter")
FuseMoeRouter();
};
} // namespace pass
} // namespace ggml
} // namespace frontend
} // namespace ov
@@ -7,13 +7,14 @@
#include <openvino/op/convert.hpp>
#include <openvino/op/convolution.hpp>
#include <openvino/op/extractimagepatches.hpp>
#include <openvino/op/group_conv.hpp>
#include <openvino/op/matmul.hpp>
#include <openvino/op/pad.hpp>
#include <openvino/op/reshape.hpp>
#include <openvino/op/transpose.hpp>
#include <openvino/pass/pattern/op/label.hpp>
#include <openvino/pass/pattern/op/pattern.hpp>
#include <openvino/pass/pattern/op/wrap_type.hpp>
#include <utility>
namespace opp = ov::pass::pattern;
@@ -22,180 +23,250 @@ namespace frontend {
namespace ggml {
namespace pass {
// This pass fuses an IM2COL + MatMul convolution into OpenVINO's Convolution op for performance gains.
// Reference the im2col.cpp translator for reference on the pattern being matched.
FuseToConv::FuseToConv() {
const auto m_wei = opp::any_input();
const auto m_act = opp::any_input();
const auto m_matmul = opp::wrap_type<ov::op::v0::MatMul>({m_wei, m_act});
const auto m_in0 = opp::any_input();
const auto m_in1 = opp::any_input();
const auto m_matmul = opp::wrap_type<ov::op::v0::MatMul>({m_in0, m_in1});
const auto callback = [=](ov::pass::pattern::Matcher & m) {
const auto & pm = m.get_pattern_value_map();
auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(pm.at(m_matmul).get_node_shared_ptr());
if (!matmul_node || matmul_node->get_transpose_a() || !matmul_node->get_transpose_b()) {
const auto callback = [=](opp::Matcher & m) {
auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(m.get_match_root());
if (!matmul_node) {
return false;
}
auto trace = matmul_node->input_value(1);
auto unwrap = [](ov::Output<Node> n) {
while (ov::is_type<ov::op::v0::Convert>(n.get_node_shared_ptr()) ||
ov::is_type<ov::op::v1::Reshape>(n.get_node_shared_ptr())) {
n = n.get_node_shared_ptr()->input_value(0);
}
return n;
};
// Optional Convert
if (auto n = ov::as_type_ptr<ov::op::v0::Convert>(trace.get_node_shared_ptr())) {
trace = n->input_value(0);
}
auto get_im2col = [&](ov::Output<Node> trace)
-> std::pair<std::shared_ptr<ov::op::v3::ExtractImagePatches>, std::shared_ptr<ov::op::v1::Pad>> {
auto t2 = ov::as_type_ptr<ov::op::v1::Transpose>(unwrap(std::move(trace)).get_node_shared_ptr());
auto r1 = t2 ? ov::as_type_ptr<ov::op::v1::Reshape>(t2->get_input_node_shared_ptr(0)) : nullptr;
auto t1 = r1 ? ov::as_type_ptr<ov::op::v1::Transpose>(r1->get_input_node_shared_ptr(0)) : nullptr;
auto eip =
t1 ? ov::as_type_ptr<ov::op::v3::ExtractImagePatches>(t1->get_input_node_shared_ptr(0)) : nullptr;
auto pad = eip ? ov::as_type_ptr<ov::op::v1::Pad>(eip->get_input_node_shared_ptr(0)) : nullptr;
return {eip, pad};
};
for (int i = 0; i < 2; ++i) {
auto n = ov::as_type_ptr<ov::op::v1::Reshape>(trace.get_node_shared_ptr());
if (!n) {
bool weight_is_in0 = true;
auto [eip, pad] = get_im2col(matmul_node->input_value(1));
ov::Output<Node> w_trace = matmul_node->input_value(0);
if (!pad) {
std::tie(eip, pad) = get_im2col(matmul_node->input_value(0));
w_trace = matmul_node->input_value(1);
weight_is_in0 = false;
if (!pad) {
return false;
}
trace = n->input_value(0);
}
if (auto n = ov::as_type_ptr<ov::op::v1::Transpose>(trace.get_node_shared_ptr())) {
trace = n->input_value(0);
} else {
auto pb_const = ov::as_type_ptr<ov::op::v0::Constant>(pad->get_input_node_shared_ptr(1));
auto pe_const = ov::as_type_ptr<ov::op::v0::Constant>(pad->get_input_node_shared_ptr(2));
if (!pb_const || !pe_const) {
return false;
}
if (auto n = ov::as_type_ptr<ov::op::v1::Reshape>(trace.get_node_shared_ptr())) {
trace = n->input_value(0);
} else {
const auto pb = pb_const->cast_vector<std::ptrdiff_t>();
const auto pe = pe_const->cast_vector<std::ptrdiff_t>();
if (pb.size() < 4 || pe.size() < 4) {
return false;
}
if (auto n = ov::as_type_ptr<ov::op::v1::Transpose>(trace.get_node_shared_ptr())) {
trace = n->input_value(0);
} else {
auto image_input = pad->input_value(0);
auto image_shape = image_input.get_partial_shape();
if (image_shape.rank() != 4 || image_shape[1].is_dynamic()) {
return false;
}
const size_t IC = static_cast<size_t>(image_shape[1].get_length());
w_trace = unwrap(w_trace);
auto weight_pshape = w_trace.get_partial_shape();
if (!weight_pshape.is_static()) {
return false;
}
auto eip = ov::as_type_ptr<ov::op::v3::ExtractImagePatches>(trace.get_node_shared_ptr());
if (!eip) {
return false;
}
const auto eip_strides = eip->get_strides(); // {stride_h, stride_w}
const auto eip_rates = eip->get_rates(); // {dil_h, dil_w}
const auto & ws = weight_pshape.to_shape();
const size_t KH = eip->get_sizes()[0];
const size_t KW = eip->get_sizes()[1];
const size_t kernel_spatial_ic = IC * KH * KW;
auto pad = ov::as_type_ptr<ov::op::v1::Pad>(eip->input_value(0).get_node_shared_ptr());
if (!pad) {
return false;
}
auto pads_begin_const =
ov::as_type_ptr<ov::op::v0::Constant>(pad->input_value(1).get_node_shared_ptr());
const auto pads_begin_vals = pads_begin_const->cast_vector<int64_t>(); // {0, 0, pad_h, pad_w}
const std::ptrdiff_t pad_h = static_cast<std::ptrdiff_t>(pads_begin_vals[2]);
const std::ptrdiff_t pad_w = static_cast<std::ptrdiff_t>(pads_begin_vals[3]);
auto image_input = pad->input_value(0); // [N, IC, 1, IW] NCHW
auto w_trace = matmul_node->input_value(0);
if (auto n = ov::as_type_ptr<ov::op::v0::Convert>(w_trace.get_node_shared_ptr())) {
w_trace = n->input_value(0);
}
for (int i = 0; i < 2; ++i) {
auto n = ov::as_type_ptr<ov::op::v1::Reshape>(w_trace.get_node_shared_ptr());
if (!n) {
break;
size_t groups = 0;
if (IC == 1 && image_shape[0].is_static() && image_shape[2].is_static() && image_shape[3].is_static()) {
if ((ws.size() == 4 && ws[1] == 1 && ws[2] == KH && ws[3] == KW && ws[0] > 1) ||
(ws.size() == 2 && ws[1] == KH * KW && ws[0] > 1) ||
(ws.size() == 3 && ws[1] == 1 && ws[2] == KH * KW && ws[0] > 1)) {
groups = ws[0];
} else if (ws.size() == 4 && ws[0] == 1 && ws[2] == 1 && ws[3] == KH * KW && ws[1] > 1) {
groups = ws[1];
}
w_trace = n->input_value(0);
}
auto weight_const = ov::as_type_ptr<ov::op::v0::Constant>(w_trace.get_node_shared_ptr());
if (!weight_const) {
return false;
}
const bool is_depthwise = groups > 1 && (image_shape[0].get_length() % groups == 0);
// Reshape weight to [OC, IC, 1, KW] (OIHW).
const auto w_shape = weight_const->get_shape();
ov::Shape conv_w_shape;
if (w_shape.size() == 3) {
conv_w_shape = {w_shape[0], w_shape[1], 1, w_shape[2]};
} else if (w_shape.size() == 4) {
conv_w_shape = {w_shape[1], w_shape[2], 1, w_shape[3]};
} else {
return false;
}
ov::Output<Node> conv_out;
size_t OC = 0;
auto weight_reshaped = register_new_node<ov::op::v0::Constant>(weight_const->get_element_type(), conv_w_shape,
if (is_depthwise) {
const size_t N = static_cast<size_t>(image_shape[0].get_length()) / groups;
const size_t IH = static_cast<size_t>(image_shape[2].get_length());
const size_t IW = static_cast<size_t>(image_shape[3].get_length());
OC = groups;
auto img_shape_const = register_new_node<ov::op::v0::Constant>(
ov::element::i64, ov::Shape{4},
std::vector<int64_t>{static_cast<int64_t>(N), static_cast<int64_t>(groups), static_cast<int64_t>(IH),
static_cast<int64_t>(IW)});
ov::Output<Node> image_reshaped =
register_new_node<ov::op::v1::Reshape>(image_input, img_shape_const, false);
const ov::Shape conv_w_shape = {groups, 1, 1, KH, KW};
ov::Output<Node> weight_input;
if (auto weight_const = ov::as_type_ptr<ov::op::v0::Constant>(w_trace.get_node_shared_ptr())) {
weight_input = register_new_node<ov::op::v0::Constant>(weight_const->get_element_type(), conv_w_shape,
weight_const->get_data_ptr());
} else {
auto shape_const = register_new_node<ov::op::v0::Constant>(
ov::element::i64, ov::Shape{5},
std::vector<int64_t>{static_cast<int64_t>(groups), 1, 1, static_cast<int64_t>(KH),
static_cast<int64_t>(KW)});
weight_input = register_new_node<ov::op::v1::Reshape>(w_trace, shape_const, false);
}
ov::Output<Node> weight_input = weight_reshaped;
if (weight_reshaped->get_element_type() != image_input.get_element_type()) {
weight_input = register_new_node<ov::op::v0::Convert>(weight_reshaped, image_input.get_element_type());
if (weight_input.get_element_type() != image_reshaped.get_element_type()) {
weight_input = register_new_node<ov::op::v0::Convert>(weight_input, image_reshaped.get_element_type());
}
conv_out = register_new_node<ov::op::v1::GroupConvolution>(
image_reshaped, weight_input, eip->get_strides(), ov::CoordinateDiff{pb[2], pb[3]},
ov::CoordinateDiff{pe[2], pe[3]}, ov::Strides{eip->get_rates()[0], eip->get_rates()[1]},
ov::op::PadType::EXPLICIT);
} else {
if ((ws.size() == 4 && ws[1] == IC && ws[2] == KH && ws[3] == KW) ||
(ws.size() == 3 && ws[1] == IC && ws[2] == KW) || (ws.size() == 2 && ws[1] == kernel_spatial_ic)) {
OC = ws[0];
} else if ((ws.size() == 4 && ws[0] == 1 && ws[2] == IC && ws[3] == KW) ||
(ws.size() == 2 && ws[0] == kernel_spatial_ic)) {
OC = ws[1];
} else if (ws.size() == 3 && ws[0] == 1 && ws[1] == IC && ws[2] == KW) {
OC = 1;
} else if (ws.size() == 4 && ws[3] == kernel_spatial_ic) {
OC = ws[2];
} else if (ws.size() == 4 && ws[2] == kernel_spatial_ic) {
OC = ws[3];
} else if (kernel_spatial_ic && ov::shape_size(ws) % kernel_spatial_ic == 0) {
OC = ov::shape_size(ws) / kernel_spatial_ic;
} else {
return false;
}
const ov::Shape conv_w_shape = {OC, IC, KH, KW};
ov::Output<Node> weight_input;
if (auto weight_const = ov::as_type_ptr<ov::op::v0::Constant>(w_trace.get_node_shared_ptr())) {
weight_input = register_new_node<ov::op::v0::Constant>(weight_const->get_element_type(), conv_w_shape,
weight_const->get_data_ptr());
} else {
auto shape_const = register_new_node<ov::op::v0::Constant>(
ov::element::i64, ov::Shape{4},
std::vector<int64_t>{static_cast<int64_t>(OC), static_cast<int64_t>(IC), static_cast<int64_t>(KH),
static_cast<int64_t>(KW)});
weight_input = register_new_node<ov::op::v1::Reshape>(w_trace, shape_const, false);
}
if (weight_input.get_element_type() != image_input.get_element_type()) {
weight_input = register_new_node<ov::op::v0::Convert>(weight_input, image_input.get_element_type());
}
conv_out = register_new_node<ov::op::v1::Convolution>(
image_input, weight_input, eip->get_strides(), ov::CoordinateDiff{pb[2], pb[3]},
ov::CoordinateDiff{pe[2], pe[3]}, ov::Strides{eip->get_rates()[0], eip->get_rates()[1]},
ov::op::PadType::EXPLICIT);
}
auto conv = register_new_node<ov::op::v1::Convolution>(
image_input, weight_input,
ov::Strides{static_cast<size_t>(eip_strides[0]), static_cast<size_t>(eip_strides[1])},
ov::CoordinateDiff{pad_h, pad_w}, ov::CoordinateDiff{pad_h, pad_w},
ov::Strides{static_cast<size_t>(eip_rates[0]), static_cast<size_t>(eip_rates[1])},
ov::op::PadType::EXPLICIT);
constexpr auto target_type = ov::element::f32;
ov::Output<Node> conv_out = conv;
if (conv_out.get_element_type() != target_type) {
conv_out = register_new_node<ov::op::v0::Convert>(conv_out, target_type);
}
std::shared_ptr<ov::op::v1::Add> add_node;
ov::Output<Node> bias_input;
for (const auto & consumer_in : matmul_node->output(0).get_target_inputs()) {
auto cast = ov::as_type_ptr<ov::op::v0::Convert>(consumer_in.get_node()->shared_from_this());
if (!cast) {
continue;
auto try_fuse_bias = [&](const std::shared_ptr<Node> & n) {
auto add = ov::as_type_ptr<ov::op::v1::Add>(n);
if (!add) {
return false;
}
for (const auto & add_in : cast->output(0).get_target_inputs()) {
auto add = ov::as_type_ptr<ov::op::v1::Add>(add_in.get_node()->shared_from_this());
if (!add) {
continue;
for (size_t i = 0; i < 2; ++i) {
if (ov::is_type<ov::op::v0::Constant>(add->get_input_node_shared_ptr(i))) {
bias_input = add->input_value(i);
add_node = add;
return true;
}
for (size_t i = 0; i < 2; ++i) {
if (ov::as_type_ptr<ov::op::v0::Constant>(add->input_value(i).get_node_shared_ptr())) {
bias_input = add->input_value(i);
add_node = add;
}
return false;
};
for (const auto & consumer : matmul_node->output(0).get_target_inputs()) {
auto n = consumer.get_node()->shared_from_this();
if (try_fuse_bias(n)) {
break;
}
if (ov::is_type<ov::op::v0::Convert>(n) || ov::is_type<ov::op::v1::Reshape>(n)) {
for (const auto & next : n->output(0).get_target_inputs()) {
if (try_fuse_bias(next.get_node()->shared_from_this())) {
break;
}
}
if (add_node) {
break;
}
}
if (add_node) {
break;
}
}
ov::Output<Node> final_out;
std::shared_ptr<Node> target_node;
ov::Output<Node> final_out = conv_out;
std::shared_ptr<Node> target_node = matmul_node;
if (add_node) {
// Reshape bias [OC, 1] → [1, OC, 1, 1] for NCHW broadcasting.
ov::Output<Node> bias = bias_input;
if (bias.get_element_type() != target_type) {
bias = register_new_node<ov::op::v0::Convert>(bias, target_type);
}
const auto oc = static_cast<int64_t>(conv_w_shape[0]);
auto bias_shape = register_new_node<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4},
std::vector<int64_t>{1, oc, 1, 1});
auto bias_shape = register_new_node<ov::op::v0::Constant>(
ov::element::i64, ov::Shape{4}, std::vector<int64_t>{1, static_cast<int64_t>(OC), 1, 1});
bias = register_new_node<ov::op::v1::Reshape>(bias, bias_shape, false);
final_out = register_new_node<ov::op::v1::Add>(conv_out, bias);
target_node = add_node;
} else {
final_out = conv_out;
target_node = matmul_node;
}
// Reshape final output back to the target node's original shape if needed.
if (!is_depthwise) {
auto perm = register_new_node<ov::op::v0::Constant>(
ov::element::i64, ov::Shape{4},
weight_is_in0 ? std::vector<int64_t>{1, 0, 2, 3} : std::vector<int64_t>{0, 2, 3, 1});
final_out = register_new_node<ov::op::v1::Transpose>(final_out, perm);
}
auto orig_shape = target_node->get_output_partial_shape(0);
if (orig_shape.is_static() && final_out.get_partial_shape().is_static()) {
if (ov::shape_size(orig_shape.to_shape()) != ov::shape_size(final_out.get_shape())) {
return false;
}
}
if (orig_shape.is_static() && final_out.get_partial_shape() != orig_shape) {
auto shape_const = register_new_node<ov::op::v0::Constant>(ov::element::i64, ov::Shape{orig_shape.size()},
orig_shape.to_shape());
final_out = register_new_node<ov::op::v1::Reshape>(final_out, shape_const, false);
}
auto orig_type = target_node->get_output_element_type(0);
if (final_out.get_element_type() != orig_type) {
final_out = register_new_node<ov::op::v0::Convert>(final_out, orig_type);
}
final_out.get_node_shared_ptr()->set_friendly_name(target_node->get_friendly_name());
ov::copy_runtime_info(m.get_matched_nodes(), final_out.get_node_shared_ptr());
ov::replace_node(target_node, final_out.get_node_shared_ptr());
@@ -5,6 +5,8 @@
#include "ggml-openvino/openvino/node_context.h"
#include "ggml-openvino/openvino/utils.h"
#include "input_model.h"
#include "pass/fuse_argsort_topk.h"
#include "pass/fuse_moe_router.h"
#include "pass/fuse_moe_compressed.h"
#include "pass/fuse_to_conv.h"
#include "pass/kv_state_seq_axis.h"
@@ -117,7 +119,7 @@ ov::pass::MakeStateful::ParamResPairs get_kv_param_res_pairs(
return pairs;
}
void add_sliced_mask_stateful(TensorMap & tensor_map) {
void add_sliced_mask_stateful(TensorMap & tensor_map, bool imrope) {
auto create_sliced_mask = [&](const std::string & mask_name, const std::string & sliced_name) {
if ((tensor_map.find(mask_name) != tensor_map.end()) &&
(tensor_map.find("token_len_per_seq") != tensor_map.end()) &&
@@ -134,7 +136,12 @@ void add_sliced_mask_stateful(TensorMap & tensor_map) {
auto axes = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
auto inp_pos = tensor_map.at("inp_pos").get_node_shared_ptr();
auto last_inp_pos = std::make_shared<ov::op::v8::Gather>(inp_pos, neg_one, three);
// IMROPE's fourth position plane is zero for text; use the token's first plane.
ov::Output<ov::Node> last_index = neg_one;
if (imrope) {
last_index = std::make_shared<ov::op::v1::Add>(token_len_per_seq, neg_one);
}
auto last_inp_pos = std::make_shared<ov::op::v8::Gather>(inp_pos, last_index, three);
auto last_inp_pos_1d = std::make_shared<ov::op::v1::Reshape>(
last_inp_pos, ov::op::v0::Constant::create(ov::element::i64, {1}, {1}), false);
auto last_inp_pos_cvt = std::make_shared<ov::op::v0::Convert>(last_inp_pos_1d, ov::element::i64);
@@ -242,7 +249,7 @@ void add_rope_sin_cos(TensorMap & tensor_map, GgmlDecoder & ggml_model_decoder)
// Create common patterns
void preprocess(TensorMap & tensor_map, GgmlDecoder & ggml_model_decoder) {
if (ggml_model_decoder.is_stateful()) {
add_sliced_mask_stateful(tensor_map);
add_sliced_mask_stateful(tensor_map, ggml_model_decoder.get_rope_params()[2] == GGML_ROPE_TYPE_IMROPE);
add_position_mask_stateful_swa(tensor_map);
}
// This optimization is error-prone
@@ -300,13 +307,22 @@ std::shared_ptr<Model> TranslateSession::translate_graph(const frontend::InputMo
return ov::OutputVector{};
}
const auto & node_output_names = decoder->get_output_names(node_idx);
if (operation_type == "GGML_OP_VIEW" && decoder->get_op_case(node_idx) == 2 && node_output_names.size() == 1) {
auto direct_output = tensor_map->find(node_output_names[0]);
if (direct_output != tensor_map->end()) {
// GDN publishes its native attention/state outputs under the two GGML VIEW names.
// Keep those mappings instead of rebuilding slices of a packed temporary.
return ov::OutputVector{direct_output->second};
}
}
auto it = m_translator_map.find(operation_type);
FRONT_END_OP_CONVERSION_CHECK(it != m_translator_map.end(), "Translation for operation type ", operation_type,
" is not implemented.");
NodeContext node_context(decoder, tensor_map, node_idx, this);
ov::OutputVector converted_outputs = it->second(node_context);
const auto & node_output_names = decoder->get_output_names(node_idx);
FRONT_END_OP_CONVERSION_CHECK(node_output_names.size() == converted_outputs.size(), "Number of ",
operation_type, " outputs greater than number of converted outputs, which are ",
node_output_names.size(), " and ", converted_outputs.size(), " respectively.");
@@ -468,11 +484,18 @@ std::shared_ptr<Model> TranslateSession::apply_transformations(std::shared_ptr<M
manager.register_pass<ov::pass::MarkDequantization>(
std::vector<ov::element::Type>{ov::element::u8, ov::element::i8, ov::element::u4, ov::element::i4});
manager.register_pass<pass::FuseToConv>();
manager.register_pass<pass::FuseArgsortTopK>();
// MOECompressed has no CPU plugin implementation, so keep the GatherMatmul path
// everywhere else. Opt-in while the fused path is being brought up.
if (ggml_openvino_get_device_name() == "GPU" && getenv("GGML_OPENVINO_MOE_OP")) {
// MOECompressed has no CPU plugin implementation, so enable it by default only on GPU.
// GGML_OPENVINO_MOE_OP=0 keeps the unfused GatherMatmul path for fallback/debugging.
if (ggml_openvino_is_gpu() &&
ggml_openvino_getenv_int("GGML_OPENVINO_MOE_OP", 1) != 0) {
manager.register_pass<pass::FuseMoeRouter>();
manager.register_pass<pass::FuseMoeCompressed>();
// Same fusion for models whose gate and up projections share one fused expert
// weight (gemma-4). It only matches that shape and only when the experts carry an
// integer zero point, so it is a no-op on the separate-gate/up models above.
manager.register_pass<pass::FuseMoeCompressedFusedGateUp>();
}
if (ggml_model_decoder->is_stateful()) {
+137 -28
View File
@@ -70,11 +70,10 @@ OutputVector rename_outputs_with_suffix(const OutputVector & outputs, const std:
}
namespace {
ov::Output<ov::Node> rope_yarn_ramp_mix(int n_dims, const float corr_dims[2], float ext_factor) {
int half_n_dims = n_dims / 2;
std::vector<float> dim_ids_vec(half_n_dims);
ov::Output<ov::Node> rope_yarn_ramp_mix(int num_elements, const float corr_dims[2], float ext_factor) {
std::vector<float> dim_ids_vec(num_elements);
std::iota(dim_ids_vec.begin(), dim_ids_vec.end(), 0.0f);
auto dim_ids = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1, (size_t) half_n_dims}, dim_ids_vec);
auto dim_ids = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1, (size_t) num_elements}, dim_ids_vec);
auto corr_low = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1, 1}, {corr_dims[0]});
auto corr_high = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1, 1}, {corr_dims[1]});
auto denom = std::make_shared<ov::op::v1::Maximum>(
@@ -114,8 +113,18 @@ void ggml_rope_yarn_corr_dims(int n_dims,
std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params,
std::shared_ptr<ov::Node> inp_pos,
std::shared_ptr<ov::Node> rope_freqs_weight,
bool imrope,
bool stateful) {
int mode,
bool stateful,
int64_t head_dim) {
constexpr int TYPE_IMROPE = 2;
constexpr int TYPE_VISION = 3;
constexpr int TYPE_MROPE = 4;
const bool is_imrope = (mode == TYPE_IMROPE);
const bool is_vision = (mode == TYPE_VISION);
const bool is_mrope = (mode == TYPE_MROPE);
const bool is_multi = is_imrope || is_vision || is_mrope;
if (stateful) {
inp_pos =
std::make_shared<ov::op::v0::Squeeze>(inp_pos, ov::op::v0::Constant::create(ov::element::i64, {1}, {0}));
@@ -123,7 +132,7 @@ std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params
auto pos_perm =
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{3}, std::vector<int64_t>{2, 1, 0});
inp_pos = std::make_shared<ov::op::v1::Transpose>(inp_pos, pos_perm);
} else if (imrope) {
} else if (is_multi) {
inp_pos = std::make_shared<ov::op::v0::Convert>(inp_pos, ov::element::f32);
auto pos_shape = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{5}, {0, 0, 0, 4, -1});
inp_pos = std::make_shared<ov::op::v1::Reshape>(inp_pos, pos_shape, true);
@@ -155,25 +164,102 @@ std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params
const float theta_scale = powf(freq_base, -2.0f / n_dims);
std::vector<float> factor(n_dims_half);
Output<Node> freq_factors;
Output<Node> theta;
float mscale = attn_factor;
if (imrope) {
std::vector<int64_t> gather_indices(n_dims_half);
for (size_t j = 0; j < n_dims_half; j++) {
gather_indices[j] = j % 3;
factor[j] = std::pow(theta_scale, j);
if (is_multi) {
const size_t num_pairs = is_vision ? (n_dims > 0 ? (size_t) n_dims : (size_t) (head_dim / 2)) : n_dims_half;
int sections[4];
memcpy(sections, rope_params + 11, sizeof(int) * 4);
int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
if (sect_dims <= 0) {
sect_dims = num_pairs;
}
int sec_w = sections[0] + sections[1];
int sec_e = sec_w + sections[2];
std::vector<int64_t> gather_indices(num_pairs);
std::vector<float> factor(num_pairs);
for (size_t j = 0; j < num_pairs; j++) {
int sector = j % sect_dims;
int plane = 0;
if (is_imrope) {
if (sector % 3 == 1 && sector < 3 * sections[1]) {
plane = 1;
} else if (sector % 3 == 2 && sector < 3 * sections[2]) {
plane = 2;
} else if (sector % 3 == 0 && sector < 3 * sections[0]) {
plane = 0;
} else {
plane = 3;
}
factor[j] = std::pow(theta_scale, j);
} else if (is_vision) {
int p_idx = 0;
if (sector < sections[0]) {
plane = 0;
p_idx = sector;
} else if (sector < sections[0] + sections[1]) {
plane = 1;
p_idx = sector - sections[0];
} else if (sector < sec_w + sections[2]) {
plane = 2;
p_idx = sector - sec_w;
} else {
plane = 3;
p_idx = sector - sec_e;
}
factor[j] = std::pow(theta_scale, p_idx);
} else {
if (sector >= sections[0] && sector < sec_w) {
plane = 1;
} else if (sector >= sec_w && sector < sec_e) {
plane = 2;
} else if (sector >= sec_e) {
plane = 3;
} else {
plane = 0;
}
factor[j] = std::pow(theta_scale, j);
}
gather_indices[j] = plane;
}
auto gather_indices_const =
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{n_dims_half}, gather_indices);
std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{num_pairs}, gather_indices);
auto gather_axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{}, {4});
inp_pos = std::make_shared<ov::op::v8::Gather>(inp_pos, gather_indices_const, gather_axis);
auto factor_const = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{n_dims_half}, factor);
theta = std::make_shared<ov::op::v1::Multiply>(inp_pos, factor_const);
Output<Node> factor_node =
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{num_pairs}, factor);
if (rope_freqs_weight) {
Output<Node> rope_factors = std::make_shared<ov::op::v8::Slice>(
rope_freqs_weight,
ov::op::v0::Constant::create(ov::element::i64, {1}, {0}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {(int64_t) num_pairs}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {1}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {rope_freqs_weight->get_output_partial_shape(0).rank().get_length() - 1}));
rope_factors = std::make_shared<ov::op::v1::Reshape>(
rope_factors,
ov::op::v0::Constant::create(ov::element::i64, {1}, {(int64_t) num_pairs}),
false);
factor_node = std::make_shared<ov::op::v1::Divide>(factor_node, rope_factors);
}
auto theta_extrap = std::make_shared<ov::op::v1::Multiply>(inp_pos, factor_node);
auto theta_interp = std::make_shared<ov::op::v1::Multiply>(
theta_extrap, ov::op::v0::Constant::create(ov::element::f32, {1}, {freq_scale}));
if (ext_factor == 0.0f) {
theta = theta_interp;
} else {
float corr_dims[2];
ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
auto ramp_mix = rope_yarn_ramp_mix(num_pairs, corr_dims, ext_factor);
Output<Node> one = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1, 1}, {1.0f});
auto one_minus_ramp = std::make_shared<ov::op::v1::Subtract>(one, ramp_mix);
theta = std::make_shared<ov::op::v1::Add>(
std::make_shared<ov::op::v1::Multiply>(theta_interp, one_minus_ramp),
std::make_shared<ov::op::v1::Multiply>(theta_extrap, ramp_mix));
mscale *= (1.0f + 0.1f * std::log(1.0f / freq_scale));
}
} else {
std::vector<float> factor(n_dims_half);
Output<Node> freq_factors;
float corr_dims[2];
ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
factor[0] = 1.0f;
@@ -215,7 +301,7 @@ std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params
if (ext_factor == 0.0f) {
theta = theta_interp;
} else {
auto ramp_mix = rope_yarn_ramp_mix(n_dims, corr_dims, ext_factor);
auto ramp_mix = rope_yarn_ramp_mix(n_dims_half, corr_dims, ext_factor);
Output<Node> one;
if (stateful) {
one = ov::op::v0::Constant::create(ov::element::f32, Shape{1, 1, 1}, {1.0f});
@@ -234,7 +320,7 @@ std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params
Output<Node> cos_theta = std::make_shared<ov::op::v0::Cos>(theta);
Output<Node> sin_theta = std::make_shared<ov::op::v0::Sin>(theta);
if (!imrope) {
if (mscale != 1.0f) {
auto mscale_node = ov::op::v0::Constant::create(ov::element::f32, Shape{}, {mscale});
cos_theta = std::make_shared<ov::op::v1::Multiply>(cos_theta, mscale_node);
@@ -305,18 +391,32 @@ ov::Output<ov::Node> process_view_input_new(const NodeContext & context, int inp
// here would re-slice/re-flatten the already-resolved single-plane view against the
// recorded (multi-plane) source strides and emit a constant-target Reshape whose baked
// dims no longer divide the concretized input -> "dimensions do not evenly divide".
// A fourth case matters for stateful execution: `expected` comes from ggml metadata and is
// always GGML_MAX_DIMS ranks, but stateful drops the leading size-1 batch dim, so the
// resolved view is one rank lower. Compare the common trailing dims and require the
// leading expected dims we skip to be 1. Without this the rank test below never matches on
// the stateful path and every already-resolved MoE expert-plane view is re-sliced.
auto expected_ov_shape = context.get_view_input_ov_shape(input_index, 0);
auto actual_shape = input.get_partial_shape();
if (expected_ov_shape.rank().is_static() && actual_shape.rank().is_static() &&
expected_ov_shape.rank() == actual_shape.rank()) {
expected_ov_shape.rank().get_length() >= actual_shape.rank().get_length()) {
const int64_t n_actual = actual_shape.rank().get_length();
const int64_t shift = expected_ov_shape.rank().get_length() - n_actual;
bool shapes_match = true;
for (int64_t i = 0; i < expected_ov_shape.rank().get_length(); ++i) {
const bool both_dynamic = expected_ov_shape[i].is_dynamic() && actual_shape[i].is_dynamic();
const bool both_static_equal = expected_ov_shape[i].is_static() && actual_shape[i].is_static() &&
expected_ov_shape[i] == actual_shape[i];
for (int64_t i = 0; i < shift; ++i) {
if (!expected_ov_shape[i].is_static() || expected_ov_shape[i].get_length() != 1) {
shapes_match = false;
break;
}
}
for (int64_t i = 0; i < n_actual && shapes_match; ++i) {
const auto & exp = expected_ov_shape[i + shift];
const bool both_dynamic = exp.is_dynamic() && actual_shape[i].is_dynamic();
const bool both_static_equal =
exp.is_static() && actual_shape[i].is_static() && exp == actual_shape[i];
// expected dynamic, actual static: the resolved view already carries the
// concrete size for this fragment; reuse it rather than re-materializing.
const bool expected_dyn_actual_static = expected_ov_shape[i].is_dynamic() && actual_shape[i].is_static();
const bool expected_dyn_actual_static = exp.is_dynamic() && actual_shape[i].is_static();
if (!both_dynamic && !both_static_equal && !expected_dyn_actual_static) {
shapes_match = false;
break;
@@ -399,6 +499,15 @@ ov::Output<ov::Node> process_view_input_new(const NodeContext & context, int inp
const ov::Shape & view_ggml_shape, const ov::PartialShape & view_ov_shape, const std::string & view_name,
size_t view_src_offset, const std::vector<size_t> & view_src_stride, const ov::Shape & view_src_ggml_shape,
const ov::PartialShape & view_src_ov_shape, const std::string & view_src_name) -> ov::Output<ov::Node> {
// Stateful execution drops a leading size-1 axis, so `current` can be one rank lower
// than the ggml shape metadata (view_stride/view_ggml_shape, always GGML_MAX_DIMS
// entries) assumes. Shift any axis index built from that metadata down by the
// difference before using it as an OV Slice axis.
const auto current_rank = current.get_partial_shape().rank();
const int axis_shift = current_rank.is_static() ?
static_cast<int>(view_stride.size()) - static_cast<int>(current_rank.get_length()) :
0;
auto build_reshape_pattern = [](const ov::PartialShape & target_ov_shape,
const ov::Shape & target_ggml_shape) -> std::vector<int64_t> {
const size_t ndims = target_ggml_shape.size();
@@ -509,7 +618,7 @@ ov::Output<ov::Node> process_view_input_new(const NodeContext & context, int inp
current, ov::op::v0::Constant::create(ov::element::i64, {1}, {begin_val}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {end_val}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {1}),
ov::op::v0::Constant::create(ov::element::i64, {1}, {slice_dim}));
ov::op::v0::Constant::create(ov::element::i64, {1}, {slice_dim - axis_shift}));
if (view_ov_shape.is_static()) {
auto reshaped = std::make_shared<ov::op::v1::Reshape>(
+19 -3
View File
@@ -4,6 +4,7 @@
#include <memory>
#include <openvino/core/node.hpp>
#include <openvino/op/convert.hpp>
#include <openvino/op/shape_of.hpp>
#include <openvino/op/slice.hpp>
#include <utility>
@@ -57,8 +58,9 @@ OutputVector rename_outputs_with_suffix(const OutputVector & outputs, const std:
std::pair<ov::Output<Node>, ov::Output<Node>> make_sin_cos(int32_t * rope_params,
std::shared_ptr<ov::Node> inp_pos,
std::shared_ptr<ov::Node> rope_freqs_weight = nullptr,
bool imrope = false,
bool stateful = false);
int mode = 0,
bool stateful = false,
int64_t head_dim = 0);
ov::Output<ov::Node> process_view_input(const NodeContext & context, int input_index, int slice_len = 0, int axis = -1);
@@ -69,7 +71,21 @@ template <typename T> OutputVector translate_1to1_match_2_inputs(const NodeConte
num_inputs_check(context, 2, 2);
auto input_0 = process_view_input_new(context, 0);
auto input_1 = process_view_input_new(context, 1);
auto res = std::make_shared<T>(input_0, input_1);
auto output_type = context.get_output_type();
if (input_0.get_element_type() != input_1.get_element_type()) {
if (input_0.get_element_type() != ov::element::f32) {
input_0 = std::make_shared<ov::op::v0::Convert>(input_0, ov::element::f32);
}
if (input_1.get_element_type() != ov::element::f32) {
input_1 = std::make_shared<ov::op::v0::Convert>(input_1, ov::element::f32);
}
}
ov::Output<ov::Node> res = std::make_shared<T>(input_0, input_1);
if (res.get_element_type() != output_type) {
res = std::make_shared<ov::op::v0::Convert>(res, output_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
}
+378 -106
View File
@@ -34,6 +34,7 @@
#include <openvino/runtime/properties.hpp>
#include <openvino/runtime/tensor.hpp>
#include <optional>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
@@ -98,27 +99,27 @@ std::optional<ov::Tensor> try_make_kv_sliced_tensor(const std::shared_ptr<GgmlOv
ov::Shape sliced_shape = full_shape;
sliced_shape[2] = static_cast<size_t>(n_kv);
// Disabling for now as gpu has bug with in-place ScatterUpdate with remote tensors, can re-enable once CVS-186519 is fixed
// if (ggml_openvino_buffer_is_remote(ggml_tensor)) {
// auto remote_context = ggml_openvino_get_remote_context();
// auto gpu_context = remote_context->as<ov::intel_gpu::ocl::ClContext>();
// return gpu_context.create_tensor(ggml_decoder->get_ov_type(ggml_tensor), sliced_shape, ggml_tensor->data);
// }
if (ggml_openvino_buffer_is_remote(ggml_tensor)) {
auto remote_context = ggml_openvino_get_remote_context();
auto gpu_context = remote_context->as<ov::intel_gpu::ocl::ClContext>();
return gpu_context.create_tensor(ggml_decoder->get_ov_type(ggml_tensor), sliced_shape, ggml_tensor->data);
}
return ov::Tensor(GgmlOvDecoder::get_ov_type(ggml_tensor), sliced_shape, ggml_tensor->data);
}
uint64_t ggml_openvino_model_cache_extra_cfg(const std::string & device, bool stateful) {
static uint64_t ggml_openvino_model_cache_extra_cfg(bool stateful, bool recurrent) {
const char * manual_gqa_env = ggml_openvino_getenv_str("GGML_OPENVINO_MANUAL_GQA_ATTN");
const bool manual_gqa_enabled = manual_gqa_env != nullptr ?
ggml_openvino_getenv_int("GGML_OPENVINO_MANUAL_GQA_ATTN") > 0 :
device == "GPU";
ggml_openvino_is_gpu();
uint64_t extra_cfg = 1; // Graph-ordinal port names (invalidate older disk-cache blobs).
extra_cfg = extra_cfg * 131 + (stateful ? 1u : 0u);
extra_cfg = extra_cfg * 131 + (stateful ? (recurrent ? 2u : 1u) : 0u);
extra_cfg = extra_cfg * 131 + (ggml_openvino_reduce_compile_mem_enabled() ? 1u : 0u);
extra_cfg = extra_cfg * 131 + (ggml_openvino_getenv_int("GGML_OPENVINO_DISABLE_KV_SLICE") ? 1u : 0u);
extra_cfg = extra_cfg * 131 + (manual_gqa_enabled ? 1u : 0u);
extra_cfg = extra_cfg * 131 + (ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING") >= 2 ? 1u : 0u);
return extra_cfg;
}
@@ -134,10 +135,11 @@ std::map<std::string, std::shared_ptr<ov::Node>> get_weight_names(ggml_cgraph *
// miss. Include topology, layouts, op parameters, constant extra inputs and weight
// allocation identities. Never use a sampled weight hash or a graph name alone:
// different models can have identical topology. OV buffer IDs survive address reuse.
std::string compiled_graph_key(const ggml_cgraph * graph,
const GgmlOvDecoder & decoder,
const std::string & device,
int prefill_chunk_size = 0) {
static std::string compiled_graph_key(const ggml_cgraph * graph,
const GgmlOvDecoder & decoder,
const std::string & device,
int prefill_chunk_size = 0,
bool disk_cache = false) {
std::string key;
auto append = [&key](const auto & value) {
key.append(reinterpret_cast<const char *>(&value), sizeof(value));
@@ -173,7 +175,7 @@ std::string compiled_graph_key(const ggml_cgraph * graph,
const auto * base = tensor->view_src ? tensor->view_src : tensor;
const bool weight = base->buffer && base->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS;
append(weight);
if (weight) {
if (weight && !disk_cache) {
const size_t buffer_id = ggml_backend_openvino_buffer_get_ctx_id(base->buffer);
has_weight_buffer_id |= buffer_id != 0;
append(buffer_id);
@@ -206,7 +208,170 @@ std::string compiled_graph_key(const ggml_cgraph * graph,
}
// Without an allocation generation, pointer reuse could select stale weights.
// Such graphs still get private requests; they simply do not share compilation.
return has_weight_buffer_id ? key : std::string{};
return disk_cache || has_weight_buffer_id ? key : std::string{};
}
static std::string dynamic_graph_signature(const ggml_cgraph * graph,
const GgmlOvDecoder & decoder,
const ModelParams & params) {
std::string key = "dynamic-1";
auto append = [&key](const auto & value) {
key.append(reinterpret_cast<const char *>(&value), sizeof(value));
};
auto append_string = [&](const std::string & value) {
append(value.size());
key.append(value);
};
graph_key graph_id(graph, true);
append(graph_id.n_nodes);
append(graph_id.n_leaves);
append_string(graph_id.first_node_name);
append_string(graph_id.last_node_name);
for (const auto & name : graph_id.input_srcs) {
append_string(name);
}
append(params.n_rs_slots);
append(params.has_rs_rollback);
append(params.mixed_rope_params);
append(params.is_cacheless_attn);
for (int layer : params.swa_layers) {
append(layer);
}
for (const auto & [layer, heads] : params.n_heads_kv_per_layer) {
append(layer);
append(heads);
}
for (const auto & [name, input] : decoder.get_model_inputs()) {
append_string(name);
append_string(input.type.get_type_name());
}
for (const auto & [name, tensor] : decoder.get_model_outputs()) {
append_string(name);
append(tensor->type);
}
for (const auto & [name, input] : decoder.get_model_extra_inputs()) {
append_string(name);
append_string(input.type.get_type_name());
append(input.is_parameter);
if (!input.is_parameter) {
append(input.value);
}
}
return key;
}
static bool compiled_model_matches_graph(const ov::CompiledModel & model,
const std::vector<std::string> & inputs,
const std::vector<std::string> & outputs,
const GgmlOvDecoder & decoder) {
if (model.inputs().size() != inputs.size() || model.outputs().size() != outputs.size()) {
return false;
}
const auto & graph_inputs = decoder.get_model_inputs();
const auto & extra_inputs = decoder.get_model_extra_inputs();
const auto & graph_outputs = decoder.get_model_outputs();
for (size_t i = 0; i < inputs.size(); ++i) {
const auto & port = model.input(i);
auto graph_it = graph_inputs.find(inputs[i]);
if (graph_it != graph_inputs.end()) {
if (port.get_element_type() != graph_it->second.type ||
!port.get_partial_shape().compatible(graph_it->second.shape)) {
return false;
}
continue;
}
auto extra_it = extra_inputs.find(inputs[i]);
if (extra_it == extra_inputs.end() || port.get_element_type() != extra_it->second.type ||
!port.get_partial_shape().compatible(ov::PartialShape(extra_it->second.shape))) {
return false;
}
}
for (size_t i = 0; i < outputs.size(); ++i) {
auto graph_it = graph_outputs.find(outputs[i]);
if (graph_it == graph_outputs.end()) {
if (outputs[i].compare(0, 8, "__debug_") != 0) {
return false;
}
continue;
}
const auto & port = model.output(i);
if (port.get_element_type() != GgmlOvDecoder::get_ov_type(graph_it->second) ||
!port.get_partial_shape().compatible(ov::PartialShape(GgmlOvDecoder::get_shape(graph_it->second)))) {
return false;
}
}
return true;
}
static std::string ov_profiling_csv_field(const std::string & value) {
std::string escaped = "\"";
for (char c : value) {
escaped += c;
if (c == '"') {
escaped += c;
}
}
escaped += '"';
return escaped;
}
static void dump_ov_profiling_info(const ov::InferRequest & infer_request) {
if (ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING") < 2) {
return;
}
static std::atomic<uint64_t> inference_index{0};
const uint64_t index = inference_index.fetch_add(1);
const std::string path = "openvino_profile_" + std::to_string(index) + ".csv";
std::ofstream output(path, std::ios::trunc);
if (!output.is_open()) {
GGML_LOG_WARN("ggml-openvino: failed to write profiling data to %s\n", path.c_str());
return;
}
output << "status,real_time_us,cpu_time_us,start_time_us,node_type,node_name,exec_type\n";
int64_t total_real_time = 0;
int64_t total_cpu_time = 0;
size_t executed_count = 0;
for (const auto & info : infer_request.get_profiling_info()) {
const char * status = "NOT_RUN";
if (info.status == ov::ProfilingInfo::Status::EXECUTED) {
status = "EXECUTED";
total_real_time += info.real_time.count();
total_cpu_time += info.cpu_time.count();
executed_count++;
} else if (info.status == ov::ProfilingInfo::Status::OPTIMIZED_OUT) {
status = "OPTIMIZED_OUT";
}
output << status << ',' << info.real_time.count() << ',' << info.cpu_time.count() << ','
<< info.start_time.count() << ',' << ov_profiling_csv_field(info.node_type) << ','
<< ov_profiling_csv_field(info.node_name) << ',' << ov_profiling_csv_field(info.exec_type) << '\n';
}
GGML_LOG_INFO("ggml-openvino: profile %s: %zu executed nodes, %.3f ms device, %.3f ms CPU\n", path.c_str(),
executed_count, total_real_time / 1000.0, total_cpu_time / 1000.0);
}
static void reset_single_slot_recurrent_cache(ggml_cgraph * cgraph,
const ModelParams & model_params,
const ComputeParams & compute_params) {
if (model_params.n_rs_slots != 1 || compute_params.cache_rs_reset_len == 0) {
return;
}
GGML_ASSERT(compute_params.cache_rs_reset_idx == 0 && compute_params.cache_rs_reset_len == 1);
std::set<ggml_backend_buffer_t> buffers;
for (int i = 0; i < cgraph->n_nodes; i++) {
auto * node = cgraph->nodes[i];
if (node->op == GGML_OP_SCALE && GgmlOvDecoder::is_recurrent_cache(node->view_src) &&
node->view_src->ne[1] == 1 && node->view_src->buffer != nullptr) {
buffers.insert(node->view_src->buffer);
}
}
for (auto * buffer : buffers) {
ggml_backend_buffer_clear(buffer, 0);
}
}
ov::Tensor create_ov_output_tensor(const std::shared_ptr<GgmlOvDecoder> & ggml_decoder,
@@ -217,16 +382,7 @@ ov::Tensor create_ov_output_tensor(const std::shared_ptr<GgmlOvDecoder> & ggml_d
return *sliced;
}
// Disabling for now as gpu has bug with in-place ScatterUpdate with remote tensors, can re-enable once CVS-186519 is fixed
// if (ggml_tensor->extra != nullptr && !ggml_decoder->is_splited_model()) {
// auto * extra_base = static_cast<ggml_openvino_extra_base *>(ggml_tensor->extra);
// if (extra_base->type == ggml_openvino_extra_base::Type::TENSOR) {
// auto * tensor_extra = static_cast<ggml_openvino_tensor_extra *>(extra_base);
// return *tensor_extra->tensor;
// }
// }
auto output_type = GgmlOvDecoder::get_ov_type(ggml_tensor);
auto output_type = ggml_decoder->get_ov_type(ggml_tensor);
ov::Shape output_shape;
void * output_data = ggml_tensor->data;
if (ggml_decoder->is_static()) {
@@ -245,6 +401,21 @@ ov::Tensor create_ov_output_tensor(const std::shared_ptr<GgmlOvDecoder> & ggml_d
output_shape = GgmlOvDecoder::get_shape(ggml_tensor);
}
}
// The sliced path above covers eligible KV outputs. This also covers recurrent state and full-size KV fallbacks.
if (!ggml_openvino_getenv_int("GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS") && !ggml_decoder->is_static() &&
!ggml_decoder->is_splited_model() && ggml_openvino_buffer_is_remote(ggml_tensor) &&
ggml_tensor->extra != nullptr) {
auto * extra_base = static_cast<ggml_openvino_extra_base *>(ggml_tensor->extra);
if (extra_base->type == ggml_openvino_extra_base::Type::TENSOR) {
auto * tensor_extra = static_cast<ggml_openvino_tensor_extra *>(extra_base);
if (tensor_extra->tensor != nullptr && tensor_extra->tensor->get_element_type() == output_type &&
tensor_extra->tensor->get_shape() == output_shape) {
return *tensor_extra->tensor;
}
}
}
ov::Tensor output_tensor(output_type, output_shape, output_data);
return output_tensor;
}
@@ -632,40 +803,63 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
auto & core = ov_singleton_core();
const auto & config = ggml_openvino_get_compile_config();
const auto & device = r_ctx->device;
const auto & stateful = r_ctx->stateful;
static auto is_static = false;
static const bool cache_disabled = ggml_openvino_getenv_int("GGML_OPENVINO_DISABLE_CACHE");
auto start_time = ggml_time_us();
// is_model_splitted is O(n_nodes^2) plus a create_weight_nodes scan and takes ~20 ms
// on a Llama-1B decode graph. It is called once per graph_compute invocation but the
// graph shape is identical across all decode steps, so memoize by graph_key: compute
// graph_key first (a few hundred us), and if the same key is already in decoder_cache
// we know the graph is not splitted (only not-splitted graphs get inserted there).
const int64_t cache_key_start_time = ggml_time_us();
graph_key key(cgraph);
const int64_t cache_key_compute_time = ggml_time_us() - cache_key_start_time;
int64_t cache_lookup_time = 0;
bool key_seen = false;
if (!cache_disabled) {
const int64_t cache_lookup_start_time = ggml_time_us();
std::lock_guard<std::mutex> map_lock(r_ctx->ctx_mutex);
key_seen = r_ctx->decoder_cache.find(key) != r_ctx->decoder_cache.end();
cache_lookup_time += ggml_time_us() - cache_lookup_start_time;
}
const int64_t model_split_check_start_time = ggml_time_us();
bool model_is_splitted = key_seen ? false : is_model_splitted(cgraph);
const int64_t model_split_check_time = ggml_time_us() - model_split_check_start_time;
if (ggml_openvino_model_cache_only() && (model_is_splitted || is_naive(cgraph))) {
GGML_LOG_ERROR("ggml-openvino: cache-only mode requires a complete model graph\n");
return GGML_STATUS_FAILED;
}
if (is_naive(cgraph)) {
if (!model_is_splitted) {
return naive_compute(cgraph, core, device, config, *r_ctx->compiled_cache);
}
}
auto start_time = ggml_time_us();
std::shared_ptr<GgmlOvDecoder> ggml_decoder;
std::shared_ptr<ov::InferRequest> infer_request;
ModelParams m_params;
ComputeParams c_params;
const int64_t graph_param_compute_start_time = ggml_time_us();
std::tie(m_params, c_params) = GgmlOvDecoder::compute_llm_params(cgraph, is_static);
const int64_t graph_param_compute_time = ggml_time_us() - graph_param_compute_start_time;
const bool cache_enabled = !model_is_splitted && !cache_disabled;
const bool stateful_recurrent = r_ctx->stateful && m_params.state_size >= 0;
const bool stateful_kv_only = r_ctx->stateful && !stateful_recurrent;
if (r_ctx->stateful &&
(m_params.n_seq > 1 || c_params.n_seq_active > 1 || m_params.n_rs_slots > 1 || m_params.has_rs_rollback)) {
GGML_LOG_ERROR("OpenVINO stateful execution requires a single slot without recurrent rollback. Use -np 1.\n");
return GGML_STATUS_FAILED;
}
if (stateful_recurrent && !cache_enabled) {
GGML_LOG_ERROR("OpenVINO recurrent stateful execution requires an unsplit graph with model caching enabled.\n");
return GGML_STATUS_FAILED;
}
bool cache_hit = false;
int64_t decoder_end_time;
@@ -678,6 +872,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
std::shared_ptr<decoder_runtime_ctx> entry;
ModelParams old_m_params;
const int64_t cache_lookup_start_time = ggml_time_us();
if (cache_enabled) {
std::lock_guard<std::mutex> map_lock(r_ctx->ctx_mutex);
auto it = r_ctx->decoder_cache.find(key);
@@ -695,6 +890,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
entry = std::make_shared<decoder_runtime_ctx>(mutex);
cache_hit = false;
}
cache_lookup_time += ggml_time_us() - cache_lookup_start_time;
std::lock_guard<std::mutex> lock(*(entry->mutex));
cache_hit = cache_hit && entry->ptr && r_ctx->infer_request_cache.count(key) != 0;
@@ -714,7 +910,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
std::map<std::string, std::shared_ptr<ov::Node>> model_weights;
ggml_decoder->set_compute_params(c_params);
ggml_decoder->set_model_params(m_params);
if (old_m_params.kv_buffer_changed(m_params)) {
if (old_m_params.kv_buffer_changed(m_params) || !ggml_decoder->is_bound_to(cgraph)) {
ggml_decoder->update_io(cgraph);
}
ggml_decoder->add_extra_inputs();
@@ -725,7 +921,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
ov_output_names = r_ctx->ov_output_names_cache.at(key);
}
if (stateful) {
if (stateful_kv_only) {
const auto * inp_pos = get_inp_pos_tensor(cgraph);
int32_t * pos_data = (int32_t *) inp_pos->data;
auto pos_shape = GgmlOvDecoder::get_shape(inp_pos);
@@ -830,67 +1026,85 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
auto shared_cache = r_ctx->compiled_cache;
std::unique_lock<std::mutex> compile_lock(shared_cache->mutex);
auto weight_names = get_weight_names(cgraph);
ggml_decoder = std::make_shared<GgmlOvDecoder>(cgraph, m_params, c_params, weight_names, is_static,
stateful, model_is_splitted);
const std::string shared_key = cache_enabled ? compiled_graph_key(cgraph, *ggml_decoder, device) : "";
ggml_decoder = std::make_shared<GgmlOvDecoder>(cgraph, m_params, c_params, weight_names,
is_static, r_ctx->stateful, model_is_splitted);
const std::string model_cache_dir = ggml_openvino_model_cache_dir();
const std::string exact_key = cache_enabled ? compiled_graph_key(cgraph, *ggml_decoder, device) : "";
uint64_t model_fp = 0;
uint64_t exact_fp = 0;
std::string blob_path, manifest_path;
if (!model_cache_dir.empty() && !model_is_splitted) {
const uint64_t extra_cfg =
ggml_openvino_model_cache_extra_cfg(r_ctx->stateful, stateful_recurrent);
model_fp =
ggml_openvino_model_fingerprint(cgraph, device, /*fa=*/true, m_params.rope_params, 16, extra_cfg,
dynamic_graph_signature(cgraph, *ggml_decoder, m_params));
exact_fp =
ggml_openvino_model_fingerprint(cgraph, device, /*fa=*/true, m_params.rope_params, 16, extra_cfg,
compiled_graph_key(cgraph, *ggml_decoder, device, 0, true));
blob_path = ggml_openvino_model_cache_blob_path(model_cache_dir, model_fp);
manifest_path = ggml_openvino_model_cache_manifest_path(model_cache_dir, model_fp);
}
std::string shared_key =
cache_enabled ? (model_fp ? "dynamic:" + std::to_string(model_fp) : exact_key) : "";
ov::CompiledModel shared_model;
bool imported = false;
auto shared_it = shared_cache->graphs.find(shared_key);
if (!shared_key.empty() && shared_it != shared_cache->graphs.end()) {
if (!shared_key.empty() && shared_it != shared_cache->graphs.end() &&
(model_fp == 0 || compiled_model_matches_graph(shared_it->second.decode, shared_it->second.input_names,
shared_it->second.output_names, *ggml_decoder))) {
shared_model = shared_it->second.decode;
infer_request = std::make_shared<ov::InferRequest>(shared_model.create_infer_request());
ov_input_names = shared_it->second.input_names;
ov_output_names = shared_it->second.output_names;
imported = true;
GGML_LOG_DEBUG("ggml-openvino: shared compiled model HIT (dynamic)\n");
}
// Fail fast: a cache-miss recompile feeds weight data to compile_model, but
// GGML_OPENVINO_RELEASE_WEIGHTS (or GGML_OPENVINO_MEMORY_OPTIMIZE on GPU)
// may have already dropped the host weight pages
// (they would read as zeros). That mode requires stable graph shapes.
if (!imported && ggml_openvino_weight_buffers_released()) {
GGML_ABORT(
"ggml-openvino: a new graph needs to be compiled but host weight buffers were already "
"released via GGML_OPENVINO_RELEASE_WEIGHTS/GGML_OPENVINO_MEMORY_OPTIMIZE. This mode requires "
"stable graph shapes; disable host weight release for dynamic workloads.");
} else if (model_fp && shared_it != shared_cache->graphs.end()) {
shared_key = exact_key;
shared_it = shared_cache->graphs.find(shared_key);
if (shared_it != shared_cache->graphs.end()) {
shared_model = shared_it->second.decode;
infer_request = std::make_shared<ov::InferRequest>(shared_model.create_infer_request());
ov_input_names = shared_it->second.input_names;
ov_output_names = shared_it->second.output_names;
imported = true;
}
model_fp = exact_fp;
blob_path = ggml_openvino_model_cache_blob_path(model_cache_dir, model_fp);
manifest_path = ggml_openvino_model_cache_manifest_path(model_cache_dir, model_fp);
}
if (cache_enabled) {
std::lock_guard<std::mutex> map_lock(r_ctx->ctx_mutex);
r_ctx->infer_request_cache.erase(key);
}
// Frontend-level compiled-model cache (GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR): if this model
// was compiled before, import the saved blob and skip requant + convert +
// compile. Only the dynamic single-model path is cached (split models compile
// two graphs and are left to the plugin-level ov::cache_dir). The decoder is
// still needed for I/O mapping, but can be built without weight nodes since
// the weights are baked into the imported CompiledModel.
const std::string model_cache_dir = ggml_openvino_model_cache_dir();
uint64_t model_fp = 0;
std::string blob_path;
std::string manifest_path;
// When the frontend model cache is active it supersedes the plugin-level
// ov::cache_dir: a blob exported from a model compiled WITH cache_dir cannot
// be re-imported (import returns an uninitialized model). Strip cache_dir /
// cache_mode from the config used for the cached compile and the import.
// Import standalone blobs with weights before graph conversion and compilation.
// Split models use the plugin cache instead. The decoder still maps graph I/O.
ov::AnyMap mc_config = config;
if (!model_cache_dir.empty()) {
mc_config.erase("CACHE_DIR");
mc_config.erase("CACHE_MODE");
mc_config[ov::cache_mode.name()] = ov::CacheMode::OPTIMIZE_SPEED;
}
if (!imported && !model_cache_dir.empty() && !model_is_splitted) {
const uint64_t extra_cfg = ggml_openvino_model_cache_extra_cfg(device, stateful);
model_fp =
ggml_openvino_model_fingerprint(cgraph, device, /*fa=*/true, m_params.rope_params, 16, extra_cfg);
blob_path = ggml_openvino_model_cache_blob_path(model_cache_dir, model_fp);
manifest_path = ggml_openvino_model_cache_manifest_path(model_cache_dir, model_fp);
std::ifstream blob_in(blob_path, std::ios::binary);
bool blob_ok = blob_in.is_open();
bool manifest_ok =
blob_ok && ggml_openvino_model_cache_verify_manifest(manifest_path, cgraph, model_fp);
if (blob_ok && manifest_ok) {
int64_t import_start = ggml_time_us();
const uint64_t preferred_fp = model_fp;
bool dynamic_incompatible = preferred_fp == exact_fp;
for (int candidate = 0; candidate < 2; ++candidate) {
if (imported || (candidate == 1 && preferred_fp == exact_fp)) {
break;
}
const uint64_t candidate_fp = candidate == 0 ? preferred_fp : exact_fp;
const std::string candidate_blob =
ggml_openvino_model_cache_blob_path(model_cache_dir, candidate_fp);
const std::string candidate_manifest =
ggml_openvino_model_cache_manifest_path(model_cache_dir, candidate_fp);
std::ifstream blob_in(candidate_blob, std::ios::binary);
std::vector<std::string> saved_inputs, saved_outputs;
if (!blob_in.is_open() ||
!ggml_openvino_model_cache_verify_manifest(candidate_manifest, cgraph, candidate_fp,
saved_inputs, saved_outputs)) {
continue;
}
const int64_t import_start = ggml_time_us();
try {
ov::CompiledModel cm;
auto remote_context = ggml_openvino_get_remote_context();
@@ -899,50 +1113,78 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
} else {
cm = core.import_model(blob_in, device, mc_config);
}
// Lightweight decoder: names-only weight map (membership is all the
// decoder needs; weights live in the imported model).
std::map<std::string, std::shared_ptr<ov::Node>> weight_names;
for (const auto & n : GgmlOvDecoder::collect_weight_names(cgraph)) {
weight_names[n] = nullptr;
// CPU serialization can rename Results that share a name with a Parameter.
if (!saved_inputs.empty() || !saved_outputs.empty()) {
ov_input_names = std::move(saved_inputs);
ov_output_names = std::move(saved_outputs);
} else {
for (const auto & p : cm.inputs()) {
ov_input_names.push_back(p.get_node()->get_friendly_name());
}
for (const auto & o : cm.outputs()) {
ov_output_names.push_back(o.get_node()->get_friendly_name());
}
}
if (!compiled_model_matches_graph(cm, ov_input_names, ov_output_names, *ggml_decoder)) {
dynamic_incompatible |= candidate == 0;
ov_input_names.clear();
ov_output_names.clear();
continue;
}
ggml_decoder = std::make_shared<GgmlOvDecoder>(cgraph, m_params, c_params, weight_names,
is_static, stateful, model_is_splitted);
infer_request = std::make_shared<ov::InferRequest>(cm.create_infer_request());
shared_model = cm;
entry->ptr = ggml_decoder;
// Names must match the decoder's ggml-tensor keys. The non-cached
// path keys off Parameter/Result *friendly names* (set by the
// frontend); export_model preserves these, and each compiled-model
// port's node is exactly that Parameter/Result. Use the port nodes
// directly (NOT get_runtime_model(), whose graph differs and is
// unsafe to deref this way).
for (const auto & p : cm.inputs()) {
ov_input_names.push_back(p.get_node()->get_friendly_name());
}
for (const auto & o : cm.outputs()) {
ov_output_names.push_back(o.get_node()->get_friendly_name());
}
imported = true;
model_fp = candidate_fp;
blob_path = candidate_blob;
manifest_path = candidate_manifest;
if (candidate_fp == exact_fp) {
shared_key = exact_key;
shared_it = shared_cache->graphs.find(shared_key);
}
if (ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING")) {
GGML_LOG_INFO(" - Model cache import time: %.3f ms \n",
(ggml_time_us() - import_start) / 1000.0);
}
GGML_LOG_INFO("ggml-openvino: model cache HIT %s\n", blob_path.c_str());
GGML_LOG_INFO("ggml-openvino: model cache HIT %s\n", candidate_blob.c_str());
} catch (const std::exception & e) {
GGML_LOG_WARN("ggml-openvino: model cache import failed (%s), recompiling\n", e.what());
GGML_LOG_WARN("ggml-openvino: model cache import failed: %s\n", e.what());
dynamic_incompatible |= candidate == 0;
imported = false;
ov_input_names.clear();
ov_output_names.clear();
infer_request.reset();
shared_model = {};
}
}
if (!imported && dynamic_incompatible) {
model_fp = exact_fp;
shared_key = exact_key;
shared_it = shared_cache->graphs.find(shared_key);
blob_path = ggml_openvino_model_cache_blob_path(model_cache_dir, model_fp);
manifest_path = ggml_openvino_model_cache_manifest_path(model_cache_dir, model_fp);
}
}
std::shared_ptr<ov::Model> model;
if (!imported && ggml_openvino_model_cache_only()) {
GGML_LOG_ERROR(
"ggml-openvino: missing or incompatible compiled model: %s; run once without "
"GGML_OPENVINO_COMPILED_MODEL_CACHE_ONLY\n",
blob_path.c_str());
return GGML_STATUS_FAILED;
}
if (!imported && ggml_openvino_weight_buffers_released()) {
GGML_LOG_ERROR("ggml-openvino: cannot compile a new graph after releasing host weights\n");
return GGML_STATUS_FAILED;
}
if (imported) {
decoder_end_time = conversion_end_time = compile_end_time = ggml_time_us();
} else {
auto model_weights = GgmlOvDecoder::create_weight_nodes(cgraph);
ggml_decoder = std::make_shared<GgmlOvDecoder>(cgraph, m_params, c_params, model_weights, is_static,
stateful, model_is_splitted);
r_ctx->stateful, model_is_splitted);
decoder_end_time = ggml_time_us();
auto input_model = std::make_shared<ov::frontend::ggml::InputModel>(ggml_decoder);
@@ -969,13 +1211,21 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
}
compile_end_time = ggml_time_us();
for (const auto & ov_param : model->get_parameters()) {
ov_input_names.push_back(ov_param->get_friendly_name());
}
for (const auto & ov_output : model->get_results()) {
ov_output_names.push_back(ov_output->get_friendly_name());
}
// Export to the frontend model cache for next time. Publish the blob first,
// then the manifest, so a cache hit only sees fully written artifacts.
if (!model_cache_dir.empty() && !model_is_splitted && model_fp != 0) {
try {
const std::string blob_tmp = blob_path + ".tmp";
const std::string manifest_tmp = manifest_path + ".tmp";
if (ggml_openvino_model_cache_write_manifest(manifest_tmp, cgraph, model_fp)) {
const std::string blob_tmp = ggml_openvino_model_cache_temp_path(blob_path);
const std::string manifest_tmp = ggml_openvino_model_cache_temp_path(manifest_path);
if (ggml_openvino_model_cache_write_manifest(manifest_tmp, cgraph, model_fp, ov_input_names,
ov_output_names)) {
std::ofstream blob_out(blob_tmp, std::ios::binary | std::ios::trunc);
if (blob_out.is_open()) {
compiled_model.export_model(blob_out);
@@ -1005,12 +1255,6 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
shared_model = compiled_model;
entry->ptr = ggml_decoder;
for (const auto & ov_param : model->get_parameters()) {
ov_input_names.push_back(ov_param->get_friendly_name());
}
for (const auto & ov_output : model->get_results()) {
ov_output_names.push_back(ov_output->get_friendly_name());
}
} // end non-imported (compile) path
entry->ptr = ggml_decoder;
@@ -1025,7 +1269,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
r_ctx->ov_output_names_cache[key] = ov_output_names;
}
if (stateful && cache_enabled) {
if (stateful_kv_only && cache_enabled) {
const auto * inp_pos = get_inp_pos_tensor(cgraph);
auto pos_shape = GgmlOvDecoder::get_shape(inp_pos);
// A freshly compiled model starts with an empty state, so it can only serve a
@@ -1048,6 +1292,22 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
}
}
if (stateful_recurrent) {
const auto * inp_pos = get_inp_pos_tensor(cgraph);
const int32_t pos_begin = static_cast<const int32_t *>(inp_pos->data)[0];
if (pos_begin == 0) {
infer_request->reset_state();
} else if (!cache_hit || old_m_params.kv_buffer_changed(m_params) || c_params.cache_rs_reset_len > 0 ||
pos_begin < 0 || static_cast<size_t>(pos_begin) != r_ctx->stateful_kv_size) {
GGML_LOG_ERROR(
"OpenVINO recurrent stateful execution cannot restore or rewind a sequence. Restart at position 0 "
"or disable GGML_OPENVINO_STATEFUL_EXECUTION.\n");
return GGML_STATUS_FAILED;
}
} else {
reset_single_slot_recurrent_cache(cgraph, m_params, c_params);
}
for (size_t i = 0; i < ov_input_names.size(); i++) {
const auto & param_name = ov_input_names[i];
auto input_tensor = get_ov_input_tensor(ggml_decoder, param_name);
@@ -1080,6 +1340,12 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
ov_raw_infer_start = ggml_time_us();
infer_request->infer();
infer_end_time = ggml_time_us();
if (stateful_recurrent) {
const auto * inp_pos = get_inp_pos_tensor(cgraph);
r_ctx->stateful_kv_size =
static_cast<const int32_t *>(inp_pos->data)[0] + get_inp_pos_n_tokens(cgraph, inp_pos);
}
dump_ov_profiling_info(*infer_request);
if (ggml_openvino_getenv_int("GGML_OPENVINO_DEBUG_OUTPUT") ||
ggml_openvino_getenv_str("GGML_OPENVINO_DEBUG_NODE")) {
@@ -1092,13 +1358,17 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
if (ggml_openvino_getenv_int("GGML_OPENVINO_PROFILING")) {
GGML_LOG_INFO("\nGGML OpenVINO Backend: \n");
GGML_LOG_INFO(" - Graph decoder time: %.3f ms \n", (decoder_end_time - start_time) / 1000.0);
GGML_LOG_INFO(" - cache key compute time: %.3f ms \n", cache_key_compute_time / 1000.0);
GGML_LOG_INFO(" - model split check time: %.3f ms \n", model_split_check_time / 1000.0);
GGML_LOG_INFO(" - graph param compute time: %.3f ms \n", graph_param_compute_time / 1000.0);
GGML_LOG_INFO(" - cache lookup time: %.3f ms \n", cache_lookup_time / 1000.0);
if (!cache_hit) {
GGML_LOG_INFO(" - Graph conversion time: %.3f ms \n",
(conversion_end_time - decoder_end_time) / 1000.0);
GGML_LOG_INFO(" - Graph compile time: %.3f ms \n", (compile_end_time - conversion_end_time) / 1000.0);
}
GGML_LOG_INFO(" - Graph inference time: %.3f ms \n", (infer_end_time - compile_end_time) / 1000.0);
GGML_LOG_INFO(" - OV raw infer time: %.3f ms \n", (infer_end_time - ov_raw_infer_start) / 1000.0);
GGML_LOG_INFO(" - OV raw infer time: %.3f ms \n", (infer_end_time - ov_raw_infer_start) / 1000.0);
}
}
@@ -1108,7 +1378,7 @@ enum ggml_status ov_graph_compute_dynamic(ggml_cgraph * cgraph, const std::share
// be reading host weights during conversion/compilation. Pin the shared compiled
// models across backend teardown; a later context can create its own request without
// reading the dropped pages. A new, uncached graph still fails fast above.
if (cache_hit && ggml_openvino_release_weights_enabled(device)) {
if (cache_hit && ggml_openvino_release_weights_enabled()) {
std::lock_guard<std::mutex> compile_lock(r_ctx->compiled_cache->mutex);
if (!ggml_openvino_weight_buffers_released()) {
ggml_openvino_release_weight_buffers();
@@ -1218,7 +1488,7 @@ enum ggml_status ov_graph_compute_static(ggml_cgraph * cgraph, const std::shared
ggml_decoder->m_is_prefill = is_prefill;
ggml_decoder->set_model_params(m_params);
ggml_decoder->set_compute_params(c_params);
if (old_m_params.kv_buffer_changed(m_params)) {
if (old_m_params.kv_buffer_changed(m_params) || !ggml_decoder->is_bound_to(cgraph)) {
ggml_decoder->update_io(cgraph);
}
ggml_decoder->add_extra_inputs();
@@ -1356,6 +1626,8 @@ enum ggml_status ov_graph_compute_static(ggml_cgraph * cgraph, const std::shared
}
}
reset_single_slot_recurrent_cache(cgraph, m_params, c_params);
if (is_prefill) {
auto inp_len = get_inp_pos_n_tokens(cgraph, inp_pos);
for (int chunk_index = 0; chunk_index * prefill_chunk_size < inp_len; chunk_index++) {
+29 -31
View File
@@ -1,8 +1,9 @@
#include "ggml-decoder.h"
#include "ggml-impl.h"
#include "ggml-openvino-extra.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
@@ -10,74 +11,71 @@
#include <openvino/runtime/infer_request.hpp>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// Local execution-cache key. A match still needs the ModelParams compatibility
// check; this key alone does not identify weights or a compiled model.
// Cache key for a translated/compiled graph. Node count plus the two end node names identify a
// graph during inference, where the same few graphs repeat for the whole session. The list of
// external input names below tells those apart more precisely, but it walks every node and every
// src slot, so it is built only when GGML_OPENVINO_FULL_GRAPH_KEY is set. Op tests run many small
// graphs that can share a node count and end names, and need the full key.
struct graph_key {
int n_nodes;
int n_leaves;
std::string first_node_name;
std::string last_node_name;
std::vector<std::string> input_src_names;
// Each entry: "<node_idx>:<src_idx>:<op_type>"
std::vector<std::string> input_srcs;
graph_key(const ggml_cgraph * cgraph) : n_nodes(cgraph->n_nodes) {
graph_key(const ggml_cgraph * cgraph, bool include_inputs = false) : n_nodes(cgraph->n_nodes), n_leaves(cgraph->n_leafs) {
if (n_nodes > 0) {
first_node_name = cgraph->nodes[0]->name;
last_node_name = cgraph->nodes[n_nodes - 1]->name;
}
std::unordered_map<const ggml_tensor *, std::string> names;
auto get_input_key_name = [&names](const ggml_cgraph * graph, const ggml_tensor * tensor) {
auto it = names.find(tensor);
if (it == names.end()) {
it = names.emplace(tensor, GgmlOvDecoder::get_tensor_name(graph, tensor)).first;
}
return it->second;
};
static const bool full_key = ggml_openvino_getenv_int("GGML_OPENVINO_FULL_GRAPH_KEY") != 0;
if (!full_key && !include_inputs) {
return;
}
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);
std::unordered_set<const ggml_tensor *> node_set;
node_set.reserve(cgraph->n_nodes);
for (int i = 0; i < cgraph->n_nodes; i++) {
node_set.insert(cgraph->nodes[i]);
}
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') {
if (src == nullptr || src->buffer == nullptr || node_set.count(src) || src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
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);
input_srcs.push_back(std::to_string(node_idx) + ":" + std::to_string(src_idx) + ":" +
GgmlOvDecoder::compute_op_type(node));
}
}
}
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;
return n_nodes == other.n_nodes && n_leaves == other.n_leaves &&
first_node_name == other.first_node_name &&
last_node_name == other.last_node_name && input_srcs == other.input_srcs;
}
};
struct graph_key_hash {
size_t operator()(const graph_key & key) const {
size_t hash = std::hash<int>{}(key.n_nodes);
hash ^= std::hash<int>{}(key.n_leaves) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
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);
for (const auto & s : key.input_srcs) {
hash ^= std::hash<std::string>{}(s) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
}
return hash;
}
@@ -674,9 +674,7 @@ struct vk_op_lightning_indexer_push_constants {
uint32_t n_kv;
uint32_t n_heads;
uint32_t n_tokens;
uint32_t n_streams;
uint32_t n_masks;
uint32_t dispatch_x;
uint32_t q_nb1;
uint32_t q_nb2;
uint32_t q_nb3;
+8 -14
View File
@@ -3654,15 +3654,9 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
ggml_vk_create_pipeline(device, device->pipeline_gated_linear_attn_f32, "gated_linear_attn_f32", gated_linear_attn_f32_len, gated_linear_attn_f32_data, "main", 6, sizeof(vk_op_gated_linear_attn_push_constants), {1, 1, 1}, {}, 1);
{
const bool li_subgroup = device->subgroup_arithmetic && device->subgroup_require_full_support;
const size_t li_len = li_subgroup ? lightning_indexer_subgroup_f32_len : lightning_indexer_f32_len;
const void * li_data = li_subgroup ? (const void *)lightning_indexer_subgroup_f32_data : (const void *)lightning_indexer_f32_data;
for (ggml_type k_type : lightning_indexer_k_types) {
const std::string name = "lightning_indexer_" + std::string(ggml_type_name(k_type)) + "_k_f32";
ggml_vk_create_pipeline(device, device->pipeline_lightning_indexer_f32[k_type], name.c_str(), li_len, li_data, "main", 5, sizeof(vk_op_lightning_indexer_push_constants), {1, 1, 1}, {(uint32_t)k_type, fa_block_bytes(k_type), device->subgroup_size}, 1, true, li_subgroup);
}
for (ggml_type k_type : lightning_indexer_k_types) {
const std::string name = "lightning_indexer_" + std::string(ggml_type_name(k_type)) + "_k_f32";
ggml_vk_create_pipeline(device, device->pipeline_lightning_indexer_f32[k_type], name.c_str(), lightning_indexer_f32_len, lightning_indexer_f32_data, "main", 5, sizeof(vk_op_lightning_indexer_push_constants), {1, 1, 1}, {(uint32_t)k_type, fa_block_bytes(k_type)}, 1, true);
}
{
@@ -10171,9 +10165,9 @@ void ggml_vk_lightning_indexer(ggml_backend_vk_context * ctx, vk_context& subctx
const uint32_t n_streams = q->ne[3];
const uint32_t n_masks = m->ne[3];
const uint32_t n_outputs = (uint32_t)(dst->ne[0] * dst->ne[1] * dst->ne[3]);
const uint32_t dispatch_x = std::min(n_outputs, ctx->device->properties.limits.maxComputeWorkGroupCount[0]);
const uint32_t dispatch_y = CEIL_DIV(n_outputs, dispatch_x);
// one workgroup per tile of 64 keys and 8 tokens, see lightning_indexer.comp
const uint32_t n_tiles_kv = CEIL_DIV(n_kv, 64);
const uint32_t n_tiles_t = CEIL_DIV(n_tokens, 8);
// q, w and dst are f32 and m is f16, so their strides are passed in elements;
// k may be quantized, so its strides stay in bytes
@@ -10190,7 +10184,7 @@ void ggml_vk_lightning_indexer(ggml_backend_vk_context * ctx, vk_context& subctx
const uint32_t d_nb3 = dst->nb[3] / sizeof(float);
const vk_op_lightning_indexer_push_constants pc = {
n_kv, n_heads, n_tokens, n_streams, n_masks, dispatch_x,
n_kv, n_heads, n_tokens, n_masks,
q_nb1, q_nb2, q_nb3,
k_nb2, k_nb3,
w_nb1, w_nb3,
@@ -10200,7 +10194,7 @@ void ggml_vk_lightning_indexer(ggml_backend_vk_context * ctx, vk_context& subctx
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
{ggml_vk_tensor_subbuffer(ctx, q), ggml_vk_tensor_subbuffer(ctx, k), ggml_vk_tensor_subbuffer(ctx, w), ggml_vk_tensor_subbuffer(ctx, m), ggml_vk_tensor_subbuffer(ctx, dst)},
pc, {dispatch_x, dispatch_y, 1});
pc, {n_tiles_kv, n_tiles_t, n_streams});
}
void ggml_vk_gated_delta_net(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst) {
@@ -3,10 +3,6 @@
#extension GL_EXT_control_flow_attributes : require
#extension GL_EXT_shader_16bit_storage : require
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
#extension GL_KHR_shader_subgroup_basic : enable
#if USE_SUBGROUP_ADD
#extension GL_KHR_shader_subgroup_arithmetic : enable
#endif
#define BINDING_IDX_K 0u
@@ -16,14 +12,19 @@
layout(constant_id = 0) const uint FaTypeK = GGML_TYPE_F32;
layout(constant_id = 1) const uint FaBlockBytesK = 4;
layout(constant_id = 2) const uint SUBGROUP_SIZE = 32;
#include "flash_attn_dequant.glsl"
// one workgroup computes one output element, one invocation per head element
// one workgroup scores a tile of BK keys against BT tokens: the keys are staged once,
// the queries one head at a time, and each invocation owns one key for TPI tokens
#define HEAD_SIZE 128
#define WG_SIZE 256
#define BK 64
#define BT 8
#define TS (WG_SIZE / BK)
#define TPI (BT / TS)
layout(local_size_x = HEAD_SIZE, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = WG_SIZE, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0) readonly buffer QBuf { float q[]; };
layout(binding = 1) readonly buffer KBufF16 { float16_t k_f16[]; };
@@ -37,9 +38,7 @@ layout(push_constant) uniform PushConstants {
uint n_kv;
uint n_heads;
uint n_tokens;
uint n_streams;
uint n_masks;
uint dispatch_x;
uint q_nb1;
uint q_nb2;
uint q_nb3;
@@ -53,99 +52,109 @@ layout(push_constant) uniform PushConstants {
uint d_nb3;
};
shared float k_row[HEAD_SIZE];
#if USE_SUBGROUP_ADD
shared float sg_partials[HEAD_SIZE / SUBGROUP_SIZE];
#else
shared float partials[HEAD_SIZE];
#endif
// the row padding keeps the keys of consecutive invocations in distinct banks
shared f16vec4 k_tile[BK][HEAD_SIZE / 4 + 1];
shared f16vec4 q_tile[BT][HEAD_SIZE / 4];
shared float w_tile[BT];
void main() {
const uint tid = gl_LocalInvocationID.x;
const uint output_idx = gl_WorkGroupID.y * dispatch_x + gl_WorkGroupID.x;
const uint n_outputs = n_kv * n_tokens * n_streams;
const uint ik0 = gl_WorkGroupID.x * BK;
const uint t0 = gl_WorkGroupID.y * BT;
const uint s = gl_WorkGroupID.z;
if (fa_type_needs_shmem(FaTypeK)) {
init_iq_shmem(gl_WorkGroupSize);
}
if (output_idx >= n_outputs) {
return;
}
const uint ik = output_idx % n_kv;
const uint ts = output_idx / n_kv;
const uint t = ts % n_tokens;
const uint s = ts / n_tokens;
const uint k_offset = ik * k_nb2 + s * k_nb3;
// k strides come in as bytes, so scale them down to the view being indexed
const uint k_block_elems = fa_block_elems(FaTypeK);
const uint k_elem_bytes = FaBlockBytesK / k_block_elems;
if (FaTypeK == GGML_TYPE_F16) {
k_row[tid] = float(k_f16[k_offset / k_elem_bytes + tid]);
} else if (FaTypeK == GGML_TYPE_F32) {
k_row[tid] = k_f32[k_offset / k_elem_bytes + tid];
} else if (FaTypeK == GGML_TYPE_BF16) {
k_row[tid] = bf16_to_fp32(uint(k_bf16[k_offset / k_elem_bytes + tid]));
} else if (4 * tid < HEAD_SIZE) {
const uint coord = 4 * tid;
const uint ib = coord / k_block_elems;
const uint iqs = coord % k_block_elems;
const vec4 values = dequantize4(ib, iqs, k_offset / FaBlockBytesK, BINDING_IDX_K);
k_row[coord + 0] = values.x;
k_row[coord + 1] = values.y;
k_row[coord + 2] = values.z;
k_row[coord + 3] = values.w;
// stage the key tile four elements at a time, rows past n_kv are zero
[[unroll]] for (uint i = tid; i < BK * HEAD_SIZE / 4; i += WG_SIZE) {
const uint r = i / (HEAD_SIZE / 4);
const uint c4 = i % (HEAD_SIZE / 4);
vec4 v = vec4(0.0);
if (ik0 + r < n_kv) {
const uint k_offset = (ik0 + r) * k_nb2 + s * k_nb3;
const uint e = k_offset / k_elem_bytes + c4 * 4;
if (FaTypeK == GGML_TYPE_F16) {
v = vec4(k_f16[e], k_f16[e + 1], k_f16[e + 2], k_f16[e + 3]);
} else if (FaTypeK == GGML_TYPE_F32) {
v = vec4(k_f32[e], k_f32[e + 1], k_f32[e + 2], k_f32[e + 3]);
} else if (FaTypeK == GGML_TYPE_BF16) {
v = bf16_to_fp32(uvec4(k_bf16[e], k_bf16[e + 1], k_bf16[e + 2], k_bf16[e + 3]));
} else {
v = dequantize4((c4 * 4) / k_block_elems, (c4 * 4) % k_block_elems, k_offset / FaBlockBytesK, BINDING_IDX_K);
}
}
k_tile[r][c4] = f16vec4(v);
}
barrier();
const float k_val = k_row[tid];
const uint kl = tid % BK;
const uint tl = tid / BK;
float score[TPI];
[[unroll]] for (uint j = 0; j < TPI; ++j) {
score[j] = 0.0;
}
float score = 0.0;
for (uint h = 0; h < n_heads; ++h) {
const float prod = q[h * q_nb1 + t * q_nb2 + s * q_nb3 + tid] * k_val;
#if USE_SUBGROUP_ADD
const float sg_sum = subgroupAdd(prod);
if (gl_SubgroupInvocationID == 0) {
sg_partials[gl_SubgroupID] = sg_sum;
}
// the previous head is fully consumed and, on the first pass, the key tile is complete
barrier();
if (tid == 0) {
float sum = 0.0;
[[unroll]] for (uint i = 0; i < HEAD_SIZE / SUBGROUP_SIZE; ++i) {
sum += sg_partials[i];
[[unroll]] for (uint i = tid; i < BT * HEAD_SIZE / 4; i += WG_SIZE) {
const uint r = i / (HEAD_SIZE / 4);
const uint c4 = i % (HEAD_SIZE / 4);
const uint t = t0 + r;
vec4 v = vec4(0.0);
if (t < n_tokens) {
const uint q_base = h * q_nb1 + t * q_nb2 + s * q_nb3 + c4 * 4;
v = vec4(q[q_base], q[q_base + 1], q[q_base + 2], q[q_base + 3]);
}
score += max(sum, 0.0) * weights[h + t * w_nb1 + s * w_nb3];
q_tile[r][c4] = f16vec4(v);
}
// the reads above must complete before the next iteration overwrites sg_partials
barrier();
#else
partials[tid] = prod;
if (tid < BT) {
const uint t = t0 + tid;
w_tile[tid] = t < n_tokens ? weights[h + t * w_nb1 + s * w_nb3] : 0.0;
}
barrier();
[[unroll]] for (uint stride = HEAD_SIZE / 2; stride > 0; stride >>= 1) {
if (tid < stride) {
partials[tid] += partials[tid + stride];
float qk[TPI];
[[unroll]] for (uint j = 0; j < TPI; ++j) {
qk[j] = 0.0;
}
[[unroll]] for (uint c4 = 0; c4 < HEAD_SIZE / 4; ++c4) {
const f16vec4 kv = k_tile[kl][c4];
[[unroll]] for (uint j = 0; j < TPI; ++j) {
const f16vec4 qv = q_tile[tl + j * TS][c4];
qk[j] += float(dot(kv, qv));
}
barrier();
}
if (tid == 0) {
score += max(partials[0], 0.0) * weights[h + t * w_nb1 + s * w_nb3];
[[unroll]] for (uint j = 0; j < TPI; ++j) {
score[j] += max(qk[j], 0.0) * w_tile[tl + j * TS];
}
// the read of partials[0] above must complete before the next iteration
// overwrites partials[tid]
barrier();
#endif
}
if (tid == 0) {
const uint mask_offset = ik + t * m_nb1 + (s % n_masks) * m_nb3;
dst[ik + t * d_nb1 + s * d_nb3] = score + float(mask[mask_offset]);
const uint ik = ik0 + kl;
if (ik >= n_kv) {
return;
}
[[unroll]] for (uint j = 0; j < TPI; ++j) {
const uint t = t0 + tl + j * TS;
if (t < n_tokens) {
const uint mask_offset = ik + t * m_nb1 + (s % n_masks) * m_nb3;
dst[ik + t * d_nb1 + s * d_nb3] = score[j] + float(mask[mask_offset]);
}
}
}
@@ -1149,7 +1149,6 @@ void process_shaders() {
// K quant type is selected at runtime via the FaTypeK spec constant.
std::map<std::string, std::string> li_dict = {{"FLOAT_TYPE", "float"}, {"FLOAT_TYPEV4", "vec4"}, {"DATA_A_IQ4_NL", "1"}};
string_to_spv("lightning_indexer_f32", "lightning_indexer.comp", li_dict);
string_to_spv("lightning_indexer_subgroup_f32", "lightning_indexer.comp", merge_maps(li_dict, {{"USE_SUBGROUP_ADD", "1"}}));
string_to_spv("rwkv_wkv7_f32", "wkv7.comp", merge_maps(base_dict, {{"A_TYPE", "float"}}));
@@ -179,20 +179,22 @@ struct ggml_webgpu_argsort_shader_lib_context {
/** Set Rows **/
struct ggml_webgpu_set_rows_pipeline_key {
int src0_type;
int dst_type;
int vec4;
int i64_idx;
int pair_blocks;
bool operator==(const ggml_webgpu_set_rows_pipeline_key & other) const {
return dst_type == other.dst_type && vec4 == other.vec4 && i64_idx == other.i64_idx &&
pair_blocks == other.pair_blocks;
return src0_type == other.src0_type && dst_type == other.dst_type && vec4 == other.vec4 &&
i64_idx == other.i64_idx && pair_blocks == other.pair_blocks;
}
};
struct ggml_webgpu_set_rows_pipeline_key_hash {
size_t operator()(const ggml_webgpu_set_rows_pipeline_key & key) const {
size_t seed = 0;
ggml_webgpu_hash_combine(seed, key.src0_type);
ggml_webgpu_hash_combine(seed, key.dst_type);
ggml_webgpu_hash_combine(seed, key.vec4);
ggml_webgpu_hash_combine(seed, key.i64_idx);
@@ -1387,9 +1389,10 @@ class ggml_webgpu_shader_lib {
webgpu_pipeline get_set_rows_pipeline(const ggml_webgpu_shader_lib_context & context) {
const bool quantized = ggml_is_quantized(context.dst->type);
ggml_webgpu_set_rows_pipeline_key key = {};
key.src0_type = context.src0->type;
key.dst_type = context.dst->type;
key.vec4 =
(context.dst->type == GGML_TYPE_F32 || context.dst->type == GGML_TYPE_F16) && context.src0->ne[0] % 4 == 0;
key.vec4 = (context.dst->type == GGML_TYPE_F32 || context.dst->type == GGML_TYPE_F16) &&
context.src0->type == GGML_TYPE_F32 && context.src0->ne[0] % 4 == 0;
key.i64_idx = context.src1->type == GGML_TYPE_I64;
key.pair_blocks = quantized && ((context.src0->ne[0] / ggml_blck_size(context.dst->type)) % 2 == 0);
@@ -1422,6 +1425,11 @@ class ggml_webgpu_shader_lib {
GGML_ABORT("Unsupported dst type for set_rows shader");
}
if (context.src0->type == GGML_TYPE_F16) {
defines.push_back("TYPE_F16");
variant += "_src0_f16";
}
if (key.vec4) {
defines.push_back("VEC4");
variant += "_vec4";
+3 -2
View File
@@ -4431,7 +4431,8 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
case GGML_OP_SET_ROWS:
supports_op = ((op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_Q8_0 ||
op->type == GGML_TYPE_Q4_0) &&
src0->type == GGML_TYPE_F32 && (src1->type == GGML_TYPE_I64 || src1->type == GGML_TYPE_I32));
(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16) &&
(src1->type == GGML_TYPE_I64 || src1->type == GGML_TYPE_I32));
break;
case GGML_OP_GET_ROWS:
if (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16 ||
@@ -4701,7 +4702,7 @@ static bool ggml_backend_webgpu_device_supports_op(ggml_backend_dev_t dev, const
supports_op = (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && (src0->type == op->type);
break;
case GGML_OP_FILL:
supports_op = op->type == GGML_TYPE_F32 && src0->type == GGML_TYPE_F32;
supports_op = (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && (src0->type == op->type);
break;
case GGML_OP_LOG:
supports_op = (op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16) && (src0->type == op->type);
@@ -11,7 +11,11 @@ enable f16;
#define DST_TYPE vec4<DST_INNER_TYPE>
#define VEC_SIZE 4
#else
#ifdef TYPE_F16
#define SRC_TYPE f16
#else
#define SRC_TYPE f32
#endif
#define DST_TYPE DST_INNER_TYPE
#define VEC_SIZE 1
#endif
@@ -1,3 +1,5 @@
enable f16;
#ifdef DST_Q8_0
#define BLOCK_SIZE 32u
#define BLOCK_BYTES 34u
@@ -8,8 +10,14 @@
#define QS_WORDS 4u
#endif
#ifdef TYPE_F16
#define SRC_TYPE f16
#else
#define SRC_TYPE f32
#endif
@group(0) @binding(0)
var<storage, read_write> src: array<f32>;
var<storage, read_write> src: array<SRC_TYPE>;
@group(0) @binding(1)
var<storage, read_write> idx: array<u32>;
@@ -112,7 +120,7 @@ fn quantize_block_params(src_block: u32) -> vec2<f32> {
#ifdef DST_Q8_0
var amax = 0.0;
for (var j: u32 = 0u; j < BLOCK_SIZE; j++) {
amax = max(amax, abs(src[src_block + j]));
amax = max(amax, abs(f32(src[src_block + j])));
}
let d = amax / 127.0;
@@ -122,7 +130,7 @@ fn quantize_block_params(src_block: u32) -> vec2<f32> {
var amax = 0.0;
var max_val = 0.0;
for (var j: u32 = 0u; j < BLOCK_SIZE; j++) {
let v = src[src_block + j];
let v = f32(src[src_block + j]);
let av = abs(v);
if (amax < av) {
amax = av;
@@ -139,15 +147,15 @@ fn quantize_block_params(src_block: u32) -> vec2<f32> {
fn quantize_block_word(src_block: u32, j: u32, id: f32) -> u32 {
#ifdef DST_Q8_0
let base = src_block + j * 4u;
return (u32(i32(round(src[base + 0u] * id)) & 0xFF) << 0u) |
(u32(i32(round(src[base + 1u] * id)) & 0xFF) << 8u) |
(u32(i32(round(src[base + 2u] * id)) & 0xFF) << 16u) |
(u32(i32(round(src[base + 3u] * id)) & 0xFF) << 24u);
return (u32(i32(round(f32(src[base + 0u]) * id)) & 0xFF) << 0u) |
(u32(i32(round(f32(src[base + 1u]) * id)) & 0xFF) << 8u) |
(u32(i32(round(f32(src[base + 2u]) * id)) & 0xFF) << 16u) |
(u32(i32(round(f32(src[base + 3u]) * id)) & 0xFF) << 24u);
#elif defined(DST_Q4_0)
var packed_q = 0u;
for (var k: u32 = 0u; k < 4u; k++) {
let x0 = src[src_block + j * 4u + k] * id;
let x1 = src[src_block + 16u + j * 4u + k] * id;
let x0 = f32(src[src_block + j * 4u + k]) * id;
let x1 = f32(src[src_block + 16u + j * 4u + k]) * id;
let q0 = u32(clamp(i32(x0 + 8.5), 0, 15));
let q1 = u32(clamp(i32(x1 + 8.5), 0, 15));
packed_q |= (q0 & 0xFu) << (8u * k);
+87
View File
@@ -324,6 +324,8 @@ class Keys:
TYPE = "{arch}.decision.type"
# note: single-use-case keys can be hard-coded in cpp code
BLOCK_COUNT = "{arch}.decision.block_count"
ROUTING_BLOCK_COUNT = "{arch}.decision.routing_block_count"
HEAD_COUNT = "{arch}.decision.head_count"
MAX_HEAD_TOKENS = "{arch}.decision.max_head_tokens"
TEMPERATURE = "{arch}.decision.temperature.{name}" # name: "<type>" or "<type>.<n_opt bucket>"
@@ -537,6 +539,7 @@ class MODEL_ARCH(IntEnum):
QWEN3VLMOE = auto()
QWEN35 = auto()
QWEN35MOE = auto()
CLEF = auto()
QWEN4EXP = auto()
PHI2 = auto()
PHI3 = auto()
@@ -861,6 +864,7 @@ class MODEL_TENSOR(IntEnum):
DEC_ATTN_OUT = auto()
DEC_ATTN_REL_B = auto()
DEC_CROSS_ATTN_NORM = auto()
DEC_CROSS_ATTN_NORM_KV = auto()
DEC_CROSS_ATTN_Q = auto()
DEC_CROSS_ATTN_K = auto()
DEC_CROSS_ATTN_V = auto()
@@ -885,6 +889,19 @@ class MODEL_TENSOR(IntEnum):
CLS = auto() # classifier
CLS_OUT = auto() # classifier output projection
CLS_NORM = auto()
DECISION_HIDDEN_NORM = auto()
DECISION_PROJ_MEMORY = auto()
DECISION_PROJ_QUESTION = auto()
DECISION_PROJ_OPTION_QUESTION = auto()
DECISION_PROJ_GLOBAL = auto()
DECISION_PROJ_OPTION_CONTEXT = auto()
DECISION_PROJ_OPTION_LEXICAL = auto()
DECISION_OPTION_SUMMARY_NORM = auto()
DECISION_FIELD_NORM = auto()
DECISION_OPTION_NORM = auto()
DECISION_SCALES = auto()
DECISION_SCORER = auto()
DECISION_SCORER_OUT = auto()
CONV1D = auto()
CONVNEXT_DW = auto()
CONVNEXT_NORM = auto()
@@ -1301,6 +1318,7 @@ MODEL_ARCH_NAMES: dict[MODEL_ARCH, str] = {
MODEL_ARCH.QWEN3VLMOE: "qwen3vlmoe",
MODEL_ARCH.QWEN35: "qwen35",
MODEL_ARCH.QWEN35MOE: "qwen35moe",
MODEL_ARCH.CLEF: "clef",
MODEL_ARCH.QWEN4EXP: "qwen4exp",
MODEL_ARCH.PHI2: "phi2",
MODEL_ARCH.PHI3: "phi3",
@@ -1624,6 +1642,7 @@ TENSOR_NAMES: dict[MODEL_TENSOR, str] = {
MODEL_TENSOR.DEC_ATTN_OUT: "dec.blk.{bid}.attn_o",
MODEL_TENSOR.DEC_ATTN_REL_B: "dec.blk.{bid}.attn_rel_b",
MODEL_TENSOR.DEC_CROSS_ATTN_NORM: "dec.blk.{bid}.cross_attn_norm",
MODEL_TENSOR.DEC_CROSS_ATTN_NORM_KV: "dec.blk.{bid}.cross_attn_norm_kv",
MODEL_TENSOR.DEC_CROSS_ATTN_Q: "dec.blk.{bid}.cross_attn_q",
MODEL_TENSOR.DEC_CROSS_ATTN_K: "dec.blk.{bid}.cross_attn_k",
MODEL_TENSOR.DEC_CROSS_ATTN_V: "dec.blk.{bid}.cross_attn_v",
@@ -1648,6 +1667,19 @@ TENSOR_NAMES: dict[MODEL_TENSOR, str] = {
MODEL_TENSOR.CLS: "cls",
MODEL_TENSOR.CLS_OUT: "cls.output",
MODEL_TENSOR.CLS_NORM: "cls.norm",
MODEL_TENSOR.DECISION_HIDDEN_NORM: "decision.hidden_norm",
MODEL_TENSOR.DECISION_PROJ_MEMORY: "decision.proj_memory",
MODEL_TENSOR.DECISION_PROJ_QUESTION: "decision.proj_question",
MODEL_TENSOR.DECISION_PROJ_OPTION_QUESTION: "decision.proj_option_question",
MODEL_TENSOR.DECISION_PROJ_GLOBAL: "decision.proj_global",
MODEL_TENSOR.DECISION_PROJ_OPTION_CONTEXT: "decision.proj_option_context",
MODEL_TENSOR.DECISION_PROJ_OPTION_LEXICAL: "decision.proj_option_lexical",
MODEL_TENSOR.DECISION_OPTION_SUMMARY_NORM: "decision.option_summary_norm",
MODEL_TENSOR.DECISION_FIELD_NORM: "decision.field_norm",
MODEL_TENSOR.DECISION_OPTION_NORM: "decision.option_norm",
MODEL_TENSOR.DECISION_SCALES: "decision.scales",
MODEL_TENSOR.DECISION_SCORER: "decision.scorer",
MODEL_TENSOR.DECISION_SCORER_OUT: "decision.scorer_out",
MODEL_TENSOR.CONV1D: "conv1d",
MODEL_TENSOR.CONVNEXT_DW: "convnext.{bid}.dw",
MODEL_TENSOR.CONVNEXT_NORM: "convnext.{bid}.norm",
@@ -2918,6 +2950,60 @@ MODEL_TENSORS: dict[MODEL_ARCH, list[MODEL_TENSOR]] = {
MODEL_TENSOR.NEXTN_SHARED_HEAD_HEAD,
MODEL_TENSOR.NEXTN_SHARED_HEAD_NORM,
],
MODEL_ARCH.CLEF: [
MODEL_TENSOR.TOKEN_EMBD,
MODEL_TENSOR.OUTPUT_NORM,
MODEL_TENSOR.OUTPUT,
MODEL_TENSOR.ATTN_NORM,
MODEL_TENSOR.ATTN_Q,
MODEL_TENSOR.ATTN_Q_NORM,
MODEL_TENSOR.ATTN_K,
MODEL_TENSOR.ATTN_K_NORM,
MODEL_TENSOR.ATTN_V,
MODEL_TENSOR.ATTN_OUT,
MODEL_TENSOR.ATTN_POST_NORM,
MODEL_TENSOR.ATTN_GATE,
MODEL_TENSOR.ATTN_QKV,
MODEL_TENSOR.FFN_GATE,
MODEL_TENSOR.FFN_DOWN,
MODEL_TENSOR.FFN_UP,
MODEL_TENSOR.SSM_A,
MODEL_TENSOR.SSM_CONV1D,
MODEL_TENSOR.SSM_DT,
MODEL_TENSOR.SSM_NORM,
MODEL_TENSOR.SSM_BETA,
MODEL_TENSOR.SSM_ALPHA,
MODEL_TENSOR.SSM_OUT,
# decision head
MODEL_TENSOR.TOKEN_TYPES,
MODEL_TENSOR.DEC_ATTN_NORM,
MODEL_TENSOR.DEC_ATTN_Q,
MODEL_TENSOR.DEC_ATTN_K,
MODEL_TENSOR.DEC_ATTN_V,
MODEL_TENSOR.DEC_ATTN_OUT,
MODEL_TENSOR.DEC_CROSS_ATTN_NORM,
MODEL_TENSOR.DEC_CROSS_ATTN_NORM_KV,
MODEL_TENSOR.DEC_CROSS_ATTN_Q,
MODEL_TENSOR.DEC_CROSS_ATTN_K,
MODEL_TENSOR.DEC_CROSS_ATTN_V,
MODEL_TENSOR.DEC_CROSS_ATTN_OUT,
MODEL_TENSOR.DEC_FFN_NORM,
MODEL_TENSOR.DEC_FFN_DOWN,
MODEL_TENSOR.DEC_FFN_UP,
MODEL_TENSOR.DECISION_HIDDEN_NORM,
MODEL_TENSOR.DECISION_PROJ_MEMORY,
MODEL_TENSOR.DECISION_PROJ_QUESTION,
MODEL_TENSOR.DECISION_PROJ_OPTION_QUESTION,
MODEL_TENSOR.DECISION_PROJ_GLOBAL,
MODEL_TENSOR.DECISION_PROJ_OPTION_CONTEXT,
MODEL_TENSOR.DECISION_PROJ_OPTION_LEXICAL,
MODEL_TENSOR.DECISION_OPTION_SUMMARY_NORM,
MODEL_TENSOR.DECISION_FIELD_NORM,
MODEL_TENSOR.DECISION_OPTION_NORM,
MODEL_TENSOR.DECISION_SCALES,
MODEL_TENSOR.DECISION_SCORER,
MODEL_TENSOR.DECISION_SCORER_OUT,
],
MODEL_ARCH.QWEN35MOE: [
MODEL_TENSOR.TOKEN_EMBD,
MODEL_TENSOR.OUTPUT_NORM,
@@ -5921,6 +6007,7 @@ class DecisionType:
LEV = "lev" # same as openjev, noul is read from a rating scale
KEV = "kev" # dot product of the hidden states of the last token and of one end token per option
NIMBLE = "nimble" # same as openjev, the prompt lists all the questions of the request
CLEF = "clef" # joint head over all questions, one score per option
class VisionProjectorType:
+6
View File
@@ -1346,6 +1346,12 @@ class GGUFWriter:
def add_decision_block_count(self, value: int) -> None:
self.add_uint32(Keys.Decision.BLOCK_COUNT.format(arch=self.arch), value)
def add_decision_routing_block_count(self, value: int) -> None:
self.add_uint32(Keys.Decision.ROUTING_BLOCK_COUNT.format(arch=self.arch), value)
def add_decision_head_count(self, value: int) -> None:
self.add_uint32(Keys.Decision.HEAD_COUNT.format(arch=self.arch), value)
def add_decision_max_head_tokens(self, value: int) -> None:
self.add_uint32(Keys.Decision.MAX_HEAD_TOKENS.format(arch=self.arch), value)
+74
View File
@@ -49,6 +49,7 @@ class TensorNameMap:
MODEL_TENSOR.TOKEN_TYPES: (
"embeddings.token_type_embeddings", # bert nomic-bert
"type_emb", # laya
"joint_head.type_embedding", # clef
),
# Normalization of token embeddings
@@ -1181,22 +1182,27 @@ class TensorNameMap:
MODEL_TENSOR.DEC_ATTN_NORM: (
"decoder.block.{bid}.layer.0.layer_norm", # t5
"joint_head.layers.{bid}.norm1", # clef
),
MODEL_TENSOR.DEC_ATTN_Q: (
"decoder.block.{bid}.layer.0.SelfAttention.q", # t5
"joint_head.layers.{bid}.self_attn.q", # clef
),
MODEL_TENSOR.DEC_ATTN_K: (
"decoder.block.{bid}.layer.0.SelfAttention.k", # t5
"joint_head.layers.{bid}.self_attn.k", # clef
),
MODEL_TENSOR.DEC_ATTN_V: (
"decoder.block.{bid}.layer.0.SelfAttention.v", # t5
"joint_head.layers.{bid}.self_attn.v", # clef
),
MODEL_TENSOR.DEC_ATTN_OUT: (
"decoder.block.{bid}.layer.0.SelfAttention.o", # t5
"joint_head.layers.{bid}.self_attn.out_proj", # clef
),
MODEL_TENSOR.DEC_ATTN_REL_B: (
@@ -1205,22 +1211,32 @@ class TensorNameMap:
MODEL_TENSOR.DEC_CROSS_ATTN_NORM: (
"decoder.block.{bid}.layer.1.layer_norm", # t5
"joint_head.layers.{bid}.norm2", # clef
"joint_head.evidence_layers.{bid}.query_norm", # clef
),
MODEL_TENSOR.DEC_CROSS_ATTN_Q: (
"decoder.block.{bid}.layer.1.EncDecAttention.q", # t5
"joint_head.layers.{bid}.multihead_attn.q", # clef
"joint_head.evidence_layers.{bid}.attention.q", # clef
),
MODEL_TENSOR.DEC_CROSS_ATTN_K: (
"decoder.block.{bid}.layer.1.EncDecAttention.k", # t5
"joint_head.layers.{bid}.multihead_attn.k", # clef
"joint_head.evidence_layers.{bid}.attention.k", # clef
),
MODEL_TENSOR.DEC_CROSS_ATTN_V: (
"decoder.block.{bid}.layer.1.EncDecAttention.v", # t5
"joint_head.layers.{bid}.multihead_attn.v", # clef
"joint_head.evidence_layers.{bid}.attention.v", # clef
),
MODEL_TENSOR.DEC_CROSS_ATTN_OUT: (
"decoder.block.{bid}.layer.1.EncDecAttention.o", # t5
"joint_head.layers.{bid}.multihead_attn.out_proj", # clef
"joint_head.evidence_layers.{bid}.attention.out_proj", # clef
),
MODEL_TENSOR.DEC_CROSS_ATTN_REL_B: (
@@ -1229,6 +1245,8 @@ class TensorNameMap:
MODEL_TENSOR.DEC_FFN_NORM: (
"decoder.block.{bid}.layer.2.layer_norm", # t5
"joint_head.layers.{bid}.norm3", # clef
"joint_head.evidence_layers.{bid}.feedforward_norm", # clef
),
MODEL_TENSOR.DEC_FFN_GATE: (
@@ -1238,16 +1256,72 @@ class TensorNameMap:
MODEL_TENSOR.DEC_FFN_UP: (
"decoder.block.{bid}.layer.2.DenseReluDense.wi", # t5
"decoder.block.{bid}.layer.2.DenseReluDense.wi_1", # flan-t5
"joint_head.layers.{bid}.linear1", # clef
"joint_head.evidence_layers.{bid}.feedforward.0", # clef
),
MODEL_TENSOR.DEC_FFN_DOWN: (
"decoder.block.{bid}.layer.2.DenseReluDense.wo", # t5
"joint_head.layers.{bid}.linear2", # clef
"joint_head.evidence_layers.{bid}.feedforward.3", # clef
),
MODEL_TENSOR.DEC_OUTPUT_NORM: (
"decoder.final_layer_norm", # t5
),
MODEL_TENSOR.DEC_CROSS_ATTN_NORM_KV: (
"joint_head.evidence_layers.{bid}.memory_norm", # clef
),
MODEL_TENSOR.DECISION_HIDDEN_NORM: (
"joint_head.hidden_norm", # clef
),
MODEL_TENSOR.DECISION_PROJ_MEMORY: (
"joint_head.memory_projection", # clef
),
MODEL_TENSOR.DECISION_PROJ_QUESTION: (
"joint_head.question_projection", # clef
),
MODEL_TENSOR.DECISION_PROJ_OPTION_QUESTION: (
"joint_head.option_question_projection", # clef
),
MODEL_TENSOR.DECISION_PROJ_GLOBAL: (
"joint_head.global_projection", # clef
),
MODEL_TENSOR.DECISION_PROJ_OPTION_CONTEXT: (
"joint_head.option_context_projection", # clef
),
MODEL_TENSOR.DECISION_PROJ_OPTION_LEXICAL: (
"joint_head.option_lexical_projection", # clef
),
MODEL_TENSOR.DECISION_OPTION_SUMMARY_NORM: (
"joint_head.option_summary_norm", # clef
),
MODEL_TENSOR.DECISION_FIELD_NORM: (
"joint_head.field_norm", # clef
),
MODEL_TENSOR.DECISION_OPTION_NORM: (
"joint_head.option_norm", # clef
),
MODEL_TENSOR.DECISION_SCORER: (
"joint_head.residual_scorer.0", # clef
),
MODEL_TENSOR.DECISION_SCORER_OUT: (
"joint_head.residual_scorer.3", # clef
),
MODEL_TENSOR.ENC_ATTN_NORM: (
"encoder.block.{bid}.layer.0.layer_norm", # t5
),
+1 -1
View File
@@ -5,7 +5,7 @@ import os
import sys
import subprocess
HTTPLIB_VERSION = "refs/tags/v0.58.0"
HTTPLIB_VERSION = "refs/tags/v0.59.0"
# used by examples/gguf-hash, these repos have no release tag, so we pin a commit
XXHASH_COMMIT = "9f465f1ea932d6ad9a26cd77496311ffa544cd68"
+32 -1
View File
@@ -40,6 +40,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
{ LLM_ARCH_QWEN3VLMOE, "qwen3vlmoe" },
{ LLM_ARCH_QWEN35, "qwen35" },
{ LLM_ARCH_QWEN35MOE, "qwen35moe" },
{ LLM_ARCH_CLEF, "clef" },
{ LLM_ARCH_QWEN4EXP, "qwen4exp" },
{ LLM_ARCH_PHI2, "phi2" },
{ LLM_ARCH_PHI3, "phi3" },
@@ -366,7 +367,9 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
{ LLM_KV_CLASSIFIER_OUTPUT_LABELS, "%s.classifier.output_labels" },
{ LLM_KV_CLASSIFIER_POOLING_TYPE, "%s.classifier.pooling_type" },
{ LLM_KV_DECISION_BLOCK_COUNT, "%s.decision.block_count" },
{ LLM_KV_DECISION_BLOCK_COUNT, "%s.decision.block_count" },
{ LLM_KV_DECISION_ROUTING_BLOCK_COUNT, "%s.decision.routing_block_count" },
{ LLM_KV_DECISION_HEAD_COUNT, "%s.decision.head_count" },
{ LLM_KV_TARGET_LAYERS, "%s.target_layers" },
{ LLM_KV_TARGET_HIDDEN_SIZE, "%s.target_hidden_size" },
@@ -490,6 +493,19 @@ static const std::map<llm_tensor, const char *> LLM_TENSOR_NAMES = {
{ LLM_TENSOR_CLS, "cls" },
{ LLM_TENSOR_CLS_OUT, "cls.output" },
{ LLM_TENSOR_CLS_NORM, "cls.norm" },
{ LLM_TENSOR_DECISION_HIDDEN_NORM, "decision.hidden_norm" },
{ LLM_TENSOR_DECISION_PROJ_MEMORY, "decision.proj_memory" },
{ LLM_TENSOR_DECISION_PROJ_QUESTION, "decision.proj_question" },
{ LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION, "decision.proj_option_question" },
{ LLM_TENSOR_DECISION_PROJ_GLOBAL, "decision.proj_global" },
{ LLM_TENSOR_DECISION_PROJ_OPTION_CONTEXT, "decision.proj_option_context" },
{ LLM_TENSOR_DECISION_PROJ_OPTION_LEXICAL, "decision.proj_option_lexical" },
{ LLM_TENSOR_DECISION_OPTION_SUMMARY_NORM, "decision.option_summary_norm" },
{ LLM_TENSOR_DECISION_FIELD_NORM, "decision.field_norm" },
{ LLM_TENSOR_DECISION_OPTION_NORM, "decision.option_norm" },
{ LLM_TENSOR_DECISION_SCALES, "decision.scales" },
{ LLM_TENSOR_DECISION_SCORER, "decision.scorer" },
{ LLM_TENSOR_DECISION_SCORER_OUT, "decision.scorer_out" },
{ LLM_TENSOR_ENC_OUTPUT_NORM, "enc.output_norm" },
{ LLM_TENSOR_FFN_GATE_INP_SHEXP, "blk.%d.ffn_gate_inp_shexp" },
{ LLM_TENSOR_SSM_A_NOSCAN, "blk.%d.ssm_a" },
@@ -606,6 +622,7 @@ static const std::map<llm_tensor, const char *> LLM_TENSOR_NAMES = {
{ LLM_TENSOR_DEC_ATTN_OUT, "dec.blk.%d.attn_o" },
{ LLM_TENSOR_DEC_ATTN_REL_B, "dec.blk.%d.attn_rel_b" },
{ LLM_TENSOR_DEC_CROSS_ATTN_NORM, "dec.blk.%d.cross_attn_norm" },
{ LLM_TENSOR_DEC_CROSS_ATTN_NORM_KV, "dec.blk.%d.cross_attn_norm_kv" },
{ LLM_TENSOR_DEC_CROSS_ATTN_Q, "dec.blk.%d.cross_attn_q" },
{ LLM_TENSOR_DEC_CROSS_ATTN_K, "dec.blk.%d.cross_attn_k" },
{ LLM_TENSOR_DEC_CROSS_ATTN_V, "dec.blk.%d.cross_attn_v" },
@@ -739,6 +756,19 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
{LLM_TENSOR_CLS, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_CLS_OUT, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_CLS_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_HIDDEN_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_PROJ_MEMORY, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_PROJ_QUESTION, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_PROJ_GLOBAL, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_PROJ_OPTION_CONTEXT, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_PROJ_OPTION_LEXICAL, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_OPTION_SUMMARY_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_FIELD_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_OPTION_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_SCALES, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
{LLM_TENSOR_DECISION_SCORER, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DECISION_SCORER_OUT, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}},
{LLM_TENSOR_DENSE_2_OUT, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}}, // Dense layer output
{LLM_TENSOR_DENSE_3_OUT, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL_MAT}}, // Dense layer output
{LLM_TENSOR_OUTPUT_NORM, {LLM_TENSOR_LAYER_OUTPUT, GGML_OP_MUL}},
@@ -908,6 +938,7 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
{LLM_TENSOR_FFN_SUB_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_DEC_ATTN_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_DEC_CROSS_ATTN_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_DEC_CROSS_ATTN_NORM_KV, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_DEC_FFN_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_ENC_ATTN_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
{LLM_TENSOR_ENC_FFN_NORM, {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
+17
View File
@@ -45,6 +45,7 @@ enum llm_arch {
LLM_ARCH_QWEN3VLMOE,
LLM_ARCH_QWEN35,
LLM_ARCH_QWEN35MOE,
LLM_ARCH_CLEF,
LLM_ARCH_QWEN4EXP,
LLM_ARCH_PHI2,
LLM_ARCH_PHI3,
@@ -413,6 +414,8 @@ enum llm_kv {
LLM_KV_CLASSIFIER_POOLING_TYPE,
LLM_KV_DECISION_BLOCK_COUNT,
LLM_KV_DECISION_ROUTING_BLOCK_COUNT,
LLM_KV_DECISION_HEAD_COUNT,
LLM_KV_TARGET_LAYERS,
LLM_KV_TARGET_HIDDEN_SIZE,
@@ -636,6 +639,7 @@ enum llm_tensor {
LLM_TENSOR_DEC_ATTN_OUT,
LLM_TENSOR_DEC_ATTN_REL_B,
LLM_TENSOR_DEC_CROSS_ATTN_NORM,
LLM_TENSOR_DEC_CROSS_ATTN_NORM_KV,
LLM_TENSOR_DEC_CROSS_ATTN_Q,
LLM_TENSOR_DEC_CROSS_ATTN_K,
LLM_TENSOR_DEC_CROSS_ATTN_V,
@@ -660,6 +664,19 @@ enum llm_tensor {
LLM_TENSOR_CLS,
LLM_TENSOR_CLS_OUT,
LLM_TENSOR_CLS_NORM,
LLM_TENSOR_DECISION_HIDDEN_NORM,
LLM_TENSOR_DECISION_PROJ_MEMORY,
LLM_TENSOR_DECISION_PROJ_QUESTION,
LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION,
LLM_TENSOR_DECISION_PROJ_GLOBAL,
LLM_TENSOR_DECISION_PROJ_OPTION_CONTEXT,
LLM_TENSOR_DECISION_PROJ_OPTION_LEXICAL,
LLM_TENSOR_DECISION_OPTION_SUMMARY_NORM,
LLM_TENSOR_DECISION_FIELD_NORM,
LLM_TENSOR_DECISION_OPTION_NORM,
LLM_TENSOR_DECISION_SCALES,
LLM_TENSOR_DECISION_SCORER,
LLM_TENSOR_DECISION_SCORER_OUT,
LLM_TENSOR_CONV1D,
LLM_TENSOR_CONVNEXT_DW,
LLM_TENSOR_CONVNEXT_NORM,
+29
View File
@@ -168,6 +168,14 @@ bool llama_batch_allocr::init(
}
}
// kept empty if no entry has one
for (int32_t i = 0; i < n_tok; ++i) {
if (batch_inp.tokens[i].decision_order != 0) {
decision_order.resize(n_tok, 0);
decision_order[i] = batch_inp.tokens[i].decision_order;
}
}
//
// set up the internal llama_batch to point to our owned arrays
//
@@ -256,6 +264,7 @@ bool llama_batch_allocr::init(
/*.seq_id_unq =*/ this->seq_id_unq.data(),
/*.seq_idx =*/ this->seq_idx.data(),
/*.output =*/ batch.logits,
/*.decision_order =*/ decision_order.empty() ? nullptr : decision_order.data(),
/*.data =*/ {},
};
@@ -462,6 +471,7 @@ llama_ubatch llama_batch_allocr::ubatch_reserve(uint32_t n_seq_tokens, uint32_t
/*.seq_id_unq =*/ udata->seq_id_unq.data(),
/*.seq_idx =*/ udata->seq_idx.data(),
/*.output =*/ udata->output.data(),
/*.decision_order =*/ nullptr,
/*.data =*/ std::move(udata),
};
@@ -765,6 +775,7 @@ void llama_batch_allocr::clear() {
seq_id .clear();
seq_id_unq .clear();
output .clear();
decision_order.clear();
for (auto & cur : seq_pos) {
cur.clear();
@@ -799,6 +810,7 @@ llama_ubatch llama_batch_allocr::ubatch_add(const std::vector<int32_t> & idxs, u
udata->seq_id_unq.resize(0);
udata->seq_idx .resize(LLAMA_MAX_SEQ, -1);
udata->output .resize(n_tokens);
udata->decision_order.resize(decision_order.empty() ? 0 : n_tokens);
udata->batch_idxs = idxs;
udata->seq_id_data.reserve(n_tokens);
@@ -826,6 +838,10 @@ llama_ubatch llama_batch_allocr::ubatch_add(const std::vector<int32_t> & idxs, u
udata->n_seq_id[i] = batch.n_seq_id[idxs[i]];
udata->output[i] = batch.logits[idxs[i]];
if (!decision_order.empty()) {
udata->decision_order[i] = decision_order[idxs[i]];
}
for (int s = 0; s < udata->n_seq_id[i]; ++s) {
const llama_seq_id seq_id = batch.seq_id[idxs[i]][s];
@@ -867,6 +883,7 @@ llama_ubatch llama_batch_allocr::ubatch_add(const std::vector<int32_t> & idxs, u
/*.seq_id_unq =*/ udata->seq_id_unq.data(),
/*.seq_idx =*/ udata->seq_idx.data(),
/*.output =*/ udata->output.data(),
/*.decision_order =*/ udata->decision_order.empty() ? nullptr : udata->decision_order.data(),
/*.data =*/ std::move(udata),
};
@@ -1175,6 +1192,14 @@ bool llama_batch_ext::set_output(int32_t idx, bool output_last) {
return true;
}
bool llama_batch_ext::set_decision_order(int32_t idx, int32_t order) {
if (idx < 0 || idx >= (int32_t) tokens.size()) {
return false;
}
tokens[idx].decision_order = order;
return true;
}
// llama_batch_ext C API
llama_batch_ext * llama_batch_ext_init(llama_context * ctx) {
@@ -1243,6 +1268,10 @@ bool llama_batch_ext_set_output_logits(llama_batch_ext * batch, int32_t idx, boo
return batch->set_output(idx, value);
}
bool llama_batch_ext_set_decision_order(llama_batch_ext * batch, int32_t idx, llama_decision_order order) {
return batch->set_decision_order(idx, order);
}
// llama_batch_compat
void llama_batch_compat::init(llama_batch_ext & dst, const llama_batch & batch_inp, size_t n_embd_row) {
+5
View File
@@ -52,6 +52,7 @@ struct llama_ubatch {
llama_seq_id * seq_id_unq; // [n_seqs_unq] | s | seq_id
int32_t * seq_idx; // [LLAMA_MAX_SEQ] | - | seq_idx
int8_t * output; // [n_tokens] | i | -
int32_t * decision_order; // [n_tokens], NULL if no entry has one, see llama_batch_ext_set_decision_order()
struct data_t {
std::vector<llama_token> token;
@@ -63,6 +64,7 @@ struct llama_ubatch {
std::vector<int32_t> seq_idx;
std::vector<int8_t> output;
std::vector<int32_t> batch_idxs; // original batch index for each token
std::vector<int32_t> decision_order;
std::vector<llama_seq_id> seq_id_data;
};
@@ -96,6 +98,7 @@ struct llama_batch_ext {
bool has_embd = false; // whether embd_off is set
size_t embd_off = 0; // index offset in the embd array
bool output = false; // TODO: have dedicated output flags
int32_t decision_order = 0; // see llama_batch_ext_set_decision_order()
std::unordered_set<llama_seq_id> seq_ids;
std::array<llama_pos, GGML_MROPE_SECTIONS> pos = {0, 0, 0, 0};
};
@@ -125,6 +128,7 @@ struct llama_batch_ext {
bool set_token_embd(int32_t idx, llama_embd embd_in);
bool set_token_pos(int32_t idx, const llama_pos * pos_in);
bool set_output(int32_t idx, bool output_last);
bool set_decision_order(int32_t idx, int32_t order);
};
// a helper for sanitizing, fulfilling and splitting a batch
@@ -202,6 +206,7 @@ private:
std::vector<llama_seq_id> seq_id_unq;
std::vector<int32_t> seq_idx;
std::vector<int8_t> output;
std::vector<int32_t> decision_order; // empty if no entry has one
using pos_set_t = std::set<llama_pos>;
using seq_cpl_t = std::vector<bool>;
+1
View File
@@ -2406,6 +2406,7 @@ uint32_t llama_context::graph_max_nodes(uint32_t n_tokens) const {
model.arch == LLM_ARCH_BAILINGMOE3 ||
model.arch == LLM_ARCH_QWEN35 ||
model.arch == LLM_ARCH_QWEN35MOE ||
model.arch == LLM_ARCH_CLEF ||
model.arch == LLM_ARCH_QWEN4EXP ||
model.arch == LLM_ARCH_DEEPSEEK4 ||
(model.arch == LLM_ARCH_DFLASH && model.hparams.dsv4_hc_mult > 0) ||
+14
View File
@@ -100,6 +100,20 @@ LLAMA_API void llama_set_embeddings_nextn(struct llama_context * ctx, bool value
// chain multiple trained NextN heads. Default 0 (first head).
LLAMA_API void llama_set_nextn_layer_offset(struct llama_context * ctx, int32_t offset);
// Marks the entries that a joint decision head (clef) reads, the default is 0
// See https://github.com/ggml-org/llama.cpp/pull/29831 for details
// A run of entries with the same value is one span, spans must be separated by entries with value 0
// An option belongs to the last question before it
enum llama_decision_order {
LLAMA_DECISION_ORDER_NONE = 0, // not read by the head
LLAMA_DECISION_ORDER_QUESTION_NOUL = 1, // text of a question
LLAMA_DECISION_ORDER_QUESTION_CHOICE = 2,
LLAMA_DECISION_ORDER_QUESTION_SCORE = 3,
LLAMA_DECISION_ORDER_OPTION = 4, // text of an option
};
// The embeddings output has one value per entry: row i is the score of option i
LLAMA_API bool llama_batch_ext_set_decision_order(struct llama_batch_ext * batch, int32_t idx, enum llama_decision_order order);
// mirrors:
// LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
LLAMA_API float * llama_get_embeddings_nextn(struct llama_context * ctx);
+22 -15
View File
@@ -351,8 +351,7 @@ bool llm_graph_input_rs::can_reuse(const llm_graph_params & params) {
res &= s_copy->ne[0] == mctx->get_n_rs();
res &= s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= s_copy_extra->ne[0] == mctx->get_n_rs() - params.ubatch.n_seqs;
res &= s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= head == mctx->get_head();
res &= rs_z == mctx->get_rs_z();
@@ -1132,8 +1131,7 @@ bool llm_graph_input_mem_hybrid::can_reuse(const llm_graph_params & params) {
res &= inp_rs->s_copy->ne[0] == mctx->get_recr()->get_n_rs();
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->s_copy_extra->ne[0] == mctx->get_recr()->get_n_rs() - params.ubatch.n_seqs;
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->head == mctx->get_recr()->get_head();
res &= inp_rs->rs_z == mctx->get_recr()->get_rs_z();
@@ -1175,8 +1173,7 @@ bool llm_graph_input_mem_hybrid_k::can_reuse(const llm_graph_params & params) {
res &= inp_rs->s_copy->ne[0] == mctx->get_recr()->get_n_rs();
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->s_copy_extra->ne[0] == mctx->get_recr()->get_n_rs() - params.ubatch.n_seqs;
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->head == mctx->get_recr()->get_head();
res &= inp_rs->rs_z == mctx->get_recr()->get_rs_z();
@@ -1263,8 +1260,7 @@ bool llm_graph_input_mem_hybrid_iswa::can_reuse(const llm_graph_params & params)
res &= inp_rs->s_copy->ne[0] == mctx->get_recr()->get_n_rs();
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->s_copy_extra->ne[0] == mctx->get_recr()->get_n_rs() - params.ubatch.n_seqs;
res &= inp_rs->s_copy_main->ne[0] == params.ubatch.n_seqs;
res &= inp_rs->head == mctx->get_recr()->get_head();
res &= inp_rs->rs_z == mctx->get_recr()->get_rs_z();
@@ -3519,8 +3515,8 @@ llm_graph_input_dsv4 * llm_graph_context::build_inp_dsv4() const {
ggml_tensor * llm_graph_context::build_rs(
ggml_tensor * s,
ggml_tensor * state_copy,
ggml_tensor * state_copy_main,
ggml_tensor * state_copy_extra,
int32_t state_size,
int32_t n_seqs,
uint32_t n_rs,
@@ -3539,12 +3535,19 @@ ggml_tensor * llm_graph_context::build_rs(
// copy states
// NOTE: assuming the copy destinations are ALL contained between rs_head and rs_head + n_rs
// {state_size, rs_size} -> {state_size, n_seqs}
ggml_tensor * output_states = get_state_rows(ctx0, states, state_copy_main);
// one gather of the states i0..n_rs (ubatch states then extra states), sized by n_rs so the reserve covers every split
// {state_size, rs_size} -> {state_size, n_rs - i0}
const int64_t i0 = n_rs - state_copy->ne[0];
ggml_tensor * states_all = ggml_get_rows(ctx0, states, state_copy);
ggml_tensor * output_states = get_state_rows ?
get_state_rows(ctx0, states, state_copy_main) :
ggml_view_2d(ctx0, states_all, state_size, n_seqs, states_all->nb[1], 0);
ggml_build_forward_expand(gf, output_states);
// copy extra states which won't be changed further (between n_seqs and n_rs)
ggml_tensor * states_extra = ggml_get_rows(ctx0, states, state_copy_extra);
ggml_tensor * states_extra = ggml_view_2d(ctx0, states_all, state_size, n_rs - n_seqs, states_all->nb[1], (n_seqs - i0)*states_all->nb[1]);
ggml_build_forward_expand(gf,
ggml_cpy(ctx0,
states_extra,
@@ -3567,8 +3570,8 @@ static std::unique_ptr<llm_graph_input_rs> build_rs_inp_impl(
ggml_set_input(inp->s_copy);
ggml_set_name(inp->s_copy, "rs_s_copy");
inp->s_copy_main = ggml_view_1d(ctx0, inp->s_copy, n_seqs, 0);
inp->s_copy_extra = ggml_view_1d(ctx0, inp->s_copy, n_rs - n_seqs, n_seqs * inp->s_copy->nb[0]);
inp->s_copy_main = ggml_view_1d(ctx0, inp->s_copy, n_seqs, 0);
inp->s_copy_tail = ggml_view_1d(ctx0, inp->s_copy, n_rs - 1, inp->s_copy->nb[0]);
inp->head = mctx_cur->get_head();
inp->rs_z = mctx_cur->get_rs_z();
@@ -3592,7 +3595,11 @@ ggml_tensor * llm_graph_context::build_rs(
const llm_graph_get_rows_fn & get_state_rows) const {
const auto * kv_state = inp->mctx;
return build_rs(s, inp->s_copy_main, inp->s_copy_extra, state_size, n_seqs,
// a custom getter reads the states of the ubatch straight from the cache, so the gather skips the first
// state: it still holds the n_rs - n_seqs extra states and copies no state of a single sequence ubatch
ggml_tensor * state_copy = get_state_rows ? inp->s_copy_tail : inp->s_copy;
return build_rs(s, state_copy, inp->s_copy_main, state_size, n_seqs,
kv_state->get_n_rs(), kv_state->get_head(), kv_state->get_size(), kv_state->get_rs_z(),
get_state_rows);
}
+5 -5
View File
@@ -273,8 +273,8 @@ public:
// views of s_copy, computed once per graph
// and shared across layers which use build_rs
ggml_tensor * s_copy_main; // I32 [n_seqs]
ggml_tensor * s_copy_extra; // I32 [n_rs - n_seqs]
ggml_tensor * s_copy_main; // I32 [n_seqs]
ggml_tensor * s_copy_tail; // I32 [n_rs - 1]
const llama_memory_recurrent_context * mctx;
@@ -1327,15 +1327,15 @@ struct llm_graph_context {
// `llama_memory_recurrent`
ggml_tensor * build_rs(
ggml_tensor * s,
ggml_tensor * state_copy,
ggml_tensor * state_copy_main,
ggml_tensor * state_copy_extra,
int32_t state_size,
int32_t n_seqs,
uint32_t n_rs,
uint32_t rs_head,
uint32_t rs_size,
int32_t rs_zero,
const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const;
const llm_graph_get_rows_fn & get_state_rows = nullptr) const;
llm_graph_input_rs * build_rs_inp() const;
@@ -1344,7 +1344,7 @@ struct llm_graph_context {
ggml_tensor * s,
int32_t state_size,
int32_t n_seqs,
const llm_graph_get_rows_fn & get_state_rows = ggml_get_rows) const;
const llm_graph_get_rows_fn & get_state_rows = nullptr) const;
ggml_tensor * build_rwkv_token_shift_load(
llm_graph_input_rs * inp,
+1
View File
@@ -164,6 +164,7 @@ static llama_ubatch dsv4_build_raw_write_ubatch(const llama_ubatch & ubatch) {
/*.seq_id_unq =*/ data->seq_id_unq.data(),
/*.seq_idx =*/ data->seq_idx.data(),
/*.output =*/ data->output.data(),
/*.decision_order =*/ nullptr,
/*.data =*/ data,
};
+1
View File
@@ -20,6 +20,7 @@ bool llama_model_saver_supports_arch(llm_arch arch) {
case LLM_ARCH_T5:
case LLM_ARCH_APERTUS:
case LLM_ARCH_STEP35:
case LLM_ARCH_CLEF: // the head tensors are not saved
return false;
default:
return true;
+12 -4
View File
@@ -326,6 +326,8 @@ static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params
return new llama_model_qwen35(params);
case LLM_ARCH_QWEN35MOE:
return new llama_model_qwen35moe(params);
case LLM_ARCH_CLEF:
return new llama_model_clef(params);
case LLM_ARCH_QWEN4EXP:
return new llama_model_qwen4exp(params);
case LLM_ARCH_MISTRAL3:
@@ -607,7 +609,7 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
// if a model has fused tensors they need to be separated into "segments", see the comments on ggml_backend_meta_split_state struct
auto get_split_segments = [&](int axis, uint32_t il) -> std::vector<std::pair<int64_t, uint32_t>> {
// TODO: deduplicate condition [TAG_SPLIT_QGATE_QWEN]
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE || ud->model->arch == LLM_ARCH_CLEF ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {
// fused full attention layers with Q gate tensors that need n_embd doubled:
@@ -762,7 +764,7 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
GGML_ASSERT(segments.size() == 1);
// some models have Q gate tensors, for those cases the granularity needs to be doubled:
// TODO: deduplicate condition [TAG_SPLIT_QGATE_QWEN]
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE || ud->model->arch == LLM_ARCH_CLEF ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {
return {std::lcm(2*n_embd_q, blck_size_perf)};
}
@@ -795,7 +797,7 @@ struct ggml_backend_meta_split_state llama_meta_device_get_split_state(const str
if (std::regex_match(tensor_name, pattern_qkv_weight) || std::regex_match(tensor_name, pattern_qkv_bias)) {
// fused full attention layers need Q gate tensors handled like above:
// TODO: deduplicate condition [TAG_SPLIT_QGATE_QWEN]
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE ||
if (ud->model->arch == LLM_ARCH_QWEN3NEXT || ud->model->arch == LLM_ARCH_QWEN35 || ud->model->arch == LLM_ARCH_QWEN35MOE || ud->model->arch == LLM_ARCH_CLEF ||
ud->model->arch == LLM_ARCH_QWEN4EXP) {
return {std::lcm(2*n_embd_q, blck_size_perf), granularity_kv};
}
@@ -1961,7 +1963,11 @@ bool llama_model_base::load_tensors(llama_model_loader & ml) {
}
ggml_tensor * llama_model_base::create_tensor(llama_model_loader & ml, const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) {
const buft_list_t * buft_list_layer = tn.bid == -1 ? nullptr : pimpl->dev_layer.at(tn.bid).buft_list;
const buft_list_t * buft_list_layer = nullptr;
if (tn.bid != -1) {
// blocks that are not model layers (e.g. the blocks of a head) go with the output
buft_list_layer = (size_t) tn.bid < pimpl->dev_layer.size() ? pimpl->dev_layer.at(tn.bid).buft_list : pimpl->dev_output.buft_list;
}
return ml.create_tensor(
hparams, &pimpl->cpu_buft_list, pimpl->dev_input.buft_list, pimpl->dev_output.buft_list, buft_list_layer,
tn, ne, flags);
@@ -2369,6 +2375,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
case LLM_ARCH_LLADA:
case LLM_ARCH_LLADA_MOE:
case LLM_ARCH_RND1:
case LLM_ARCH_CLEF:
{
res = nullptr;
} break;
@@ -3200,6 +3207,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
case LLM_ARCH_QWEN3VLMOE:
case LLM_ARCH_QWEN35:
case LLM_ARCH_QWEN35MOE:
case LLM_ARCH_CLEF:
case LLM_ARCH_QWEN4EXP:
case LLM_ARCH_QWEN3TTS:
return LLAMA_ROPE_TYPE_IMROPE;
+616
View File
@@ -0,0 +1,616 @@
#include "models.h"
#include "llama-ext.h"
#include <cmath>
void llama_model_clef::load_arch_hparams(llama_model_loader & ml) {
llama_model_qwen35::load_arch_hparams(ml);
ml.get_key(LLM_KV_DECISION_ROUTING_BLOCK_COUNT, n_layer_routing);
ml.get_key(LLM_KV_DECISION_BLOCK_COUNT, n_layer_joint);
ml.get_key(LLM_KV_DECISION_HEAD_COUNT, n_head_decision);
if (n_head_decision == 0 || n_layer_routing > LLAMA_MAX_LAYERS || n_layer_joint > LLAMA_MAX_LAYERS) {
throw std::runtime_error("invalid size of the decision head");
}
// used by the head
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
// the output is one score per token, see llama_batch_ext_set_decision_order()
hparams.n_embd_out_impl = 1;
}
void llama_model_clef::load_arch_tensors(llama_model_loader & ml) {
llama_model_qwen35::load_arch_tensors(ml);
LLAMA_LOAD_LOCALS;
const auto * w_memory = ml.get_weight(tn(LLM_TENSOR_DECISION_PROJ_MEMORY, "weight").str().c_str());
const auto * w_ffn = ml.get_weight(tn(LLM_TENSOR_DEC_FFN_UP, "weight", 0).str().c_str());
if (w_memory == nullptr || w_ffn == nullptr) {
throw std::runtime_error("the decision head is missing");
}
const int64_t n_embd_h = w_memory->tensor->ne[1];
const int64_t n_ff_h = w_ffn->tensor->ne[1];
if (n_embd_h % n_head_decision != 0) {
throw std::runtime_error("invalid width of the decision head");
}
auto load_norm = [&](norm & n, llm_tensor type, int64_t size, int il = -1) {
n.w = il < 0 ? create_tensor(tn(type, "weight"), {size}, 0) : create_tensor(tn(type, "weight", il), {size}, 0);
n.b = il < 0 ? create_tensor(tn(type, "bias"), {size}, 0) : create_tensor(tn(type, "bias", il), {size}, 0);
};
auto load_attn = [&](attn & a, llm_tensor q, llm_tensor k, llm_tensor v, llm_tensor o, int il) {
a.wq = create_tensor(tn(q, "weight", il), {n_embd_h, n_embd_h}, 0);
a.bq = create_tensor(tn(q, "bias", il), {n_embd_h}, 0);
a.wk = create_tensor(tn(k, "weight", il), {n_embd_h, n_embd_h}, 0);
a.bk = create_tensor(tn(k, "bias", il), {n_embd_h}, 0);
a.wv = create_tensor(tn(v, "weight", il), {n_embd_h, n_embd_h}, 0);
a.bv = create_tensor(tn(v, "bias", il), {n_embd_h}, 0);
a.wo = create_tensor(tn(o, "weight", il), {n_embd_h, n_embd_h}, 0);
a.bo = create_tensor(tn(o, "bias", il), {n_embd_h}, 0);
};
head_layers.resize(n_layer_routing + n_layer_joint);
for (int il = 0; il < (int) head_layers.size(); ++il) {
auto & layer = head_layers[il];
if (il < (int) n_layer_routing) {
load_norm(layer.cross_norm_kv, LLM_TENSOR_DEC_CROSS_ATTN_NORM_KV, n_embd_h, il);
} else {
load_norm(layer.self_norm, LLM_TENSOR_DEC_ATTN_NORM, n_embd_h, il);
load_attn(layer.self_attn, LLM_TENSOR_DEC_ATTN_Q, LLM_TENSOR_DEC_ATTN_K, LLM_TENSOR_DEC_ATTN_V, LLM_TENSOR_DEC_ATTN_OUT, il);
}
load_norm(layer.cross_norm, LLM_TENSOR_DEC_CROSS_ATTN_NORM, n_embd_h, il);
load_attn(layer.cross_attn, LLM_TENSOR_DEC_CROSS_ATTN_Q, LLM_TENSOR_DEC_CROSS_ATTN_K, LLM_TENSOR_DEC_CROSS_ATTN_V, LLM_TENSOR_DEC_CROSS_ATTN_OUT, il);
load_norm(layer.ffn_norm, LLM_TENSOR_DEC_FFN_NORM, n_embd_h, il);
layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", il), {n_embd_h, n_ff_h}, 0);
layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "bias", il), {n_ff_h}, 0);
layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", il), {n_ff_h, n_embd_h}, 0);
layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "bias", il), {n_embd_h}, 0);
}
load_norm(hidden_norm, LLM_TENSOR_DECISION_HIDDEN_NORM, n_embd);
load_norm(option_summary_norm, LLM_TENSOR_DECISION_OPTION_SUMMARY_NORM, n_embd_h);
load_norm(field_norm, LLM_TENSOR_DECISION_FIELD_NORM, n_embd_h);
load_norm(option_norm, LLM_TENSOR_DECISION_OPTION_NORM, n_embd_h);
proj_memory = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_MEMORY, "weight"), {n_embd, n_embd_h}, 0);
proj_question = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_QUESTION, "weight"), {n_embd, n_embd_h}, 0);
proj_option_question = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION, "weight"), {n_embd, n_embd_h}, 0);
proj_global = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_GLOBAL, "weight"), {n_embd, n_embd_h}, 0);
proj_option_context = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_OPTION_CONTEXT, "weight"), {n_embd, n_embd_h}, 0);
proj_option_lexical = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_OPTION_LEXICAL, "weight"), {n_embd, n_embd_h}, 0);
scales = create_tensor(tn(LLM_TENSOR_DECISION_SCALES), {3}, 0);
type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd_h, 3}, 0);
scorer = create_tensor(tn(LLM_TENSOR_DECISION_SCORER, "weight"), {4 * n_embd_h, n_embd_h}, 0);
scorer_b = create_tensor(tn(LLM_TENSOR_DECISION_SCORER, "bias"), {n_embd_h}, 0);
scorer_out = create_tensor(tn(LLM_TENSOR_DECISION_SCORER_OUT, "weight"), {n_embd_h, 1}, 0);
scorer_out_b = create_tensor(tn(LLM_TENSOR_DECISION_SCORER_OUT, "bias"), {1}, 0);
}
std::unique_ptr<llm_graph_context> llama_model_clef::build_arch_graph(const llm_graph_params & params) const {
return std::make_unique<graph>(*this, params);
}
// spans read by the head, [start, end) in ubatch token indices
struct clef_spans {
struct question {
int32_t type; // noul, choice, score
int32_t start;
int32_t end;
};
struct option {
int32_t question;
int32_t start;
int32_t end;
};
std::vector<question> questions;
std::vector<option> options;
bool valid = false;
};
// see llama_batch_ext_set_decision_order()
// if the batch has no usable order, returns one empty question with one empty option
static clef_spans clef_get_spans(const llama_ubatch & ubatch) {
clef_spans res;
std::vector<bool> has_option;
// TODO: support multiple sequences
bool ok = ubatch.decision_order != nullptr && ubatch.n_seqs_unq == 1;
const int32_t n_tokens = ubatch.n_tokens;
for (int32_t i = 0; ok && i < n_tokens;) {
const int32_t order = ubatch.decision_order[i];
int32_t end = i + 1;
while (end < n_tokens && ubatch.decision_order[end] == order) {
end++;
}
switch (order) {
case LLAMA_DECISION_ORDER_NONE:
break;
case LLAMA_DECISION_ORDER_QUESTION_NOUL:
case LLAMA_DECISION_ORDER_QUESTION_CHOICE:
case LLAMA_DECISION_ORDER_QUESTION_SCORE:
res.questions.push_back({ order - LLAMA_DECISION_ORDER_QUESTION_NOUL, i, end });
has_option.push_back(false);
break;
case LLAMA_DECISION_ORDER_OPTION:
ok = !res.questions.empty();
if (ok) {
res.options.push_back({ (int32_t) res.questions.size() - 1, i, end });
has_option.back() = true;
}
break;
default:
ok = false;
}
i = end;
}
// each question needs an option
res.valid = ok && !res.questions.empty() && std::find(has_option.begin(), has_option.end(), false) == has_option.end();
if (!res.valid) {
res.questions.clear();
res.options.clear();
res.questions.push_back({ 0, 0, 0 });
res.options.push_back({ 0, 0, 0 });
}
return res;
}
// pooling matrices and indices computed from the spans
class llama_model_clef::input_decision : public llm_graph_input_i {
public:
input_decision(const llama_ubatch & ubatch) : n_tokens(ubatch.n_tokens) {
const auto spans = clef_get_spans(ubatch);
n_questions = spans.questions.size();
n_options = spans.options.size();
}
void set_input(const llama_ubatch * ubatch) override {
GGML_ASSERT(ubatch->token);
ggml_backend_tensor_set(tokens, ubatch->token, 0, n_tokens * sizeof(llama_token));
const auto spans = clef_get_spans(*ubatch);
GGML_ASSERT(spans.questions.size() == n_questions && spans.options.size() == n_options);
// the scores are NaN if the batch has a decision order that cannot be used
const float status_data = spans.valid || ubatch->decision_order == nullptr ? 0.0f : NAN;
ggml_backend_tensor_set(status, &status_data, 0, ggml_nbytes(status));
std::vector<float> pool_q_data(n_tokens * n_questions, 0.0f);
std::vector<int32_t> types(n_questions);
for (size_t i = 0; i < n_questions; i++) {
const auto & q = spans.questions[i];
for (int32_t j = q.start; j < q.end; j++) {
pool_q_data[i * n_tokens + j] = 1.0f / (q.end - q.start);
}
types[i] = q.type;
}
std::vector<float> pool_o_data(n_tokens * n_options, 0.0f);
std::vector<int32_t> owner(n_options);
std::vector<float> mask(n_options * n_questions, -INFINITY);
for (size_t i = 0; i < n_options; i++) {
const auto & o = spans.options[i];
for (int32_t j = o.start; j < o.end; j++) {
pool_o_data[i * n_tokens + j] = 1.0f / (o.end - o.start);
}
owner[i] = o.question;
mask[o.question * n_options + i] = 0.0f;
}
ggml_backend_tensor_set(pool_q, pool_q_data.data(), 0, ggml_nbytes(pool_q));
ggml_backend_tensor_set(pool_o, pool_o_data.data(), 0, ggml_nbytes(pool_o));
ggml_backend_tensor_set(question_type, types.data(), 0, ggml_nbytes(question_type));
ggml_backend_tensor_set(option_question, owner.data(), 0, ggml_nbytes(option_question));
ggml_backend_tensor_set(option_mask, mask.data(), 0, ggml_nbytes(option_mask));
}
bool can_reuse(const llm_graph_params & params) override {
// the values are computed again in set_input(), only the shapes must match
const auto spans = clef_get_spans(params.ubatch);
return spans.questions.size() == n_questions && spans.options.size() == n_options;
}
ggml_tensor * tokens = nullptr; // I32 [n_tokens]
ggml_tensor * pool_q = nullptr; // F32 [n_tokens, n_questions], mean over the span of the question
ggml_tensor * pool_o = nullptr; // F32 [n_tokens, n_options], mean over the span of the option
ggml_tensor * question_type = nullptr; // I32 [n_questions]
ggml_tensor * option_question = nullptr; // I32 [n_options]
ggml_tensor * option_mask = nullptr; // F32 [n_options, n_questions], 0 if the option belongs to the question, else -inf
ggml_tensor * status = nullptr; // F32 [1], added to the scores: 0, or NaN on invalid input
const int64_t n_tokens;
size_t n_questions;
size_t n_options;
};
// the backbone is copied from llama_model_qwen35::graph, without the memory module
llama_model_clef::graph::graph(const llama_model & model_base, const llm_graph_params & params) :
llm_build_delta_net_base(params), model(static_cast<const llama_model_clef &>(model_base)) {
const int64_t n_embd_head = hparams.n_embd_head_v();
GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
int sections[4];
std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
ggml_tensor * cur;
ggml_tensor * inpL;
inpL = build_inp_embd(model.tok_embd);
ggml_tensor * inp_pos = build_inp_pos();
auto * inp_attn = build_attn_inp_causal();
auto inp_decision_ptr = std::make_unique<input_decision>(ubatch);
auto * inp_decision = inp_decision_ptr.get();
res->add_input(std::move(inp_decision_ptr));
for (int il = 0; il < n_layer; ++il) {
ggml_tensor * inpSA = inpL;
cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
cb(cur, "attn_norm", il);
if (hparams.is_recr(il)) {
cur = build_layer_attn_linear(cur, il);
} else {
cur = build_layer_attn(inp_attn, cur, inp_pos, sections, il);
}
cur = ggml_add(ctx0, cur, inpSA);
cb(cur, "attn_residual", il);
ggml_tensor * ffn_residual = cur;
cur = build_norm(cur, model.layers[il].attn_post_norm, nullptr, LLM_NORM_RMS, il);
cb(cur, "attn_post_norm", il);
cur = build_ffn(cur,
model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
cur = ggml_add(ctx0, cur, ffn_residual);
cb(cur, "l_out", il);
inpL = cur;
}
cur = build_norm(inpL, model.output_norm, nullptr, LLM_NORM_RMS, -1);
cb(cur, "result_norm", -1);
// the head is always evaluated, so that the graph has the same nodes for every batch
cur = build_head(cur, inp_decision);
// row i of the output is the score of option i
cur = ggml_pad(ctx0, cur, 0, n_tokens - cur->ne[1], 0, 0);
cur = ggml_add(ctx0, cur, inp_decision->status);
cb(cur, "result_decision", -1);
res->t_embd = cur;
ggml_build_forward_expand(gf, cur);
}
// same as build_attn_inp_no_cache(), but the mask is causal even if the batch is processed by the encoder path
llm_graph_input_attn_no_cache * llama_model_clef::graph::build_attn_inp_causal() {
llama_cparams cparams_causal = cparams;
cparams_causal.causal_attn = true;
auto inp = std::make_unique<llm_graph_input_attn_no_cache>(hparams, cparams_causal);
const auto type_mask = cparams.flash_attn ? GGML_TYPE_F16 : GGML_TYPE_F32;
inp->self_kq_mask = ggml_new_tensor_4d(ctx0, type_mask, n_tokens, n_tokens, 1, 1);
ggml_set_input(inp->self_kq_mask);
cb(inp->self_kq_mask, "self_kq_mask", -1);
inp->self_kq_mask_cnv = inp->self_kq_mask;
return (llm_graph_input_attn_no_cache *) res->add_input(std::move(inp));
}
ggml_tensor * llama_model_clef::graph::build_layer_attn(
llm_graph_input_attn_no_cache * inp,
ggml_tensor * cur,
ggml_tensor * inp_pos,
int * sections,
int il) {
const int64_t n_embd_head = hparams.n_embd_head_v();
// the Q projection outputs query + gate
auto [Qcur_full, Kcur, Vcur] = build_qkv(model.layers[il], cur,
n_embd_head * 2, n_head,
n_embd_head, n_head_kv,
n_embd_head, n_head_kv,
il, false);
ggml_tensor * Qcur = ggml_view_3d(ctx0, Qcur_full, n_embd_head, n_head, n_tokens,
ggml_element_size(Qcur_full) * n_embd_head * 2,
ggml_element_size(Qcur_full) * n_embd_head * 2 * n_head, 0);
Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);
cb(Qcur, "Qcur_normed", il);
Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, nullptr, LLM_NORM_RMS, il);
cb(Kcur, "Kcur_normed", il);
ggml_tensor * gate = ggml_view_3d(ctx0, Qcur_full, n_embd_head, n_head, n_tokens,
ggml_element_size(Qcur_full) * n_embd_head * 2,
ggml_element_size(Qcur_full) * n_embd_head * 2 * n_head,
ggml_element_size(Qcur_full) * n_embd_head);
gate = ggml_cont_2d(ctx0, gate, n_embd_head * n_head, n_tokens);
Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
Qcur = ggml_rope_multi(
ctx0, Qcur, inp_pos, nullptr,
n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
ext_factor, attn_factor, beta_fast, beta_slow
);
Kcur = ggml_rope_multi(
ctx0, Kcur, inp_pos, nullptr,
n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
ext_factor, attn_factor, beta_fast, beta_slow
);
const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
cur = build_attn(inp,
nullptr, nullptr, nullptr,
Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
cb(cur, "attn_pregate", il);
cur = ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate));
cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
cb(cur, "attn_output", il);
return cur;
}
// gated delta net over the whole batch, the conv and recurrent states start from zero and are not kept
ggml_tensor * llama_model_clef::graph::build_layer_attn_linear(
ggml_tensor * cur,
int il) {
const auto & layer = model.layers[il];
const int64_t d_inner = hparams.ssm_d_inner;
const int64_t head_k_dim = hparams.ssm_d_state;
const int64_t num_k_heads = hparams.ssm_n_group;
const int64_t num_v_heads = hparams.ssm_dt_rank;
const int64_t head_v_dim = d_inner / num_v_heads;
const int64_t n_seqs = 1;
ggml_tensor * qkv_mixed = build_lora_mm(layer.wqkv, cur, layer.wqkv_s);
qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_tokens, n_seqs);
ggml_tensor * z = build_lora_mm(layer.wqkv_gate, cur, layer.wqkv_gate_s);
ggml_tensor * beta = build_lora_mm(layer.ssm_beta, cur, layer.ssm_beta_s);
beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_tokens, n_seqs);
beta = ggml_sigmoid(ctx0, beta);
ggml_tensor * alpha = build_lora_mm(layer.ssm_alpha, cur, layer.ssm_alpha_s);
alpha = ggml_reshape_3d(ctx0, alpha, num_v_heads, n_tokens, n_seqs);
ggml_tensor * gate = ggml_softplus(ctx0, ggml_add(ctx0, alpha, layer.ssm_dt));
gate = ggml_mul(ctx0, gate, layer.ssm_a);
gate = ggml_reshape_4d(ctx0, gate, 1, num_v_heads, n_tokens, n_seqs);
const int64_t conv_kernel_size = layer.ssm_conv1d->ne[0];
const int64_t conv_channels = d_inner + 2 * num_k_heads * head_k_dim;
ggml_tensor * conv_states = ggml_fill(ctx0, ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, conv_kernel_size - 1, conv_channels, n_seqs), 0.0f);
ggml_tensor * conv_input = ggml_concat(ctx0, conv_states, ggml_transpose(ctx0, qkv_mixed), 0);
ggml_tensor * conv_out = ggml_silu(ctx0, ggml_ssm_conv(ctx0, conv_input, layer.ssm_conv1d));
cb(conv_out, "conv_output_silu", il);
const int64_t qkv_dim = head_k_dim * num_k_heads * 2 + head_v_dim * num_v_heads;
const int64_t nb1_qkv = ggml_row_size(conv_out->type, qkv_dim);
ggml_tensor * q_conv = ggml_view_4d(ctx0, conv_out, head_k_dim, num_k_heads, n_tokens, n_seqs,
ggml_row_size(conv_out->type, head_k_dim), nb1_qkv, nb1_qkv * n_tokens,
0);
ggml_tensor * k_conv = ggml_view_4d(ctx0, conv_out, head_k_dim, num_k_heads, n_tokens, n_seqs,
ggml_row_size(conv_out->type, head_k_dim), nb1_qkv, nb1_qkv * n_tokens,
head_k_dim * num_k_heads * ggml_element_size(conv_out));
ggml_tensor * v_conv = ggml_view_4d(ctx0, conv_out, head_v_dim, num_v_heads, n_tokens, n_seqs,
ggml_row_size(conv_out->type, head_v_dim), nb1_qkv, nb1_qkv * n_tokens,
ggml_row_size(conv_out->type, 2 * head_k_dim * num_k_heads));
q_conv = build_gdn_l2_norm(ctx0, q_conv, hparams.f_norm_rms_eps);
k_conv = build_gdn_l2_norm(ctx0, k_conv, hparams.f_norm_rms_eps);
// note: need explicit repeat only if we are not using the fused GDN
if (num_k_heads != num_v_heads && (!cparams.fused_gdn_ar || !cparams.fused_gdn_ch)) {
GGML_ASSERT(num_v_heads % num_k_heads == 0);
q_conv = ggml_repeat_4d(ctx0, q_conv, head_k_dim, num_v_heads, n_tokens, n_seqs);
k_conv = ggml_repeat_4d(ctx0, k_conv, head_k_dim, num_v_heads, n_tokens, n_seqs);
}
ggml_tensor * state = ggml_fill(ctx0, ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_v_dim, head_v_dim, num_v_heads, n_seqs), 0.0f);
ggml_tensor * output = build_delta_net(q_conv, k_conv, v_conv, gate, beta, state, il).first;
cb(output, "attn_output", il);
// gated normalization
ggml_tensor * z_4d = ggml_reshape_4d(ctx0, z, head_v_dim, num_v_heads, n_tokens, n_seqs);
output = build_norm(output, layer.ssm_norm, nullptr, LLM_NORM_RMS, il);
output = ggml_mul(ctx0, output, ggml_silu(ctx0, z_4d));
output = ggml_reshape_3d(ctx0, output, head_v_dim * num_v_heads, n_tokens, n_seqs);
cur = build_lora_mm(layer.ssm_out, output, layer.ssm_out_s);
cb(cur, "linear_attn_out", il);
return ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
}
ggml_tensor * llama_model_clef::graph::build_head_norm(ggml_tensor * cur, const norm & n) {
return build_norm(cur, n.w, n.b, LLM_NORM, -1);
}
// q: [n_embd_h, n_q], kv: [n_embd_h, n_kv], no mask
ggml_tensor * llama_model_clef::graph::build_head_attn(ggml_tensor * q, ggml_tensor * kv, const attn & a) {
const int64_t n_embd_h = q->ne[0];
const int64_t n_head_h = model.n_head_decision;
const int64_t d_head = n_embd_h / n_head_h;
const int64_t n_q = q->ne[1];
const int64_t n_kv = kv->ne[1];
ggml_tensor * Qcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wq, q), a.bq);
ggml_tensor * Kcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wk, kv), a.bk);
ggml_tensor * Vcur = ggml_add(ctx0, ggml_mul_mat(ctx0, a.wv, kv), a.bv);
Qcur = ggml_permute(ctx0, ggml_reshape_3d(ctx0, Qcur, d_head, n_head_h, n_q), 0, 2, 1, 3); // [d_head, n_q, n_head]
Kcur = ggml_permute(ctx0, ggml_reshape_3d(ctx0, Kcur, d_head, n_head_h, n_kv), 0, 2, 1, 3); // [d_head, n_kv, n_head]
Vcur = ggml_permute(ctx0, ggml_reshape_3d(ctx0, Vcur, d_head, n_head_h, n_kv), 1, 2, 0, 3); // [n_kv, d_head, n_head]
Vcur = ggml_cont(ctx0, Vcur);
ggml_tensor * kq = ggml_mul_mat(ctx0, Kcur, Qcur); // [n_kv, n_q, n_head]
kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f / sqrtf(float(d_head)), 0.0f);
ggml_tensor * cur = ggml_mul_mat(ctx0, Vcur, kq); // [d_head, n_q, n_head]
cur = ggml_cont_2d(ctx0, ggml_permute(ctx0, cur, 0, 2, 1, 3), n_embd_h, n_q);
return ggml_add(ctx0, ggml_mul_mat(ctx0, a.wo, cur), a.bo);
}
ggml_tensor * llama_model_clef::graph::build_head_ffn(ggml_tensor * cur, const head_layer & layer) {
cur = build_head_norm(cur, layer.ffn_norm);
cur = ggml_add(ctx0, ggml_mul_mat(ctx0, layer.ffn_up, cur), layer.ffn_up_b);
cur = ggml_gelu_erf(ctx0, cur);
return ggml_add(ctx0, ggml_mul_mat(ctx0, layer.ffn_down, cur), layer.ffn_down_b);
}
// ref: JointSchemaHead in joint_schema_model.py of the model repo
ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_decision * inp) {
const int64_t n_questions = inp->n_questions;
const int64_t n_options = inp->n_options;
inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
inp->pool_q = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_tokens, n_questions);
inp->pool_o = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_tokens, n_options);
inp->question_type = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_questions);
inp->option_question = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_options);
inp->option_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_options, n_questions);
inp->status = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1);
for (ggml_tensor * t : { inp->tokens, inp->pool_q, inp->pool_o, inp->question_type, inp->option_question, inp->option_mask, inp->status }) {
ggml_set_input(t);
}
hidden = build_head_norm(hidden, model.hidden_norm);
ggml_tensor * memory = ggml_mul_mat(ctx0, model.proj_memory, hidden); // [n_embd_h, n_tokens]
const int64_t n_embd_h = memory->ne[0];
// hidden state of the last token
ggml_tensor * global = ggml_view_2d(ctx0, hidden, n_embd, 1, hidden->nb[1], (n_tokens - 1) * hidden->nb[1]);
// mean of the hidden states over each span
ggml_tensor * hidden_t = ggml_cont(ctx0, ggml_transpose(ctx0, hidden));
ggml_tensor * question = ggml_mul_mat(ctx0, hidden_t, inp->pool_q); // [n_embd, n_questions]
ggml_tensor * opt_ctx = ggml_mul_mat(ctx0, hidden_t, inp->pool_o); // [n_embd, n_options]
// mean of the output embeddings of the tokens of each option
ggml_tensor * lexical = ggml_get_rows(ctx0, model.output, inp->tokens);
lexical = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, lexical)), inp->pool_o); // [n_embd, n_options]
// one query per option
ggml_tensor * options = ggml_mul_mat(ctx0, model.proj_option_context, opt_ctx);
options = ggml_add(ctx0, options, ggml_mul_mat(ctx0, model.proj_option_lexical, lexical));
options = ggml_add(ctx0, options, ggml_get_rows(ctx0, ggml_mul_mat(ctx0, model.proj_option_question, question), inp->option_question));
// the options read the prompt
for (uint32_t il = 0; il < model.n_layer_routing; ++il) {
const auto & layer = model.head_layers[il];
ggml_tensor * cur = build_head_attn(
build_head_norm(options, layer.cross_norm),
build_head_norm(memory, layer.cross_norm_kv),
layer.cross_attn);
options = ggml_add(ctx0, options, cur);
options = ggml_add(ctx0, options, build_head_ffn(options, layer));
}
cb(options, "decision_options", -1);
// one vector per question: its text, a summary of its options, the end of the prompt and its type
ggml_tensor * fields = ggml_mul_mat(ctx0, model.proj_question, question); // [n_embd_h, n_questions]
{
// each question weights its own options
ggml_tensor * weights = ggml_mul_mat(ctx0, options, fields); // [n_options, n_questions]
weights = ggml_scale(ctx0, weights, 1.0f / sqrtf(float(n_embd_h)));
weights = ggml_soft_max(ctx0, ggml_add(ctx0, weights, inp->option_mask));
ggml_tensor * summary = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, options)), weights);
fields = ggml_add(ctx0, fields, build_head_norm(summary, model.option_summary_norm));
fields = ggml_add(ctx0, fields, ggml_mul_mat(ctx0, model.proj_global, global));
fields = ggml_add(ctx0, fields, ggml_get_rows(ctx0, model.type_embd, inp->question_type));
}
// the questions read each other and the prompt
for (uint32_t il = model.n_layer_routing; il < model.head_layers.size(); ++il) {
const auto & layer = model.head_layers[il];
ggml_tensor * cur = build_head_norm(fields, layer.self_norm);
fields = ggml_add(ctx0, fields, build_head_attn(cur, cur, layer.self_attn));
cur = build_head_norm(fields, layer.cross_norm);
fields = ggml_add(ctx0, fields, build_head_attn(cur, memory, layer.cross_attn));
fields = ggml_add(ctx0, fields, build_head_ffn(fields, layer));
}
fields = build_head_norm(fields, model.field_norm);
cb(fields, "decision_fields", -1);
const float eps = 1e-12f;
// prior: the output embeddings of the option against the question and the end of the prompt
ggml_tensor * anchor = ggml_l2_norm(ctx0, ggml_add(ctx0, question, global), eps);
anchor = ggml_get_rows(ctx0, anchor, inp->option_question);
ggml_tensor * prior = ggml_sum_rows(ctx0, ggml_mul(ctx0, ggml_l2_norm(ctx0, lexical, eps), anchor)); // [1, n_options]
// joint: each option against the vector of its question
options = build_head_norm(options, model.option_norm);
ggml_tensor * field = ggml_get_rows(ctx0, fields, inp->option_question); // [n_embd_h, n_options]
ggml_tensor * cosine = ggml_sum_rows(ctx0, ggml_mul(ctx0, ggml_l2_norm(ctx0, field, eps), ggml_l2_norm(ctx0, options, eps)));
ggml_tensor * features = ggml_concat(ctx0, field, options, 0);
features = ggml_concat(ctx0, features, ggml_mul(ctx0, field, options), 0);
features = ggml_concat(ctx0, features, ggml_abs(ctx0, ggml_sub(ctx0, field, options)), 0);
ggml_tensor * residual = ggml_add(ctx0, ggml_mul_mat(ctx0, model.scorer, features), model.scorer_b);
residual = ggml_gelu_erf(ctx0, residual);
residual = ggml_add(ctx0, ggml_mul_mat(ctx0, model.scorer_out, residual), model.scorer_out_b); // [1, n_options]
auto scale = [&](int i) {
return ggml_view_1d(ctx0, model.scales, 1, i * ggml_element_size(model.scales));
};
ggml_tensor * joint = ggml_add(ctx0, ggml_mul(ctx0, cosine, scale(1)), residual);
return ggml_add(ctx0, ggml_mul(ctx0, prior, scale(0)), ggml_mul(ctx0, joint, scale(2)));
}
+93
View File
@@ -2391,6 +2391,99 @@ struct llama_model_qwen35 : public llama_model_base {
};
// Qwen3.5 backbone evaluated without memory, with a joint decision head on top of its hidden states
struct llama_model_clef : public llama_model_qwen35 {
llama_model_clef(const struct llama_model_params & params) : llama_model_qwen35(params) {}
void load_arch_hparams(llama_model_loader & ml) override;
void load_arch_tensors(llama_model_loader & ml) override;
struct attn {
ggml_tensor * wq = nullptr;
ggml_tensor * bq = nullptr;
ggml_tensor * wk = nullptr;
ggml_tensor * bk = nullptr;
ggml_tensor * wv = nullptr;
ggml_tensor * bv = nullptr;
ggml_tensor * wo = nullptr;
ggml_tensor * bo = nullptr;
};
struct norm {
ggml_tensor * w = nullptr;
ggml_tensor * b = nullptr;
};
// the first n_layer_routing blocks have no self attention
struct head_layer {
norm self_norm;
attn self_attn;
norm cross_norm;
norm cross_norm_kv; // routing blocks only
attn cross_attn;
norm ffn_norm;
ggml_tensor * ffn_up = nullptr;
ggml_tensor * ffn_up_b = nullptr;
ggml_tensor * ffn_down = nullptr;
ggml_tensor * ffn_down_b = nullptr;
};
uint32_t n_layer_routing = 0;
uint32_t n_layer_joint = 0;
uint32_t n_head_decision = 0;
std::vector<head_layer> head_layers; // routing blocks, then joint blocks
norm hidden_norm;
norm option_summary_norm;
norm field_norm;
norm option_norm;
ggml_tensor * proj_memory = nullptr;
ggml_tensor * proj_question = nullptr;
ggml_tensor * proj_option_question = nullptr;
ggml_tensor * proj_global = nullptr;
ggml_tensor * proj_option_context = nullptr;
ggml_tensor * proj_option_lexical = nullptr;
ggml_tensor * scales = nullptr; // prior scale, joint scale, residual gate
ggml_tensor * scorer = nullptr;
ggml_tensor * scorer_b = nullptr;
ggml_tensor * scorer_out = nullptr;
ggml_tensor * scorer_out_b = nullptr;
class input_decision;
struct graph : public llm_build_delta_net_base {
graph(const llama_model & model, const llm_graph_params & params);
private:
llm_graph_input_attn_no_cache * build_attn_inp_causal();
ggml_tensor * build_layer_attn(
llm_graph_input_attn_no_cache * inp_attn,
ggml_tensor * cur,
ggml_tensor * inp_pos,
int * sections,
int il);
ggml_tensor * build_layer_attn_linear(
ggml_tensor * cur,
int il);
// hidden: [n_embd, n_tokens], returns one score per option: [1, n_options]
ggml_tensor * build_head(
ggml_tensor * hidden,
input_decision * inp);
ggml_tensor * build_head_norm(ggml_tensor * cur, const norm & n);
ggml_tensor * build_head_attn(ggml_tensor * q, ggml_tensor * kv, const attn & a);
ggml_tensor * build_head_ffn (ggml_tensor * cur, const head_layer & layer);
const llama_model_clef & model;
};
std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
};
struct llama_model_qwen4exp : public llama_model_base {
llama_model_qwen4exp(const struct llama_model_params & params) : llama_model_base(params) {}
+7 -16
View File
@@ -682,7 +682,7 @@ public:
ggml_tensor * k_idxs = nullptr; // I64 [n_tokens]
ggml_tensor * pool_cells = nullptr; // I32 [n_pool] cell caching each block's pooled key
ggml_tensor * pool_idxs = nullptr; // I32 [kpool, n_pool] member cells per block, n_kv sentinel for the padded blocks
ggml_tensor * pool_mask = nullptr; // F32 [n_pool, n_tokens]
ggml_tensor * pool_mask = nullptr; // F16 [n_pool, n_tokens]
ggml_tensor * tail_idxs = nullptr; // I32 [kpool - 1, n_tokens]
ggml_tensor * new_pool_idxs = nullptr; // I32 [kpool, n_new] members of the blocks to re-pool this ubatch
ggml_tensor * new_pool_rep = nullptr; // I64 [n_new] cell to write each new pooled key into
@@ -708,7 +708,7 @@ llama_model_qwen4exp::llm_graph_input_kpool * llama_model_qwen4exp::graph::build
inp->k_idxs = mctx_idx->build_input_k_idxs(ctx0, ubatch);
inp->pool_cells = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pool);
inp->pool_idxs = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, kpool, n_pool);
inp->pool_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_pool, n_tokens);
inp->pool_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F16, n_pool, n_tokens);
inp->tail_idxs = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, kpool - 1, n_tokens);
ggml_set_input(inp->pool_cells);
ggml_set_input(inp->pool_idxs);
@@ -812,20 +812,11 @@ ggml_tensor * llama_model_qwen4exp::graph::build_qsa_sel(
ext_factor, attn_factor, beta_fast, beta_slow);
cb(q, "indexer_q", il);
// the reference sums the rectified head scores unweighted, scaled by 1/sqrt(head_dim)
// one product for all heads, then the heads are summed as slices, so nothing is transposed
ggml_tensor * kq = ggml_mul_mat(ctx0,
ggml_reshape_2d(ctx0, pooled, idx_dim, n_pool),
ggml_reshape_2d(ctx0, q, idx_dim, n_idx_h*n_tokens)); // [n_pool, n_idx_h*n_tokens]
kq = ggml_relu(ctx0, ggml_reshape_3d(ctx0, kq, n_pool, n_idx_h, n_tokens));
ggml_tensor * score = nullptr;
for (int64_t h = 0; h < n_idx_h; ++h) {
ggml_tensor * slice = ggml_view_2d(ctx0, kq, n_pool, n_tokens, kq->nb[2], h*kq->nb[1]);
score = score ? ggml_add(ctx0, score, slice) : ggml_cont(ctx0, slice);
}
score = ggml_scale(ctx0, score, 1.0f/sqrtf((float) idx_dim));
score = ggml_add(ctx0, score, inp_kpool->pool_mask); // [n_pool, n_tokens]
// the reference sums the rectified head scores unweighted, scaled by 1/sqrt(head_dim),
// which is the lightning indexer with every head weight set to that scale
ggml_tensor * weights = ggml_fill(ctx0, ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_idx_h, n_tokens), 1.0f/sqrtf((float) idx_dim));
ggml_tensor * score = ggml_lightning_indexer(ctx0, q, pooled, weights, inp_kpool->pool_mask); // [n_pool, n_tokens]
res->add_fused_node({LLM_FUSED_OP_LIGHTNING_INDEXER, score, il});
cb(score, "indexer_score", il);
const int64_t n_top_pool = std::min<int64_t>(n_pool, hparams.indexer_top_k / kpool);

Some files were not shown because too many files have changed in this diff Show More