w
gcw_uQ09W7jl/nvidia-llama-nemotron-embed-1b-v2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

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

1. 模型简介

Llama Nemotron Embedding 1B 是 NVIDIA 基于 Llama-3.2-1B 微调的双向 Transformer 文本嵌入模型,支持 26 种语言、最大 8192 tokens 上下文、Matryoshka 动态维度(384/512/768/1024/2048)。模型采用 LlamaBidirectionalModel 架构(16层、hidden_size 2048、32 heads、GQA 8 kv heads),通过 trust_remote_code=True 加载自定义 llama_bidirectional_model.py 实现非因果双向注意力,池化为 mean-pooling + L2 normalize,适用于问答检索、语义搜索。权重格式 safetensors 单文件 2.47 GB(FP16 显存约 1.2 GB,峰值约 5-6 GB),官方推荐 torch_dtype=bfloat16,本仓 NPU 适配使用 float16 在 npu:0 单卡验证。

  • 模型权重: https://huggingface.co/nvidia/llama-nemotron-embed-1b-v2
  • 本地路径: /tmp/nvidia-llama-nemotron-embed-1b-v2
  • 架构: LlamaBidirectionalModel (llama_bidirec)
  • 任务: text-embedding / dense retrieval
  • 输入: 文本列表,自动前缀 query: / passage:
  • 输出: L2 归一化 embedding [batch, 2048] 及相似度矩阵
  • 依赖: transformers>=4.44, torch, torch_npu, sentence-transformers

虽然任务标签为“计算机视觉”,本模型经 config.json 侦察确认实为 文本嵌入 模型,已按实际输出契约(embedding + similarity)完成 NPU 适配与验证。

2. 验证环境

项值
NPUAscend910_9362 (Ascend910) 单卡 npu:0
CANN8.5.1
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers4.44.2+
Python3.11.14
显存总量61.27 GiB (free ~58.3 GiB benchmark时)
权重大小2.47 GiB safetensors + .bin 镜像,共 4.6 GiB
dtypefloat16 (NPU) / float16 (CPU对比)
设备检查torch.npu.is_available()=True, npu-smi info 可用, torch.npu.get_device_name(0)=Ascend910_9362

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 已包含 torch torch_npu transformers sentence-transformers accelerate huggingface_hub

模型权重需预先下载(离线推理不依赖运行时下载):

from huggingface_hub import snapshot_download
snapshot_download(repo_id="nvidia/llama-nemotron-embed-1b-v2", local_dir="/tmp/nvidia-llama-nemotron-embed-1b-v2", local_dir_use_symlinks=False)
# 或依赖 HF_ENDPOINT=https://hf-mirror.com 自动镜像

4. NPU 推理

默认命令(NPU 单卡 float16):

python inference.py

其他模式:

python inference.py --mode validate   # CPU-NPU 一致性对比
python inference.py --mode benchmark  # 10次稳定推理计时(预热3次,含 torch.npu.synchronize)
python inference.py --dtype float32   # 切换精度

inference.py 已在 npu:0 上完成真实权重加载与端到端推理,非 dummy。

5. 真实推理结果

固定输入(来自官方 README):

queries = ["how much protein should a female eat", "summit define"]
documents = ["As a general guideline, the CDC's average requirement of protein for women ...", "Definition of summit for English Language Learners ..."]
prefix: query: / passage:
tokenize: queries [2,10]  documents [2,74]

NPU (npu:0, float16) 输出:

[OUTPUT] query_embeddings shape: (2, 2048)
[OUTPUT] document_embeddings shape: (2, 2048)
[OUTPUT] query_embeddings[0][:5]: [-0.0146026611328125, -0.0022830963134765625, 0.01157379150390625, 0.016571044921875, -0.01473236083984375]
[OUTPUT] document_embeddings[0][:5]: [-0.016754150390625, 0.01739501953125, -0.00786590576171875, 0.025115966796875, -0.014068603515625]
[OUTPUT] similarity scores (queries x documents):
[[0.5966796875, -0.0452880859375], [-0.0335693359375, 0.4609375]]
[OUTPUT] inference time (end-to-end, batch_queries+batch_documents): 403.65 ms
[CHECK] query0 vs doc0=0.5967 vs doc1=-0.0453 -> PASS
[CHECK] query1 vs doc1=0.4609 vs doc0=-0.0336 -> PASS
[RESULT] PASS

预期对角线相似度显著高于非对角线,与官方示例(0.5968 / 0.4613)一致(float16 误差 <0.001)。

6. CPU-NPU 一致性验证

执行 python inference.py --mode validate 同时加载 CPU 與 NPU 模型对比(已释放 CPU 模型後加载 NPU 避免 OOM):

[VALIDATE] CPU scores: [[0.59716796875, -0.04541015625], [-0.03363037109375, 0.4609375]]
[VALIDATE] NPU scores: [[0.5966796875, -0.0452880859375], [-0.0335693359375, 0.4609375]]
[VALIDATE] max_abs_diff scores: 0.000488
[VALIDATE] cosine similarity eq: [0.9999988675, 0.9999984502] mean=0.999999
[VALIDATE] cosine similarity ed: [0.9999989867, 0.9999993443] mean=0.999999
[VALIDATE] RMSE eq: 0.000038 ed: 0.000030
[VALIDATE] PASS (threshold max_abs_diff<0.02 cosine>0.99)

说明:单样本一致性验证,非全量数据集评测;NPU float16 与 CPU float16 数值高度一致,满足精度阈值。

7. 性能测试

python inference.py --mode benchmark(warmup 3, iters 10, batch=2 queries + 2 docs, max_length 8192, include torch.npu.synchronize):

[BENCHMARK] warmup=3 iters=10 device=npu:0 dtype=float16
[BENCHMARK] avg=36.42 ms min=33.88 ms max=58.51 ms p50=34.00 ms p90=34.05 ms p95=34.05 ms
[BENCHMARK] NPU memory free=58.32 GiB total=61.27 GiB
  • 完整端到端(2 queries 编码 + 2 documents 编码 + pooling + 相似度)平均 36.42 ms
  • 同上首帧编译耗时约 403.65 ms,稳定后 33-34 ms
  • 峰值显存 < 6 GiB,单卡 64 GiB 充足
  • 计时已在关键区间前后调用 torch.npu.synchronize()

8. 自验证截图

  • assets/agent_workflow.png — 下载、侦察、NPU推理、验证、性能全流程日志
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、Ascend910_9362、模型参数 npu:0/float16
  • assets/model_result.png — python inference.py 真实输出、相似度矩阵、耗时、PASS/FAIL

三张图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,含 SHA-256 与退出码。

9. 已知限制

  • 模型要求 trust_remote_code=True,已审查 llama_bidirectional_model.py 与 pooling.py 无网络/文件写入高风险行为。
  • 官方默认 bfloat16,NPU 上实测 float16 数值等价且兼容性更好;bfloat16 在 Ascend910 上部分算子回退,已记录切换原因。
  • 文本长度上限 8192,需预先 truncation=True;更长文本需分块。
  • 当前仅单样本一致性验证,未跑完整 BEIR/MTEB;多并发与大 batch 未测试。
  • 权重未随仓提交,需自行 snapshot_download 至 /tmp/nvidia-llama-nemotron-embed-1b-v2。
  • 任务标签与模型实际模态不一致(标签“计算机视觉” vs 实际“文本嵌入”)已在侦察阶段纠正。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910