当前位置: 首页 > article >正文

DeepSeek模型量化

技术背景

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

安装llama.cpp

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

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

最好创建一个虚拟环境,以避免各种软件依赖的问题,推荐Python3.10:

# 创建虚拟环境
$ conda create -n llama python=3.10
# 激活虚拟环境
$ conda activate llama

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

$ cd llama.cpp/
$ python3 -m pip install -e .

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

$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Git: /usr/bin/git (found version "2.25.1") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Warning: ccache not found - consider installing it for faster compilation or disable this warning with GGML_CCACHE=OFF
-- CMAKE_SYSTEM_PROCESSOR: x86_64
-- Including CPU backend
-- Found OpenMP_C: -fopenmp (found version "4.5") 
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 
-- Found OpenMP: TRUE (found version "4.5")  
-- x86 detected
-- Adding CPU backend variant ggml-cpu: -march=native 
-- Configuring done
-- Generating done
-- Build files have been written to: /datb/DeepSeek/llama/llama.cpp/build
$ cmake --build . --config Release
Scanning dependencies of target ggml-base
[  0%] Building C object ggml/src/CMakeFiles/ggml-base.dir/ggml.c.o
[  1%] Building C object ggml/src/CMakeFiles/ggml-base.dir/ggml-alloc.c.o
[100%] Linking CXX executable ../../bin/llama-vdot
[100%] Built target llama-vdot

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

llama.cpp之CUDA加速

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

$ sudo apt install curl libcurl4-openssl-dev

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

$ 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,有可能出现下面这种报错:

Make Error in ggml/src/ggml-cuda/CMakeLists.txt:Target "ggml-cuda" requires the language dialect "CUDA17" (with compilerextensions), but CMake does not know the compile flags to use to enable it.

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

$ cmake --build . --config Release

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

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

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

/home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/nvidia/cuda_runtime/include/cuda_bf16.h
/home/dechin/anaconda3/envs/llama/lib/python3.10/site-packages/triton/backends/nvidia/include/cuda_bf16.h

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

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

如果是出现这个报错:

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

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

$ 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

如果是下面这些报错:

/datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(138): error: identifier "cublasGetStatusString" is undefined/datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(417): error: A __device__ variable cannot be marked constexpr/datb/DeepSeek/llama/llama.cpp/ggml/src/ggml-cuda/common.cuh(745): error: identifier "CUBLAS_TF32_TENSOR_OP_MATH" is undefined3 errors detected in the compilation of "/tmp/tmpxft_000a126f_00000000-9_acc.compute_75.cpp1.ii".
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
make[1]: *** [CMakeFiles/Makefile2:1964:ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/all] 错误 2
make: *** [Makefile:160:all] 错误 2

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

$ conda install nvidia::cuda-toolkit

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

Collecting package metadata (current_repodata.json): failed# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<Traceback (most recent call last):File "/home/dechin/anaconda3/lib/python3.8/site-packages/conda/gateways/repodata/__init__.py", line 132, in conda_http_errorsyieldFile "/home/dechin/anaconda3/lib/python3.8/site-packages/conda/gateways/repodata/__init__.py", line 101, in repodataresponse.raise_for_status()File "/home/dechin/anaconda3/lib/python3.8/site-packages/requests/models.py", line 1024, in raise_for_statusraise HTTPError(http_error_msg, response=self)requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://conda.anaconda.org/defaults/linux-64/current_repodata.json

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

$ conda config --remove-key channels
$ conda config --remove-key default_channels
$ conda config --append channels conda-forge

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

$ 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

如果出现如下报错:

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

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

$ 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:

$ python3 -m pip install modelscope
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: modelscope in /home/dechin/anaconda3/lib/python3.8/site-packages (1.22.3)
Requirement already satisfied: requests>=2.25 in /home/dechin/.local/lib/python3.8/site-packages (from modelscope) (2.25.1)
Requirement already satisfied: urllib3>=1.26 in /home/dechin/.local/lib/python3.8/site-packages (from modelscope) (1.26.5)
Requirement already satisfied: tqdm>=4.64.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from modelscope) (4.67.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/dechin/.local/lib/python3.8/site-packages (from requests>=2.25->modelscope) (2021.5.30)
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)
Requirement already satisfied: idna<3,>=2.5 in /home/dechin/.local/lib/python3.8/site-packages (from requests>=2.25->modelscope) (2.10)

