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

Bug: src/llama.cpp:15099: Deepseek2 does not support K-shift

Open heyjohnlim opened this issue 1 year ago • 5 comments
trafficstars

What happened?

Hi, when stress testing llama-server (--parallel 3, prompt="Count 1 to 10000 in words") and running deepseek-coder-v2:16b-lite-instruct-q8_0 i got this assertion error in the logs and everything stopped working, so i have to restart llm-server.

Startup script:

~/llama.cpp/llama-server -m /usr/share/ollama/.ollama/models/blobs/sha256-373dcfc92e01372709b6164fc836f677a6280e25e9eac5c434c64223207bfc4f --port 8000 --host 0.0.0.0 -ngl 28 -c 24600 --threads 16 --parallel 3 --log-format text --predict -2 --logdir ~/llama.cpp/logs --log-append $1 $2 >> ~/llama.cpp/logs/deepseek.log 2>&1

Name and Version

version: 3509 (ecf6b7f2) built with cc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22.0.1) for x86_64-redhat-linux

What operating system are you seeing the problem on?

No response

Relevant log output

**Logs just before it crashed:**

INFO [   launch_slot_with_task] slot is processing task | tid="139873529581568" timestamp=1722830275 id_slot=2 id_task=8969
INFO [            update_slots] kv cache rm [p0, end) | tid="139873529581568" timestamp=1722830275 id_slot=2 id_task=8969 p0=0
INFO [            update_slots] kv cache rm [p0, end) | tid="139873529581568" timestamp=1722830276 id_slot=2 id_task=8969 p0=2046
INFO [            update_slots] kv cache rm [p0, end) | tid="139873529581568" timestamp=1722830277 id_slot=2 id_task=8969 p0=4092
INFO [            update_slots] kv cache rm [p0, end) | tid="139873529581568" timestamp=1722830278 id_slot=2 id_task=8969 p0=6138
INFO [            update_slots] slot context shift | tid="139873529581568" timestamp=1722830294 id_slot=2 id_task=8969 n_keep=1 n_left=8200 n_discard=4100 n_ctx=24608 n_past=8201 n_system_tokens=0 n_cache_tokens=6138
Deepseek2 does not support K-shift


**My startup logs:**

INFO [                    main] build info | tid="139628879708160" timestamp=1722830350 build=3509 commit="ecf6b7f2"
INFO [                    main] system info | tid="139628879708160" timestamp=1722830350 n_threads=16 n_threads_batch=-1 total_threads=32 system_info="AVX = 1 | AVX_VNNI = 0 | AVX2 = 1 | AVX512 = 1 | AVX512_VBMI = 1 | AVX512_VNNI = 1 | AVX512_BF16 = 0 | FMA = 1 | NEON = 0 | SVE = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | SSSE3 = 1 | VSX = 0 | MATMUL_INT8 = 0 | LLAMAFILE = 1 | "
llama_model_loader: loaded meta data with 38 key-value pairs and 377 tensors from /usr/share/ollama/.ollama/models/blobs/sha256-373dcfc92e01372709b6164fc836f677a6280e25e9eac5c434c64223207bfc4f (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              = deepseek2
llama_model_loader: - kv   1:                               general.name str              = DeepSeek-Coder-V2-Lite-Instruct
llama_model_loader: - kv   2:                      deepseek2.block_count u32              = 27
llama_model_loader: - kv   3:                   deepseek2.context_length u32              = 163840
llama_model_loader: - kv   4:                 deepseek2.embedding_length u32              = 2048
llama_model_loader: - kv   5:              deepseek2.feed_forward_length u32              = 10944
llama_model_loader: - kv   6:             deepseek2.attention.head_count u32              = 16
llama_model_loader: - kv   7:          deepseek2.attention.head_count_kv u32              = 16
llama_model_loader: - kv   8:                   deepseek2.rope.freq_base f32              = 10000.000000
llama_model_loader: - kv   9: deepseek2.attention.layer_norm_rms_epsilon f32              = 0.000001
llama_model_loader: - kv  10:                deepseek2.expert_used_count u32              = 6
llama_model_loader: - kv  11:                          general.file_type u32              = 7
llama_model_loader: - kv  12:        deepseek2.leading_dense_block_count u32              = 1
llama_model_loader: - kv  13:                       deepseek2.vocab_size u32              = 102400
llama_model_loader: - kv  14:           deepseek2.attention.kv_lora_rank u32              = 512
llama_model_loader: - kv  15:             deepseek2.attention.key_length u32              = 192
llama_model_loader: - kv  16:           deepseek2.attention.value_length u32              = 128
llama_model_loader: - kv  17:       deepseek2.expert_feed_forward_length u32              = 1408
llama_model_loader: - kv  18:                     deepseek2.expert_count u32              = 64
llama_model_loader: - kv  19:              deepseek2.expert_shared_count u32              = 2
llama_model_loader: - kv  20:             deepseek2.expert_weights_scale f32              = 1.000000
llama_model_loader: - kv  21:             deepseek2.rope.dimension_count u32              = 64
llama_model_loader: - kv  22:                deepseek2.rope.scaling.type str              = yarn
llama_model_loader: - kv  23:              deepseek2.rope.scaling.factor f32              = 40.000000
llama_model_loader: - kv  24: deepseek2.rope.scaling.original_context_length u32              = 4096
llama_model_loader: - kv  25: deepseek2.rope.scaling.yarn_log_multiplier f32              = 0.070700
llama_model_loader: - kv  26:                       tokenizer.ggml.model str              = gpt2
llama_model_loader: - kv  27:                         tokenizer.ggml.pre str              = deepseek-llm
llama_model_loader: - kv  28:                      tokenizer.ggml.tokens arr[str,102400]  = ["!", "\"", "#", "$", "%", "&", "'", ...
llama_model_loader: - kv  29:                  tokenizer.ggml.token_type arr[i32,102400]  = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
llama_model_loader: - kv  30:                      tokenizer.ggml.merges arr[str,99757]   = ["?|  ?| ", "?|  t", "?|  a", "i n", "h e...
llama_model_loader: - kv  31:                tokenizer.ggml.bos_token_id u32              = 100000
llama_model_loader: - kv  32:                tokenizer.ggml.eos_token_id u32              = 100001
llama_model_loader: - kv  33:            tokenizer.ggml.padding_token_id u32              = 100001
llama_model_loader: - kv  34:               tokenizer.ggml.add_bos_token bool             = true
llama_model_loader: - kv  35:               tokenizer.ggml.add_eos_token bool             = false
llama_model_loader: - kv  36:                    tokenizer.chat_template str              = {% if not add_generation_prompt is de...
llama_model_loader: - kv  37:               general.quantization_version u32              = 2
llama_model_loader: - type  f32:  108 tensors
llama_model_loader: - type q8_0:  269 tensors
llm_load_vocab: special tokens cache size = 2400
llm_load_vocab: token to piece cache size = 0.6661 MB
llm_load_print_meta: format           = GGUF V3 (latest)
llm_load_print_meta: arch             = deepseek2
llm_load_print_meta: vocab type       = BPE
llm_load_print_meta: n_vocab          = 102400
llm_load_print_meta: n_merges         = 99757
llm_load_print_meta: vocab_only       = 0
llm_load_print_meta: n_ctx_train      = 163840
llm_load_print_meta: n_embd           = 2048
llm_load_print_meta: n_layer          = 27
llm_load_print_meta: n_head           = 16
llm_load_print_meta: n_head_kv        = 16
llm_load_print_meta: n_rot            = 64
llm_load_print_meta: n_swa            = 0
llm_load_print_meta: n_embd_head_k    = 192
llm_load_print_meta: n_embd_head_v    = 128
llm_load_print_meta: n_gqa            = 1
llm_load_print_meta: n_embd_k_gqa     = 3072
llm_load_print_meta: n_embd_v_gqa     = 2048
llm_load_print_meta: f_norm_eps       = 0.0e+00
llm_load_print_meta: f_norm_rms_eps   = 1.0e-06
llm_load_print_meta: f_clamp_kqv      = 0.0e+00
llm_load_print_meta: f_max_alibi_bias = 0.0e+00
llm_load_print_meta: f_logit_scale    = 0.0e+00
llm_load_print_meta: n_ff             = 10944
llm_load_print_meta: n_expert         = 64
llm_load_print_meta: n_expert_used    = 6
llm_load_print_meta: causal attn      = 1
llm_load_print_meta: pooling type     = 0
llm_load_print_meta: rope type        = 0
llm_load_print_meta: rope scaling     = yarn
llm_load_print_meta: freq_base_train  = 10000.0
llm_load_print_meta: freq_scale_train = 0.025
llm_load_print_meta: n_ctx_orig_yarn  = 4096
llm_load_print_meta: rope_finetuned   = unknown
llm_load_print_meta: ssm_d_conv       = 0
llm_load_print_meta: ssm_d_inner      = 0
llm_load_print_meta: ssm_d_state      = 0

llm_load_print_meta: ssm_dt_rank      = 0
llm_load_print_meta: model type       = 16B
llm_load_print_meta: model ftype      = Q8_0
llm_load_print_meta: model params     = 15.71 B
llm_load_print_meta: model size       = 15.55 GiB (8.51 BPW)
llm_load_print_meta: general.name     = DeepSeek-Coder-V2-Lite-Instruct
llm_load_print_meta: BOS token        = 100000 '<?~\begin?~V~Aof?~V~Asentence?~\>'
llm_load_print_meta: EOS token        = 100001 '<?~\end?~V~Aof?~V~Asentence?~\>'
llm_load_print_meta: PAD token        = 100001 '<?~\end?~V~Aof?~V~Asentence?~\>'
llm_load_print_meta: LF token         = 126 '?~D'
llm_load_print_meta: max token length = 256
llm_load_print_meta: n_layer_dense_lead   = 1
llm_load_print_meta: n_lora_q             = 0
llm_load_print_meta: n_lora_kv            = 512
llm_load_print_meta: n_ff_exp             = 1408
llm_load_print_meta: n_expert_shared      = 2
llm_load_print_meta: expert_weights_scale = 1.0
llm_load_print_meta: rope_yarn_log_mul    = 0.0707
ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
ggml_cuda_init: found 1 CUDA devices:
  Device 0: NVIDIA GeForce RTX 4090, compute capability 8.9, VMM: yes
llm_load_tensors: ggml ctx size =    0.32 MiB
llm_load_tensors: offloading 27 repeating layers to GPU
llm_load_tensors: offloading non-repeating layers to GPU
llm_load_tensors: offloaded 28/28 layers to GPU
llm_load_tensors:        CPU buffer size =   212.50 MiB
llm_load_tensors:      CUDA0 buffer size = 15712.47 MiB
.......................................................................................
llama_new_context_with_model: n_ctx      = 24608
llama_new_context_with_model: n_batch    = 2048
llama_new_context_with_model: n_ubatch   = 512
llama_new_context_with_model: flash_attn = 0
llama_new_context_with_model: freq_base  = 10000.0
llama_new_context_with_model: freq_scale = 0.025
llama_kv_cache_init:      CUDA0 KV buffer size =  6488.44 MiB
llama_new_context_with_model: KV self size  = 6488.44 MiB, K (f16): 3893.06 MiB, V (f16): 2595.38 MiB
llama_new_context_with_model:  CUDA_Host  output buffer size =     1.56 MiB
llama_new_context_with_model:      CUDA0 compute buffer size =   841.07 MiB
llama_new_context_with_model:  CUDA_Host compute buffer size =    52.07 MiB
llama_new_context_with_model: graph nodes  = 1924
llama_new_context_with_model: graph splits = 2

heyjohnlim avatar Aug 05 '24 04:08 heyjohnlim

This is expected because DS2 uses MLA which prevents from applying context shifts. So you have to stay within the training context of the model, otherwise you will get this error

ggerganov avatar Aug 05 '24 06:08 ggerganov

Thanks for the prompt answer. Much appreciated. I am not sure what setting to tune. I changed --predict -2 to --predict 4096. This seems to help as i cannot reproduce the problem now when i stress test.

heyjohnlim avatar Aug 05 '24 10:08 heyjohnlim

Roughly speaking, the context length per sequence/client is:

-c 24600
--parallel 3

context per client = 24600 / 3 = 8200

So any generation that exceeds 8200 tokens (prompt + generated text) will cause a context shift and thus the error. By limiting --predict to 4096, you guarantee that you will always stay inside the allocated context. You can even reduce the -c to 3*4096 = 12288 - this will save you some VRAM and will still work without errors

ggerganov avatar Aug 05 '24 12:08 ggerganov

I'm confused

 ollama run --verbose deepseek-coder-v2:16b-lite-instruct-q8_0
>>> /show info
  Model                         
  	arch            	deepseek2	     
  	parameters      	15.7B    	     
  	quantization    	Q8_0     	     
  	context length  	163840   	     
  	embedding length	2048     

It looks like the training context length is 163k. How can it be trying a context shift at a mere 8k or 4k of context?

devlux76 avatar Aug 11 '24 11:08 devlux76

Ok so I figured this out on my own with a little help from deepseek-coder-v2:16b-lite-instruct-q8_0.

The context length reported is the maximum length the model can support even in theory. Deepseek folks designed it to go to 163k.

deepseek-v2 the big boy version was trained to 128k, they felt that bigger contexts would be worth training on later.

The light version was only trained to 32k.

num_ctx in Ollama is -c in llama.cpp. num_ctx needs to be 32k / num_processes.

If you set it in the model file to 32k it will provision 98304 in mem if you have 3 processes. That's a lot of memory to chew up.

There doesn't seem to be a hard limit in place though...

>>> /show info 
  Model                         
  	arch            	deepseek2	     
  	parameters      	15.7B    	     
  	quantization    	Q8_0     	     
  	context length  	163840   	     
  	embedding length	2048     	     
  	                              
  Parameters                    
  	num_ctx    	24576       	       
  	num_predict	8192 

As you can see 24576+8192 = 32k

total duration:       21.112569308s
load duration:        17.900183ms
prompt eval count:    12647 token(s)
prompt eval duration: 1.005556s
prompt eval rate:     12577.12 tokens/s
eval count:           71 token(s)
eval duration:        18.527142s
eval rate:            3.83 tokens/s

So that's the trick.

num_predict must be less than or equal to num_ctx / process count. If not then when the context grows beyond what's been provisioned the program hard exits. That feels like a bug to me. But apparently it's as designed.

devlux76 avatar Aug 11 '24 17:08 devlux76

Ok so I figured this out on my own with a little help from deepseek-coder-v2:16b-lite-instruct-q8_0.

The context length reported is the maximum length the model can support even in theory. Deepseek folks designed it to go to 163k.

deepseek-v2 the big boy version was trained to 128k, they felt that bigger contexts would be worth training on later.

The light version was only trained to 32k.

num_ctx in Ollama is -c in llama.cpp. num_ctx needs to be 32k / num_processes.

If you set it in the model file to 32k it will provision 98304 in mem if you have 3 processes. That's a lot of memory to chew up.

There doesn't seem to be a hard limit in place though...

>>> /show info 
  Model                         
  	arch            	deepseek2	     
  	parameters      	15.7B    	     
  	quantization    	Q8_0     	     
  	context length  	163840   	     
  	embedding length	2048     	     
  	                              
  Parameters                    
  	num_ctx    	24576       	       
  	num_predict	8192 

As you can see 24576+8192 = 32k

total duration:       21.112569308s
load duration:        17.900183ms
prompt eval count:    12647 token(s)
prompt eval duration: 1.005556s
prompt eval rate:     12577.12 tokens/s
eval count:           71 token(s)
eval duration:        18.527142s
eval rate:            3.83 tokens/s

So that's the trick.

num_predict must be less than or equal to num_ctx / process count. If not then when the context grows beyond what's been provisioned the program hard exits. That feels like a bug to me. But apparently it's as designed.

Im new to this stuff. How do i edit the context length? /set parameter num_ctx <any value> doesnt seem to work even when it says Set parameter 'num_ctx' to 'value'. Mine is:

        parameters              8.0B
        quantization            Q4_0
        arch                    llama
        context length          131072
        embedding length        4096```

theoneandonlyshadow avatar Sep 15 '24 23:09 theoneandonlyshadow

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Oct 30 '24 01:10 github-actions[bot]

Is there any way to fix this, I'm ready to work on this.

usernaamee avatar Jan 29 '25 15:01 usernaamee

I do this with a parameter over the openai api, just follow the openai docs for the REST API. It works.

devlux76 avatar Jan 30 '25 07:01 devlux76

Roughly speaking, the context length per sequence/client is:

-c 24600
--parallel 3

context per client = 24600 / 3 = 8200

So any generation that exceeds 8200 tokens (prompt + generated text) will cause a context shift and thus the error. By limiting --predict to 4096, you guarantee that you will always stay inside the allocated context. You can even reduce the -c to 3*4096 = 12288 - this will save you some VRAM and will still work without errors

I ever thought that context-length assigned should be for SOLO client, well, I'm wrong, hope that documantion / wiki can be updated to indicate this point .

simonchen avatar Feb 24 '25 07:02 simonchen