[PR #4961] [Engine] Metal batch slots: each slot rotates by its own M-RoPE table (pictures beside other requests) #5266

Open
opened 2026-11-07 16:33:55 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4961
Author: @oceanio
Created: 11/1/2026
Status: 🔄 Open

Base: main ← Head: fix/metal-slot-mrope


📝 Commits (1)

  • 0aefdce metal: pictures in batch slots, each slot rotates by its own M-RoPE table

📊 Changes

2 files changed (+49 additions, -10 deletions)

View changed files

📝 metal/kernels/qsa.metal (+11 -6)
📝 metal/src/engine/window.cpp (+38 -4)

📄 Description

Problem and result

With --vision and "parallel": 2 on the Mac engine (0.2.9 and 0.2.10), a picture request decodes other text once another request joins it in the batch slots (#4908). The batch rows' qsa_prep is built with the rows constant (30) but not the M-RoPE one (24), so every slot row rotates by its plain cell position. After a picture, the text sits at fewer M-RoPE positions than it has cells, so it is rotated as if it came later. The slot-by-slot path (STRATA_METAL_BATCH_ROWS=0) reads the process's table instead, which the last request read in rebuilds. On the rows path text requests are not affected, because their table is the identity. On the slot-by-slot path a text request beside a picture can be affected too, because it reads the picture's table (T2 beside I2 on 0.2.10, below).

With this PR each slot rotates by its own table, and every request of a pair decodes the same text as alone. Greedy, through 0.2.10's serve/server.py, up to 400 tokens each, the second request 1 s after the first (rows path):

Pair 0.2.10 this PR
text + text (T1, T2) both as alone both as alone
picture + text (I1, T1) I1 differs both as alone
text + picture (T2, I2) I2 differs both as alone
picture + picture (I1, I2) both differ both as alone
picture + picture (I3, I1) both as alone (I3 was done before I1 started) both as alone

Fixes #4908 (bug report, with the cause above). No maintainer reply yet.

Approach

Only metal/src/engine/window.cpp and metal/kernels/qsa.metal:

  • Each slot keeps its own M-RoPE table (int32 [cells][3]). The tables share one buffer beside the slots' other per-set state (mropeBig_, set idx at idx * st_mrope_), allocated in slots_init only when the table is in use (--vision).
  • slot_copy into a slot fills the slot's table from the admitting request's table: cells [0, upto) as they are (the K/V rows already hold them rotated), then one position per cell after the last one. A copy whose last cell is part of an image is refused with an error. A chat prompt ends in the assistant turn's text, so serve's requests never end in one.
  • A batch row reads its own slot's table: qsa_prep#rows#mrope (constants 30 and 24), with the row's offset in rinfo_'s fourth word, which was 0 until now. In the kernel, every rotation in qsa_prep (Q, K, and the indexer's queries and keys) takes the row's table.
  • On the slot-by-slot path, mrope_sync copies from the active slot's table instead of the process's.
  • The solo window keeps reading the process's table. Without --vision nothing changes: no new buffer, the same pipelines, and the row word stays 0.

Cost with --vision and --batch N: (N + 1) tables of 3.1 MB each at 262K context, so 9.4 MB with 2 slots.

Validation

Environment: Apple M5 Max (128 GB), macOS 26.7.1. Revision: this branch, 0aefdce7 = 0.2.10 (ffa80d73) + 1 commit, engine 0.1.56-metal. Rebased from 0.2.9 with one conflict, in window.cpp's activation buffer size (0.2.10 adds room for its fp16 decode); the resolution keeps both, and the diff is otherwise the same lines. Model: Swift 1.5 IQ3_S (SC117's abliterated build, the same file layout as UkisAI's), repacked by setup, Swift's projector, --max-context 262144 --spec 2 with MTP, "parallel": 2. Requests go through this tree's serve/server.py on a private port: temperature 0, up to 400 tokens, three synthetic pictures (6 shapes 640×400, a bar chart 640×400, an invoice 800×300). The 0.2.10 column comes from 0.2.10's own build with the same requests, in the same round.

Passed:

  • Metal build: 0 errors, the same 16 warnings as 0.2.10.
  • --vision, batch rows: each request of the 5 pairs above gives its text alone (10 of 10; 0.2.10: 6 of 10). The engine stays the same process, with 10 slot admissions in the log.
  • --vision with STRATA_METAL_BATCH_ROWS=0 (slot by slot): the same 10 of 10 (0.2.10: 8 of 10, I1 beside T1 and T2 beside I2 differ), and each request alone gives the same text as on the rows path.
  • Without --vision: two texts side by side give their text alone. T1 and T2 give the same text with and without --vision.
  • Alone, every request gives the same text as on 0.2.10 (the solo path is unchanged): T1 and T2 with and without --vision; I1, I2 and I3 with it.
  • The same checks on 0.2.9 (25eedc11, before the rebase) gave the same results: 10 of 10 on both paths, 0.2.9 itself 6 of 10 on the rows path.

Failed:
None in the checks above. Separately, on 0.2.9 and 0.2.10 two requests that decode side by side in the slots for more than 60 s are stopped by the serve watchdog, with or without pictures and with or without this PR. #4960 fixes that.

Not run:

  • CUDA, HIP and SYCL: the change is under metal/ only, which only the Metal build compiles.
  • More than 2 slots, other pictures and sizes, video, Qwen3.8-Flash-Next with pictures (only Swift here), other Macs.
  • STRATA_METAL_F16=1: 0.2.10's fp16 decode is used by the solo window only (f16_enc_ needs !rows_), so with it a request in a slot is not expected to give its solo text, pictures or not. All checks above run with the default (off).

Limits

  • A slot's table is filled when a request is copied into the slot. A copy that ends inside an image is refused with an error (a slot copy that ends in an image) instead of guessing the positions after it.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4961 **Author:** [@oceanio](https://github.com/oceanio) **Created:** 11/1/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/metal-slot-mrope` --- ### 📝 Commits (1) - [`0aefdce`](https://github.com/Niko1221/Strata/commit/0aefdce7ba069de27f11e3da6341783aa509eee1) metal: pictures in batch slots, each slot rotates by its own M-RoPE table ### 📊 Changes **2 files changed** (+49 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `metal/kernels/qsa.metal` (+11 -6) 📝 `metal/src/engine/window.cpp` (+38 -4) </details> ### 📄 Description ## Problem and result With `--vision` and `"parallel": 2` on the Mac engine (0.2.9 and 0.2.10), a picture request decodes other text once another request joins it in the batch slots (#4908). The batch rows' `qsa_prep` is built with the rows constant (30) but not the M-RoPE one (24), so every slot row rotates by its plain cell position. After a picture, the text sits at fewer M-RoPE positions than it has cells, so it is rotated as if it came later. The slot-by-slot path (`STRATA_METAL_BATCH_ROWS=0`) reads the process's table instead, which the last request read in rebuilds. On the rows path text requests are not affected, because their table is the identity. On the slot-by-slot path a text request beside a picture can be affected too, because it reads the picture's table (T2 beside I2 on 0.2.10, below). With this PR each slot rotates by its own table, and every request of a pair decodes the same text as alone. Greedy, through 0.2.10's `serve/server.py`, up to 400 tokens each, the second request 1 s after the first (rows path): | Pair | 0.2.10 | this PR | |---|---|---| | text + text (T1, T2) | both as alone | both as alone | | picture + text (I1, T1) | I1 differs | both as alone | | text + picture (T2, I2) | I2 differs | both as alone | | picture + picture (I1, I2) | both differ | both as alone | | picture + picture (I3, I1) | both as alone (I3 was done before I1 started) | both as alone | ## Related discussion Fixes #4908 (bug report, with the cause above). No maintainer reply yet. ## Approach Only `metal/src/engine/window.cpp` and `metal/kernels/qsa.metal`: - Each slot keeps its own M-RoPE table (int32 [cells][3]). The tables share one buffer beside the slots' other per-set state (`mropeBig_`, set `idx` at `idx * st_mrope_`), allocated in `slots_init` only when the table is in use (`--vision`). - `slot_copy` into a slot fills the slot's table from the admitting request's table: cells `[0, upto)` as they are (the K/V rows already hold them rotated), then one position per cell after the last one. A copy whose last cell is part of an image is refused with an error. A chat prompt ends in the assistant turn's text, so serve's requests never end in one. - A batch row reads its own slot's table: `qsa_prep#rows#mrope` (constants 30 and 24), with the row's offset in `rinfo_`'s fourth word, which was 0 until now. In the kernel, every rotation in `qsa_prep` (Q, K, and the indexer's queries and keys) takes the row's table. - On the slot-by-slot path, `mrope_sync` copies from the active slot's table instead of the process's. - The solo window keeps reading the process's table. Without `--vision` nothing changes: no new buffer, the same pipelines, and the row word stays 0. Cost with `--vision` and `--batch N`: (N + 1) tables of 3.1 MB each at 262K context, so 9.4 MB with 2 slots. ## Validation Environment: Apple M5 Max (128 GB), macOS 26.7.1. Revision: this branch, `0aefdce7` = 0.2.10 (`ffa80d73`) + 1 commit, engine 0.1.56-metal. Rebased from 0.2.9 with one conflict, in `window.cpp`'s activation buffer size (0.2.10 adds room for its fp16 decode); the resolution keeps both, and the diff is otherwise the same lines. Model: Swift 1.5 IQ3_S (SC117's abliterated build, the same file layout as UkisAI's), repacked by setup, Swift's projector, `--max-context 262144 --spec 2` with MTP, `"parallel": 2`. Requests go through this tree's `serve/server.py` on a private port: temperature 0, up to 400 tokens, three synthetic pictures (6 shapes 640×400, a bar chart 640×400, an invoice 800×300). The 0.2.10 column comes from 0.2.10's own build with the same requests, in the same round. **Passed:** - Metal build: 0 errors, the same 16 warnings as 0.2.10. - `--vision`, batch rows: each request of the 5 pairs above gives its text alone (10 of 10; 0.2.10: 6 of 10). The engine stays the same process, with 10 slot admissions in the log. - `--vision` with `STRATA_METAL_BATCH_ROWS=0` (slot by slot): the same 10 of 10 (0.2.10: 8 of 10, I1 beside T1 and T2 beside I2 differ), and each request alone gives the same text as on the rows path. - Without `--vision`: two texts side by side give their text alone. T1 and T2 give the same text with and without `--vision`. - Alone, every request gives the same text as on 0.2.10 (the solo path is unchanged): T1 and T2 with and without `--vision`; I1, I2 and I3 with it. - The same checks on 0.2.9 (`25eedc11`, before the rebase) gave the same results: 10 of 10 on both paths, 0.2.9 itself 6 of 10 on the rows path. **Failed:** None in the checks above. Separately, on 0.2.9 and 0.2.10 two requests that decode side by side in the slots for more than 60 s are stopped by the serve watchdog, with or without pictures and with or without this PR. #4960 fixes that. **Not run:** - CUDA, HIP and SYCL: the change is under `metal/` only, which only the Metal build compiles. - More than 2 slots, other pictures and sizes, video, Qwen3.8-Flash-Next with pictures (only Swift here), other Macs. - `STRATA_METAL_F16=1`: 0.2.10's fp16 decode is used by the solo window only (`f16_enc_` needs `!rows_`), so with it a request in a slot is not expected to give its solo text, pictures or not. All checks above run with the default (off). ## Limits - A slot's table is filled when a request is copied into the slot. A copy that ends inside an image is refused with an error (`a slot copy that ends in an image`) instead of guessing the positions after it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5266
No description provided.