DeepSeek模子量化

打印 上一主题 下一主题

主题 891|帖子 891|积分 2673

技术配景


大语言模子(Large Language Model,LLM),可以通过量化(Quantization)操作来节流内存/显存的使用,而且降低了通讯开销,进而到达加速模子推理的效果。常见的就是把Float16的浮点数,转换成低精度的整数,比方Int4整数。最极限的情况下,可以把参数转化成二值Bool变量,也就是只有0和1,但是这种大幅度的量化有可能导致模子的推理效果不佳。常用的是,在70B以下的模子用Q8,70B以上可以用Q4。具体的原理,包罗对称量化和非对称量化等,这里就不作介绍了,主要看看工程上怎么实现,主要使用了llama.cpp来完成量化。

安装llama.cpp


这里我们在Ubuntu上使用本地编译构建的方法举行安装,首先从github上面clone下来:

  1. $ git clone https://github.com/ggerganov/llama.cpp.git
  2. 正克隆到 'llama.cpp'...
  3. remote: Enumerating objects: 43657, done.
  4. remote: Counting objects: 100% (15/15), done.
  5. remote: Compressing objects: 100% (14/14), done.
  6. remote: Total 43657 (delta 3), reused 5 (delta 1), pack-reused 43642 (from 3)
  7. 接收对象中: 100% (43657/43657), 88.26 MiB | 8.30 MiB/s, 完成.
  8. 处理 delta 中: 100% (31409/31409), 完成.
复制代码

最好创建一个虚拟情况,以避免各种软件依赖的问题,保举Python3.10:

  1. # 创建虚拟环境
  2. $ conda create -n llama python=3.10
  3. # 激活虚拟环境
  4. $ conda activate llama
复制代码

进入下载好的llama.cpp路径,安装所有的依赖项:

  1. $ cd llama.cpp/
  2. $ python3 -m pip install -e .
复制代码

创建一个编译目次,执行编译指令:

  1. $ mkdir build
  2. $ cd build/
  3. $ cmake ..
  4. -- The C compiler identification is GNU 7.5.0
  5. -- The CXX compiler identification is GNU 9.4.0
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info - done
  8. -- Check for working C compiler: /usr/bin/cc - skipped
  9. -- Detecting C compile features
  10. -- Detecting C compile features - done
  11. -- Detecting CXX compiler ABI info
  12. -- Detecting CXX compiler ABI info - done
  13. -- Check for working CXX compiler: /usr/bin/c++ - skipped
  14. -- Detecting CXX compile features
  15. -- Detecting CXX compile features - done
  16. -- Found Git: /usr/bin/git (found version "2.25.1")
  17. -- Looking for pthread.h
  18. -- Looking for pthread.h - found
  19. -- Performing Test CMAKE_HAVE_LIBC_PTHREAD
  20. -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
  21. -- Check if compiler accepts -pthread
  22. -- Check if compiler accepts -pthread - yes
  23. -- Found Threads: TRUE  
  24. -- Warning: ccache not found - consider installing it for faster compilation or disable this warning with GGML_CCACHE=OFF
  25. -- CMAKE_SYSTEM_PROCESSOR: x86_64
  26. -- Including CPU backend
  27. -- Found OpenMP_C: -fopenmp (found version "4.5")
  28. -- Found OpenMP_CXX: -fopenmp (found version "4.5")
  29. -- Found OpenMP: TRUE (found version "4.5")  
  30. -- x86 detected
  31. -- Adding CPU backend variant ggml-cpu: -march=native
  32. -- Configuring done
  33. -- Generating done
  34. -- Build files have been written to: /datb/DeepSeek/llama/llama.cpp/build
  35. $ cmake --build . --config Release
  36. Scanning dependencies of target ggml-base
  37. [  0%] Building C object ggml/src/CMakeFiles/ggml-base.dir/ggml.c.o
  38. [  1%] Building C object ggml/src/CMakeFiles/ggml-base.dir/ggml-alloc.c.o
  39. [100%] Linking CXX executable ../../bin/llama-vdot
  40. [100%] Built target llama-vdot
复制代码

到这里,就成功构建了cpu版本的llama.cpp,可以直接使用了。如果需要安装gpu加速的版本,可以参考下面这一末节,如果嫌麻烦发起直接跳过。

llama.cpp之CUDA加速


安装GPU版本llama.cpp需要先安装一些依赖:

  1. $ sudo apt install curl libcurl4-openssl-dev
复制代码

