llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Misc. bug: Unsupported op "CPY" / Segmentation fault on Metal

Open firelex opened this issue 1 year ago • 12 comments

Name and Version

version: 4391 (9ba399df) built with Apple clang version 16.0.0 (clang-1600.0.26.6) for arm64-apple-darwin24.1.0

Operating systems

Mac (M4 Max / 128 GB)

Which llama.cpp modules do you know to be affected?

llama-server

Problem description & steps to reproduce

./build/bin/llama-server -m /Users/mattsinalco/.cache/huggingface/hub/models--unsloth--Llama-3.3-70B-Instruct-GGUF/snapshots/0c14ebbedd129fb190c8241facca9a360e81c650/Llama-3.3-70B-Instruct-Q4_K_M.gguf -md /Users/mattsinalco/.cache/huggingface/hub/models--unsloth--Llama-3.2-1B-Instruct-GGUF/snapshots/a5594fb18df5dfc6b43281423fcce6750cd92de5/Llama-3.2-1B-Instruct-Q4_K_M.gguf -ngl 99 -ngld 99 -fa --port 8034 --ctx-size 8192 --ctx-size-draft 8192 --draft-min 0 --draft-max 16 -np 7 --host 0.0.0.0 --slots --slot-save-path /Users/mattsinalco/mathias/caching -ctk q4_1 -ctv q4_1

Sometimes (reproducibly) gives me this:

/Users/mattsinalco/mathias/llama.cpp/ggml/src/ggml-metal/ggml-metal.m:1263: unsupported op ggml_metal_encode_node: error: unsupported op 'CPY'

Other quantizations give me this:

zsh: segmentation fault ./build/bin/llama-server -m -md -ngl 99 -ngld 99 -fa --port 8034 --ctx-size

Related question - in the absence of quantization the KV cache workign reliabely, can I resize the KV cache size? I can't seem to load slots of 200MB (100MB is possible).

First Bad Commit

No response

Relevant log output

No response

firelex avatar Dec 25 '24 18:12 firelex

I have stumbled upon the same problem on M1 Pro:

lama.cpp/ggml/src/ggml-metal/ggml-metal.m:1340: unsupported op
ggml_metal_encode_node: error: unsupported op 'CPY'
zsh: abort      ./build/bin/llama-cli --model  --cache-type-k q8_0 --threads 16 --prompt 

llama.cpp version: 178a7eb952d211b8d4232d5e50ae1b64519172a9

rchmielarz avatar Jan 26 '25 23:01 rchmielarz

I have hit the same issue too.

llama-server -m models/DeepSeek-R1-Distill-Qwen-32B-Q4_K_M.gguf -ngl 999 -c 4096 -a deepseek-chat --host 192.168.1.11 --threads 4 --cache-type-k q8_0 --api-key 123456

build: 4570 (6e84b0ab) with Apple clang version 16.0.0 (clang-1600.0.26.6) for arm64-apple-darwin24.2.0 system info: n_threads = 4, n_threads_batch = 4, total_threads = 10

using device Metal (Apple M1 Max) - 21845 MiB free

/tmp/llama.cpp-20250128-5366-7zvo0y/ggml/src/ggml-metal/ggml-metal.m:1344: unsupported op ggml_metal_encode_node: error: unsupported op 'CPY'

After removing --cache-type-k q8_0 --api-key 123456, I didn't have this problem any more seemingly.

stonybeach avatar Jan 29 '25 11:01 stonybeach

same here. Issue occurs whilst running some inferences (but not all)

/tmp/llama.cpp-20250202-5286-e8qa8q/ggml/src/ggml-metal/ggml-metal.m:1344: unsupported op
ggml_metal_encode_node: error: unsupported op 'CPY'

EDIT: Added command which causes error

llama-server --port 2284 --parallel 2 --jinja --chat-template-file <( python scripts/get_chat_template.py deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja tool_use ) --ctx-size 64000 --cache-type-k q8_0 --temp 0.6 -hf bartowski/DeepSeek-R1-Distill-Qwen-14B-GGUF:Q5_K_L

jcbdev avatar Feb 04 '25 14:02 jcbdev

Please attach a full log of the command that triggers this error.

ggerganov avatar Feb 04 '25 14:02 ggerganov

