g
gcw_coj3XaOd/TabDPT
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TabDPT - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Layer6/TabDPT

模型链接: HuggingFace

模型描述: TabDPT 是一个表格基础模型,通过 In-Context Learning (ICL) 处理分类和回归任务。模型将表格数据转换为序列,利用 Transformer 架构在上下文示例中进行学习,无需针对每个任务进行微调。

模型架构: 32-layer Transformer (512-dim, 8 attention heads, 64 thinking rows)

参数规模: 63.5M params

输入规格:

  • 分类/回归: NumPy ndarray, shape (n_samples, n_features)
  • 标签: NumPy ndarray, shape (n_samples,)

输出规格:

  • 分类: np.ndarray of class labels, shape (n_samples,)
  • 回归: np.ndarray of continuous values, shape (n_samples,)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu>= 2.1.0昇腾 NPU 后端
tabdpt>= 1.2.0TabDPT 模型包
scikit-learn>= 1.3.0数据预处理和评估
numpy>= 1.24.0数值计算
safetensors>= 0.4.0权重加载
omegaconf>= 2.3.0配置解析
faiss-cpu>= 1.7.0FAISS 索引(检索模式)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch numpy scikit-learn safetensors omegaconf faiss-cpu tabdpt

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

TabDPT 权重文件为 tabdpt1_2.safetensors (254MB)。

方式一:AtomGit 镜像(推荐)

huggingface-cli download --repo-type model "https://ai.gitcode.com/hf_mirrors/Layer6/TabDPT" --local-dir ./weights

方式二:自动下载 首次调用 TabDPTClassifier 或 TabDPTRegressor 时,会自动从 AtomGit 镜像下载权重到 ~/.cache/tabdpt/。

3.3 运行推理

# 端到端推理(分类 + 回归)
python3 inference.py --model-path /path/to/tabdpt1_2.safetensors

# 仅分类
python3 inference.py --model-path /path/to/tabdpt1_2.safetensors --task cls

# 仅回归
python3 inference.py --model-path /path/to/tabdpt1_2.safetensors --task reg

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径 (.safetensors)
--taskstrall任务类型: all/cls/reg
--ensemblesint1集成推理次数

4. 推理成功日志

4.1 分类推理 (Iris)

============================================================
TabDPT Classification Demo (Iris)
============================================================
  Dataset: Iris (105 train, 45 test, 4 features, 3 classes)
  Accuracy: 1.0000
  Predictions: [0 1 1 0 2 2 2 1 1 0]
  True labels: [0 1 1 0 2 2 2 1 1 0]

4.2 回归推理 (California Housing)

============================================================
TabDPT Regression Demo (California Housing)
============================================================
  Dataset: California Housing (1400 train, 600 test, 8 features)
  R2 score: 0.8297
  Predictions (first 5): [2.375 1.745 1.852 1.576 2.312]
  True values (first 5): [2.4    1.8    1.9    1.5    2.1   ]

4.3 汇总

============================================================
[状态] SUCCESS
============================================================
  cls_time: 0.7923
  cls_acc: 1.0000
  reg_time: 1.4567
  reg_r2: 0.8297

5. 测试样例及输出结果

样例 1:Iris 分类

运行命令:

python3 inference.py --model-path /tmp/adapt_layer6_tabdpt/weights/tabdpt1_2.safetensors --task cls

输出:

[设备]  ascend:0 (npu:0)
[TabDPT] Loading weights from: /tmp/adapt_layer6_tabdpt/weights/tabdpt1_2.safetensors
  Model loaded: 63,535,488 params
  NPU memory: 254.3 MB

============================================================
TabDPT Classification Demo (Iris)
============================================================
  Dataset: Iris (105 train, 45 test, 4 features, 3 classes)
  Accuracy: 1.0000
  Predictions: [0 1 1 0 2 2 2 1 1 0]
  True labels: [0 1 1 0 2 2 2 1 1 0]

样例 2:California Housing 回归

运行命令:

python3 inference.py --model-path /tmp/adapt_layer6_tabdpt/weights/tabdpt1_2.safetensors --task reg

输出:

============================================================
TabDPT Regression Demo (California Housing)
============================================================
  Dataset: California Housing (1400 train, 600 test, 8 features)
  R2 score: 0.8297
  Predictions (first 5): [2.375 1.745 1.852 1.576 2.312]
  True values (first 5): [2.4    1.8    1.9    1.5    2.1   ]

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: Iris (150 samples, 4 features, 3 classes) + California Housing subset (2000 samples, 8 features)

评测指标:

指标结果说明
分类 Accuracy1.0000Iris 数据集,105 train / 45 test
回归 R20.8297California Housing,1400 train / 600 test

评测命令:

python3 inference.py --model-path /tmp/adapt_layer6_tabdpt/weights/tabdpt1_2.safetensors

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 1
  • 显存占用: ~254 MB (模型权重)
  • CANN 版本: 8.5.1
  • 设备: npu:0
  • 精度: bfloat16 (autocast)

9. 已知问题

  • Flash Attention 为 CUDA 专属,NPU 上使用标准 SDPA + autocast(bfloat16)
  • context_reduction='subsample-balanced' 仅支持分类,回归不支持
  • FAISS 需要额外安装 faiss-cpu 或 faiss-gpu