跟cpu版本差别的地方,主要在于cmake的编译指令(如果已经编译了cpu的版本,最好先清空build路径下的文件):

  1. $ cmake .. -DCMAKE_CUDA_COMPILER=/usr/bin/nvcc -DGGML_CUDA=ON -DBUILD_SHARED_LIBS=OFF -DLLAMA_CURL=ON -DCMAKE_CUDA_STANDARD=17
复制代码

这里加的一个FLAG:-DCMAKE_CUDA_STANDARD=17可以办理Llama.cpp仓库里面的Issue,如果不加这个Flag,有可能出现下面这种报错:

  1. Make Error in ggml/src/ggml-cuda/CMakeLists.txt:
  2.   Target "ggml-cuda" requires the language dialect "CUDA17" (with compiler
  3.   extensions), but CMake does not know the compile flags to use to enable it.
复制代码

如果顺遂的话,执行下面这个指令,成功编译通过的话就是成功了:

  1. $ cmake --build . --config Release
复制代码

但是如果像我如许有报错信息,那就得单独处理以下。

  1. /datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/vendors/cuda.h:6:10: fatal error: cuda_bf16.h: 没有那个文件或目录
  2. #include <cuda_bf16.h>
  3.           ^~~~~~~~~~~~~
  4. compilation terminated.
复制代码

这个报错是说找不到头文件,于是在情况里面find / -name cuda_bf16.h了一下,发现实在是有这个头文件的:

  1. /home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/nvidia/cuda_runtime/include/cuda_bf16.h
  2. /home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/triton/backends/nvidia/include/cuda_bf16.h
复制代码

处理方式是把这个路径加到CPATH里面:

  1. $ export CPATH=$CPATH:/home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/nvidia/cuda_runtime/include/
复制代码

如果是出现这个报错:

  1. /home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/nvidia/cuda_runtime/include/cuda_fp16.h:4100:10: fatal error: nv/target: 没有那个文件或目录
  2. #include <nv/target>
  3.           ^~~~~~~~~~~
  4. compilation terminated.
复制代码

那就是找不到target目次的路径,如果本地有target路径的话,也可以直接配置到CPATH里面:

  1. $ export CPATH=/home/dechin/anaconda3/pkgs/cupy-core-13.3.0-py310h5da974a_2/lib/python3.10/site-packages/cupy/_core/include/cupy/_cccl/libcudacxx/:$CPATH
复制代码

如果是下面这些报错:

  1. /datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(138): error: identifier "cublasGetStatusString" is undefined
  2. /datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(417): error: A __device__ variable cannot be marked constexpr
  3. /datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(745): error: identifier "CUBLAS_TF32_TENSOR_OP_MATH" is undefined
  4. 3 errors detected in the compilation of "/tmp/tmpxft_000a126f_00000000-9_acc.compute_75.cpp1.ii".
  5. make[2]: *** [ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/build.make:82:ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/acc.cu.o] 错误 1
  6. make[1]: *** [CMakeFiles/Makefile2:1964:ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/all] 错误 2
  7. make: *** [Makefile:160:all] 错误 2
复制代码

那么很有可能是cuda-toolkit的版本问题,尝试安装cuda-12:

  1. $ conda install nvidia::cuda-toolkit
复制代码

如果使用conda安装过程有这种问题:

  1. Collecting package metadata (current_repodata.json): failed
  2. # >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<
  3.     Traceback (most recent call last):
  4.       File "/home/dechin/anaconda3/lib/python3.8/site-packages/conda/gateways/repodata/__init__.py", line 132, in conda_http_errors
  5.         yield
  6.       File "/home/dechin/anaconda3/lib/python3.8/site-packages/conda/gateways/repodata/__init__.py", line 101, in repodata
  7.         response.raise_for_status()
  8.       File "/home/dechin/anaconda3/lib/python3.8/site-packages/requests/models.py", line 1024, in raise_for_status
  9.         raise HTTPError(http_error_msg, response=self)
  10.     requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://conda.anaconda.org/defaults/linux-64/current_repodata.json
复制代码

那应该是conda源的问题,可以删掉旧的channels,使用默认channels大概找一个国内可以用的镜像源举行配置:

  1. $ conda config --remove-key channels
  2. $ conda config --remove-key default_channels
  3. $ conda config --append channels conda-forge
复制代码

