liuhongwei-2026/time-series-transformer-tourism-monthly
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

huggingface/time-series-transformer-tourism-monthly on Ascend NPU

模型适配 · 表格/时序数据概率点预测 (Time-Series Forecasting) · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: huggingface/time-series-transformer-tourism-monthly(AtomGit 镜像: hf_mirrors/huggingface/time-series-transformer-tourism-monthly)
  • 部署仓库: liuhongwei-2026/time-series-transformer-tourism-monthly(昇腾 NPU 适配版,含 NPU 标签)
  • 模型类型: TimeSeriesTransformerForPrediction —— 原生 encoder-decoder Transformer 概率时序预测模型(vanilla Transformer + Student-t 输出头),在 monash_tsf/tourism-monthly(澳大利亚 366 条月度游客量序列)上训练
  • 参数量: 0.03M(d_model=26,encoder/decoder 各 2 层,1 个静态类别特征 cardinality=366)
  • 输入/输出: 24 个月历史 → 24 个月概率预测(100 个平行样本,Student-t 分布)
  • 权重格式: fp32 · model.safetensors(≈ 130 KB)
  • 适配状态: SUCCESS
  • 适配说明: 该模型是时序预测模型(无 tokenizer、输入为原始时序数值),不适用 vLLM-Ascend 的 OpenAI 兼容 chat 服务;本项目采用 torch_npu + transformers 内置实现在昇腾 NPU 上完成零改造适配,并以 FastAPI 提供 HTTP 服务化推理。官方内置 TimeSeriesTransformerForPrediction 类全程 fp32、纯 PyTorch 算子,昇腾 NPU 原生可跑,无需 vendor 代码、无需算子替换。

2. 架构说明

输入: 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 个月概率预测

3. 适配要点(昇腾 NPU 移植)

3.1 零改造、零算子替换

模型使用 transformers 内置的 TimeSeriesTransformerForPrediction,所有算子(Attention、FFN、LayerNorm、Student-t 采样、instance scaling)均为标准 PyTorch 算子,昇腾 NPU(torch_npu)原生支持,未修改任何模型代码。

3.2 输入特征约定(与官方训练流水线逐位一致)

用官方训练 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() 已按此约定实现,用户只需提供原始月度序列 + 起点日期即可。

4. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
transformers4.57.6
CANN8.5.1
NPUAscend 910B (2 × 64GB HBM)
Python3.11.14

5. 精度评测(CPU vs NPU 确定性 backbone)

对官方 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 上近乎逐位一致,证明移植完全正确。

6. 预测质量(真实 tourism_monthly 数据)

inference.py --demo 使用官方 tourism_monthly 真实样本(series #29,前 61 个月为输入),对后续真实 24 个月做预测:

指标数值
MAE≈ 2190(游客量单位)
MAPE≈ 5.9%

预测均值能很好地跟踪真实游客量的月度季节性(9月-12月旺季、1月低谷)与水平变化。

7. 性能

指标数值
平均推理延迟(NPU, 100 样本)≈ 110 ms / 次
CPU 参考延迟(同配置)≈ 244 ms / 次
NPU 加速比≈ 2.2×
峰值显存(NPU 进程内分配)≈ 7.4 MB(模型仅 0.03M 参数)

8. 复现步骤

8.1 安装依赖

pip install -r requirements.txt

8.2 获取模型权重

# 方式一: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.git

8.3 单次推理(NPU,官方真实样本演示)

python3 inference.py --model-path /models/time-series-transformer-tourism-monthly \
  --device npu:0 --demo

8.4 自定义时序预测

python3 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 100

8.5 CPU vs NPU 精度对比

python3 inference.py --model-path /models/time-series-transformer-tourism-monthly --compare

8.6 服务化推理(HTTP Service)

# 启动服务(默认 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 预测

9. 目录结构

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    # 推理结果截图

10. 免责声明

本模型为 HuggingFace 官方在 MIT 许可下发布的时序预测模型;本项目仅做昇腾 NPU 部署适配,模型权重与官方完全一致,未做任何修改。