本文档涵盖了 llama.cpp 全面的测试基础设施,包括后端操作验证、持续集成工作流和跨平台测试系统。该测试框架确保了跨不同硬件后端的数值一致性,验证了模型的准确性,并维护了性能基准。
有关支持测试的构建系统的信息,请参阅 构建系统。有关 CI/CD 管道配置的详细信息,请参阅 CI/CD 管道。
llama.cpp 测试框架由多个相互关联的组件组成,用于验证系统的不同方面
测试架构
来源: tests/test-backend-ops.cpp1-22 ci/run.sh1-50
test-backend-ops 是测试框架的核心,它验证了所有支持的后端上的 GGML 操作,以确保数值一致性和正确性。
后端测试工作流
来源: tests/test-backend-ops.cpp320-594 tests/test-backend-ops.cpp388-401
每个 GGML 操作测试都继承自 test_case 基类,并实现了特定的验证逻辑
| 方法 | 目的 | 默认值 |
|---|---|---|
build_graph() | 构建计算图 | 抽象 |
max_nmse_err() | 最大归一化均方误差 | 1e-7 |
max_maa_err() | 最大平均绝对不对称性 | 1e-4 |
initialize_tensors() | 设置输入数据 | 随机均匀分布 |
grad_eps() | 梯度有限差分步长 | 1e-1 |
来源: tests/test-backend-ops.cpp320-367
测试框架使用多个指标来验证数值一致性
归一化均方误差 (NMSE)
平均绝对不对称性 (MAA)
来源: tests/test-backend-ops.cpp176-223
框架包含哨兵张量来检测缓冲区溢出和内存损坏
来源: tests/test-backend-ops.cpp394-401 tests/test-backend-ops.cpp522-533
框架在三种不同的模式下运行,每种模式服务于不同的验证目的
比较两个后端之间的输出,以确保数值一致性
测量操作吞吐量和内存带宽
使用有限差分法近似验证梯度
来源: tests/test-backend-ops.cpp314-318 tests/test-backend-ops.cpp435-594 tests/test-backend-ops.cpp596-747 tests/test-backend-ops.cpp749-980
CI 系统通过 ci/run.sh 脚本协调跨多个平台和配置的测试
CI 管道流程
来源: ci/run.sh129-242 ci/run.sh304-436 ci/run.sh440-580
CI 框架为不同的场景提供了专门的测试函数
| 功能 | 目的 | 测试覆盖 |
|---|---|---|
gg_run_ctest_debug | 调试模式单元测试 | 除 test-opt 外的所有 ctest 标签 |
gg_run_ctest_release | 发布模式单元测试 | 所有 ctest 标签 |
gg_run_test_scripts_debug | 工具验证(调试) | gguf-split、quantize 工具 |
gg_run_ctest_with_model | 基于模型的测试 | 实际模型推理验证 |
来源: ci/run.sh133-194 ci/run.sh196-242 ci/run.sh260-300
CI 系统使用实际模型验证完整的推理管道
来源: ci/run.sh463-488 ci/run.sh490-512 ci/run.sh519-530
测试框架包含了全面的性能测量能力
性能测试工作流
来源: tests/test-backend-ops.cpp654-747
框架根据硬件类型使用不同的性能目标
| 硬件类型 | FLOP 目标 | 内存目标 | 运行次数计算 |
|---|---|---|---|
| CPU | 8 GFLOP | 8 GB | target_flops / op_flops(out) |
| 显卡 | 100 GFLOP | 32 GB | target_size / op_size(out) |
来源: tests/test-backend-ops.cpp658-672
梯度测试模式通过比较解析梯度和数值近似来验证自动微分
该系统使用平均绝对不对称性来比较梯度,同时过滤无效结果
来源: tests/test-backend-ops.cpp908-956 tests/test-backend-ops.cpp341-349 tests/test-backend-ops.cpp356-361
| 参数 | 目的 | 默认值 |
|---|---|---|
grad_eps() | 有限差分步长 | 1e-1f |
grad_precise() | 使用 4 点法还是 2 点法 | false |
grad_nmax() | 要检查的最大梯度数 | 10000 |
grad_expect() | 不连续函数的预期梯度值 | {} |
来源: tests/test-backend-ops.cpp341-361
该测试框架确保 llama.cpp 在各种硬件平台上保持高可靠性和性能,同时为前向和后向计算路径提供全面的验证。