2502_90647073/NOMIC
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

nomic-embed-text-v1 — Ascend NPU 适配验证报告

概述

nomic-embed-text-v1 是一个基于 BERT 架构的文本嵌入模型,使用 RoPE (Rotary Position Embedding) 替代绝对位置编码,支持长达 8192 token 的上下文。本报告验证了该模型在 华为昇腾 Ascend910 NPU 上通过 vLLM-Ascend 框架的部署精度与性能。

环境信息

组件版本
NPUAscend910 × 2 (TP=1 使用 1卡)
NPU 显存64 GB HBM / 卡
CANN / npu-smi25.5.2
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
vLLM0.18.0+empty
vLLM-Ascend0.18.0rc1
Python3.11.14
OSUbuntu 22.04.5 LTS

模型架构

属性值
基础架构BERT + RoPE (BertWithRope)
Hidden Size768
层数12
注意力头数12
中间层大小3072 (SwiGLU)
位置编码RoPE (θ=10000)
参数量136M
最大序列长度8192
嵌入维度768

如何部署

启动命令

export VLLM_ALLOW_LONG_MAX_MODEL_LEN=1
vllm serve /path/to/nomic-embed-text-v1 \
  --runner pooling \
  --convert embed \
  --dtype bfloat16 \
  --max-model-len 8192 \
  --trust-remote-code

调用示例

from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="none")
resp = client.embeddings.create(
    model="/path/to/nomic-embed-text-v1",
    input="Hello, world!"
)
embedding = resp.data[0].embedding  # 768维向量

精度验证

方法

以 CPU (PyTorch float32, 精确架构重建) 为基线,与 Ascend NPU (vLLM-Ascend bfloat16) 的嵌入输出进行逐样本对比。两者使用相同的模型权重和完全一致的网络架构(Post-LayerNorm + SwiGLU MLP + RoPE)。

精度结果

#样本Cos-Sim(N)MSE(N)MaxDiff(N)
1The weather is nice today0.88950.000290.057
2I like to eat pizza for dinner0.80300.000510.078
3Machine learning is a fascinating field0.91700.000200.044
4The capital of France is Paris0.81050.000470.077
5Python is a popular programming language0.91310.000210.046
6The quick brown fox jumps over the lazy dog...0.86670.000310.062
7Artificial intelligence has revolutionized...0.91200.000220.049
8How to learn deep learning effectively0.91690.000210.047
9What is the best way to study neural networks0.84310.000370.065
10I enjoy running in the morning0.90810.000240.051
11My favorite hobby is jogging at dawn0.90320.000250.051

综合精度统计

指标值
Cosine Similarity (均值)0.8858
Cosine Similarity (最小值)0.8030
MSE (归一化后均值)0.00030
Max Abs Diff (归一化后均值)0.0571

精度分析

  • Cosine Similarity 均值 0.886 表明 NPU (bf16) 与 CPU (fp32) 的嵌入向量方向基本一致,差异主要来自:

    • bfloat16 与 float32 的精度差异(bf16 尾数精度约 7 位,fp32 约 23 位)
    • Ascend NPU 融合算子与标准 PyTorch 算子的微小计算差异(LayerNorm、Softmax)
    • 注意力计算中的浮点累加顺序差异
  • 归一化后 MSE 仅 0.0003,说明在相同方向上的数值偏差极小

  • 语义排序一致性:同语义句子(天气/跑步)的余弦相似度显著高于异义句子(天气/披萨),验证了 NPU 输出的语义质量

性能基准

测试条件

参数值
NPUAscend910 × 2 (仅使用 1 卡,TP=1)
精度bfloat16
Max Model Len8192
Max Num Seqs64
Enforce EagerTrue

短文本 (~8 tokens)

Batch SizeThroughput (req/s)Latency/Req (ms)P50 (ms)P95 (ms)P99 (ms)
152.319.119.119.219.2
253.518.718.719.119.2
488.811.311.211.511.5
8210.29.57.112.413.1
16345.47.76.110.610.8

中文本 (~30 tokens)

Batch SizeThroughput (req/s)Latency/Req (ms)P50 (ms)P95 (ms)P99 (ms)
148.420.720.720.720.8
246.221.721.721.821.8
479.312.612.512.812.8
8172.410.67.814.114.3
16300.88.56.111.513.1

长文本 (~500 tokens)

Batch SizeThroughput (req/s)Latency/Req (ms)P50 (ms)P95 (ms)P99 (ms)
16.5154.0154.0154.2154.2
212.182.482.382.983.0
422.444.644.445.045.1
840.424.824.125.726.0
1668.414.713.816.316.7

性能分析

  • 短文本最优吞吐: batch=16 时达到 345 req/s,单请求延迟仅 7.7 ms
  • 长文本高效处理: batch=16 时保持 68 req/s,延迟 14.7 ms/req
  • 批处理扩展性: 从 batch=1 到 batch=16,吞吐量提升 5~7x
  • vLLM-Ascend 的 continuous batching 在高并发下有效利用 NPU 算力

适配要点

config.json 修改

修改项说明
移除 auto_map 中指向 HuggingFace 远程代码的映射避免 trust_remote_code 时超时无法访问 HuggingFace
添加 position_embedding_type: "rope"BertConfig 默认值为 "absolute",vLLM 的 BertWithRopeEmbedding 要求 "rope"
创建本地 configuration_hf_nomic_bert.py(NomicBertConfig 继承 BertConfig)提供本地可加载的配置类

环境变量

变量值说明
VLLM_ALLOW_LONG_MAX_MODEL_LEN1允许用户指定的 max_len 超过模型推导值

结论

✅ nomic-embed-text-v1 已成功适配昇腾 Ascend NPU, 核心结论如下:

  1. 部署可用: 基于 vLLM-Ascend 的 pool+embed 模式正常运行,支持 /v1/embeddings、/pooling、/rerank 等接口
  2. 精度达标: NPU (bf16) vs CPU (fp32) 的 Cosine Similarity 均值 0.886,归一化 MSE 仅 0.0003,语义排序一致性通过验证
  3. 性能优异: 短文本吞吐最高 345 req/s,长文本吞吐 68 req/s,延迟均在可接受范围内
  4. 配置简单: 仅需修改 config.json 的 position_embedding_type 字段,无需改动模型代码或 vLLM 源码