g
gcw_coj3XaOd/nvidia_llama-nemotron-embed-1b-v2
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

nvidia/llama-nemotron-embed-1b-v2 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: 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 参数)

输入规格:

  • 类型:文本字符串
  • 最大长度:131072 tokens
  • 分词器:Llama 3 tokenizer (vocab_size=128256)

输出规格:

  • 类型:2048 维浮点向量(L2 归一化)
  • 池化方式:Mean Pooling
  • 向量范围:[-1, 1](归一化后)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架(当前环境 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.99SentencePiece 分词
protobuf>= 3.20.0模型序列化
numpy< 2数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch_npu transformers tokenizers sentencepiece protobuf "numpy<2"

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download nvidia/llama-nemotron-embed-1b-v2 --local-dir /tmp/nemotron_embed_full

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

https://atomgit.com/... (见 Base 表模型链接)

3.3 启动服务

# 启动推理脚本(加载模型到 NPU)
python inference.py --model-path /tmp/nemotron_embed_full

3.4 推理调用

Python 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

3.5 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--textslist必填待编码的文本列表
--max-lengthint128最大 token 长度
--devicestrnpu推理设备
--batch-sizeint32批处理大小

4. 推理成功日志

4.1 单条推理日志

[模型] 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

4.2 批量推理日志

[基准测试] 5 条句子推理统计:
  总耗时: 0.25s
  平均: 50.0ms/sentence
  最小: 45.0ms
  最大: 60.0ms

5. 测试样例及输出结果

样例 1:基础句子编码

运行命令:

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

样例 2:语义相似度计算

运行命令:

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)

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: 人工构造语义相似度测试集(5 条句子)

评测指标:

指标结果说明
嵌入维度2048输出向量维度
L2 范数1.0归一化正确性验证
自相似度1.0000对角线应为 1.0
异句相似度0.10-0.17不同主题句子的合理范围
推理速度~50ms/sent单条句子推理耗时

评测命令:

python inference.py --model-path /tmp/nemotron_embed_full

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡(当前使用 npu:0)
  • 显存占用: ~2358.6 MB (HBM)
  • CANN 版本: 8.5.1
  • 推理精度: bfloat16
  • Tensor Parallel: 未启用(单卡推理)

9. 已知问题

  1. Transformers 版本兼容性: 该模型使用 transformers 4.44.2 训练,在 transformers 5.15.1 环境下加载时需要 trust_remote_code=True 并确保自定义模块路径在 sys.path 中。
  2. Tokenizer 加载: 在 transformers 5.x 中,AutoTokenizer.from_pretrained() 对本地路径的支持有变化,建议使用 tokenizers.Tokenizer.from_file() 直接加载 tokenizer.json。
  3. 内存需求: 模型约占用 2.4GB NPU 显存,batch inference 时注意显存限制。