重新安装以后,nvcc的路径发生了变革,要注意修改下编译时的DCMAKE_CUDA_COMPILER参数配置:

  1. $ cmake .. -DCMAKE_CUDA_COMPILER=/home/dechin/anaconda3/envs/llama/bin/nvcc -DGGML_CUDA=ON -DBUILD_SHARED_LIBS=OFF -DLLAMA_CURL=ON -DCMAKE_CUDA_STANDARD=17
复制代码

如果出现如下报错:

  1. -- Unable to find cuda_runtime.h in "/home/dechin/anaconda3/envs/llama/include" for CUDAToolkit_INCLUDE_DIR.
  2. -- Could NOT find CUDAToolkit (missing: CUDAToolkit_INCLUDE_DIR)
  3. CMake Error at ggml/src/ggml-cuda/CMakeLists.txt:151 (message):
  4.   CUDA Toolkit not found
  5. -- Configuring incomplete, errors occurred!
  6. See also "/datb/DeepSeek/llama/llama.cpp/build/CMakeFiles/CMakeOutput.log".
  7. See also "/datb/DeepSeek/llama/llama.cpp/build/CMakeFiles/CMakeError.log".
复制代码

这是找不到CUDAToolkit_INCLUDE_DIR的路径配置,只要在cmake的指令里面加上一个include路径即可:

  1. $ cmake .. -DCMAKE_CUDA_COMPILER=/home/dechin/anaconda3/envs/llama/bin/nvcc -DGGML_CUDA=ON -DBUILD_SHARED_LIBS=OFF -DLLAMA_CURL=ON -DCMAKE_CUDA_STANDARD=17 -DCUDAToolkit_INCLUDE_DIR=/home/dechin/anaconda3/envs/llama/targets/x86_64-linux/include/ -DCURL_LIBRARY=/usr/lib/x86_64-linux-gnu/
复制代码

如果经过以上的一串处理,依然有报错信息,那我发起还是用个Docker吧,大概直接用CPU版本执行quantize,模子调用使用Ollama,如许方便一些。

下载Hugging Face模子


由于很多已经完成量化的GGUF模子文件,无法被二次量化,所以发起直接从Hugging Face下载safetensors模子文件。然后用llama.cpp里面的一个Python脚本将hf模子转为gguf模子,然后再使用llama.cpp举行模子quantize。

关于模子下载这部门,因为Hugging Face的访问有时间也会受限,所以这里首推的还是国内的ModelScope平台。从ModelScope平台下载模子,可以装一个这种Python情势的modelscope:

  1. $ python3 -m pip install modelscope
  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
  3. Requirement already satisfied: modelscope in /home/dechin/anaconda3/lib/python3.8/site-packages (1.22.3)
  4. Requirement already satisfied: requests>=2.25 in /home/dechin/.local/lib/python3.8/site-packages (from modelscope) (2.25.1)
  5. Requirement already satisfied: urllib3>=1.26 in /home/dechin/.local/lib/python3.8/site-packages (from modelscope) (1.26.5)
  6. Requirement already satisfied: tqdm>=4.64.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from modelscope) (4.67.1)
  7. Requirement already satisfied: certifi>=2017.4.17 in /home/dechin/.local/lib/python3.8/site-packages (from requests>=2.25->modelscope) (2021.5.30)
  8. Requirement already satisfied: chardet<5,>=3.0.2 in /home/dechin/.local/lib/python3.8/site-packages (from requests>=2.25->modelscope) (4.0.0)
  9. Requirement already satisfied: idna<3,>=2.5 in /home/dechin/.local/lib/python3.8/site-packages (from requests>=2.25->modelscope) (2.10)
复制代码

然后使用modelcope下载模子:

  1. $ modelscope download --model deepseek-ai/DeepSeek-R1-Distill-Qwen-32B
复制代码

如果出现报错(如果没有报错就不消分析,等待模子下载完成即可):

  1. safetensors integrity check failed, expected sha256 signature is xxx
复制代码

可以尝试另一种安装方式:

  1. $ sudo apt install git-lfs
复制代码

下载模子:

  1. $ git clone https://www.modelscope.cn/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B.git
  2. 正克隆到 'DeepSeek-R1-Distill-Qwen-32B'...
  3. remote: Enumerating objects: 52, done.
  4. remote: Counting objects: 100% (52/52), done.
  5. remote: Compressing objects: 100% (37/37), done.
  6. remote: Total 52 (delta 17), reused 42 (delta 13), pack-reused 0
  7. 展开对象中: 100% (52/52), 2.27 MiB | 2.62 MiB/s, 完成.
  8. 过滤内容: 100% (8/8), 5.02 GiB | 912.00 KiB/s, 完成.
  9. Encountered 8 file(s) that may not have been copied correctly on Windows:
  10.         model-00005-of-000008.safetensors
  11.         model-00004-of-000008.safetensors
  12.         model-00008-of-000008.safetensors
  13.         model-00002-of-000008.safetensors
  14.         model-00007-of-000008.safetensors
  15.         model-00003-of-000008.safetensors
  16.         model-00006-of-000008.safetensors
  17.         model-00001-of-000008.safetensors
  18. See: `git lfs help smudge` for more details.
