[GH-ISSUE #15813] Metal backend crash on Apple M5 - bfloat/half type mismatch in MPPTensorOpsMatMul2d (v0.21.2) #87764

Open
opened 2026-05-10 06:20:24 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @egalarza007 on GitHub (Apr 25, 2026).
Original GitHub issue: https://github.com/ollama/ollama/issues/15813

Description

Ollama fails to run any model on Apple M5 hardware. The Metal shader compilation
fails with a bfloat/half type mismatch in MetalPerformancePrimitives.

Environment

  • Ollama version: 0.21.2
  • macOS: Sequoia 15.3+ (Apple M5)
  • GPU: Apple M5 (MTLGPUFamilyApple10)
  • VRAM: ~19GB available

Error

From ~/.ollama/logs/server.log:

ggml_metal_init: the device does not have a precompiled Metal library - this is unexpected
ggml_metal_init: will try to compile it on the fly
ggml_metal_library_init: error: Error Domain=MTLLibraryErrorDomain Code=3
static_assert failed due to requirement '__tensor_ops_detail::__is_same_v<bfloat, half>'
"Input types must match cooperative tensor types"
ggml_metal_init: error: failed to initialize the Metal library
ggml_backend_metal_device_init: error: failed to allocate context
llama_init_from_model: failed to initialize the context: failed to initialize Metal backend
panic: unable to create llama context

The server reports the GPU as MTLGPUFamilyApple10 (1010) which is M5-specific.
The embedded Metal library does not have a precompiled library for this GPU family,
and on-the-fly compilation fails due to a bfloat16/half type mismatch in the
MetalPerformancePrimitives framework.

Impact

All models fail with: 500 Internal Server Error: llama runner process has terminated
OLLAMA_NO_METAL=1 is silently ignored in v0.21.2.

Workaround

Setting GGML_METAL_BF16_DISABLE=1 before starting the server allows models to
load and run (CPU fallback, no GPU acceleration):

GGML_METAL_BF16_DISABLE=1 ollama serve

Expected Behavior

Ollama should either:

  1. Ship a precompiled Metal library for MTLGPUFamilyApple10 (M5), or
  2. Correctly fall back to CPU when Metal compilation fails, or
  3. Honor OLLAMA_NO_METAL=1 to explicitly disable Metal
Originally created by @egalarza007 on GitHub (Apr 25, 2026). Original GitHub issue: https://github.com/ollama/ollama/issues/15813 ## Description Ollama fails to run any model on Apple M5 hardware. The Metal shader compilation fails with a bfloat/half type mismatch in MetalPerformancePrimitives. ## Environment - Ollama version: 0.21.2 - macOS: Sequoia 15.3+ (Apple M5) - GPU: Apple M5 (MTLGPUFamilyApple10) - VRAM: ~19GB available ## Error From ~/.ollama/logs/server.log: ggml_metal_init: the device does not have a precompiled Metal library - this is unexpected ggml_metal_init: will try to compile it on the fly ggml_metal_library_init: error: Error Domain=MTLLibraryErrorDomain Code=3 static_assert failed due to requirement '__tensor_ops_detail::__is_same_v<bfloat, half>' "Input types must match cooperative tensor types" ggml_metal_init: error: failed to initialize the Metal library ggml_backend_metal_device_init: error: failed to allocate context llama_init_from_model: failed to initialize the context: failed to initialize Metal backend panic: unable to create llama context The server reports the GPU as MTLGPUFamilyApple10 (1010) which is M5-specific. The embedded Metal library does not have a precompiled library for this GPU family, and on-the-fly compilation fails due to a bfloat16/half type mismatch in the MetalPerformancePrimitives framework. ## Impact All models fail with: 500 Internal Server Error: llama runner process has terminated OLLAMA_NO_METAL=1 is silently ignored in v0.21.2. ## Workaround Setting GGML_METAL_BF16_DISABLE=1 before starting the server allows models to load and run (CPU fallback, no GPU acceleration): GGML_METAL_BF16_DISABLE=1 ollama serve ## Expected Behavior Ollama should either: 1. Ship a precompiled Metal library for MTLGPUFamilyApple10 (M5), or 2. Correctly fall back to CPU when Metal compilation fails, or 3. Honor OLLAMA_NO_METAL=1 to explicitly disable Metal
Author
Owner

@asqararslonov commented on GitHub (May 3, 2026):

model failed to load on Apple M5 (Metal 4) — Metal shader half/bfloat type mismatch

Environment:

  • Apple M5 chip (Metal 4)
  • macOS Tahoe (latest)
  • Ollama 0.22.1 (stable / Ollama.app)
  • Any model (gemma4, qwen3, llama3, etc.)

Symptom

Every model fails immediately with:

Error: 500 Internal Server Error: model failed to load, this may be due to 
resource limitations or an internal error, check ollama server logs for details

This is not a RAM issue. It affects all models regardless of size.


Root Cause

The embedded Metal shader in Ollama's ggml backend fails to compile on Metal 4 (M5). Apple's MetalPerformancePrimitives framework on Metal 4 enforces strict type matching for cooperative tensor matmul — but the shader mixes half and bfloat types:

static_assert failed: "Input types must match cooperative tensor types" (half vs bfloat)
ggml_metal_library_init: error: Error Domain=MTLLibraryErrorDomain Code=3
ggml_backend_metal_device_init: error: failed to allocate context
GGML_ASSERT(backend) failed → exit status 2

The following workarounds do not help — Metal is always initialized regardless:

  • OLLAMA_METAL=0
  • OLLAMA_LLM_LIBRARY=cpu
  • OLLAMA_NEW_ENGINE=true

Fix

Install the HEAD build via Homebrew, which includes the Metal 4 shader patch:

# Quit Ollama.app first if it's running
brew unlink ollama        # only if stable was already installed via brew
brew install --HEAD ollama
brew services start ollama

The HEAD brew service automatically sets OLLAMA_FLASH_ATTENTION=1 and
OLLAMA_KV_CACHE_TYPE=q8_0 in the launchd plist.

Also remove Ollama.app from Login Items (System Settings → General → Login Items) to prevent conflicts with the brew service on reboot.


Verification

After the fix, all models load and run on GPU:

$ ollama ps
NAME             ID              SIZE     PROCESSOR    CONTEXT
gemma4:latest    c6eb396dbd59    10 GB    100% GPU     4096

Tested with gemma4:latest (9.6 GB) and qwen3:8b on Apple M5.

<!-- gh-comment-id:4366508665 --> @asqararslonov commented on GitHub (May 3, 2026): ## `model failed to load` on Apple M5 (Metal 4) — Metal shader `half`/`bfloat` type mismatch **Environment:** - Apple M5 chip (Metal 4) - macOS Tahoe (latest) - Ollama 0.22.1 (stable / Ollama.app) - Any model (gemma4, qwen3, llama3, etc.) --- ### Symptom Every model fails immediately with: ``` Error: 500 Internal Server Error: model failed to load, this may be due to resource limitations or an internal error, check ollama server logs for details ``` This is **not** a RAM issue. It affects all models regardless of size. --- ### Root Cause The embedded Metal shader in Ollama's ggml backend fails to compile on **Metal 4 (M5)**. Apple's `MetalPerformancePrimitives` framework on Metal 4 enforces strict type matching for cooperative tensor matmul — but the shader mixes `half` and `bfloat` types: ``` static_assert failed: "Input types must match cooperative tensor types" (half vs bfloat) ggml_metal_library_init: error: Error Domain=MTLLibraryErrorDomain Code=3 ggml_backend_metal_device_init: error: failed to allocate context GGML_ASSERT(backend) failed → exit status 2 ``` The following workarounds do **not** help — Metal is always initialized regardless: - `OLLAMA_METAL=0` - `OLLAMA_LLM_LIBRARY=cpu` - `OLLAMA_NEW_ENGINE=true` --- ### Fix Install the HEAD build via Homebrew, which includes the Metal 4 shader patch: ```bash # Quit Ollama.app first if it's running brew unlink ollama # only if stable was already installed via brew brew install --HEAD ollama brew services start ollama ``` > The HEAD brew service automatically sets `OLLAMA_FLASH_ATTENTION=1` and > `OLLAMA_KV_CACHE_TYPE=q8_0` in the launchd plist. Also remove **Ollama.app** from Login Items (**System Settings → General → Login Items**) to prevent conflicts with the brew service on reboot. --- ### Verification After the fix, all models load and run on GPU: ``` $ ollama ps NAME ID SIZE PROCESSOR CONTEXT gemma4:latest c6eb396dbd59 10 GB 100% GPU 4096 ``` Tested with `gemma4:latest` (9.6 GB) and `qwen3:8b` on Apple M5.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#87764