m
mengsihan/huggingface-time-series-transformer-tourism-monthly-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Time Series Transformer (Tourism Monthly) — NPU 适配

#NPU

将 HuggingFace 模型 huggingface/time-series-transformer-tourism-monthly 适配到单卡昇腾 NPU(Ascend 910B / npu:0)真实运行的时间序列预测模型仓。

  • 模型:huggingface/time-series-transformer-tourism-monthly
  • revision:2a40ad41f6ffe61e7bef6099b08c6c2fce36ac35
  • 架构:TimeSeriesTransformerForPrediction(Encoder-Decoder Transformer + StudentT 概率分布头)
  • 任务:时间序列预测(time-series-forecasting),概率预测(分位数/样本)
  • 训练数据:Monash tourism_monthly 月度游客量数据集(366 条序列)
  • NPU:单卡 npu:0(Ascend910_9362)

模型简介

Time Series Transformer 是将 vanilla Transformer 用于时间序列预测的模型, Encoder 读取历史窗口(context),Decoder 自回归生成未来值,分布头输出 StudentT 分布,通过采样得到预测样本。本模型在 tourism_monthly 数据集上 训练 30 epochs。

关键配置:

参数值
context_length24(月)
prediction_length24(月)
input_size1(单变量)
num_parallel_samples100
d_model26
encoder / decoder layers2 / 2
lags_sequence[1..7, 11..13, 23..25, 35..37]
num_time_features2(month_of_year + age)
num_static_categorical_features1(序列 ID,cardinality 366)
num_static_real_features1(序列均值)
distribution_outputstudent_t

数据契约

  • 数据来源:模型官方文档引用的 hf-internal-testing/tourism-monthly-batch 官方训练批(真实 monash_tsf/tourism_monthly 月度数据,366 条序列)。 序列数值为月度游客量(正整数,量级数百至数万)。
  • 输入 schema(模型 forward/generate 直接输入):
    • past_values:[batch, 61](= context_length 24 + max(lags) 37), dtype float32
    • past_time_features:[batch, 61, 2](month_of_year 归一化 + age 特征)
    • past_observed_mask:[batch, 61](全 1,无缺失)
    • static_categorical_features:[batch, 1](序列 ID,int64)
    • static_real_features:[batch, 1](序列均值)
    • future_time_features:[batch, 24, 2]
  • 输出 schema:sequences 为 [batch, 100, 24](100 个样本 × 24 步预测), 取 median 得到点预测;分位数可反映不确定性。
  • 预处理契约:时间特征(month_of_year、age)与静态特征(序列 ID、均值) 由官方批处理完成,脚本不做列重排或缩放变换;模型内部 scaling=True 自动进行均值/尺度归一化。

环境

  • NPU:Ascend 910B(Ascend910_9362),CANN 8.5.1
  • Python:3.11
  • torch:2.9.0 + torch_npu 2.9.0
  • transformers:4.40.1
  • 操作系统:Linux aarch64(内核 5.10)

安装

pip install -r requirements.txt
# 昇腾 NPU 运行时(torch_npu)需按 Ascend 官方文档安装并完成 CANN 环境配置

NPU 推理

# 默认:自动从 HuggingFace 下载权重与官方数据批(优先本地缓存)
python inference.py

# 使用本地模型快照与本地官方数据批(无网络依赖)
python inference.py \
  --model-dir /path/to/model_snapshot \
  --batch-path /path/to/train-batch.pt \
  --num-series 4 \
  --output-dir ./outputs

脚本会把模型与所有参与计算的 Tensor 显式迁移到 npu:0,运行真实 StudentT 概率预测并打印未来 24 个月的游客量中位数预测。

真实结果(NPU 推理)

在 npu:0 上以 4 条真实 tourism_monthly 序列推理,输出 sequences [4, 100, 24],中位数预测(未来 24 个月游客量,节选):

序列 1 (item_id=29): [25353, 32548, 32719, 35365, 30097, 39338, 34247, 32980, ...]
序列 2 (item_id=31): [25897, 34971, 49073, 58331, 45850, 51218, 48072, 34757, ...]
序列 3 (item_id=32): [1130, 1019, 1015, 1030, 999, 1313, 2538, 3083, ...]

完整输出见 assets/model_result.png(由 xterm.js 根据本次真实日志生成)。

一致性

CPU 与 NPU 使用同一权重、同一输入切片、同一 dtype(float32)、同一随机种子 对比:

  • 确定性分布参数(解码循环逐 24 步提取 StudentT 参数 loc/scale/df): 最大相对误差 5.85e-5,loc/scale 最大绝对差 0.0 → 高度一致。
  • 点预测(median):由于 CPU/NPU 使用各自设备的随机数流,100 个 采样样本存在蒙特卡洛噪声,median 平均相对差约 3.2%,属概率采样的 正常统计波动。
  • 阈值说明:分布参数以 atol=1e-4, rtol=1e-3 校验通过 (compare_outputs.py 返回 0)。

CPU fallback 处理(重要)

Ascend 后端不支持 aten::_standard_gamma(StudentT 采样所需的 gamma 随机数 生成),默认会静默回退 CPU。为满足「真实 NPU 推理、无 CPU fallback」, 本仓 inference.py 内置了 Marsaglia-Tsang 方法的纯 PyTorch 等价实现, 仅使用 NPU 原生支持的 randn/rand/log/sqrt/pow 等算子,等价替换 torch._standard_gamma,使采样全程在 npu:0 上执行。该替换不改变预测 分布,仅在设备上以等价算法生成同分布随机样本(已用 30000 样本统计验证 均值相对误差 <1%)。

性能

单卡 npu:0,batch=16,context=24,prediction=24,num_samples=100, dtype=float32,预热 3 次、正式 10 次:

指标值
首轮(编译后首次)0.140 s
平均耗时0.143 s
min / max0.137 / 0.150 s
p50 / p90 / p950.145 / 0.149 / 0.149 s
峰值显存0.129 GB
吞吐量111.5 windows/s

证据图

三张证据图均由 xterm.js 根据本次真实执行日志生成(非手工绘制):

  • assets/agent_workflow.png:模型侦察、数据检查、下载、加载、NPU 推理、 一致性、性能基准与提交校验的完整工作流。
  • assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device。
  • assets/model_result.png:默认 python inference.py 的真实完整输出, 含窗口摘要、真实预测、同步耗时与最终状态。

限制

  • 概率预测输出为样本/分位数,未做 inverse transform(模型输出即原始 游客量尺度)。
  • 单样本一致性为 smoke consistency,非完整数据集精度评测。
  • 需要 CANN 与 torch_npu 环境;若在非昇腾环境运行,请改用 npu:0 可用的设备名。