Closed Bug 2051973 Opened 4 months ago Closed 2 months ago

Vendor an enable the Metal GGML backend

Categories

(Core :: Machine Learning: On Device, enhancement, P2)

enhancement

Tracking

()

RESOLVED FIXED
157 Branch
Tracking Status
firefox157 --- fixed

People

(Reporter: padenot, Assigned: padenot)

References

(Blocks 1 open bug)

Details

Attachments

(2 external files, 3 obsolete files)

This will provide hardware acceleration on macOS with Metal. Another choice is CoreML.

This includes the Metal shader source in asm and compiles it. Specific
symbols are then picked up by the implementation, and the byte range is
fed to the Metal shader compiler. The resulting object file is
subsequently cached by the OS.

First compilation is in the order of 4-5s, subsequent instantiation is
10-15ms.

This backend will always ever run in a dedicated process, that has a
sandbox very similar to the GPU process, but doesn't have access to the
display server, fonts, and other facilities related to rendering. It can
access one more folder than the GPU process (read/write) to cache the
compiled shaders.

This benefits llama.cpp, but also whisper.cpp, that is introduced in a
later patch. This can multiply the performance by multiple dozen times
(depending on the hardware and use-case). More importantly, it
completely offload inference to the GPU. While (e.g.) real-time local
speech recognition is running with this backend, about:processes
show all thread of the HWInference process as "idle".

Assignee: nobody → padenot
Status: NEW → ASSIGNED
Blocks: 2046988
No longer blocks: llama-cpp-process
Whiteboard: [aiplatform]
Whiteboard: [aiplatform]
Attachment #9603485 - Attachment description: WIP: Bug 2051973 - Vendor and compile the Metal backend for libggml. r?#firefox-build-system-reviewers,atossou → Bug 2051973 - Vendor and compile the Metal backend for libggml. r?#firefox-build-system-reviewers

The severity field is not set for this bug.
:gregtatum, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(gtatum)
Type: defect → enhancement
Flags: needinfo?(gtatum)
Priority: -- → P2

The Metal libggml backend can register a GPU device that then fails during real
backend init (e.g. CI VMs or older Intel Macs, where the device exists but the
command queue / shader library can't be created). Because the vendored library
is built without exceptions, a failure inside llama_context would abort instead
of falling back. Probe the GPU backend once and force CPU offload off when it is
unusable.

Attachment #9620514 - Attachment description: WIP: Bug 2051973 - Fall back to CPU when the GPU backend fails to initialize. r?valentinp → Bug 2051973 - Fall back to CPU when the GPU backend fails to initialize. r?valentinp
Pushed by padenot@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/20d174c4e184 https://hg.mozilla.org/integration/autoland/rev/d6582396cb89 Vendor and compile the Metal backend for libggml. r=firefox-build-system-reviewers,glandium,sergesanspaille,valentinp https://github.com/mozilla-firefox/firefox/commit/c4618016af72 https://hg.mozilla.org/integration/autoland/rev/a8d279045916 Fall back to CPU when the GPU backend fails to initialize. r=valentinp,ai-platform-reviewers

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.

Attachment #9635353 - Attachment description: Bug 2051973 - Vendor the Metal backend at the pinned llama.cpp revision. r?#firefox-build-system-reviewers → Bug 2051973 - Vendor the Metal backend at the pinned llama.cpp revision. r?#ai-platform-reviewers
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 157 Branch
Pushed by padenot@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/1e6254cb9780 https://hg.mozilla.org/integration/autoland/rev/257df0560db0 Vendor the Metal backend at the pinned llama.cpp revision. r=ai-platform-reviewers,valentinp
Pushed by sstanca@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/e632432a4120 https://hg.mozilla.org/integration/autoland/rev/d6eeba45df19 Revert "Bug 2051973 - Vendor the Metal backend at the pinned llama.cpp revision. r=ai-platform-reviewers,valentinp" for causing @ ggml_metal_encoder_set_pipeline related mochitests failures.

Reverted this because it was causing @ ggml_metal_encoder_set_pipeline related mochitests failures.

  • Revert link
  • Push with failures
  • Failure Log
  • Failure line: PROCESS-CRASH | ADB9796F-FF88-4AC1-969E-E2C78DF0900A | application crashed [@ ggml_metal_encoder_set_pipeline] | dom/media/webspeech/recognition/test/test_concurrent_session.html
Flags: needinfo?(padenot)
Flags: needinfo?(padenot)

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.

Attachment #9637486 - Attachment description: Bug 2051973 - Fall back to the general mat-vec kernel when Metal lacks the _short one. r?#ai-platform-reviewers → Bug 2051973 - Fall back to the general mat-vec kernel when Metal lacks the _short one. r?#valentinp

A patch has been attached on this bug, which was already closed. Filing a separate bug will ensure better tracking. If this was not by mistake and further action is needed, please alert the appropriate party. (Or: if the patch doesn't change behavior -- e.g. landing a test case, or fixing a typo -- then feel free to disregard this message)

Blocks: 2068758

Comment on attachment 9635353 [details]
Bug 2051973 - Vendor the Metal backend at the pinned llama.cpp revision. r?#ai-platform-reviewers

Revision D322929 was moved to bug 2068580. Setting attachment 9635353 [details] to obsolete.

Attachment #9635353 - Attachment is obsolete: true

Comment on attachment 9637486 [details]
Bug 2051973 - Fall back to the general mat-vec kernel when Metal lacks the _short one. r?#valentinp

Revision D324025 was moved to bug 2068580. Setting attachment 9637486 [details] to obsolete.

Attachment #9637486 - Attachment is obsolete: true

Comment on attachment 9637487 [details]
Bug 2051973 - Reassemble the embedded Metal shader when it changes. r?valentinp

Revision D324026 was moved to bug 2068580. Setting attachment 9637487 [details] to obsolete.

Attachment #9637487 - Attachment is obsolete: true
QA Whiteboard: [qa-triage-done-c158/b157]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: