Repository: ollama
Issue: Uneven split across GPUs
Labels: bug,memory

When loading a model across 2 GPUs, the layers are split evenly, but the GPU memory usage is quite a bit higher on the first GPU:

```
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 3090        On  |   00000000:01:00.0 Off |                  N/A |
| 55%   58C    P0            188W /  275W |   23747MiB /  24576MiB |     27%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
|   1  NVIDIA GeForce RTX 3090        On  |   00000000:02:00.0 Off |                  N/A |
| 53%   49C    P0            179W /  275W |   22519MiB /  24576MiB |     26%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
```

This seems to be due do `CUDA0 compute buffer size` being ~1GB higher than the CUDA1 equivalent. When using text-generation-webui & llamacpp I'm able to specify a `50,51` split which results in the second GPU getting a layer or two more, thus balancing the memory usage, and allowing bigger models to run (or more layers to get offloaded). Does this exist? If not, is it possible?

<details>
<summary>Server log</summary>

```
Sep 30 15:00:25 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:25.780Z level=INFO source=memory.go:326 msg="offload to cuda" layers.requested=78 layers.model=81 layers.offload=70 layers.split=35,35 memory.available="[23.3 GiB 23.3 GiB]" memory.gpu_overhead="0 B" memory.required.full="52.4 GiB" memory.required.partial="45.9 GiB" memory.required.kv="5.0 GiB" memory.required.allocations="[23.0 GiB 22.9 GiB]" memory.weights.total="44.3 GiB" memory.weights.repeating="43.3 GiB" memory.weights.nonrepeating="974.6 MiB" memory.graph.full="2.6 GiB" memory.graph.partial="2.6 GiB"
Sep 30 15:00:25 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:25.783Z level=INFO source=server.go:388 msg="starting llama server" cmd="/usr/lib/ollama/runners/cuda_v12/ollama_llama_server --model /root/.ollama/models/blobs/sha256-c9ff230988a3c90f5beec5da2ebbd8b77d953389b587cb7398c6abd671b7562f --ctx-size 16384 --batch-size 512 --embedding --log-disable --n-gpu-layers 78 --flash-attn --parallel 1 --tensor-split 35,35 --port 37883"
Sep 30 15:00:25 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:25.783Z level=INFO source=sched.go:449 msg="loaded runners" count=1
Sep 30 15:00:25 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:25.783Z level=INFO source=server.go:587 msg="waiting for llama runner to start responding"
Sep 30 15:00:25 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:25.783Z level=INFO source=server.go:621 msg="waiting for server to become available" status="llm server error"
Sep 30 15:00:25 dying-love 6f929011afab[1234]: INFO [main] build info | build=10 commit="3f6ec33" tid="139699938238464" timestamp=1727708425
Sep 30 15:00:25 dying-love 6f929011afab[1234]: INFO [main] system info | n_threads=16 n_threads_batch=16 system_info="AVX = 1 | AVX_VNNI = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | AVX512_BF16 = 0 | FMA = 0 | NEON = 0 | SVE = 0 | ARM_FMA = 0 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | SSSE3 = 1 | VSX = 0 | MATMUL_INT8 = 0 | LLAMAFILE = 1 | " tid="139699938238464" timestamp=1727708425 total_threads=32
Sep 30 15:00:25 dying-love 6f929011afab[1234]: INFO [main] HTTP server listening | hostname="127.0.0.1" n_threads_http="31" port="37883" tid="139699938238464" timestamp=1727708425
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: loaded meta data with 35 key-value pairs and 963 tensors from /root/.ollama/models/blobs/sha256-c9ff230988a3c90f5beec5da2ebbd8b77d953389b587cb7398c6abd671b7562f (version GGUF V3 (latest))
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   0:                       general.architecture str              = qwen2
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   1:                               general.type str              = model
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   2:                               general.name str              = Qwen2.5 72B Instruct
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   3:                           general.finetune str              = Instruct
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   4:                           general.basename str              = Qwen2.5
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   5:                         general.size_label str              = 72B
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   6:                            general.license str              = other
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   7:                       general.license.name str              = qwen
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   8:                       general.license.link str              = https://huggingface.co/Qwen/Qwen2.5-7...
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv   9:                   general.base_model.count u32              = 1
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  10:                  general.base_model.0.name str              = Qwen2.5 72B
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  11:          general.base_model.0.organization str              = Qwen
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  12:              general.base_model.0.repo_url str              = https://huggingface.co/Qwen/Qwen2.5-72B
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  13:                               general.tags arr[str,2]       = ["chat", "text-generation"]
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  14:                          general.languages arr[str,1]       = ["en"]
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  15:                          qwen2.block_count u32              = 80
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  16:                       qwen2.context_length u32              = 32768
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  17:                     qwen2.embedding_length u32              = 8192
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  18:                  qwen2.feed_forward_length u32              = 29568
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  19:                 qwen2.attention.head_count u32              = 64
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  20:              qwen2.attention.head_count_kv u32              = 8
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  21:                       qwen2.rope.freq_base f32              = 1000000.000000
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  22:     qwen2.attention.layer_norm_rms_epsilon f32              = 0.000001
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  23:                          general.file_type u32              = 14
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  24:                       tokenizer.ggml.model str              = gpt2
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  25:                         tokenizer.ggml.pre str              = qwen2
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  26:                      tokenizer.ggml.tokens arr[str,152064]  = ["!", "\"", "#", "$", "%", "&", "'", ...
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  27:                  tokenizer.ggml.token_type arr[i32,152064]  = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  28:                      tokenizer.ggml.merges arr[str,151387]  = ["Ġ Ġ", "ĠĠ ĠĠ", "i n", "Ġ t",...
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  29:                tokenizer.ggml.eos_token_id u32              = 151645
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  30:            tokenizer.ggml.padding_token_id u32              = 151643
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  31:                tokenizer.ggml.bos_token_id u32              = 151643
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  32:               tokenizer.ggml.add_bos_token bool             = false
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  33:                    tokenizer.chat_template str              = {%- if tools %}\n    {{- '
...
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - kv  34:               general.quantization_version u32              = 2
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type  f32:  401 tensors
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type q5_0:   70 tensors
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type q5_1:   10 tensors
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type q4_K:  401 tensors
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type q5_K:   80 tensors
Sep 30 15:00:25 dying-love 6f929011afab[1234]: llama_model_loader: - type q6_K:    1 tensors
Sep 30 15:00:26 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:26.034Z level=INFO source=server.go:621 msg="waiting for server to become available" status="llm server loading model"
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_vocab: special tokens cache size = 22
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_vocab: token to piece cache size = 0.9310 MB
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: format           = GGUF V3 (latest)
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: arch             = qwen2
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: vocab type       = BPE
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_vocab          = 152064
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_merges         = 151387
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: vocab_only       = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_ctx_train      = 32768
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_embd           = 8192
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_layer          = 80
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_head           = 64
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_head_kv        = 8
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_rot            = 128
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_swa            = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_embd_head_k    = 128
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_embd_head_v    = 128
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_gqa            = 8
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_embd_k_gqa     = 1024
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_embd_v_gqa     = 1024
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: f_norm_eps       = 0.0e+00
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: f_norm_rms_eps   = 1.0e-06
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: f_clamp_kqv      = 0.0e+00
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: f_max_alibi_bias = 0.0e+00
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: f_logit_scale    = 0.0e+00
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_ff             = 29568
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_expert         = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_expert_used    = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: causal attn      = 1
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: pooling type     = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: rope type        = 2
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: rope scaling     = linear
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: freq_base_train  = 1000000.0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: freq_scale_train = 1
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: n_ctx_orig_yarn  = 32768
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: rope_finetuned   = unknown
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: ssm_d_conv       = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: ssm_d_inner      = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: ssm_d_state      = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: ssm_dt_rank      = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: ssm_dt_b_c_rms   = 0
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: model type       = 70B
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: model ftype      = Q4_K - Small
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: model params     = 72.71 B
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: model size       = 40.87 GiB (4.83 BPW)
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: general.name     = Qwen2.5 72B Instruct
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: BOS token        = 151643 '
'
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: EOS token        = 151645 '
'
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: PAD token        = 151643 '
'
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: LF token         = 148848 'ÄĬ'
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: EOT token        = 151645 '
'
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_print_meta: max token length = 256
Sep 30 15:00:26 dying-love 6f929011afab[1234]: ggml_cuda_init: GGML_CUDA_FORCE_MMQ:    no
Sep 30 15:00:26 dying-love 6f929011afab[1234]: ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no
Sep 30 15:00:26 dying-love 6f929011afab[1234]: ggml_cuda_init: found 2 CUDA devices:
Sep 30 15:00:26 dying-love 6f929011afab[1234]:   Device 0: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes
Sep 30 15:00:26 dying-love 6f929011afab[1234]:   Device 1: NVIDIA GeForce RTX 3090, compute capability 8.6, VMM: yes
Sep 30 15:00:26 dying-love 6f929011afab[1234]: llm_load_tensors: ggml ctx size =    1.27 MiB
Sep 30 15:00:27 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:27.490Z level=INFO source=server.go:621 msg="waiting for server to become available" s>
Sep 30 15:00:29 dying-love 6f929011afab[1234]: llm_load_tensors: offloading 78 repeating layers to GPU
Sep 30 15:00:29 dying-love 6f929011afab[1234]: llm_load_tensors: offloaded 78/81 layers to GPU
Sep 30 15:00:29 dying-love 6f929011afab[1234]: llm_load_tensors:        CPU buffer size = 41850.31 MiB
Sep 30 15:00:29 dying-love 6f929011afab[1234]: llm_load_tensors:      CUDA0 buffer size = 19646.28 MiB
Sep 30 15:00:29 dying-love 6f929011afab[1234]: llm_load_tensors:      CUDA1 buffer size = 19530.78 MiB
Sep 30 15:00:29 dying-love 6f929011afab[1234]: time=2024-09-30T15:00:29.545Z level=INFO source=server.go:621 msg="waiting for server to become available" s>
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: n_ctx      = 16384
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: n_batch    = 512
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: n_ubatch   = 512
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: flash_attn = 1
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: freq_base  = 1000000.0
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: freq_scale = 1
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_kv_cache_init:  CUDA_Host KV buffer size =   128.00 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_kv_cache_init:      CUDA0 KV buffer size =  2496.00 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_kv_cache_init:      CUDA1 KV buffer size =  2496.00 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: KV self size  = 5120.00 MiB, K (f16): 2560.00 MiB, V (f16): 2560.00 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model:  CUDA_Host  output buffer size =     0.61 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model:      CUDA0 compute buffer size =  1287.53 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model:      CUDA1 compute buffer size =   163.50 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model:  CUDA_Host compute buffer size =    48.01 MiB
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: graph nodes  = 2487
Sep 30 15:00:32 dying-love 6f929011afab[1234]: llama_new_context_with_model: graph splits = 33
```

</details>