复制代码

这个过程会消耗很多时间,请耐烦等待模子下载完成为止。下载完成后查看路径:

  1. $ cd DeepSeek-R1-Distill-Qwen-32B/
  2. $ ll
  3. 总用量 63999072
  4. drwxrwxr-x 4 dechin dechin       4096 2月  12 19:22 ./
  5. drwxrwxr-x 3 dechin dechin       4096 2月  12 17:46 ../
  6. -rw-rw-r-- 1 dechin dechin        664 2月  12 17:46 config.json
  7. -rw-rw-r-- 1 dechin dechin         73 2月  12 17:46 configuration.json
  8. drwxrwxr-x 2 dechin dechin       4096 2月  12 17:46 figures/
  9. -rw-rw-r-- 1 dechin dechin        181 2月  12 17:46 generation_config.json
  10. drwxrwxr-x 9 dechin dechin       4096 2月  12 19:22 .git/
  11. -rw-rw-r-- 1 dechin dechin       1519 2月  12 17:46 .gitattributes
  12. -rw-rw-r-- 1 dechin dechin       1064 2月  12 17:46 LICENSE
  13. -rw-rw-r-- 1 dechin dechin 8792578462 2月  12 19:22 model-00001-of-000008.safetensors
  14. -rw-rw-r-- 1 dechin dechin 8776906899 2月  12 19:03 model-00002-of-000008.safetensors
  15. -rw-rw-r-- 1 dechin dechin 8776906927 2月  12 19:18 model-00003-of-000008.safetensors
  16. -rw-rw-r-- 1 dechin dechin 8776906927 2月  12 18:56 model-00004-of-000008.safetensors
  17. -rw-rw-r-- 1 dechin dechin 8776906927 2月  12 18:38 model-00005-of-000008.safetensors
  18. -rw-rw-r-- 1 dechin dechin 8776906927 2月  12 19:19 model-00006-of-000008.safetensors
  19. -rw-rw-r-- 1 dechin dechin 8776906927 2月  12 19:15 model-00007-of-000008.safetensors
  20. -rw-rw-r-- 1 dechin dechin 4073821536 2月  12 19:02 model-00008-of-000008.safetensors
  21. -rw-rw-r-- 1 dechin dechin      64018 2月  12 17:46 model.safetensors.index.json
  22. -rw-rw-r-- 1 dechin dechin      18985 2月  12 17:46 README.md
  23. -rw-rw-r-- 1 dechin dechin       3071 2月  12 17:46 tokenizer_config.json
  24. -rw-rw-r-- 1 dechin dechin    7031660 2月  12 17:46 tokenizer.json
复制代码

这就是下载成功了。

HF模子转GGUF模子


找到编译好的llama/llama.cpp/下的python脚本文件,可以先看下其用法:

  1. $ python3 convert_hf_to_gguf.py --help
  2. usage: convert_hf_to_gguf.py [-h] [--vocab-only] [--outfile OUTFILE] [--outtype {f32,f16,bf16,q8_0,tq1_0,tq2_0,auto}] [--bigendian] [--use-temp-file] [--no-lazy]
  3.                              [--model-name MODEL_NAME] [--verbose] [--split-max-tensors SPLIT_MAX_TENSORS] [--split-max-size SPLIT_MAX_SIZE] [--dry-run]
  4.                              [--no-tensor-first-split] [--metadata METADATA] [--print-supported-models]
  5.                              [model]
  6. Convert a huggingface model to a GGML compatible file
  7. positional arguments:
  8.   model                 directory containing model file
  9. options:
  10.   -h, --help            show this help message and exit
  11.   --vocab-only          extract only the vocab
  12.   --outfile OUTFILE     path to write to; default: based on input. {ftype} will be replaced by the outtype.
  13.   --outtype {f32,f16,bf16,q8_0,tq1_0,tq2_0,auto}
  14.                         output format - use f32 for float32, f16 for float16, bf16 for bfloat16, q8_0 for Q8_0, tq1_0 or tq2_0 for ternary, and auto for the highest-
  15.                         fidelity 16-bit float type depending on the first loaded tensor type
  16.   --bigendian           model is executed on big endian machine
  17.   --use-temp-file       use the tempfile library while processing (helpful when running out of memory, process killed)
  18.   --no-lazy             use more RAM by computing all outputs before writing (use in case lazy evaluation is broken)
  19.   --model-name MODEL_NAME
  20.                         name of the model
  21.   --verbose             increase output verbosity
  22.   --split-max-tensors SPLIT_MAX_TENSORS
  23.                         max tensors in each split
  24.   --split-max-size SPLIT_MAX_SIZE
  25.                         max size per split N(M|G)
  26.   --dry-run             only print out a split plan and exit, without writing any new files
  27.   --no-tensor-first-split
  28.                         do not add tensors to the first split (disabled by default)
  29.   --metadata METADATA   Specify the path for an authorship metadata override file
  30.   --print-supported-models
  31.                         Print the supported models
