模型适配 · 表格/时序数据概率点预测 (Time-Series Forecasting) · 昇腾 NPU
Beta 版本头信息
{hardware NPU}NPU 标签)model.safetensors(≈ 130 KB)TimeSeriesTransformerForPrediction 类全程 fp32、纯 PyTorch 算子,昇腾 NPU 原生可跑,无需 vendor 代码、无需算子替换。输入: past_values (1, 61) + 特征(月度/年龄/序列编号)
↓
实例归一化 TimeSeriesMeanScaler (loc/scale 自动缩放)
↓
Lags (16 个月度滞后 1,2,3,...,36,37) + 静态特征嵌入 + 时间特征
↓
Encoder × 2 层 (Multi-Head Self-Attention + FFN, d_model=26, heads=2)
↓
Decoder × 2 层 (因果 Self-Attention + Cross-Attention,逐月自回归)
↓
输出头 → Student-t 分布 (df, loc, scale) → 采样
↓
输出: 样本预测 (1, 100, 24) —— 24 个月概率预测模型使用 transformers 内置的 TimeSeriesTransformerForPrediction,所有算子(Attention、FFN、LayerNorm、Student-t 采样、instance scaling)均为标准 PyTorch 算子,昇腾 NPU(torch_npu)原生支持,未修改任何模型代码。
用官方训练 batch(hf-internal-testing/tourism-monthly-batch)逐项核对后的输入约定:
| 输入 | 形状 | 取值 |
|---|---|---|
past_values | (B, 61) | 过去 24 个月 + 最大滞后 37 个月 = 61 个观测 |
past_time_features | (B, 61, 2) | [month_of_year, log10(2+age)] |
future_time_features | (B, 24, 2) | 预测窗口的 [month_of_year, log10(2+age)] |
static_categorical_features | (B, 1) | 序列编号 ∈ [0, 366) |
static_real_features | (B, 1) | 官方训练 batch 中恒为 0.0 |
past_observed_mask | (B, 61) | 全 1(全部观测到) |
其中:
month_of_year(m) = (m-1)/11 - 0.5,m ∈ [1,12]age(n) = log10(2 + n),n 为该月在整个序列中的 0 基位置(GluonTS AddAgeFeature 约定,log_scale=True)inference.py 中的 build_features() 已按此约定实现,用户只需提供原始月度序列 + 起点日期即可。
| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| NPU | Ascend 910B (2 × 64GB HBM) |
| Python | 3.11.14 |
对官方 tourism_monthly 真实样本(series #29)运行 --compare:
| 指标 | 数值 |
|---|---|
| NLL loss(CPU vs NPU) | 9.51795864 vs 9.51797962 |
encoder_last_hidden_state 余弦相似度 | 0.99999999927 |
loc 最大绝对误差 | 0.0(逐位一致) |
scale 最大绝对误差 | 0.0(逐位一致) |
| 输出头 Student-t 参数(df/loc/scale)余弦 | 0.99999999996+ |
| 输出头 Student-t 参数最大绝对误差 | 2.2e-05 |
生成结果本身是 Student-t 采样(100 个平行样本),因此 CPU 与 NPU 的单样本结果天然不同;但模型的确定性 backbone(编码器隐藏状态、归一化 loc/scale、输出头分布参数)在 CPU 与 NPU 上近乎逐位一致,证明移植完全正确。
inference.py --demo 使用官方 tourism_monthly 真实样本(series #29,前 61 个月为输入),对后续真实 24 个月做预测:
| 指标 | 数值 |
|---|---|
| MAE | ≈ 2190(游客量单位) |
| MAPE | ≈ 5.9% |
预测均值能很好地跟踪真实游客量的月度季节性(9月-12月旺季、1月低谷)与水平变化。
| 指标 | 数值 |
|---|---|
| 平均推理延迟(NPU, 100 样本) | ≈ 110 ms / 次 |
| CPU 参考延迟(同配置) | ≈ 244 ms / 次 |
| NPU 加速比 | ≈ 2.2× |
| 峰值显存(NPU 进程内分配) | ≈ 7.4 MB(模型仅 0.03M 参数) |
pip install -r requirements.txt# 方式一:HuggingFace (hf-mirror,推荐)
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download \
huggingface/time-series-transformer-tourism-monthly --local-dir /models/time-series-transformer-tourism-monthly
# 方式二:AtomGit 镜像仓
git clone https://gitcode.com/hf_mirrors/huggingface/time-series-transformer-tourism-monthly.gitpython3 inference.py --model-path /models/time-series-transformer-tourism-monthly \
--device npu:0 --demopython3 inference.py --model-path /models/time-series-transformer-tourism-monthly \
--device npu:0 \
--values "[100.0, 120.0, 150.0, ...]" \ # 月度序列,长度 >= 61
--start 2000-01 --series-id 100python3 inference.py --model-path /models/time-series-transformer-tourism-monthly --compare# 启动服务(默认 8000 端口)
python3 inference.py --model-path /models/time-series-transformer-tourism-monthly \
--device npu:0 --serve --port 8000# 健康检查
curl -s http://127.0.0.1:8000/health
# {"status":"ok","device":"npu:0","model":"..."}
# 时序概率预测(values 为一段月度时序,长度 >= 61)
curl -s http://127.0.0.1:8000/v1/forecast \
-H 'Content-Type: application/json' \
-d '{"values": [100.0, 120.0, 150.0, ...], "start": "2000-01", "series_id": 100, "num_samples": 100}'
# 返回 24 个月的 mean / std / p5 / p95 预测time-series-transformer-tourism-monthly/
├── inference.py # 推理 / 精度对比 / 基准 / HTTP 服务(必选)
├── README.md # 部署说明文档(必选)
├── requirements.txt # 环境依赖清单
└── assets/ # 素材目录(必选)
├── tourism_demo.json # 官方 tourism_monthly 真实样本(series #29)
├── agent_workflow.png # 适配工作流截图
├── npu_device_call.png # NPU 调用截图
└── model_result.png # 推理结果截图本模型为 HuggingFace 官方在 MIT 许可下发布的时序预测模型;本项目仅做昇腾 NPU 部署适配,模型权重与官方完全一致,未做任何修改。