Debian 12 bookworm
Sponsored Link

llama.cpp : インストール2024/02/22

 

Meta の Llama (Large Language Model Meta AI) モデルのインターフェースである [llama.cpp] をインストールします。

[1]

こちらを参考に CUDA をインストールしておきます

[2] その他必要なパッケージをインストールしておきます。
root@dlp:~#
apt -y install nvidia-cudnn gcc g++ cmake ccache python3-numpy curl git jq

[3] [llama-cpp] をビルドします。
debian@dlp:~$
git clone https://github.com/ggerganov/llama.cpp

Cloning into 'llama.cpp'...
remote: Enumerating objects: 18978, done.
remote: Counting objects: 100% (6489/6489), done.
remote: Compressing objects: 100% (540/540), done.
remote: Total 18978 (delta 6260), reused 5989 (delta 5947), pack-reused 12489
Receiving objects: 100% (18978/18978), 21.49 MiB | 19.02 MiB/s, done.
Resolving deltas: 100% (13356/13356), done.

debian@dlp:~$
cd llama.cpp

debian@dlp:~/llama.cpp$
make LLAMA_CUBLAS=1
# * CPU のみで実行するバイナリをビルドする場合はオプション無しの [make] のみで実行

[4]

GGML 形式のモデルをダウンロードして GGUF 形式に変換し、[llama-cpp] を起動します。
モデルは下記サイトからダウンロードできます。当例では [llama-2-13b-chat.ggmlv3.q8_0.bin] を使用します。

⇒ https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/tree/main
⇒ https://huggingface.co/TheBloke/Llama-2-13B-chat-GGML/tree/main
⇒ https://huggingface.co/TheBloke/Llama-2-70B-Chat-GGML/tree/main
debian@dlp:~/llama.cpp$
curl -LO https://huggingface.co/TheBloke/Llama-2-13B-chat-GGML/resolve/main/llama-2-13b-chat.ggmlv3.q8_0.bin?download=true
# GGUF に変換

debian@dlp:~/llama.cpp$
python3 ./convert-llama-ggml-to-gguf.py --input ./llama-2-13b-chat.ggmlv3.q8_0.bin --output ./llama-2-13b-chat.ggmlv3.q8_0.gguf

.....
.....
* Preparing to save GGUF file
gguf: This GGUF file is for Little Endian only
* Adding model parameters and KV items
* Adding 32000 vocab item(s)
* Adding 363 tensor(s)
    gguf: write header
    gguf: write metadata
    gguf: write tensors
* Successful completion. Output saved to: llama-2-13b-chat.ggmlv3.q8_0.gguf

# [--n-gpu-layers] : GPU に配置するレイヤーの数
# -- よくわからない場合は [-1] を指定

debian@dlp:~/llama.cpp$
./server --model ./llama-2-13b-chat.ggmlv3.q8_0.gguf --n-gpu-layers -1 --host 0.0.0.0 --port 8000 &

ggml_init_cublas: GGML_CUDA_FORCE_MMQ:   no
ggml_init_cublas: CUDA_USE_TENSOR_CORES: yes
ggml_init_cublas: found 1 CUDA devices:
  Device 0: NVIDIA GeForce RTX 3060, compute capability 8.6, VMM: yes
{"timestamp":1708566953,"level":"INFO","function":"main","line":2573,"message":"build info","build":2234,"commit":"973053d8"}
{"timestamp":1708566953,"level":"INFO","function":"main","line":2576,"message":"system info","n_threads":4,"n_threads_batch":-1,"total_threads":8,"system_info":"AVX = 1 | AVX_VNNI = 0 | AVX2 = 1 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | FMA = 1 | NEON = 0 | ARM_FMA = 0 | F16C = 1 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 1 | SSE3 = 1 | SSSE3 = 1 | VSX = 0 | MATMUL_INT8 = 0 | "}

llama server listening at http://0.0.0.0:8000

