light-llm-hp - 轻量级 LLM 推理框架
在 CPU 上运行的简化推理框架,支持 REST API 服务。
🚀 Apple Silicon 优化: 支持 MLX 后端,比 PyTorch MPS 快 2-5 倍
快速开始
from hllm import HLLM
# 自动选择最佳后端 (Apple Silicon 上自动使用 MLX)
model = HLLM(model_path="microsoft/Phi-3-mini-4k-instruct")
# 生成文本
result = model.generate("Write a short story about a robot.")
print(result)
Apple Silicon 优化 (MLX)
在 M1/M2/M3 Mac 上,使用 MLX 后端可获得最佳性能:
# 安装 MLX 支持
pip install light-llm-hp[mlx]
from hllm import HLLM
# 显式使用 MLX 后端 (推荐)
model = HLLM(model_path="mlx-community/Llama-3.2-1B-Instruct-4bit", backend="mlx")
# 或使用 PyTorch MPS
model = HLLM(model_path="microsoft/Phi-3-mini-4k-instruct", backend="pytorch", device="mps")
# 查看后端信息
print(model.get_info())
# {'name': 'mlx', 'device': 'mlx', ...}
性能对比
在 M1 MacBook Pro 上的典型性能 (Llama-3.2-1B, 100 tokens):
| 后端 | 首 token 延迟 | 吞吐量 | 内存占用 |
|---|---|---|---|
| MLX | ~50ms | ~45 tok/s | ~800MB |
| PyTorch MPS | ~150ms | ~15 tok/s | ~1200MB |
| PyTorch CPU | ~500ms | ~5 tok/s | ~1200MB |
运行基准测试:
python examples/benchmark.py
REST API 服务 (OpenAI 兼容)
安装 API 依赖
pip install light-llm-hp[api]
启动服务
python -m hllm.server --model ./TinyLlama-1.1B-Chat-v1.0 --port 8000
使用 OpenAI 官方客户端
import httpx
from openai import OpenAI
# 禁用代理避免 502 错误
http_client = httpx.Client(trust_env=False)
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="not-needed",
http_client=http_client
)
# 对话
response = client.chat.completions.create(
model="hllm-model",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
完整示例:examples/test_openai_client.py
OpenAI 兼容端点
| 端点 | 方法 | 说明 |
|---|---|---|
/v1/models |
GET | 模型列表 |
/v1/chat/completions |
POST | 对话补全 (支持流式) |
/v1/completions |
POST | 文本补全 (支持流式) |
详细 API 文档见 docs/api.md。
目录结构
hllm/
├── hllm/ # 核心模块
│ ├── __init__.py
│ ├── model.py # 模型加载与推理
│ ├── tokenizer.py # 分词器封装
│ ├── generate.py # 生成逻辑
│ ├── server.py # REST API 服务端
│ └── client.py # REST API 客户端
├── tests/ # 测试
├── examples/ # 示例
└── docs/ # 文档
Metadata
Release files for light-llm-hp 0.4.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| light_llm_hp-0.4.5.tar.gz | 26.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| light_llm_hp-0.4.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 45.9 kB
Release files / light_llm_hp-0.4.5.tar.gz
| Download URL | light_llm_hp-0.4.5.tar.gz |
|---|---|
| Size | 26.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5848a2c5ead26b8410d7acf838fb6c72f339ffef857f3c1b67fc1a1ae50c984
|
|
BLAKE2b-256 checksum How to use checksums |
30ebc4ed506036af45c92b2657a3019190e4fede32fb2500ed2c2f3dbc67cae0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Release files / light_llm_hp-0.4.5-py3-none-any.whl
| Download URL | light_llm_hp-0.4.5-py3-none-any.whl |
|---|---|
| Size | 19.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e6b020881c4811ba8578ab559f97c3252476195618c3e49ef4b724921893d36
|
|
BLAKE2b-256 checksum How to use checksums |
c72b58ded7378bfcdceb19b9dbfeb835ce2b22a0e4fcfd77c77b01c40193250e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.15
|
Security for light-llm-hp 0.4.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.