Metal: SIGSEGV on any micro-batch of 32+ tokens (no kernel_mul_mm_id_map0 for 12 experts)

#2
by inclub9 - opened

Thanks for the port. On Apple Silicon it crashes on the first prompt of 32 or more tokens. Here is the cause, which looks like a one-line fix.

Setup: M1 Max 64 GB, macOS 27.2.1, llama.cpp d3c3e242c + maxwell-moe-port.patch (sha256 55049b84…), Release build with default Metal, Maxwell-V1-Flash-AD-Q6_K.gguf at 0e2d02c0, llama-server --jinja -ngl 99 -fa on -c 131072.

Symptom: llama-server dies with SIGSEGV (exit 139) on any ubatch of 32+ tokens.

  • Prompts of 21–29 tokens work; a 32-token prompt crashes.
  • With a 612-token prompt, -ub 16/24/28/31 work and -ub 32 crashes.
  • -ngl 0 (CPU) works.

Faulting thread (macOS crash report):

ggml_metal_pipeline_max_theads_per_threadgroup + 12   KERN_INVALID_ADDRESS at 0x0
ggml_metal_op_mul_mat_id + 804
ggml_metal_op_encode + 2152
ggml_metal_graph_compute + 712
ggml_backend_sched_graph_compute_async + 1820
llama_context::graph_compute
llama_context::process_ubatch
llama_context::decode

Cause: ggml_metal_op_mul_mat_id_use_mm() (ggml-metal-common.cpp) switches to the matrix-matrix path when ne21 >= 32. That path fetches kernel_mul_mm_id_map0_ne20_<n_expert_used>, but kernels/mul_mm.metal instantiates it only for ne20 = 1, 2, 4, 5, 6, 8, 9, 10, 16 and 22. Maxwell routes 12 experts per token, so the pipeline comes back null and ggml_metal_pipeline_max_theads_per_threadgroup() dereferences it. Current llama.cpp master (0a7675746) has the same list. CUDA does not specialize per ne20, which would explain why test-llama-archs passed there.

Likely fix (not tested; we no longer have the weights on disk): in ggml/src/ggml-metal/kernels/mul_mm.metal, next to the existing lines:

template [[host_name("kernel_mul_mm_id_map0_ne20_12")]] kernel kernel_mul_mm_id_map0_t kernel_mul_mm_id_map0<12>;

It may be worth putting this in the patch and in the upstream PR. Until then, -ub 31 avoids the crash, but prompt processing ran at only about 110 tokens/s on this machine.

Sign up or log in to comment