快速开始

备注

阅读本篇前,请确保已按照 安装指南 准备好昇腾环境及 llama.cpp !

本教程聚焦大语言模型(Large Language Model,LLM)的推理过程,以 Qwen2.5-7B 模型为例,讲述如何使用 llama.cpp 在昇腾 NPU 上进行推理。

模型文件准备及量化

llama.cpp 的推理需要使用 gguf 格式文件,llama.cpp 提供了两种方式转换 Hugging Face 模型文件:

  • 使用 GGUF-my-repo 将模型进行转换。

  • 使用项目中的 convert_hf_to_gguf.py 文件将 Hugging Face 模型转换为 gguf 格式:

    1python convert_hf_to_gguf.py path/to/model
    

详情请参考 Prepare and Quantize

注意:目前仅支持 FP16 精度及 Q4_0/Q8_0 量化模型。

推理

有两种设备选择模式:

  • 单设备:使用用户指定的一个设备目标。

  • 多设备:自动选择具有相同后端的设备。

设备选择

参数

单设备

--split-mode none --main-gpu DEVICE_ID

多设备

--split-mode layer (default)

使用单卡推理

1./build/bin/llama-cli -m path_to_model -p "Building a website can be done in 10 simple steps:" -n 400 -e -ngl 33 -sm none -mg 0

使用多卡推理

1./build/bin/llama-cli -m path_to_model -p "Building a website can be done in 10 simple steps:" -n 400 -e -ngl 33 -sm layer

