DeepSparse是由Neural Magic開發的一個革命性的CPU推論運行時,專門設計用於利用神經網路的稀疏性來加速深度學習模型的推論過程。該項目通過結合SparseML優化庫,在CPU硬體上實現了卓越的推論效能。
重要更新: 2025年1月,Neural Magic被Red Hat收購,DeepSparse社群版本將於2025年6月2日停止維護並棄用。團隊將轉向基於vLLM的商業和開源解決方案。
DeepSparse為大語言模型推論提供初始支持,包括:
注意:Mac和Windows用戶建議使用Docker Linux容器
pip install deepsparse
pip install deepsparse-nightly
pip install -U deepsparse-nightly[llm]
pip install -e path/to/deepsparse
最低級別的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)
包裝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]
基於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)
DeepSparse收集基本使用遙測數據用於產品使用分析。用戶可通過設置環境變數禁用:
export NM_DISABLE_ANALYTICS=True
該項目基於多篇重要學術論文,包括:
DeepSparse代表了CPU推論優化領域的重大突破,通過創新的稀疏性利用技術,在普通CPU硬體上實現了前所未有的深度學習推論效能。儘管社群版本即將停止維護,但其技術創新和理念將在Red Hat的支持下繼續發展,為AI推論優化領域做出更大貢獻。