{"timestamp":1708566953,"level":"INFO","function":"main","line":2731,"message":"HTTP server listening","port":"8000","hostname":"0.0.0.0"}
llama_model_loader: loaded meta data with 19 key-value pairs and 363 tensors from ./llama-2-13b-chat.ggmlv3.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              = llama
llama_model_loader: - kv   1:                               general.name str              = llama-2-13b-chat.ggmlv3.q8_0.bin
llama_model_loader: - kv   2:                        general.description str              = converted from legacy GGJTv3 MOSTLY_Q...
llama_model_loader: - kv   3:                          general.file_type u32              = 7
llama_model_loader: - kv   4:                       llama.context_length u32              = 2048
llama_model_loader: - kv   5:                     llama.embedding_length u32              = 5120
llama_model_loader: - kv   6:                          llama.block_count u32              = 40
llama_model_loader: - kv   7:                  llama.feed_forward_length u32              = 13824
llama_model_loader: - kv   8:                 llama.rope.dimension_count u32              = 128
llama_model_loader: - kv   9:                 llama.attention.head_count u32              = 40
llama_model_loader: - kv  10:              llama.attention.head_count_kv u32              = 40
llama_model_loader: - kv  11:     llama.attention.layer_norm_rms_epsilon f32              = 0.000005
llama_model_loader: - kv  12:                       tokenizer.ggml.model str              = llama
llama_model_loader: - kv  13:                      tokenizer.ggml.tokens arr[str,32000]   = ["<unk>", "<s>", "</s>", "<0x00>", "<...
llama_model_loader: - kv  14:                      tokenizer.ggml.scores arr[f32,32000]   = [0.000000, 0.000000, 0.000000, 0.0000...
llama_model_loader: - kv  15:                  tokenizer.ggml.token_type arr[i32,32000]   = [2, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, ...
llama_model_loader: - kv  16:            tokenizer.ggml.unknown_token_id u32              = 0
llama_model_loader: - kv  17:                tokenizer.ggml.bos_token_id u32              = 1
llama_model_loader: - kv  18:                tokenizer.ggml.eos_token_id u32              = 2
llama_model_loader: - type  f32:   81 tensors
llama_model_loader: - type q8_0:  282 tensors
llm_load_vocab: special tokens definition check successful ( 259/32000 ).
llm_load_print_meta: format           = GGUF V3 (latest)
llm_load_print_meta: arch             = llama
llm_load_print_meta: vocab type       = SPM
llm_load_print_meta: n_vocab          = 32000
llm_load_print_meta: n_merges         = 0
llm_load_print_meta: n_ctx_train      = 2048
llm_load_print_meta: n_embd           = 5120
llm_load_print_meta: n_head           = 40
llm_load_print_meta: n_head_kv        = 40
llm_load_print_meta: n_layer          = 40
llm_load_print_meta: n_rot            = 128
llm_load_print_meta: n_embd_head_k    = 128
llm_load_print_meta: n_embd_head_v    = 128
llm_load_print_meta: n_gqa            = 1
llm_load_print_meta: n_embd_k_gqa     = 5120
llm_load_print_meta: n_embd_v_gqa     = 5120
llm_load_print_meta: f_norm_eps       = 0.0e+00
llm_load_print_meta: f_norm_rms_eps   = 5.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: n_ff             = 13824
llm_load_print_meta: n_expert         = 0
llm_load_print_meta: n_expert_used    = 0
llm_load_print_meta: rope scaling     = linear
llm_load_print_meta: freq_base_train  = 10000.0
llm_load_print_meta: freq_scale_train = 1
llm_load_print_meta: n_yarn_orig_ctx  = 2048
llm_load_print_meta: rope_finetuned   = unknown
llm_load_print_meta: model type       = 13B
llm_load_print_meta: model ftype      = Q8_0
llm_load_print_meta: model params     = 13.02 B
llm_load_print_meta: model size       = 12.88 GiB (8.50 BPW)
llm_load_print_meta: general.name     = llama-2-13b-chat.ggmlv3.q8_0.bin
llm_load_print_meta: BOS token        = 1 '<s>'
llm_load_print_meta: EOS token        = 2 '</s>'
llm_load_print_meta: UNK token        = 0 '<unk>'
llm_load_print_meta: LF token         = 13 '<0x0A>'
llm_load_tensors: ggml ctx size =    0.14 MiB
llm_load_tensors: offloading 0 repeating layers to GPU
llm_load_tensors: offloaded 0/41 layers to GPU
llm_load_tensors:        CPU buffer size = 13189.86 MiB
....................................................................................................
llama_new_context_with_model: n_ctx      = 512
llama_new_context_with_model: freq_base  = 10000.0
llama_new_context_with_model: freq_scale = 1
llama_kv_cache_init:  CUDA_Host KV buffer size =   400.00 MiB
llama_new_context_with_model: KV self size  =  400.00 MiB, K (f16):  200.00 MiB, V (f16):  200.00 MiB
llama_new_context_with_model:  CUDA_Host input buffer size   =    12.01 MiB
llama_new_context_with_model:  CUDA_Host compute buffer size =    80.00 MiB
llama_new_context_with_model: graph splits (measure): 1
Available slots:
 -> Slot 0 - max context: 512
{"timestamp":1708566955,"level":"INFO","function":"main","line":2752,"message":"model loaded"}
all slots are idle and system prompt is empty, clear the KV cache
[5] 簡単な質問を投入して動作確認します。
質問の内容や使用しているモデルによって、応答時間や応答内容は変わります。
ちなみに、当例では、8 vCPU + 16G メモリ + GeForce RTX 3060 (12G) のマシンで実行しています。
# 球の体積を求める公式を教えて

debian@dlp:~/llama.cpp$
curl -s -XPOST -H 'Content-Type: application/json' localhost:8000/v1/chat/completions \
-d '{"messages": [{"role": "user", "content": "Tell me the formula to calculate the volume of a sphere."}]}' | jq | sed -e 's/\\n/\n/g'


print_timings: prompt eval time =    3530.30 ms /    40 tokens (   88.26 ms per token,    11.33 tokens per second)
print_timings:        eval time =   50448.40 ms /    79 runs   (  638.59 ms per token,     1.57 tokens per second)
print_timings:       total time =   53978.71 ms
slot 0 released (119 tokens in cache)
{"timestamp":1708568316,"level":"INFO","function":"log_server_request","line":2510,"message":"request","remote_addr":"127.0.0.1","remote_port":38516,"status":200,"method":"POST","path":"/v1/chat/completions","params":{}}
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Certainly! The formula to calculate the volume of a sphere is:

V = (4/3)πr³

Where V is the volume of the sphere, and r is the radius of the sphere.

Would you like me to explain how this formula works or would you like to try solving some problems using it?",
        "role": "assistant"
      }
    }
  ],
  "created": 1708568316,
  "id": "chatcmpl-bxLY6rzxnoAKYraeTLAgDu8OQxngdNyA",
  "model": "unknown",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 79,
    "prompt_tokens": 40,
    "total_tokens": 119
  }
}
関連コンテンツ