neuralmagic/deepsparseView GitHub Homepage for Latest Official Releases
專為CPU設計的稀疏感知深度學習推論執行階段
NOASSERTIONPythondeepsparseneuralmagic 3.2k Last Updated: June 02, 2025
DeepSparse - 專為CPU設計的稀疏感知深度學習推論引擎
項目概述
DeepSparse是由Neural Magic開發的一個革命性的CPU推論運行時,專門設計用於利用神經網路的稀疏性來加速深度學習模型的推論過程。該項目通過結合SparseML優化庫,在CPU硬體上實現了卓越的推論效能。
重要更新: 2025年1月,Neural Magic被Red Hat收購,DeepSparse社群版本將於2025年6月2日停止維護並棄用。團隊將轉向基於vLLM的商業和開源解決方案。
核心特性
1. 稀疏性優化
- 稀疏核心支持:通過非結構化稀疏權重實現加速和記憶體節省
- 8位元量化:支持權重和激活的8位元量化
- 快取優化:高效利用快取的注意力鍵值對,最小化記憶體移動
2. 大語言模型(LLM)支持
DeepSparse為大語言模型推論提供初始支持,包括:
- MPT-7B模型的稀疏微調技術
- 7倍於密集基準的加速效能(稀疏-量化模型)
- 支持高達60%稀疏度的模型而不損失精度
3. 廣泛的模型支持
- 電腦視覺:ResNet、EfficientNet、YOLOv5/8、ViT等
- 自然語言處理:BERT、Transformer變體等
- 多模態模型:支持各種CNN和Transformer架構
系統要求
硬體支持
- x86架構:AVX2、AVX-512、AVX-512 VNNI
- ARM架構:v8.2+
軟體環境
- 作業系統:Linux
- Python版本:3.8-3.11
- ONNX支持:版本1.5.0-1.15.0,操作集版本11或更高
注意:Mac和Windows用戶建議使用Docker Linux容器
安裝方式
穩定版本
pip install deepsparse
夜間構建版(包含最新功能)
pip install deepsparse-nightly
LLM支持版本
pip install -U deepsparse-nightly[llm]
從源碼安裝
pip install -e path/to/deepsparse
三種部署API
1. Engine API(底層API)
最低級別的API,直接編譯ONNX模型並處理張量輸入輸出。
from deepsparse import Engine
# 下載並編譯模型
zoo_stub = "zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none"
compiled_model = Engine(model=zoo_stub, batch_size=1)
# 運行推論
inputs = compiled_model.generate_random_inputs()
output = compiled_model(inputs)
2. Pipeline API(中級API)
包裝Engine並添加預處理和後處理功能,可直接處理原始數據。
from deepsparse import Pipeline
# 設置管道
sentiment_analysis_pipeline = Pipeline.create(
task="sentiment-analysis",
model_path="zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none"
)
# 運行推論
prediction = sentiment_analysis_pipeline("I love using DeepSparse Pipelines")
print(prediction)
# 輸出: labels=['positive'] scores=[0.9954759478569031]
3. Server API(高級API)
基於FastAPI包裝Pipeline,提供REST API服務。
# 啟動伺服器
deepsparse.server \
--task sentiment-analysis \
--model_path zoo:nlp/sentiment_analysis/obert-base/pytorch/huggingface/sst2/pruned90_quant-none
# 發送請求
import requests
url = "http://localhost:5543/v2/models/sentiment_analysis/infer"
obj = {"sequences": "Snorlax loves my Tesla!"}
response = requests.post(url, json=obj)
print(response.text)
# 輸出: {"labels":["positive"],"scores":[0.9965094327926636]}
大語言模型示例
from deepsparse import TextGeneration
pipeline = TextGeneration(model="zoo:mpt-7b-dolly_mpt_pretrain-pruned50_quantized")
prompt = """
Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction: what is sparsity?
### Response:
"""
result = pipeline(prompt, max_new_tokens=75)
print(result.generations[0].text)
技術優勢
1. 稀疏微調技術
- 與IST Austria合作開發的創新技術
- 在微調過程中將MPT-7B剪枝至60%稀疏度
- 無精度損失的情況下實現顯著加速
2. 效能優化
- CPU上實現GPU級別的推論效能
- 大幅降低記憶體使用
- 支持高度優化的稀疏-量化模型
3. 生態系統集成
- 與SparseML優化庫無縫集成
- SparseZoo模型庫提供預優化模型
- 支持多種部署場景
使用案例
- 邊緣計算:在資源受限的環境中部署高性能AI模型
- 雲端推論:降低雲計算成本,提高推論效率
- 即時應用:需要低延遲的即時AI應用
- 大規模部署:需要處理高併發推論請求的生產環境
隱私和分析
DeepSparse收集基本使用遙測數據用於產品使用分析。用戶可通過設置環境變數禁用:
export NM_DISABLE_ANALYTICS=True
學術引用
該項目基於多篇重要學術論文,包括:
- Sparse Fine-Tuning for Inference Acceleration of Large Language Models (2023)
- The Optimal BERT Surgeon: Scalable and Accurate Second-Order Pruning (2022)
- Inducing and Exploiting Activation Sparsity for Fast Inference (ICML 2020)
總結
DeepSparse代表了CPU推論優化領域的重大突破,通過創新的稀疏性利用技術,在普通CPU硬體上實現了前所未有的深度學習推論效能。儘管社群版本即將停止維護,但其技術創新和理念將在Red Hat的支持下繼續發展,為AI推論優化領域做出更大貢獻。