以下为正常推理结果:

  1Log start
  2main: build = 3520 (8e707118)
  3main: built with cc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 for aarch64-linux-gnu
  4main: seed  = 1728907816
  5llama_model_loader: loaded meta data with 22 key-value pairs and 291 tensors from /home/jiahao/models/llama3-8b-instruct-fp16.gguf (version GGUF V3 (latest))
  6llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
  7llama_model_loader: - kv   0:                       general.architecture str              = llama
  8llama_model_loader: - kv   1:                               general.name str              = Meta-Llama-3-8B-Instruct
  9llama_model_loader: - kv   2:                          llama.block_count u32              = 32
 10llama_model_loader: - kv   3:                       llama.context_length u32              = 8192
 11llama_model_loader: - kv   4:                     llama.embedding_length u32              = 4096
 12llama_model_loader: - kv   5:                  llama.feed_forward_length u32              = 14336
 13llama_model_loader: - kv   6:                 llama.attention.head_count u32              = 32
 14llama_model_loader: - kv   7:              llama.attention.head_count_kv u32              = 8
 15llama_model_loader: - kv   8:                       llama.rope.freq_base f32              = 500000.000000
 16llama_model_loader: - kv   9:     llama.attention.layer_norm_rms_epsilon f32              = 0.000010
 17llama_model_loader: - kv  10:                          general.file_type u32              = 1
 18llama_model_loader: - kv  11:                           llama.vocab_size u32              = 128256
 19llama_model_loader: - kv  12:                 llama.rope.dimension_count u32              = 128
 20llama_model_loader: - kv  13:                       tokenizer.ggml.model str              = gpt2
 21llama_model_loader: - kv  14:                         tokenizer.ggml.pre str              = llama-bpe
 22llama_model_loader: - kv  15:                      tokenizer.ggml.tokens arr[str,128256]  = ["!", "\"", "#", "$", "%", "&", "'", ...
 23llama_model_loader: - kv  16:                  tokenizer.ggml.token_type arr[i32,128256]  = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
 24llama_model_loader: - kv  17:                      tokenizer.ggml.merges arr[str,280147]  = ["Ġ Ġ", "Ġ ĠĠĠ", "ĠĠ ĠĠ", "...
 25llama_model_loader: - kv  18:                tokenizer.ggml.bos_token_id u32              = 128000
 26llama_model_loader: - kv  19:                tokenizer.ggml.eos_token_id u32              = 128009
 27llama_model_loader: - kv  20:                    tokenizer.chat_template str              = {% set loop_messages = messages %}{% ...
 28llama_model_loader: - kv  21:               general.quantization_version u32              = 2
 29llama_model_loader: - type  f32:   65 tensors
 30llama_model_loader: - type  f16:  226 tensors
 31llm_load_vocab: special tokens cache size = 256
 32llm_load_vocab: token to piece cache size = 0.8000 MB
 33llm_load_print_meta: format           = GGUF V3 (latest)
 34llm_load_print_meta: arch             = llama
 35llm_load_print_meta: vocab type       = BPE
 36llm_load_print_meta: n_vocab          = 128256
 37llm_load_print_meta: n_merges         = 280147
 38llm_load_print_meta: vocab_only       = 0
 39llm_load_print_meta: n_ctx_train      = 8192
 40llm_load_print_meta: n_embd           = 4096
 41llm_load_print_meta: n_layer          = 32
 42llm_load_print_meta: n_head           = 32
 43llm_load_print_meta: n_head_kv        = 8
 44llm_load_print_meta: n_rot            = 128
 45llm_load_print_meta: n_swa            = 0
 46llm_load_print_meta: n_embd_head_k    = 128
 47llm_load_print_meta: n_embd_head_v    = 128
 48llm_load_print_meta: n_gqa            = 4
 49llm_load_print_meta: n_embd_k_gqa     = 1024
 50llm_load_print_meta: n_embd_v_gqa     = 1024
 51llm_load_print_meta: f_norm_eps       = 0.0e+00
 52llm_load_print_meta: f_norm_rms_eps   = 1.0e-05
 53llm_load_print_meta: f_clamp_kqv      = 0.0e+00
 54llm_load_print_meta: f_max_alibi_bias = 0.0e+00
 55llm_load_print_meta: f_logit_scale    = 0.0e+00
 56llm_load_print_meta: n_ff             = 14336
 57llm_load_print_meta: n_expert         = 0
 58llm_load_print_meta: n_expert_used    = 0
 59llm_load_print_meta: causal attn      = 1
 60llm_load_print_meta: pooling type     = 0
 61llm_load_print_meta: rope type        = 0
 62llm_load_print_meta: rope scaling     = linear
 63llm_load_print_meta: freq_base_train  = 500000.0
 64llm_load_print_meta: freq_scale_train = 1
 65llm_load_print_meta: n_ctx_orig_yarn  = 8192
 66llm_load_print_meta: rope_finetuned   = unknown
 67llm_load_print_meta: ssm_d_conv       = 0
 68llm_load_print_meta: ssm_d_inner      = 0
 69llm_load_print_meta: ssm_d_state      = 0
 70llm_load_print_meta: ssm_dt_rank      = 0
 71llm_load_print_meta: model type       = 8B
 72llm_load_print_meta: model ftype      = F16
 73llm_load_print_meta: model params     = 8.03 B
 74llm_load_print_meta: model size       = 14.96 GiB (16.00 BPW)
 75llm_load_print_meta: general.name     = Meta-Llama-3-8B-Instruct
 76llm_load_print_meta: BOS token        = 128000 '<|begin_of_text|>'
 77llm_load_print_meta: EOS token        = 128009 '<|eot_id|>'
 78llm_load_print_meta: LF token         = 128 'Ä'
 79llm_load_print_meta: EOT token        = 128009 '<|eot_id|>'
 80llm_load_print_meta: max token length = 256
 81llm_load_tensors: ggml ctx size =    0.27 MiB
 82llm_load_tensors:        CPU buffer size = 15317.02 MiB
 83llm_load_tensors:       CANN buffer size = 13313.00 MiB
 84.........................................................................................
 85llama_new_context_with_model: n_ctx      = 8192
 86llama_new_context_with_model: n_batch    = 2048
 87llama_new_context_with_model: n_ubatch   = 512
 88llama_new_context_with_model: flash_attn = 0
 89llama_new_context_with_model: freq_base  = 500000.0
 90llama_new_context_with_model: freq_scale = 1
 91llama_kv_cache_init:       CANN KV buffer size =  1024.00 MiB
 92llama_new_context_with_model: KV self size  = 1024.00 MiB, K (f16):  512.00 MiB, V (f16):  512.00 MiB
 93llama_new_context_with_model:        CPU  output buffer size =     0.49 MiB
 94llama_new_context_with_model:       CANN compute buffer size =  1260.50 MiB
 95llama_new_context_with_model:        CPU compute buffer size =    24.01 MiB
 96llama_new_context_with_model: graph nodes  = 1030
 97llama_new_context_with_model: graph splits = 4
 98
 99system_info: n_threads = 192 / 192 | AVX = 0 | AVX_VNNI = 0 | AVX2 = 0 | AVX512 = 0 | AVX512_VBMI = 0 | AVX512_VNNI = 0 | AVX512_BF16 = 0 | FMA = 0 | NEON = 1 | SVE = 0 | ARM_FMA = 1 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 0 | SSSE3 = 0 | VSX = 0 | MATMUL_INT8 = 0 | LLAMAFILE = 1 |
100sampling:
101    repeat_last_n = 64, repeat_penalty = 1.000, frequency_penalty = 0.000, presence_penalty = 0.000
102    top_k = 40, tfs_z = 1.000, top_p = 0.950, min_p = 0.050, typical_p = 1.000, temp = 0.800
103    mirostat = 0, mirostat_lr = 0.100, mirostat_ent = 5.000
104sampling order:
105CFG -> Penalties -> top_k -> tfs_z -> typical_p -> top_p -> min_p -> temperature
106generate: n_ctx = 8192, n_batch = 2048, n_predict = -1, n_keep = 1
107
108
109Building a website can be done in 10 simple steps: 1. Define your website's purpose and target audience 2. Choose a domain name and register it with a registrar 3. Select a web hosting service and set up your hosting account 4. Design your website's layout and structure 5. Create content for your website, including text, images, and other media 6. Build a responsive website design that adapts to different devices and screen sizes 7. Choose a Content Management System (CMS) and install it on your website 8. Customize your website's design and layout using a CMS
110
111llama_print_timings:        load time =    9074.69 ms
112llama_print_timings:      sample time =      31.97 ms /   112 runs   (    0.29 ms per token,  3503.28 tokens per second)
113llama_print_timings: prompt eval time =     238.53 ms /    13 tokens (   18.35 ms per token,    54.50 tokens per second)
114llama_print_timings:        eval time =   13152.29 ms /   111 runs   (  118.49 ms per token,     8.44 tokens per second)
115llama_print_timings:       total time =   13623.53 ms /   124 tokens