然后使用modelcope下载模型:

$ modelscope download --model deepseek-ai/DeepSeek-R1-Distill-Qwen-32B

如果出现报错(如果没有报错就不用理会,等待模型下载完成即可):

safetensors integrity check failed, expected sha256 signature is xxx

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

$ sudo apt install git-lfs

下载模型:

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

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

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

这就是下载成功了。

HF模型转GGUF模型

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

$ python3 convert_hf_to_gguf.py --help
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][--model-name MODEL_NAME] [--verbose] [--split-max-tensors SPLIT_MAX_TENSORS] [--split-max-size SPLIT_MAX_SIZE] [--dry-run][--no-tensor-first-split] [--metadata METADATA] [--print-supported-models][model]Convert a huggingface model to a GGML compatible filepositional arguments:model                 directory containing model fileoptions:-h, --help            show this help message and exit--vocab-only          extract only the vocab--outfile OUTFILE     path to write to; default: based on input. {ftype} will be replaced by the outtype.--outtype {f32,f16,bf16,q8_0,tq1_0,tq2_0,auto}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-fidelity 16-bit float type depending on the first loaded tensor type--bigendian           model is executed on big endian machine--use-temp-file       use the tempfile library while processing (helpful when running out of memory, process killed)--no-lazy             use more RAM by computing all outputs before writing (use in case lazy evaluation is broken)--model-name MODEL_NAMEname of the model--verbose             increase output verbosity--split-max-tensors SPLIT_MAX_TENSORSmax tensors in each split--split-max-size SPLIT_MAX_SIZEmax size per split N(M|G)--dry-run             only print out a split plan and exit, without writing any new files--no-tensor-first-splitdo not add tensors to the first split (disabled by default)--metadata METADATA   Specify the path for an authorship metadata override file--print-supported-modelsPrint the supported models

然后执行构建GGUF:

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

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

GGUF模型量化

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

$ ./llama-quantize --help
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]--allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit--leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing--pure: Disable k-quant mixtures and quantize all tensors to the same type--imatrix file_name: use data in file_name as importance matrix for quant optimizations--include-weights tensor_name: use importance matrix for this/these tensor(s)--exclude-weights tensor_name: use importance matrix for this/these tensor(s)--output-tensor-type ggml_type: use this ggml_type for the output.weight tensor--token-embedding-type ggml_type: use this ggml_type for the token embeddings tensor--keep-split: will generate quantized model in the same shards as input--override-kv KEY=TYPE:VALUEAdvanced option to override model metadata by key in the quantized model. May be specified multiple times.
Note: --include-weights and --exclude-weights cannot be used togetherAllowed quantization types:2  or  Q4_0    :  4.34G, +0.4685 ppl @ Llama-3-8B3  or  Q4_1    :  4.78G, +0.4511 ppl @ Llama-3-8B8  or  Q5_0    :  5.21G, +0.1316 ppl @ Llama-3-8B9  or  Q5_1    :  5.65G, +0.1062 ppl @ Llama-3-8B19  or  IQ2_XXS :  2.06 bpw quantization20  or  IQ2_XS  :  2.31 bpw quantization28  or  IQ2_S   :  2.5  bpw quantization29  or  IQ2_M   :  2.7  bpw quantization24  or  IQ1_S   :  1.56 bpw quantization31  or  IQ1_M   :  1.75 bpw quantization36  or  TQ1_0   :  1.69 bpw ternarization37  or  TQ2_0   :  2.06 bpw ternarization10  or  Q2_K    :  2.96G, +3.5199 ppl @ Llama-3-8B21  or  Q2_K_S  :  2.96G, +3.1836 ppl @ Llama-3-8B23  or  IQ3_XXS :  3.06 bpw quantization26  or  IQ3_S   :  3.44 bpw quantization27  or  IQ3_M   :  3.66 bpw quantization mix12  or  Q3_K    : alias for Q3_K_M22  or  IQ3_XS  :  3.3 bpw quantization11  or  Q3_K_S  :  3.41G, +1.6321 ppl @ Llama-3-8B12  or  Q3_K_M  :  3.74G, +0.6569 ppl @ Llama-3-8B13  or  Q3_K_L  :  4.03G, +0.5562 ppl @ Llama-3-8B25  or  IQ4_NL  :  4.50 bpw non-linear quantization30  or  IQ4_XS  :  4.25 bpw non-linear quantization15  or  Q4_K    : alias for Q4_K_M14  or  Q4_K_S  :  4.37G, +0.2689 ppl @ Llama-3-8B15  or  Q4_K_M  :  4.58G, +0.1754 ppl @ Llama-3-8B17  or  Q5_K    : alias for Q5_K_M16  or  Q5_K_S  :  5.21G, +0.1049 ppl @ Llama-3-8B17  or  Q5_K_M  :  5.33G, +0.0569 ppl @ Llama-3-8B18  or  Q6_K    :  6.14G, +0.0217 ppl @ Llama-3-8B7  or  Q8_0    :  7.96G, +0.0026 ppl @ Llama-3-8B1  or  F16     : 14.00G, +0.0020 ppl @ Mistral-7B32  or  BF16    : 14.00G, -0.0050 ppl @ Mistral-7B0  or  F32     : 26.00G              @ 7BCOPY    : only copy tensors, no quantizing

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