复制代码

然后执行构建GGUF:

  1. $ python3 convert_hf_to_gguf.py /datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B --outfile /datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B.gguf
  2. INFO:hf-to-gguf:Set model quantization version
  3. INFO:gguf.gguf_writer:Writing the following files:
  4. INFO:gguf.gguf_writer:/datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B.gguf: n_tensors = 771, total_size = 65.5G
  5. Writing: 100%|██████████████████████████████████████████████████████████████| 65.5G/65.5G [19:42<00:00, 55.4Mbyte/s]
  6. INFO:hf-to-gguf:Model successfully exported to /datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B.gguf
复制代码

完成转化后,会在指定的路径下生成一个gguf文件,也就是all-in-one的模子文件。默认是fp32的精度,可以用于执行下一步的量化操作。

GGUF模子量化


在编译好的llama.cpp的build/bin/路径下,可以找到量化的可执行文件:

  1. $ ./llama-quantize --help
  2. usage: ./llama-quantize [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--include-weights] [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--override-kv] model-f32.gguf [model-quant.gguf] type [nthreads]
  3.   --allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit
  4.   --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing
  5.   --pure: Disable k-quant mixtures and quantize all tensors to the same type
  6.   --imatrix file_name: use data in file_name as importance matrix for quant optimizations
  7.   --include-weights tensor_name: use importance matrix for this/these tensor(s)
  8.   --exclude-weights tensor_name: use importance matrix for this/these tensor(s)
  9.   --output-tensor-type ggml_type: use this ggml_type for the output.weight tensor
  10.   --token-embedding-type ggml_type: use this ggml_type for the token embeddings tensor
  11.   --keep-split: will generate quantized model in the same shards as input
  12.   --override-kv KEY=TYPE:VALUE
  13.       Advanced option to override model metadata by key in the quantized model. May be specified multiple times.
  14. Note: --include-weights and --exclude-weights cannot be used together
  15. Allowed quantization types:
  16.    2  or  Q4_0    :  4.34G, +0.4685 ppl @ Llama-3-8B
  17.    3  or  Q4_1    :  4.78G, +0.4511 ppl @ Llama-3-8B
  18.    8  or  Q5_0    :  5.21G, +0.1316 ppl @ Llama-3-8B
  19.    9  or  Q5_1    :  5.65G, +0.1062 ppl @ Llama-3-8B
  20.   19  or  IQ2_XXS :  2.06 bpw quantization
  21.   20  or  IQ2_XS  :  2.31 bpw quantization
  22.   28  or  IQ2_S   :  2.5  bpw quantization
  23.   29  or  IQ2_M   :  2.7  bpw quantization
  24.   24  or  IQ1_S   :  1.56 bpw quantization
  25.   31  or  IQ1_M   :  1.75 bpw quantization
  26.   36  or  TQ1_0   :  1.69 bpw ternarization
  27.   37  or  TQ2_0   :  2.06 bpw ternarization
  28.   10  or  Q2_K    :  2.96G, +3.5199 ppl @ Llama-3-8B
  29.   21  or  Q2_K_S  :  2.96G, +3.1836 ppl @ Llama-3-8B
  30.   23  or  IQ3_XXS :  3.06 bpw quantization
  31.   26  or  IQ3_S   :  3.44 bpw quantization
  32.   27  or  IQ3_M   :  3.66 bpw quantization mix
  33.   12  or  Q3_K    : alias for Q3_K_M
  34.   22  or  IQ3_XS  :  3.3 bpw quantization
  35.   11  or  Q3_K_S  :  3.41G, +1.6321 ppl @ Llama-3-8B
  36.   12  or  Q3_K_M  :  3.74G, +0.6569 ppl @ Llama-3-8B
  37.   13  or  Q3_K_L  :  4.03G, +0.5562 ppl @ Llama-3-8B
  38.   25  or  IQ4_NL  :  4.50 bpw non-linear quantization
  39.   30  or  IQ4_XS  :  4.25 bpw non-linear quantization
  40.   15  or  Q4_K    : alias for Q4_K_M
  41.   14  or  Q4_K_S  :  4.37G, +0.2689 ppl @ Llama-3-8B
  42.   15  or  Q4_K_M  :  4.58G, +0.1754 ppl @ Llama-3-8B
  43.   17  or  Q5_K    : alias for Q5_K_M
  44.   16  or  Q5_K_S  :  5.21G, +0.1049 ppl @ Llama-3-8B
  45.   17  or  Q5_K_M  :  5.33G, +0.0569 ppl @ Llama-3-8B
  46.   18  or  Q6_K    :  6.14G, +0.0217 ppl @ Llama-3-8B
  47.    7  or  Q8_0    :  7.96G, +0.0026 ppl @ Llama-3-8B
  48.    1  or  F16     : 14.00G, +0.0020 ppl @ Mistral-7B
  49.   32  or  BF16    : 14.00G, -0.0050 ppl @ Mistral-7B
  50.    0  or  F32     : 26.00G              @ 7B
  51.           COPY    : only copy tensors, no quantizing
