冬
gcw_IDzXRVNw/sap-rpt-1-oss-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

sap-rpt-1-oss 昇腾 NPU 适配部署文档

1. 模型简介

sap-rpt-1-oss (formerly ConTextTab) 是来自论文 ConTextTab: A Semantics-Aware Tabular In-Context Learner 的表格数据上下文学习模型。

该模型结合了语义理解和表格数据结构的优势,在大规模真实世界表格数据上训练,可在多种表格预测任务上达到 SOTA 性能。

模型架构

属性值
模型类型RPT (Recursive Percolation Transformer)
参数量172.07M
隐藏层维度768
Encoder 层数12
Attention 头数12
注意力机制2D Attention (行+列)
文本嵌入模型sentence-transformers/all-MiniLM-L6-v2 (384维)
支持任务分类 / 回归
Checkpointl2/base.pt (~62MB)

模型特点

  • 语义感知: 使用 LLM 嵌入列名和单元格内容
  • 2D 注意力: 同时建模行间和列间关系
  • 上下文学习: 无需微调,通过上下文示例预测
  • 支持 Bagging: 可通过多 bagging 提升预测稳定性
  • 原生支持表格: 直接处理 pandas DataFrame

输入输出格式

  • 分类输入: (n_samples, n_features) 的特征矩阵 + 类别标签
  • 分类输出: 类别预测标签列表
  • 回归输入: (n_samples, n_features) 的特征矩阵 + 连续目标值
  • 回归输出: 预测值数组

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 4 GB NPU 显存(sap-rpt-1-oss 推理峰值约 2.2GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
sentence-transformers5.7.0+文本嵌入模型
scikit-learn1.5.0+数据处理
pandas1.5+表格数据处理
numpy1.21+数值计算

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装 sap-rpt-oss(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -e /workspace/agent/sap-rpt-1-oss/

# 3) 安装 Sentence Transformer(使用 HF 镜像)
export HF_ENDPOINT=https://hf-mirror.com
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple sentence-transformers

# 4) 验证环境
python -c "
import torch, torch_npu
from sentence_transformers import SentenceTransformer
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent/sap-rpt-1-oss-ascend

# 确认上游权重存在
ls -la /workspace/agent/sap-rpt-1-oss/

# 期望输出:config.json l2/base.pt (~62MB) ...

步骤 2:设置 HuggingFace 镜像(首次运行需要下载 Sentence Transformer)

# 设置 HF 镜像以加速下载
export HF_ENDPOINT=https://hf-mirror.com

步骤 3:运行推理脚本

# 分类任务(默认)
python inference.py --device npu:0

# 分类任务 + 查看 NPU 显存
python inference.py --device npu:0 --show_npu_memory

# 回归任务
python inference.py --device npu:0 --task regression

# 指定其他 NPU 卡
python inference.py --device npu:1

4. 完整测试用例

测试用例 1:分类任务(NPU:0)

conda activate pt2100
export HF_ENDPOINT=https://hf-mirror.com
cd /workspace/agent/sap-rpt-1-oss-ascend
python inference.py --device npu:0 --show_npu_memory --task classification

实际输出:

[W819 01:58:18.842643600 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W819 01:58:33.130696370 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size...
[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2

[模型加载] checkpoint: /workspace/agent/sap-rpt-1-oss/l2/base.pt
[任务类型] classification
Loading weights: 100%|██████████| 103/103 [00:00<00:00, 6831.44it/s]
[模型] sap-rpt-1-oss (classification)
[参数量] 172.07M
[模型] 已迁移至 npu:0
[NPU显存 · 模型加载后] 总显存=61.0GB | 已分配=656.6MB | 缓存=742.00MB | 峰值=656.6MB

[数据集] Breast Cancer (训练: (300, 30), 测试: (269, 30))
训练模型...
[训练耗时] 0.07s
预测...

==============================================================================
输出摘要
==============================================================================
  模型: sap-rpt-1-oss (分类)
  预测数量: 269
  预测耗时: 0.96s
  预测类别: [0 1]
  预测分布: {0: 70, 1: 199}
[NPU显存 · 推理完成后] 总显存=61.0GB | 已分配=656.6MB | 缓存=2648.00MB | 峰值=2174.5MB

✅ 验证要点:

  • NPU 推理稳定: 分类推理 0.96 秒
  • NPU 显存峰值: 约 2.2 GB
  • 输出有限值: 所有预测均为有效类别
  • 预测分布合理: 二分类问题,类别分布符合预期

测试用例 2:回归任务(NPU:0)

conda activate pt2100
export HF_ENDPOINT=https://hf-mirror.com
cd /workspace/agent/sap-rpt-1-oss-ascend
python inference.py --device npu:0 --show_npu_memory --task regression

实际输出:

[设备] npu:0
[模型加载] checkpoint: /workspace/agent/sap-rpt-1-oss/l2/base.pt
[任务类型] regression
[模型] sap-rpt-1-oss (regression)
[参数量] 172.07M
[模型] 已迁移至 npu:0
[NPU显存 · 模型加载后] 总显存=61.0GB | 已分配=656.6MB | 缓存=742.00MB | 峰值=656.6MB

[数据集] Boston (训练: (253, 13), 测试: (253, 13))
训练模型...
[训练耗时] 0.00s
预测...

==============================================================================
输出摘要
==============================================================================
  模型: sap-rpt-1-oss (回归)
  预测形状: (253,)
  预测耗时: 0.77s
  预测范围: [7.0773, 49.4948]
  预测均值: 22.1474
[NPU显存 · 推理完成后] 总显存=61.0GB | 已分配=656.6MB | 缓存=1400.00MB | 峰值=1219.1MB

✅ 验证要点:

  • NPU 推理稳定: 回归推理 0.77 秒
  • NPU 显存峰值: 约 1.2 GB
  • 预测值合理: 房价预测范围符合 Boston 数据集实际范围

5. 常见问题解答

Q1:提示 "Cannot access gated repo" 怎么办?

解决方案:模型 checkpoint 已本地存储在 /workspace/agent/sap-rpt-1-oss/l2/base.pt。脚本会自动使用本地 checkpoint,无需访问 HuggingFace。

Q2:提示 "sentence-transformers model not found" 怎么办?

解决方案:首次运行需要下载 Sentence Transformer 模型。请设置 HF 镜像:

export HF_ENDPOINT=https://hf-mirror.com
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple sentence-transformers

Q3:模型支持 GPU 吗?

解决方案:支持 CUDA GPU 和 NPU。脚本会自动检测可用设备,优先使用 NPU。

Q4:NPU 显存不足怎么办?

解决方案:

  1. 减小 max_context_size 参数(如从 8192 降至 2048)
  2. 减小 bagging 参数(如从 8 降至 1)
  3. 使用更小的 batch

Q5:如何进行批量预测?

解决方案:当前脚本支持 sklearn 风格的 fit/predict 接口,可直接对整个测试集预测。

6. 文件结构

sap-rpt-1-oss-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                   # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent/sap-rpt-1-oss/
├── l2/base.pt               # 权重(172.07M, ~62MB)
├── 2025-11-04_sap-rpt-one-oss.pt  # 原始格式权重
├── configuration.json         # 模型配置
├── README.md                 # 原始文档
└── ...

7. requirements.txt - 依赖列表

# sap-rpt-1-oss 昇腾 NPU 推理环境依赖

# 核心依赖(必须)
torch>=2.0.0
torch_npu>=2.0.0
sentence-transformers>=5.7.0
scikit-learn>=1.5.0
pandas>=1.5.0
numpy>=1.21.0
pyarrow>=10.0.0
huggingface_hub>=0.20.0

使用方式:

# 激活环境
conda activate pt2100

# 设置 HF 镜像
export HF_ENDPOINT=https://hf-mirror.com

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

8. 参考资源

  • sap-rpt-1-oss 论文 (arXiv:2506.10707)
  • sap-rpt-1-oss GitHub 仓库
  • HuggingFace 模型页 (SAP/sap-rpt-1-oss)
  • ConTextTab 论文 (NeurIPS 2025)
  • 华为昇腾文档
  • torch_npu GitHub