f
gcw_pCMO8DfP/bge-base-en-v1.5
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

BAAI/bge-base-en-v1.5 · 昇腾 Ascend NPU 适配版

BGE (BAAI General Embedding) base-en-v1.5 —— 智源通用英文文本嵌入模型, 完成 昇腾 NPU (Ascend910) 端到端适配。输入一段英文文本, 输出 768 维归一化语义向量 (mean pooling + L2 normalize), 常用于语义检索 / RAG / 文本相似度。

  • 原始模型: https://huggingface.co/BAAI/bge-base-en-v1.5
  • 适配工程: /opt/atomgit/npu_adapt/BAAI/bge-base-en-v1.5
  • 本仓库: https://ai.gitcode.com/gcw_pCMO8DfP/bge-base-en-v1.5

模型说明

项目内容
任务特征提取 (feature-extraction) — 文本语义嵌入
架构BertModel (BERT-base, 12 层 / hidden=768 / 12 heads / max_seq=512 / vocab=30522 / gelu / absolute PE)
输入任意英文文本, 自动 tokenize + padding + truncation 至 512
输出768 维向量, 最后一层 hidden state mean pooling + L2 归一化
权重model.safetensors (fp32, 约 438 MB, 201 张量)
用途语义相似度 / 稠密检索 / RAG 向量化
LicenseMIT

环境

  • 昇腾 Ascend910×2, CANN 8.5.1, torch 2.9.0+cpu + torch_npu 2.9.0.post1
  • transformers 4.57.6 / fastapi / uvicorn / pydantic
  • 设备统一 npu:0, 全域无 CUDA 调用 (已移除所有 cuda 相关语句)

快速开始

# 1) 创建 venv 软链 (复用已装好 torch_npu 的基线环境)
ln -s /opt/atomgit/npu_adapt/lerobot/smolvla_base/.venv .venv

# 2) 单文本嵌入 (CLI)
./.venv/bin/python inference.py --text "What is BGE model?"

# 3) FastAPI 服务 (POST /predict JSON, /health 健康检查)
./.venv/bin/python inference.py --server --port 8000
#    然后: curl -X POST http://127.0.0.1:8000/predict -H 'Content-Type: application/json' -d '{"text":"What is BGE model?"}'

权重缓存固定 ./model_weights/BAAI/bge-base-en-v1.5/, 优先本地缓存, 缺失时自动从镜像下载 (ai.gitcode.com/hf_mirrors → hf-mirror.com 回退)。

CLI 推理示例

$ ./.venv/bin/python inference.py --text "What is BGE model?"
[权重] 命中本地缓存: .../model.safetensors (438000000 B)
[模型] 模型加载完成, 耗时 1.2s, 参数量 109.5M
============================================================
模型      : BAAI/bge-base-en-v1.5
设备      : npu:0
输入文本  : What is BGE model?
嵌入维度  : 768
向量模长  : 1.0
前 10 维  : [-0.014606, -0.015887, ..., 0.029768]
推理耗时  : 0.045s
============================================================
{
  "model": "BAAI/bge-base-en-v1.5",
  "pipeline_tag": "feature-extraction",
  "device": "npu:0",
  "count": 1,
  "inference_time_s": 0.045,
  "results": [
    {
      "text": "What is BGE model?",
      "embedding": [-0.014606, -0.015887, ...],
      "embedding_dim": 768,
      "norm": 1.0
    }
  ]
}

FastAPI 返回示例 (POST /predict)

{
  "model": "BAAI/bge-base-en-v1.5",
  "pipeline_tag": "feature-extraction",
  "device": "npu:0",
  "embedding": [-0.014606, -0.015887, -0.005263, 0.016966, -0.010442, -0.004095, -0.017452, 0.011199, -0.000537, 0.029768],
  "embedding_dim": 768,
  "norm": 1.0,
  "inference_time_s": 0.045
}

截图 (昇腾 NPU 推理验证)

部署与任务工作流

agent_workflow

NPU 设备调用 (CLI 单文本嵌入)

npu_device_call

FastAPI 推理结果

model_result

原始日志见 assets/_logs/ (npu_device_call.log, model_result.json 等)。

推理服务 API

端点方法说明
/GET服务说明 (usage)
/predictPOSTJSON {"text":"..."}, 返回 768 维嵌入 JSON
/healthGET健康检查 {"status":"ok","device":"npu:0"}

适配说明 (NPU 关键点)

  1. 初始化顺序: import torch; import torch_npu; torch.npu.set_device(0); 设备字符串统一 npu:0。
  2. CUDA 清理: 源码与推理脚本中已删除全部 cuda 相关语句, 无 torch.cuda.* / .to("cuda")。
  3. 离线加载: AutoModel.from_pretrained(本地权重目录) + AutoTokenizer.from_pretrained(本地权重目录), 不触网加载。
  4. 权重缓存: ./model_weights/BAAI/bge-base-en-v1.5/ 优先本地, 缺失才下载; 镜像限流自动退避。
  5. 池化: 使用 Sentence-BERT 标准 mean pooling (按 attention_mask 对非 padding token 取均值), 再 L2 归一化, 与官方 sentence-transformers 配置 (1_Pooling, word_embedding_type=MEAN) 保持一致; 仅加载 BertModel 主干 (无需分类头), 201 个权重张量全部加载 (missing=0, unexpected=0)。

引用

@misc{bge_embedding,
      title={C-Pack: Packaged Resources To Advance General Chinese Embedding},
      author={Shitao Xiao and Zheng Liu and Peitian Zhang and Niklas Muennighoff},
      year={2023},
      eprint={2309.07597},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}