$ ./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模型):

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

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

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

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

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

其他报错处理

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

./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快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

相关文章:

DeepSeek模型量化

技术背景 大语言模型&#xff08;Large Language Model&#xff0c;LLM&#xff09;&#xff0c;可以通过量化&#xff08;Quantization&#xff09;操作来节约内存/显存的使用&#xff0c;并且降低了通讯开销&#xff0c;进而达到加速模型推理的效果。常见的就是把Float16的浮…...

原生稀疏注意力机制(NSA):硬件对齐且可原生训练的稀疏注意力机制-论文阅读

摘要 长上下文建模对于下一代语言模型至关重要&#xff0c;但标准注意力机制的高计算成本带来了巨大的计算挑战。稀疏注意力提供了一种在保持模型能力的同时提高效率的有前途的方向。本文提出了一种名为 NSA&#xff08;原生可训练稀疏注意力机制&#xff09; 的方法&#xff…...

从0到1:固件分析

固件分析 0x01 固件提取 1、从厂商官网下载 例如D-link的固件&#xff1a; https://support.dlink.com/resource/products/ 2、代理或镜像设备更新时的流量 发起中间人攻击MITM #启用IP转发功能 echo 1 > /proc/sys/net/ipv4/ip_forward#配置iptables&#xff0c;将目…...

Zookeeper(58)如何在Zookeeper中实现分布式锁?

在 Zookeeper 中实现分布式锁是一种常见的用例。Zookeeper 提供了强一致性、高可用性的分布式协调服务&#xff0c;使得它非常适合用来实现分布式锁。以下是详细的步骤和代码示例&#xff0c;展示如何在 Zookeeper 中实现分布式锁。 1. Zookeeper 分布式锁的基本原理 Zookeep…...

23种设计模式 - 观察者模式

模式定义 观察者模式&#xff08;Observer Pattern&#xff09;是一种行为型设计模式&#xff0c;定义了一对多的依赖关系&#xff1a;当一个对象&#xff08;称为主题&#xff09;状态变化时&#xff0c;所有依赖它的对象&#xff08;称为观察者&#xff09;会自动收到通知并…...

conda、anaconda、pip、pytorch、tensorflow有什么区别?

先画一张图&#xff0c;可以大致看出它们的区别和关联&#xff1a; pytorch、tensorflow都是Python的第三方库&#xff0c;相当于封装的代码工具集库&#xff0c;通过import导入使用。这两个都是深度学习框架&#xff0c;用来搭建AI模型什么的&#xff0c;使用范围非常之广&…...

项目设置内网 IP 访问实现方案

在我们平常的开发工作中&#xff0c;项目开发、测试完成后进行部署上线。比如电商网站、新闻网站、社交网站等&#xff0c;通常对访问不会进行限制。但是像企业内部网站、内部管理系统等&#xff0c;这种系统一般都需要限制访问&#xff0c;比如内网才能访问等。那么一个网站应…...

Vue面试2

1.跨域问题以及如何解决跨域 跨域问题&#xff08;Cross-Origin Resource Sharing, CORS&#xff09;是指在浏览器中&#xff0c;当一个资源试图从一个不同的源请求另一个资源时所遇到的限制。这种限制是浏览器为了保护用户安全而实施的一种同源策略&#xff08;Same-origin p…...

合合信息2025届春季校园招聘全面启动!

