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

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

#NPU

nvidia/llama-nemotron-embed-1b-v2 is an NV-EmbedQA 1B bidirectional Llama-3.2 embedding model (16 layers, hidden 2048, intermediate 8192, 32 heads, vocab 128256, max 131k positions with Llama3 RoPE scaling). This repository adapts the model to run on a single Ascend 910 NPU (npu:0) via torch_npu + sentence_transformers.

模型简介

  • Model: nvidia/llama-nemotron-embed-1b-v2 (revision 113abe4acafa848e77ead9c0623205e511932348)
  • Task: text embedding / retrieval (query & passage with prompts query: / passage: )
  • Architecture: LlamaBidirectionalModel (non-causal attention), mean pooling + L2 normalize, 2048-dim
  • Parameters: ~1.2B (≈2.4 GB safetensors, bf16 storage)
  • Output: (batch, 2048) L2-normalized embeddings, cosine similarity for retrieval
  • License: Apache-2.0
  • Reference: Model Card · llama_bidirectional_model.py

数据契约

Default evaluation uses 3 retrieval-style sentences (same preprocessing as official SentenceTransformer example):

项值
数据源手工 retrieval 样例(prompt 包含 query/passage)
样例query: What is the capital of France?, passage: Paris is the capital of France, located in Europe., query: Hello world, testing embedding on Ascend NPU
提示词query: / passage: (via config_sentence_transformers.json)
Tokenizertokenizer.json (8.9 MB), vocab 128256, bos 128000, eos 128001
最大长度8192 (truncation via tokenizer), 实测平均 ~15 tokens /句
预处理Tokenizer -> LlamaBidirectionalModel -> mean pooling (attention_mask) -> L2 normalize
输出(3, 2048) float32, 已归一化 (norm≈1.0)
权重来源先下载到 working/nvidia-llama-nemotron-embed-1b-v2-snap 再从本地加载,正式推理不隐式联网

本地快照:model.safetensors 2,438,??? bytes, tokenizer.json 9,085,657 bytes。

环境

  • Ascend 910 NPU (Ascend910, SOC asc910), CANN 8.5.1, torch 2.9.0 + torch_npu 2.9.0.post1
  • Python 3.11, transformers 4.57.6, sentence_transformers 5.7.0, safetensors
  • 单卡 npu:0, torch.npu.is_available()==True, device_count 2

安装

pip install -r requirements.txt

权重可通过 huggingface_hub 下载至 working/nvidia-llama-nemotron-embed-1b-v2-snap,或使用 HF_ENDPOINT=https://hf-mirror.com 镜像;支持代理 http://127.0.0.1:7890(已配置 no_proxy bypass 修复)。

NPU 推理

默认命令(无需参数,自动定位本地快照或 HF):

python inference.py

显式指定:

python inference.py --model-dir /opt/atomgit/adapt-npu-agent/working/nvidia-llama-nemotron-embed-1b-v2-snap --device npu:0

脚本会把模型与输入 Tensor 显式搬到 npu:0,前后均 torch.npu.synchronize() 计时,并打印 param device/dtype、输入输出 device、同步耗时与 [STATUS] NPU inference PASS。

真实结果

输入 3 句(batch=3, dim=2048, dtype=float32, normalize=True):

句内容
0query: What is the capital of France?
1passage: Paris is the capital of France, located in Europe.
2query: Hello world, testing embedding on Ascend NPU

输出:

embeddings shape (3, 2048) device npu:0 dtype torch.float32
emb[0] norm 1.0000 sample [-0.0166, 0.0411, -0.0157, 0.0324, 0.0451]
emb[1] norm 1.0000 sample [-0.0090, 0.0190, -0.0054, -0.0073, 0.0173]
emb[2] norm 1.0000 sample [0.0455, 0.0207, 0.0253, 0.0079, 0.0387]
cosine(query_France, passage_Paris)=0.4886
cosine(query_France, query_Hello)=0.0142
L2 normalize PASS
[STATUS] NPU inference PASS

检索语义正确:相似 query-passage 对(France/Paris)余弦 0.4886 远高于不相关 query 对 0.0142。

一致性

CPU 与 NPU 使用完全相同权重、同一输入、同一预处理、相同 normalize、固定 prompts,分别保存 .npy 后对比:

指标值
输入同上 3 句, batch 3
输出 shape(3, 2048) CPU=NPU
dtypefloat32
max_abs_error3.33e-07
mean_abs_error5.65e-08
atol / rtol1e-4 / 1e-3
归一化均 PASS (norm 1.0±1e-3)
余弦 diff<1.1e-07 (diagonal)
结果PASS (smoke consistency, 3 sentences)

阈值说明:float32 嵌入量纲约 ±0.05,NPU 与 CPU 在 1e-7 量级内一致,远小于 atol 1e-4。

性能

单卡 npu:0, batch=3, seq ~15 tokens, dim 2048, float32, torch.npu.synchronize() 前后计时:

指标值
首次运行(含编译)18.2–19.4 ms
稳定 avg18.24 ms
min / max18.06 / 18.43 ms
p50 / p90 / p9518.29 / 18.43 / 18.43 ms
吞吐164.4 sentences/s
峰值显存4735.1 MiB (torch.npu.max_memory_allocated())
迭代3 warmup + 10 measured

预处理(tokenization + pooling)计入推理耗时;warmup 后稳定。

证据图

以下三张图由 xterm.js 根据本次真实日志生成(非手工绘制),仅展示真实命令输出:

图片说明
assets/agent_workflow.png侦察、下载、加载、NPU 推理、一致性、benchmark 与提交校验完整工作流
assets/npu_device_call.pngnpu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
assets/model_result.png默认 python inference.py 的真实完整输出(输入摘要、真实嵌入、余弦、同步耗时、状态)

agent workflow npu device call model result

限制

  • 本仓为 smoke consistency(3 句)与单卡推理验证,不构成完整检索榜单(MTEB/BEIR 未在此评测)。
  • 默认 prompts query: / passage: ,更换 prompt 需重新验证余弦分布。
  • 权重约 2.4 GB,需自行下载;本仓不提交权重、缓存、kernel_meta/。
  • 仅验证 NPU 推理;训练仍需 GPU。