欢迎回来

登录 EAKE AI,继续您的智能之旅

忘记密码?
还没有账号?立即注册

llama.cpp 本地推理

llama.cpp 本地推理

llama.cpp 本地 GGUF 推理 + HF Hub 模型发现

llama.cpp + GGUF

使用场景

  • 在 CPU、Apple Silicon、CUDA、ROCm 或 Intel GPU 上运行本地模型
  • 为特定 Hugging Face 仓库找到合适的 GGUF
  • 从 Hub 构建 llama-serverllama-cli 命令
  • 在 Hub 上搜索已支持 llama.cpp 的模型
  • 枚举仓库中可用的 .gguf 文件和大小
  • 为用户的 RAM 或显存选择 Q4/Q5/Q6/IQ 量化方案
  • 模型发现工作流

    优先使用 URL 工作流,再考虑 hf、Python 或自定义脚本。

  • 在 Hub 上搜索候选仓库:
  • 基础:https://huggingface.co/models?apps=llama.cpp&sort=trending
  • 添加 search= 搜索模型系列
  • 用户有大小限制时添加 num_parameters=min:0,max:24B
  • 用 llama.cpp local-app 视图打开仓库:
  • https://huggingface.co/?local-app=llama.cpp
  • local-app 代码片段可见时以其为准:
  • 复制精确的 llama-serverllama-cli 命令
  • 严格按 HF 显示的报告推荐量化
  • 将同一 ?local-app=llama.cpp URL 作为页面文本或 HTML 读取,提取 Hardware compatibility 下的内容:
  • 优先使用其精确的量化标签和大小,而非通用表格
  • 保留仓库特定的标签,如 UD-Q4_K_MIQ4_NL_XL
  • 如果页面源码中没有该部分,说明并回退到 tree API + 通用量化指导
  • 查询 tree API 确认实际存在的文件:
  • https://huggingface.co/api/models//tree/main?recursive=true
  • 保留 typefilepath.gguf 结尾的条目
  • pathsize 作为文件名和字节大小的权威来源
  • 区分量化 checkpoint 与 mmproj-*.gguf 投影器文件和 BF16/ 分片文件
  • 仅将 https://huggingface.co//tree/main 作为人工备查
  • 如果仓库尚未暴露 GGUF 文件,才建议从 Transformers 权重转换:
  • 简写量化选择:llama-server -hf :
  • 精确文件回退:llama-server --hf-repo --hf-file
  • 快速入门

    安装 llama.cpp

    
    # macOS / Linux(最简)
    brew install llama.cpp
    
    
    winget install llama.cpp
    
    
    git clone https://github.com/ggml-org/llama.cpp
    cd llama.cpp
    cmake -B build
    cmake --build build --config Release
    

    直接从 Hugging Face Hub 运行

    
    llama-cli -hf bartowski/Llama-3.2-3B-Instruct-GGUF:Q8_0
    
    
    llama-server -hf bartowski/Llama-3.2-3B-Instruct-GGUF:Q8_0
    

    从 Hub 运行精确 GGUF 文件

    当 tree API 显示自定义文件名或缺少精确 HF 代码片段时使用。

    
    llama-server 
        --hf-repo microsoft/Phi-3-mini-4k-instruct-gguf 
        --hf-file Phi-3-mini-4k-instruct-q4.gguf 
        -c 4096
    

    OpenAI 兼容服务器检查

    
    curl http://localhost:8080/v1/chat/completions 
      -H "Content-Type: application/json" 
      -d '{
        "messages": [
          {"role": "user", "content": "Write a limerick about Python exceptions"}
        ]
      }'
    

    Python 绑定(llama-cpp-python)

    pip install llama-cpp-python(CUDA:CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python --force-reinstall --no-cache-dir;Metal:CMAKE_ARGS="-DGGML_METAL=on" ...)。

    基础生成

    
    from llama_cpp import Llama
    
    llm = Llama(
        model_path="./model-q4_k_m.gguf",
        n_ctx=4096,
        n_gpu_layers=35,     # 0 for CPU, 99 加载全部到 GPU
        n_threads=8,
    )
    
    out = llm("What is machine learning?", max_tokens=256, temperature=0.7)
    print(out["choices"][0]["text"])
    

    聊天 + 流式输出

    
    llm = Llama(
        model_path="./model-q4_k_m.gguf",
        n_ctx=4096,
        n_gpu_layers=35,
        chat_format="llama-3",   # 或 "chatml"、"mistral" 等
    )
    
    resp = llm.create_chat_completion(
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "What is Python?"},
        ],
        max_tokens=256,
    )
    print(resp["choices"][0]["message"]["content"])
    
    # 流式输出
    for chunk in llm("Explain quantum computing:", max_tokens=256, stream=True):
        print(chunk["choices"][0]["text"], end="", flush=True)
    

    嵌入向量

    
    llm = Llama(model_path="./model-q4_k_m.gguf", embedding=True, n_gpu_layers=35)
    vec = llm.embed("This is a test sentence.")
    print(f"Embedding dimension: {len(vec)}")
    

    也可以直接从 Hub 加载 GGUF:

    
    llm = Llama.from_pretrained(
        repo_id="bartowski/Llama-3.2-3B-Instruct-GGUF",
        filename="*Q4_K_M.gguf",
        n_gpu_layers=35,
    )
    

    选择量化方案

    优先使用 Hub 页面指定的量化,其次参考通用启发式。

  • 优先使用 HF 为用户硬件配置标记的精确量化。
  • 通用聊天场景,从 Q4_K_M 开始。
  • 代码或技术工作,如果内存允许,偏好 Q5_K_MQ6_K
  • 内存极度紧张时,才考虑 Q3_K_MIQ 变体或 Q2 变体(且用户明确优先考虑能否运行而非质量)。
  • 多模态仓库,单独说明 mmproj-*.gguf。投影器不是主模型文件。
  • 不要规范化仓库原生的标签。如果页面显示 UD-Q4_K_M,就报告 UD-Q4_K_M
  • 从仓库提取可用 GGUF

    当用户询问存在哪些 GGUF 时,返回:

  • 文件名
  • 文件大小
  • 量化标签
  • 是主模型还是辅助投影器
  • 忽略(除非用户要求):

  • README
  • BF16 分片文件
  • imatrix blob 或校准产物
  • 使用 tree API:

  • https://huggingface.co/api/models//tree/main?recursive=true

    例如对于 unsloth/Qwen3.6-35B-A3B-GGUF 仓库,local-app 页面可能显示量化标签如 UD-Q4_K_MUD-Q5_K_MQ6_KQ8_0,而 tree API 暴露精确文件路径如 Qwen3.6-35B-A3B-UD-Q4_K_M.ggufQwen3.6-35B-A3B-Q8_0.gguf 及字节大小。使用 tree API 将量化标签转换为精确文件名。

    搜索模式

    直接使用以下 URL:

    
    https://huggingface.co/models?apps=llama.cpp&sort=trending
    https://huggingface.co/models?search=&apps=llama.cpp&sort=trending
    https://huggingface.co/models?search=&apps=llama.cpp&num_parameters=min:0,max:24B&sort=trending
    https://huggingface.co/?local-app=llama.cpp
    https://huggingface.co/api/models//tree/main?recursive=true
    https://huggingface.co//tree/main
    

    输出格式

    回答发现请求时,优先使用紧凑结构化格式:

    
    Repo: 
    Recommended quant from HF: 

    参考资料

  • hub-discovery.md - 仅 URL 的 Hugging Face 工作流、搜索模式、GGUF 提取和命令重构
  • advanced-usage.md — 推测解码、批量推理、语法约束生成、LoRA、多 GPU、自定义构建、基准测试脚本
  • quantization.md — 量化质量权衡、何时使用 Q4/Q5/Q6/IQ、模型大小缩放、imatrix
  • server.md — 直连 Hub 启动服务器、OpenAI API 端点、Docker 部署、Nginx 负载均衡、监控
  • optimization.md — CPU 线程、BLAS、GPU 卸载启发式、批处理调优、基准测试
  • troubleshooting.md — 安装/转换/量化/推理/服务器问题、Apple Silicon、调试
  • 资源

  • GitHub: https://github.com/ggml-org/llama.cpp
  • Hugging Face GGUF + llama.cpp 文档: https://huggingface.co/docs/hub/gguf-llamacpp
  • Hugging Face Local Apps 文档: https://huggingface.co/docs/hub/main/local-apps
  • Hugging Face Local Agents 文档: https://huggingface.co/docs/hub/agents-local
  • local-app 页面示例: https://huggingface.co/unsloth/Qwen3.6-35B-A3B-GGUF?local-app=llama.cpp
  • tree API 示例: https://huggingface.co/api/models/unsloth/Qwen3.6-35B-A3B-GGUF/tree/main?recursive=true
  • llama.cpp 搜索示例: https://huggingface.co/models?num_parameters=min:0,max:24B&apps=llama.cpp&sort=trending
  • 许可证: MIT
  • 安装指南

    复制下方命令,在终端运行即可安装:

    # 安装到当前项目
    npx skills add llama-cpp
    # 全局安装 — 所有项目可用
    npx skills add llama-cpp -g

    使用指南

    安装完成后,在对话框中直接使用此技能。

    基本信息
    作者 Community 分类 coding 难度 Intermediate 时长 1 hour
    🛠️ 安装命令
    # 安装到当前项目
    npx skills add llama-cpp
    # 全局安装
    npx skills add llama-cpp -g

    发表评论