LLM 速度基准

项目的 GitHub 仓库

背景故事

我在担任 Anarchy (YC W23) 的兼职工程实习生期间构建了 LLM 速度基准(LLMSB)。LLMSB 是一个用于评估 LLM 模型性能的基准测试工具。它使用 HuggingFace 的 transformers 库来加载和运行 LLM 模型,并测量:

  • 总运行时间
  • 每秒令牌数
  • 通用硬件规格
  • CPU 使用率(当前频率和核心使用百分比随时间变化)
  • RAM 使用率(RAM 和交换空间随时间变化)
  • GPU 使用率(负载、内存使用和温度随时间变化)

[这里] 是对在 H100 上运行的 codellama-13b-oasst-sft-v10 模型的基准运行示例。我个人拥有一块 Nvidia RTX 2070 Ti,其显存为 8 GB。遗憾的是,对于大多数现代 LLM 模型来说,8 GB 的显存不足以加载模型。因此,我使用了 RunPod 来“租用” GPU 并在特定模型上运行我的基准测试。

这就是 LLMSB 的背景/起源故事。由于项目是开源的,你可以在 [这里] 查看代码。下面,我附上了仓库的 README,供你查看。

关于

🚧 LLM Speed Benchmark (LLMSB) 目前处于 beta(v0)阶段。请不要在生产环境中使用,或自行承担风险。我们仍在修复一些问题并改进功能。如果你遇到任何错误或有建议,请在 [ISSUES] 下报告。你的反馈非常宝贵!

LLM Speed Benchmark (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 的模型专用类,如 LlamaTokenizerLlamaForCausalLM,来替代。
  • LLMSB 只收集通用的高层指标。未来我们希望收集更底层的指标。我们认为可以部分通过 PyTorch 的 porfiler wrapper 实现。

示例输出

2023年11月22日

LLMSB 在 [RunPod] 上的 L40 和 H100 GPU 上运行并测试了模型 llama-2-7b-hfcodellama-13b-oasst-sft-v10mpt-7b。请在 [这里] 查看结果。如果发现任何错误/问题,请在 ISSUES 中报告。

设置

  1. 创建并激活 Python 环境:

    python3 -m venv env
    source env/bin/activate
    
  2. 安装软件包依赖(使用 APT):

    apt -y update
    apt install -y vim
    apt install -y neofetch
    
  3. 安装 Python 依赖:

    pip3 install transformers
    pip3 install psutil
    pip3 install gputil
    pip3 install tabulate
    pip3 install sentencepiece
    pip3 install protobuf
    
  4. 安装 Pytorch

    # install pytorch stable build, for linux, using CUDA 12.1:
    pip3 install torch torchvision torchaudio
    
  5. 安装 LLM-VM

    pip install llm-vm
    
  6. (可选)如果使用类似 LLAMA 的模型,需要一个 HuggingFace 访问令牌。请在 [这里] 设置你的访问令牌,然后运行以下命令将令牌保存到控制台:

    huggingface-cli login
    

如何运行

  1. 完成 设置 部分列出的步骤。

  2. 为了配置你的集合,需要创建一个包含以下参数的 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
    }
    
  3. 使用上一步创建的配置文件路径,运行以下命令启动基准测试(任选其一):

    # run one benchmark
    python3 run.py --config ./configs/llmvm_test.json
    
    # run more then one benchmark (in this case 3)
    python3 run.py --config ./configs/llmvm_test.json --loops 3
    
  4. 基准测试完成后,查看类似以下文件的最终结果:

    report_2023-11-25_05:55:04.207515_utc_1ffc4fa7-3aa9-4878-b874-1ff445e1ff8a.json
    

设置 RunPod:

  1. 设置 RunPod,配置你的 ssh 证书/密钥,并启动一个 pod。你可以在 [RunPod 的控制台页面] 访问你的 pod。

  2. 点击 “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>
      
  3. 使用第 2 步的命令,你应该能够 ssh 进入 pod 并使用该 RunPod pod 中选择的 GPU。

  4. 如果想将文件从 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/
      
  5. 使用完 pod 后,关闭或暂停它。但请注意,暂停后仍会产生费用,只是会少很多。

优秀资源