Pygraphviz安装问题解决

Creado el: 30 de diciembre de 2024

Respondido usando O1 por Chat01

Pregunta

Building wheels for collected packages: pygraphviz
Building wheel for pygraphviz (pyproject.toml) ... error
error: subprocess-exited-with-error

× Building wheel for pygraphviz (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [58 lines of output]
running bdist_wheel
running build
running build_py
creating build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/agraph.py -> build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/init.py -> build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/testing.py -> build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/scraper.py -> build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/graphviz.py -> build/lib.linux-x86_64-cpython-311/pygraphviz
creating build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_repr_mimebundle.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_edge_attributes.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_string.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_close.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_attribute_defaults.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_graph.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_drawing.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_node_attributes.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/init.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_readwrite.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_html.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_unicode.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_scraper.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_clear.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_layout.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
copying pygraphviz/tests/test_subgraph.py -> build/lib.linux-x86_64-cpython-311/pygraphviz/tests
running egg_info
writing pygraphviz.egg-info/PKG-INFO
writing dependency_links to pygraphviz.egg-info/dependency_links.txt
writing top-level names to pygraphviz.egg-info/top_level.txt
reading manifest file 'pygraphviz.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '.swg'
warning: no files found matching '
.png' under directory 'doc'
warning: no files found matching '.html' under directory 'doc'
warning: no files found matching '
.txt' under directory 'doc'
warning: no files found matching '.css' under directory 'doc'
warning: no previously-included files matching '
~' found anywhere in distribution
warning: no previously-included files matching '*.pyc' found anywhere in distribution
warning: no previously-included files matching '.svn' found anywhere in distribution
no previously-included directories found matching 'doc/build'
adding license file 'LICENSE'
writing manifest file 'pygraphviz.egg-info/SOURCES.txt'
copying pygraphviz/graphviz.i -> build/lib.linux-x86_64-cpython-311/pygraphviz
copying pygraphviz/graphviz_wrap.c -> build/lib.linux-x86_64-cpython-311/pygraphviz
running build_ext
building 'pygraphviz._graphviz' extension
creating build/temp.linux-x86_64-cpython-311/pygraphviz
gcc -pthread -B /home/yangy53/.conda/envs/ollama/compiler_compat -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /home/yangy53/.conda/envs/ollama/include -fPIC -O2 -isystem /home/yangy53/.conda/envs/ollama/include -fPIC -DSWIG_PYTHON_STRICT_BYTE_CHAR -I/home/yangy53/.conda/envs/ollama/include/python3.11 -c pygraphviz/graphviz_wrap.c -o build/temp.linux-x86_64-cpython-311/pygraphviz/graphviz_wrap.o
pygraphviz/graphviz_wrap.c:9: warning: "SWIG_PYTHON_STRICT_BYTE_CHAR" redefined
9 | #define SWIG_PYTHON_STRICT_BYTE_CHAR
|
<command-line>: note: this is the location of the previous definition
pygraphviz/graphviz_wrap.c:3023:10: fatal error: graphviz/cgraph.h: No such file or directory
3023 | #include "graphviz/cgraph.h"
| ^~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command '/usr/bin/gcc' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pygraphviz
Failed to build pygraphviz
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (pygraphviz)帮我解决这个报错

Pensamiento

解决依赖问题

用户正在安装pygraphviz时遇到问题,因为缺少graphviz/cgraph.h头文件。首先,考虑通过系统包管理器或conda安装Graphviz开发包。使用conda-forge可能更好,特别是在无root访问的情况下。

选择安装方式

建议通过conda-forge安装Graphviz和pygraphviz,特别是如果使用pip时需确保Graphviz开发头文件可用。调整环境变量或使用完整路径也是选项之一。

思考,持续 4 秒

Respuesta

出现这个错误是因为在你的系统中缺少 Graphviz 的开发头文件(特别是 graphviz/cgraph.h)。要解决这个问题,你需要确保在安装 pygraphviz 之前已经安装了 Graphviz 及其开发文件。以下是几种解决方法:

方法一:使用 Conda 安装(推荐)

如果你正在使用 Conda 环境,最简单的方法是通过 conda-forge 频道同时安装 graphvizpygraphvizconda-forge 会自动处理依赖关系,确保所有必要的文件都已安装。

  1. 更新 Conda(可选,但推荐)

    bash
    conda update conda
  2. 安装 Graphviz 和 pygraphviz

    bash
    conda install -c conda-forge graphviz pygraphviz

方法二:使用系统包管理器安装 Graphviz,然后使用 Pip 安装 pygraphviz

如果你更喜欢使用 pip 来安装 pygraphviz,你需要先通过系统的包管理器安装 Graphviz 及其开发文件。

对于 Ubuntu/Debian 系统:

  1. 更新包列表

    bash
    sudo apt-get update
  2. 安装 Graphviz 及其开发文件

    bash
    sudo apt-get install graphviz graphviz-dev
  3. 安装 pygraphviz

    bash
    pip install pygraphviz

对于 macOS 系统(使用 Homebrew):

  1. 安装 Homebrew(如果尚未安装):

    bash
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. 安装 Graphviz

    bash
    brew install graphviz
  3. 安装 pygraphviz

    bash
    pip install pygraphviz

对于 Windows 系统:

  1. 下载并安装 Graphviz

  2. 确保将 Graphviz 的 bin 目录添加到系统的环境变量 PATH

  3. 安装 pygraphviz

    bash
    pip install pygraphviz

    注意:在 Windows 上,编译 pygraphviz 可能会比较复杂。建议使用预编译的二进制文件,或者使用 Conda 来简化安装过程。

方法三:手动指定 Graphviz 的路径

如果你已经安装了 Graphviz,但 pygraphviz 仍然无法找到 cgraph.h,你可以手动指定包含路径和库路径。

  1. 找到 Graphviz 的安装路径。例如,假设 Graphviz 安装在 /usr/local/graphviz

  2. 设置环境变量

    bash
    export C_INCLUDE_PATH=/usr/local/graphviz/include/graphviz:$C_INCLUDE_PATH export LIBRARY_PATH=/usr/local/graphviz/lib:$LIBRARY_PATH
  3. 安装 pygraphviz

    bash
    pip install pygraphviz

其他建议

  • 使用虚拟环境:无论使用 Conda 还是 Pip,建议在虚拟环境中进行安装,以避免与系统中的其他包发生冲突。

  • 检查 Graphviz 安装:确保 Graphviz 正常安装并且可以在命令行中使用。例如,运行 dot -V 来检查 Graphviz 的版本。

  • 查看详细错误信息:如果仍然遇到问题,可以尝试查看更详细的编译日志,以确定缺少哪些文件或依赖。

总结

最简单和推荐的方法是使用 Conda 并通过 conda-forge 频道安装 graphvizpygraphviz。这样可以自动处理所有依赖关系,避免手动配置路径和安装开发文件的麻烦。

如果你选择使用 pip,确保先通过系统包管理器安装 Graphviz 及其开发文件,然后再安装 pygraphviz

希望这些方法能帮助你顺利安装 pygraphviz

Pregunta

为 llama.cpp 选择理想的量化类型
James James 收录于 大语言模型 和 Ollama
2024-03-15 约 1000 字 预计阅读 2 分钟
警告
本文最后更新于 2024-03-15,文中内容可能已过时。
当我们部署 llama.cpp 或 Ollama 实例时,我们更倾向于运行量化模型以节省内存并加速推理。

但是,量化模型的质量并不总是很好。我们需要选择合适的量化类型来平衡质量和性能。

llama.cpp 中的量化类型
llama.cpp 中有许多量化类型。当你尝试量化模型时,可以看到以下打印信息。

量化类型比较
为了帮助我们比较量化类型,我制作了一个表格来显示每种量化类型的大小和 ppl(困惑度)变化。ppl 变化越小意味着质量越好。困惑度通常衡量模型预测结果的置信度。困惑度越低,模型越好。

Q Type Size ppl Change Note
Q2_K_S 2.16G +9.0634 @ LLaMA-v1-7B
Q2_K 2.63G +0.6717 @ LLaMA-v1-7B
Q3_K_S 2.75G +0.5551 @ LLaMA-v1-7B
Q3_K - - alias for Q3_K_M
Q3_K_M 3.07G +0.2496 @ LLaMA-v1-7B
Q3_K_L 3.35G +0.1764 @ LLaMA-v1-7B
Q4_0 3.56G +0.2166 @ LLaMA-v1-7B
Q4_K_S 3.59G +0.0992 @ LLaMA-v1-7B
Q4_K - - alias for Q4_K_M
Q4_K_M 3.80G +0.0532 @ LLaMA-v1-7B
Q4_1 3.90G +0.1585 @ LLaMA-v1-7B
Q5_0 4.33G +0.0683 @ LLaMA-v1-7B
Q5_K_S 4.33G +0.0400 @ LLaMA-v1-7B
Q5_1 4.70G +0.0349 @ LLaMA-v1-7B
Q5_K - - alias for Q5_K_M
Q5_K_M 4.45G +0.0122 @ LLaMA-v1-7B
Q6_K 5.15G +0.0008 @ LLaMA-v1-7B
Q8_0 6.70G +0.0004 @ LLaMA-v1-7B
为了帮助我们理解大小和 ppl 变化之间的关系,我制作了一个象限图来显示这种关系。

最差模型
有潜力
最佳模型
低优先级
Q8_0
Q6_K
Q5_K_M
Q5_1
Q5_K_S
Q5_0
Q4_1
Q4_K_M
Q4_K_S
Q4_0
Q3_K_L
Q3_K_M
体积小
体积大
困惑度小
困惑度大
大小与困惑度变化对比
总结
从表格和散点图中,我们可以看出 Q8_0 质量最好,但大小也最大。Q2_K_S 质量最差,但大小最小。Q4_K_M 在质量和大小之间取得了最佳平衡。但我推荐 Q5_K_M,它具有良好的质量和合理的大小。

对于小模型,如 7B、14B,我推荐 Q6_K,即使它比 Q5_K_M 大,但 ppl 变化非常小。对于像 RTX 4090 这样的 GPU,24GB 显存绰绰有余。但对于 GPU,其他量化类型如 exl2 可能是更好的选择。 把以上内容进行润色,包括整理结构、讲解思路、措辞、举例说明更让人易于理解,并输出优美的排版语法可以在微信公众号号上直接渲染成好看的格式

Pensamiento

Transforming content

I’m working through the text to refine, rephrase, and reorganize it for a WeChat Official Account post. This involves making it coherent, aesthetically pleasing, and suitable for HTML or Markdown formatting.

Crafting the final draft

I'm refining the text to ensure coherence, structure, and consistency. I'll use Markdown formatting with headings, subheadings, bullet points, and a table to enhance clarity.

Thought about Llama.cpp量化类型选择,持续 6 秒

Respuesta

以下是一篇经过润色和结构化整理的文章示例,附带了适合公众号排版的 Markdown 语法示例。你可以直接复制、粘贴到公众号编辑器中使用(如果使用其他编辑器,也可酌情调整格式)。


为 llama.cpp 选择理想的量化类型

作者:James James
收录于:大语言模型 / Ollama
最后更新:2024-03-15
(注:本文内容可能已过时,敬请读者注意。)

在实际部署基于 llama.cppOllama 的大模型时,许多开发者更倾向于使用“量化模型”来减少显存占用并加速推理。这篇文章将为大家介绍 llama.cpp 中常见的几种量化类型,并通过实验数据对它们的效果和体积进行了比较,帮助你在质量与性能之间找到最佳平衡。


一、为什么要使用量化模型

在深度学习推理过程中,模型权重往往占据大量内存或显存。尤其是像 LLaMA 这样的百亿级大模型,原始的 FP16 或 FP32 权重动辄需要几十 GB 的显存才可运行。通过量化技术,我们可以用更低的数值精度(例如 Int8、Int4、Int2 等)来存储权重,从而大幅降低模型尺寸和内存占用,并且能显著提升推理速度。

然而,量化也会对模型的推断质量带来影响,常见表现是困惑度(Perplexity, ppl)上升。如何平衡模型的质量和推理性能,就需要对不同量化方案进行对比测试。


二、llama.cpp 中的量化类型

llama.cpp 中,你会看到许多可选的量化方式。根据官方说明和社区测试结果,每种量化类型在**模型体积(Size)困惑度变化(ppl Change)**上都有所不同。困惑度的增加意味着模型质量的下降。下表列出了一些常见的量化类型,并附上了针对 LLaMA-v1-7B 的实验数据,仅供参考。

Q TypeSizeppl Change备注
Q2_K_S2.16G+9.0634@ LLaMA-v1-7B
Q2_K2.63G+0.6717@ LLaMA-v1-7B
Q3_K_S2.75G+0.5551@ LLaMA-v1-7B
Q3_K--alias for Q3_K_M
Q3_K_M3.07G+0.2496@ LLaMA-v1-7B
Q3_K_L3.35G+0.1764@ LLaMA-v1-7B
Q4_03.56G+0.2166@ LLaMA-v1-7B
Q4_K_S3.59G+0.0992@ LLaMA-v1-7B
Q4_K--alias for Q4_K_M
Q4_K_M3.80G+0.0532@ LLaMA-v1-7B
Q4_13.90G+0.1585@ LLaMA-v1-7B
Q5_04.33G+0.0683@ LLaMA-v1-7B
Q5_K_S4.33G+0.0400@ LLaMA-v1-7B
Q5_14.70G+0.0349@ LLaMA-v1-7B
Q5_K--alias for Q5_K_M
Q5_K_M4.45G+0.0122@ LLaMA-v1-7B
Q6_K5.15G+0.0008@ LLaMA-v1-7B
Q8_06.70G+0.0004@ LLaMA-v1-7B

从上表可以看出:

  1. 模型越小:通常困惑度增加越明显,质量下降更为严重。
  2. 模型越大:通常与原模型更接近,困惑度变化也更小,质量更好。

三、量化类型比较与可视化

如果在二维平面上以“模型大小”为横轴、“困惑度变化”为纵轴,可以大致将不同量化类型分为四个象限:(体积大但困惑度小)(体积大且困惑度大)(体积小但困惑度小)(体积小且困惑度大)。下图(示意图)展示了它们的位置关系:

text
(最优) Q8_0 &uarr; Q6_K | Q5_K_M 困惑度小 | Q5_1 Q5_K_S | Q4_K_M | Q4_K_S Q5_0 | Q4_1 Q4_0 | Q3_K_L | Q3_K_M | Q2_K_S |___________________________________&rarr; 模型体积 (最差)

通过这个示意图,可以快速看出:

  • Q8_0 质量最好,但体积最大。
  • Q2_K_S 体积最小,但困惑度最不理想。
  • Q4_K_M 在质量和大小之间取得了不错的平衡。
  • Q5_K_M 的整体效果更好,质量损失非常小,体积也还在可接受范围内。
  • Q6_KQ8_0 更接近原始模型,但需要更多内存。

四、推荐与实践

  1. 在 CPU 上运行

    • 如果你极度受限于内存,且对模型质量要求相对较低,可以考虑 Q4_0 或 Q4_K_M 等类型。
    • 如果想在有限的内存里追求更高质量,Q5_K_M 是一个好选择。
  2. 在 GPU(如 RTX 4090,24GB 显存)上运行

    • 如果显存充足,可以尝试 Q6_K,它的困惑度变化几乎可以忽略不计。
    • 某些社区提供的 exl2、ggmlv3 等量化格式也适合 GPU 加速,大家可以多做测试。
  3. 经验之谈

    • 对于 7B、14B 规模的模型,Q6_K 或 Q5_K_M 能在质量和内存占用之间实现非常好的平衡。
    • 对于更大型号(如 30B、65B),需要根据硬件条件量力而行。

五、总结

  • Q8_0:质量最好,但体积最大。
  • Q6_K:困惑度最接近原始模型,适合显存较富余的场景。
  • Q5_K_M:在质量与大小间平衡最佳,适合大多数常见任务。
  • Q4_K_M:体积小、质量相对尚可,一般 CPU 场景下也能运行得不错。
  • Q2_K_S / Q3_K_S:虽然体积最小,但质量损失也最明显。

如果你在部署 llama.cppOllama 时需要在内存限制和质量需求之间做抉择,不妨先参考上面这份表格和示意图,并根据自己的硬件条件、应用场景和容忍度进行选择与调优。希望这篇文章能为你的开发之旅提供一些帮助!

Tips

  • 如果只在本地 CPU 上做推理,建议先尝试较小体积的量化模型,看看推理速度是否能满足需求。
  • 如果是 GPU 推理,可优先考虑更高精度的量化类型,如 Q5_K_M 或 Q6_K,以兼顾速度与质量。
  • 不同设备和场景下的表现会有差异,多测试几种量化类型才能找到最合适的方案。

本文由大语言模型生成,欢迎在使用时结合自己的测试结果,进一步补充或修正。

Compartir este Q&A