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 单卡验证。
/tmp/nvidia-llama-nemotron-embed-1b-v2query: / passage:虽然任务标签为“计算机视觉”,本模型经
config.json侦察确认实为 文本嵌入 模型,已按实际输出契约(embedding + similarity)完成 NPU 适配与验证。
| 项 | 值 |
|---|---|
| NPU | Ascend910_9362 (Ascend910) 单卡 npu:0 |
| CANN | 8.5.1 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| transformers | 4.44.2+ |
| Python | 3.11.14 |
| 显存总量 | 61.27 GiB (free ~58.3 GiB benchmark时) |
| 权重大小 | 2.47 GiB safetensors + .bin 镜像,共 4.6 GiB |
| dtype | float16 (NPU) / float16 (CPU对比) |
| 设备检查 | torch.npu.is_available()=True, npu-smi info 可用, torch.npu.get_device_name(0)=Ascend910_9362 |
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 自动镜像默认命令(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。
固定输入(来自官方 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)。
执行 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 数值高度一致,满足精度阈值。
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 GiBtorch.npu.synchronize()assets/agent_workflow.png — 下载、侦察、NPU推理、验证、性能全流程日志assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、Ascend910_9362、模型参数 npu:0/float16assets/model_result.png — python inference.py 真实输出、相似度矩阵、耗时、PASS/FAIL三张图由
scripts/render_xterm_evidence.mjs --style raw从本次真实日志渲染,含 SHA-256 与退出码。
trust_remote_code=True,已审查 llama_bidirectional_model.py 与 pooling.py 无网络/文件写入高风险行为。bfloat16,NPU 上实测 float16 数值等价且兼容性更好;bfloat16 在 Ascend910 上部分算子回退,已记录切换原因。truncation=True;更长文本需分块。snapshot_download 至 /tmp/nvidia-llama-nemotron-embed-1b-v2。Hardware: NPU, NPU, Ascend, Ascend910