liuhongwei-2026/UAE-Large-V1
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

UAE-Large-V1 on Ascend NPU

模型适配 · 文本嵌入 / 语义检索(bi-encoder)基础模型 · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: WhereIsAI/UAE-Large-V1(AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/WhereIsAI/UAE-Large-V1)
  • 模型类型: BERT-large bi-encoder 句向量模型 —— 基于 bert-large-uncased(24 层 / hidden 1024 / 16 头),将句子编码为 1024 维向量,面向语义检索 / 语义相似度 / RAG 向量召回场景。UAE-Large-V1 发布时登顶 MTEB 英文排行榜,是知名的高质量英文嵌入基线。
  • 参数量: 335,141,888(≈335.1M,纯 encoder + pooler,无 LM head)
  • 句向量维度: 1024(1_Pooling/config.json:word_embedding_dimension: 1024)
  • 池化方式: [CLS] token pooling(pooling_mode_cls_token: true),不做 L2 归一化(modules.json 仅含 Transformer + Pooling 两个模块,无 Normalize)
  • 最大输入长度: 512(sentence_bert_config.json)
  • 权重格式: fp32 · model.safetensors(≈ 1.25 GiB)
  • 适配状态: SUCCESS
  • 适配说明: BERT 为 transformers 内置模型(BertModel),零代码改动直接用 AutoModel.from_pretrained 加载本地 model.safetensors(strict 加载,0 missing / 0 unexpected)。官方 UAE 的 [CLS] pooling 在 inference.py 中复现,与官方 sentence-transformers 输出逐位一致。所有算子(Embedding / LayerNorm / GELU / 绝对位置编码自注意力 / Linear)均映射到 torch_npu 的 aclnn* NPU kernel,无 host 侧 CPU 回退。非 LLM 模型不适用 vLLM-Ascend chat 服务,提供 --serve FastAPI HTTP 服务化推理。

2. 适配要点(昇腾 NPU 移植)

  1. 零代码改动加载:模型为 transformers 内置 BertModel,权重 model.safetensors 中仅含 encoder + pooler(无 lm_head 键),AutoModel.from_pretrained strict=True 直接加载,无需 vendor 官方源码(不依赖 angle_emb / sentence-transformers)。
  2. 复现官方 [CLS] pooling(无归一化):1_Pooling/config.json 指定 pooling_mode_cls_token: true,modules.json 无 Normalize 模块。inference.py 的 embed() 复现官方逻辑:
    sentence_embeddings = model_output.last_hidden_state[:, 0]   # [CLS] pooling
    # 无 L2 归一化 —— UAE 官方输出原始向量
  3. 检索 query 指令前缀(Prompts.C):模型 card 明确要求检索任务对 query 追加 Prompts.C(document 不需要),非检索任务不追加任何前缀:
    Prompts.C = "Represent this sentence for searching relevant passages: "
    inference.py 以 --query-prompt 参数内置该前缀,/v1/similarity 端点默认对 query 追加。
  4. 全算子 NPU 原生:BERT 仅使用 nn.Embedding / nn.LayerNorm / GELU / 绝对位置编码自注意力(F.scaled_dot_product_attention)/ nn.Linear 等,全部映射到 torch_npu 的 aclnn* kernel,推理全程无 host 侧 CPU 回退。
  5. 余弦相似度计算:UAE 句向量未归一化,相似度须用余弦相似度(先归一化再点积),inference.py 的 cosine() 与 /v1/similarity 均按此实现。

3. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
CANN8.5.1
NPUAscend 910B
Python3.11.14
transformers4.57.6
numpy1.26.4
safetensors0.7.0
huggingface-hub0.36.2
fastapi / uvicorn0.123.10 / 0.46.0

4. 精度评测

4.1 官方实现交叉验证(CPU):以官方 sentence-transformers 5.7.0 pipeline(模块 0=BERT → 1=CLS pooling,无 Normalize)为参考,对 8 个英文句子分别用官方与 inference.py(CPU)编码,比较 1024 维句向量:

