DeepSparse 模型性能基准测试指南
概述
DeepSparse 是一个针对稀疏神经网络优化的高性能推理引擎,能够显著提升模型在CPU上的推理速度。本文将详细介绍如何使用DeepSparse的基准测试工具来评估模型性能,帮助开发者了解不同配置下的模型表现。
环境准备
安装DeepSparse
首先需要安装DeepSparse及其依赖项:
pip install deepsparse[onnxruntime]
建议在32核的AWS c6i.16xlarge实例上进行测试,以获得最佳性能表现。
快速入门
稀疏与密集模型性能对比
让我们通过BERT模型来比较DeepSparse在密集模型和稀疏模型上的表现差异。
测试密集BERT模型:
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/base-none --batch_size 64
测试90%稀疏度的BERT模型:
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none --batch_size 64
测试结果显示,稀疏模型达到了889 items/sec的吞吐量,而密集模型仅为102 items/sec,性能提升了8.7倍!
与ONNX Runtime对比
DeepSparse还支持与ONNX Runtime进行性能对比:
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/base-none --batch_size 64 --engine onnxruntime
测试结果表明,在相同条件下,DeepSparse比ONNX Runtime快13.8倍,这充分展示了DeepSparse在稀疏模型优化方面的优势。
高级使用指南
本地模型测试
除了使用预置模型,也可以测试本地ONNX模型:
- 首先下载模型:
sparsezoo.download zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned85_quant-none --save-dir yolov5-download
- 然后测试本地模型:
deepsparse.benchmark yolov5-download/model.onnx
批处理大小优化
批处理大小对性能有显著影响。以ResNet-50为例:
- 批处理大小为1时:
deepsparse.benchmark zoo:cv/classification/resnet_v1-50/pytorch/sparseml/imagenet/pruned95_quant-none --batch_size 1
- 批处理大小为64时:
deepsparse.benchmark zoo:cv/classification/resnet_v1-50/pytorch/sparseml/imagenet/pruned95_quant-none --batch_size 64
在多核CPU上,增大批处理大小通常能更好地利用硬件资源,提高吞吐量。
自定义输入尺寸
通过--input_shape
参数可以调整输入尺寸:
- BERT模型序列长度调整为384:
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none --input_shape [1,384]
- YOLOv5输入图像尺寸调整为320x320:
deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned85_quant-none -i [1,3,320,320]
推理场景选择
DeepSparse支持两种推理场景:
- 同步推理(默认):
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none --scenario sync
适合边缘计算场景,追求单次推理延迟最低。
- 异步推理:
deepsparse.benchmark zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none --scenario async --num_streams 8
适合服务器场景,通过并发处理提高吞吐量。
性能优化建议
- 稀疏模型优先:尽可能使用经过剪枝和量化的稀疏模型,能获得最佳性能提升
- 批处理大小调优:根据实际业务需求和硬件配置,找到最佳批处理大小
- 多核利用:在高核数CPU上,使用异步推理可以更好地利用硬件资源
- 输入尺寸优化:在不影响精度的前提下,适当减小输入尺寸可提高性能
通过合理配置这些参数,开发者可以在不同应用场景下获得最优的模型推理性能。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考