Thread swizzling in kernel_mul_mm (Metal) for better cache locality. - #27529
Draft
skoulik wants to merge 2 commits into
Draft
Thread swizzling in kernel_mul_mm (Metal) for better cache locality.#27529skoulik wants to merge 2 commits into
skoulik wants to merge 2 commits into
Conversation
Avoids throughput collapse after hitting the SLC limit.
|
Hi @skoulik, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This adds swizzling trick into the Metal backend's multiplication kernels to avoid hitting the cache size performance cliff on large inputs. Gains 5.5% - 22.5% t/s for Qwen 3.8 27B on Apple M1 Max (depending on ubatch).
Additional information
./llama-bench -m ~./models/qwen3.8-27b/Qwen3.8-27B-Q8_0.gguf -p 8980 -n 0 -ngl 99 -fa on -b 2048 -ub 512,1024,2048 -r 3
ggml_metal_device_init: tensor API disabled for pre-M5 and pre-A19 devices
ggml_metal_library_init: using embedded metal library
ggml_metal_library_init: loaded in 0.014 sec
ggml_metal_rsets_init: creating a residency set collection (keep_alive = 180 s)
ggml_metal_device_init: GPU name: MTL0 (Apple M1 Max)
ggml_metal_device_init: GPU family: MTLGPUFamilyApple7 (1007)
ggml_metal_device_init: GPU family: MTLGPUFamilyCommon3 (3003)
ggml_metal_device_init: GPU family: MTLGPUFamilyMetal4 (5002)
ggml_metal_device_init: simdgroup reduction = true
ggml_metal_device_init: simdgroup matrix mul. = true
ggml_metal_device_init: has unified memory = true
ggml_metal_device_init: has bfloat = true
ggml_metal_device_init: has tensor = false
ggml_metal_device_init: use residency sets = true
ggml_metal_device_init: use shared buffers = true
ggml_metal_device_init: recommendedMaxWorkingSetSize = 55662.79 MB
Before:
After:
ctest --output-on-failure -R test-backend-ops
Test project /Users/sergeikulik/CODE/llama.cpp/build/swz
Start 43: test-backend-ops
1/1 Test #43: test-backend-ops ................. Passed 165.43 sec
Perplexity byte-identical on wikitext-2-raw/wiki.test.raw
Final estimate: PPL = 6.7478 +/- 0.10325
Requirements
YES
Used Claude Opus 5 to debug the inefficiency and suggest the patch.