世界因你而AI&#xff0c;合合信息2025届春季校园招聘启动&#xff01; 我们是谁&#xff1f; 我们是一家行业领先的人工智能及大数据科技企业 18年深耕AI领域&#xff0c;C端产品与B端服务布局矩阵完善 9.4亿全球累计用户首次下载量&#x1f4a5; 来到这里你能得到什么&a…...

如何利用 Vue 的生命周期钩子进行初始化和清理操作?

一、初始化操作的核心钩子 1. created&#xff08;选项式API&#xff09; export default {data() {return { user: null };},created() {// 适合初始化数据、发起非DOM操作请求this.fetchUser();},methods: {async fetchUser() {const response await fetch(/api/user);thi…...

Excell 代码处理

文章目录 Excell 代码处理cvc格式xlsl格式小结 Excell 代码处理 有时候要对excell进行分析&#xff0c;或者数据的导入导出&#xff0c;这个时候如果可以用代码读写分析操作那么会方便很多 cvc格式 CSV&#xff08;Comma-Separated Values&#xff0c;逗号分隔值&#xff09;是…...

KMP的next数组构建详解

KMP的next数组构建详解 1. next数组的作用 核心功能&#xff1a;在KMP算法中&#xff0c;当模式串与主串发生不匹配时&#xff0c;next数组决定模式串指针回退的位置&#xff0c;避免无效匹配。 定义&#xff1a;next[i]表示子串s[0...i]的最长公共前后缀长度。例如&#xff…...

Docker 的安全配置与优化(二)

Docker 安全优化策略 &#xff08;一&#xff09;多阶段构建优化镜像大小 多阶段构建是 Docker 17.05 版本引入的强大功能&#xff0c;它允许在一个 Dockerfile 中定义多个构建阶段&#xff0c;每个阶段都可以使用不同的基础镜像和依赖项&#xff0c;最终只将必要的文件和依赖…...

shiro代码层面追踪

文章目录 环境漏洞分析硬编码 反序列化Gadget构造 环境 环境搭建&#xff1a;https://blog.csdn.net/qq_44769520/article/details/123476443 漏洞分析 硬编码 shiro是对rememberMe这个cookie进⾏反序列化的时候出现了问题。 相应代码 // // Source code recreated from …...

通信系统中物理层与网络层联系与区别

在通信系统中&#xff0c;物理层和网络层是OSI&#xff08;开放系统互连&#xff09;模型中的两个重要层次&#xff0c;分别位于协议栈的最底层和第三层。它们在功能、职责和实现方式上有显著的区别&#xff0c;但同时也在某些方面存在联系。以下是物理层与网络层的联系与区别的…...

虚拟机网络ssh连接失败,没有网络

vscode进行ssh时连接失败&#xff0c;发现是虚拟机没有网络。 虚拟机ping不通www.baidu.com但可以ping通内网 ping 8.8.8.8ping不通。 sudo dhclient -r ens33 sudo dhclient ens33 ip route show可以了。 20250221记录&#xff1a;不知道是不是重启了虚拟机还是咋了&#…...

已知点矩阵的三个顶点坐标、行列数和行列的间距,计算得出剩余所有点的坐标

已知点矩阵的三个顶点坐标、行列数和行列的间距&#xff0c;计算得出剩余所有点的坐标 计算矩阵中每个点的坐标代码实现案例图调用验证 计算矩阵中每个点的坐标 给定左上角、左下角和右上角三个点的坐标&#xff0c;以及矩阵的行数、列数、行间距和列间距&#xff0c;我们可以…...

Python Cookbook-2.4 从文件中读取指定的行

任务 根据给出的行号&#xff0c;从文本文件中读取一行数据。 解决方案 Python标准库linecache模块非常适合这个任务: import linecache theline linecache.getline(thefilepath, desired_line_number)讨论 对这个任务而言&#xff0c;标准的 linecache 模块是 Python 能…...

go 并发 gorouting chan channel select Mutex sync.One

goroutine // head&#xff1a; 前缀 index&#xff1a;是一个int的指针 func print(head string, index *int) {for i : 0; i < 5; i {// 指针对应的int *indexfmt.Println(*index, head, i)// 暂停1stime.Sleep(1 * time.Second)} }/* Go 允许使用 go 语句开启一个新的运…...

Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符