llama-server -m Darkest-muse-v1-Q8_0.gguf -ngl -1 -fa --cache-type-v q8_0 --cache-type-k q8_0 -c 8192 -n 32768
build: 4667 (d2fe216f) with Apple clang version 16.0.0 (clang-1600.0.26.6) for arm64-apple-darwin24.2.0
system info: n_threads = 10, n_threads_batch = 10, total_threads = 14

system_info: n_threads = 10 (n_threads_batch = 10) / 14 | Metal : EMBED_LIBRARY = 1 | CPU : NEON = 1 | ARM_FMA = 1 | FP16_VA = 1 | DOTPROD = 1 | LLAMAFILE = 1 | ACCELERATE = 1 | AARCH64_REPACK = 1 |

main: HTTP server is listening, hostname: 127.0.0.1, port: 8080, http threads: 13
main: loading model
srv    load_model: loading model 'Darkest-muse-v1-Q8_0.gguf'
llama_model_load_from_file_impl: using device Metal (Apple M4 Pro) - 36863 MiB free
llama_model_loader: loaded meta data with 53 key-value pairs and 464 tensors from Darkest-muse-v1-Q8_0.gguf (version GGUF V3 (latest))
llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
llama_model_loader: - kv   0:                       general.architecture str              = gemma2
llama_model_loader: - kv   1:                               general.type str              = model
llama_model_loader: - kv   2:                               general.name str              = Gemma 2 Ataraxy v2 9B
llama_model_loader: - kv   3:                            general.version str              = v1
llama_model_loader: - kv   4:                       general.organization str              = Lemon07R
llama_model_loader: - kv   5:                           general.basename str              = Gemma-2-Ataraxy-v2
llama_model_loader: - kv   6:                         general.size_label str              = 9B
llama_model_loader: - kv   7:                            general.license str              = apache-2.0
llama_model_loader: - kv   8:                   general.base_model.count u32              = 3
llama_model_loader: - kv   9:                  general.base_model.0.name str              = Gemma 2 Ataraxy v2 9B
llama_model_loader: - kv  10:          general.base_model.0.organization str              = Lemon07R
llama_model_loader: - kv  11:              general.base_model.0.repo_url str              = https://huggingface.co/lemon07r/Gemma...
llama_model_loader: - kv  12:                  general.base_model.1.name str              = Delirium v1
llama_model_loader: - kv  13:               general.base_model.1.version str              = v1
llama_model_loader: - kv  14:          general.base_model.1.organization str              = Sam Paech
llama_model_loader: - kv  15:              general.base_model.1.repo_url str              = https://huggingface.co/sam-paech/Deli...
llama_model_loader: - kv  16:                  general.base_model.2.name str              = Quill v1
llama_model_loader: - kv  17:               general.base_model.2.version str              = v1
llama_model_loader: - kv  18:          general.base_model.2.organization str              = Sam Paech
llama_model_loader: - kv  19:              general.base_model.2.repo_url str              = https://huggingface.co/sam-paech/Quil...
llama_model_loader: - kv  20:                               general.tags arr[str,2]       = ["creative-writing", "gemma2"]
llama_model_loader: - kv  21:                           general.datasets arr[str,1]       = ["sam-paech/gutenberg3-generalfiction...
llama_model_loader: - kv  22:                      gemma2.context_length u32              = 8192
llama_model_loader: - kv  23:                    gemma2.embedding_length u32              = 3584
llama_model_loader: - kv  24:                         gemma2.block_count u32              = 42
llama_model_loader: - kv  25:                 gemma2.feed_forward_length u32              = 14336
llama_model_loader: - kv  26:                gemma2.attention.head_count u32              = 16
llama_model_loader: - kv  27:             gemma2.attention.head_count_kv u32              = 8
llama_model_loader: - kv  28:    gemma2.attention.layer_norm_rms_epsilon f32              = 0.000001
llama_model_loader: - kv  29:                gemma2.attention.key_length u32              = 256
llama_model_loader: - kv  30:              gemma2.attention.value_length u32              = 256
llama_model_loader: - kv  31:                          general.file_type u32              = 7
llama_model_loader: - kv  32:              gemma2.attn_logit_softcapping f32              = 50.000000
llama_model_loader: - kv  33:             gemma2.final_logit_softcapping f32              = 30.000000
llama_model_loader: - kv  34:            gemma2.attention.sliding_window u32              = 4096
llama_model_loader: - kv  35:                       tokenizer.ggml.model str              = llama
llama_model_loader: - kv  36:                         tokenizer.ggml.pre str              = default
llama_model_loader: - kv  37:                      tokenizer.ggml.tokens arr[str,256000]  = ["<pad>", "<eos>", "<bos>", "<unk>", ...
llama_model_loader: - kv  38:                      tokenizer.ggml.scores arr[f32,256000]  = [-1000.000000, -1000.000000, -1000.00...
llama_model_loader: - kv  39:                  tokenizer.ggml.token_type arr[i32,256000]  = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...
llama_model_loader: - kv  40:                tokenizer.ggml.bos_token_id u32              = 2
llama_model_loader: - kv  41:                tokenizer.ggml.eos_token_id u32              = 1
llama_model_loader: - kv  42:            tokenizer.ggml.unknown_token_id u32              = 3
llama_model_loader: - kv  43:            tokenizer.ggml.padding_token_id u32              = 0
llama_model_loader: - kv  44:               tokenizer.ggml.add_bos_token bool             = true
llama_model_loader: - kv  45:               tokenizer.ggml.add_eos_token bool             = false
llama_model_loader: - kv  46:                    tokenizer.chat_template str              = {{ bos_token }}{% if messages[0]['rol...
llama_model_loader: - kv  47:            tokenizer.ggml.add_space_prefix bool             = false
llama_model_loader: - kv  48:               general.quantization_version u32              = 2
llama_model_loader: - kv  49:                      quantize.imatrix.file str              = /models_out/Darkest-muse-v1-GGUF/Dark...
llama_model_loader: - kv  50:                   quantize.imatrix.dataset str              = /training_dir/calibration_datav3.txt
llama_model_loader: - kv  51:             quantize.imatrix.entries_count i32              = 294
llama_model_loader: - kv  52:              quantize.imatrix.chunks_count i32              = 128
llama_model_loader: - type  f32:  169 tensors
llama_model_loader: - type q8_0:  295 tensors
print_info: file format = GGUF V3 (latest)
print_info: file type   = Q8_0
print_info: file size   = 9.15 GiB (8.50 BPW)
load: special_eos_id is not in special_eog_ids - the tokenizer config may be incorrect
load: special tokens cache size = 249
load: token to piece cache size = 1.6014 MB
print_info: arch             = gemma2
print_info: vocab_only       = 0
print_info: n_ctx_train      = 8192
print_info: n_embd           = 3584
print_info: n_layer          = 42
print_info: n_head           = 16
print_info: n_head_kv        = 8
print_info: n_rot            = 256
print_info: n_swa            = 4096
print_info: n_embd_head_k    = 256
print_info: n_embd_head_v    = 256
print_info: n_gqa            = 2
print_info: n_embd_k_gqa     = 2048
print_info: n_embd_v_gqa     = 2048
print_info: f_norm_eps       = 0.0e+00
print_info: f_norm_rms_eps   = 1.0e-06
print_info: f_clamp_kqv      = 0.0e+00
print_info: f_max_alibi_bias = 0.0e+00
print_info: f_logit_scale    = 0.0e+00
print_info: n_ff             = 14336
print_info: n_expert         = 0
print_info: n_expert_used    = 0
print_info: causal attn      = 1
print_info: pooling type     = 0
print_info: rope type        = 2
print_info: rope scaling     = linear
print_info: freq_base_train  = 10000.0
print_info: freq_scale_train = 1
print_info: n_ctx_orig_yarn  = 8192
print_info: rope_finetuned   = unknown
print_info: ssm_d_conv       = 0
print_info: ssm_d_inner      = 0
print_info: ssm_d_state      = 0
print_info: ssm_dt_rank      = 0
print_info: ssm_dt_b_c_rms   = 0
print_info: model type       = 9B
print_info: model params     = 9.24 B
print_info: general.name     = Gemma 2 Ataraxy v2 9B
print_info: vocab type       = SPM
print_info: n_vocab          = 256000
print_info: n_merges         = 0
print_info: BOS token        = 2 '<bos>'
print_info: EOS token        = 1 '<eos>'
print_info: EOT token        = 107 '<end_of_turn>'
print_info: UNK token        = 3 '<unk>'
print_info: PAD token        = 0 '<pad>'
print_info: LF token         = 227 '<0x0A>'
print_info: EOG token        = 1 '<eos>'
print_info: EOG token        = 107 '<end_of_turn>'
print_info: max token length = 48
load_tensors: loading model tensors, this can take a while... (mmap = true)
load_tensors: offloading 42 repeating layers to GPU
load_tensors: offloading output layer to GPU
load_tensors: offloaded 43/43 layers to GPU
load_tensors: Metal_Mapped model buffer size =  9366.13 MiB
load_tensors:   CPU_Mapped model buffer size =   929.69 MiB
...................................................................................
llama_init_from_model: n_seq_max     = 1
llama_init_from_model: n_ctx         = 8192
llama_init_from_model: n_ctx_per_seq = 8192
llama_init_from_model: n_batch       = 2048
llama_init_from_model: n_ubatch      = 512
llama_init_from_model: flash_attn    = 1
llama_init_from_model: freq_base     = 10000.0
llama_init_from_model: freq_scale    = 1
ggml_metal_init: allocating
ggml_metal_init: found device: Apple M4 Pro
ggml_metal_init: picking default device: Apple M4 Pro
ggml_metal_init: using embedded metal library
ggml_metal_init: GPU name:   Apple M4 Pro
ggml_metal_init: GPU family: MTLGPUFamilyApple9  (1009)
ggml_metal_init: GPU family: MTLGPUFamilyCommon3 (3003)
ggml_metal_init: GPU family: MTLGPUFamilyMetal3  (5001)
ggml_metal_init: simdgroup reduction   = true
ggml_metal_init: simdgroup matrix mul. = true
ggml_metal_init: has residency sets    = true
ggml_metal_init: has bfloat            = true
ggml_metal_init: use bfloat            = false
ggml_metal_init: hasUnifiedMemory      = true
ggml_metal_init: recommendedMaxWorkingSetSize  = 38654.71 MB
ggml_metal_init: skipping kernel_get_rows_bf16                     (not supported)
ggml_metal_init: skipping kernel_mul_mv_bf16_f32                   (not supported)
ggml_metal_init: skipping kernel_mul_mv_bf16_f32_1row              (not supported)
ggml_metal_init: skipping kernel_mul_mv_bf16_f32_l4                (not supported)
ggml_metal_init: skipping kernel_mul_mv_bf16_bf16                  (not supported)
ggml_metal_init: skipping kernel_mul_mv_id_bf16_f32                (not supported)
ggml_metal_init: skipping kernel_mul_mm_bf16_f32                   (not supported)
ggml_metal_init: skipping kernel_mul_mm_id_bf16_f32                (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h64           (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h80           (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h96           (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h112          (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h128          (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_bf16_h256          (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_vec_bf16_h128      (not supported)
ggml_metal_init: skipping kernel_flash_attn_ext_vec_bf16_h256      (not supported)
ggml_metal_init: skipping kernel_cpy_f32_bf16                      (not supported)
ggml_metal_init: skipping kernel_cpy_bf16_f32                      (not supported)
ggml_metal_init: skipping kernel_cpy_bf16_bf16                     (not supported)
llama_kv_cache_init: kv_size = 8192, offload = 1, type_k = 'q8_0', type_v = 'q8_0', n_layer = 42, can_shift = 1
llama_kv_cache_init:      Metal KV buffer size =  1428.00 MiB
llama_init_from_model: KV self size  = 1428.00 MiB, K (q8_0):  714.00 MiB, V (q8_0):  714.00 MiB
llama_init_from_model:        CPU  output buffer size =     0.98 MiB
llama_init_from_model:      Metal compute buffer size =   507.00 MiB
llama_init_from_model:        CPU compute buffer size =    39.01 MiB
llama_init_from_model: graph nodes  = 1398
llama_init_from_model: graph splits = 2
common_init_from_params: setting dry_penalty_last_n to ctx_size = 8192
common_init_from_params: warming up the model with an empty run - please wait ... (--no-warmup to disable)
srv          init: initializing slots, n_slots = 1
slot         init: id  0 | task -1 | new slot n_ctx_slot = 8192
main: model loaded
main: chat template, chat_template: {{ bos_token }}{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '<start_of_turn>' + role + '
' + message['content'] | trim + '<end_of_turn>
' }}{% endfor %}{% if add_generation_prompt %}{{'<start_of_turn>model
'}}{% endif %}, example_format: '<start_of_turn>user
You are a helpful assistant

Hello<end_of_turn>
<start_of_turn>model
Hi there<end_of_turn>
<start_of_turn>user
How are you?<end_of_turn>
<start_of_turn>model
'
main: server is listening on http://127.0.0.1:8080 - starting the main loop
srv  update_slots: all slots are idle
Wrong type supplied for parameter 'max_tokens'. Expected 'number', using default value
slot launch_slot_: id  0 | task 0 | processing task
slot update_slots: id  0 | task 0 | new prompt, n_ctx_slot = 8192, n_keep = 0, n_prompt_tokens = 8112
slot update_slots: id  0 | task 0 | kv cache rm [0, end)
slot update_slots: id  0 | task 0 | prompt processing progress, n_past = 2048, n_tokens = 2048, progress = 0.252465
slot update_slots: id  0 | task 0 | kv cache rm [2048, end)
slot update_slots: id  0 | task 0 | prompt processing progress, n_past = 4096, n_tokens = 2048, progress = 0.504931
slot update_slots: id  0 | task 0 | kv cache rm [4096, end)
slot update_slots: id  0 | task 0 | prompt processing progress, n_past = 6144, n_tokens = 2048, progress = 0.757396
slot update_slots: id  0 | task 0 | kv cache rm [6144, end)
slot update_slots: id  0 | task 0 | prompt processing progress, n_past = 8112, n_tokens = 1968, progress = 1.000000
slot update_slots: id  0 | task 0 | prompt done, n_past = 8112, n_tokens = 1968
slot update_slots: id  0 | task 0 | slot context shift, n_keep = 1, n_left = 8190, n_discard = 4095
/tmp/llama.cpp-20250207-5311-s6u92s/ggml/src/ggml-metal/ggml-metal.m:1352: unsupported op
ggml_metal_encode_node: error: unsupported op 'CPY'
zsh: abort      llama-server -m Darkest-muse-v1-Q8_0.gguf -ngl -1 -fa --cache-type-v q8_0   -

I think it happens during KV context shifting?

gcp avatar Feb 08 '25 00:02 gcp

Yes, we need to add kernels for Q8_0 -> F32, but it's not a priority. PRs welcome. In the meantime, you can simply use F16 KV cache - you have plenty of VRAM.

ggerganov avatar Feb 08 '25 09:02 ggerganov

Yes, but unfortunately some uses cases are much tighter on RAM. I'll have a look how hard this is to fix.

gcp avatar Feb 09 '25 11:02 gcp

llama.cpp/ggml/src/ggml-cuda/cpy.cu:540: ggml_cuda_cpy_fn: unsupported type combination (q5_1 t o f32) looks like the CUDA code is also missing most those extra quants.

gcp avatar Feb 17 '25 14:02 gcp

Okay, now to figure out how to test if the dequant is actually right.

gcp avatar Feb 17 '25 17:02 gcp

The test-backend-ops tool can be used to compare the results of ops running on the CPU vs a backend.

ggerganov avatar Feb 17 '25 17:02 ggerganov

  CPY(type_src=q4_0,type_dst=f32,ne=[256,4,4,4],permute=[0,0,0,0]): OK
  CPY(type_src=q4_0,type_dst=f32,ne=[256,2,3,4],permute=[0,2,1,3]): OK
  CPY(type_src=q4_1,type_dst=f32,ne=[256,4,4,4],permute=[0,0,0,0]): OK
  CPY(type_src=q4_1,type_dst=f32,ne=[256,2,3,4],permute=[0,2,1,3]): OK
  CPY(type_src=q5_0,type_dst=f32,ne=[256,4,4,4],permute=[0,0,0,0]): OK
  CPY(type_src=q5_0,type_dst=f32,ne=[256,2,3,4],permute=[0,2,1,3]): OK
  CPY(type_src=q5_1,type_dst=f32,ne=[256,4,4,4],permute=[0,0,0,0]): OK
  CPY(type_src=q5_1,type_dst=f32,ne=[256,2,3,4],permute=[0,2,1,3]): OK

Yay I guess.

gcp avatar Feb 19 '25 22:02 gcp

Both Metal and CUDA are passing CI now, PR is up.

gcp avatar Feb 20 '25 22:02 gcp

This should be fixed now.

gcp avatar Feb 25 '25 09:02 gcp