m
mengsihan/llama-nemotron-embed-1b-v2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

llama-nemotron-embed-1b-v2 on Ascend NPU

1. 模型简介

nvidia/llama-nemotron-embed-1b-v2 是 NVIDIA 开源的多语言文本 embedding / 检索模型,基于 Llama3.2-1B 微调,采用双向(非因果)注意力的 LlamaBidirectionalModel 架构。模型支持 26 种语言(含中文)的问答检索、语义搜索和长文档(最长 8192 tokens)表征,输出最大 2048 维可配置 embedding(Matryoshka Embeddings)。

本仓库将该模型真实适配到昇腾 Ascend910B 单卡 NPU,使用模型真实权重完成 npu:0 上的多语言文本检索推理,并完成 CPU-NPU 一致性验证与性能测试。

项目值
模型名称nvidia/llama-nemotron-embed-1b-v2
架构LlamaBidirectionalModel(Llama3.2-1B,双向注意力)
模型类型llama_bidirec
参数量~1B(16 层,hidden_size 2048)
任务多语言文本 embedding / 问答检索
权重格式safetensors(bfloat16)
加载后端transformers AutoModel(trust_remote_code=True)
输出维度2048(平均池化 + L2 归一化)

说明:任务清单将模型类型标注为「计算机视觉」,但该模型为多语言文本 embedding / 检索模型,官方 README 仅定义文本输入(query/passage),不含视觉塔或图像处理器。本仓库按模型真实能力进行文本检索适配,不虚构图像输入路径。

2. 验证环境

项目值
NPU 设备Ascend910B(Ascend910_9362)× 2,使用 npu:0
NPU 驱动npu-smi 25.5.5
CANN8.5.1
PyTorch2.9.0
torch_npu2.9.0.post1
Transformers4.56.2
推理精度bfloat16
峰值显存2.34 GB(batch size 1)

3. 安装依赖

pip install -r requirements.txt

依赖:transformers>=4.44、torch>=2.1.0、torch_npu>=2.1.0。

4. NPU 推理

默认命令执行完整流程:加载真实权重到 npu:0、编码固定查询与文档、计算余弦相似度并输出检索排序、性能统计与 PASS/FAIL:

python inference.py

可选模式:

python inference.py --mode cpu          # CPU 推理(用于一致性对比)
python inference.py --mode benchmark    # 预热3次 + 正式10次性能测试
python inference.py --mode validate     # 仅检索验证
python inference.py --mode consistency  # CPU-NPU 一致性验证

模型权重目录可通过 --model-path <dir> 或环境变量 MODEL_PATH 指定。

5. 真实推理结果

固定测试集:4 条查询(中英文混合)× 6 篇文档(含正确匹配文档),在 npu:0 上真实执行结果:

查询与期望匹配文档:

查询期望文档NPU Top-1相似度
how much protein should a female eat0(CDC 蛋白质摄入指南)00.598
summit define1(summit 定义)10.463
什么是机器学习中的梯度下降2(梯度下降段落)20.660
how to make a password more secure3(强密码建议)30.492

完整相似度矩阵(NPU,bf16):

[[0.598, -0.045, -0.012,  0.104,  0.412, -0.060],
 [-0.033,  0.463, -0.031, -0.021, -0.001,  0.307],
 [ 0.031,  0.078,  0.660, -0.025,  0.034,  0.047],
 [ 0.127,  0.026,  0.032,  0.492,  0.101, -0.011]]

Top-1 检索准确率:4/4 = 1.0(PASS)

6. CPU-NPU 一致性验证

相同权重、相同输入、相同 bf16 精度下,CPU 与 NPU(npu:0)对比:

指标结果
查询 embedding CPU-NPU cosine1.0078
文档 embedding CPU-NPU cosine1.0000
相似度矩阵 max absolute error0.0039
相似度矩阵 mean absolute error0.0009
Top-1 排序是否一致完全一致([0,1,2,3])

结论:CPU 与 NPU 数值一致,Top-1 完全一致,通过一致性验证。

7. 性能测试

在 npu:0、bf16、batch size 1、max_seq_len 1024、4 查询 + 6 文档条件下,预热 3 次后正式测试 10 次:

阶段avgminmaxp50p90p95
查询编码17.69 ms17.5317.8817.6817.8817.88
文档编码17.97 ms17.8418.1217.9418.1018.10
相似度计算0.07 ms0.060.070.070.070.07
端到端35.72 ms35.5235.9035.7335.8335.83

峰值 NPU 显存:2.34 GB。

8. 自验证截图

截图说明
assets/agent_workflow.png下载、侦察、加载、NPU 推理、验证、性能全流程日志
assets/npu_device_call.pngnpu-smi info、torch.npu.is_available()、设备名与模型参数设备
assets/model_result.png默认 python inference.py 完整输出与 PASS

9. 已知限制

  • 模型本身为纯文本 embedding 模型,不支持图像输入;任务清单的「计算机视觉」分类与模型真实能力不一致,本仓库按文本检索适配。
  • 为兼容模型官方自定义代码,使用了 transformers==4.56.2(官方支持 4.44~5.0+);环境预装 transformers 5.7.0 与模型 LlamaBidirectionalConfig 存在 RoPE 参数兼容问题,故固定为 4.56.2。
  • 推理精度为 bf16;与 CPU 相比相似度最大绝对误差 0.0039,在可接受范围内。
  • 最大序列长度受模型限制为 8192 tokens,本验证使用 1024。

10. 标签

NPU、Ascend、Ascend910、Hardware: NPU、text-embeddings、retrieval、semantic-search、transformers