1. 基本算术运算符 算术运算符主要用于对数值类型&#xff08;整型和浮点型&#xff09;进行基本的数学运算。以下是常见的算术运算符及其说明&#xff1a; 运算符描述示例结果加法运算符&#xff0c;用于两个数相加&#xff0c;也可用于字符串连接int a 5 3; string str &…...

深度学习入门--python入门2

以前学的全忘了&#xff0c;现在算是才开始学&#xff0c;有错误&#xff0c;恳请指正。 目录 1.4 Python脚本文件 1.4.1保存为文件 1.4.2 类 1.5 Numpy 1.5.1 导入Numpy 1.5.2 生成Numpy数组 1.5.3 Numpy的算术运算 1.5.4 Numpy的N维数组 1.5.5 广播 1.5.6 访问元素…...

题海拾贝:【枚举】P2010 [NOIP 2016 普及组] 回文日期

Hello大家好&#xff01;很高兴我们又见面啦&#xff01;给生活添点passion&#xff0c;开始今天的编程之路&#xff01; 我的博客&#xff1a;<但凡. 我的专栏&#xff1a;《编程之路》、《数据结构与算法之美》、《题海拾贝》 欢迎点赞&#xff0c;关注&#xff01; 1、题…...

Mac端homebrew安装配置

拷打了一下午o3-mini-high&#xff0c;不如这位博主的超强帖子&#xff0c;10分钟结束战斗 跟随该文章即可&#xff0c;2025/2/19亲测可行 mac 安装HomeBrew(100%成功)_mac安装homebrew-CSDN博客文章浏览阅读10w次&#xff0c;点赞258次&#xff0c;收藏837次。一直觉得自己写…...

Web - JS基础语法与表达式

概述 这篇文章主要介绍了 JavaScript 的基础语法&#xff0c;包括代码书写位置、ERPL 环境、变量&#xff08;命名规则、默认值、初始化&#xff09;、数据类型&#xff08;基本和复杂&#xff0c;及各类型特点、转换&#xff09;、表达式和运算符&#xff08;算数、特殊算数、…...

Python高级语法之selenium

目录&#xff1a; 1、selenium的使用2、selenium元素定位3、selenium使用功能Phantomjs模拟浏览器启动4、selenium使用功能ChromsHandless模拟浏览器启动 1、selenium的使用 2、selenium元素定位 3、selenium使用功能Phantomjs模拟浏览器启动 4、selenium使用功能ChromsHandles…...

AIP-148 标准域

编号148原文链接AIP-148: Standard fields状态批准创建日期2020-10-06更新日期2020-10-06 一些概念在任何API集合中都很常用。对于这些概念&#xff0c;使用统一的标准域名字和行为来表达它们&#xff0c;是非常有用的。 指南 标准域 应当 用于描述其相应概念&#xff0c; 不…...

Docker构建时,设定默认进入的工作目录的方法

在 Docker 中,你可以通过不同的方式来设定容器默认进入的目录,以下针对不同场景分别介绍具体方法: 1. 使用 Dockerfile 设定工作目录 如果你是通过构建镜像的方式来运行容器,那么可以在 Dockerfile 中使用 WORKDIR 指令来设置容器启动时的默认工作目录。以下是具体步骤:…...

2025年3月最新算法-鲸鱼迁徙优化算法Whale Migration Algorithm-附Matlab免费代码

引言 本期介绍了一种基于座头鲸协同迁移行为的创新生物启发式优化方法——鲸鱼迁徙优化算法Whale Migration Algorithm&#xff0c;WMA。该算法于2025年3月最新发表在期刊 Results in Engineering 在本节中&#xff0c;我们概述了开发鲸鱼迁移算法&#xff08;WMA&#xff09;…...

day56 第十一章:图论part06

108.冗余连接 注意init初始化 改进&#xff1a; 其实只有一条边冗余&#xff0c;改为&#xff0c;如果两条边在同一个集合里&#xff0c;就输出&#xff0c;不然加入。 #include <iostream> #include <vector> using namespace std;int n 1005; vector<int>…...

flowable适配达梦数据库

文章目录 适配相关问题无法从数据库产品名称“DM DBMS”中推断数据库类型分析解决 构建ibatis SqlSessionFactory时出错&#xff1a;inStream参数为null分析解决 liquibase相关问题问题一&#xff1a;不支持的数据库 Error executing SQL call current_schema: 无法解析的成员访…...