diff --git a/.github/workflows/behavior-check.yml b/.github/workflows/behavior-check.yml
index 73c3b8fe223a..544fb6caf600 100644
--- a/.github/workflows/behavior-check.yml
+++ b/.github/workflows/behavior-check.yml
@@ -140,6 +140,11 @@ jobs:
source .venv/Scripts/activate
pip install -r requirements/requirements-convert_hf_to_gguf.txt
+ - name: Install LLVM
+ run: |
+ choco install llvm --version=22.1.8
+ echo "C:\Program Files\LLVM\bin" >> $env:GITHUB_PATH
+
- name: Build
id: cmake_build
shell: cmd
diff --git a/.github/workflows/build-opencl.yml b/.github/workflows/build-opencl.yml
index ab9ed3ec2cb6..4b45ae666b4e 100644
--- a/.github/workflows/build-opencl.yml
+++ b/.github/workflows/build-opencl.yml
@@ -61,6 +61,11 @@ jobs:
run: |
choco install ninja
+ - name: Install LLVM
+ run: |
+ choco install llvm --version=22.1.8
+ echo "C:\Program Files\LLVM\bin" >> $env:GITHUB_PATH
+
- name: Install OpenCL Headers and Libs
id: install_opencl
run: |
diff --git a/.github/workflows/build-vulkan.yml b/.github/workflows/build-vulkan.yml
index d2375b0141fb..b699f2a1723c 100644
--- a/.github/workflows/build-vulkan.yml
+++ b/.github/workflows/build-vulkan.yml
@@ -213,6 +213,11 @@ jobs:
run: |
choco install ninja
+ - name: Install LLVM
+ run: |
+ choco install llvm --version=22.1.8
+ echo "C:\Program Files\LLVM\bin" >> $env:GITHUB_PATH
+
- name: Build
id: cmake_build
run: |
diff --git a/README.md b/README.md
index 0bdcbc8ced79..8f2b10141540 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@
LLM inference in C/C++
[](https://opensource.org/licenses/MIT)
-[](https://github.com/ggml-org/llama.cpp/releases?q=tag:v1)
+[](https://github.com/ggml-org/llama.cpp/releases?q=tag:v1)
[](https://github.com/ggml-org/llama.cpp/releases?q=b)
[](https://github.com/ggml-org/llama.cpp/actions/workflows/server.yml)
[](https://github.com/ggml-org/llama.cpp/actions/workflows/docker.yml)
diff --git a/conversion/__init__.py b/conversion/__init__.py
index 1182c06be3c0..253a18fd48aa 100644
--- a/conversion/__init__.py
+++ b/conversion/__init__.py
@@ -78,6 +78,7 @@
"DistilBertForMaskedLM": "bert",
"DistilBertForSequenceClassification": "bert",
"DistilBertModel": "bert",
+ "DomynEdgeForCausalLM": "domynedge",
"Dots1ForCausalLM": "dots1",
"Dots3NoteForCausalLM": "dots3",
"Dots3NoteForConditionalGeneration": "dots3",
diff --git a/conversion/base.py b/conversion/base.py
index 7cfbff321e0d..540a66ba5098 100644
--- a/conversion/base.py
+++ b/conversion/base.py
@@ -2470,6 +2470,9 @@ def get_vocab_base_pre(self, tokenizer) -> str:
if chkhsh == "5635e1c8d621dff7b4825f41f2f97b6ceb39bcf2d5c541db28577080d6efd5e5":
# ref: https://huggingface.co/thinkingmachines/Inkling-2-Small
res = "inkling2"
+ if chkhsh == "32d85c31273f8019248f2559fed492d929ea28b17e51d81d3bb36fff23ca72b3":
+ # ref: https://huggingface.co/domyn/Domyn-Edge-Teacher-IF
+ res = "domyn-edge"
if res is None:
logger.warning("\n")
diff --git a/conversion/domynedge.py b/conversion/domynedge.py
new file mode 100644
index 000000000000..df07add83cbf
--- /dev/null
+++ b/conversion/domynedge.py
@@ -0,0 +1,53 @@
+from .base import TextModel, ModelBase
+import gguf
+
+
+@ModelBase.register("DomynEdgeForCausalLM")
+@ModelBase.example("domyn/Domyn-Edge-Teacher-IF")
+class DomynEdgeModel(TextModel):
+ model_arch = gguf.MODEL_ARCH.DOMYN_EDGE
+
+ def set_gguf_parameters(self):
+ super().set_gguf_parameters()
+ hparams = self.hparams
+ self.gguf_writer.add_vocab_size(hparams["vocab_size"])
+
+ position_layers = hparams["position_layers"] # list of "rope"/"nope" strings per layer
+ head_sizes = hparams["head_sizes"] # list of int head dims per layer
+
+ # Per-layer rope/SWA pattern: True="rope", False="nope"
+ rope_pattern = [v == "rope" for v in position_layers]
+ self.gguf_writer.add_rope_pattern(rope_pattern)
+ self.gguf_writer.add_sliding_window_pattern(rope_pattern) # SWA <-> RoPE in domyn-edge
+
+ # window_sizes entries are flash-attn style [left, right] (current token excluded; -1 = full attention),
+ # while llama.cpp's n_swa counts the current token, hence the +1 (matches HF's sliding_window=1025)
+ swa_window = hparams.get("sliding_window")
+ if swa_window is None:
+ window_sizes = hparams.get("window_sizes") # list of [size, offset] pairs per layer
+ if window_sizes is None:
+ raise KeyError("Neither `sliding_window` nor `window_sizes` found among domyn edge hyperparameters")
+ left = next((ws[0] for v, ws in zip(position_layers, window_sizes) if v == "rope"), None)
+ swa_window = left + 1 if left is not None and left > 0 else None
+ if swa_window is None:
+ raise ValueError("No sliding window size found among domyn edge hyperparameters")
+ self.gguf_writer.add_sliding_window(swa_window)
+
+ # Head dims differ between full-attention ("nope") and SWA ("rope") layers
+ full_head_dims = {h for v, h in zip(position_layers, head_sizes) if v == "nope"}
+ swa_head_dims = {h for v, h in zip(position_layers, head_sizes) if v == "rope"}
+
+ if len(full_head_dims) != 1:
+ raise ValueError(f"Expected one head dimension for full attention layers, found {len(full_head_dims)}")
+ full_head_dim = full_head_dims.pop()
+
+ self.gguf_writer.add_key_length(full_head_dim)
+ self.gguf_writer.add_value_length(full_head_dim)
+
+ if len(swa_head_dims) != 1:
+ raise ValueError(f"Expected one head dimension for SWA attention layers, found {len(swa_head_dims)}")
+ swa_head_dim = swa_head_dims.pop()
+
+ self.gguf_writer.add_key_length_swa(swa_head_dim)
+ self.gguf_writer.add_value_length_swa(swa_head_dim)
+ self.gguf_writer.add_rope_dimension_count_swa(swa_head_dim)
diff --git a/convert_hf_to_gguf_update.py b/convert_hf_to_gguf_update.py
index ff5fca4fff89..91fb418a5f2d 100755
--- a/convert_hf_to_gguf_update.py
+++ b/convert_hf_to_gguf_update.py
@@ -168,6 +168,7 @@ class TOKENIZER_TYPE(IntEnum):
{"name": "mmbert", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/jhu-clsp/mmBERT-base", },
{"name": "k2-horizon", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/IFM/K2-Horizon-36B", },
{"name": "inkling2", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/thinkingmachines/Inkling-2-Small", },
+ {"name": "domyn-edge", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/domyn/Domyn-Edge-Teacher-IF", },
]
# some models are known to be broken upstream, so we will skip them as exceptions
@@ -227,8 +228,6 @@ def download_model(model):
repo = model["repo"]
tokt = model["tokt"]
- os.makedirs(f"models/tokenizers/{name}", exist_ok=True)
-
files = ["config.json", "tokenizer.json", "tokenizer_config.json"]
if name == "gpt-4o":
@@ -243,6 +242,7 @@ def download_model(model):
if os.path.isdir(repo):
# If repo is a path on the file system, copy the directory
+ os.makedirs(f"models/tokenizers/{name}", exist_ok=True)
for file in files:
src_path = os.path.join(repo, file)
dst_path = f"models/tokenizers/{name}/{file}"
@@ -484,7 +484,7 @@ def get_vocab_base_pre(self, tokenizer) -> str:
tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}", use_fast=False)
else:
tokenizer = AutoTokenizer.from_pretrained(f"models/tokenizers/{name}")
- except (OSError, TypeError) as e:
+ except (OSError, TypeError, ValueError) as e:
logger.error(f"Failed to load tokenizer for model {name}. Error: {e}")
continue # Skip this model and continue with the next one in the loop
diff --git a/docs/backend/OPENVINO.md b/docs/backend/OPENVINO.md
index 0b5615ca2460..a3a94aa16fb6 100644
--- a/docs/backend/OPENVINO.md
+++ b/docs/backend/OPENVINO.md
@@ -195,6 +195,7 @@ The models below were tested with `llama-cli` and `Q4_K_M` files on an Intel® C
| [bartowski/prism-ml_Bonsai-8B-unpacked-Q4_K_M](https://huggingface.co/bartowski/prism-ml_Bonsai-8B-unpacked-GGUF) | ✓ | ✓ | ✓ |
| [LiquidAI/LFM2.5-2.6B-Q4_K_M](https://huggingface.co/LiquidAI/LFM2.5-2.6B-GGUF) | ✓ | ✓ | ✗ |
| [bartowski/inclusionAI_Ling-mini-2.0-Q4_K_M](https://huggingface.co/bartowski/inclusionAI_Ling-mini-2.0-GGUF) | ✓ | ✓ | ✗ |
+| [tiiuae/Falcon-H1-0.5B-Instruct-Q4_K_M](https://huggingface.co/tiiuae/Falcon-H1-0.5B-Instruct-GGUF) | ✓ | ✓ | ✗ |
| | | | |
| [Qwen/Qwen3-Embedding-0.6B-Q8_0](https://huggingface.co/Qwen/Qwen3-Embedding-0.6B-GGUF) | ✓ | ✓ | ✗ |
| [ggml-org/bge-m3-Q8_0](https://huggingface.co/ggml-org/bge-m3-Q8_0-GGUF) | ✓ | ✓ | ✗ |
@@ -990,6 +991,7 @@ build\ReleaseOV\bin\llama-simple.exe -m "C:\models\Llama-3.2-1B-Instruct-Q4_K_M.
- Multimodal features (audio/image/video) are a work in progress.
- Embedding models: reranking models are not validated.
- Llama.cpp tool coverage across CPU/GPU/NPU is not uniform.
+- Mamba-based models (e.g. Falcon-H1): `llama-perplexity` with several sequences per batch fails; set `-b` to the same value as `-c`. GPU accuracy is slightly lower than CPU.
**GPU-specific**
@@ -1012,6 +1014,7 @@ build\ReleaseOV\bin\llama-simple.exe -m "C:\models\Llama-3.2-1B-Instruct-Q4_K_M.
- `llama-perplexity`: requires `-b 512` or smaller.
- Flash attention must stay on, which is the default (`-fa auto`). With `-fa off`, the run fails on the NPU.
- `llama-embedding` fails on the NPU.
+- Mamba-based models (e.g. Falcon-H1) fail on the NPU with `Failed to set tensor` (element type mismatch).
- Lunar Lake NPU: decode slows down sharply when the context is large. The context size at which this starts depends on the model.
- **Workaround:** Pass the smallest context that fits the use, e.g. `-c 512`, or `-c 256` for larger models.
- Panther Lake NPU: the load of a Qwen3.5 model can end with a segmentation fault in the NPU compiler of OpenVINO 2026.4.1.
diff --git a/docs/ops.md b/docs/ops.md
index 7cd9bc2f8c5f..88e92c701d10 100644
--- a/docs/ops.md
+++ b/docs/ops.md
@@ -24,7 +24,7 @@ Legend:
| ARGSORT | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CEIL | ❌ | ❌ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| CLAMP | ❌ | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
-| COL2IM_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
+| COL2IM_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | 🟡 | ❌ | ❌ |
| CONCAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
| CONT | ❌ | 🟡 | ✅ | ✅ | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | 🟡 | ❌ | ❌ |
| CONV_1D_DW | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
@@ -90,7 +90,7 @@ Legend:
| PAD | ❌ | 🟡 | ✅ | 🟡 | ❌ | ✅ | 🟡 | ✅ | 🟡 | 🟡 | ✅ | ✅ | ❌ | ❌ |
| PAD_REFLECT_1D | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ | ❌ |
| POOL_1D | ❌ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
-| POOL_2D | ❌ | 🟡 | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ |
+| POOL_2D | ❌ | 🟡 | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ |
| REGLU | ❌ | ✅ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ❌ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| RELU | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | ✅ | 🟡 | ✅ | ✅ | ✅ | ❌ | ❌ |
| REPEAT | ❌ | ✅ | ✅ | 🟡 | 🟡 | 🟡 | ✅ | 🟡 | ✅ | ✅ | ✅ | 🟡 | ❌ | ❌ |
diff --git a/docs/ops/WebGPU.csv b/docs/ops/WebGPU.csv
index 2ae3a1d36dd8..2ad8ded659ec 100644
--- a/docs/ops/WebGPU.csv
+++ b/docs/ops/WebGPU.csv
@@ -1155,134 +1155,134 @@
"WebGPU: WebGPU","SET_ROWS","type_src=f16,type_dst=f16,type_idx=i32,ne=[1,8,1,3],nr23=[1,1],r=2,v=0","support","0","no","WebGPU"
"WebGPU: WebGPU","SET_ROWS","type_src=f16,type_dst=f16,type_idx=i64,ne=[1,8,1,3],nr23=[1,1],r=2,v=1","support","0","no","WebGPU"
"WebGPU: WebGPU","SET_ROWS","type_src=f16,type_dst=f16,type_idx=i32,ne=[1,8,1,3],nr23=[1,1],r=2,v=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=1","support","0","no","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=avg,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=1,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=1,k1=3,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=1,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=1,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=1,p0=1,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=0,p1=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","POOL_2D","pool_type=max,type_input=f32,ne_input=[10,10,3,1],k0=3,k1=3,s0=2,s1=2,p0=1,p1=1","support","1","yes","WebGPU"
"WebGPU: WebGPU","POOL_1D","pool_type=avg,type_input=f32,ne_input=[10,3,2,1],k0=1,s0=1,p0=0","support","0","no","WebGPU"
"WebGPU: WebGPU","POOL_1D","pool_type=avg,type_input=f32,ne_input=[11,1,3,2],k0=1,s0=1,p0=0","support","0","no","WebGPU"
"WebGPU: WebGPU","POOL_1D","pool_type=avg,type_input=f32,ne_input=[128,2,1,3],k0=1,s0=1,p0=0","support","0","no","WebGPU"
@@ -5426,39 +5426,44 @@
"WebGPU: WebGPU","CONV_TRANSPOSE_1D","ne_input=[3,2,1,1],ne_kernel=[3,2,2,1],s0=1,p0=0,d0=1","support","0","no","WebGPU"
"WebGPU: WebGPU","CONV_TRANSPOSE_1D","ne_input=[3,2,1,1],ne_kernel=[3,1,2,1],s0=1,p0=0,d0=1","support","0","no","WebGPU"
"WebGPU: WebGPU","CONV_TRANSPOSE_1D","ne_input=[2,1,1,1],ne_kernel=[3,1,1,1],s0=1,p0=0,d0=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=16,OC=32,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=4,OC=3,T_in=7,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=5,T_in=13,s0=1,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=6,OC=4,T_in=11,s0=3,p0=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=2,OC=3,T_in=9,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=5,OC=4,T_in=11,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=8,OC=4,T_in=13,s0=4,p0=2","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=4,OC=3,T_in=1,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=16,OC=1,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=5,T_in=13,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f32,K=8,OC=2,T_in=3,s0=2,p0=5","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=16,OC=32,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=4,OC=3,T_in=7,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=1,OC=5,T_in=13,s0=1,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=6,OC=4,T_in=11,s0=3,p0=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=2,OC=3,T_in=9,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=5,OC=4,T_in=11,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=8,OC=4,T_in=13,s0=4,p0=2","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=4,OC=3,T_in=1,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=16,OC=1,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=1,OC=5,T_in=13,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=f16,K=8,OC=2,T_in=3,s0=2,p0=5","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=16,OC=32,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=4,OC=3,T_in=7,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=1,OC=5,T_in=13,s0=1,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=6,OC=4,T_in=11,s0=3,p0=1","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=2,OC=3,T_in=9,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=5,OC=4,T_in=11,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=8,OC=4,T_in=13,s0=4,p0=2","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=4,OC=3,T_in=1,s0=2,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=16,OC=1,T_in=197,s0=8,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=1,OC=5,T_in=13,s0=3,p0=0","support","0","no","WebGPU"
-"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=8,OC=2,T_in=3,s0=2,p0=5","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=16,OC=32,T_in=197,s0=8,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=4,OC=3,T_in=7,s0=2,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=5,T_in=13,s0=1,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=6,OC=4,T_in=11,s0=3,p0=1,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=2,OC=3,T_in=9,s0=3,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=5,OC=4,T_in=11,s0=2,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=8,OC=4,T_in=13,s0=4,p0=2,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=4,OC=3,T_in=1,s0=2,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=16,OC=1,T_in=197,s0=8,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=5,T_in=13,s0=3,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=8,OC=2,T_in=3,s0=2,p0=5,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=16,OC=32,T_in=197,s0=8,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=4,OC=3,T_in=7,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=1,OC=5,T_in=13,s0=1,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=6,OC=4,T_in=11,s0=3,p0=1,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=2,OC=3,T_in=9,s0=3,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=5,OC=4,T_in=11,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=8,OC=4,T_in=13,s0=4,p0=2,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=4,OC=3,T_in=1,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=16,OC=1,T_in=197,s0=8,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=1,OC=5,T_in=13,s0=3,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f16,K=8,OC=2,T_in=3,s0=2,p0=5,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=16,OC=32,T_in=197,s0=8,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=4,OC=3,T_in=7,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=1,OC=5,T_in=13,s0=1,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=6,OC=4,T_in=11,s0=3,p0=1,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=2,OC=3,T_in=9,s0=3,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=5,OC=4,T_in=11,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=8,OC=4,T_in=13,s0=4,p0=2,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=4,OC=3,T_in=1,s0=2,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=16,OC=1,T_in=197,s0=8,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=1,OC=5,T_in=13,s0=3,p0=0,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=bf16,K=8,OC=2,T_in=3,s0=2,p0=5,view=0","support","0","no","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=5,OC=3,T_in=17,s0=2,p0=1,view=1","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=1,T_in=255,s0=1,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=1,T_in=256,s0=1,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=1,T_in=257,s0=1,p0=0,view=0","support","1","yes","WebGPU"
+"WebGPU: WebGPU","COL2IM_1D","type=f32,K=1,OC=1,T_in=16777217,s0=1,p0=0,view=0","support","1","yes","WebGPU"
"WebGPU: WebGPU","CONV_TRANSPOSE_2D","kernel_type=f32,ne_input=[3,2,3,1],ne_kernel=[2,2,1,3],stride=1","support","0","no","WebGPU"
"WebGPU: WebGPU","CONV_TRANSPOSE_2D","kernel_type=f32,ne_input=[10,10,9,1],ne_kernel=[3,3,1,9],stride=2","support","0","no","WebGPU"
"WebGPU: WebGPU","CONV_TRANSPOSE_2D","kernel_type=f32,ne_input=[129,63,35,1],ne_kernel=[3,3,48,35],stride=1","support","0","no","WebGPU"
diff --git a/ggml/include/ggml-rpc.h b/ggml/include/ggml-rpc.h
index da1d4e1f167b..168ee774e488 100644
--- a/ggml/include/ggml-rpc.h
+++ b/ggml/include/ggml-rpc.h
@@ -8,10 +8,10 @@ extern "C" {
#define RPC_PROTO_MAJOR_VERSION 10
#define RPC_PROTO_MINOR_VERSION 0
-#define RPC_PROTO_PATCH_VERSION 0
+#define RPC_PROTO_PATCH_VERSION 1
#ifdef __cplusplus
-static_assert(GGML_OP_COUNT == 99, "GGML_OP_COUNT has changed - update RPC_PROTO_PATCH_VERSION");
+static_assert(GGML_OP_COUNT == 100, "GGML_OP_COUNT has changed - update RPC_PROTO_PATCH_VERSION");
#endif
#define GGML_RPC_MAX_SERVERS 16
diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h
index 2341fa6c107e..dc9997514d06 100644
--- a/ggml/include/ggml.h
+++ b/ggml/include/ggml.h
@@ -429,6 +429,8 @@ extern "C" {
GGML_OP_GLU,
+ GGML_OP_SLEEP,
+
GGML_OP_COUNT,
};
@@ -1314,12 +1316,6 @@ extern "C" {
// indirect matrix multiplication
// an id of -1 skips the slot and zeroes the matching row of the result
- //
- // [TAG_MUL_MAT_ID_DUPLICATE_ID]
- // note: this op has undefined behaviour if a given `a` is referenced more than once for a given `b`
- // (i.e. duplicate experts per token are not guaranteed to be supported in all backends)
- // TODO: add backend support: https://github.com/ggml-org/llama.cpp/pull/29998#issuecomment-6153742533
- // the CPU backend skips negative ids, their rows of the result are zero
GGML_API struct ggml_tensor * ggml_mul_mat_id(
struct ggml_context * ctx,
struct ggml_tensor * as,
@@ -2599,6 +2595,12 @@ extern "C" {
int n_tasks,
void * userdata);
+ // Busy-wait operator for scheduling and backend testing.
+ GGML_API struct ggml_tensor * ggml_sleep(
+ struct ggml_context * ctx,
+ struct ggml_tensor * a, // wait after this tensor has executed, return value is simply a full view of this tensor
+ int32_t us); // amount of time to wait in microseconds
+
// loss function
GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp
index 2e2f9c036560..d598ca3d86d1 100644
--- a/ggml/src/ggml-backend.cpp
+++ b/ggml/src/ggml-backend.cpp
@@ -2584,6 +2584,7 @@ bool ggml_op_alloc_size_may_expand(enum ggml_op op) {
case GGML_OP_ARGSORT:
case GGML_OP_TOP_K:
case GGML_OP_ARGMAX:
+ case GGML_OP_CONV_2D:
return true;
default:
return false;
diff --git a/ggml/src/ggml-cann/ggml-cann.cpp b/ggml/src/ggml-cann/ggml-cann.cpp
index 7e692f1b545f..0adc8680f5a4 100644
--- a/ggml/src/ggml-cann/ggml-cann.cpp
+++ b/ggml/src/ggml-cann/ggml-cann.cpp
@@ -2501,6 +2501,9 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
break;
case GGML_OP_SET_ROWS:
{
+ if (op->src[0]->type == GGML_TYPE_BF16) {
+ return false;
+ }
switch (op->type) {
case GGML_TYPE_F32:
case GGML_TYPE_F16:
@@ -2553,6 +2556,9 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
if (((const int32_t *) op->op_params)[15] != 0) {
return false; // FIXME: support ggml_rope_set_offset
}
+ if (op->src[0]->type != GGML_TYPE_F32 && op->src[0]->type != GGML_TYPE_F16) {
+ return false;
+ }
if (op->src[0]->ne[0] > 896) {
return false;
}
diff --git a/ggml/src/ggml-cpu/CMakeLists.txt b/ggml/src/ggml-cpu/CMakeLists.txt
index 1e759ffcaf83..75bf04cec6de 100644
--- a/ggml/src/ggml-cpu/CMakeLists.txt
+++ b/ggml/src/ggml-cpu/CMakeLists.txt
@@ -670,7 +670,7 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
set(GGML_CPU_KLEIDIAI_AARCH64 OFF)
if (GGML_SYSTEM_ARCH STREQUAL "ARM" AND
- (APPLE OR WIN32 OR CMAKE_SYSTEM_NAME MATCHES "^(Linux|Android)$") AND
+ (APPLE OR WIN32 OR CMAKE_SYSTEM_NAME MATCHES "^(Linux|Android|OHOS)$") AND
(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|ARM64|arm64-v8a)$" OR
CMAKE_OSX_ARCHITECTURES MATCHES "arm64" OR
CMAKE_GENERATOR_PLATFORM_LWR STREQUAL "arm64" OR
diff --git a/ggml/src/ggml-cpu/ggml-cpu.c b/ggml/src/ggml-cpu/ggml-cpu.c
index fbc9f26a9bb3..5eb39ee2c357 100644
--- a/ggml/src/ggml-cpu/ggml-cpu.c
+++ b/ggml/src/ggml-cpu/ggml-cpu.c
@@ -2048,6 +2048,10 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
{
ggml_compute_forward_fill(params, tensor);
} break;
+ case GGML_OP_SLEEP:
+ {
+ ggml_compute_forward_sleep(params, tensor);
+ } break;
case GGML_OP_FLASH_ATTN_EXT:
{
ggml_compute_forward_flash_attn_ext(params, tensor);
@@ -2320,6 +2324,7 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
case GGML_OP_REPEAT:
case GGML_OP_REPEAT_BACK:
case GGML_OP_LEAKY_RELU:
+ case GGML_OP_SLEEP:
{
n_tasks = 1;
} break;
@@ -2949,7 +2954,8 @@ struct ggml_cplan ggml_graph_plan(
} break;
case GGML_OP_SET_ROWS:
{
- if (node->src[0]->type == GGML_TYPE_F16 && node->type != GGML_TYPE_F16) {
+ if ((node->src[0]->type == GGML_TYPE_F16 && node->type != GGML_TYPE_F16) ||
+ (node->src[0]->type == GGML_TYPE_BF16 && node->type != GGML_TYPE_BF16)) {
cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
}
} break;
diff --git a/ggml/src/ggml-cpu/llamafile/sgemm.cpp b/ggml/src/ggml-cpu/llamafile/sgemm.cpp
index 68105f029fbf..5d660911316d 100644
--- a/ggml/src/ggml-cpu/llamafile/sgemm.cpp
+++ b/ggml/src/ggml-cpu/llamafile/sgemm.cpp
@@ -1474,6 +1474,282 @@ class tinyBLAS_Q0_VXE {
const int32_t ith;
const int32_t nth;
};
+
+template
+class tinyBLAS_QK_VXE {
+ public:
+ tinyBLAS_QK_VXE(int64_t k,
+ const TA * A, int64_t lda,
+ const block_q8_K * B, int64_t ldb,
+ float * C, int64_t ldc,
+ int32_t ith, int32_t nth)
+ : A(A), B(B), C(C), k(k), lda(lda), ldb(ldb), ldc(ldc), ith(ith), nth(nth) {
+ }
+
+ void matmul(int64_t m, int64_t n) {
+ const int64_t n4 = n / 4 * 4;
+ const int64_t n2 = n / 2 * 2;
+ gemm<4>(m, 0, n4);
+ gemm<2>(m, n4, n2);
+ gemm<1>(m, n2, n);
+ }
+
+ private:
+ // one block of A is loaded once and used for RN columns of B
+ template
+ void gemm(int64_t m, int64_t n0, int64_t n) {
+ const int64_t tiles = m * ((n - n0) / RN);
+ const int64_t duty = (tiles + nth - 1) / nth;
+ const int64_t start = duty * ith;
+ const int64_t end = MIN(start + duty, tiles);
+
+ for (int64_t job = start; job < end; ++job) {
+ const int64_t ii = job % m;
+ const int64_t jj = n0 + RN * (job / m);
+ float32x4_t v_sum[RN] = {};
+
+ for (int64_t l = 0; l < k; ++l) {
+ const TA & x = A[lda * ii + l];
+
+ int8x16_t v_x[n_group];
+ load(x, v_x);
+
+ // scale and minimum of the 16 groups, 8 groups per vector
+ int16x8_t v_scalel, v_scaleh;
+ int16x8_t v_minl, v_minh;
+ load_scale(x, v_scalel, v_scaleh, v_minl, v_minh);
+
+ int16x8_t v_scale[n_group];
+ for (int32_t g = 0; g < n_group; ++g) {
+ v_scale[g] = vec_splats((g < n_group / 2 ? v_scalel : v_scaleh)[g % (n_group / 2)]);
+ }
+
+ const int8_t * y_qs[RN];
+ for (int32_t j = 0; j < RN; ++j) {
+ y_qs[j] = B[ldb * (jj + j) + l].qs;
+ }
+
+ // the columns are in the inner loop, so that their sums do not wait for each other
+ int32x4_t v_dot[RN] = {};
+ for (int32_t g = 0; g < n_group; ++g) {
+ for (int32_t j = 0; j < RN; ++j) {
+ const int8x16_t v_y = vec_xl(0, y_qs[j] + 16 * g);
+ const int16x8_t v_xy = vec_meadd(v_x[g], v_y, vec_mulo(v_x[g], v_y));
+ v_dot[j] = vec_meadd(v_xy, v_scale[g], vec_moadd(v_xy, v_scale[g], v_dot[j]));
+ }
+ }
+
+ const float x_d = unhalf(x.d);
+ for (int32_t j = 0; j < RN; ++j) {
+ const block_q8_K & y = B[ldb * (jj + j) + l];
+ v_sum[j] = vec_madd(vec_float(v_dot[j]), vec_splats(x_d * y.d), v_sum[j]);
+ }
+
+ if (has_min) {
+ const float x_dmin = dmin(x);
+
+ for (int32_t j = 0; j < RN; ++j) {
+ const block_q8_K & y = B[ldb * (jj + j) + l];
+
+ const int16_t * y_bsums = y.bsums;
+ const int16x8_t v_bsumsl = vec_xl(0, y_bsums);
+ const int16x8_t v_bsumsh = vec_xl(0, y_bsums + n_group / 2);
+
+ const int32x4_t v_minsl = vec_meadd(v_bsumsl, v_minl, vec_mulo(v_bsumsl, v_minl));
+ const int32x4_t v_minsh = vec_meadd(v_bsumsh, v_minh, vec_mulo(v_bsumsh, v_minh));
+ v_sum[j] = vec_madd(vec_float(v_minsl + v_minsh), vec_splats(-x_dmin * y.d), v_sum[j]);
+ }
+ }
+ }
+
+ for (int32_t j = 0; j < RN; ++j) {
+ C[ldc * (jj + j) + ii] = hsum(v_sum[j]);
+ }
+ }
+ }
+
+ // a block has 16 groups of 16 weights, Q4_K and Q5_K share one scale and minimum per 2 groups
+ static constexpr int32_t n_group = QK_K / 16;
+
+ // Q3_K and Q6_K have no minimum, their weights are loaded as signed values.
+ static constexpr bool has_min = !std::is_same::value && !std::is_same::value;
+
+ static float dmin(const block_q2_K & x) { return unhalf(x.dmin); }
+ static float dmin(const block_q4_K & x) { return unhalf(x.dmin); }
+ static float dmin(const block_q5_K & x) { return unhalf(x.dmin); }
+ template static float dmin(const T &) { return 0.0f; }
+
+ static void load(const block_q2_K & x, int8x16_t * v_x) {
+ const uint8x16_t v_mask = vec_splats((uint8_t)0x03);
+ const uint8_t * x_qs = x.qs;
+
+ for (int32_t g = 0; g < n_group; ++g) {
+ const int32_t qs_offset = 16 * (2 * (g / 8) + g % 2);
+ const int32_t qs_shift = 2 * (g % 8 / 2);
+
+ const uint8x16_t v_qs = vec_xl(0, x_qs + qs_offset);
+ v_x[g] = (int8x16_t)vec_and(vec_sr(v_qs, qs_shift), v_mask);
+ }
+ }
+
+ static void load(const block_q3_K & x, int8x16_t * v_x) {
+ const uint8x16_t v_mask = vec_splats((uint8_t)0x03);
+ const uint8x16_t v_one = vec_splats((uint8_t)0x01);
+
+ const uint8_t * x_qs = x.qs;
+ const uint8_t * x_hmask = x.hmask;
+
+ for (int32_t g = 0; g < n_group; ++g) {
+ const int32_t qs_offset = 16 * (2 * (g / 8) + g % 2);
+ const int32_t qs_shift = 2 * (g % 8 / 2);
+ const int32_t hmask_offset = 16 * (g % 2);
+ const int32_t hmask_shift = g / 2;
+
+ const uint8x16_t v_qs = vec_xl(0, x_qs + qs_offset);
+ const uint8x16_t v_hmask = vec_xl(0, x_hmask + hmask_offset);
+ const uint8x16_t v_lo = vec_and(vec_sr(v_qs, qs_shift), v_mask);
+ const uint8x16_t v_hi = vec_sl(vec_and(vec_sr(v_hmask, hmask_shift), v_one), 2);
+ v_x[g] = (int8x16_t)vec_or(v_lo, v_hi) - vec_splats((int8_t)4);
+ }
+ }
+
+ static void load(const block_q4_K & x, int8x16_t * v_x) {
+ const uint8x16_t v_mask = vec_splats((uint8_t)0x0F);
+ const uint8_t * x_qs = x.qs;
+
+ for (int32_t g = 0; g < n_group; ++g) {
+ const int32_t qs_offset = 16 * (2 * (g / 4) + g % 2);
+ const bool qs_low = g % 4 < 2;
+
+ const uint8x16_t v_qs = vec_xl(0, x_qs + qs_offset);
+ v_x[g] = (int8x16_t)(qs_low ? vec_and(v_qs, v_mask) : vec_sr(v_qs, 4));
+ }
+ }
+
+ static void load(const block_q5_K & x, int8x16_t * v_x) {
+ const uint8x16_t v_mask = vec_splats((uint8_t)0x0F);
+ const uint8x16_t v_one = vec_splats((uint8_t)0x01);
+ const uint8_t * x_qs = x.qs;
+ const uint8_t * x_qh = x.qh;
+
+ for (int32_t g = 0; g < n_group; ++g) {
+ const int32_t qs_offset = 16 * (2 * (g / 4) + g % 2);
+ const bool qs_low = g % 4 < 2;
+ const int32_t qh_offset = 16 * (g % 2);
+ const int32_t qh_shift = g / 2;
+
+ const uint8x16_t v_qs = vec_xl(0, x_qs + qs_offset);
+ const uint8x16_t v_qh = vec_xl(0, x_qh + qh_offset);
+ const uint8x16_t v_lo = qs_low ? vec_and(v_qs, v_mask) : vec_sr(v_qs, 4);
+ const uint8x16_t v_hi = vec_sl(vec_and(vec_sr(v_qh, qh_shift), v_one), 4);
+ v_x[g] = (int8x16_t)vec_or(v_lo, v_hi);
+ }
+ }
+
+ static void load(const block_q6_K & x, int8x16_t * v_x) {
+ const uint8x16_t v_mask4 = vec_splats((uint8_t)0x0F);
+ const uint8x16_t v_mask2 = vec_splats((uint8_t)0x03);
+ const uint8_t * x_ql = x.ql;
+ const uint8_t * x_qh = x.qh;
+
+ for (int32_t g = 0; g < n_group; ++g) {
+ const int32_t ql_offset = 16 * (4 * (g / 8) + 2 * (g % 4 / 2) + g % 2);
+ const bool ql_low = g % 8 < 4;
+ const int32_t qh_offset = 16 * (2 * (g / 8) + g % 2);
+ const int32_t qh_shift = 2 * (g % 8 / 2);
+
+ const uint8x16_t v_ql = vec_xl(0, x_ql + ql_offset);
+ const uint8x16_t v_qh = vec_xl(0, x_qh + qh_offset);
+ const uint8x16_t v_lo = ql_low ? vec_and(v_ql, v_mask4) : vec_sr(v_ql, 4);
+ const uint8x16_t v_hi = vec_sl(vec_and(vec_sr(v_qh, qh_shift), v_mask2), 4);
+ v_x[g] = (int8x16_t)vec_or(v_lo, v_hi) - vec_splats((int8_t)32);
+ }
+ }
+
+ static void load_scale(const block_q2_K & x, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ const uint8_t * x_scales = x.scales;
+ const uint8x16_t v_s = vec_xl(0, x_scales);
+ const uint8x16_t v_scale = vec_and(v_s, vec_splats((uint8_t)0x0F));
+ const uint8x16_t v_min = vec_sr(v_s, 4);
+
+ v_scalel = (int16x8_t)vec_unpackh(v_scale);
+ v_scaleh = (int16x8_t)vec_unpackl(v_scale);
+ v_minl = (int16x8_t)vec_unpackh(v_min);
+ v_minh = (int16x8_t)vec_unpackl(v_min);
+ }
+
+ static void load_scale(const block_q3_K & x, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ // 6-bit scales: low 4 bits in scales[0..7], high 2 bits in scales[8..11]
+ const uint8x16_t v_lo_byte = { 4, 5, 6, 7, 8, 9, 10, 11, 4, 5, 6, 7, 8, 9, 10, 11 };
+ const uint8x16_t v_lo_shift = { 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4 };
+ const uint8x16_t v_hi_byte = { 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15 };
+ const uint8x16_t v_hi_shift = { 0, 0, 0, 0, 2, 2, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6 };
+
+ // the load starts 4 bytes before scales, so that it stays inside the block
+ const uint8_t * x_qs = x.qs;
+ const uint8x16_t v_s = vec_xl(0, x_qs + sizeof(x.qs) - 4);
+ const uint8x16_t v_lo = vec_and(vec_sr(vec_perm(v_s, v_s, v_lo_byte), v_lo_shift), vec_splats((uint8_t)0x0F));
+ const uint8x16_t v_hi = vec_sl(vec_and(vec_sr(vec_perm(v_s, v_s, v_hi_byte), v_hi_shift), vec_splats((uint8_t)0x03)), 4);
+ const int8x16_t v_scale = (int8x16_t)vec_or(v_lo, v_hi) - vec_splats((int8_t)32);
+
+ v_scalel = vec_unpackh(v_scale);
+ v_scaleh = vec_unpackl(v_scale);
+ v_minl = vec_splats((int16_t)0);
+ v_minh = vec_splats((int16_t)0);
+ }
+
+ static void load_scale(const block_q4_K & x, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ load_scale_min_k4(x.scales, v_scalel, v_scaleh, v_minl, v_minh);
+ }
+
+ static void load_scale(const block_q5_K & x, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ load_scale_min_k4(x.scales, v_scalel, v_scaleh, v_minl, v_minh);
+ }
+
+ static void load_scale(const block_q6_K & x, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ const int8_t * x_scales = x.scales;
+ const int8x16_t v_scale = vec_xl(0, x_scales);
+
+ v_scalel = vec_unpackh(v_scale);
+ v_scaleh = vec_unpackl(v_scale);
+ v_minl = vec_splats((int16_t)0);
+ v_minh = vec_splats((int16_t)0);
+ }
+
+ // 8 scales and 8 minimums of 6 bits, same packing as get_scale_min_k4 in ggml-quants.c
+ static void load_scale_min_k4(const uint8_t * q, int16x8_t & v_scalel, int16x8_t & v_scaleh, int16x8_t & v_minl, int16x8_t & v_minh) {
+ // bytes 0..3 and 4..7 have the high 2 bits of scale 4..7 and minimum 4..7, move them to bytes 8..11
+ const uint8x16_t v_hi_scale = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0 };
+ const uint8x16_t v_hi_min = { 0, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 7, 0, 0, 0, 0 };
+ // each scale and minimum is for 2 groups
+ const uint8x16_t v_group_scale = { 0, 0, 1, 1, 2, 2, 3, 3, 24, 24, 25, 25, 26, 26, 27, 27 };
+ const uint8x16_t v_group_min = { 4, 4, 5, 5, 6, 6, 7, 7, 24, 24, 25, 25, 26, 26, 27, 27 };
+
+ // the 4 bytes after the 12 scale bytes are in the same block
+ const uint8x16_t v_s = vec_xl(0, q);
+ const uint8x16_t v_lo = vec_and(v_s, vec_splats((uint8_t)0x3F));
+ const uint8x16_t v_hi = vec_sl(vec_sr(v_s, 6), 4);
+ const uint8x16_t v_scale_47 = vec_or(vec_and(v_s, vec_splats((uint8_t)0x0F)), vec_perm(v_hi, v_hi, v_hi_scale));
+ const uint8x16_t v_min_47 = vec_or(vec_sr(v_s, 4), vec_perm(v_hi, v_hi, v_hi_min));
+ const uint8x16_t v_scale = vec_perm(v_lo, v_scale_47, v_group_scale);
+ const uint8x16_t v_min = vec_perm(v_lo, v_min_47, v_group_min);
+
+ v_scalel = (int16x8_t)vec_unpackh(v_scale);
+ v_scaleh = (int16x8_t)vec_unpackl(v_scale);
+ v_minl = (int16x8_t)vec_unpackh(v_min);
+ v_minh = (int16x8_t)vec_unpackl(v_min);
+ }
+
+ const TA * const A;
+ const block_q8_K * const B;
+ float * const C;
+ const int64_t k;
+ const int64_t lda;
+ const int64_t ldb;
+ const int64_t ldc;
+ const int32_t ith;
+ const int32_t nth;
+};
#endif
#if defined(__ARM_FEATURE_DOTPROD)
@@ -4488,6 +4764,91 @@ bool llamafile_sgemm(const struct ggml_compute_params * params, int64_t m, int64
#endif
}
+ case GGML_TYPE_Q2_K: {
+ if (Btype != GGML_TYPE_Q8_K) {
+ return false;
+ }
+#if defined(__VXE__) || defined(__VXE2__)
+ tinyBLAS_QK_VXE tb{
+ k, (const block_q2_K *)A, lda,
+ (const block_q8_K *)B, ldb,
+ (float *)C, ldc,
+ params->ith, params->nth};
+ tb.matmul(m, n);
+ return true;
+#else
+ return false;
+#endif
+ }
+
+ case GGML_TYPE_Q3_K: {
+ if (Btype != GGML_TYPE_Q8_K) {
+ return false;
+ }
+#if defined(__VXE__) || defined(__VXE2__)
+ tinyBLAS_QK_VXE tb{
+ k, (const block_q3_K *)A, lda,
+ (const block_q8_K *)B, ldb,
+ (float *)C, ldc,
+ params->ith, params->nth};
+ tb.matmul(m, n);
+ return true;
+#else
+ return false;
+#endif
+ }
+
+ case GGML_TYPE_Q4_K: {
+ if (Btype != GGML_TYPE_Q8_K) {
+ return false;
+ }
+#if defined(__VXE__) || defined(__VXE2__)
+ tinyBLAS_QK_VXE tb{
+ k, (const block_q4_K *)A, lda,
+ (const block_q8_K *)B, ldb,
+ (float *)C, ldc,
+ params->ith, params->nth};
+ tb.matmul(m, n);
+ return true;
+#else
+ return false;
+#endif
+ }
+
+ case GGML_TYPE_Q5_K: {
+ if (Btype != GGML_TYPE_Q8_K) {
+ return false;
+ }
+#if defined(__VXE__) || defined(__VXE2__)
+ tinyBLAS_QK_VXE tb{
+ k, (const block_q5_K *)A, lda,
+ (const block_q8_K *)B, ldb,
+ (float *)C, ldc,
+ params->ith, params->nth};
+ tb.matmul(m, n);
+ return true;
+#else
+ return false;
+#endif
+ }
+
+ case GGML_TYPE_Q6_K: {
+ if (Btype != GGML_TYPE_Q8_K) {
+ return false;
+ }
+#if defined(__VXE__) || defined(__VXE2__)
+ tinyBLAS_QK_VXE tb{
+ k, (const block_q6_K *)A, lda,
+ (const block_q8_K *)B, ldb,
+ (float *)C, ldc,
+ params->ith, params->nth};
+ tb.matmul(m, n);
+ return true;
+#else
+ return false;
+#endif
+ }
+
case GGML_TYPE_IQ4_NL: {
if (Btype != GGML_TYPE_Q8_0)
return false;
diff --git a/ggml/src/ggml-cpu/ops.cpp b/ggml/src/ggml-cpu/ops.cpp
index 72efbc6e9fa9..cd8134740e19 100644
--- a/ggml/src/ggml-cpu/ops.cpp
+++ b/ggml/src/ggml-cpu/ops.cpp
@@ -2238,6 +2238,19 @@ void ggml_compute_forward_fill(const ggml_compute_params * params, ggml_tensor *
}
}
+// ggml_compute_forward_sleep
+
+void ggml_compute_forward_sleep(const ggml_compute_params * params, ggml_tensor * dst) {
+ if (params->ith != 0) {
+ return;
+ }
+
+ // same clock as ggml_time_us() callers; on macOS steady_clock is CLOCK_MONOTONIC_RAW, which can run faster
+ const int64_t t_end = ggml_time_us() + ggml_get_op_params_i32(dst, 0);
+
+ while (ggml_time_us() < t_end) {}
+}
+
// ggml_compute_tri
template
@@ -5518,7 +5531,7 @@ static void ggml_compute_forward_set_rows_impl(
assert(ne0 == nc);
assert(ne2 == ne02);
assert(ne3 == ne03);
- GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
+ GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
assert(ne02 % ne11 == 0);
assert(ne03 % ne12 == 0);
@@ -5565,6 +5578,20 @@ static void ggml_compute_forward_set_rows_impl(
from_float(wdata,
((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc);
}
+ } else if constexpr (std::is_same_v) {
+ if (dst->type == GGML_TYPE_BF16) {
+ memcpy(
+ ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3),
+ ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03),
+ rs);
+ } else {
+ float * wdata = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith;
+ ggml_bf16_to_fp32_row(
+ (const ggml_bf16_t *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03),
+ wdata, nc);
+ from_float(wdata,
+ ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc);
+ }
} else {
GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type));
}
@@ -5601,6 +5628,16 @@ void ggml_compute_forward_set_rows(
GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type));
}
} break;
+ case GGML_TYPE_BF16:
+ {
+ if (src1->type == GGML_TYPE_I64) {
+ ggml_compute_forward_set_rows_impl(params, dst);
+ } else if (src1->type == GGML_TYPE_I32) {
+ ggml_compute_forward_set_rows_impl(params, dst);
+ } else {
+ GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type));
+ }
+ } break;
default:
{
GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type));
@@ -6289,7 +6326,7 @@ static void rotate_pairs(const int64_t n, const int64_t n_offset, const float *
}
}
-template //float or ggml_fp16_t
+template //float, ggml_fp16_t or ggml_bf16_t
static void ggml_compute_forward_rope_flt(
const ggml_compute_params * params,
ggml_tensor * dst,
@@ -6299,7 +6336,7 @@ static void ggml_compute_forward_rope_flt(
const ggml_tensor * src1 = dst->src[1];
const ggml_tensor * src2 = dst->src[2];
- GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
+ GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
GGML_ASSERT(src1->type == GGML_TYPE_I32);
float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
@@ -6459,6 +6496,10 @@ void ggml_compute_forward_rope(
{
ggml_compute_forward_rope_flt(params, dst, true);
} break;
+ case GGML_TYPE_BF16:
+ {
+ ggml_compute_forward_rope_flt(params, dst, true);
+ } break;
case GGML_TYPE_F32:
{
ggml_compute_forward_rope_flt(params, dst, true);
@@ -6483,6 +6524,10 @@ void ggml_compute_forward_rope_back(
{
ggml_compute_forward_rope_flt(params, dst, false);
} break;
+ case GGML_TYPE_BF16:
+ {
+ ggml_compute_forward_rope_flt(params, dst, false);
+ } break;
case GGML_TYPE_F32:
{
ggml_compute_forward_rope_flt(params, dst, false);
diff --git a/ggml/src/ggml-cpu/ops.h b/ggml/src/ggml-cpu/ops.h
index aac527a64c54..3a4fa0707e49 100644
--- a/ggml/src/ggml-cpu/ops.h
+++ b/ggml/src/ggml-cpu/ops.h
@@ -84,6 +84,7 @@ void ggml_compute_forward_top_k(const struct ggml_compute_params * params, struc
void ggml_compute_forward_leaky_relu(const struct ggml_compute_params * params, struct ggml_tensor * dst);
void ggml_compute_forward_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst);
void ggml_compute_forward_fill(const struct ggml_compute_params * params, struct ggml_tensor * dst);
+void ggml_compute_forward_sleep(const struct ggml_compute_params * params, struct ggml_tensor * dst);
void ggml_compute_forward_flash_attn_ext(const struct ggml_compute_params * params, struct ggml_tensor * dst);
void ggml_compute_forward_flash_attn_back(
const struct ggml_compute_params * params,
diff --git a/ggml/src/ggml-cuda/batch-get.cu b/ggml/src/ggml-cuda/batch-copy.cu
similarity index 58%
rename from ggml/src/ggml-cuda/batch-get.cu
rename to ggml/src/ggml-cuda/batch-copy.cu
index 60c4747b967c..920b2b15d355 100644
--- a/ggml/src/ggml-cuda/batch-get.cu
+++ b/ggml/src/ggml-cuda/batch-copy.cu
@@ -1,17 +1,17 @@
-#include "batch-get.cuh"
+#include "batch-copy.cuh"
#include "common.cuh"
#include "ggml-cpp.h"
#include
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
-struct ggml_cuda_batch_get_descriptor {
+struct ggml_cuda_batch_copy_descriptor {
const unsigned char * src;
unsigned char * dst;
size_t size;
};
-static __global__ void ggml_cuda_batch_get_kernel(const ggml_cuda_batch_get_descriptor * copies, size_t count) {
+static __global__ void ggml_cuda_batch_copy_kernel(const ggml_cuda_batch_copy_descriptor * copies, size_t count) {
// Blocks along y cooperate on each descriptor.
const size_t index = blockIdx.x;
if (index >= count) {
@@ -50,11 +50,11 @@ static __global__ void ggml_cuda_batch_get_kernel(const ggml_cuda_batch_get_desc
}
}
-struct ggml_cuda_batch_get_mapping {
+struct ggml_cuda_batch_copy_mapping {
void * host = nullptr;
unsigned char * device = nullptr;
- ggml_cuda_batch_get_mapping(void * host, size_t size) {
+ ggml_cuda_batch_copy_mapping(void * host, size_t size) {
const cudaError_t err = cudaHostRegister(host, size, cudaHostRegisterMapped | cudaHostRegisterPortable);
if (err != cudaSuccess) {
(void) cudaGetLastError();
@@ -70,10 +70,10 @@ struct ggml_cuda_batch_get_mapping {
}
}
- ggml_cuda_batch_get_mapping(const ggml_cuda_batch_get_mapping &) = delete;
- ggml_cuda_batch_get_mapping & operator=(const ggml_cuda_batch_get_mapping &) = delete;
+ ggml_cuda_batch_copy_mapping(const ggml_cuda_batch_copy_mapping &) = delete;
+ ggml_cuda_batch_copy_mapping & operator=(const ggml_cuda_batch_copy_mapping &) = delete;
- ~ggml_cuda_batch_get_mapping() {
+ ~ggml_cuda_batch_copy_mapping() {
if (host) {
// Release only the registration acquired by this object.
CUDA_CHECK(cudaHostUnregister(host));
@@ -82,11 +82,12 @@ struct ggml_cuda_batch_get_mapping {
};
#endif
-bool ggml_cuda_get_tensor_n(int device,
- const ggml_tensor * const * tensors, void * const * data, const size_t * offsets,
+static bool ggml_cuda_copy_tensor_n(int device, cudaMemcpyKind direction,
+ const ggml_tensor * const * tensors, const void * const * data, const size_t * offsets,
const size_t * sizes, size_t n_tensors) {
#if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA)
GGML_UNUSED(device);
+ GGML_UNUSED(direction);
GGML_UNUSED(tensors);
GGML_UNUSED(data);
GGML_UNUSED(offsets);
@@ -94,14 +95,17 @@ bool ggml_cuda_get_tensor_n(int device,
GGML_UNUSED(n_tensors);
return false;
#else
+ const bool to_host = direction == cudaMemcpyDeviceToHost;
// Provisional dispatch values based on experiments.
constexpr size_t min_batch_reads = 128;
- constexpr size_t max_gather_read_bytes = 128 * 1024;
- // Collect non-empty CPU destination ranges.
+ constexpr size_t max_kernel_read_bytes = 128 * 1024;
+ // Collect destination ranges; repeated sources are allowed.
std::vector> ranges;
ranges.reserve(n_tensors);
size_t payload_bytes = 0;
- // Number of non-empty reads whose size is at most max_gather_read_bytes.
+ uintptr_t begin = UINTPTR_MAX;
+ uintptr_t end = 0;
+ // Number of non-empty reads whose size is at most max_kernel_read_bytes.
size_t n_reads_small = 0;
for (size_t i = 0; i < n_tensors; ++i) {
if (sizes[i] == 0) {
@@ -111,15 +115,19 @@ bool ggml_cuda_get_tensor_n(int device,
GGML_ASSERT(address <= UINTPTR_MAX - sizes[i]);
GGML_ASSERT(payload_bytes <= SIZE_MAX - sizes[i]);
payload_bytes += sizes[i];
- n_reads_small += sizes[i] <= max_gather_read_bytes;
- ranges.emplace_back(address, address + sizes[i]);
+ n_reads_small += sizes[i] <= max_kernel_read_bytes;
+ begin = std::min(begin, address);
+ end = std::max(end, address + sizes[i]);
+ const uintptr_t destination = to_host ? address : (uintptr_t) tensors[i]->data + offsets[i];
+ GGML_ASSERT(destination <= UINTPTR_MAX - sizes[i]);
+ ranges.emplace_back(destination, destination + sizes[i]);
}
if (ranges.empty()) {
return true;
}
// Total number of non-empty reads.
const size_t n_reads = ranges.size();
- if (n_reads > SIZE_MAX / sizeof(ggml_cuda_batch_get_descriptor) || n_reads > INT_MAX) {
+ if (n_reads > SIZE_MAX / sizeof(ggml_cuda_batch_copy_descriptor) || n_reads > INT_MAX) {
return false;
}
if (n_reads < 2 || (n_reads < min_batch_reads && n_reads_small == n_reads)) {
@@ -127,72 +135,85 @@ bool ggml_cuda_get_tensor_n(int device,
}
std::sort(ranges.begin(), ranges.end());
for (size_t i = 1; i < ranges.size(); ++i) {
- // Preserve the ordered copy path when CPU destinations overlap.
+ // Preserve the ordered copy path when destinations overlap.
if (ranges[i].first < ranges[i - 1].second) {
return false;
}
}
- // Find the CPU span to register, including gaps between destinations.
- const uintptr_t begin = ranges.front().first;
- const uintptr_t end = ranges.back().second;
- if (end - begin - payload_bytes > payload_bytes) {
+ // Limit gaps in the CPU span without underflow when sources overlap.
+ if (end - begin > payload_bytes && end - begin - payload_bytes > payload_bytes) {
return false;
}
- // Register and map the existing CPU destination so the kernel can write to it directly.
- ggml_cuda_batch_get_mapping mapping((void *) begin, end - begin);
+ // Register and map the CPU span for direct kernel access.
+ ggml_cuda_batch_copy_mapping mapping((void *) begin, end - begin);
if (!mapping.host) {
return false;
}
- // Reads selected for gather; zero means all reads use DMA.
- size_t n_reads_gather = mapping.device && n_reads_small >= min_batch_reads ? n_reads_small : 0;
+ // Reads selected for the kernel; zero means all reads use DMA.
+ size_t n_reads_kernel = mapping.device && n_reads_small >= min_batch_reads ? n_reads_small : 0;
// Keep descriptor storage alive until the final stream synchronization.
constexpr size_t max_batch_descriptors = 128 * 1024;
- const size_t bytes = std::min(n_reads_gather, max_batch_descriptors) * sizeof(ggml_cuda_batch_get_descriptor);
- ggml_backend_buffer_ptr descriptors(n_reads_gather ? ggml_backend_buft_alloc_buffer(ggml_backend_cuda_buffer_type(device), bytes) : nullptr);
+ const size_t bytes = std::min(n_reads_kernel, max_batch_descriptors) * sizeof(ggml_cuda_batch_copy_descriptor);
+ ggml_backend_buffer_ptr descriptors(n_reads_kernel ? ggml_backend_buft_alloc_buffer(ggml_backend_cuda_buffer_type(device), bytes) : nullptr);
if (!descriptors) {
- n_reads_gather = 0;
+ n_reads_kernel = 0;
}
const auto host_type = ggml_backend_cuda_host_buffer_type();
- ggml_backend_buffer_ptr host_descriptors(n_reads_gather ? ggml_backend_buft_alloc_buffer(host_type, bytes) : nullptr);
+ ggml_backend_buffer_ptr host_descriptors(n_reads_kernel ? ggml_backend_buft_alloc_buffer(host_type, bytes) : nullptr);
if (!host_descriptors || ggml_backend_buffer_get_type(host_descriptors.get()) != host_type) {
- n_reads_gather = 0;
+ n_reads_kernel = 0;
}
- if (n_reads_gather) {
- auto * copies = static_cast(ggml_backend_buffer_get_base(host_descriptors.get()));
- auto * device_copies = static_cast(ggml_backend_buffer_get_base(descriptors.get()));
+ if (n_reads_kernel) {
+ auto * copies = static_cast(ggml_backend_buffer_get_base(host_descriptors.get()));
+ auto * device_copies = static_cast(ggml_backend_buffer_get_base(descriptors.get()));
const int threads = 256;
const size_t nsm = ggml_cuda_info().devices[device].nsm;
size_t i = 0;
- for (size_t first = 0; first < n_reads_gather; first += max_batch_descriptors) {
- const size_t n_batch = std::min(n_reads_gather - first, max_batch_descriptors);
+ for (size_t first = 0; first < n_reads_kernel; first += max_batch_descriptors) {
+ const size_t n_batch = std::min(n_reads_kernel - first, max_batch_descriptors);
size_t count = 0;
for (; i < n_tensors && count < n_batch; ++i) {
- if (sizes[i] == 0 || sizes[i] > max_gather_read_bytes) {
+ if (sizes[i] == 0 || sizes[i] > max_kernel_read_bytes) {
continue;
}
- copies[count++] = {(const unsigned char *) tensors[i]->data + offsets[i],
- mapping.device + ((uintptr_t) data[i] - begin), sizes[i]};
+ auto * host = mapping.device + ((uintptr_t) data[i] - begin);
+ auto * tensor = (unsigned char *) tensors[i]->data + offsets[i];
+ copies[count++] = {to_host ? tensor : host, to_host ? host : tensor, sizes[i]};
}
const size_t blocks_per_copy = std::min(UINT16_MAX, std::max(1, (nsm + n_batch - 1) / n_batch));
const dim3 blocks((unsigned int) n_batch, (unsigned int) blocks_per_copy);
// Stream ordering keeps each kernel ahead of the next descriptor overwrite.
CUDA_CHECK(cudaMemcpyAsync(device_copies, copies, n_batch * sizeof(copies[0]), cudaMemcpyHostToDevice, cudaStreamPerThread));
- ggml_cuda_batch_get_kernel<<>>(device_copies, n_batch);
+ ggml_cuda_batch_copy_kernel<<>>(device_copies, n_batch);
CUDA_CHECK(cudaGetLastError());
- if (first + n_batch < n_reads_gather) {
+ if (first + n_batch < n_reads_kernel) {
// Wait before refilling the pinned host descriptor buffer.
CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
}
}
}
- // Keep the destination pinned for DMA too; overlapping destinations already declined above.
+ // Keep the CPU span pinned for DMA too; overlapping destinations already declined above.
for (size_t i = 0; i < n_tensors; ++i) {
- if (sizes[i] && (!n_reads_gather || sizes[i] > max_gather_read_bytes)) {
- CUDA_CHECK(cudaMemcpyAsync(data[i], (const char *) tensors[i]->data + offsets[i], sizes[i], cudaMemcpyDeviceToHost, cudaStreamPerThread));
+ if (sizes[i] && (!n_reads_kernel || sizes[i] > max_kernel_read_bytes)) {
+ void * host = const_cast(data[i]);
+ void * tensor = (char *) tensors[i]->data + offsets[i];
+ CUDA_CHECK(cudaMemcpyAsync(to_host ? host : tensor, to_host ? tensor : host, sizes[i], direction, cudaStreamPerThread));
}
}
- // Wait for GPU writes before releasing the CPU mapping and descriptor buffers.
+ // Wait for transfers before releasing the CPU mapping and descriptor buffers.
CUDA_CHECK(cudaStreamSynchronize(cudaStreamPerThread));
return true;
#endif
}
+
+bool ggml_cuda_get_tensor_n(int device,
+ const ggml_tensor * const * tensors, void * const * data, const size_t * offsets,
+ const size_t * sizes, size_t n_tensors) {
+ return ggml_cuda_copy_tensor_n(device, cudaMemcpyDeviceToHost, tensors, data, offsets, sizes, n_tensors);
+}
+
+bool ggml_cuda_set_tensor_n(int device,
+ ggml_tensor * const * tensors, const void * const * data, const size_t * offsets,
+ const size_t * sizes, size_t n_tensors) {
+ return ggml_cuda_copy_tensor_n(device, cudaMemcpyHostToDevice, tensors, data, offsets, sizes, n_tensors);
+}
diff --git a/ggml/src/ggml-cuda/batch-get.cuh b/ggml/src/ggml-cuda/batch-copy.cuh
similarity index 55%
rename from ggml/src/ggml-cuda/batch-get.cuh
rename to ggml/src/ggml-cuda/batch-copy.cuh
index eb9bf0cc81bd..bb7fca4c02dd 100644
--- a/ggml/src/ggml-cuda/batch-get.cuh
+++ b/ggml/src/ggml-cuda/batch-copy.cuh
@@ -5,3 +5,7 @@
bool ggml_cuda_get_tensor_n(int device,
const ggml_tensor * const * tensors, void * const * data, const size_t * offsets,
const size_t * sizes, size_t n_tensors);
+
+bool ggml_cuda_set_tensor_n(int device,
+ ggml_tensor * const * tensors, const void * const * data, const size_t * offsets,
+ const size_t * sizes, size_t n_tensors);
diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index fd047d7c3333..053ed60f798b 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -452,6 +452,16 @@ static __device__ __forceinline__ ggml_cuda_thread_mask_t ggml_cuda_ballot(int p
return __ballot_sync(mask, predicate);
}
+static __device__ __forceinline__ int ggml_cuda_popc(ggml_cuda_thread_mask_t x) {
+ static_assert(sizeof(ggml_cuda_thread_mask_t) == 4 || sizeof(ggml_cuda_thread_mask_t) == 8, "unsupported thread mask type");
+ if constexpr (sizeof(ggml_cuda_thread_mask_t) == 4) {
+ return __popc(x);
+ } else {
+ return __popcll(x);
+ }
+}
+template static __device__ __forceinline__ int ggml_cuda_popc(T x) = delete; // Makes implicit type casts result in an error.
+
static __device__ __forceinline__ int ggml_cuda_all(int predicate, ggml_cuda_thread_mask_t mask = GGML_CUDA_FULL_THREAD_MASK) {
return __all_sync(mask, predicate);
}
diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cu
index 2e32760847bc..10aa5af15863 100644
--- a/ggml/src/ggml-cuda/fattn.cu
+++ b/ggml/src/ggml-cuda/fattn.cu
@@ -17,15 +17,6 @@ static __global__ void flash_attn_mask_to_sparse_indices(
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int values_per_lane = 8;
- using lane_mask_t = std::conditional_t;
- constexpr uint64_t ballot_mask = ~(lane_mask_t)0;
- const auto popc = [](const lane_mask_t mask) {
- if constexpr (warp_size == 64) {
- return __popcll(mask);
- } else {
- return __popc(mask);
- }
- };
const int tid = threadIdx.x;
const int warp = tid / warp_size;
const int lane = tid % warp_size;
@@ -48,7 +39,7 @@ static __global__ void flash_attn_mask_to_sparse_indices(
__syncthreads();
for (int i0 = 0; i0 < ne30; i0 += blockDim.x*values_per_lane) {
- lane_mask_t selected_warp[values_per_lane];
+ ggml_cuda_thread_mask_t selected_warp[values_per_lane];
int warp_count = 0;
#pragma unroll
for (int item = 0; item < values_per_lane; ++item) {
@@ -60,8 +51,8 @@ static __global__ void flash_attn_mask_to_sparse_indices(
selected |= (!oob || q < q1 - q0) && isfinite(__half2float(mask[q*s31 + i]));
}
}
- selected_warp[item] = (lane_mask_t)ggml_cuda_ballot((int) selected, ballot_mask);
- warp_count += popc(selected_warp[item]);
+ selected_warp[item] = ggml_cuda_ballot((int) selected);
+ warp_count += ggml_cuda_popc(selected_warp[item]);
}
if (lane == 0) {
@@ -81,16 +72,16 @@ static __global__ void flash_attn_mask_to_sparse_indices(
}
__syncthreads();
- const lane_mask_t lane_mask = lane == 0 ? 0 : ((lane_mask_t)1 << lane) - 1;
+ const ggml_cuda_thread_mask_t lane_mask = lane == 0 ? 0 : ((ggml_cuda_thread_mask_t)1 << lane) - 1;
int warp_item_offset = 0;
#pragma unroll
for (int item = 0; item < values_per_lane; ++item) {
const int i = i0 + (warp*values_per_lane + item)*warp_size + lane;
- const int dst = row_count + warp_offsets[warp] + warp_item_offset + popc(selected_warp[item] & lane_mask);
- if ((selected_warp[item] & ((lane_mask_t)1 << lane)) && dst < n_kv_max) {
+ const int dst = row_count + warp_offsets[warp] + warp_item_offset + ggml_cuda_popc(selected_warp[item] & lane_mask);
+ if ((selected_warp[item] & ((ggml_cuda_thread_mask_t)1 << lane)) && dst < n_kv_max) {
indices[dst] = i;
}
- warp_item_offset += popc(selected_warp[item]);
+ warp_item_offset += ggml_cuda_popc(selected_warp[item]);
}
__syncthreads();
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index 40e83750efba..8702dd32d366 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -10,7 +10,7 @@
#include "ggml-cuda/arange.cuh"
#include "ggml-cuda/argmax.cuh"
#include "ggml-cuda/argsort.cuh"
-#include "ggml-cuda/batch-get.cuh"
+#include "ggml-cuda/batch-copy.cuh"
#include "ggml-cuda/binbcast.cuh"
#include "ggml-cuda/clamp.cuh"
#include "ggml-cuda/col2im-1d.cuh"
@@ -48,6 +48,7 @@
#include "ggml-cuda/rope.cuh"
#include "ggml-cuda/roll.cuh"
#include "ggml-cuda/scale.cuh"
+#include "ggml-cuda/sleep.cuh"
#include "ggml-cuda/snake.cuh"
#include "ggml-cuda/softcap.cuh"
#include "ggml-cuda/softmax.cuh"
@@ -1116,6 +1117,11 @@ static void ggml_backend_cuda_buffer_set_tensor_n(ggml_backend_buffer_t buffer,
ggml_cuda_set_device(ctx->device);
+ if (!ggml_cuda_pinned_memory_disabled() &&
+ ggml_cuda_set_tensor_n(ctx->device, tensors, data, offsets, sizes, n_tensors)) {
+ return;
+ }
+
for (size_t i = 0; i < n_tensors; i++) {
if (sizes[i] == 0) {
continue;
@@ -2724,7 +2730,8 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
ids_from_sorted_host[i12*n_expert_used + iex] = ids_to_sorted_host.size();
ids_to_sorted_host.push_back(i12*ne11 + iex % ne11);
tokens_per_expert[i02]++;
- break;
+ // TODO: removing the break here was done to make duplicate IDs work but this caused a ~10% performance regression, better solution needed.
+ // break;
}
}
}
@@ -2776,6 +2783,7 @@ static void ggml_cuda_mul_mat_id(ggml_backend_cuda_context & ctx, ggml_tensor *
ggml_tensor dst_slice;
memset(&dst_slice, 0, sizeof(dst_slice));
memcpy(dst_slice.op_params, dst->op_params, sizeof(dst_slice.op_params));
+ dst_slice.op = GGML_OP_MUL_MAT; // ggml_prec_get_acc/ggml_prec_get_src check the op
dst_slice.buffer = dst->buffer;
dst_slice.type = type_dst_sorted;
dst_slice.ne[0] = ne0;
@@ -3145,6 +3153,9 @@ static bool ggml_cuda_compute_forward(ggml_backend_cuda_context & ctx, struct gg
case GGML_OP_FILL:
ggml_cuda_op_fill(ctx, dst);
break;
+ case GGML_OP_SLEEP:
+ ggml_cuda_op_sleep(ctx, dst);
+ break;
case GGML_OP_LIGHTNING_INDEXER:
ggml_cuda_lightning_indexer(ctx, dst);
break;
@@ -3495,7 +3506,12 @@ static bool ggml_cuda_should_fuse_rope_set_rows(const ggml_tensor * rope,
return false;
}
- if (set_rows->type != GGML_TYPE_F32 && set_rows->type != GGML_TYPE_F16) {
+ if (set_rows->type != GGML_TYPE_F32 && set_rows->type != GGML_TYPE_F16 && set_rows->type != GGML_TYPE_BF16) {
+ return false;
+ }
+
+ // BF16 rope fuses into an F16 or BF16 cache, other ropes not into a BF16 cache
+ if (rope->type == GGML_TYPE_BF16 ? set_rows->type == GGML_TYPE_F32 : set_rows->type == GGML_TYPE_BF16) {
return false;
}
@@ -6501,7 +6517,7 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
(op->type == GGML_TYPE_F32 || op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_BF16 || op->type == GGML_TYPE_F8_E4M3 ||
op->type == GGML_TYPE_Q4_0 || op->type == GGML_TYPE_Q4_1 || op->type == GGML_TYPE_Q5_0 ||
op->type == GGML_TYPE_Q5_1 || op->type == GGML_TYPE_Q8_0 || op->type == GGML_TYPE_IQ4_NL) &&
- op->src[0]->type == GGML_TYPE_F32
+ (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_BF16)
) || (
op->type == GGML_TYPE_F16 && op->src[0]->type == GGML_TYPE_F16
)
@@ -6722,7 +6738,9 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
return false;
case GGML_OP_ROPE:
case GGML_OP_ROPE_BACK: {
- return op->src[0]->nb[0] == ggml_type_size(op->src[0]->type) && ggml_is_contiguous_2(op->src[0]);
+ return (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16 || op->src[0]->type == GGML_TYPE_BF16) &&
+ op->type == op->src[0]->type &&
+ op->src[0]->nb[0] == ggml_type_size(op->src[0]->type) && ggml_is_contiguous_2(op->src[0]);
}
case GGML_OP_IM2COL:
case GGML_OP_IM2COL_3D:
@@ -6809,6 +6827,12 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
return ggml_cuda_lightning_indexer_supported(dev_ctx->device, op);
case GGML_OP_TOP_K_MASK:
return op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32;
+ case GGML_OP_SLEEP:
+#if defined(GGML_USE_HIP) || defined(GGML_USE_MUSA)
+ return false;
+#else
+ return true;
+#endif // defined(GGML_USE_HIP) || defined(GGML_USE_MUSA)
default:
return false;
diff --git a/ggml/src/ggml-cuda/mmf.cu b/ggml/src/ggml-cuda/mmf.cu
index 040d390e0cc5..396ad1c1184d 100644
--- a/ggml/src/ggml-cuda/mmf.cu
+++ b/ggml/src/ggml-cuda/mmf.cu
@@ -43,7 +43,6 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
const int64_t s13 = src1->nb[3] / ts_src1;
const int64_t s3 = dst->nb[3] / ts_dst;
- const int64_t ids_s0 = ids ? ids->nb[0] / ggml_type_size(ids->type) : 0;
const int64_t ids_s1 = ids ? ids->nb[1] / ggml_type_size(ids->type) : 0;
mmf_ids_data ids_info{};
@@ -70,7 +69,7 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
nchannels_y = ids->ne[0];
}
- if (ids && ncols_dst > 16) {
+ if (ids) {
const int64_t n_expert_used = ids->ne[0];
const int64_t n_experts = ne02;
const int64_t n_tokens = ne12;
@@ -110,7 +109,7 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
constexpr int vals_per_T = 1;
mul_mat_f_switch_rows_per_block(
rows_per_block, src0_d, src1_d, ids_d, dst_d, ne00/vals_per_T, ne01, ncols_dst, s01/vals_per_T, stride_col_y/vals_per_T, stride_col_dst,
- ids_s0, ids_s1, ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
+ ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
ne03, ne3, s03/vals_per_T, s13, s3, ctx.stream(), ids_info_ptr);
} break;
case GGML_TYPE_F16: {
@@ -118,7 +117,7 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
constexpr int vals_per_T = 2;
mul_mat_f_switch_rows_per_block(
rows_per_block, src0_d, src1_d, ids_d, dst_d, ne00/vals_per_T, ne01, ncols_dst, s01/vals_per_T, stride_col_y/vals_per_T, stride_col_dst,
- ids_s0, ids_s1, ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
+ ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
ne03, ne3, s03/vals_per_T, s13, s3, ctx.stream(), ids_info_ptr);
} break;
case GGML_TYPE_BF16: {
@@ -126,7 +125,7 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
constexpr int vals_per_T = 2;
mul_mat_f_switch_rows_per_block(
rows_per_block, src0_d, src1_d, ids_d, dst_d, ne00/vals_per_T, ne01, ncols_dst, s01/vals_per_T, stride_col_y/vals_per_T, stride_col_dst,
- ids_s0, ids_s1, ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
+ ne02, nchannels_y, nchannels_dst, s02/vals_per_T, stride_channel_y, stride_channel_dst,
ne03, ne3, s03/vals_per_T, s13, s3, ctx.stream(), ids_info_ptr);
} break;
default:
diff --git a/ggml/src/ggml-cuda/mmf.cuh b/ggml/src/ggml-cuda/mmf.cuh
index c49f6c47ad1d..46a123a5c3c1 100644
--- a/ggml/src/ggml-cuda/mmf.cuh
+++ b/ggml/src/ggml-cuda/mmf.cuh
@@ -47,12 +47,11 @@ void ggml_cuda_mul_mat_f(ggml_backend_cuda_context & ctx, const ggml_tensor * sr
bool ggml_cuda_should_use_mmf(enum ggml_type type, int cc, int warp_size, const int64_t * scr0_ne, const size_t * src0_nb, const int src1_ncols, bool mul_mat_id);
-template
+template
__launch_bounds__(ggml_cuda_get_physical_warp_size()*nwarps, 1)
static __global__ void mul_mat_f(
- const T * __restrict__ x, const float * __restrict__ y, const int32_t * __restrict__ ids, float * __restrict__ dst,
- const int ncols, const int ncols_dst_total, const int nchannels_dst, const int stride_row, const int stride_col_y, const int stride_col_dst,
- const int stride_col_id, const int stride_row_id,
+ const T * __restrict__ x, const float * __restrict__ y, float * __restrict__ dst,
+ const int ncols, const int stride_row, const int stride_col_y, const int stride_col_dst,
const int channel_ratio, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,
const int sample_ratio, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst) {
// TODO: handle this in a consistent and simpler way after AMD MFMA support has been added
@@ -90,85 +89,26 @@ static __global__ void mul_mat_f(
constexpr int ntA = rows_per_block / tile_A::I;
constexpr int ntB = (cols_per_block + tile_B::I - 1) / tile_B::I;
- const int row0 = blockIdx.x * rows_per_block;
-
- int expert_idx = 0;
- [[maybe_unused]] int col_base = 0;
-
- const int channel_dst = has_ids ? 0 : blockIdx.y;
-
- if constexpr (has_ids) {
- // experts + tiles of ncols_dst are packed in the y dimension
- int col_tiles = (ncols_dst_total + cols_per_block - 1) / cols_per_block;
- const int nchannels_x = gridDim.y / col_tiles;
- const int tile_idx = blockIdx.y / nchannels_x;
- expert_idx = blockIdx.y - tile_idx * nchannels_x;
- col_base = tile_idx * cols_per_block;
- }
+ const int row0 = blockIdx.x * rows_per_block;
- const int channel_x = has_ids ? expert_idx : (channel_dst / channel_ratio);
+ const int channel_dst = blockIdx.y;
+ const int channel_x = channel_dst / channel_ratio;
const int channel_y = channel_dst;
const int sample_dst = blockIdx.z;
const int sample_x = sample_dst / sample_ratio;
const int sample_y = sample_dst;
x += int64_t(sample_x) *stride_sample_x + channel_x *stride_channel_x + row0*stride_row ;
- y += int64_t(sample_y) *stride_sample_y + (has_ids ? 0 : channel_y *stride_channel_y);
- dst += int64_t(sample_dst)*stride_sample_dst + (has_ids ? 0 : channel_dst*stride_channel_dst);
-
- if constexpr (has_ids) {
- constexpr int y_stride_scale = std::is_same_v ? 1 : 2;
- const int64_t col_offset = col_base;
- y += col_offset * stride_col_y * y_stride_scale;
- dst += col_offset * stride_col_dst;
- ids += col_offset * stride_row_id;
- }
+ y += int64_t(sample_y) *stride_sample_y + channel_y *stride_channel_y;
+ dst += int64_t(sample_dst)*stride_sample_dst + channel_dst*stride_channel_dst;
[[maybe_unused]] const float2 * y2 = (const float2 *) y;
extern __shared__ char data_mmv[];
- char * shmem_base = data_mmv;
- [[maybe_unused]] int * slot_map = (int *) shmem_base;
- char * compute_base = has_ids ? (shmem_base + GGML_PAD(cols_per_block, 16) * sizeof(int)) : shmem_base;
-
tile_C C[ntA][ntB];
- T * tile_xy = (T *) compute_base + threadIdx.y*(tile_A::I * tile_k_padded);
-
- if constexpr (has_ids) {
- int found = 0;
-
- for (int j0 = 0; j0 < cols_per_block; j0 += nwarps) {
- const int j = j0 + threadIdx.y;
-
- if (threadIdx.x == 0) {
- slot_map[j] = -1;
- }
- ggml_cuda_syncwarp();
-
- if (col_base + j >= ncols_dst_total) {
- continue;
- }
-
- const int32_t * __restrict__ id_row = ids + j*stride_row_id;
-
- for (int k = threadIdx.x; k < nchannels_dst; k += warp_size) {
- int match = id_row[k*stride_col_id] == expert_idx;
-
- if (match) {
- slot_map[j] = k;
- found = 1;
- break;
- }
- }
- }
-
- if (!__syncthreads_or(found)) {
- return;
- }
- }
-
+ T * tile_xy = (T *) data_mmv + threadIdx.y*(tile_A::I * tile_k_padded);
for (int col = threadIdx.y*warp_size + threadIdx.x; col < ncols; col += nwarps*warp_size) {
tile_A A[ntA][warp_size / tile_A::J];
@@ -194,26 +134,15 @@ static __global__ void mul_mat_f(
for (int j0 = 0; j0 < tile_B::I; ++j0) {
const int j = j0 + itB*tile_B::I;
- if constexpr (!has_ids) {
- tile_xy[j0*tile_k_padded + threadIdx.x] = j < cols_per_block ? y[j*stride_col_y + col] : 0.0f;
- } else {
- const bool valid = j < cols_per_block && (col_base + j) < ncols_dst_total && slot_map[j] >= 0;
- tile_xy[j0*tile_k_padded + threadIdx.x] = valid ? y[slot_map[j]*stride_channel_y + j*stride_col_y + col] : 0.0f;
- }
+ tile_xy[j0*tile_k_padded + threadIdx.x] = j < cols_per_block ? y[j*stride_col_y + col] : 0.0f;
}
} else if constexpr (std::is_same_v || std::is_same_v) {
#pragma unroll
for (int j0 = 0; j0 < tile_B::I; ++j0) {
const int j = j0 + itB*tile_B::I;
- if constexpr (!has_ids) {
- const float2 tmp = j < cols_per_block ? y2[j*stride_col_y + col] : make_float2(0.0f, 0.0f);
- tile_xy[j0*tile_k_padded + threadIdx.x] = ggml_cuda_cast(tmp);
- } else {
- const bool valid = j < cols_per_block && (col_base + j) < ncols_dst_total && slot_map[j] >= 0;
- float2 tmp = valid ? *(const float2*) &y[slot_map[j]*stride_channel_y + 2*(j*stride_col_y + col)] : make_float2(0.0f, 0.0f);
- tile_xy[j0*tile_k_padded + threadIdx.x] = ggml_cuda_cast(tmp);
- }
+ const float2 tmp = j < cols_per_block ? y2[j*stride_col_y + col] : make_float2(0.0f, 0.0f);
+ tile_xy[j0*tile_k_padded + threadIdx.x] = ggml_cuda_cast(tmp);
}
} else {
static_assert(std::is_same_v, "unsupported type");
@@ -231,7 +160,7 @@ static __global__ void mul_mat_f(
}
}
- float * buf_iw = (float *) compute_base;
+ float * buf_iw = (float *) data_mmv;
constexpr int kiw = nwarps*rows_per_block + mmf_get_padding();
if (nwarps > 1) {
@@ -278,26 +207,15 @@ static __global__ void mul_mat_f(
}
}
- if constexpr (!has_ids) {
#pragma unroll
- for (int i0 = 0; i0 < sizeof(sum)/sizeof(sum[0]); ++i0) {
- dst[j*stride_col_dst + row0 + i0*warp_size + threadIdx.x] = sum[i0];
- }
- } else {
- const int slot = (j < cols_per_block) ? slot_map[j] : -1;
- if (slot >= 0 && (col_base + j) < ncols_dst_total) {
-#pragma unroll
- for (int i0 = 0; i0 < sizeof(sum)/sizeof(sum[0]); ++i0) {
- dst[slot*stride_channel_dst + j*stride_col_dst + row0 + i0*warp_size + threadIdx.x] = sum[i0];
- }
- }
+ for (int i0 = 0; i0 < sizeof(sum)/sizeof(sum[0]); ++i0) {
+ dst[j*stride_col_dst + row0 + i0*warp_size + threadIdx.x] = sum[i0];
}
}
}
#else
- GGML_UNUSED_VARS(x, y, ids, dst,
- ncols, ncols_dst_total, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id,
+ GGML_UNUSED_VARS(x, y, dst,
+ ncols, stride_row, stride_col_y, stride_col_dst,
channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst);
NO_DEVICE_CODE;
@@ -588,22 +506,21 @@ static __global__ void mul_mat_f_ids(
template
static inline void mul_mat_f_switch_ids(
- const T * x, const float * y, const int32_t * ids, float * dst,
+ const T * x, const float * y, float * dst,
const int64_t ncols_x, const int64_t ncols_dst, const int64_t nchannels_dst,
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
- const int64_t stride_col_id, const int64_t stride_row_id,
const int64_t channel_ratio, const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst,
const int64_t sample_ratio, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
const dim3 & block_nums, const dim3 & block_dims, const int nbytes_shared_total, cudaStream_t stream,
const mmf_ids_data * ids_data) {
+
const bool has_ids_data = ids_data && ids_data->ids_src_compact;
- // Use the compact-ids kernel only for larger tiles; for small ncols_dst (< 16)
- // we prefer the normal mul_mat_f path with has_ids=true.
- if (has_ids_data && ncols_dst > 16) {
+ if (has_ids_data) {
if (ids_data->max_tiles == 0) {
return;
}
+ GGML_ASSERT(block_nums.z == 1);
dim3 block_nums_ids(block_nums.x, ids_data->max_tiles, 1);
const uint3 sis1_fd = ids_data->sis1 > 0 ? init_fastdiv_values((uint32_t) ids_data->sis1) : make_uint3(0, 0, 1);
@@ -615,19 +532,10 @@ static inline void mul_mat_f_switch_ids(
channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
sis1_fd, nch_fd);
- } else if (ids) {
- const int64_t col_tiles = (ncols_dst + cols_per_block - 1) / cols_per_block;
- dim3 block_nums_ids = block_nums;
- block_nums_ids.y *= col_tiles;
-
- mul_mat_f<<>>
- (x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst);
} else {
- mul_mat_f<<>>
- (x, y, ids, dst, ncols_x, cols_per_block, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
+ mul_mat_f<<>>
+ (x, y, dst, ncols_x, stride_row, stride_col_y, stride_col_dst,
+ channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst);
}
}
@@ -637,7 +545,6 @@ void mul_mat_f_cuda(
const T * x, const float * y, const int32_t * ids, float * dst,
const int64_t ncols_x, const int64_t nrows_x, const int64_t ncols_dst,
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
- const int64_t stride_col_id, const int64_t stride_row_id,
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
@@ -673,70 +580,59 @@ void mul_mat_f_cuda(
const int nbytes_shared_iter = nwarps_best * (volta_mma_available(cc) ? tile_A_32::I : tile_A_16::I) * (warp_size + mmf_get_padding(cc)) * 4;
const int nbytes_cols_per_block_pad = (amd_wmma_available(cc) || amd_mfma_available(cc)) ? tile_B_16::I : tile_B_8::I;
const int nbytes_shared_combine = GGML_PAD(cols_per_block, nbytes_cols_per_block_pad) * (nwarps_best*rows_per_block + mmf_get_padding(cc)) * 4;
- const int nbytes_shared = std::max(nbytes_shared_iter, nbytes_shared_combine);
- const int nbytes_slotmap = ids ? GGML_PAD(cols_per_block, 16) * sizeof(int) : 0;
- const int nbytes_shared_total = nbytes_shared + nbytes_slotmap;
- const int64_t grid_y = ids ? nchannels_x : nchannels_dst;
+ const int nbytes_shared_total = std::max(nbytes_shared_iter, nbytes_shared_combine);
- const dim3 block_nums(nrows_x/rows_per_block, grid_y, nsamples_dst);
+ const dim3 block_nums(nrows_x/rows_per_block, nchannels_dst, nsamples_dst);
const dim3 block_dims(warp_size, nwarps_best, 1);
switch (nwarps_best) {
case 1: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 2: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 3: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 4: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 5: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 6: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 7: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
case 8: {
mul_mat_f_switch_ids(
- x, y, ids, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, channel_ratio, stride_channel_x, stride_channel_y, stride_channel_dst,
- sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst, block_nums, block_dims, nbytes_shared_total, stream,
- ids_data);
+ x, y, dst, ncols_x, ncols_dst, nchannels_dst, stride_row, stride_col_y, stride_col_dst, channel_ratio,
+ stride_channel_x, stride_channel_y, stride_channel_dst, sample_ratio, stride_sample_x, stride_sample_y, stride_sample_dst,
+ block_nums, block_dims, nbytes_shared_total, stream, ids_data);
} break;
default:
GGML_ABORT("fatal error");
@@ -750,95 +646,94 @@ static void mul_mat_f_switch_cols_per_block(
const T * x, const float * y, const int32_t * ids, float * dst,
const int64_t ncols_x, const int64_t nrows_x, const int64_t ncols_dst,
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
- const int64_t stride_col_id, const int stride_row_id,
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
cudaStream_t stream, const mmf_ids_data * ids_data) {
- const int ncols_case = (ids && ncols_dst > 16) ? 16 : ncols_dst;
+ const int ncols_case = ids ? 16 : ncols_dst;
GGML_ASSERT(ids || ncols_dst <= 16);
switch (ncols_case) {
case 1: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 2: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 3: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 4: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 5: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 6: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 7: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 8: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 9: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 10: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 11: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 12: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 13: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 14: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 15: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case 16: {
mul_mat_f_cuda(x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
default:
@@ -851,7 +746,6 @@ static void mul_mat_f_switch_rows_per_block(
const int rows_per_block, const T * x, const float * y, const int32_t * ids, float * dst,
const int64_t ncols_x, const int64_t nrows_x, const int64_t ncols_dst,
const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst,
- const int64_t stride_col_id, const int stride_row_id,
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst,
const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,
const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst,
@@ -860,13 +754,13 @@ static void mul_mat_f_switch_rows_per_block(
case MMF_ROWS_PER_BLOCK: {
mul_mat_f_switch_cols_per_block(
x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
case MMF_ROWS_PER_BLOCK_CDNA: {
mul_mat_f_switch_cols_per_block(
x, y, ids, dst, ncols_x, nrows_x, ncols_dst, stride_row, stride_col_y, stride_col_dst,
- stride_col_id, stride_row_id, nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
+ nchannels_x, nchannels_y, nchannels_dst, stride_channel_x, stride_channel_y, stride_channel_dst,
nsamples_x, nsamples_dst, stride_sample_x, stride_sample_y, stride_sample_dst, stream, ids_data);
} break;
default:
@@ -878,7 +772,6 @@ static void mul_mat_f_switch_rows_per_block(
template void mul_mat_f_cuda( \
const T * x, const float * y, const int32_t * ids, float * dst, \
const int64_t ncols_x, const int64_t nrows_x, int64_t ncols_dst_total, const int64_t stride_row, const int64_t stride_col_y, const int64_t stride_col_dst, \
- const int64_t stride_col_id, const int64_t stride_row_id, \
const int64_t nchannels_x, const int64_t nchannels_y, const int64_t nchannels_dst, \
const int64_t stride_channel_x, const int64_t stride_channel_y, const int64_t stride_channel_dst, const int64_t nsamples_x,\
const int64_t nsamples_dst, const int64_t stride_sample_x, const int64_t stride_sample_y, const int64_t stride_sample_dst, \
diff --git a/ggml/src/ggml-cuda/mmid.cu b/ggml/src/ggml-cuda/mmid.cu
index 6b425a377244..fe9458763e13 100644
--- a/ggml/src/ggml-cuda/mmid.cu
+++ b/ggml/src/ggml-cuda/mmid.cu
@@ -24,6 +24,50 @@ template struct mm_ids_pow2 { static constexpr int value = 2*mm_ids_pow2
template <> struct mm_ids_pow2<1> { static constexpr int value = 1; };
template <> struct mm_ids_pow2<0> { static constexpr int value = 1; };
+template
+static __device__ __forceinline__ void check_experts(
+ mm_ids_helper_store * store, int & nex_prev, int & it_compact,
+ const int expert, const int it, const int iex, const int expert_found) {
+ if (increment_nex_prev) {
+ nex_prev += expert_found < expert;
+ }
+
+ if (write_to_store) {
+ const ggml_cuda_thread_mask_t iex_used = ggml_cuda_ballot(expert_found == expert);
+ const ggml_cuda_thread_mask_t mask = (ggml_cuda_thread_mask_t(1) << threadIdx.x) - 1;
+ const int it_compact_offset = ggml_cuda_popc(iex_used & mask);
+
+ if (expert_found == expert) {
+ store[it_compact + it_compact_offset] = mm_ids_helper_store(it, iex);
+ }
+
+ it_compact += ggml_cuda_popc(iex_used);
+ }
+}
+
+static __device__ __forceinline__ void dump_store(
+ int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst,
+ const int n_expert_used, const int nchannels_y, const int sis1, const bool write_inverse,
+ const mm_ids_helper_store * __restrict__ store, const int nex_prev, int & n_dumped, const int it_compact) {
+ constexpr int warp_size = ggml_cuda_get_physical_warp_size();
+
+ for (int itc = threadIdx.x; itc < it_compact; itc += warp_size) {
+ const mm_ids_helper_store store_it = store[itc];
+ const int it = store_it.it();
+ const int iex_used = store_it.iex_used();
+ ids_dst[nex_prev + n_dumped + itc] = it*n_expert_used + iex_used;
+ // ids_src1 holds the forward map, or the inverse map (token slot -> compact row) for quant dedup
+ if (write_inverse) {
+ ids_src1[it*n_expert_used + iex_used] = nex_prev + n_dumped + itc;
+ } else {
+ ids_src1[nex_prev + n_dumped + itc] = it*sis1 + iex_used % nchannels_y;
+ }
+ }
+ n_dumped += it_compact;
+}
+
+constexpr int n_unroll = 4; // Manually unroll this many loop iterations for better performance.
+
// Helper function for mul_mat_id, converts ids to a more convenient format.
// ids_src1 describes how to permute the flattened column indices of src1 in order to get a compact src1 tensor sorted by expert.
// ids_dst describes the same mapping but for the dst tensor.
@@ -36,85 +80,138 @@ static __global__ void mm_ids_helper(
const int32_t * __restrict__ ids,
int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst, ggml_cuda_mm_id_tile_spec * __restrict__ tile_spec,
const int n_tokens, const int n_expert_used_var, const int nchannels_y, const uint3 tile_width,
- const int si1, const int sis1, const bool write_inverse) {
+ const int si1, const int sis1, const bool write_inverse, const int ne_store) {
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
const int n_expert_used = n_expert_used_template == 0 ? n_expert_used_var : n_expert_used_template;
- const int expert = blockIdx.x;
+
+ const int expert = blockIdx.x; // The expert this CUDA block handles.
extern __shared__ char data_mm_ids_helper[];
mm_ids_helper_store * store = (mm_ids_helper_store *) data_mm_ids_helper;
int nex_prev = 0; // Number of columns for experts with a lower index.
+ int n_dumped = 0; // Number of data values that have been dumped to output buffers.
int it_compact = 0; // Running index for the compact slice of this expert.
+ // Step 1: Iterate over n_tokens, increment nex_prev to determine the output indices, fill up store.
+ // Step 1.5: If the store does not have enough space to hold all values, iterate over the rest of the tokens just to increment nex_prev.
+ // Step 2: Dump the stored values using nex_prev to get the correct output indices.
+ // Step 3: If we were not able to process all values, resume at the index up to which we were able to dump the output and re-do.
+
if constexpr (n_expert_used_template == 0) {
// Generic implementation:
- for (int it = 0; it < n_tokens; ++it) {
- int iex_used = -1; // The index at which the expert is used, if any.
- for (int iex = threadIdx.x; iex < n_expert_used; iex += warp_size) {
- const int expert_used = ids[it*si1 + iex];
- nex_prev += expert_used < expert;
- if (expert_used == expert) {
- iex_used = iex;
- }
+ int it = 0;
+ for (; it < n_tokens && it_compact + max(warp_size, n_expert_used) <= ne_store; ++it) {
+ for (int iex0 = 0; iex0 < n_expert_used; iex0 += warp_size) {
+ const int iex = iex0 + threadIdx.x;
+ const int expert_found = iex < n_expert_used ? ids[it*si1 + iex] : INT_MAX;
+
+ constexpr bool increment_nex_prev = true;
+ constexpr bool write_to_store = true;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
}
+ }
+ int it_dumped = it;
+ for (; it < n_tokens; ++it) {
+ for (int iex0 = 0; iex0 < n_expert_used; iex0 += warp_size) {
+ const int iex = iex0 + threadIdx.x;
+ const int expert_found = iex < n_expert_used ? ids[it*si1 + iex] : INT_MAX;
- if (iex_used != -1) {
- store[it_compact] = mm_ids_helper_store(it, iex_used);
+ constexpr bool increment_nex_prev = true;
+ constexpr bool write_to_store = false;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
}
+ }
+
+ nex_prev = warp_reduce_sum(nex_prev);
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
+ it_compact = 0;
- if (warp_reduce_any(iex_used != -1)) {
- it_compact++;
+ for (int it = it_dumped; it < n_tokens; ++it) {
+ for (int iex0 = 0; iex0 < n_expert_used; iex0 += warp_size) {
+ const int iex = iex0 + threadIdx.x;
+ const int expert_found = iex < n_expert_used ? ids[it*si1 + iex] : INT_MAX;
+
+ constexpr bool increment_nex_prev = false;
+ constexpr bool write_to_store = true;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
+ if (it_compact + max(warp_size, n_expert_used) >= ne_store) {
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
+ it_compact = 0;
+ }
}
}
+ if (it_compact > 0) {
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
+ }
} else {
// Implementation optimized for specific numbers of experts used:
// a warp holds a whole number of token slots, so the slot count is padded to a power of 2
constexpr int neu_padded = mm_ids_pow2::value;
static_assert(neu_padded <= warp_size && warp_size % neu_padded == 0, "bad n_expert_used");
- for (int it0 = 0; it0 < n_tokens; it0 += warp_size/neu_padded) {
- const int it = it0 + threadIdx.x / neu_padded;
-
- const int iex = threadIdx.x % neu_padded; // The index at which the expert is used, if any.
- const int expert_used = (neu_padded == n_expert_used || iex < n_expert_used) && it < n_tokens ?
- ids[it*si1 + iex] : INT_MAX;
- const int iex_used = expert_used == expert ? iex : -1;
- nex_prev += expert_used < expert;
-
- // Whether the threads at this token position have used the expert:
- const int it_compact_add_self = warp_reduce_any(iex_used != -1);
+ const int iex = threadIdx.x % neu_padded; // The index at which the expert is used, if any.
- // Do a scan over threads at lower token positions in warp to get the correct index for writing data:
- int it_compact_add_lower = 0;
+ int it0 = 0;
+ for (; it0 < n_tokens && it_compact + n_unroll*warp_size <= ne_store; it0 += n_unroll*warp_size/neu_padded) {
#pragma unroll
- for (int offset = neu_padded; offset < warp_size; offset += neu_padded) {
- const int tmp = ggml_cuda_shfl_up(it_compact_add_self, offset, warp_size);
- if (threadIdx.x >= static_cast(offset)) {
- it_compact_add_lower += tmp;
- }
- }
+ for (int it1 = 0; it1 < n_unroll*warp_size/neu_padded; it1 += warp_size/neu_padded) {
+ const int it = it0 + it1 + threadIdx.x / neu_padded;
+ const int expert_found = (neu_padded == n_expert_used || iex < n_expert_used) && it < n_tokens ?
+ ids[it*si1 + iex] : INT_MAX;
- if (iex_used != -1) {
- store[it_compact + it_compact_add_lower] = mm_ids_helper_store(it, iex_used);
+ constexpr bool increment_nex_prev = true;
+ constexpr bool write_to_store = true;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
}
+ }
+ int it0_dumped = it0;
+ for (; it0 < n_tokens; it0 += n_unroll*warp_size/neu_padded) {
+#pragma unroll
+ for (int it1 = 0; it1 < n_unroll*warp_size/neu_padded; it1 += warp_size/neu_padded) {
+ const int it = it0 + it1 + threadIdx.x / neu_padded;
+ const int expert_found = (neu_padded == n_expert_used || iex < n_expert_used) && it < n_tokens ?
+ ids[it*si1 + iex] : INT_MAX;
- // The thread with the highest index in the warp always has the sum over the whole warp, use it to increment all threads:
- it_compact += ggml_cuda_shfl(it_compact_add_lower + it_compact_add_self, warp_size - 1, warp_size);
+ constexpr bool increment_nex_prev = true;
+ constexpr bool write_to_store = false;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
+ }
}
- }
- nex_prev = warp_reduce_sum(nex_prev);
- ggml_cuda_syncwarp();
- for (int itc = threadIdx.x; itc < it_compact; itc += warp_size) {
- const mm_ids_helper_store store_it = store[itc];
- const int it = store_it.it();
- const int iex_used = store_it.iex_used();
- ids_dst[nex_prev + itc] = it*n_expert_used + iex_used;
- // ids_src1 holds the forward map, or the inverse map (token slot -> compact row) for quant dedup
- if (write_inverse) {
- ids_src1[it*n_expert_used + iex_used] = nex_prev + itc;
- } else {
- ids_src1[nex_prev + itc] = it*sis1 + iex_used % nchannels_y;
+ nex_prev = warp_reduce_sum(nex_prev);
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
+ it_compact = 0;
+
+ for (it0 = it0_dumped; it0 < n_tokens; it0 += n_unroll*warp_size/neu_padded) {
+#pragma unroll
+ for (int it1 = 0; it1 < n_unroll*warp_size/neu_padded; it1 += warp_size/neu_padded) {
+ const int it = it0 + it1 + threadIdx.x / neu_padded;
+ const int expert_found = (neu_padded == n_expert_used || iex < n_expert_used) && it < n_tokens ?
+ ids[it*si1 + iex] : INT_MAX;
+
+ constexpr bool increment_nex_prev = false;
+ constexpr bool write_to_store = true;
+ check_experts
+ (store, nex_prev, it_compact, expert, it, iex, expert_found);
+ if (it_compact + warp_size >= ne_store) {
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
+ it_compact = 0;
+ }
+ }
+ }
+ if (it_compact > 0) {
+ ggml_cuda_syncwarp();
+ dump_store(ids_src1, ids_dst, n_expert_used, nchannels_y, sis1, write_inverse, store, nex_prev, n_dumped, it_compact);
}
}
@@ -123,7 +220,7 @@ static __global__ void mm_ids_helper(
}
int col_low = nex_prev;
- int col_high = nex_prev + min(int(tile_width.z), it_compact);
+ int col_high = nex_prev + min(int(tile_width.z), n_dumped);
// The while loop covers all tiles at the seams containing the column with % tile_width == 0.
while (col_high > fastdiv(col_low + tile_width.z - 1, tile_width) * tile_width.z) {
@@ -131,7 +228,7 @@ static __global__ void mm_ids_helper(
= {expert, col_low, col_high, /*padding =*/ 0};
col_low = col_high;
- col_high = min(col_high + int(tile_width.z), nex_prev + it_compact);
+ col_high = min(col_high + int(tile_width.z), nex_prev + n_dumped);
}
// Finally, at most one extra tile_spec between the seams per expert.
@@ -157,10 +254,12 @@ static void launch_mm_ids_helper(
const dim3 num_blocks(n_experts, 1, 1);
const dim3 block_size(warp_size, 1, 1);
- const size_t nbytes_shared = n_tokens*sizeof(mm_ids_helper_store);
- GGML_ASSERT(nbytes_shared <= smpbo);
+ const size_t ne_shared_pad = n_expert_used_template == 0 ? std::max(n_expert_used_var, warp_size) : n_unroll*warp_size;
+ const size_t nbytes_store_core = std::min(smpbo - ne_shared_pad * sizeof(mm_ids_helper_store), n_tokens * sizeof(mm_ids_helper_store));
+ const size_t nbytes_shared = nbytes_store_core + ne_shared_pad * sizeof(mm_ids_helper_store);
+ const size_t ne_store = nbytes_shared / sizeof(mm_ids_helper_store);
mm_ids_helper<<>>
- (ids, ids_src1, ids_dst, tile_spec, n_tokens, n_expert_used_var, nchannels_y, tile_width_fd, si1, sis1, write_inverse);
+ (ids, ids_src1, ids_dst, tile_spec, n_tokens, n_expert_used_var, nchannels_y, tile_width_fd, si1, sis1, write_inverse, ne_store);
}
void ggml_cuda_launch_mm_ids_helper(
diff --git a/ggml/src/ggml-cuda/mmq.cuh b/ggml/src/ggml-cuda/mmq.cuh
index 7fb09a353786..be2f11430079 100644
--- a/ggml/src/ggml-cuda/mmq.cuh
+++ b/ggml/src/ggml-cuda/mmq.cuh
@@ -1094,7 +1094,7 @@ static __global__ void mul_mat_q(
int col_high = min(col_low + J, ncols_dst);
int offset_y = wt*stride_sample_y + zt*stride_channel_y;
int offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y;
- int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst;
+ int64_t offset_dst = (int64_t) wt*stride_sample_dst + (int64_t) zt*stride_channel_dst + (int64_t) jt*J*stride_col_dst;
if (ids_dst) {
const ggml_cuda_mm_id_tile_spec tsj = tile_spec[jt];
@@ -1176,7 +1176,7 @@ static __global__ void mul_mat_q(
int col_high = min(col_low + J, ncols_dst);
int offset_y = wt*stride_sample_y + zt*stride_channel_y;
int offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y;
- int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst;
+ int64_t offset_dst = (int64_t) wt*stride_sample_dst + (int64_t) zt*stride_channel_dst + (int64_t) jt*J*stride_col_dst;
if (ids_dst) {
const ggml_cuda_mm_id_tile_spec tsj = tile_spec[jt];
@@ -1257,7 +1257,7 @@ static __global__ void mul_mat_q(
int col_high = min(col_low + J, ncols_dst);
int offset_y = wt*stride_sample_y + zt*stride_channel_y;
int offset_y_scale = wt*nchannels_y.z*ncols_y + zt*ncols_y;
- int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst;
+ int64_t offset_dst = (int64_t) wt*stride_sample_dst + (int64_t) zt*stride_channel_dst + (int64_t) jt*J*stride_col_dst;
if (ids_dst) {
const ggml_cuda_mm_id_tile_spec tsj = tile_spec[jt];
@@ -1403,7 +1403,7 @@ static __global__ void mul_mat_q_stream_k_fixup(
const int it = tmp2.x;
if (!ids_dst) {
- const int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*J*stride_col_dst + it*I;
+ const int64_t offset_dst = (int64_t) wt*stride_sample_dst + (int64_t) zt*stride_channel_dst + (int64_t) jt*J*stride_col_dst + it*I;
dst += offset_dst;
const int i_max = nrows_x - it*I - 1;
diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu
index 612d3e313b25..091fc3071561 100644
--- a/ggml/src/ggml-cuda/mmvq.cu
+++ b/ggml/src/ggml-cuda/mmvq.cu
@@ -96,7 +96,8 @@ enum mmvq_parameter_table_id {
MMVQ_PARAMETERS_RDNA2,
MMVQ_PARAMETERS_RDNA3_0,
MMVQ_PARAMETERS_RDNA4,
- MMVQ_PARAMETERS_GB10
+ MMVQ_PARAMETERS_GB10,
+ MMVQ_PARAMETERS_ORIN
};
static constexpr __device__ mmvq_parameter_table_id get_device_table_id() {
@@ -112,6 +113,8 @@ static constexpr __device__ mmvq_parameter_table_id get_device_table_id() {
return MMVQ_PARAMETERS_TURING;
#elif __CUDA_ARCH__ == GGML_CUDA_CC_GB10
return MMVQ_PARAMETERS_GB10;
+#elif __CUDA_ARCH__ == GGML_CUDA_CC_ORIN
+ return MMVQ_PARAMETERS_ORIN;
#else
return MMVQ_PARAMETERS_GENERIC;
#endif
@@ -136,6 +139,9 @@ static __host__ mmvq_parameter_table_id get_device_table_id(int cc) {
if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) == GGML_CUDA_CC_GB10) {
return MMVQ_PARAMETERS_GB10;
}
+ if (GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) == GGML_CUDA_CC_ORIN) {
+ return MMVQ_PARAMETERS_ORIN;
+ }
return MMVQ_PARAMETERS_GENERIC;
}
@@ -580,6 +586,9 @@ static constexpr __host__ __device__ int calc_nwarps(ggml_type type, int ncols_d
}
return generic;
}
+ if (table_id == MMVQ_PARAMETERS_ORIN) {
+ return calc_nwarps(type, ncols_dst, MMVQ_PARAMETERS_GENERIC);
+ }
return 1;
}
@@ -620,6 +629,41 @@ static constexpr __host__ __device__ int calc_rows_per_block(ggml_type type, int
return 1;
}
}
+ if (table_id == MMVQ_PARAMETERS_ORIN) {
+ switch (ncols_dst) {
+ case 1:
+ if (small_k) {
+ return nwarps;
+ }
+ // One row per warp (more rows per block, more loads in flight) for these types, measured on Jetson Orin Nano.
+ // Other types are faster in isolated mat-vec too, but gained at most 2 % end to end.
+ switch (type) {
+ case GGML_TYPE_Q4_0:
+ case GGML_TYPE_Q4_1:
+ case GGML_TYPE_Q5_0:
+ case GGML_TYPE_Q5_1:
+ case GGML_TYPE_Q8_0:
+ case GGML_TYPE_Q4_K:
+ case GGML_TYPE_Q5_K:
+ case GGML_TYPE_Q6_K:
+ case GGML_TYPE_IQ4_NL:
+ return nwarps;
+ default:
+ break;
+ }
+ return mmvq_two_rows_ncols1(type) ? 2 : 1;
+ case 2:
+ case 3:
+ case 4:
+ case 5:
+ case 6:
+ case 7:
+ case 8:
+ return 2;
+ default:
+ return 1;
+ }
+ }
return 1;
}
diff --git a/ggml/src/ggml-cuda/rope.cu b/ggml/src/ggml-cuda/rope.cu
index 2604d014f854..657b58a4d05c 100644
--- a/ggml/src/ggml-cuda/rope.cu
+++ b/ggml/src/ggml-cuda/rope.cu
@@ -92,13 +92,16 @@ static __global__ void rope_norm(const T * x,
} else if constexpr (std::is_same_v) {
half2 v = make_half2(x0, x1);
ggml_cuda_memcpy_1<4>(dst + idst, &v);
+ } else if constexpr (std::is_same_v) {
+ const __align__(4) nv_bfloat16 v[2] = { ggml_cuda_cast(x0), ggml_cuda_cast(x1) };
+ ggml_cuda_memcpy_1(dst + idst, v);
}
};
if (i0 < n_offs || i0 >= n_offs + n_dims) {
if (inplace) {
return;
}
- store_coaelsced(x[ix + 0], x[ix + 1]);
+ store_coaelsced(ggml_cuda_cast(x[ix + 0]), ggml_cuda_cast(x[ix + 1]));
return;
}
@@ -113,8 +116,8 @@ static __global__ void rope_norm(const T * x,
rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
- const float x0 = x[ix + 0];
- const float x1 = x[ix + 1];
+ const float x0 = ggml_cuda_cast(x[ix + 0]);
+ const float x1 = ggml_cuda_cast(x[ix + 1]);
store_coaelsced(x0 * cos_theta - x1 * sin_theta, x0 * sin_theta + x1 * cos_theta);
}
@@ -189,8 +192,8 @@ static __global__ void rope_neox(const T * x,
rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
// idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2
- const float x0 = x[ix + n_offs/2 + 0];
- const float x1 = x[ix + n_offs/2 + n_dims/2];
+ const float x0 = ggml_cuda_cast(x[ix + n_offs/2 + 0]);
+ const float x1 = ggml_cuda_cast(x[ix + n_offs/2 + n_dims/2]);
dst[idst + n_offs/2 + 0] = ggml_cuda_cast(x0 * cos_theta - x1 * sin_theta);
dst[idst + n_offs/2 + n_dims / 2] = ggml_cuda_cast(x0 * sin_theta + x1 * cos_theta);
@@ -293,8 +296,8 @@ static __global__ void rope_multi(const T * x,
rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, iw, ext_factor, attn_factor, cos_theta, sin_theta);
// idst/ix point at channel i0/2; the first channel of the rotated pair is n_offs + iw/2 = i0/2 + n_offs/2
- const float x0 = x[ix + n_offs/2 + 0];
- const float x1 = x[ix + n_offs/2 + n_dims/2];
+ const float x0 = ggml_cuda_cast(x[ix + n_offs/2 + 0]);
+ const float x1 = ggml_cuda_cast(x[ix + n_offs/2 + n_dims/2]);
dst[idst + n_offs/2 + 0] = ggml_cuda_cast(x0*cos_theta - x1*sin_theta);
dst[idst + n_offs/2 + n_dims/2] = ggml_cuda_cast(x0*sin_theta + x1*cos_theta);
@@ -357,11 +360,11 @@ static __global__ void rope_vision(const T * x,
rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, cos_theta, sin_theta);
- const float x0 = x[ix + 0];
- const float x1 = x[ix + n_dims];
+ const float x0 = ggml_cuda_cast(x[ix + 0]);
+ const float x1 = ggml_cuda_cast(x[ix + n_dims]);
- dst[idst + 0] = x0*cos_theta - x1*sin_theta;
- dst[idst + n_dims] = x0*sin_theta + x1*cos_theta;
+ dst[idst + 0] = ggml_cuda_cast(x0*cos_theta - x1*sin_theta);
+ dst[idst + n_dims] = ggml_cuda_cast(x0*sin_theta + x1*cos_theta);
}
template
@@ -569,8 +572,8 @@ void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
}
cudaStream_t stream = ctx.stream();
- GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
- GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
+ GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_BF16);
+ GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_BF16);
// When not fused, src0 and dst types must match
// When fused (ROPE+VIEW+SET_ROWS), src0 may be F32 and dst may be F16
GGML_ASSERT(src0->type == dst->type || (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F16));
@@ -655,6 +658,16 @@ void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_BF16) {
+ rope_neox_cuda((const nv_bfloat16 *) src0_d, (nv_bfloat16 *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
+ set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_F16) {
+ rope_neox_cuda((const nv_bfloat16 *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
+ set_rows_stride, inplace, stream);
} else {
GGML_ABORT("fatal error");
}
@@ -674,6 +687,16 @@ void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
row_indices, set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_BF16) {
+ rope_multi_cuda((const nv_bfloat16 *) src0_d, (nv_bfloat16 *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
+ row_indices, set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_F16) {
+ rope_multi_cuda((const nv_bfloat16 *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, sections, is_imrope,
+ row_indices, set_rows_stride, inplace, stream);
} else {
GGML_ABORT("fatal error");
}
@@ -686,6 +709,10 @@ void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
rope_vision_cuda((const half *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02, s03, s1,
s2, s3, n_dims, nr, pos, freq_scale, freq_base, ext_factor, attn_factor,
corr_dims, freq_factors, sections, stream);
+ } else if (src0->type == GGML_TYPE_BF16) {
+ rope_vision_cuda((const nv_bfloat16 *) src0_d, (nv_bfloat16 *) dst_d, ne00, ne01, ne02, s01, s02, s03, s1,
+ s2, s3, n_dims, nr, pos, freq_scale, freq_base, ext_factor, attn_factor,
+ corr_dims, freq_factors, sections, stream);
} else {
GGML_ABORT("fatal error");
}
@@ -705,6 +732,16 @@ void ggml_cuda_op_rope_impl(ggml_backend_cuda_context & ctx,
s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_BF16) {
+ rope_norm_cuda((const nv_bfloat16 *) src0_d, (nv_bfloat16 *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
+ set_rows_stride, inplace, stream);
+ } else if (src0->type == GGML_TYPE_BF16 && dst_type == GGML_TYPE_F16) {
+ rope_norm_cuda((const nv_bfloat16 *) src0_d, (half *) dst_d, ne00, ne01, ne02, s01, s02,
+ s03, s1, s2, s3, n_dims, n_offs, nr, pos, freq_scale, freq_base,
+ ext_factor, attn_factor, corr_dims, freq_factors, row_indices,
+ set_rows_stride, inplace, stream);
} else {
GGML_ABORT("fatal error");
}
diff --git a/ggml/src/ggml-cuda/set-rows.cu b/ggml/src/ggml-cuda/set-rows.cu
index 4be3e649b73b..e4fb51619e66 100644
--- a/ggml/src/ggml-cuda/set-rows.cu
+++ b/ggml/src/ggml-cuda/set-rows.cu
@@ -4,8 +4,8 @@
typedef void (*set_rows_kernel_t)(const char * src, char * dst);
// Generic quantized set_rows kernel template
-template
-static __global__ void k_set_rows_quant(const float * __restrict__ src0,
+template
+static __global__ void k_set_rows_quant(const src_t * __restrict__ src0,
const idx_t * __restrict__ src1,
block_type * __restrict__ dst,
const int64_t ne_total,
@@ -56,13 +56,23 @@ static __global__ void k_set_rows_quant(const float * __restrict__ src0,
ggml_cuda_pdl_sync();
const int64_t dst_row = *(src1 + i10*s10 + i11*s11 + i12*s12);
- const float * src0_row = src0 + i01*s01 + i02*s02 + i03*s03;
+ const src_t * src0_row = src0 + i01*s01 + i02*s02 + i03*s03;
block_type * dst_row_ptr = dst + (dst_row*s1 + i02*s2 + i03*s3) / sizeof(block_type);
- const float * src_block = src0_row + i00;
+ const src_t * src_block = src0_row + i00;
block_type * dst_block = dst_row_ptr + i00 / qk;
- quantize_func(src_block, dst_block);
+ if constexpr (std::is_same_v) {
+ quantize_func(src_block, dst_block);
+ } else {
+ // the quantize functions take F32 input: widen the block first
+ float src_block_f32[qk];
+#pragma unroll
+ for (int j = 0; j < qk; ++j) {
+ src_block_f32[j] = ggml_cuda_cast(src_block[j]);
+ }
+ quantize_func(src_block_f32, dst_block);
+ }
GGML_UNUSED(ne10);
GGML_UNUSED(ne11);
@@ -71,9 +81,9 @@ static __global__ void k_set_rows_quant(const float * __restrict__ src0,
}
// Template dispatch function for quantized set_rows
-template
+template
static void set_rows_cuda_quant(
- const float * src0_d, const idx_t * src1_d, block_type * dst_d,
+ const src_t * src0_d, const idx_t * src1_d, block_type * dst_d,
const int64_t ne00, const int64_t ne01, const int64_t ne02, const int64_t ne03,
const int64_t ne10, const int64_t ne11, const int64_t ne12, const int64_t ne13,
const size_t nb01, const size_t nb02, const size_t nb03,
@@ -87,9 +97,9 @@ static void set_rows_cuda_quant(
const dim3 block_size(CUDA_SET_ROWS_BLOCK_SIZE);
const dim3 grid_size(num_blocks);
- const int64_t s01 = nb01/sizeof(float);
- const int64_t s02 = nb02/sizeof(float);
- const int64_t s03 = nb03/sizeof(float);
+ const int64_t s01 = nb01/sizeof(src_t);
+ const int64_t s02 = nb02/sizeof(src_t);
+ const int64_t s03 = nb03/sizeof(src_t);
const int64_t s10 = nb10/sizeof(idx_t);
const int64_t s11 = nb11/sizeof(idx_t);
const int64_t s12 = nb12/sizeof(idx_t);
@@ -104,7 +114,7 @@ static void set_rows_cuda_quant(
const uint3 ne11_fd = init_fastdiv_values((uint32_t) ne11);
const uint3 ne12_fd = init_fastdiv_values((uint32_t) ne12);
- k_set_rows_quant<<>>(
+ k_set_rows_quant<<>>(
src0_d, src1_d, dst_d, ne_total, ne10, ne11, ne12, ne13, s01, s02, s03, s10, s11, s12, s1, s2, s3, ne00_fd,
ne01_fd, ne02_fd, ne11_fd, ne12_fd);
}
@@ -268,7 +278,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_Q4_0) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_q4_0*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -278,7 +288,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_Q4_1) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_q4_1*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -288,7 +298,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_Q5_0) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_q5_0*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -298,7 +308,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_Q5_1) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_q5_1*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -308,7 +318,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_Q8_0) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_q8_0*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -318,7 +328,7 @@ static void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_tensor * s
stream
);
} else if (dst->type == GGML_TYPE_IQ4_NL) {
- set_rows_cuda_quant(
+ set_rows_cuda_quant(
src0_d, src1_d, (block_iq4_nl*)dst->data,
ne00, ne01, ne02, ne03,
ne10, ne11, ne12, ne13,
@@ -382,12 +392,11 @@ void set_rows_cuda(ggml_backend_cuda_context & ctx, const ggml_te
}
}
-
void ggml_cuda_op_set_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const ggml_tensor * src0 = dst->src[0];
const ggml_tensor * src1 = dst->src[1];
- GGML_ASSERT(src0->type == GGML_TYPE_F32 || (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16));
+ GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_BF16 || (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16));
GGML_ASSERT(src1->type == GGML_TYPE_I64 || src1->type == GGML_TYPE_I32);
if (src0->type == GGML_TYPE_F32) {
@@ -402,6 +411,12 @@ void ggml_cuda_op_set_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
} else {
set_rows_cuda(ctx, src0, src1, dst);
}
+ } else if (src0->type == GGML_TYPE_BF16) {
+ if (src1->type == GGML_TYPE_I64) {
+ set_rows_cuda(ctx, src0, src1, dst);
+ } else {
+ set_rows_cuda(ctx, src0, src1, dst);
+ }
} else {
GGML_ABORT("unsupported type %s", ggml_type_name(src0->type));
}
diff --git a/ggml/src/ggml-cuda/sleep.cu b/ggml/src/ggml-cuda/sleep.cu
new file mode 100644
index 000000000000..e6a40e5f213a
--- /dev/null
+++ b/ggml/src/ggml-cuda/sleep.cu
@@ -0,0 +1,32 @@
+#include "sleep.cuh"
+
+#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
+
+// %globaltimer is a nanosecond wall clock, unlike clock64() it is unaffected by the SM clock and by frequency scaling
+static __device__ __forceinline__ uint64_t globaltimer_ns() {
+ uint64_t t;
+ asm volatile("mov.u64 %0, %%globaltimer;" : "=l"(t));
+ return t;
+}
+
+// a single thread is enough, the next kernel on the same stream cannot start before this one retires
+static __global__ void sleep_ns(const uint64_t ns) {
+ const uint64_t t0 = globaltimer_ns();
+
+ while (globaltimer_ns() - t0 < ns) {}
+}
+
+void ggml_cuda_op_sleep(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+ sleep_ns<<<1, 1, 0, ctx.stream()>>>(1000*(uint64_t) ggml_get_op_params_i32(dst, 0));
+ CUDA_CHECK(cudaGetLastError());
+}
+
+#else
+
+void ggml_cuda_op_sleep(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
+ GGML_UNUSED(ctx);
+ GGML_UNUSED(dst);
+ GGML_ABORT("GGML_OP_SLEEP requires the %%globaltimer register, which is only available on CUDA");
+}
+
+#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
diff --git a/ggml/src/ggml-cuda/sleep.cuh b/ggml/src/ggml-cuda/sleep.cuh
new file mode 100644
index 000000000000..d62c3178565c
--- /dev/null
+++ b/ggml/src/ggml-cuda/sleep.cuh
@@ -0,0 +1,3 @@
+#include "common.cuh"
+
+void ggml_cuda_op_sleep(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
diff --git a/ggml/src/ggml-cuda/top-k.cu b/ggml/src/ggml-cuda/top-k.cu
index 227751eeadeb..2639a664d034 100644
--- a/ggml/src/ggml-cuda/top-k.cu
+++ b/ggml/src/ggml-cuda/top-k.cu
@@ -184,13 +184,6 @@ static __global__ void top_k_radix_gather(
constexpr int NBINS = 1 << RADIX_BITS;
constexpr int warp_size = ggml_cuda_get_physical_warp_size();
constexpr int nwarps = BLOCK_SIZE / warp_size;
- const auto popc = [](const ggml_cuda_thread_mask_t mask) {
- if constexpr (warp_size == 64) {
- return __popcll(mask);
- } else {
- return __popc(mask);
- }
- };
__shared__ int warp_ties[nwarps];
const int row = blockIdx.x / blocks_per_row;
@@ -247,11 +240,11 @@ static __global__ void top_k_radix_gather(
const bool is_tie = col < warp_end && top_k_float_to_ordered(row_src[col]) == threshold;
const ggml_cuda_thread_mask_t ties = ggml_cuda_ballot(is_tie);
- const int tie_pos = pos + popc(ties & (((ggml_cuda_thread_mask_t) 1 << lane) - 1));
+ const int tie_pos = pos + ggml_cuda_popc(ties & (((ggml_cuda_thread_mask_t) 1 << lane) - 1));
if (is_tie && tie_pos < pos_end) {
row_dst[k - rank + tie_pos] = col;
}
- pos += popc(ties);
+ pos += ggml_cuda_popc(ties);
}
}
diff --git a/ggml/src/ggml-metal/ggml-metal-device.cpp b/ggml/src/ggml-metal/ggml-metal-device.cpp
index 3fa6856e1629..cd9e0785053c 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-device.cpp
@@ -2551,6 +2551,26 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_mm(ggml_
char base[256];
char name[256];
+ const bool has_tensor = ggml_metal_device_get_props(ggml_metal_library_get_device(lib))->has_tensor;
+
+ if (has_tensor) {
+ // nr0 output positions per threadgroup, nr1 output channels, same tiles as kernel_mul_mm
+ snprintf(base, 256, "kernel_conv_2d_mm_tensor_%s_%s", ggml_type_name(op->src[0]->type), ggml_type_name(op->src[1]->type));
+ snprintf(name, 256, "%s", base);
+
+ ggml_metal_pipeline_with_params res = ggml_metal_library_get_pipeline(lib, name);
+ if (!res.pipeline) {
+ res = ggml_metal_library_compile_pipeline(lib, base, name, nullptr);
+ }
+
+ res.nr0 = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y;
+ res.nr1 = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X;
+ res.nsg = N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
+ res.smem = res.nr0 * N_MM_NK_TOTAL * sizeof(ggml_fp16_t);
+
+ return res;
+ }
+
const int nr0 = op->src[0]->ne[3] <= 32 ? 32 : 64;
const int nr1 = 2048/nr0;
@@ -2564,13 +2584,27 @@ ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_mm(ggml_
res.nr0 = nr0;
res.nr1 = nr1;
- res.nsg = 4;
+ res.nsg = N_SG_CONV_2D;
res.smem = nr0*nr1*sizeof(float);
return res;
}
+ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_mm_reduce(ggml_metal_library_t lib, const ggml_tensor * op) {
+ assert(op->op == GGML_OP_CONV_2D);
+ GGML_UNUSED(op);
+
+ const char * name = "kernel_conv_2d_mm_reduce";
+
+ 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);
+ }
+
+ return res;
+}
+
ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_dw(ggml_metal_library_t lib, const ggml_tensor * op, bool tiled) {
assert(op->op == GGML_OP_CONV_2D_DW);
diff --git a/ggml/src/ggml-metal/ggml-metal-device.h b/ggml/src/ggml-metal/ggml-metal-device.h
index df22635e0df4..c0466d62ff6f 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.h
+++ b/ggml/src/ggml-metal/ggml-metal-device.h
@@ -170,6 +170,7 @@ struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_col2im_1d
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_snake (ggml_metal_library_t lib, enum ggml_type type);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d (ggml_metal_library_t lib, const struct ggml_tensor * op);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_mm (ggml_metal_library_t lib, const struct ggml_tensor * op);
+struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_mm_reduce (ggml_metal_library_t lib, const struct ggml_tensor * op);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_2d_dw (ggml_metal_library_t lib, const struct ggml_tensor * op, bool tiled);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_conv_3d (ggml_metal_library_t lib, const struct ggml_tensor * op);
struct ggml_metal_pipeline_with_params ggml_metal_library_get_pipeline_upscale (ggml_metal_library_t lib, const struct ggml_tensor * op);
diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m
index f4c4991e934d..2e085453adb9 100644
--- a/ggml/src/ggml-metal/ggml-metal-device.m
+++ b/ggml/src/ggml-metal/ggml-metal-device.m
@@ -1713,7 +1713,7 @@ bool ggml_metal_device_supports_op(ggml_metal_device_t dev, const struct ggml_te
return has_simdgroup_reduction && op->src[0]->type == GGML_TYPE_F32 && (ggml_is_contiguous_rows(op->src[0]));
case GGML_OP_ROPE:
case GGML_OP_ROPE_BACK:
- return ggml_rope_get_truncate(op);
+ return ggml_rope_get_truncate(op) && (op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16);
case GGML_OP_IM2COL:
return ggml_is_contiguous(op->src[1]) && op->src[1]->type == GGML_TYPE_F32 && (op->type == GGML_TYPE_F16 || op->type == GGML_TYPE_F32);
case GGML_OP_CONV_2D:
@@ -2375,7 +2375,7 @@ void ggml_metal_buffer_memset_tensor(ggml_metal_buffer_t buf, struct ggml_tensor
id encoder = [cmd_buf blitCommandEncoder];
[encoder fillBuffer:bid_dst.metal
- range:NSMakeRange(bid_dst.offs, bid_dst.offs + size)
+ range:NSMakeRange(bid_dst.offs, size)
value:value];
[encoder endEncoding];
diff --git a/ggml/src/ggml-metal/ggml-metal-impl.h b/ggml/src/ggml-metal/ggml-metal-impl.h
index 7fd9d97cff22..aad014d837e9 100644
--- a/ggml/src/ggml-metal/ggml-metal-impl.h
+++ b/ggml/src/ggml-metal/ggml-metal-impl.h
@@ -17,6 +17,8 @@
#define N_ARGMAX_NPART_MAX 256
#define N_ARGMAX_CHUNK 4096
+#define N_SG_CONV_2D 4
+
// kernel parameters for mat-vec threadgroups
//
// N_R0: number of src0 rows to process per simdgroup
@@ -752,8 +754,14 @@ typedef struct {
int32_t p1;
int32_t d0;
int32_t d1;
+ int32_t nk; // reduction length per split
} ggml_metal_kargs_conv_2d;
+typedef struct {
+ int32_t ne; // output elements
+ int32_t nsplit;
+} ggml_metal_kargs_conv_2d_reduce;
+
typedef struct {
uint64_t nb00; // kernel strides
uint64_t nb01;
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.cpp b/ggml/src/ggml-metal/ggml-metal-ops.cpp
index 65a81b81e9d0..1ae7f3d5dcf9 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.cpp
+++ b/ggml/src/ggml-metal/ggml-metal-ops.cpp
@@ -4959,6 +4959,33 @@ int ggml_metal_op_im2col(ggml_metal_op_t ctx, int idx) {
return 1;
}
+// number of reduction splits of the conv_2d matmul kernels
+// A small output with a long reduction leaves the GPU idle, so the reduction is split until there are about 1M partial sums.
+static int ggml_metal_op_conv_2d_nsplit(const ggml_tensor * op, int32_t * nk) {
+ const int64_t K = op->src[0]->ne[0]*op->src[0]->ne[1]*op->src[0]->ne[2];
+ const int64_t nsteps = (K + 31)/32;
+ const int64_t ne = ggml_nelements(op);
+
+ // at least 3 reduction steps of 32 per split, at most 32 splits
+ const int64_t nsplit = std::max(1, std::min({ (1024*1024 + ne - 1)/ne, nsteps/3, 32 }));
+ const int64_t steps = (nsteps + nsplit - 1)/nsplit;
+
+ if (nk) {
+ *nk = (int32_t) (steps*32);
+ }
+
+ return (int) ((nsteps + steps - 1)/steps);
+}
+
+size_t ggml_metal_op_conv_2d_extra_tmp(const ggml_tensor * op) {
+ assert(op->op == GGML_OP_CONV_2D);
+
+ const int nsplit = ggml_metal_op_conv_2d_nsplit(op, nullptr);
+
+ // partial sums of the reduction splits
+ return nsplit > 1 ? nsplit*ggml_nelements(op)*sizeof(float) : 0;
+}
+
int ggml_metal_op_conv_2d(ggml_metal_op_t ctx, int idx) {
ggml_tensor * op = ctx->node(idx);
@@ -5012,11 +5039,17 @@ int ggml_metal_op_conv_2d(ggml_metal_op_t ctx, int idx) {
/*.p1 =*/ p1,
/*.d0 =*/ d0,
/*.d1 =*/ d1,
+ /*.nk =*/ 0,
};
const ggml_metal_device_props * props_dev = ggml_metal_device_get_props(ctx->dev);
- if (props_dev->has_simdgroup_mm) {
+ // the matmul kernels walk the patches with 32-bit byte offsets inside an image
+ // larger images, possibly a strided view, take the generic kernel
+ const uint64_t max_off = (ne12 - 1)*nb12 + (ne11 - 1)*nb11 + (ne10 - 1)*nb10 + sizeof(float);
+ const bool fits_32 = max_off <= UINT32_MAX;
+
+ if (props_dev->has_simdgroup_mm && fits_32) {
auto pipeline = ggml_metal_library_get_pipeline_conv_2d_mm(lib, op);
const int nr0 = pipeline.nr0;
@@ -5026,15 +5059,48 @@ int ggml_metal_op_conv_2d(ggml_metal_op_t ctx, int idx) {
const int64_t NP = ne3*ne1*ne0;
const int64_t OC = ne03;
+ const int nsplit = ggml_metal_op_conv_2d_nsplit(op, &args.nk);
+
+ ggml_metal_buffer_id bid_dst = ggml_metal_get_buffer_id(op);
+ ggml_metal_buffer_id bid_tmp = bid_dst;
+ bid_tmp.offs += ggml_nbytes(op);
+
ggml_metal_encoder_set_pipeline(enc, pipeline);
ggml_metal_encoder_set_bytes (enc, &args, sizeof(args), 0);
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[0]), 1);
ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op->src[1]), 2);
- ggml_metal_encoder_set_buffer (enc, ggml_metal_get_buffer_id(op), 3);
+ ggml_metal_encoder_set_buffer (enc, nsplit > 1 ? bid_tmp : bid_dst, 3);
ggml_metal_encoder_set_threadgroup_memory_size(enc, pipeline.smem, 0);
- ggml_metal_encoder_dispatch_threadgroups(enc, (NP + nr1 - 1)/nr1, (OC + nr0 - 1)/nr0, 1, 32, nsg, 1);
+ if (props_dev->has_tensor) {
+ // per image: nr0 positions times nr1 channels per threadgroup
+ const int64_t OHW = ne1*ne0;
+ ggml_metal_encoder_dispatch_threadgroups(enc, (OC + nr1 - 1)/nr1, (OHW + nr0 - 1)/nr0, ne3*nsplit, 32, nsg, 1);
+ } else {
+ ggml_metal_encoder_dispatch_threadgroups(enc, (NP + nr1 - 1)/nr1, (OC + nr0 - 1)/nr0, nsplit, 32, nsg, 1);
+ }
+
+ if (nsplit > 1) {
+ GGML_ASSERT(ggml_is_contiguous(op));
+
+ // sync the 2 kernels
+ ggml_metal_op_concurrency_reset(ctx);
+
+ ggml_metal_kargs_conv_2d_reduce args0 = {
+ /*.ne =*/ (int32_t) ggml_nelements(op),
+ /*.nsplit =*/ nsplit,
+ };
+
+ auto pipeline0 = ggml_metal_library_get_pipeline_conv_2d_mm_reduce(lib, op);
+
+ ggml_metal_encoder_set_pipeline(enc, pipeline0);
+ ggml_metal_encoder_set_bytes (enc, &args0, sizeof(args0), 0);
+ ggml_metal_encoder_set_buffer (enc, bid_tmp, 1);
+ ggml_metal_encoder_set_buffer (enc, bid_dst, 2);
+
+ ggml_metal_encoder_dispatch_threadgroups(enc, (args0.ne + 255)/256, 1, 1, 256, 1, 1);
+ }
return 1;
}
diff --git a/ggml/src/ggml-metal/ggml-metal-ops.h b/ggml/src/ggml-metal/ggml-metal-ops.h
index 47889fa8490a..7dae178d5fb4 100644
--- a/ggml/src/ggml-metal/ggml-metal-ops.h
+++ b/ggml/src/ggml-metal/ggml-metal-ops.h
@@ -49,6 +49,8 @@ size_t ggml_metal_op_flash_attn_ext_extra_rows(const struct ggml_tensor * op);
size_t ggml_metal_op_argmax_extra(const struct ggml_tensor * op);
+size_t ggml_metal_op_conv_2d_extra_tmp(const struct ggml_tensor * op);
+
int ggml_metal_op_concat (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_repeat (ggml_metal_op_t ctx, int idx);
int ggml_metal_op_acc (ggml_metal_op_t ctx, int idx);
diff --git a/ggml/src/ggml-metal/ggml-metal.cpp b/ggml/src/ggml-metal/ggml-metal.cpp
index 9bed6421bbdd..9a1bd2c773a3 100644
--- a/ggml/src/ggml-metal/ggml-metal.cpp
+++ b/ggml/src/ggml-metal/ggml-metal.cpp
@@ -311,6 +311,10 @@ static size_t ggml_backend_metal_buffer_type_get_alloc_size(ggml_backend_buffer_
{
res = 2*sizeof(int32_t)*ggml_nelements(tensor->src[0]);
} break;
+ case GGML_OP_CONV_2D:
+ {
+ res += ggml_metal_op_conv_2d_extra_tmp(tensor);
+ } break;
case GGML_OP_ARGMAX:
{
res += ggml_metal_op_argmax_extra(tensor);
diff --git a/ggml/src/ggml-metal/kernels/conv.metal b/ggml/src/ggml-metal/kernels/conv.metal
index fdc32756ae3a..9f9b6edfc0df 100644
--- a/ggml/src/ggml-metal/kernels/conv.metal
+++ b/ggml/src/ggml-metal/kernels/conv.metal
@@ -285,7 +285,7 @@ kernel void kernel_conv_2d_mm(
ushort tiitg[[thread_index_in_threadgroup]],
ushort tiisg[[thread_index_in_simdgroup]],
ushort sgitg[[simdgroup_index_in_threadgroup]]) {
- constexpr short NSG = 4;
+ constexpr short NSG = N_SG_CONV_2D;
constexpr short NTH = 32*NSG;
constexpr short NK = 32;
constexpr short NKB = NK/8;
@@ -300,6 +300,10 @@ kernel void kernel_conv_2d_mm(
const int K = args.IC*args.KH*args.KW;
const int NP = args.N*args.OH*args.OW;
+ // this threadgroup reduces [k_begin, k_end), the splits are summed by kernel_conv_2d_mm_reduce
+ const int k_begin = tgpig.z*args.nk;
+ const int k_end = min(K, k_begin + args.nk);
+
const int r0 = tgpig.y*NR0;
const int r1 = tgpig.x*NR1;
@@ -308,8 +312,14 @@ kernel void kernel_conv_2d_mm(
const short lb_n = 8*sgitg + tiisg%8;
const short lb_k = 8*(tiisg/8);
+ // the patches are walked with 32-bit byte offsets inside the image, the host checks that an image fits
+ const uint nb10 = args.nb10;
+ const uint nb11 = args.nb11;
+ const uint nb12 = args.nb12;
+
int ix0[NPL];
int iy0[NPL];
+ uint off0[NPL];
bool ip_ok[NPL];
device const char * srcb[NPL];
@@ -324,6 +334,8 @@ kernel void kernel_conv_2d_mm(
ix0[t] = iow*args.s0 - args.p0;
iy0[t] = ioh*args.s1 - args.p1;
+ off0[t] = iy0[t]*nb11 + ix0[t]*nb10;
+
ip_ok[t] = ip < NP;
srcb[t] = src + in*args.nb13;
@@ -337,7 +349,7 @@ kernel void kernel_conv_2d_mm(
mc[i] = make_filled_simdgroup_matrix(0.f);
}
- for (int k0 = 0; k0 < K; k0 += NK) {
+ for (int k0 = k_begin; k0 < k_end; k0 += NK) {
threadgroup_barrier(mem_flags::mem_threadgroup);
{
@@ -349,7 +361,7 @@ kernel void kernel_conv_2d_mm(
const int oc = r0 + row;
half v = 0;
- if (kk < K && oc < args.OC) {
+ if (kk < k_end && oc < args.OC) {
v = *((device const TK *)(weights + oc*args.nb03) + kk);
}
@@ -364,7 +376,11 @@ kernel void kernel_conv_2d_mm(
int kw = kk % args.KW;
int kh = (kk / args.KW) % args.KH;
- int ic = kk / (args.KW*args.KH);
+ int dx = kw*args.d0;
+ int dy = kh*args.d1;
+
+ // byte offset of the reduction index kk, relative to the patch origin
+ uint offk = (kk / (args.KW*args.KH))*nb12 + dy*nb11 + dx*nb10;
FOR_UNROLL (short i = 0; i < 8; i++) {
const short k = lb_k + i;
@@ -372,12 +388,12 @@ kernel void kernel_conv_2d_mm(
FOR_UNROLL (short t = 0; t < NPL; t++) {
const short n = 32*t + lb_n;
- const int ix = ix0[t] + kw*args.d0;
- const int iy = iy0[t] + kh*args.d1;
+ const int ix = ix0[t] + dx;
+ const int iy = iy0[t] + dy;
half v = 0;
- if (ip_ok[t] && kk < K && ix >= 0 && ix < args.IW && iy >= 0 && iy < args.IH) {
- v = *(device const float *)(srcb[t] + ic*args.nb12 + iy*args.nb11 + ix*args.nb10);
+ if (ip_ok[t] && kk < k_end && (uint) ix < (uint) args.IW && (uint) iy < (uint) args.IH) {
+ v = *(device const float *)(srcb[t] + (off0[t] + offk));
}
const short ib = NKB*(n/8) + k/8;
@@ -386,11 +402,17 @@ kernel void kernel_conv_2d_mm(
}
kk++;
+ dx += args.d0;
+ offk += args.d0*nb10;
if (++kw == args.KW) {
- kw = 0;
+ kw = 0;
+ dx = 0;
+ dy += args.d1;
+ offk += args.d1*nb11 - args.KW*args.d0*nb10;
if (++kh == args.KH) {
- kh = 0;
- ic++;
+ kh = 0;
+ dy = 0;
+ offk += nb12 - args.KH*args.d1*nb11;
}
}
}
@@ -398,7 +420,7 @@ kernel void kernel_conv_2d_mm(
threadgroup_barrier(mem_flags::mem_threadgroup);
- const short nkb = min((K - k0 + 7)/8, (int) NKB);
+ const short nkb = min((k_end - k0 + 7)/8, (int) NKB);
for (short ik = 0; ik < nkb; ik++) {
FOR_UNROLL (short i = 0; i < 4; i++) {
@@ -436,7 +458,8 @@ kernel void kernel_conv_2d_mm(
const int oh = (p / args.OW) % args.OH;
const int n = p / (args.OW*args.OH);
- device char * dstp = dst + n*args.nb3 + oh*args.nb1 + ow*args.nb0;
+ // with several splits, dst is the partial sum buffer, one contiguous output per split
+ device char * dstp = dst + (uint64_t) tgpig.z*args.N*args.nb3 + n*args.nb3 + oh*args.nb1 + ow*args.nb0;
for (short j = 0; j < NR0*NR1/NTH; j++) {
const short row = tiitg/NR1 + (NTH/NR1)*j;
@@ -448,6 +471,158 @@ kernel void kernel_conv_2d_mm(
}
}
+#ifdef GGML_METAL_HAS_TENSOR
+// conv_2d as a matmul on the tensor cores, like kernel_mul_mm
+// The patches of NRA output positions are gathered into threadgroup memory, the kernel rows are read from device memory.
+// One threadgroup computes NRA positions of one image times NRB output channels, over the reduction range of its split.
+template
+kernel void kernel_conv_2d_mm_tensor(
+ constant ggml_metal_kargs_conv_2d & args,
+ device const char * weights,
+ device const char * src,
+ device char * dst,
+ threadgroup char * shmem [[threadgroup(0)]],
+ uint3 tgpig[[threadgroup_position_in_grid]],
+ uint3 tgpg[[threadgroups_per_grid]],
+ ushort tiitg[[thread_index_in_threadgroup]]) {
+ constexpr int NRB = SZ_SIMDGROUP * N_MM_BLOCK_X * N_MM_SIMD_GROUP_X; // output channels
+ constexpr int NRA = SZ_SIMDGROUP * N_MM_BLOCK_Y * N_MM_SIMD_GROUP_Y; // output positions
+ constexpr int NK = N_MM_NK_TOTAL;
+ constexpr int NTH = N_SIMDWIDTH * N_MM_SIMD_GROUP_X * N_MM_SIMD_GROUP_Y;
+
+ // each thread gathers NK/(NTH/NRA) consecutive reduction indices of one position
+ constexpr int KPT = NK/(NTH/NRA);
+
+ const int K = args.IC*args.KH*args.KW;
+ const int OHW = args.OH*args.OW;
+
+ const int nsplit = tgpg.z/args.N;
+ const int img = tgpig.z/nsplit;
+ const int split = tgpig.z%nsplit;
+
+ const int k_begin = split*args.nk;
+ const int k_end = min(K, k_begin + args.nk);
+
+ const int ra = tgpig.y*NRA;
+ const int rb = tgpig.x*NRB;
+
+ threadgroup half * sa = (threadgroup half *) shmem;
+
+ // the position this thread gathers
+ const int row = tiitg%NRA;
+ const int kc = (tiitg/NRA)*KPT;
+ const int p = ra + row;
+ const bool pok = p < OHW;
+ const int ix0 = (p%args.OW)*args.s0 - args.p0;
+ const int iy0 = (p/args.OW)*args.s1 - args.p1;
+
+ device const char * srcb = src + img*args.nb13;
+
+ // the patch is walked with 32-bit byte offsets inside the image, the host checks that an image fits
+ const uint nb10 = args.nb10;
+ const uint nb11 = args.nb11;
+ const uint nb12 = args.nb12;
+
+ const uint off0 = iy0*nb11 + ix0*nb10;
+
+ device TK * ptrB = (device TK *) weights;
+ const int strideB = args.nb03/sizeof(TK);
+
+ mpp::tensor_ops::matmul2d<
+ mpp::tensor_ops::matmul2d_descriptor(
+ NRB, NRA, static_cast(dynamic_extent), false, true, true,
+ mpp::tensor_ops::matmul2d_descriptor::mode::multiply_accumulate),
+ execution_simdgroups> mm;
+
+ auto tB0 = tensor(ptrB, dextents(K, args.OC), array({1, strideB}));
+ auto tA0 = tensor(sa, dextents(NK, NRA));
+ auto cT = mm.get_destination_cooperative_tensor();
+
+ for (int k0 = k_begin; k0 < k_end; k0 += NK) {
+ // the reduction indices past k_end are beyond the extent given to the matmul, they are not gathered
+ if (k0 + kc < k_end) {
+ // gather KPT patch values of this position, walking the reduction index incrementally
+ int kk = k0 + kc;
+ int kw = kk % args.KW;
+ int kh = (kk / args.KW) % args.KH;
+ int dx = kw*args.d0;
+ int dy = kh*args.d1;
+
+ // byte offset of the reduction index kk, relative to the patch origin
+ uint offk = (kk / (args.KW*args.KH))*nb12 + dy*nb11 + dx*nb10;
+
+ FOR_UNROLL (short i = 0; i < KPT; i++) {
+ const int ix = ix0 + dx;
+ const int iy = iy0 + dy;
+
+ half v = 0;
+ if (pok && kk < k_end && (uint) ix < (uint) args.IW && (uint) iy < (uint) args.IH) {
+ v = *(device const float *)(srcb + (off0 + offk));
+ }
+ sa[row*NK + kc + i] = v;
+
+ kk++;
+ dx += args.d0;
+ offk += args.d0*nb10;
+ if (++kw == args.KW) {
+ kw = 0;
+ dx = 0;
+ dy += args.d1;
+ offk += args.d1*nb11 - args.KW*args.d0*nb10;
+ if (++kh == args.KH) {
+ kh = 0;
+ dy = 0;
+ offk += nb12 - args.KH*args.d1*nb11;
+ }
+ }
+ }
+ }
+
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+
+ // clamp the reduction extent so neither operand is read past the end of the split
+ const int kExt = min(NK, k_end - k0);
+
+ auto tAv = tensor(sa, dextents(kExt, NRA), array({1, NK}));
+ auto tBv = tensor(ptrB + k0 + rb*strideB, dextents(kExt, args.OC - rb), array({1, strideB}));
+
+ mm.run(tBv, tAv, cT);
+
+ threadgroup_barrier(mem_flags::mem_threadgroup);
+ }
+
+ // dst holds [OHW, OC] per image, with several splits it is the partial sum buffer, one contiguous output per split
+ const int tileM = min(NRA, OHW - ra);
+ const int tileN = min(NRB, args.OC - rb);
+
+ device float * dstTile = (device float *) (dst + (uint64_t) split*args.N*args.nb3 + img*args.nb3) + (uint64_t) rb*OHW + ra;
+
+ auto tD = tensor(dstTile, dextents(tileM, tileN), array({1, OHW}));
+ cT.store(tD);
+}
+
+typedef decltype(kernel_conv_2d_mm_tensor) kernel_conv_2d_mm_tensor_t;
+
+template [[host_name("kernel_conv_2d_mm_tensor_f32_f32")]] kernel kernel_conv_2d_mm_tensor_t kernel_conv_2d_mm_tensor;
+template [[host_name("kernel_conv_2d_mm_tensor_f16_f32")]] kernel kernel_conv_2d_mm_tensor_t kernel_conv_2d_mm_tensor;
+#endif
+
+kernel void kernel_conv_2d_mm_reduce(
+ constant ggml_metal_kargs_conv_2d_reduce & args,
+ device const float * tmp,
+ device float * dst,
+ uint tpig[[thread_position_in_grid]]) {
+ if ((int) tpig >= args.ne) {
+ return;
+ }
+
+ float sum = 0.0f;
+ for (int s = 0; s < args.nsplit; ++s) {
+ sum += tmp[(uint64_t) s*args.ne + tpig];
+ }
+ dst[tpig] = sum;
+}
+
typedef decltype(kernel_conv_2d_mm) kernel_conv_2d_mm_t;
template [[host_name("kernel_conv_2d_mm_f32_f32_64x32")]] kernel kernel_conv_2d_mm_t kernel_conv_2d_mm;
diff --git a/ggml/src/ggml-opencl/CMakeLists.txt b/ggml/src/ggml-opencl/CMakeLists.txt
index cdf4a5d54455..325f5c4bfc75 100644
--- a/ggml/src/ggml-opencl/CMakeLists.txt
+++ b/ggml/src/ggml-opencl/CMakeLists.txt
@@ -323,6 +323,9 @@ set(GGML_OPENCL_KERNELS
mul_mm_f16_f32_kq_kqv
mul_mm_f16_f32_kq_p8
mul_mm_q8_f32_kqv
+ mul_mm_q8_kq_p8
+ mul_mm_q8_kq_p8_dk512
+ flash_attn_q8_rows
conv2d
conv2d_f16_f32
conv2_dw
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cpp
index 0d9212232871..894f8235e9a0 100644
--- a/ggml/src/ggml-opencl/ggml-opencl.cpp
+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp
@@ -781,6 +781,10 @@ struct ggml_backend_opencl_context {
ggml_cl_buffer prealloc_fa_pd;
ggml_cl_buffer prealloc_fa_bmax;
ggml_cl_buffer prealloc_fa_bsum;
+ ggml_cl_buffer prealloc_fa_k8;
+ ggml_cl_buffer prealloc_fa_k8_scale;
+ ggml_cl_buffer prealloc_fa_q8;
+ ggml_cl_buffer prealloc_fa_q8_scale;
#ifdef GGML_OPENCL_USE_ADRENO_KERNELS
ggml_cl_buffer prealloc_adreno_xmem_const;
@@ -899,6 +903,10 @@ struct ggml_backend_opencl_context {
cl_kernel kernel_fa_kqv_direct_f16 = nullptr;
cl_kernel kernel_fa_kqv_direct_reduce = nullptr;
cl_kernel kernel_fa_v_transpose_q8 = nullptr;
+ cl_kernel kernel_fa_q8_rows_f16 = nullptr;
+ cl_kernel kernel_fa_q8_rows_f32 = nullptr;
+ cl_kernel kernel_mul_mm_q8_kq_p8 = nullptr;
+ cl_kernel kernel_mul_mm_q8_kq_p8_dk512 = nullptr;
ggml_opencl_fa_kernels fa;
#ifdef GGML_OPENCL_USE_ADRENO_KERNELS
ggml_cl_adreno_xmem_attn_state adreno_xmem_attn;
@@ -1506,7 +1514,18 @@ struct ggml_backend_opencl_context {
#endif // GGML_OPENCL_USE_ADRENO_KERNELS
~ggml_backend_opencl_context() {
- for (cl_kernel kernel : {kernel_mul_mm_f16_f32_kq_p8, kernel_mul_mm_q8_f32_kqv, kernel_fa_p8_fixup, kernel_fa_v_transpose_q8, kernel_fa_kqv_direct_f16, kernel_fa_kqv_direct_reduce}) {
+ for (cl_kernel kernel : {
+ kernel_mul_mm_f16_f32_kq_p8,
+ kernel_mul_mm_q8_f32_kqv,
+ kernel_fa_p8_fixup,
+ kernel_fa_v_transpose_q8,
+ kernel_fa_kqv_direct_f16,
+ kernel_fa_kqv_direct_reduce,
+ kernel_fa_q8_rows_f16,
+ kernel_fa_q8_rows_f32,
+ kernel_mul_mm_q8_kq_p8,
+ kernel_mul_mm_q8_kq_p8_dk512
+ }) {
if (kernel != nullptr) {
CL_CHECK(clReleaseKernel(kernel));
}
@@ -1648,7 +1667,7 @@ static cl_program build_program_from_source_ex(cl_context ctx, cl_device_id dev,
return NULL;
}
-static cl_program build_program_from_source(ggml_backend_opencl_context * backend_ctx, const char* program_buffer, const std::string &compile_opts) {
+static cl_program build_program_from_source(ggml_backend_opencl_context * backend_ctx, const char* program_buffer, const std::string &compile_opts, bool fatal = true) {
cl_context ctx = backend_ctx->context;
cl_device_id dev = backend_ctx->device;
@@ -1660,7 +1679,7 @@ static cl_program build_program_from_source(ggml_backend_opencl_context * backen
return p_cached;
}
- cl_program p = build_program_from_source_ex(ctx, dev, program_buffer, compile_opts, /*fatal=*/true);
+ cl_program p = build_program_from_source_ex(ctx, dev, program_buffer, compile_opts, fatal);
// Best-effort save of the freshly-built binary (no-op if cache disabled).
if (p != nullptr) {
@@ -3995,6 +4014,52 @@ static void load_cl_kernels(ggml_backend_opencl_context *backend_ctx) {
}
#ifdef GGML_OPENCL_USE_ADRENO_KERNELS
+ if (backend_ctx->gpu_family == ADRENO && backend_ctx->has_integer_dot) {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string rows_src {
+ #include "flash_attn_q8_rows.cl.h"
+ };
+ const std::string kq_src {
+ #include "mul_mm_q8_kq_p8.cl.h"
+ };
+#else
+ const std::string rows_src = read_file("flash_attn_q8_rows.cl");
+ const std::string kq_src = read_file("mul_mm_q8_kq_p8.cl");
+#endif
+ cl_program prog = build_program_from_source(backend_ctx, rows_src.c_str(), compile_opts, /*fatal=*/false);
+ if (prog != nullptr) {
+ CL_CHECK((backend_ctx->kernel_fa_q8_rows_f16 = clCreateKernel(prog, "kernel_fa_q8_rows_f16", &err), err));
+ CL_CHECK((backend_ctx->kernel_fa_q8_rows_f32 = clCreateKernel(prog, "kernel_fa_q8_rows_f32", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ }
+ prog = build_program_from_source(backend_ctx, kq_src.c_str(), compile_opts, /*fatal=*/false);
+ if (prog != nullptr) {
+ CL_CHECK((backend_ctx->kernel_mul_mm_q8_kq_p8 = clCreateKernel(prog, "kernel_mul_mm_q8_kq_p8", &err), err));
+ CL_CHECK(clReleaseProgram(prog));
+ }
+ GGML_LOG_CONT(".");
+ }
+
+ // mul_mm_q8_kq_p8_dk512
+ if (backend_ctx->gpu_family == ADRENO && backend_ctx->has_integer_dot) {
+#ifdef GGML_OPENCL_EMBED_KERNELS
+ const std::string kernel_src {
+ #include "mul_mm_q8_kq_p8_dk512.cl.h"
+ };
+#else
+ const std::string kernel_src = read_file("mul_mm_q8_kq_p8_dk512.cl");
+#endif
+ cl_program prog = build_program_from_source(backend_ctx, kernel_src.c_str(), compile_opts, /*fatal=*/false);
+ if (prog != nullptr) {
+ cl_kernel kernel = clCreateKernel(prog, "kernel_mul_mm_q8_kq_p8_dk512", &err);
+ CL_CHECK(clReleaseProgram(prog));
+ if (err == CL_SUCCESS) {
+ backend_ctx->kernel_mul_mm_q8_kq_p8_dk512 = kernel;
+ }
+ }
+ GGML_LOG_CONT(".");
+ }
+
// mul_mm_f16_f32_kq_p8
if (backend_ctx->gpu_family == ADRENO && backend_ctx->has_integer_dot) {
#ifdef GGML_OPENCL_EMBED_KERNELS
@@ -12305,7 +12370,7 @@ static bool ggml_opencl_supports_op(ggml_backend_dev_t dev, const struct ggml_te
}
return false;
}
- return true;
+ return op->src[0]->type == GGML_TYPE_F32 || op->src[0]->type == GGML_TYPE_F16;
}
case GGML_OP_SOLVE_TRI:
return op->src[0]->type == GGML_TYPE_F32 && ggml_is_contiguous(op->src[0]);
@@ -22922,12 +22987,58 @@ static void ggml_cl_flash_attn_prefill_bin(ggml_backend_t backend, const ggml_te
}
#endif // GGML_OPENCL_USE_ADRENO_KERNELS
+static void ggml_cl_fa_q8_rows(
+ ggml_backend_t backend, const ggml_tensor * src, const ggml_tensor * dst,
+ cl_mem values, cl_mem scales, int64_t row0, int nrow
+) {
+ auto * backend_ctx = (ggml_backend_opencl_context *) backend->context;
+ auto * extra = (ggml_tensor_extra_cl *) (src->view_src ? src->view_src->extra : src->extra);
+ const cl_ulong offset = extra->offset + src->view_offs + row0 * src->nb[1];
+ const cl_ulong nb1 = src->nb[1];
+ const cl_ulong nb2 = src->nb[2];
+ const int dk = src->ne[0];
+ const int nhead = src->ne[2];
+ cl_kernel kernel;
+ switch (src->type) {
+ case GGML_TYPE_F16: kernel = backend_ctx->kernel_fa_q8_rows_f16; break;
+ case GGML_TYPE_F32: kernel = backend_ctx->kernel_fa_q8_rows_f32; break;
+ default: GGML_ABORT("unsupported int8 KQ input type");
+ }
+ cl_uint a = 0;
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &extra->data_device));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &offset));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &nb1));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &nb2));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &values));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &scales));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &dk));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &nrow));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &nhead));
+ const size_t blocks = (size_t) (dk / 32) * nrow * nhead;
+ size_t gws[3] = {(blocks + 63) / 64 * 64, 1, 1};
+ size_t lws[3] = {64, 1, 1};
+ backend_ctx->enqueue_ndrange_kernel(kernel, 3, gws, lws, dst);
+}
+
+static void ggml_cl_fa_kq_int8_prepare(ggml_backend_t backend, const ggml_tensor * dst, int64_t n_q_chunk) {
+ auto * backend_ctx = (ggml_backend_opencl_context *) backend->context;
+ const ggml_tensor * q = dst->src[0];
+ const ggml_tensor * k = dst->src[1];
+ const size_t k_bytes = (size_t) k->ne[0] * k->ne[1] * k->ne[2];
+ const size_t q_bytes = (size_t) q->ne[0] * n_q_chunk * q->ne[2];
+ backend_ctx->prealloc_fa_k8.allocate(backend_ctx->context, k_bytes);
+ backend_ctx->prealloc_fa_k8_scale.allocate(backend_ctx->context, k_bytes / 32 * sizeof(cl_half));
+ backend_ctx->prealloc_fa_q8.allocate(backend_ctx->context, q_bytes);
+ backend_ctx->prealloc_fa_q8_scale.allocate(backend_ctx->context, q_bytes / 32 * sizeof(cl_half));
+ ggml_cl_fa_q8_rows(backend, k, dst, backend_ctx->prealloc_fa_k8.buffer, backend_ctx->prealloc_fa_k8_scale.buffer, 0, k->ne[1]);
+}
+
static void ggml_cl_fa_kq_p8_dispatch(
ggml_backend_t backend,
const ggml_tensor * q, const ggml_tensor * k,
const ggml_tensor * mask, const ggml_tensor * sinks,
const ggml_tensor * dst,
- int64_t q0, int nqc, float scale, float max_bias, int p_pitch
+ int64_t q0, int nqc, float scale, float max_bias, int p_pitch, bool kq_int8
) {
auto * backend_ctx = (ggml_backend_opencl_context *) backend->context;
auto * extra_k = (ggml_tensor_extra_cl *) (k->view_src ? k->view_src->extra : k->extra);
@@ -22940,64 +23051,103 @@ static void ggml_cl_fa_kq_p8_dispatch(
const int D_A = k->ne[2];
const int D_B = q->ne[2];
- cl_int err;
-
- cl_buffer_region region = {extra_k->offset + k->view_offs, k->nb[1] * k->ne[1]};
- cl_mem k_sub = clCreateSubBuffer(extra_k->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err);
- CL_CHECK(err);
-
- cl_image_format fmt = {CL_RGBA, CL_FLOAT};
- cl_image_desc desc = {};
- desc.image_type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
- desc.image_width = region.size / 16;
- desc.buffer = k_sub;
- cl_mem k_img = clCreateImage(backend_ctx->context, CL_MEM_READ_ONLY, &fmt, &desc, nullptr, &err);
- CL_CHECK(err);
-
- region.origin = extra_q->offset + q->view_offs + q0 * q->nb[1];
- region.size = (size_t) K * N * D_B * sizeof(float);
- cl_mem q_sub = clCreateSubBuffer(extra_q->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err);
- CL_CHECK(err);
-
const cl_ulong off_mask = extra_m->offset + mask->view_offs + q0 * mask->nb[1];
const cl_ulong mask_nb1 = mask->nb[1];
const int n_head_log2 = 1u << (unsigned) floorf(log2f((float) D_B));
const float m0 = powf(2.0f, -max_bias / n_head_log2);
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
+ cl_kernel kernel;
cl_uint a = 0;
- cl_kernel kernel = backend_ctx->kernel_mul_mm_f16_f32_kq_p8;
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &k_img));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &q_sub));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_pq.buffer));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bmax.buffer));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bsum.buffer));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &extra_m->data_device));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &off_mask));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &mask_nb1));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &scale));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &max_bias));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m0));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m1));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &n_head_log2));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &M));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &K));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &N));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_A));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_B));
- CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &p_pitch));
+ if (kq_int8) {
+ ggml_cl_fa_q8_rows(backend, q, dst, backend_ctx->prealloc_fa_q8.buffer, backend_ctx->prealloc_fa_q8_scale.buffer, q0, nqc);
+ kernel = K <= 256 ? backend_ctx->kernel_mul_mm_q8_kq_p8 : backend_ctx->kernel_mul_mm_q8_kq_p8_dk512;
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_k8.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_k8_scale.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_q8.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_q8_scale.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_pq.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bmax.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bsum.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &extra_m->data_device));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &off_mask));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &mask_nb1));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &scale));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &max_bias));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m0));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m1));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &n_head_log2));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &K));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &M));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &N));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_B));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_A));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &p_pitch));
+ size_t gws[3] = {64 * (((size_t) N + 31) / 32) * (D_B / D_A), ((size_t) M + 511) / 512, (size_t) D_A};
+ size_t lws[3] = {64, 1, 1};
+ backend_ctx->enqueue_ndrange_kernel(kernel, 3, gws, lws, dst);
+ } else {
+ cl_int err;
- const size_t n_tiles = (N + 31) / 32;
- size_t gws[3] = {64, n_tiles, (size_t)(M / 64) * D_B};
+ cl_buffer_region region = {extra_k->offset + k->view_offs, k->nb[1] * k->ne[1]};
+ cl_mem k_sub = clCreateSubBuffer(extra_k->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err);
+ CL_CHECK(err);
- const bool two_tiles = n_tiles % 2 == 0 && backend_ctx->get_kernel_workgroup_size(kernel) >= 128;
- size_t lws[3] = {64, two_tiles ? (size_t) 2 : (size_t) 1, 1};
+ cl_image_format fmt = {CL_RGBA, CL_FLOAT};
+ cl_image_desc desc = {};
+ desc.image_type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
+ desc.image_width = region.size / 16;
+ desc.buffer = k_sub;
+ cl_mem k_img = clCreateImage(backend_ctx->context, CL_MEM_READ_ONLY, &fmt, &desc, nullptr, &err);
+ CL_CHECK(err);
- backend_ctx->enqueue_ndrange_kernel(kernel, 3, gws, lws, dst);
- CL_CHECK(clReleaseMemObject(k_img));
- CL_CHECK(clReleaseMemObject(k_sub));
- CL_CHECK(clReleaseMemObject(q_sub));
+ region.origin = extra_q->offset + q->view_offs + q0 * q->nb[1];
+ region.size = (size_t) K * N * D_B * sizeof(float);
+ cl_mem q_sub = clCreateSubBuffer(extra_q->data_device, 0, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err);
+ CL_CHECK(err);
+
+ kernel = backend_ctx->kernel_mul_mm_f16_f32_kq_p8;
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &k_img));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &q_sub));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_pq.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bmax.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &backend_ctx->prealloc_fa_bsum.buffer));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_mem), &extra_m->data_device));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &off_mask));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(cl_ulong), &mask_nb1));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &scale));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &max_bias));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m0));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(float), &m1));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &n_head_log2));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &M));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &K));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &N));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_A));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &D_B));
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &p_pitch));
+
+ // Pack query heads of each KV group into a short batch's tile.
+ static const bool fold_enabled = [] {
+ const char * e = getenv("GGML_OPENCL_FA_KQ_P8_FOLD");
+ return !e || e[0] != '0';
+ }();
+ const int gqa = D_B / D_A;
+ const int fold = fold_enabled && gqa > 1 && N < 32 && gqa <= INT_MAX / N ? gqa : 1;
+ CL_CHECK(clSetKernelArg(kernel, a++, sizeof(int), &fold));
+
+ const size_t n_tiles = ((size_t) N * fold + 31) / 32;
+ size_t gws[3] = {64, n_tiles, (size_t)(M / 64) * (fold > 1 ? D_A : D_B)};
+
+ const bool two_tiles = n_tiles % 2 == 0 && backend_ctx->get_kernel_workgroup_size(kernel) >= 128;
+ size_t lws[3] = {64, two_tiles ? (size_t) 2 : (size_t) 1, 1};
+
+ backend_ctx->enqueue_ndrange_kernel(kernel, 3, gws, lws, dst);
+ CL_CHECK(clReleaseMemObject(k_img));
+ CL_CHECK(clReleaseMemObject(k_sub));
+ CL_CHECK(clReleaseMemObject(q_sub));
+ }
cl_mem sinks_buf = extra_q->data_device;
cl_ulong sinks_off = 0;
@@ -23261,7 +23411,7 @@ static void ggml_cl_flash_attn_decompose_kq_p8(
ggml_backend_t backend,
const ggml_tensor * q, const ggml_tensor * k,
ggml_tensor * dst,
- int64_t n_q_chunk, float scale, float max_bias, bool kqv_direct
+ int64_t n_q_chunk, float scale, float max_bias, bool kqv_direct, bool kq_int8
) {
auto * backend_ctx = (ggml_backend_opencl_context *) backend->context;
const ggml_tensor * v = dst->src[2];
@@ -23345,13 +23495,17 @@ static void ggml_cl_flash_attn_decompose_kq_p8(
backend_ctx->enqueue_ndrange_kernel(kernel, 3, gws, lws, dst);
}
+ if (kq_int8) {
+ ggml_cl_fa_kq_int8_prepare(backend, dst, n_q_chunk);
+ }
+
ggml_tensor_extra_cl extra_kqv;
ggml_tensor kqv;
for (int64_t q0 = 0; q0 < n_q; q0 += n_q_chunk) {
const int64_t nqc = MIN(n_q_chunk, n_q - q0);
- ggml_cl_fa_kq_p8_dispatch(backend, q, k, mask, sinks, dst, q0, nqc, scale, max_bias, kv_pitch);
+ ggml_cl_fa_kq_p8_dispatch(backend, q, k, mask, sinks, dst, q0, nqc, scale, max_bias, kv_pitch, kq_int8);
ggml_cl_fa_decompose_scratch_tensor(kqv, extra_kqv, backend_ctx->prealloc_fa_kqv.buffer, GGML_TYPE_F32, dv, nqc, n_head);
@@ -23408,6 +23562,7 @@ static void ggml_cl_flash_attn_decompose_kq_p8(
// head size. GGML_OPENCL_FA_DECOMPOSE_MB sets the KQ scratch budget (default 256).
// GGML_OPENCL_FA_KQ_P8=0 disables U8 probabilities and int8 KQV.
// GGML_OPENCL_FA_KQV_DIRECT=0 disables direct PV for small query batches.
+// GGML_OPENCL_FA_KQ_INT8=0 keeps F16 KQ; =1 enables int8 KQ for supported shapes.
static bool ggml_cl_flash_attn_decompose_dispatch(
ggml_backend_t backend,
const ggml_tensor * q, const ggml_tensor * k, ggml_tensor * dst
@@ -23537,12 +23692,46 @@ static bool ggml_cl_flash_attn_decompose_dispatch(
return false;
}
+ static const int kq_int8_env = [] {
+ const char * e = getenv("GGML_OPENCL_FA_KQ_INT8");
+ return (e && e[0]) ? (e[0] != '0' ? 1 : 0) : -1;
+ }();
+ static const int dk512_env = [] {
+ const char * e = getenv("GGML_OPENCL_FA_KQ_DK512_F16");
+ return (e && e[0]) ? (e[0] != '0' ? 1 : 0) : -1;
+ }();
+ const bool dk512 = dk > 256 && dk <= 512 && n_q >= 32 &&
+ (dk512_env == 1 || (dk512_env < 0 && backend_ctx->adreno_gen == ADRENO_GPU_GEN::X2E));
+ const bool kq_int8_possible = kq_p8 && (dk <= 256 || dk512) && dk % 32 == 0 &&
+ backend_ctx->kernel_fa_q8_rows_f16 != nullptr && backend_ctx->kernel_fa_q8_rows_f32 != nullptr &&
+ n_head <= INT_MAX && n_head_kv <= INT_MAX &&
+ n_q <= INT_MAX / n_head / (dk / 32) && n_kv <= INT_MAX / n_head_kv / (dk / 32) &&
+ (dst->src[4] == nullptr || dk > 64) &&
+ (kq_int8_env == 1 || (kq_int8_env < 0 && backend_ctx->adreno_gen == ADRENO_GPU_GEN::X2E && n_kv >= 2048)) &&
+ backend_ctx->get_kernel_workgroup_size(backend_ctx->kernel_fa_q8_rows_f16) >= 64 &&
+ backend_ctx->get_kernel_workgroup_size(backend_ctx->kernel_fa_q8_rows_f32) >= 64;
+ cl_kernel kq_int8_kernel = nullptr;
+ if (kq_int8_possible) {
+ kq_int8_kernel = dk <= 256 ? backend_ctx->kernel_mul_mm_q8_kq_p8 : backend_ctx->kernel_mul_mm_q8_kq_p8_dk512;
+ }
+ const bool kq_int8 = kq_int8_kernel != nullptr && backend_ctx->get_kernel_workgroup_size(kq_int8_kernel) >= 64;
+
+ // Fewer query tiles per KV block reduce cache pressure in the int8 KQ kernel.
+ static const int64_t kq_int8_max_chunk = [] {
+ const char * e = getenv("GGML_OPENCL_FA_KQ_INT8_MAX_CHUNK");
+ const int v = (e && e[0]) ? atoi(e) : 96;
+ return (int64_t) (v >= 32 ? (v / 32) * 32 : 96);
+ }();
+ if (kq_int8) {
+ n_q_chunk = MIN(n_q_chunk, kq_int8_max_chunk);
+ }
+
// even out the chunks, then align to the 32-wide n-tile
const int64_t n_chunks = (n_q + n_q_chunk - 1) / n_q_chunk;
n_q_chunk = MIN(n_q_chunk, ((n_q + n_chunks - 1) / n_chunks + 31) & ~(int64_t) 31);
if (kq_p8) {
- ggml_cl_flash_attn_decompose_kq_p8(backend, q, k, dst, n_q_chunk, scale, max_bias, kqv_direct);
+ ggml_cl_flash_attn_decompose_kq_p8(backend, q, k, dst, n_q_chunk, scale, max_bias, kqv_direct, kq_int8);
} else {
ggml_cl_flash_attn_decompose(backend, q, k, dst, n_q_chunk, scale, max_bias);
}
diff --git a/ggml/src/ggml-opencl/kernels/flash_attn_q8_rows.cl b/ggml/src/ggml-opencl/kernels/flash_attn_q8_rows.cl
new file mode 100644
index 000000000000..f0eda2539bcd
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/flash_attn_q8_rows.cl
@@ -0,0 +1,77 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+// Store 32-element blocks as separate int8 values and half scales.
+
+kernel void kernel_fa_q8_rows_f16(
+ global const char * src, ulong off_src, ulong nb1, ulong nb2,
+ global char * dq, global half * dd,
+ int ne0, int nrow, int nhead
+) {
+ const int nblk = ne0 / 32;
+ const int gid = get_global_id(0);
+ if (gid >= nblk*nrow*nhead) {
+ return;
+ }
+ const int b = gid % nblk;
+ const int r = (gid / nblk) % nrow;
+ const int h = gid / (nblk*nrow);
+
+ global const half * p = (global const half *)(src + off_src + (ulong)h*nb2 + (ulong)r*nb1) + b*32;
+ float v[32];
+ float amax = 0.0f;
+ #pragma unroll
+ for (int i = 0; i < 8; ++i) {
+ const float4 x = convert_float4(vload4(i, p));
+ v[i*4+0] = x.s0;
+ v[i*4+1] = x.s1;
+ v[i*4+2] = x.s2;
+ v[i*4+3] = x.s3;
+ amax = fmax(amax, fmax(fmax(fabs(x.s0), fabs(x.s1)), fmax(fabs(x.s2), fabs(x.s3))));
+ }
+ const float d = amax / 127.0f;
+ const float id = amax > 0.0f ? 127.0f / amax : 0.0f;
+ const size_t row = (size_t)h*nrow + r;
+ dd[row*nblk + b] = (half)d;
+ global char * q = dq + row*ne0 + b*32;
+ #pragma unroll
+ for (int i = 0; i < 32; ++i) {
+ q[i] = (char)clamp((int)rint(v[i]*id), -127, 127);
+ }
+}
+
+kernel void kernel_fa_q8_rows_f32(
+ global const char * src, ulong off_src, ulong nb1, ulong nb2,
+ global char * dq, global half * dd,
+ int ne0, int nrow, int nhead
+) {
+ const int nblk = ne0 / 32;
+ const int gid = get_global_id(0);
+ if (gid >= nblk*nrow*nhead) {
+ return;
+ }
+ const int b = gid % nblk;
+ const int r = (gid / nblk) % nrow;
+ const int h = gid / (nblk*nrow);
+
+ global const float * p = (global const float *)(src + off_src + (ulong)h*nb2 + (ulong)r*nb1) + b*32;
+ float v[32];
+ float amax = 0.0f;
+ #pragma unroll
+ for (int i = 0; i < 8; ++i) {
+ const float4 x = vload4(i, p);
+ v[i*4+0] = x.s0;
+ v[i*4+1] = x.s1;
+ v[i*4+2] = x.s2;
+ v[i*4+3] = x.s3;
+ amax = fmax(amax, fmax(fmax(fabs(x.s0), fabs(x.s1)), fmax(fabs(x.s2), fabs(x.s3))));
+ }
+ const float d = amax / 127.0f;
+ const float id = amax > 0.0f ? 127.0f / amax : 0.0f;
+ const size_t row = (size_t)h*nrow + r;
+ dd[row*nblk + b] = (half)d;
+ global char * q = dq + row*ne0 + b*32;
+ #pragma unroll
+ for (int i = 0; i < 32; ++i) {
+ q[i] = (char)clamp((int)rint(v[i]*id), -127, 127);
+ }
+}
diff --git a/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_p8.cl b/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_p8.cl
index 2c0a56c855c7..9c4f71a6d910 100644
--- a/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_p8.cl
+++ b/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_p8.cl
@@ -119,7 +119,8 @@ __kernel void mul_mm_f16_f32_kq_p8(
int N,
int D_A,
int D_B,
- int p_pitch // bytes per P row, >= M, multiple of 32
+ int p_pitch, // bytes per P row, >= M, multiple of 32
+ int gqa_fold
) {
// Match the n-tile-first grid in ggml_cl_fa_kq_p8_dispatch.
@@ -135,7 +136,8 @@ __kernel void mul_mm_f16_f32_kq_p8(
const uint col = block_id_m * TILESIZE_M;
const uint row = block_id_n * TILESIZE_N;
- const uint depth_A = block_id_d / (D_B/D_A);
+ const uint fold = (uint)gqa_fold;
+ const uint depth_A = fold > 1 ? block_id_d : block_id_d / (D_B/D_A);
const uint depth_B = block_id_d;
int line_stride_matrix_A_in_bytes = K * D_A * 2;
@@ -158,11 +160,18 @@ __kernel void mul_mm_f16_f32_kq_p8(
__local float matrix_B_local[1024];
- // Clamp Q loads in the last tile; columns past N are not stored.
- const uint c0 = min(row + b_globalOffsetInWords_xy.y, (uint)N - 1);
- const uint c1 = min(row + b_globalOffsetInWords_xy.y + 16, (uint)N - 1);
- const uint b_globalOffsetInWords00 = c0*strideBinElements + depth_B*K + b_globalOffsetInWords_xy.x;
- const uint b_globalOffsetInWords16 = c1*strideBinElements + depth_B*K + b_globalOffsetInWords_xy.x;
+ // Folded columns enumerate (query, head within the KV group).
+ const uint c0 = min(row + b_globalOffsetInWords_xy.y, (uint)N * fold - 1);
+ const uint c1 = min(row + b_globalOffsetInWords_xy.y + 16, (uint)N * fold - 1);
+ uint b_globalOffsetInWords00;
+ uint b_globalOffsetInWords16;
+ if (fold > 1) {
+ b_globalOffsetInWords00 = (c0/fold)*strideBinElements + (depth_B*fold + c0%fold)*K + b_globalOffsetInWords_xy.x;
+ b_globalOffsetInWords16 = (c1/fold)*strideBinElements + (depth_B*fold + c1%fold)*K + b_globalOffsetInWords_xy.x;
+ } else {
+ b_globalOffsetInWords00 = c0*strideBinElements + depth_B*K + b_globalOffsetInWords_xy.x;
+ b_globalOffsetInWords16 = c1*strideBinElements + depth_B*K + b_globalOffsetInWords_xy.x;
+ }
for (uint step=0; step < K; step+=TILESIZE_K) {
regA = mm_load_a(matrix_A, subMatrixAStartInElements, line_stride_matrix_A_in_bytes);
@@ -199,9 +208,10 @@ __kernel void mul_mm_f16_f32_kq_p8(
if (lane < 32) {
const int j = lane >> 1;
const int b = lane & 1;
- const uint n = row + half_*16 + j;
- const uint h = depth_B;
- if (n < (uint)N) {
+ const uint c = row + half_*16 + j;
+ const uint n = c / fold;
+ const uint h = depth_B*fold + c % fold;
+ if (c < (uint)N * fold) {
float slope = 1.0f;
if (max_bias > 0.0f) {
const float base = (int)h < n_head_log2 ? m0 : m1;
diff --git a/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8.cl b/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8.cl
new file mode 100644
index 000000000000..8e04ad393b5b
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8.cl
@@ -0,0 +1,174 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#ifdef cl_khr_integer_dot_product
+#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
+#endif
+
+#define KQ_TN 32
+#define KQ_MB 8
+#define KQ_TM 64
+#define KQ_WG 64
+#define KQ_DK_MAX 256
+
+__attribute__((qcom_wave_pair_mode(1)))
+kernel void kernel_mul_mm_q8_kq_p8(
+ global const uint * kq, // K int8, [head_kv][n_kv][dk] as packed uints
+ global const half * kd, // K scales, [head_kv][n_kv][dk/32]
+ global const uint * qq, // Q int8, [head][n_q][dk] as packed uints
+ global const half * qd, // Q scales, [head][n_q][dk/32]
+ global uchar * qp, // u8 P, [head][n_q][p_pitch]
+ global float * bmax, // per 32-row block max, [head][n_q][p_pitch/32]
+ global float * bsum, // per block sum of exp(s - bmax), [head][n_q][p_pitch/32]
+ global const char * mask, // f16 [>=n_kv][n_q] view, row stride mask_nb1 bytes
+ ulong offset_mask,
+ ulong mask_nb1,
+ float scale,
+ float max_bias,
+ float m0,
+ float m1,
+ int n_head_log2,
+ int dk,
+ int n_kv,
+ int n_q,
+ int n_head,
+ int n_head_kv,
+ int p_pitch // bytes per P row, >= n_kv, multiple of 32
+) {
+ const int lid = get_local_id(0);
+ const int gsz = n_head / n_head_kv;
+ const int g0 = get_group_id(0);
+ const int head_kv = get_group_id(2);
+ const int head = head_kv*gsz + (g0 % gsz);
+ const int qn0 = (g0 / gsz) * KQ_TN;
+ const int kv0 = get_group_id(1)*KQ_TM*KQ_MB + lid;
+
+ const int nblk = dk / 32;
+ const int nu = dk / 4;
+
+ __local uint sh_q [KQ_TN][KQ_DK_MAX/4];
+ __local half sh_qd[KQ_TN][KQ_DK_MAX/32];
+ __local float p8_lds[1024];
+
+ const size_t qbase = (size_t)head*n_q;
+ for (int i = lid; i < KQ_TN*(KQ_DK_MAX/16); i += KQ_WG) {
+ const int t = i / (KQ_DK_MAX/16);
+ const int u = (i % (KQ_DK_MAX/16)) * 4;
+ const int qi = qn0 + t;
+ uint4 v = (uint4)(0u);
+ if (qi < n_q && u < nu) {
+ v = vload4(0, &qq[(qbase + qi)*nu + u]);
+ }
+ vstore4(v, 0, &sh_q[t][u]);
+ }
+ for (int i = lid; i < KQ_TN*(KQ_DK_MAX/32); i += KQ_WG) {
+ const int t = i / (KQ_DK_MAX/32);
+ const int b = i % (KQ_DK_MAX/32);
+ const int qi = qn0 + t;
+ sh_qd[t][b] = (qi < n_q && b < nblk) ? qd[(qbase + qi)*nblk + b] : (half)0.0f;
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ float slope = 1.0f;
+ if (max_bias > 0.0f) {
+ const float base = head < n_head_log2 ? m0 : m1;
+ const int ex = head < n_head_log2 ? head + 1 : 2*(head - n_head_log2) + 1;
+ slope = pow(base, ex);
+ }
+ global const half * mrow = (global const half *)(mask + offset_mask);
+ const uint mstride = (uint)(mask_nb1 / 2);
+ const int pblk = p_pitch / 32;
+
+ for (int m = 0; m < KQ_MB; ++m) {
+ const int kv = kv0 + m*KQ_TM;
+ if (kv - lid >= n_kv) {
+ break; // uniform across the workgroup
+ }
+
+ float acc[KQ_TN];
+ #pragma unroll
+ for (int t = 0; t < KQ_TN; ++t) {
+ acc[t] = 0.0f;
+ }
+
+ const int kl = min(kv, n_kv - 1);
+ const size_t kbase = ((size_t)head_kv*n_kv + kl)*nu;
+ const size_t kdbas = ((size_t)head_kv*n_kv + kl)*nblk;
+
+ for (int b = 0; b < nblk; ++b) {
+ const uint4 w0 = vload4(0, &kq[kbase + b*8]);
+ const uint4 w1 = vload4(0, &kq[kbase + b*8 + 4]);
+ const float dks = (float)kd[kdbas + b];
+
+ #pragma unroll
+ for (int t = 0; t < KQ_TN; ++t) {
+ const uint4 a0 = vload4(0, &sh_q[t][b*8]);
+ const uint4 a1 = vload4(0, &sh_q[t][b*8 + 4]);
+ int raw = 0;
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s0, a0.s0, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s1, a0.s1, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s2, a0.s2, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s3, a0.s3, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s0, a1.s0, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s1, a1.s1, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s2, a1.s2, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s3, a1.s3, raw);
+ acc[t] += dks * (float)sh_qd[t][b] * (float)raw;
+ }
+ }
+
+ // Each lane owns one complete 32-row softmax block after the transpose.
+ const int kvblk0 = kv - lid;
+ #pragma unroll
+ for (int half_ = 0; half_ < 2; ++half_) {
+ barrier(CLK_LOCAL_MEM_FENCE);
+ #pragma unroll
+ for (int j = 0; j < 16; ++j) {
+ p8_lds[j*64 + lid] = acc[half_*16 + j];
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+ if (lid < 32) {
+ const int j = lid >> 1;
+ const int b = lid & 1;
+ const int n = qn0 + half_*16 + j;
+ if (n < n_q) {
+ const int kvb = kvblk0 + b*32;
+ const __local float * src = p8_lds + j*64 + b*32;
+ float16 s0 = vload16(0, src);
+ float16 s1 = vload16(1, src);
+ const global half * mp = mrow + (size_t)n*mstride + kvb;
+ const float16 m0v = convert_float16(vload16(0, mp));
+ const float16 m1v = convert_float16(vload16(1, mp));
+ s0 = s0*scale + slope*m0v;
+ s1 = s1*scale + slope*m1v;
+ const float16 mx16 = fmax(s0, s1);
+ const float8 mx8 = fmax(mx16.lo, mx16.hi);
+ const float4 mx4 = fmax(mx8.lo, mx8.hi);
+ const float2 mx2 = fmax(mx4.lo, mx4.hi);
+ const float amax = fmax(mx2.x, mx2.y);
+ const size_t prow = qbase + n;
+ const int blk = kvb >> 5;
+ float sum = 0.0f;
+ uchar16 q0 = (uchar16)(0);
+ uchar16 q1 = (uchar16)(0);
+ // finite sentinel: the program builds with -cl-finite-math-only
+ if (amax > -1.0e30f) {
+ const float16 e0 = exp(s0 - amax);
+ const float16 e1 = exp(s1 - amax);
+ const float16 a16 = e0 + e1;
+ const float8 a8 = a16.lo + a16.hi;
+ const float4 a4 = a8.lo + a8.hi;
+ const float2 a2 = a4.lo + a4.hi;
+ sum = a2.x + a2.y;
+ q0 = convert_uchar16_sat_rte(e0*255.0f);
+ q1 = convert_uchar16_sat_rte(e1*255.0f);
+ }
+ global uchar * qdst = qp + prow*(size_t)p_pitch + (size_t)kvb;
+ vstore16(q0, 0, qdst);
+ vstore16(q1, 1, qdst);
+ bmax[prow*pblk + blk] = amax;
+ bsum[prow*pblk + blk] = sum;
+ }
+ }
+ }
+ }
+}
diff --git a/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8_dk512.cl b/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8_dk512.cl
new file mode 100644
index 000000000000..f6ba2b6c2247
--- /dev/null
+++ b/ggml/src/ggml-opencl/kernels/mul_mm_q8_kq_p8_dk512.cl
@@ -0,0 +1,170 @@
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+#pragma OPENCL EXTENSION cl_khr_subgroups : enable
+#ifdef cl_khr_integer_dot_product
+#pragma OPENCL EXTENSION cl_khr_integer_dot_product : enable
+#endif
+
+#define KQ_TN 32
+#define KQ_MB 8
+#define KQ_TM 64
+#define KQ_WG 64
+#define KQ_DKC 256
+
+__attribute__((qcom_wave_pair_mode(1)))
+kernel void kernel_mul_mm_q8_kq_p8_dk512(
+ global const uint * kq, // K int8, [head_kv][n_kv][dk] as packed uints
+ global const half * kd, // K scales, [head_kv][n_kv][dk/32]
+ global const uint * qq, // Q int8, [head][n_q][dk] as packed uints
+ global const half * qd, // Q scales, [head][n_q][dk/32]
+ global uchar * qp, // u8 P, [head][n_q][p_pitch]
+ global float * bmax, // per 32-row block max, [head][n_q][p_pitch/32]
+ global float * bsum, // per block sum of exp(s - bmax), [head][n_q][p_pitch/32]
+ global const char * mask, // f16 [>=n_kv][n_q] view, row stride mask_nb1 bytes
+ ulong offset_mask,
+ ulong mask_nb1,
+ float scale,
+ float max_bias,
+ float m0,
+ float m1,
+ int n_head_log2,
+ int dk,
+ int n_kv,
+ int n_q,
+ int n_head,
+ int n_head_kv,
+ int p_pitch // bytes per P row, >= n_kv, multiple of 32
+) {
+ const int lid = get_local_id(0);
+ const int gsz = n_head / n_head_kv;
+ const int g0 = get_group_id(0);
+ const int head_kv = get_group_id(2);
+ const int head = head_kv*gsz + (g0 % gsz);
+ const int qn0 = (g0 / gsz) * KQ_TN;
+ const int kv0 = get_group_id(1)*KQ_TM*KQ_MB + lid;
+
+ const int nblk = dk / 32;
+ const int nu = dk / 4;
+
+ // Reuse the 256-wide Q slice for the F32 softmax scores after the contraction.
+ __local uint sh_q [KQ_TN*KQ_DKC/4];
+ __local half sh_qd[KQ_TN][KQ_DKC/32];
+ __local float * p8_lds = (__local float *)sh_q;
+
+ const size_t qbase = (size_t)head*n_q;
+
+ float slope = 1.0f;
+ if (max_bias > 0.0f) {
+ const float base = head < n_head_log2 ? m0 : m1;
+ const int ex = head < n_head_log2 ? head + 1 : 2*(head - n_head_log2) + 1;
+ slope = pow(base, ex);
+ }
+ global const half * mrow = (global const half *)(mask + offset_mask);
+ const uint mstride = (uint)(mask_nb1 / 2);
+ const int pblk = p_pitch / 32;
+
+ for (int m = 0; m < KQ_MB; ++m) {
+ const int kv = kv0 + m*KQ_TM;
+ if (kv - lid >= n_kv) {
+ break; // uniform across the workgroup
+ }
+
+ float acc[KQ_TN];
+ #pragma unroll
+ for (int t = 0; t < KQ_TN; ++t) {
+ acc[t] = 0.0f;
+ }
+
+ const int kl = min(kv, n_kv - 1);
+ const size_t kbase = ((size_t)head_kv*n_kv + kl)*nu;
+ const size_t kdbas = ((size_t)head_kv*n_kv + kl)*nblk;
+
+ for (int c0 = 0; c0 < nblk; c0 += KQ_DKC/32) {
+ const int cb = min(KQ_DKC/32, nblk - c0);
+ barrier(CLK_LOCAL_MEM_FENCE);
+ for (int i = lid; i < KQ_TN*(KQ_DKC/16); i += KQ_WG) {
+ const int t = i / (KQ_DKC/16);
+ const int u = (i % (KQ_DKC/16)) * 4;
+ const int qi = qn0 + t;
+ uint4 v = (uint4)(0u);
+ if (qi < n_q && u < cb*8) {
+ v = vload4(0, &qq[(qbase + qi)*nu + c0*8 + u]);
+ }
+ vstore4(v, 0, &sh_q[t*(KQ_DKC/4) + u]);
+ }
+ for (int i = lid; i < KQ_TN*(KQ_DKC/32); i += KQ_WG) {
+ const int t = i / (KQ_DKC/32);
+ const int b = i % (KQ_DKC/32);
+ const int qi = qn0 + t;
+ sh_qd[t][b] = (qi < n_q && b < cb) ? qd[(qbase + qi)*nblk + c0 + b] : (half)0.0f;
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ for (int b = 0; b < cb; ++b) {
+ const uint4 w0 = vload4(0, &kq[kbase + (c0 + b)*8]);
+ const uint4 w1 = vload4(0, &kq[kbase + (c0 + b)*8 + 4]);
+ const float dks = (float)kd[kdbas + c0 + b];
+
+ #pragma unroll
+ for (int t = 0; t < KQ_TN; ++t) {
+ const uint4 a0 = vload4(0, &sh_q[t*(KQ_DKC/4) + b*8]);
+ const uint4 a1 = vload4(0, &sh_q[t*(KQ_DKC/4) + b*8 + 4]);
+ int raw = 0;
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s0, a0.s0, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s1, a0.s1, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s2, a0.s2, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w0.s3, a0.s3, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s0, a1.s0, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s1, a1.s1, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s2, a1.s2, raw);
+ raw = dot_acc_sat_4x8packed_ss_int(w1.s3, a1.s3, raw);
+ acc[t] += dks * (float)sh_qd[t][b] * (float)raw;
+ }
+ }
+ }
+
+ // Finish reading the Q slice before writing scores to the same local memory.
+ const int kvblk0 = kv - lid;
+ barrier(CLK_LOCAL_MEM_FENCE);
+ #pragma unroll
+ for (int t = 0; t < KQ_TN; ++t) {
+ p8_lds[t*64 + lid] = acc[t];
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+ {
+ const int j = lid >> 1;
+ const int b = lid & 1;
+ const int n = qn0 + j;
+ if (n < n_q) {
+ const int kvb = kvblk0 + b*32;
+ __local float * src = p8_lds + j*64 + b*32;
+ const global half * mp = mrow + (size_t)n*mstride + kvb;
+ float4 s = vload4(0, src)*scale + slope*convert_float4(vload4(0, mp));
+ vstore4(s, 0, src);
+ float amax = fmax(fmax(s.x, s.y), fmax(s.z, s.w));
+ for (int c = 1; c < 8; ++c) {
+ s = vload4(c, src)*scale + slope*convert_float4(vload4(c, mp));
+ vstore4(s, c, src);
+ amax = fmax(amax, fmax(fmax(s.x, s.y), fmax(s.z, s.w)));
+ }
+ const size_t prow = qbase + n;
+ const int blk = kvb >> 5;
+ global uchar * qdst = qp + prow*(size_t)p_pitch + (size_t)kvb;
+ float sum = 0.0f;
+ // finite sentinel: the program builds with -cl-finite-math-only
+ if (amax > -1.0e30f) {
+ for (int c = 0; c < 8; ++c) {
+ const float4 e = exp(vload4(c, src) - amax);
+ sum += (e.x + e.y) + (e.z + e.w);
+ vstore4(convert_uchar4_sat_rte(e*255.0f), c, qdst);
+ }
+ } else {
+ for (int c = 0; c < 8; ++c) {
+ vstore4((uchar4)(0), c, qdst);
+ }
+ }
+ bmax[prow*pblk + blk] = amax;
+ bsum[prow*pblk + blk] = sum;
+ }
+ }
+ }
+}
diff --git a/ggml/src/ggml-openvino/ggml-decoder.cpp b/ggml/src/ggml-openvino/ggml-decoder.cpp
index 38db895c532f..519c35bc21a1 100644
--- a/ggml/src/ggml-openvino/ggml-decoder.cpp
+++ b/ggml/src/ggml-openvino/ggml-decoder.cpp
@@ -107,7 +107,8 @@ GgmlOvDecoder::GgmlOvDecoder(ggml_cgraph * cgraph, std::mapn_nodes; ++i) {
if ((cgraph->nodes[i]->op == GGML_OP_GATED_DELTA_NET && cgraph->nodes[i]->op_params[0] > 1) ||
- is_sampling_view(cgraph->nodes[i]) || is_conv_state_snapshot_writeback(cgraph->nodes[i])) {
+ is_sampling_view(cgraph->nodes[i]) || is_conv_state_snapshot_writeback(cgraph->nodes[i]) ||
+ is_ssm_state_writeback(cgraph->nodes[i])) {
std::tie(m_model_params, m_compute_params) = compute_llm_params(cgraph, false);
add_extra_inputs();
break;
@@ -494,6 +495,10 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
}
} else if (node->ne[0] == 1 && src->ne[0] * src->ne[1] * src->ne[2] == node->ne[1]) {
op_case = 3;
+ } else if (node->ne[0] * node->ne[1] == src->ne[0] * src->ne[1] && node->ne[2] == src->ne[2] &&
+ node->ne[3] == src->ne[3]) {
+ // head regroup, e.g. mamba2's [head_dim, n_head, n_tokens, n_seqs] -> [d_inner/n_group, n_group, n_tokens, n_seqs]
+ op_case = 1;
} else if (name.find("linear_attn_qkv_mixed") == 0 || name.find("alpha") == 0) {
op_case = 6;
} else if (name.find("linear_attn_out") == 0) {
@@ -597,6 +602,10 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
op_case = 3;
break;
}
+ if (is_column_set_rows(node)) {
+ op_case = 4;
+ break;
+ }
// `-fa off` writes the V cache transposed, through a [1, n] reshape of the cache: one element per row
if (node->src[2] != nullptr && node->src[2]->op == GGML_OP_RESHAPE && node->src[2]->ne[0] == 1 &&
is_kvcache(node->src[2]->src[0], nullptr)) {
@@ -797,6 +806,9 @@ int GgmlOvDecoder::compute_op_case(const ggml_tensor * node) const {
} else {
op_case = 1;
}
+ } else if (is_ssm_state_writeback(node)) {
+ op_case = 13;
+ break;
} else if (GgmlOvDecoder::is_conv_state_writeback(node)) {
op_case = 2;
break;
@@ -1296,6 +1308,11 @@ std::pair GgmlOvDecoder::compute_llm_params(ggml_cgr
writeback.snapshot_stride = destination->nb[2] / destination->nb[1];
compute_params.rs_writebacks[get_tensor_ov_name(cgraph, node)] = writeback;
}
+ if (is_ssm_state_writeback(node)) {
+ ComputeParams::RsWriteback writeback;
+ writeback.slot_begin = (int64_t) (node->src[1]->view_offs / node->src[1]->nb[1]);
+ compute_params.rs_writebacks[get_tensor_ov_name(cgraph, node)] = writeback;
+ }
if (is_conv_state_snapshot_writeback(node)) {
const auto source = node->src[0];
const auto dest = node->src[2];
@@ -1406,6 +1423,14 @@ std::pair GgmlOvDecoder::compute_llm_params(ggml_cgr
compute_params.attention_size =
ggml_openvino_static_kv_window(compute_params.attention_size, model_params.ctx_per_seq);
}
+ // The GPU plugin keeps device memory for each KV length it sees, and llama.cpp grows the KV length in steps of
+ // 256. Round it up to a power of two the same way, so that a long context uses a few lengths and not one per
+ // step. The KQ mask input is padded to this length with -inf (convert_ggml_input_to_ov).
+ if (!is_static && ggml_openvino_is_gpu() && !ggml_openvino_getenv_int("GGML_OPENVINO_STATEFUL_EXECUTION") &&
+ !compute_params.mask_name.empty() && compute_params.attention_size > 0 && model_params.ctx_per_seq > 0) {
+ compute_params.attention_size =
+ ggml_openvino_static_kv_window(compute_params.attention_size, model_params.ctx_per_seq);
+ }
model_params.ctx = model_params.ctx_per_seq * model_params.n_seq;
return {model_params, compute_params};
}
@@ -1416,6 +1441,15 @@ void GgmlOvDecoder::validate_cgraph() const {
}
}
+bool GgmlOvDecoder::has_inp_embd_leaf_use(const ggml_tensor * tensor) const {
+ for (int i = 0; i < m_cgraph->n_nodes; i++) {
+ if (is_inp_embd_leaf(tensor, m_cgraph->nodes[i])) {
+ return true;
+ }
+ }
+ return false;
+}
+
ov::PartialShape GgmlOvDecoder::get_graph_input_shape(const ggml_tensor * op,
const ggml_tensor * input,
int dynamic_dim_index) const {
@@ -1530,7 +1564,7 @@ ov::PartialShape GgmlOvDecoder::get_graph_input_shape(const ggml_tensor * op,
// index list has a static length; on CPU/GPU it may change across compiles (defrag).
input_shape = m_is_static ? ov::PartialShape{get_shape(input)} : ov::PartialShape{1, 1, 1, -1};
- } else if (input != nullptr && op != nullptr && is_inp_emb(input, op)) {
+ } else if (input != nullptr && op != nullptr && (is_inp_emb(input, op) || has_inp_embd_leaf_use(input))) {
input_shape = ov::PartialShape{get_shape(input)};
if (!m_is_static) {
input_shape[2] = -1;
@@ -1914,7 +1948,11 @@ void GgmlOvDecoder::compute_model_outputs() {
continue;
}
const bool inactive_top_k = is_sampling_top_k(cur_node) && !(cur_node->flags & GGML_TENSOR_FLAG_COMPUTE);
- if ((cur_node->flags & GGML_TENSOR_FLAG_OUTPUT) || inactive_top_k) {
+ auto cur_node_use_count = m_cgraph->use_counts[ggml_hash_find(&m_cgraph->visited_hash_set, cur_node)];
+ // llama flags result_norm in every graph but reads it only with embeddings on, when it has no consumer
+ // and is kept by the rule below.
+ const bool unread_embd = strcmp(cur_node->name, "result_norm") == 0 && cur_node_use_count > 0;
+ if (((cur_node->flags & GGML_TENSOR_FLAG_OUTPUT) && !unread_embd) || inactive_top_k) {
const auto name = get_tensor_ov_name(m_cgraph, cur_node);
m_model_outputs[name] = cur_node;
m_model_output_names.insert(name);
@@ -1925,7 +1963,12 @@ void GgmlOvDecoder::compute_model_outputs() {
m_model_outputs[name] = cache;
m_model_output_names.insert(name);
}
- auto cur_node_use_count = m_cgraph->use_counts[ggml_hash_find(&m_cgraph->visited_hash_set, cur_node)];
+ if (is_ssm_state_writeback(cur_node)) {
+ auto * cache = cur_node->view_src;
+ const auto name = get_tensor_ov_name(m_cgraph, cache);
+ m_model_outputs[name] = cache;
+ m_model_output_names.insert(name);
+ }
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.
if (cur_node != nullptr && ::is_inplace_op(cur_node) &&
@@ -2641,6 +2684,10 @@ bool GgmlOvDecoder::is_direct_gdn_view(const ggml_tensor * node) const {
std::vector GgmlOvDecoder::get_output_names(int node_idx) const {
auto * node = m_node_info_list[node_idx].node;
+ if (is_ssm_state_writeback(node)) {
+ // the written state block plus the full updated cache (see translate_cpy op_case 13)
+ return {m_node_info_list[node_idx].node_name, get_tensor_ov_name(m_cgraph, node->view_src)};
+ }
if (node->op == GGML_OP_GATED_DELTA_NET && (!m_model_params.has_rs_rollback || node->op_params[0] > 1)) {
std::string attn_name;
std::string state_name;
@@ -2669,6 +2716,10 @@ std::vector GgmlOvDecoder::get_output_names(int node_idx) const {
std::string GgmlOvDecoder::get_inplace_op_src(int node_idx) const {
auto * node = m_node_info_list[node_idx].node;
+ if (is_ssm_state_writeback(node)) {
+ // translate_cpy publishes the updated cache as a second named output
+ return "";
+ }
// Reset lengths (also in static mode) and dynamic remainder views can become nonempty
// after compilation. Keep their alias updates in the translated cache chain.
const bool runtime_recurrent_write = !m_naive && is_recurrent_cache(node->view_src) &&
@@ -2786,7 +2837,7 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
m_node_dynamic_dims[src] = 0;
continue;
}
- if (is_inp_scale_rows(src, node)) {
+ if (is_inp_scale_rows(src, node) || is_inp_embd_leaf(src, node)) {
m_node_dynamic_dims[src] = 1;
continue;
}
@@ -2865,6 +2916,11 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
m_node_dynamic_dims[node] = 2;
break;
}
+ if (node->src[0]->op == GGML_OP_SSM_SCAN) {
+ // y part [head_dim, n_head, n_tokens, n_seqs] at offset 0; the state part is token-independent
+ m_node_dynamic_dims[node] = node->view_offs == 0 ? 2 : -1;
+ break;
+ }
// Use stride-based matching: the stride of a VIEW dimension directly
// encodes which source dimension it indexes into, so it uniquely
// identifies the dynamic dim even when two dims share the same size.
@@ -3030,6 +3086,10 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
case GGML_OP_GATED_DELTA_NET:
m_node_dynamic_dims[node] = 1;
break;
+ case GGML_OP_SSM_SCAN:
+ // packed 1-D [y | state]; the y part grows with tokens
+ m_node_dynamic_dims[node] = 0;
+ break;
case GGML_OP_RMS_NORM:
case GGML_OP_L2_NORM:
case GGML_OP_NORM:
@@ -3080,12 +3140,15 @@ void GgmlOvDecoder::compute_node_dynamic_dims() {
case GGML_OP_CPY:
case GGML_OP_SUM:
case GGML_OP_MEAN:
+ case GGML_OP_ARANGE:
m_node_dynamic_dims[node] = -1;
break;
case GGML_OP_SET_ROWS:
- // a cache write returns the cache; other rows writes keep the shape of their destination
- m_node_dynamic_dims[node] = (is_kvcache_write(node) || is_conv_state_snapshot_writeback(node)) ?
- -1 : m_node_dynamic_dims[node->src[2]];
+ // a cache or column write returns its whole buffer; other rows writes keep the shape of their destination
+ m_node_dynamic_dims[node] =
+ (is_kvcache_write(node) || is_conv_state_snapshot_writeback(node) || is_column_set_rows(node)) ?
+ -1 :
+ m_node_dynamic_dims[node->src[2]];
break;
case GGML_OP_IM2COL: {
m_node_dynamic_dims[node] = -1;
diff --git a/ggml/src/ggml-openvino/ggml-decoder.h b/ggml/src/ggml-openvino/ggml-decoder.h
index b7ff4b715f06..12b08cebce4b 100644
--- a/ggml/src/ggml-openvino/ggml-decoder.h
+++ b/ggml/src/ggml-openvino/ggml-decoder.h
@@ -477,6 +477,21 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
return tensor->op == GGML_OP_GET_ROWS && op->op == GGML_OP_RMS_NORM;
}
+ // Raw float input [n_embd, n_tokens]: DFlash/DSpark/EAGLE3 features into fc, or raw embd batch / EAGLE3 g-embd into RMS_NORM
+ // MUL_MAT needs a weight leaf as src[0], so inp_mean (src[0] is computed) does not match
+ static bool is_inp_embd_leaf(const ggml_tensor * tensor, const ggml_tensor * op) {
+ if (tensor->op != GGML_OP_NONE || !(tensor->flags & GGML_TENSOR_FLAG_INPUT) || tensor->type != GGML_TYPE_F32) {
+ return false;
+ }
+ if (op->op == GGML_OP_MUL_MAT) {
+ return op->src[1] == tensor && op->src[0] != nullptr && op->src[0]->op == GGML_OP_NONE;
+ }
+ return op->op == GGML_OP_RMS_NORM && op->src[0] == tensor;
+ }
+
+ // true if any node of the current graph uses tensor as a raw embd input (see is_inp_embd_leaf)
+ bool has_inp_embd_leaf_use(const ggml_tensor * tensor) const;
+
static bool is_activation_input(const ggml_tensor * tensor, const ggml_tensor * op) {
if (tensor == nullptr || op == nullptr || tensor->type != GGML_TYPE_F32 || tensor->ne[2] != 1 ||
tensor->ne[3] != 1 || (tensor->buffer != nullptr && tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS)) {
@@ -578,11 +593,11 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
bool is_direct_gdn_view(const ggml_tensor * node) const;
- inline static bool is_cache(const ggml_tensor * tensor, const ggml_tensor * op) {
+ 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) {
+ static bool is_kvcache(const ggml_tensor * tensor, const ggml_tensor * op) {
if (tensor == nullptr || is_recurrent_cache(tensor)) {
return false;
}
@@ -590,27 +605,68 @@ class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder {
(op != nullptr && op->op == GGML_OP_SET_ROWS && op->src[2] == tensor);
}
- inline static bool is_conv_state_writeback(const ggml_tensor * node) {
+ 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;
}
+ // The final-state writeback of SSM_SCAN (K == 1): the state tail of the packed [y | state]
+ // output copied into a slot block of the recurrent cache.
+ static bool is_ssm_state_writeback(const ggml_tensor * node) {
+ if (!(node != nullptr && node->op == GGML_OP_CPY && node->view_src != nullptr && 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_SSM_SCAN && node->src[0]->src[0]->op_params[0] == 1 &&
+ node->src[1] != nullptr && node->src[1]->op == GGML_OP_VIEW &&
+ node->src[1]->view_src == node->view_src)) {
+ return false;
+ }
+ const auto * scan = node->src[0]->src[0];
+ const auto * src = node->src[0];
+ const auto * dst = node->src[1];
+ if (node->type != GGML_TYPE_F32 || src->type != GGML_TYPE_F32 || dst->type != GGML_TYPE_F32 ||
+ src->ne[2] != 1 || src->ne[3] != 1) {
+ return false;
+ }
+ for (int i = 0; i < GGML_MAX_DIMS; ++i) {
+ if (src->ne[i] != dst->ne[i]) {
+ return false;
+ }
+ }
+ return dst->nb[1] > 0 && dst->view_offs % dst->nb[1] == 0 &&
+ src->view_offs == (size_t) ggml_nelements(scan->src[1]) * ggml_type_size(scan->type);
+ }
+
// build_conv_state_store writes overlapping [history, window, channel, seq] windows into newest-first rollback planes.
// Match this layout separately from general SET_ROWS.
static bool is_conv_state_snapshot_writeback(const ggml_tensor * node);
+ // SET_ROWS into a column range of each row of a plain 2-D buffer, e.g. build_layer_inp_dev(): every enabled layer
+ // input goes to view_2d(layer_inp_dev, n_embd, n_batch, nb1, col * n_embd * elem) at the batch rows of the ubatch.
+ // The buffer sits in a regular device buffer, so is_kvcache() matches it: check this first
+ static bool is_column_set_rows(const ggml_tensor * node) {
+ if (node == nullptr || node->op != GGML_OP_SET_ROWS || node->src[2] == nullptr ||
+ node->src[2]->op != GGML_OP_VIEW) {
+ return false;
+ }
+ const ggml_tensor * dst = node->src[2];
+ const ggml_tensor * buf = dst->view_src;
+ return buf != nullptr && buf->op == GGML_OP_NONE && !is_recurrent_cache(buf) && buf->ne[2] == 1 &&
+ buf->ne[3] == 1 && dst->ne[2] == 1 && dst->ne[3] == 1 && dst->ne[0] < buf->ne[0] &&
+ dst->nb[0] == buf->nb[0] && dst->nb[1] == buf->nb[1] && dst->ne[1] == buf->ne[1];
+ }
+
// SET_ROWS into a KV cache, directly or through the [1, n] reshape of the transposed V cache with `-fa off`.
// Other SET_ROWS write rows of a compute tensor, e.g. the MoE expert-group mask
- inline static bool is_kvcache_write(const ggml_tensor * node) {
+ static bool is_kvcache_write(const ggml_tensor * node) {
const ggml_tensor * dst = node->src[2];
return dst != nullptr && is_kvcache(dst->view_src != nullptr ? dst->view_src : dst, nullptr);
}
// GET_ROWS batched over ggml dims 2 and 3, e.g. the MoE expert-group scores
// ggml_get_rows(ggml_reshape_4d(groups, 1, n_exp_per_group, n_groups, n_tokens), top_groups)
- inline static bool is_get_rows_batched_4d(const ggml_tensor * node) {
+ static bool is_get_rows_batched_4d(const ggml_tensor * node) {
return node->op == GGML_OP_GET_ROWS && !ggml_is_quantized(node->src[0]->type) &&
node->src[1]->ne[1] == node->src[0]->ne[2] && node->src[1]->ne[2] == node->src[0]->ne[3] &&
node->src[1]->ne[3] == 1;
diff --git a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp
index c76b423983f3..2537f8af3a31 100644
--- a/ggml/src/ggml-openvino/ggml-openvino-extra.cpp
+++ b/ggml/src/ggml-openvino/ggml-openvino-extra.cpp
@@ -616,6 +616,10 @@ bool ggml_openvino_is_npu() {
return ggml_openvino_get_device_config().is_npu;
}
+bool ggml_openvino_is_static() {
+ return ggml_openvino_is_npu() || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC");
+}
+
bool ggml_openvino_is_gpu() {
return has_prefix(ggml_openvino_get_device_name(), "GPU");
}
diff --git a/ggml/src/ggml-openvino/ggml-openvino-extra.h b/ggml/src/ggml-openvino/ggml-openvino-extra.h
index 5a64c67ef9c5..406ac6d4cb5a 100644
--- a/ggml/src/ggml-openvino/ggml-openvino-extra.h
+++ b/ggml/src/ggml-openvino/ggml-openvino-extra.h
@@ -134,6 +134,9 @@ bool ggml_openvino_release_weights_enabled();
// Check if running on NPU
bool ggml_openvino_is_npu();
+// Static graphs: NPU, or any device with GGML_OPENVINO_FORCE_STATIC
+bool ggml_openvino_is_static();
+
// KV-cache slicing: bind the KV cache, and size the KQ masks, at the attended length instead of the full context.
// One mechanism, two modes:
// dynamic graphs (CPU/GPU): the KV input is a view over the cache; the graph is not recompiled.
diff --git a/ggml/src/ggml-openvino/ggml-openvino.cpp b/ggml/src/ggml-openvino/ggml-openvino.cpp
index a7f5d177f527..bd49560d8e1e 100644
--- a/ggml/src/ggml-openvino/ggml-openvino.cpp
+++ b/ggml/src/ggml-openvino/ggml-openvino.cpp
@@ -1504,6 +1504,38 @@ static bool has_strides_on_element_grid(const ggml_tensor * t) {
return true;
}
+// A VIEW that regroups one contiguous span of every row of its contiguous source, e.g. the z/x/B/C
+// splits of the fused mamba projections. The strides do not nest on an element grid, but
+// process_view_input_new resolves it as a Slice of the source's innermost axis plus a Reshape.
+// Keep in sync with the matching branch there; dynamic execution only.
+static bool is_row_span_view(const ggml_tensor * t) {
+ if (ggml_openvino_is_static() || is_stateful_enabled()) {
+ return false;
+ }
+ const ggml_tensor * src = t->op == GGML_OP_VIEW ? t->src[0] : nullptr;
+ // a RESHAPE source is fine (the translation reads its value); a chained VIEW is not, because
+ // the view metadata then describes the intermediate view instead of the resolved input
+ if (src == nullptr || src->op == GGML_OP_VIEW || !ggml_is_contiguous(src) || t->type != src->type) {
+ return false;
+ }
+ const size_t es = ggml_type_size(t->type);
+ int d = 0;
+ size_t span = 1;
+ while (d < GGML_MAX_DIMS && t->nb[d] == span * es) {
+ span *= t->ne[d];
+ d++;
+ }
+ if (d < 2) {
+ return false;
+ }
+ for (int s = 1; d < GGML_MAX_DIMS; ++d, ++s) {
+ if (t->ne[d] != src->ne[s] || t->nb[d] != src->nb[s]) {
+ return false;
+ }
+ }
+ return t->view_offs % es == 0 && t->view_offs / es + span <= (size_t) src->ne[0];
+}
+
static bool has_non_contiguous_view_input(const ggml_tensor * op) {
for (int i = 0; i < GGML_MAX_SRC; i++) {
if (op->src[i] == nullptr) {
@@ -1624,7 +1656,7 @@ static bool cpy_output_view_is_supported(const ggml_tensor * op) {
}
return ggml_nbytes(op) == 0 || ggml_is_contiguous(op) || GgmlOvDecoder::is_conv_state_writeback(op) ||
- GgmlOvDecoder::is_gdn_snapshot_writeback(op);
+ GgmlOvDecoder::is_gdn_snapshot_writeback(op) || GgmlOvDecoder::is_ssm_state_writeback(op);
}
static bool mul_mat_id_requires_large_tmp(const ggml_tensor * op) {
@@ -1727,14 +1759,16 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
}
case GGML_OP_GET_ROWS:
case GGML_OP_SET_ROWS: {
- if (GgmlOvDecoder::is_conv_state_snapshot_writeback(op) &&
- (ggml_openvino_is_npu() || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC"))) {
+ if (GgmlOvDecoder::is_conv_state_snapshot_writeback(op) && ggml_openvino_is_static()) {
return {false, "convolution snapshots require dynamic execution"};
}
if (op->ne[3] != 1 && !GgmlOvDecoder::is_get_rows_batched_4d(op) &&
!GgmlOvDecoder::is_conv_state_snapshot_writeback(op)) {
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_SET_ROWS && op->src[0]->type == GGML_TYPE_BF16) {
+ return {false, "SET_ROWS with BF16 src0 is not supported"};
+ }
if (op->ne[0] == 256 && (op->src[0]->type == GGML_TYPE_Q4_K || op->src[0]->type == GGML_TYPE_Q5_K ||
op->src[0]->type == GGML_TYPE_Q4_1 || op->src[0]->type == GGML_TYPE_Q5_1)) {
// These are all f16-arithmetic dequant rounding errors that intermittently exceed the
@@ -2004,6 +2038,9 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
if (op->view_src != nullptr && !ggml_is_contiguous(op->src[0])) {
return {false, "ROPE on VIEW / non-contiguous input is not supported"};
}
+ if (op->type == GGML_TYPE_BF16) {
+ return {false, "ROPE with BF16 type is not supported"};
+ }
break;
}
case GGML_OP_TRANSPOSE: {
@@ -2034,7 +2071,7 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
reason = "GATED_DELTA_NET with kda (per-key-dimension gating) is not supported";
}
else if (op->op_params[0] > 1) {
- if (ggml_openvino_is_npu() || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC")) {
+ if (ggml_openvino_is_static()) {
reason = "GATED_DELTA_NET snapshots require dynamic execution";
}
}
@@ -2053,6 +2090,19 @@ static ggml_openvino_op_support is_op_supported_case(const ggml_tensor * op) {
// return true;
break;
}
+ case GGML_OP_SSM_SCAN: {
+ // SelectiveSSM implements the Mamba-2 recurrence: scalar decay per head, one final state
+ if (op->src[3]->ne[0] != 1) {
+ return {false, "SSM_SCAN with Mamba-1 state-wise decay is not supported"};
+ }
+ if (op->op_params[0] > 1) {
+ return {false, "SSM_SCAN with rollback snapshots (K > 1) is not supported"};
+ }
+ if (ggml_openvino_is_static()) {
+ return {false, "SSM_SCAN requires dynamic execution"};
+ }
+ break;
+ }
case GGML_OP_VIEW: {
// Skip TOPK_MOE fused tests until it is fully supported.
// The argsort_top_k VIEW wrapping ARGSORT is named "selected_experts" in test_topk_moe.
@@ -2274,7 +2324,8 @@ static ggml_openvino_op_support ggml_backend_openvino_device_supports_op_impl(gg
if (supported_types.find(src->type) == supported_types.end()) {
return {false, "src[" + std::to_string(i) + "] type " + std::string(ggml_type_name(src->type)) + " is not supported"};
}
- if (!has_strides_on_element_grid(src) && !(i == 1 && GgmlOvDecoder::is_gdn_snapshot_writeback(op)) &&
+ if (!has_strides_on_element_grid(src) && !is_row_span_view(src) &&
+ !(i == 1 && GgmlOvDecoder::is_gdn_snapshot_writeback(op)) &&
!(i == 2 && GgmlOvDecoder::is_conv_state_snapshot_writeback(op))) {
return {false, "src[" + std::to_string(i) + "] strides are not multiples of each other"};
}
diff --git a/ggml/src/ggml-openvino/model-cache.cpp b/ggml/src/ggml-openvino/model-cache.cpp
index 597233d7e6d9..7bfa7ad08b11 100644
--- a/ggml/src/ggml-openvino/model-cache.cpp
+++ b/ggml/src/ggml-openvino/model-cache.cpp
@@ -197,8 +197,7 @@ void ggml_openvino_model_cache_init() {
}
return;
}
- if (cache_only && (ggml_openvino_is_npu() || ggml_openvino_getenv_int("GGML_OPENVINO_FORCE_STATIC") ||
- ggml_openvino_getenv_int("GGML_OPENVINO_DISABLE_CACHE") ||
+ if (cache_only && (ggml_openvino_is_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");
}
@@ -368,7 +367,8 @@ uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
// Version 5 requantizes K-quant MoE experts to Q4_1_64 on GPU by default.
// Version 6 clears recurrent-cache slots with Select, so a NaN in a cleared slot does not survive.
// Version 7 reads ROPE frequency factors named other than rope_freqs.weight as a Constant, not a Parameter.
- h = fnv1a_u64(h, 7);
+ // Version 8 drops result_norm from the outputs when it has a consumer, and hashes the output flags.
+ h = fnv1a_u64(h, 8);
h = fnv1a(h, graph_signature.data(), graph_signature.size());
for (const char * name : cache_settings) {
const char * value = ggml_openvino_getenv_str(name, "");
@@ -391,6 +391,8 @@ uint64_t ggml_openvino_model_fingerprint(const ggml_cgraph * cgraph,
const ggml_tensor * node = cgraph->nodes[i];
h = fnv1a_u64(h, static_cast(node->op));
h = fnv1a(h, node->name, strlen(node->name));
+ // Each output is a Result of the model; llama_set_embeddings_layer_inp() changes only the flags.
+ h = fnv1a_u64(h, (node->flags & GGML_TENSOR_FLAG_OUTPUT) ? 1u : 0u);
has_gdn = has_gdn || node->op == GGML_OP_GATED_DELTA_NET;
}
if (has_gdn && ggml_openvino_is_npu()) {
diff --git a/ggml/src/ggml-openvino/openvino/op/cpy.cpp b/ggml/src/ggml-openvino/openvino/op/cpy.cpp
index 0bb6bd054d15..e0d63ac95179 100644
--- a/ggml/src/ggml-openvino/openvino/op/cpy.cpp
+++ b/ggml/src/ggml-openvino/openvino/op/cpy.cpp
@@ -75,6 +75,28 @@ OutputVector translate_cpy(const NodeContext & context) {
return rename_outputs_with_suffix({result}, context.get_name());
}
+ if (op_case == 13) {
+ // SSM_SCAN final-state writeback: output the written state block and the full updated cache
+ auto src = process_view_input_new(context, 0);
+ auto base = context.get_input(1);
+ if (src.get_element_type() != context.get_output_type()) {
+ src = std::make_shared(src, context.get_output_type());
+ }
+ auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
+ auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
+ auto int_max = ov::op::v0::Constant::create(ov::element::i64, {1}, {INT_MAX});
+ auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {2});
+ auto begin = context.get_input("rs_slot_begin_" + context.get_name());
+ auto src_len = std::make_shared(
+ std::make_shared(src, ov::element::i64), axis,
+ ov::op::v0::Constant::create(ov::element::i64, {}, {0}));
+ auto end = std::make_shared(begin, src_len);
+ auto head_part = std::make_shared(base, zero, begin, one, axis);
+ auto tail_part = std::make_shared(base, end, int_max, one, axis);
+ auto updated = std::make_shared(ov::OutputVector{head_part, src, tail_part}, 2);
+ return rename_outputs_with_suffix({src, updated}, context.get_name());
+ }
+
if (op_case == 4) {
auto src = process_view_input_new(context, 0);
auto base = context.get_input(1);
diff --git a/ggml/src/ggml-openvino/openvino/op/scale.cpp b/ggml/src/ggml-openvino/openvino/op/scale.cpp
index f77d8ce58a6e..e0d0ea182c19 100644
--- a/ggml/src/ggml-openvino/openvino/op/scale.cpp
+++ b/ggml/src/ggml-openvino/openvino/op/scale.cpp
@@ -4,6 +4,7 @@
#include
#include
+#include
#include
#include
@@ -26,15 +27,20 @@ OutputVector translate_scale(const NodeContext & context) {
return translate_recurrent_cache_reset(context);
}
- auto scaled = std::make_shared(process_view_input_new(context, 0), scale_node);
-
- std::shared_ptr res;
+ // Multiply and Add need matching types; compute f16 input in f32 and round once, as the CPU backend does.
+ auto input = process_view_input_new(context, 0);
+ const auto input_type = input.get_element_type();
+ if (input_type != ov::element::f32) {
+ input = std::make_shared(input, ov::element::f32);
+ }
+ ov::Output res = std::make_shared(input, scale_node);
if (bias != 0.0f) {
auto bias_node =
std::make_shared(ov::element::f32, ov::Shape{}, std::vector{bias});
- res = std::make_shared(scaled, bias_node);
- } else {
- res = scaled;
+ res = std::make_shared(res, bias_node);
+ }
+ if (input_type != ov::element::f32) {
+ res = std::make_shared(res, input_type);
}
return rename_outputs_with_suffix({res}, context.get_name());
diff --git a/ggml/src/ggml-openvino/openvino/op/selective_ssm.hpp b/ggml/src/ggml-openvino/openvino/op/selective_ssm.hpp
new file mode 100644
index 000000000000..509b43e0005f
--- /dev/null
+++ b/ggml/src/ggml-openvino/openvino/op/selective_ssm.hpp
@@ -0,0 +1,42 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+// Local mirror of OpenVINO's ov::op::internal::SelectiveSSM declaration
+// (src/core/dev_api/openvino/op/selective_ssm.hpp). The implementation comes from the linked
+// OpenVINO runtime; the class layout must stay in sync with the runtime's declaration.
+#pragma once
+
+#include "openvino/op/op.hpp"
+
+namespace ov::op::internal {
+/// Mamba2 selective state-space recurrence (arXiv:2405.21060).
+/// dA = exp(A * dt), dtB = dt * B; per token t:
+/// dBx_t = x_t (x) dtB_t, state_t = state_{t-1} * dA_t + dBx_t, y_t = sum(state_t * C_t).
+/// B and C are grouped and shared across heads: head h reads group h / (num_heads / num_groups).
+class OPENVINO_API SelectiveSSM : public ov::op::Op {
+public:
+ OPENVINO_OP("SelectiveSSM")
+
+ SelectiveSSM() = default;
+ /// \param A (Negative) log-decay rates per head [num_heads].
+ /// \param dt Per-token, per-head time steps [batch_size, seq_len, num_heads].
+ /// \param B Grouped input projection [batch_size, seq_len, num_groups, state_size].
+ /// \param x Input hidden states [batch_size, seq_len, num_heads, head_dim].
+ /// \param C Grouped output projection [batch_size, seq_len, num_groups, state_size].
+ /// \param recurrent_state Initial state [batch_size, num_heads, head_dim, state_size].
+ SelectiveSSM(const Output & A,
+ const Output & dt,
+ const Output & B,
+ const Output & x,
+ const Output & C,
+ const Output & recurrent_state);
+
+ /// \param args Input tensor vector in order: A, dt, B, x, C, recurrent_state.
+ explicit SelectiveSSM(const ov::OutputVector & args);
+
+ void validate_and_infer_types() override;
+ bool visit_attributes(AttributeVisitor & visitor) override;
+ std::shared_ptr clone_with_new_inputs(const ov::OutputVector & new_args) const override;
+};
+
+} // namespace ov::op::internal
diff --git a/ggml/src/ggml-openvino/openvino/op/set_rows.cpp b/ggml/src/ggml-openvino/openvino/op/set_rows.cpp
index 1eae9408b83e..476890911c04 100644
--- a/ggml/src/ggml-openvino/openvino/op/set_rows.cpp
+++ b/ggml/src/ggml-openvino/openvino/op/set_rows.cpp
@@ -90,12 +90,71 @@ static OutputVector translate_conv_snapshots(const NodeContext & context) {
return rename_outputs_with_suffix({result}, name);
}
+// SET_ROWS into a column range of a plain 2-D buffer (see GgmlOvDecoder::is_column_set_rows). The result is the
+// whole buffer: the indexed rows get the new columns, the other columns and rows keep their values, so the next
+// write into another column range of the same buffer reads this one.
+static OutputVector translate_column_set_rows(const NodeContext & context) {
+ using namespace ov::op;
+ auto i64_const = [](const std::vector & values) {
+ return v0::Constant::create(ov::element::i64, {values.size()}, values);
+ };
+
+ const auto view_shape = context.get_view_input_ggml_shape(2, 0); // [1, 1, rows, width]
+ FRONT_END_OP_CONVERSION_CHECK(view_shape.size() == 4, "SET_ROWS column write needs a 4-D destination view");
+ const int64_t width = (int64_t) view_shape[3];
+
+ auto base = context.get_input(2);
+ const auto base_shape = base.get_partial_shape();
+ FRONT_END_OP_CONVERSION_CHECK(base_shape.rank().is_static() && base_shape[base_shape.size() - 1].is_static(),
+ "SET_ROWS column write needs a static buffer row size");
+ const int64_t row_size = base_shape[base_shape.size() - 1].get_length();
+ const size_t elem_size = context.get_output_type().size();
+ const int64_t col_begin = (int64_t) (context.get_view_input_offset(2, 0) / elem_size) % row_size;
+ const int64_t col_end = col_begin + width;
+ FRONT_END_OP_CONVERSION_CHECK(col_end <= row_size, "SET_ROWS column range is out of bounds");
+
+ Output data = std::make_shared(process_view_input_new(context, 0), i64_const({1, 1, -1, width}),
+ false);
+ if (data.get_element_type() != context.get_output_type()) {
+ data = std::make_shared(data, context.get_output_type());
+ }
+ Output rows = std::make_shared(process_view_input_new(context, 1), i64_const({-1}), false);
+ if (rows.get_element_type() != ov::element::i64) {
+ rows = std::make_shared(rows, ov::element::i64);
+ }
+
+ auto axis = v0::Constant::create(ov::element::i64, {}, {2});
+ auto buffer = std::make_shared(base, i64_const({1, 1, -1, row_size}), false);
+ Output updates = data;
+ if (width < row_size) {
+ auto existing = std::make_shared(buffer, rows, axis);
+ auto col_axis = i64_const({3});
+ OutputVector parts;
+ if (col_begin > 0) {
+ parts.push_back(std::make_shared(existing, i64_const({0}), i64_const({col_begin}),
+ i64_const({1}), col_axis));
+ }
+ parts.push_back(data);
+ if (col_end < row_size) {
+ parts.push_back(std::make_shared(existing, i64_const({col_end}), i64_const({row_size}),
+ i64_const({1}), col_axis));
+ }
+ updates = std::make_shared(parts, 3);
+ }
+ auto updated = std::make_shared(buffer, rows, updates, axis);
+ auto res = std::make_shared(updated, std::make_shared(base, ov::element::i64), false);
+ return rename_outputs_with_suffix({res}, context.get_name());
+}
+
OutputVector translate_set_rows(const NodeContext & context) {
num_inputs_check(context, 3, 3);
if (context.get_op_case() == 3) {
return translate_conv_snapshots(context);
}
+ if (context.get_op_case() == 4) {
+ return translate_column_set_rows(context);
+ }
auto data = process_view_input_new(context, 0);
auto indices = process_view_input_new(context, 1);
diff --git a/ggml/src/ggml-openvino/openvino/op/ssm_scan.cpp b/ggml/src/ggml-openvino/openvino/op/ssm_scan.cpp
new file mode 100644
index 000000000000..fce8e111ed42
--- /dev/null
+++ b/ggml/src/ggml-openvino/openvino/op/ssm_scan.cpp
@@ -0,0 +1,63 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+// Adapted from OpenVINO's GGUF frontend (src/frontends/gguf/src/op/ssm_scan.cpp).
+
+#include "../node_context.h"
+#include "../op_table.h"
+#include "../utils.h"
+#include "selective_ssm.hpp"
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+namespace ov {
+namespace frontend {
+namespace ggml {
+namespace op {
+
+OutputVector translate_ssm_scan(const NodeContext & context) {
+ num_inputs_check(context, 7, 7);
+ using namespace ov::op;
+
+ // ggml: s [slots,H,D,S], x [B,T,H,D], dt [1,B,T,H], A [1,1,H,1],
+ // B/C [B,T,G,S], ids [1,1,1,B]. Only Mamba2 has scalar decay per head.
+ const auto a_shape = context.get_input_shape(3);
+ FRONT_END_OP_CONVERSION_CHECK(a_shape.rank() == 4 && a_shape[0] == 1 && a_shape[1] == 1 && a_shape[3] == 1,
+ "SSM_SCAN: SelectiveSSM requires Mamba2 scalar decay per head; "
+ "Mamba1 state-wise decay is not supported");
+ const auto x = process_view_input_new(context, 1);
+ const auto flat = v0::Constant::create(element::i64, {1}, {-1});
+ const auto axis0 = v0::Constant::create(element::i64, {}, {0});
+ const auto ids = std::make_shared(process_view_input_new(context, 6), flat, false);
+ const auto state = std::make_shared(process_view_input_new(context, 0), ids, axis0);
+ const auto a = std::make_shared(process_view_input_new(context, 3), flat, false);
+ const auto dt_shape = std::make_shared(std::make_shared(x),
+ v0::Constant::create(element::i64, {3}, {0, 1, 2}),
+ axis0);
+ const auto dt =
+ std::make_shared(std::make_shared(process_view_input_new(context, 2), dt_shape, false));
+ const auto ssm = std::make_shared(a,
+ dt,
+ process_view_input_new(context, 4),
+ x,
+ process_view_input_new(context, 5),
+ state);
+ // Pack directly in GGML layout so whole-part views can select either Concat input.
+ const auto packed_shape = v0::Constant::create(element::i64, {4}, {1, 1, 1, -1});
+ const auto y = std::make_shared(ssm->output(0), packed_shape, false);
+ const auto s = std::make_shared(ssm->output(1), packed_shape, false);
+ const auto result = std::make_shared(OutputVector{y, s}, 3);
+ result->get_rt_info()[GGML_OV_SSM_SCAN_PACKED] = true;
+ return rename_outputs_with_suffix({result}, context.get_name());
+}
+
+} // namespace op
+} // namespace ggml
+} // namespace frontend
+} // namespace ov
+
diff --git a/ggml/src/ggml-openvino/openvino/op/tri.cpp b/ggml/src/ggml-openvino/openvino/op/tri.cpp
index 9b7774a383ec..ba2efdc0bf1f 100644
--- a/ggml/src/ggml-openvino/openvino/op/tri.cpp
+++ b/ggml/src/ggml-openvino/openvino/op/tri.cpp
@@ -70,7 +70,8 @@ OutputVector translate_tri(const NodeContext & context) {
throw std::runtime_error("translate_tri: invalid tri_type " + std::to_string(tri_type));
}
- auto zero = ov::op::v0::Constant::create(ov::element::f32, {}, {0.0f});
+ // Select needs the zero in the input's type (f32, f16 or bf16).
+ auto zero = ov::op::v0::Constant::create(x.get_element_type(), {}, {0.0f});
auto res = std::make_shared(mask, x, zero);
return rename_outputs_with_suffix({res}, context.get_name());
diff --git a/ggml/src/ggml-openvino/openvino/op_table.cpp b/ggml/src/ggml-openvino/openvino/op_table.cpp
index 399c17ac0162..defc428d1d4c 100644
--- a/ggml/src/ggml-openvino/openvino/op_table.cpp
+++ b/ggml/src/ggml-openvino/openvino/op_table.cpp
@@ -98,6 +98,7 @@ std::unordered_map get_supported_ops() {
{"GGML_OP_PAD", op::translate_pad },
{"GGML_OP_TOP_K", op::translate_top_k },
{"GGML_OP_SSM_CONV", op::translate_ssm_conv },
+ {"GGML_OP_SSM_SCAN", op::translate_ssm_scan },
{"GGML_OP_GATED_DELTA_NET", op::translate_gated_delta_net },
{"GGML_OP_REPEAT", op::translate_repeat },
{"GGML_OP_CUMSUM", op::translate_cumsum },
diff --git a/ggml/src/ggml-openvino/openvino/op_table.h b/ggml/src/ggml-openvino/openvino/op_table.h
index 206966a935ff..54c60c634bf6 100644
--- a/ggml/src/ggml-openvino/openvino/op_table.h
+++ b/ggml/src/ggml-openvino/openvino/op_table.h
@@ -49,6 +49,7 @@ GGML_OP_CONVERTER(translate_flash_attn_ext);
GGML_OP_CONVERTER(translate_clamp);
GGML_OP_CONVERTER(translate_pad);
GGML_OP_CONVERTER(translate_ssm_conv);
+GGML_OP_CONVERTER(translate_ssm_scan);
GGML_OP_CONVERTER(translate_gated_delta_net);
#ifdef GGML_OPENVINO_HAS_CACHE_UPDATE
OutputVector translate_recurrent_gated_delta_net(const NodeContext & context, const OutputVector & cache_inputs);
diff --git a/ggml/src/ggml-openvino/openvino/utils.cpp b/ggml/src/ggml-openvino/openvino/utils.cpp
index ec7513951c01..e82a6c92fc47 100644
--- a/ggml/src/ggml-openvino/openvino/utils.cpp
+++ b/ggml/src/ggml-openvino/openvino/utils.cpp
@@ -465,6 +465,25 @@ ov::Output process_view_input_new(const NodeContext & context, int inp
return input;
}
+ // A VIEW over the packed SSM_SCAN output selects one whole Concat part: y at offset 0, the
+ // final state after it. The y length is token-dependent, so a static Slice of the flat
+ // tensor would bake in the captured token count; return the native part instead.
+ if (view_input_size == 1 && input.get_node_shared_ptr()->get_rt_info().count(GGML_OV_SSM_SCAN_PACKED)) {
+ const bool is_y = context.get_view_input_offset(input_index, 0) == 0;
+ auto part = input.get_node_shared_ptr()->input_value(is_y ? 0 : 1).get_node_shared_ptr()->input_value(0);
+ if (is_y) {
+ // y keeps its native [n_seqs, n_tokens, n_head, head_dim] layout
+ return part;
+ }
+ // state [n_seqs, n_head, head_dim, d_state] -> view layout [1, 1, n_seqs, d_state * d_inner]
+ auto n_seqs = get_dimensions(std::make_shared(part, ov::element::i64), {0});
+ auto target = std::make_shared(
+ ov::OutputVector{ov::op::v0::Constant::create(ov::element::i64, {2}, {1, 1}), n_seqs,
+ ov::op::v0::Constant::create(ov::element::i64, {1}, {-1})},
+ 0);
+ return std::make_shared(part, target, false);
+ }
+
// Integer router views can share these strides when their row count is zero.
// Packed projections can store query and gate in separate halves of each head.
if (!context.is_static() && view_input_size == 1 && input.get_element_type().is_real()) {
@@ -534,6 +553,70 @@ ov::Output