指标数值
Cosine Similarity(官方 ST vs 本实现, 逐句均值)1.000000000
Cosine Similarity(官方 ST vs 本实现, 逐句最小值)0.999999881
最大绝对误差0.0

本实现与官方 sentence-transformers 逐位一致,证明 [CLS] pooling(无归一化)复现正确。

4.2 CPU vs NPU 精度:对同一组 8 个句子分别在 CPU 与 NPU(fp32)上编码:

指标数值
Cosine Similarity (NPU vs CPU, 逐句均值)0.999993324
Cosine Similarity (NPU vs CPU, 逐句最小值)0.999991477
最大绝对误差6.78e-03

句向量为稠密向量,各分量可接近 0,相对误差无意义,故以上以余弦相似度为准。误差来源为 NPU/CPU 浮点累加顺序差异,对下游检索排序无影响。

5. 性能(NPU vs CPU,fp32,max_seq_length=512)

指标NPU (npu:0)CPU
encode 单句延迟13.912 ms/句337.908 ms/句
encode batch=32 延迟0.558 ms/句142.19 ms/句
语义检索 demo(1 query + 3 docs)30.39 ms786.93 ms
峰值 HBM1.646 GB—

batch=32 时 NPU 相对 CPU 约 254× 加速,单句约 24.3×,demo 约 25.9×。

6. 使用方法

6.1 安装依赖

pip install -r requirements.txt

6.2 下载模型权重

# 方式一:ModelScope
python -c "from modelscope.hub.snapshot_download import snapshot_download; snapshot_download('WhereIsAI/UAE-Large-V1', local_dir='/tmp/models/UAE-Large-V1')"
# 方式二:AtomGit 镜像
git clone https://atomgit.com/hf_mirrors/WhereIsAI/UAE-Large-V1.git /tmp/models/UAE-Large-V1
# 方式三:HuggingFace(国内可用 hf-mirror 镜像)
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download WhereIsAI/UAE-Large-V1 --local-dir /tmp/models/UAE-Large-V1

6.3 单次推理(NPU,语义检索 demo)

python3 inference.py --model-dir /tmp/models/UAE-Large-V1 --device npu:0 --report report.json

输出 JSON 报告(含余弦分数、排序结果、延迟、峰值 HBM、参数量)。

6.4 CPU vs NPU 精度对比(含官方 ST 交叉验证)

python3 inference.py --model-dir /tmp/models/UAE-Large-V1 --device npu:0 --compare

6.5 服务化推理(FastAPI HTTP)

python3 inference.py --model-dir /tmp/models/UAE-Large-V1 --device npu:0 --serve --port 8010
端点方法说明
/healthGET健康检查
/v1/encodePOST批量编码句向量(1024 维,未归一化)
/v1/similarityPOSTquery + documents 的余弦相似度及排序
# 示例:语义检索(query 自动追加 Prompts.C 前缀)
curl -X POST http://127.0.0.1:8010/v1/similarity -H "Content-Type: application/json" \
  -d '{"query":"what is the weather?","documents":["The weather is great!","it is rainy today.","i am going to bed"]}'

7. 推理结果示例

query: what is the weather?   (Prompts.C: "Represent this sentence for searching relevant passages: ")
#1  The weather is great!   (score=0.753556)
#2  it is rainy today.      (score=0.694417)
#3  i am going to bed       (score=0.366964)

8. 目录结构

UAE-Large-V1/
├── inference.py        # 推理脚本(单次/对比/服务化三种模式)
├── README.md           # 本文件
├── requirements.txt    # 环境依赖清单
└── assets/             # 截图素材(agent_workflow.png / npu_device_call.png / model_result.png)

9. 参考

  • 模型主页: https://huggingface.co/WhereIsAI/UAE-Large-V1
  • AnglE 官方仓库(UAE 训练框架): https://github.com/SeanLee97/AnglE
  • UAE 论文: AnglE-optimized Text Embeddings(arXiv:2309.12871,ACL 2024)