1
gcw_xUvjaUyi/sap-rpt-1-oss-npu
模型介绍
文件和版本
Pull Requests
讨论
分析

SAP/sap-rpt-1-oss · 昇腾 NPU 适配

在 昇腾 Ascend NPU(npu:0)上运行 SAP/sap-rpt-1-oss(原 ConTextTab)表格型上下文学习模型。全程 无 CUDA,权重本地缓存加载。

模型与模态说明(重要)

SAP/sap-rpt-1-oss 是一个表格型(tabular)上下文学习模型,用于分类 / 回归任务(pipeline_tag: tabular-classification),不是语音/音频模型。它接受 pandas DataFrame / CSV 作为输入,列名与单元格值由后台句向量模型 sentence-transformers/all-MiniLM-L6-v2 自动嵌入。

因此本适配的输入标志按真实模态取 --csv(FastAPI 接收上传的 CSV 文件),而非 --audio。

  • 官方仓库:https://github.com/SAP-samples/sap-rpt-1-oss
  • 模型卡片:https://huggingface.co/SAP/sap-rpt-1-oss
  • 论文:ConTextTab: A Semantics-Aware Tabular In-Context Learner(NeurIPS 2025)

昇腾 NPU 适配点

  1. 全域无 CUDA:删除所有 .cuda() / cuda: / CUDA 设备调用,模型与输入 Tensor 全部位于 npu:0。
  2. NPU 初始化保护:inference.py 入口用 try-except 捕获 torch_npu 导入 / npu:0 不可用异常,失败时打印清晰错误并优雅退出。
  3. 本地缓存优先:权重从 ./model_weights/SAP__sap-rpt-1-oss/2025-11-04_sap-rpt-one-oss.pt 加载(命中即用,不触发 HF 下载);句向量模型从 ./model_weights/sentence-transformers__all-MiniLM-L6-v2/ 加载。缺失时才通过 HF_ENDPOINT=https://hf-mirror.com 镜像下载。
  4. 双模式:CLI 单次推理 + FastAPI HTTP 推理服务。
  5. 源码 sap_rpt_oss/ 为官方 Apache-2.0 包 v1.0.1 的昇腾适配副本(见 LICENSE 与 ORIGINAL_README.md)。

环境

  • Python 3.11
  • torch + torch_npu(昇腾后端),transformers、pandas、scikit-learn、pyarrow、torcheval
  • 依赖安装:pip install -r requirements.txt

权重准备

权重较大且不随仓库提交,需先放入本地缓存目录:

model_weights/
├── SAP__sap-rpt-1-oss/
│   ├── configuration.json
│   ├── README.md
│   ├── notebook.ipynb
│   └── 2025-11-04_sap-rpt-one-oss.pt     # 主模型权重
└── sentence-transformers__all-MiniLM-L6-v2/   # 后台句向量模型
    ├── config.json
    ├── pytorch_model.bin
    ├── tokenizer.json
    └── ...

句向量模型可由镜像自动下载:

export HF_ENDPOINT=https://hf-mirror.com
python - <<'PY'
from huggingface_hub import snapshot_download
snapshot_download('sentence-transformers/all-MiniLM-L6-v2',
                  local_dir='model_weights/sentence-transformers__all-MiniLM-L6-v2')
PY

使用方式

CLI(单次推理)

CSV 的最后一列为目标列。整数目标列默认按回归处理;若是分类标签请加 --task classification。

python inference.py --csv sample_data.csv --task classification
python inference.py --csv sample_regression.csv

输出一份推理 JSON(含 device: npu:0、分类概率 / 回归预测、准确率等)。

常用参数:

  • --task auto|classification|regression(默认 auto,按目标列 dtype 自动判定)
  • --max-context-size(默认 2048)
  • --bagging(默认 1)
  • --train-fraction(默认 0.8,前 80% 行作为上下文,其余作为查询)

FastAPI 服务

python -m uvicorn inference:app --host 0.0.0.0 --port 8001
  • GET /health:健康检查(含 npu_available)
  • POST /predict:curl -F "file=@sample_data.csv" http://127.0.0.1:8001/predict,返回推理 JSON

示例:

curl -X POST http://127.0.0.1:8001/predict -F "file=@sample_data.csv"

验证结果(昇腾 NPU,npu:0)

  • torch.npu.is_available() == True,torch.npu.device_count() == 2
  • CLI 分类推理:device: npu:0,30 条查询全部正确(accuracy 1.0)
  • FastAPI POST /predict:HTTP 200,返回真实推理 JSON

截图见 assets/:

  • assets/agent_workflow.png:环境部署与任务启动日志
  • assets/npu_device_call.png:CLI 推理 stdout(含 NPU 设备与结果)
  • assets/model_result.png:FastAPI 上传 CSV 返回的推理 JSON

License

  • 模型与源码:Apache-2.0(SAP SE),见 LICENSE
  • 模型权重训练自 T4 数据集,仅限研究用途