1
gcw_xUvjaUyi/UAE-Large-V1-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

WhereIsAI/UAE-Large-V1 昇腾 NPU 适配

WhereIsAI/UAE-Large-V1(文本向量嵌入模型,输出维度 1024)在 昇腾 Ascend910 NPU 上基于 torch-npu 的适配与推理实现。

本仓库仅含适配代码与说明,权重不入库;推理时从镜像自动下载到本地缓存 ./model_weights/WhereIsAI__UAE-Large-V1(已加入 .gitignore)。

本适配遵循统一强制模板:全域无 CUDA,模型与输入 Tensor 全部迁移至 npu:0, 入口 try-except 捕获 NPU 初始化异常并优雅退出。

1. 环境

项值
硬件Ascend910 × 2(本实现固定使用 npu:0)
框架torch 2.9.0 + torch_npu 2.9.0.post1
模型WhereIsAI/UAE-Large-V1(1024 维,mean pooling)
权重源hf-mirror.com 镜像(HF_ENDPOINT)

验证 NPU 可用性:

python3 -c "import torch, torch_npu; print(torch.npu.device_count(), torch.npu.is_available())"

2. 安装

pip install -r requirements.txt

3. CLI 推理

python inference.py --text "Hello world"

输出示例(真实运行):

============================================================
模型      : WhereIsAI/UAE-Large-V1
设备      : npu:0
[NPU] device=npu:0 available=True count=2 name=Ascend910_9362 torch=2.9.0+cpu
文本      : Hello world
维度      : 1024
前8个数值 : [0.339121, 0.385567, 0.40856, -1.007536, -0.470366, -0.089666, 0.453688, 0.56013]
L2 范数   : 17.595107
耗时      : 0.200s
============================================================

4. FastAPI 服务

uvicorn inference:app --host 0.0.0.0 --port 8002

请求:

curl -s -X POST http://127.0.0.1:8002/predict \
  -H "Content-Type: application/json" \
  -d '{"text":"Hello world"}'

响应 JSON:

{
  "model": "WhereIsAI/UAE-Large-V1",
  "device": "npu:0",
  "dim": 1024,
  "embedding": [0.3391207158565521, 0.38556724786758423, 0.40855956077575684, -1.0075359344482422, ...],
  "head8": [0.339121, 0.385567, 0.40856, -1.007536, -0.470366, -0.089666, 0.453688, 0.56013],
  "norm": 17.595107
}

健康检查:GET /health → {"status": "ok", "device": "npu:0"}

5. 实现说明

  • 加载:AutoModel + AutoTokenizer,eval() 模式,max_length=512。
  • 池化:mean pooling —— last_hidden_state × attention_mask 逐 token 加权求平均。
  • 权重:优先加载本地缓存 model_weights/WhereIsAI__UAE-Large-V1,命中跳过下载; 未命中则通过 HF_ENDPOINT=https://hf-mirror.com 镜像自动下载。
  • 设备:模型与输入 Tensor 全部 .to("npu:0"),输出 cpu() 后转列表返回。

6. 目录结构

UAE-Large-V1-npu/
├── inference.py          # 单文件双模式推理脚本(CLI + FastAPI)
├── requirements.txt
├── README.md
├── .gitignore            # 忽略 model_weights/ 权重缓存
└── assets/               # 运行截图
    ├── agent_workflow.png    # NPU 环境部署与任务启动
    ├── npu_device_call.png   # CLI 推理真实输出
    └── model_result.png      # FastAPI 返回真实 JSON

7. 截图说明

三张截图均由真实命令输出经 render_terminal.py 渲染:

  • assets/agent_workflow.png:NPU 环境检测、依赖、权重下载、任务启动日志。
  • assets/npu_device_call.png:python inference.py --text "Hello world" 的 CLI 推理输出。
  • assets/model_result.png:curl POST /predict 的 FastAPI 真实响应。