forked from shakfu/cyllama
-
Notifications
You must be signed in to change notification settings - Fork 14
BUG: Fix missing bf16 Metal kernels on Apple Silicon #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4dfa7f8
Implement patch application and reversion for llama.cpp
codingl2k1 593d675
Pin MSL version for Metal shader compilation
codingl2k1 5da2d54
Enhance revert_llamacpp_patches to update file mtime
codingl2k1 ab91cb7
Add unit tests for llama.cpp patching functionality
codingl2k1 43bd465
Add regression tests for Metal bf16 kernel support
codingl2k1 6c43f04
Pin MSL version for Metal shader compilation
codingl2k1 f7d01bf
Fix patch application handling in build script
codingl2k1 2d497b5
Enhance Metal bf16 tests for Apple Silicon support
codingl2k1 eec2e56
Update tests/test_metal_bf16.py
codingl2k1 43ad564
Improve error handling during patch application
codingl2k1 9ec7505
Pin MSL version for Metal shader compilation
codingl2k1 635e5c3
Pin MSL version for shader compilation
codingl2k1 c1a1129
Enhance CI workflow with crash report dumping
codingl2k1 8671494
Increase verbosity level in test_metal_bf16.py
codingl2k1 2cd7014
Revise Metal bf16 tests for Apple Silicon support
codingl2k1 e0ed1c7
Merge remote-tracking branch 'origin/main' into fix_bf16_on_m5_issue
codingl2k1 975804a
Update MSL version handling in ggml-metal-device.m
codingl2k1 4cca62a
Delete patches/llama.cpp/0001-metal-pin-msl-language-version.patch
codingl2k1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
patches/llama.cpp/0001-metal-pin-msl-language-version.patch
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| diff --git a/ggml/src/ggml-metal/ggml-metal-device.m b/ggml/src/ggml-metal/ggml-metal-device.m | ||
| index 80e47f2c2..c7831c3a2 100644 | ||
| --- a/ggml/src/ggml-metal/ggml-metal-device.m | ||
| +++ b/ggml/src/ggml-metal/ggml-metal-device.m | ||
| @@ -103,6 +103,44 @@ struct ggml_metal_library { | ||
| NSLock * lock; | ||
| }; | ||
|
|
||
| +// Pin the Metal Shading Language (MSL) version used for runtime shader compilation. | ||
| +// | ||
| +// When the language version is not set explicitly, the Metal compiler derives a | ||
| +// default from the LC_BUILD_VERSION of the host executable (e.g. the python | ||
| +// interpreter that loaded the library). Host executables built against old SDKs | ||
| +// get a default older than MSL 3.1, in which case the shaders silently drop the | ||
| +// bf16 kernels through the __METAL_VERSION__ < 310 guard in ggml-metal.metal, | ||
| +// while the device still reports bfloat support. This results in runtime | ||
| +// failures such as: | ||
| +// | ||
| +// ggml_metal_library_compile_pipeline: ... "Function kernel_mul_mv_ext_bf16_f32_r1_2 | ||
| +// was not found in the library" | ||
| +// | ||
| +// ref: https://github.com/ggml-org/llama.cpp/issues/21381 | ||
| +// ref: https://github.com/hybridgroup/yzma/issues/226 | ||
| +static void ggml_metal_compile_options_set_language_version(ggml_metal_device_t dev, MTLCompileOptions * options) { | ||
| + if (ggml_metal_device_get_props(dev)->has_bfloat) { | ||
| + // bfloat requires MSL 3.1+ | ||
| + if (@available(macOS 14.0, iOS 17.0, *)) { | ||
| + if (options.languageVersion < MTLLanguageVersion3_1) { | ||
| + options.languageVersion = MTLLanguageVersion3_1; | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + if (ggml_metal_device_get_props(dev)->has_tensor) { | ||
| + // the tensor API requires MSL 4.0+ | ||
| + // note: MTLLanguageVersion4_0 requires the macOS 26 SDK, so use the raw | ||
| + // value (major << 16 | minor) to stay compatible with older Xcode | ||
| + if (@available(macOS 26.0, iOS 26.0, *)) { | ||
| + const MTLLanguageVersion msl_4_0 = (MTLLanguageVersion) (4 << 16); | ||
| + if (options.languageVersion < msl_4_0) { | ||
| + options.languageVersion = msl_4_0; | ||
| + } | ||
| + } | ||
| + } | ||
| +} | ||
| + | ||
| ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) { | ||
| id<MTLLibrary> library = nil; | ||
| id<MTLDevice> device = ggml_metal_device_get_obj(dev); | ||
| @@ -228,6 +266,8 @@ ggml_metal_library_t ggml_metal_library_init(ggml_metal_device_t dev) { | ||
| MTLCompileOptions * options = [MTLCompileOptions new]; | ||
| options.preprocessorMacros = prep; | ||
|
|
||
| + ggml_metal_compile_options_set_language_version(dev, options); | ||
| + | ||
| //[options setFastMathEnabled:false]; | ||
|
|
||
| library = [device newLibraryWithSource:src options:options error:&error]; | ||
| @@ -285,6 +325,8 @@ ggml_metal_library_t ggml_metal_library_init_from_source(ggml_metal_device_t dev | ||
| MTLCompileOptions * options = [MTLCompileOptions new]; | ||
| options.preprocessorMacros = prep; | ||
|
|
||
| + ggml_metal_compile_options_set_language_version(dev, options); | ||
| + | ||
| library = [device newLibraryWithSource:src options:options error:&error]; | ||
| if (error) { | ||
| if (verbose) { | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """Unit tests for the build-time llama.cpp patch machinery in scripts/build.py. | ||
|
|
||
| The wheel build applies hotfix patches from patches/llama.cpp/*.patch to the | ||
| vendored submodule before the CMake build and reverts them right after, so | ||
| the submodule working tree stays pristine. These tests exercise that logic | ||
| against a throwaway git repository instead of the real submodule. | ||
| """ | ||
|
|
||
| import importlib.util | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| ROOT = Path(__file__).parent.parent | ||
|
|
||
| spec = importlib.util.spec_from_file_location( | ||
| "xllamacpp_scripts_build", ROOT / "scripts" / "build.py" | ||
| ) | ||
| build = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(build) | ||
|
|
||
|
|
||
| def run_git(repo: Path, *args: str) -> subprocess.CompletedProcess: | ||
| return subprocess.run( | ||
| ["git", *args], cwd=repo, check=True, capture_output=True, text=True | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fake_llamacpp(tmp_path, monkeypatch): | ||
| """A throwaway git repo standing in for the llama.cpp submodule. | ||
|
|
||
| The patch file is authored the same way as the real one: edit, git diff, | ||
| revert. | ||
| """ | ||
| proj = tmp_path / "llama.cpp" | ||
| proj.mkdir() | ||
| run_git(proj, "init") | ||
| target = proj / "ggml-metal-device.m" | ||
| target.write_text("line one\nline two\n") | ||
| run_git(proj, "add", ".") | ||
| run_git( | ||
| proj, | ||
| "-c", | ||
| "user.email=test@example.com", | ||
| "-c", | ||
| "user.name=test", | ||
| "commit", | ||
| "-m", | ||
| "init", | ||
| ) | ||
|
|
||
| target.write_text("line one\nline two patched\n") | ||
| patch_dir = tmp_path / "patches" / "llama.cpp" | ||
| patch_dir.mkdir(parents=True) | ||
| patch_file = patch_dir / "0001-test.patch" | ||
| patch_file.write_text(run_git(proj, "diff").stdout) | ||
| run_git(proj, "checkout", "--", ".") | ||
|
|
||
| monkeypatch.setattr(build, "PROJECT", proj) | ||
| monkeypatch.setattr(build, "PATCH_DIR", patch_dir) | ||
| return proj, target, patch_file | ||
|
|
||
|
|
||
| def git_is_clean(proj: Path) -> bool: | ||
| return run_git(proj, "status", "--porcelain").stdout == "" | ||
|
|
||
|
|
||
| def test_no_patch_dir(tmp_path, monkeypatch): | ||
| monkeypatch.setattr(build, "PATCH_DIR", tmp_path / "does-not-exist") | ||
| assert build.llamacpp_patches() == [] | ||
|
|
||
|
|
||
| def test_apply_then_revert_leaves_tree_clean(fake_llamacpp): | ||
| proj, target, patch_file = fake_llamacpp | ||
|
|
||
| patches = build.llamacpp_patches() | ||
| assert patches == [patch_file] | ||
|
|
||
| applied = build.apply_llamacpp_patches(patches) | ||
| assert applied == [patch_file] | ||
| assert target.read_text() == "line one\nline two patched\n" | ||
|
|
||
| build.revert_llamacpp_patches(applied) | ||
| assert target.read_text() == "line one\nline two\n" | ||
| assert git_is_clean(proj) | ||
|
|
||
|
|
||
| def test_apply_is_idempotent(fake_llamacpp): | ||
| proj, target, patch_file = fake_llamacpp | ||
|
|
||
| applied_first = build.apply_llamacpp_patches(build.llamacpp_patches()) | ||
| assert applied_first == [patch_file] | ||
|
|
||
| # second build against an already-patched tree: skipped, not reverted | ||
| applied_second = build.apply_llamacpp_patches(build.llamacpp_patches()) | ||
| assert applied_second == [] | ||
| assert target.read_text() == "line one\nline two patched\n" | ||
|
|
||
| build.revert_llamacpp_patches(applied_first) | ||
| assert git_is_clean(proj) | ||
|
|
||
|
|
||
| def test_revert_only_what_was_applied(fake_llamacpp): | ||
| proj, target, patch_file = fake_llamacpp | ||
|
|
||
| build.apply_llamacpp_patches(build.llamacpp_patches()) | ||
| # simulate an interrupted build: patch left applied, next build skips it | ||
| applied = build.apply_llamacpp_patches(build.llamacpp_patches()) | ||
| build.revert_llamacpp_patches(applied) | ||
| # nothing was applied by this run, so nothing is reverted either | ||
| assert target.read_text() == "line one\nline two patched\n" | ||
|
|
||
|
|
||
| def test_inapplicable_patch_fails_loudly(fake_llamacpp, tmp_path): | ||
| proj, target, patch_file = fake_llamacpp | ||
| patch_file.write_text( | ||
| "diff --git a/ggml-metal-device.m b/ggml-metal-device.m\n" | ||
| "--- a/ggml-metal-device.m\n" | ||
| "+++ b/ggml-metal-device.m\n" | ||
| "@@ -1,2 +1,2 @@\n" | ||
| "-this content does not exist\n" | ||
| "-neither does this\n" | ||
| "+garbage\n" | ||
| "+patch\n" | ||
| ) | ||
| with pytest.raises(subprocess.CalledProcessError): | ||
| build.apply_llamacpp_patches(build.llamacpp_patches()) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.