Closed Bug 2068580 Opened 2 months ago Closed 2 months ago

Update llama.cpp metal backend

Categories

(Core :: Machine Learning: On Device, defect)

defect

Tracking

()

RESOLVED FIXED
158 Branch
Tracking Status
firefox157 --- fixed
firefox158 --- fixed

People

(Reporter: padenot, Assigned: padenot)

Details

Attachments

(6 external files)

No description provided.

When filing bugs on this component, please add a bug description here, as it's hard for maintainers to know what's going on with cryptic bugs filed like this. For instance, why are you needing to update it. Is there a specific backend you are targeting? Is there some kind of mismatch going on? Does this affect speech recognition? We actively triage and this is marked as a defect that will show up in our triage. This looks like a "task" rather a "defect".

Flags: needinfo?(padenot)

The Metal files were taken from an older llama.cpp than the revision moz.yaml
pins, which is why they had to be in "keep" rather than revendored.

This vendors the backend at 74ade527 instead, matching llama.cpp. At that rev,
it is split into a few files. The embedding of the shader source is unchanged: the
.metal file still has the embed_ggml-common.h and ggml-metal-impl.h
placeholders generate_metal_embed.py substitutes. etc.

ggml-metal-device.m and ggml-metal-device.cpp would compile to the same object
name, so run_parser.py renames the former to ggml-metal-device-m.m, like it
already does for ggml.c and ggml-cpu.c.

Two patches are needed on top: <memory> for std::unique_ptr in ggml-metal.cpp,
and a guard around -[MTLSharedEvent waitUntilSignaledValue:timeoutMS:], which
is macOS 12 while we deploy to 10.15 (11.0 on aarch64).

mach vendor --patch-mode check now reproduces the tree.

ggml_metal_library_get_pipeline_mul_mv asks for a _short kernel variant when
ne00 < 32. On the macOS 15 machines the Metal compiler does not emit those
specializations from the vendored shader source, so the lookup fails with

MTLLibraryErrorDomain Code=5 "Function kernel_mul_mv_f32_f32_short was not
found in the library"

get_pipeline_mul_mv returns a null pipeline and the caller encodes it without
checking.

llama.cpp doesn't use this path, parakeet.cpp does, we can simply use the general
kernel, which lives in the same source, it is emitted everywhere, and handles any size.

Editing ggml-metal.metal did not change the built binary, very confusing.

I mistakenly attached the patches to bug 2051973, this should have been clear from the start, sorry I just found out and fixed it.

Flags: needinfo?(padenot)
Pushed by padenot@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/a45275fb793f https://hg.mozilla.org/integration/autoland/rev/f771b516e40f Vendor the Metal backend at the pinned llama.cpp revision. r=ai-platform-reviewers,valentinp https://github.com/mozilla-firefox/firefox/commit/153ac773b884 https://hg.mozilla.org/integration/autoland/rev/d9f68a527c41 Fall back to the general mat-vec kernel when Metal lacks the _short one. r=ai-platform-reviewers,valentinp https://github.com/mozilla-firefox/firefox/commit/f8396e0da6dd https://hg.mozilla.org/integration/autoland/rev/52cdfd8dd5f4 Reassemble the embedded Metal shader when it changes. r=valentinp
Status: NEW → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 158 Branch

The Metal files were taken from an older llama.cpp than the revision moz.yaml
pins, which is why they had to be in "keep" rather than revendored.

This vendors the backend at 74ade527 instead, matching llama.cpp. At that rev,
it is split into a few files. The embedding of the shader source is unchanged: the
.metal file still has the embed_ggml-common.h and ggml-metal-impl.h
placeholders generate_metal_embed.py substitutes. etc.

ggml-metal-device.m and ggml-metal-device.cpp would compile to the same object
name, so run_parser.py renames the former to ggml-metal-device-m.m, like it
already does for ggml.c and ggml-cpu.c.

Two patches are needed on top: <memory> for std::unique_ptr in ggml-metal.cpp,
and a guard around -[MTLSharedEvent waitUntilSignaledValue:timeoutMS:], which
is macOS 12 while we deploy to 10.15 (11.0 on aarch64).

mach vendor --patch-mode check now reproduces the tree.

Original Revision: https://phabricator.services.mozilla.com/D322929

Attachment #9643345 - Flags: approval-mozilla-beta?

ggml_metal_library_get_pipeline_mul_mv asks for a _short kernel variant when
ne00 < 32. On the macOS 15 machines the Metal compiler does not emit those
specializations from the vendored shader source, so the lookup fails with

MTLLibraryErrorDomain Code=5 "Function kernel_mul_mv_f32_f32_short was not
found in the library"

get_pipeline_mul_mv returns a null pipeline and the caller encodes it without
checking.

llama.cpp doesn't use this path, parakeet.cpp does, we can simply use the general
kernel, which lives in the same source, it is emitted everywhere, and handles any size.

Original Revision: https://phabricator.services.mozilla.com/D324025

Attachment #9643346 - Flags: approval-mozilla-beta?

Editing ggml-metal.metal did not change the built binary, very confusing.

Original Revision: https://phabricator.services.mozilla.com/D324026

Attachment #9643347 - Flags: approval-mozilla-beta?
Attachment #9643347 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
Attachment #9643346 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
Attachment #9643345 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: