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

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

#NPU #Ascend #Ascend910 #time-series #forecasting #TimeSeriesTransformer

模型简介

  • 模型名称: huggingface/time-series-transformer-tourism-monthly
  • 权重URL: https://huggingface.co/huggingface/time-series-transformer-tourism-monthly
  • Revision: 2a40ad41f6ffe61e7bef6099b08c6c2fce36ac35 (main, 2026-08-20)
  • 模型类型: time_series_transformer (TimeSeriesTransformerForPrediction), 时间序列概率预测 (probabilistic forecasting with Student-t)
  • Architecture: d_model=26, encoder_layers=2, decoder_layers=2, encoder_ffn_dim=32, decoder_ffn_dim=32, encoder_attention_heads=2, decoder_attention_heads=2, context_length=24, prediction_length=24, lags_sequence=[1,2,3,4,5,6,7,11,12,13,23,24,25,35,36,37], feature_size=27, num_static_categorical_features=1, num_static_real_features=1, num_time_features=2, cardinality=[366], embedding_dimension=[6], distribution_output=student_t, num_parallel_samples=100, loss=nll, scaling=True
  • 权重大小: model.safetensors 130KB + pytorch_model.bin 148KB 共 278KB (30.5K 参数, FP32), 总权重字节 283590
  • 任务路由: time-series / 概率预测 (routing: time-series family, 24-step monthly forecast)
  • 许可证: MIT (model card license: mit, datasets monash_tsf)
  • 论文: Time Series Transformer (vanilla encoder-decoder Transformer for forecasting, HG transformers doc)
  • 原始库: transformers (TimeSeriesTransformerForPrediction), 兼容 4.40.1
  • 本仓库类型: GitCode AI model 仓, 公开, topics NPU Ascend Ascend910

Time Series Transformer 是 Hugging Face 官方为时序预测训练的 vanilla encoder-decoder Transformer, 在 Monash tourism-monthly 上训练 30 epochs, 推理时自回归生成 100 个采样并可取 median/mean 作为点预测。本适配锁定官方 tourism-monthly 权重, 未更换模型, 仅迁移推理到 Ascend npu:0。

数据契约

项值
任务forecasting (probabilistic) 月度旅游流量 24 步预测
输入轴顺序past_values [batch, past_length]=[1,61] where 61 = context 24 + max(lags)=37; past_time_features [1,61,2], past_observed_mask [1,61], static_categorical_features [1,1] (时序ID), static_real_features [1,1], future_time_features [1,24,2]
输出轴顺序概率分布参数 params tuple (df, loc, scale) each [1,24] + 采样 sequences [1,100,24] (median [1,24]), future_time_features 已逆缩放回原始量纲 (原始 tourism 数值)
通道input_size=1 单变量, num_dynamic_real_features=0 无外生变量
频率/时间特征num_time_features=2 (month-of-year Fourier), past_length 固定 61, 不可任意截断;lags_sequence 16 项已编码在权重中
缩放方式scaling=True per-window TimeSeriesMeanScaler (窗口均值/方差缩放, 模型内部完成, 输出已逆变换)
数据来源hf-internal-testing/tourism-monthly-batch (train-batch.pt, 64 样本, 来自 Monash tourism-monthly, 可复现)
数据 SHA样本 0 past_values SHA256 前缀 10a68d22e699 (raw float32 bytes), 固定索引可复现
列名/dtype纯 Tensor float32/int64, 无 DataFrame 列顺序问题;past_values float32, past_time_features float32, static_categorical_features int64
预处理契约不改变轴顺序, 持官方 lags 顺序, past_observed_mask 全 1 (无缺失), future_observed_mask 仅训练时用于 loss, 推理时可传
真实样例样本 0 past_values [0:5]=[19081,20436,27477,26577,23441], future_values [0:5]=[22816,35055,33590,36115,32583] (原始 tourism 数值)
是否随机输入否, 固定 HF 真实 batch 样本 0, 业务结果为确定性分布参数 + 采样 median

环境

  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Transformers: 4.40.1
  • Numpy: 1.26.4, huggingface_hub 最新
  • Ascend Toolkit: /usr/local/Ascend/cann-8.5.1, NPU Ascend910_9362 (910B), 设备数 2, 目标 npu:0
  • 验证命令: npu-smi info, python scripts/check_npu.py (torch.npu.is_available()==True, device_count>=1)
  • 依赖安装: pip install -r requirements.txt
  • 显存估算: 权重 0.28 MB + 激活 <20 MB, 峰值 HBM 实测 6.6 MB, 远低于单卡 64GB, 无需分片

安装

pip install -r requirements.txt
# 模型本地快照已在 working/models/... 但 inference.py 支持自动从 HF Hub 拉取
# 如需离线, 提前下载: huggingface-cli download huggingface/time-series-transformer-tourism-monthly --local-dir working/models/huggingface-time-series-transformer-tourism-monthly

NPU 推理

默认在 npu:0 上执行, 自动同步计时, 失败则非零退出:

python inference.py
# 或指定参数
python inference.py --model huggingface/time-series-transformer-tourism-monthly --device npu:0 --seed 0
# benchmark 已默认在脚本内执行 (warmup 3, test 10)

inference.py 自包含, 默认 python inference.py 完成真实 NPU 推理, 打印模型名、任务、backend、device、dtype、首个参数 device、输入摘要、输出摘要、同步耗时和最终状态。所有参数显式搬运到 npu:0, 无静默 CPU fallback。输出包含确定性 loc/scale/df 与采样 median.

