LLM 速度基准
背景故事
我在 Anarchy (YC W23) 担任兼职工程实习生期间构建了 LLM 速度基准(LLMSB)。LLMSB 是一个用于评估 LLM 模型性能的基准测试工具。它使用 HuggingFace 的 transformers 库来加载和运行 LLM 模型,并测量:
- 总运行时间
- 每秒 token 数
- 通用硬件规格
- CPU 使用率(当前频率和各核心使用百分比随时间变化)
- RAM 使用率(内存和交换空间随时间变化)
- GPU 使用率(负载、显存使用和温度随时间变化)
这里 是对 codellama-13b-oasst-sft-v10 模型在 H100 上运行的基准示例。我个人拥有一块 Nvidia RTX 2070 Ti,显存为 8 GB。遗憾的是,对于大多数现代 LLM 模型来说,8 GB 显存不足以加载模型。因此,我使用 RunPod “租用” GPU 并在特定模型上运行基准测试。
这就是 LLMSB 的背景/起源故事。由于项目是开源的,你可以在 这里 查看代码。下面,我附上了仓库的 README,供你参考。
关于
🚧 LLM 速度基准(LLMSB)目前处于 beta(v0)阶段。请勿在生产环境中使用,或自行承担风险。我们仍在修复一些问题并改进功能。如果你发现任何 bug 或有改进建议,请在 ISSUES 中提交。你的反馈非常宝贵!
LLM 速度基准(LLMSB)是一个用于评估不同硬件平台上 LLM 模型性能的基准测试工具。其最终目标是汇总一个全面的数据集,记录 LLM 模型在各种系统上的性能表现,帮助用户更有效地为项目选择合适的 LLM 模型。
限制
LLMSB 仍处于 v0 版本,存在以下限制:
- 仅设计在基于 Debian 的操作系统上运行,即不支持 Windows。这是因为 LLMSB 在底层使用 neofetch 和 nvidia-smi 收集指标,文件路径逻辑基于 Unix 系统。
- 由于指标记录方式的原因,指标收集器可能需要最长 1 秒才能完成一次采集。这意味着在最快情况下,我们每秒只能收集一次硬件指标。
- LLMSB 仅使用 HuggingFace 加载和运行模型。此方案目前可行,但未来的目标是让 LLMSB 支持多种框架,而不仅限于 HuggingFace。
- 目前,所有模型均通过位于
src/hf.py
中的run_llm()
函数执行,该函数使用AutoTokenizer()
和AutoModelForCausalLM()
加载并运行模型。这种方式可行,但限制了对特定模型的配置和优化。为此,我们计划为每个流行模型创建独立的类,使用 HuggingFace 的模型专属类,如LlamaTokenizer
与LlamaForCausalLM
。 - LLMSB 只收集通用的高层指标。未来我们希望能够收集更底层的指标。我们认为可以部分通过 PyTorch 的 profiler wrapper 实现。
示例输出
2023年11月22日
LLMSB 在 RunPod 上的 L40 与 H100 GPU 上运行测试。在这些基准中,模型 llama-2-7b-hf、codellama-13b-oasst-sft-v10 与 mpt-7b 均被测试。查看结果请前往 这里。如果发现任何错误/问题,请在 ISSUES 中报告。
设置
-
创建并激活 Python 环境:
python3 -m venv env source env/bin/activate
-
安装系统依赖(使用 APT):
apt -y update apt install -y vim apt install -y neofetch
-
安装 Python 依赖:
pip3 install transformers pip3 install psutil pip3 install gputil pip3 install tabulate pip3 install sentencepiece pip3 install protobuf
-
安装 Pytorch:
# 安装适用于 Linux、CUDA 12.1 的 PyTorch 稳定版: pip3 install torch torchvision torchaudio
-
安装 LLM-VM:
pip install llm-vm
-
(可选)如果使用 LLAMA 等模型,需要 HuggingFace 访问令牌。请在 这里 设置你的访问令牌,然后运行以下命令将令牌保存到控制台:
huggingface-cli login
如何运行
-
完成 设置 部分列出的所有步骤。
-
为你的实验配置 JSON 文件(以下为示例):
- 注意:并非所有框架都支持相同的参数
{ "model": "bigscience/bloom-560m", # the model's path/repo on HuggingFace (https://huggingface.co/models) "prompt": "Hello World!", # the prompt you want to input into the LLM model "device": "cuda:0", # the device you want to run the LLM model on (GPU/CPU) "max_length": 50, # the maximun length of the generated tokens "temperature": 0.9, # temperatue value for the LLM model "top_k": 50, # top-k value for the LLM model "top_p": 0.9, # top-p value for the LLM model "num_return_sequences": 1, # the number of independently ran instances of the model "time_delay": 0, # the time delay (seconds) the metrics-collecter will wait per interation "model_start_pause": 1, # the time (seconds) the test will wait BEFORE running the LLM model "model_end_pause": 1 # the time (seconds) the test will wait AFTER the LLM model is done running, "framework": "llm-vm" # the name of the framework/library you want to use to run the model }
-
使用上一步创建的配置文件路径,运行以下命令启动基准(任选其一):
-
运行单个基准
python3 run.py --config ./configs/llmvm_test.json
-
运行多个基准(此例为 3 次)
python3 run.py --config ./configs/llmvm_test.json --loops 3
-
-
基准运行完成后,查看类似以下名称的结果文件:
report_2023-11-25_05:55:04.207515_utc_1ffc4fa7-3aa9-4878-b874-1ff445e1ff8a.json
设置 RunPod:
-
配置 RunPod,设置你的 SSH 证书/密钥,并启动一个 pod。你可以在 RunPod 的控制台页面 访问你的 pod。
-
点击 “Connect” 按钮获取 SSH 连接信息。该信息大致如下:
ssh root&12.345.678.90 -p 12345 -i ~/.ssh/id_example
-
该命令的格式如下:
ssh <user>@<ip-address> -p <port> -i <local-path-to-ssh-cert>
-
-
使用第 2 步的命令,你即可 SSH 进入 pod 并使用该 RunPod pod 中选择的 GPU。
-
若需将文件从 pod 复制到本地机器,可使用如下格式的命令(对应第 2 步中展示的变量):
scp -P <port> -i <local-path-to-ssh-cert> <user>@<ip-address>:<path-to-file-in-pod> <path-to-local-directory>
-
示例命令:
scp -P 12345 -i ~/.ssh/id_example <user>@<ip-address>:/root/test.txt /home/user1/Downloads/
-
-
使用完 pod 后,请关闭或暂停它。但请注意,暂停后仍会产生费用,只是会少很多。
优秀资源:
- Prompt 数据集: awesome-chatgpt-prompts、bigscience/P3 与 writing-prompts
- 了解更多 LLM 参数
- 用于云端 LLM 模型的优秀基准测试
- 出色的 LLM 智能排行榜: FastEval 与 open_llm_leaderboard