BGE (BAAI General Embedding) base-en-v1.5 —— 智源通用英文文本嵌入模型, 完成 昇腾 NPU (Ascend910) 端到端适配。输入一段英文文本, 输出 768 维归一化语义向量 (mean pooling + L2 normalize), 常用于语义检索 / RAG / 文本相似度。
/opt/atomgit/npu_adapt/BAAI/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 向量化 |
| License | MIT |
torch 2.9.0+cpu + torch_npu 2.9.0.post1transformers 4.57.6 / fastapi / uvicorn / pydanticnpu: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 回退)。
$ ./.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
}
]
}{
"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
}


原始日志见 assets/_logs/ (npu_device_call.log, model_result.json 等)。
| 端点 | 方法 | 说明 |
|---|---|---|
/ | GET | 服务说明 (usage) |
/predict | POST | JSON {"text":"..."}, 返回 768 维嵌入 JSON |
/health | GET | 健康检查 {"status":"ok","device":"npu:0"} |
import torch; import torch_npu; torch.npu.set_device(0); 设备字符串统一 npu:0。cuda 相关语句, 无 torch.cuda.* / .to("cuda")。AutoModel.from_pretrained(本地权重目录) + AutoTokenizer.from_pretrained(本地权重目录), 不触网加载。./model_weights/BAAI/bge-base-en-v1.5/ 优先本地, 缺失才下载; 镜像限流自动退避。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}
}