Skip to content

[Engine] Metal: fit Inkling 2's mix kernels to the GPU's threadgroup limit - #359

Open
HugoLopes45 wants to merge 1 commit into
simplexai-labs:mainfrom
HugoLopes45:fix/metal-mix-threadgroup-limit
Open

HugoLopes45 wants to merge 1 commit into
simplexai-labs:mainfrom
HugoLopes45:fix/metal-mix-threadgroup-limit

Conversation

@HugoLopes45

Copy link
Copy Markdown

Problem and result

On an Apple M1 Max (64 GB, macOS 27.2.1), Inkling 2 Mini UD-Q3_K_XL loads, but the first chat request stops the engine:

strata metal: the kernel poly_inkling::ink_conv_mix_rows_3_4 can run at most 832 threads per threadgroup on this GPU (Apple M1 Max), but the launch asks for 1024.

The server then returns the engine stopped unexpectedly (exit code 6) for every request. The four Inkling 2 mix launches (ink_mix, ink_conv_mix, ink_conv_mix_rows, ink_moe_conv_mix) always ask for 1,024 threads, and this pipeline gets 832 on the M1 Max. After the change, the same model answers on the M1 Max: greedy code and tool-call requests complete with correct output.

Related discussion

Same class of bug as Niko1221/Strata#2323 (a pipeline limit below the launch size, caught by its guard), here for the Inkling 2 mix kernels. No existing issue or PR found for these kernels.

Approach

The mix kernels stride by the threadgroup width (i = tid + h * nt, at least 3 slots a thread up to 3,072 values, 4 above) and reduce over nt / 32 SIMD groups, so they compute the same mix with a narrower threadgroup as long as it covers the row (Inkling 2 Mini: 3 x 832 = 2,496 >= 2,304). mix_threads() launches with the pipeline's limit rounded down to whole SIMD groups, capped at 1,024, and throws when the limit cannot cover the row instead of computing a partial row. A GPU whose pipeline allows 1,024 threads launches exactly as before. The limit comes from pipe_max_threads(), as the attention dispatch in the same file already does.

On the M1 Max the reductions run over 26 SIMD groups instead of 32, so the floating-point order of the sums differs from a 1,024-thread GPU; the parity test below bounds the difference against the CPU reference.

Validation

Passed:

  • Apple M1 Max 32-core GPU, 64 GB, macOS 27.2.1, base d62dfac (= main for engine/metal/src/poly/inkling.cpp), engine/metal built with STRATA_METAL_POLY=ON, Release.
  • poly_inkling_parity with the new conv_mixes cases (ink_conv_mix and ink_conv_mix_rows, width 2,304, 1 and 3 queries, 1 and 4 rows): all passed, 0 mismatches, max error 4.8e-7 (norm) and 2.4e-7 (mix outputs).
  • Control: the same test on unpatched d62dfac stops at ink_conv_mix_rows with 3 queries, exit 6, with the message above.
  • ctest -L poly: 48 of 49 passed; the failure is listed below.
  • End to end: ./setup.sh --family inkling --model UD-Q3_K_XL --context 131072 (DSpark draft on, --metal-optimize --metal-memory balanced), then /v1/chat/completions at temperature 0: an is_prime function that is correct for 0-29 when executed, and a get_weather tool call with {"city": "Lisbon"} (finish_reason: tool_calls).

Failed:

  • poly_runtime_failure (strata metal: command buffer failed: injected test failure). It fails the same way on unpatched d62dfac on this Mac, so it is not caused by this change.

Not run:

  • A GPU whose pipelines allow 1,024 threads for these kernels: the launch size there is unchanged by construction (min(1024, limit)), but I did not run one.
  • ink_moe_conv_mix and ink_mix have no new test; they go through the same mix_threads(). ink_mix keeps its existing parity case, which passes.
  • CUDA, HIP and SYCL: no file of theirs is touched.
  • Speed: no performance claim. A narrower threadgroup may cost some decode speed on the M1 Max, which previously did not run at all.

Limits

These four were the only launches in engine/metal/src/poly/inkling.cpp with a fixed 1,024 threads; the others already size by pipe_max_threads() or ask for 256 or fewer. Metal kernels outside Inkling 2 were not reviewed.

…limit

On an Apple M1 Max the ink_conv_mix_rows_3_4 pipeline allows at most 832 threads per threadgroup, but the mix kernels were always launched with 1,024, so the first request stopped the engine (exit 6, the #2323 guard). The kernels stride by the threadgroup width and reduce over its SIMD groups, so they compute the same mix with fewer threads as long as those cover the row. Launch with the pipeline's limit, rounded down to whole SIMD groups, and refuse when it cannot cover the row. A GPU that allows 1,024 threads launches as before.

inkling_parity now checks ink_conv_mix and ink_conv_mix_rows at Inkling 2 Mini's width (2,304) with 1 and 3 queries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant