#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.
113abe4acafa848e77ead9c0623205e511932348)query: / passage: )(batch, 2048) L2-normalized embeddings, cosine similarity for retrievalDefault 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) |
| Tokenizer | tokenizer.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。
Ascend910, SOC asc910), CANN 8.5.1, torch 2.9.0 + torch_npu 2.9.0.post1npu:0, torch.npu.is_available()==True, device_count 2pip 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 修复)。
默认命令(无需参数,自动定位本地快照或 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):
| 句 | 内容 |
|---|---|
| 0 | query: What is the capital of France? |
| 1 | passage: Paris is the capital of France, located in Europe. |
| 2 | query: 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 |
| dtype | float32 |
| max_abs_error | 3.33e-07 |
| mean_abs_error | 5.65e-08 |
| atol / rtol | 1e-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 |
| 稳定 avg | 18.24 ms |
| min / max | 18.06 / 18.43 ms |
| p50 / p90 / p95 | 18.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.png | npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device |
assets/model_result.png | 默认 python inference.py 的真实完整输出(输入摘要、真实嵌入、余弦、同步耗时、状态) |

query: / passage: ,更换 prompt 需重新验证余弦分布。kernel_meta/。