复制代码

这里可以看到完整的可以执行量化操作的精度。比方我们可以量化一个q4_0精度的32B模子:

  1. $ ./llama-quantize /datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B.gguf /datb/DeepSeek/models/DeepSeek-R1-Distill-Qwen-32B-Q4_0.gguf q4_0
复制代码

输出效果对比(这里的Q8_0是直接从模子仓库里面下载的别人量化出来的Q8_0模子):

  1. -rw-rw-r-- 1 dechin dechin 65535969184 2月  13 09:33 DeepSeek-R1-Distill-Qwen-32B.gguf
  2. -rw-rw-r-- 1 dechin dechin 18640230304 2月  13 09:51 DeepSeek-R1-Distill-Qwen-32B-Q4_0.gguf
  3. -rw-rw-r-- 1 dechin dechin 34820884384 2月   9 01:44 DeepSeek-R1-Distill-Qwen-32B-Q8_0.gguf
复制代码

从F32到Q8再到Q4,可以看到有一个很明显的内存占用的降落。我们可以根据自己本地的盘算机资源来决定要做多少精度的量化操作。

量化完成后,导入模子成功以后,可以用ollama list查看到所有的本地模子:

  1. $ ollama list
  2. NAME                            ID              SIZE      MODIFIED      
  3. deepseek-r1:32b-q2k             8d2a0c19f6e0    12 GB     5 seconds ago   
  4. deepseek-r1:32b-q40             13c7c287f615    18 GB     3 minutes ago   
  5. deepseek-r1:32b                 91f2de3dd7fd    34 GB     42 hours ago     
  6. nomic-embed-text-v1.5:latest    5b3683392ccb    274 MB    43 hours ago     
  7. deepseek-r1:14b                 ea35dfe18182    9.0 GB    7 days ago  
复制代码

这里q2k也是本地量化的Q2_K的模子。只是从Q4_0到Q2_k已经没有太大的参数内存缩减了,所以很多人量化一样平常就到Q4_0这个级别,可以兼具性能与精确性。

其他报错处理


如果运行llama-quantize这个可执行文件出现这种报错:

  1. ./xxx/llama-quantize: error while loading shared libraries: libllama.so: cannot open shared object file: No such file or directory
复制代码

动态链接库路径LD_LIBRARY_PATH没有设置,也可以选择直接进入到bin/路径下运行该可执行文件。

总结概要


这篇文章主要介绍了llama.cpp这一大模子工具的使用。因为已经使用Ollama来run大模子,因此仅介绍了llama.cpp在HF模子转GGUF模子中的应用,及其在大模子量化中的使用。大模子的参数量化技术,使得我们可以在本地有限预算的硬件条件下,也能够运行DeepSeek的蒸馏模子。

   文章转载自:Dechin的博客
  原文链接:DeepSeek模子量化 - DECHIN - 博客园
  体验地址:引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表