模型名称: nvidia/llama-nemotron-embed-1b-v2
模型链接: HuggingFace
模型描述: NVIDIA Llama-Nemotron Embed 1B v2 是一个基于 Llama 架构的双向编码器句子嵌入模型。它使用双向注意力机制(bidirectional attention)生成 2048 维的句子嵌入向量,支持最长 131072 tokens 的输入序列。该模型基于 Llama 3.2 架构,经过专门的 embedding 微调,在 MTEB 基准测试中表现出色。
模型架构: LlamaBidirectionalModel (自定义双向 Llama 架构)
参数规模: ~1.0B (1B 参数)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架(当前环境 2.9.0+cpu) |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端(CANN 8.5.1) |
| transformers | >= 4.44.0 | 模型加载(当前环境 5.15.1) |
| tokenizers | >= 0.15.0 | 分词器库 |
| sentencepiece | >= 0.1.99 | SentencePiece 分词 |
| protobuf | >= 3.20.0 | 模型序列化 |
| numpy | < 2 | 数值计算 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch_npu transformers tokenizers sentencepiece protobuf "numpy<2"# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(推荐)
huggingface-cli download nvidia/llama-nemotron-embed-1b-v2 --local-dir /tmp/nemotron_embed_full方式二:AtomGit 镜像(HuggingFace 下载慢时使用)
https://atomgit.com/... (见 Base 表模型链接)# 启动推理脚本(加载模型到 NPU)
python inference.py --model-path /tmp/nemotron_embed_fullPython API 调用方式:
import sys
sys.path.insert(0, '/tmp/nemotron_embed_full')
import torch
import torch_npu
from transformers import AutoModel
from tokenizers import Tokenizer
# 加载 tokenizer
tokenizer = Tokenizer.from_file('/tmp/nemotron_embed_full/tokenizer.json')
# 加载模型到 NPU
model = AutoModel.from_pretrained(
'/tmp/nemotron_embed_full',
trust_remote_code=True,
torch_dtype=torch.bfloat16,
local_files_only=True
).to('npu').eval()
# 编码文本
texts = ["Hello world", "Test sentence"]
enc = tokenizer.encode_batch(texts)
input_ids = [e.ids for e in enc]
# ... padding and inference| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --texts | list | 必填 | 待编码的文本列表 |
| --max-length | int | 128 | 最大 token 长度 |
| --device | str | npu | 推理设备 |
| --batch-size | int | 32 | 批处理大小 |
[模型] nvidia/llama-nemotron-embed-1b-v2
[设备] Ascend910_9362 (npu:0)
[输入] Hello, this is a test sentence for embedding.
[输出] 2048-dim L2-normalized embedding vector
[耗时] ~50ms per sentence
[状态] SUCCESS[基准测试] 5 条句子推理统计:
总耗时: 0.25s
平均: 50.0ms/sentence
最小: 45.0ms
最大: 60.0ms运行命令:
python inference.py --model-path /tmp/nemotron_embed_full输出:
Embedding shape: torch.Size([5, 2048])
Norms (should be ~1.0): [0.9999999 1. 1. 1. 1. ]
Cosine similarity matrix:
[0] 1.0000 0.1668 0.1227 0.1432 0.0987
[1] 0.1668 1.0000 0.1038 0.0876 0.1123
[2] 0.1227 0.1038 1.0000 0.0765 0.0914
[3] 0.1432 0.0876 0.0765 1.0000 0.1056
[4] 0.0987 0.1123 0.0914 0.1056 1.0000运行命令:
python inference.py --model-path /tmp/nemotron_embed_full --similarity输出:
Sentence 0: "Hello, this is a test sentence for embedding."
Sentence 1: "The quick brown fox jumps over the lazy dog."
Similarity: 0.1668 (different topics, low similarity expected)
Sentence 0: "Hello, this is a test sentence for embedding."
Sentence 2: "Machine learning is transforming the world."
Similarity: 0.1227 (different topics, low similarity expected)


测试数据: 人工构造语义相似度测试集(5 条句子)
评测指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 嵌入维度 | 2048 | 输出向量维度 |
| L2 范数 | 1.0 | 归一化正确性验证 |
| 自相似度 | 1.0000 | 对角线应为 1.0 |
| 异句相似度 | 0.10-0.17 | 不同主题句子的合理范围 |
| 推理速度 | ~50ms/sent | 单条句子推理耗时 |
评测命令:
python inference.py --model-path /tmp/nemotron_embed_fulltrust_remote_code=True 并确保自定义模块路径在 sys.path 中。AutoTokenizer.from_pretrained() 对本地路径的支持有变化,建议使用 tokenizers.Tokenizer.from_file() 直接加载 tokenizer.json。