模型名称: GLM-OCR-8bit 模型架构: GlmOcrForConditionalGeneration 模型类型: 视觉语言模型 (VLM) 任务: 图像文字识别 (OCR) 来源: mlx-community/GLM-OCR-8bit (基于 zai-org/GLM-OCR)
| 参数 | 值 |
|---|---|
| hidden_size | 1536 |
| num_hidden_layers | 16 |
| num_attention_heads | 16 |
| num_key_value_heads | 8 |
| intermediate_size | 4608 |
| max_position_embeddings | 131072 |
| vocab_size | 59392 |
| dtype | bfloat16 |
| image_size | 336 |
| patch_size | 14 |
# 安装 vLLM-Ascend
pip install vllm==0.18.0
pip install vllm-ascend==0.18.0rc1
# 安装 transformers (支持 GLM-OCR)
pip install git+https://github.com/huggingface/transformers.git
# 安装其他依赖
pip install requests numpy pillowfrom modelscope import snapshot_download
model_dir = snapshot_download('mlx-community/GLM-OCR-8bit', cache_dir='./models')# 使用镜像源
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download mlx-community/GLM-OCR-8bit --local-dir ./models/GLM-OCR-8bit# 原始权重
git clone https://atomgit.com/zai-org/GLM-OCR.gitvllm serve /path/to/GLM-OCR-bf16 \
--dtype bfloat16 \
--tensor-parallel-size 1 \
--max-model-len 4096 \
--max-num-seqs 16 \
--port 8000 \
--trust-remote-code \
--served-model-name glm-ocr-8bit \
--enforce-eager参数说明:
--dtype bfloat16: 使用 bfloat16 精度--tensor-parallel-size 1: 单卡部署--max-model-len 4096: 最大序列长度--max-num-seqs 16: 最大并发请求数--trust-remote-code: 信任远程代码--enforce-eager: 使用 eager 模式 (调试用)# 检查服务状态
curl http://localhost:8000/v1/models
# 测试推理
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "glm-ocr-8bit",
"messages": [{"role": "user", "content": "say hi"}],
"max_tokens": 16
}'python inference.py \
--image /path/to/image.jpg \
--prompt "请识别图片中的文字内容" \
--model glm-ocr-8bit \
--output result.jsonimport requests
import base64
def ocr_image(image_path, prompt="请识别图片中的文字内容"):
with open(image_path, "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = requests.post(
"http://localhost:8000/v1/chat/completions",
json={
"model": "glm-ocr-8bit",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
]
}],
"max_tokens": 2048
}
)
return response.json()["choices"][0]["message"]["content"]
# 使用示例
result = ocr_image("test.jpg")
print(result)| 项目 | 配置 |
|---|---|
| 硬件 | Atlas 800 A2 |
| NPU | 昇腾 910B |
| 内存 | 64GB |
| Python | 3.11.14 |
| vLLM | 0.18.0 |
| vLLM-Ascend | 0.18.0rc1 |
| transformers | 5.8.0.dev0 |
| torch-npu | 2.9.0.post1 |
| 指标 | Logits | Hidden States |
|---|---|---|
| max_abs_error | 0.000392 | 0.000046 |
| mean_abs_error | 0.000080 | 0.000008 |
| relative_error | 0.0516% | 0.0067% |
| cosine_similarity | 1.000000 | 1.000000 |
| threshold | 1.0% | 1.0% |
| 结果 | PASS | PASS |
结论:NPU 与 CPU 基线高度一致,cosine_similarity = 1.000000,relative_error < 0.0516%,验证通过。
| 指标 | 值 |
|---|---|
| 模型加载时间 | ~2.2 秒 |
| KV Cache 大小 | 852,096 tokens |
| 最大并发 | 208x (4096 tokens/req) |
| 推理延迟 | ~0.5 秒 (单次请求) |
Q1: 服务启动失败,提示 transformers 不支持 glm_ocr 架构
A: 需要安装最新版本的 transformers:
pip install git+https://github.com/huggingface/transformers.git问题2:处理器加载失败,提示 TokenizersBackend 类型错误
A: 修改 processor_config.json,将 processor_class 改为 "Glm46VProcessor"问题3:量化模型加载失败,提示 KeyError
A: 使用 bf16 版本的模型,或禁用量化:
vllm serve ... --quantization None问题4:内存不足
A: 减少 max-model-len 或 max-num-seqs 参数:
vllm serve ... --max-model-len 2048 --max-num-seqs 8vllm serve ... --compilation-config '{"cudagraph_mode": "FULL_DECODE_ONLY"}'vllm serve ... --enable-prefix-cachingvllm serve ... --max-num-batched-tokens 8192| 文件 | 说明 |
|---|---|
inference.py | 推理脚本 |
eval_accuracy.py | 精度评测脚本 |
test_cases.json | 测试用例 |
README.md | 本文档 |
config.json | 模型配置 |
processor_config.json | 处理器配置 |
model.safetensors | 模型权重 |
本项目基于 MIT 许可证开源。
适配日期: 2026-05-15 适配人员: Model Agent 验证状态: ✅ 通过