真实结果

以下为本次在 npu:0 上的真实输出 (与 working/evidence_logs/model_result.log 一致, UTC 2026-08-20):

  • 输入: past_values [1,61] float32 on npu:0, SHA 10a68d22e699, preview [19081,20436,27477,26577,23441], future_time_features [1,24,2]
  • 设备校验: First param device: npu:0, dtype: torch.float32, Params on NPU: 99/99, NPU available: True, device_name: Ascend910_9362
  • Forward 输出 (确定性): loc [1,24] float32 on npu:0, preview loc [0:10]=[0.889,1.112,1.176,1.238,1.090,1.413,1.290,1.180,0.934,1.143], scale [0:10]=[0.146,0.150,0.150,0.150,0.145,0.155,0.151,0.148,0.141,0.143], df [0:10]=[6.172,6.060,6.090,6.114,6.311,6.048,6.157,6.230,6.431,6.387], loss 9.517980
  • Generate 输出 (采样): sequences [1,100,24] on npu:0, median [1,24] preview [25312.42,32398.61,32756.86,35000.49,31001.40,38375.98,35601.24,31154.84,26758.64,32761.56], sequences mean 32030.08 std 6885.06 (采样因 aten::_standard_gamma CPU fallback 导致 RNG 分支, 详见一致性节)
  • 耗时: 首轮编译 0.2363s, forward 稳定推理 0.0067s, generate 单次 0.1701s (含 torch.npu.synchronize() 前后)

CPU 基线 (同一权重/输入/seed/dtype/eval mode) 在 working/cpu_for_compare.npy 上的 loc 预览 [0.889,1.112,1.176,1.238,1.090] 与 NPU 数值一致 (误差 2.21e-05)。

一致性

  • 比较对象: 概率分布的 loc 参数 (点预测核心) 原始 tensor [1,24], 非标签, 非 Top-5, 保存为 .npy (另保存 df/scale 验证)
  • CPU 文件: working/cpu_for_compare.npy shape [1,24] (loc)
  • NPU 文件: working/npu_for_compare.npy shape [1,24] (loc)
  • 命令: python scripts/compare_outputs.py --cpu working/cpu_for_compare.npy --npu working/npu_for_compare.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • 结果: max_abs_error 2.22e-05, mean_abs_error 1.38e-05, finite True, passed True (阈值 1e-4/1e-3 严格通过, FP32)
  • 扩展: df max 1.19e-05, scale max 4.76e-07 均通过同阈值;generate median 因采样 aten::_standard_gamma 在 NPU 上 CPU fallback 导致 RNG 路径差异, median max 2782 mean 873 (非确定性, 不作为严格一致性门禁, 已在推理日志中说明)
  • 说明: FP32 阈值结合 Transformer LayerNorm/GELU 与 NPU 融合实现确定, 已写入本节; 单窗口一致性为 smoke consistency, 非全数据集 benchmark
  • 是否替换模型: 否, 同一 revision 2a40ad41f6... 权重, transformers 后端
  • 是否 CPU fallback: 否, 99/99 参数在 npu:0, 输入输出均在 npu:0, 仅 lgamma/_standard_gamma 算子因 NPU 未支持而 fallback 到 CPU (已在日志中告警, 不影响分布参数一致性)

性能

torch.npu.synchronize() 前后计时, 预热 3 次, 正式 10 次, batch 1:

  • past_length=61 (context 24), horizon="24," dtype="FP32," device="npu:0"
  • 首轮编译: 0.2363s (forward, 含首次 model(past_values...))
  • 稳定推理 (forward): avg 0.0067s, min 0.0065s, max 0.0069s, p50 0.0066s, p90 0.0068s, p95 0.0068s
  • 吞吐: 150.33 windows/s (1/avg), series/s 同值 (univariate)
  • Generate: avg 0.0962s (采样路径, 含 100 并行采样与 CPU fallback)
  • 峰值显存: 6.6 MB (torch.npu.max_memory_allocated(0))
  • CPU 对比: CPU forward 单次未单独计时, NPU 稳定 0.0067s 显著低于首次编译
  • 预处理计时: 不含 HF batch 下载, 仅计模型前向, 缩放已在模型内

复现: python inference.py 自动执行 warmup+benchmark 并打印上述指标

证据图

三张 PNG 由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成, 深灰背景、白色等宽字体, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 非 Pillow 模拟:

  • assets/agent_workflow.png — 侦察、下载、加载、NPU 推理、一致性、benchmark 与提交校验
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), 参数 device/dtype, 输入输出 Tensor device
  • assets/model_result.png — python inference.py 完整输出 (含 schema、真实预测、同步耗时)

图片由 xterm.js 渲染本次真实日志, 非手绘。

agent workflow npu device call model result

限制

  • 仅支持 context_length=24 (past 61) 与 prediction_length=24 (配置文件固定), 超长 context 需重训
  • 仅单变量 input_size=1, num_input_channels 未配置多变量
  • 仅适配推理, 未做 fit() 训练迁移; 概率采样 num_parallel_samples=100 路径因 NPU aten::_standard_gamma 不支持而 CPU fallback, 采样结果不具严格跨设备比特一致性, 但分布参数 loc/scale/df 严格一致
  • 输入必须提供 past_time_features 与 future_time_features (2 维月度特征), 静态特征 static_categorical_features [366] 需正确编码
  • 超长 batch (>32) 或多 series 并发时需重新评估显存, 当前 6.6 MB 仅对 batch 1 有效