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推理优化领域做出更大贡献。