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

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

#NPU

  • model_name: huggingface/time-series-transformer-tourism-monthly
  • revision: local snapshot_download (5 files, HF_ENDPOINT=https://hf-mirror.com)
  • task_type: time-series forecasting (probabilistic, Student-T distribution)
  • architecture_family: TimeSeriesTransformerForPrediction (encoder-decoder Transformer, 2+2 layers, d_model 26, 2 heads)
  • model_type: time_series_transformer
  • loading_backend: transformers 4.57.6 TimeSeriesTransformerForPrediction.from_pretrained
  • requires_remote_code: False
  • input_shape: past_values [batch, past_length=61] (context 24 + max_lag 37), past_time_features [batch, 61, 2], future_time_features [batch, 24, 2]
  • output_shape: params [batch, prediction_length=24, 3] (df, loc, scale) -> StudentT samples [batch, 100, 24] via generate()
  • dtype: float32
  • preprocessing: per-series scaling (mean scaling via MeanScaler), lag features (16 lags), time features (2), static categorical embedding (cardinality 366) + static real
  • context_length: 24, prediction_length: 24, past_length: 61 (context + max_lag 37)
  • estimated_weight_memory: 0.13 MB (model.safetensors 130K, ~33K params float32) + pytorch_model.bin 148K
  • validation_strategy: deterministic forward (teacher-forcing) comparison of Student-T params (df/loc/scale), generate() sampling verified

数据契约

  • 数据来源: 合成可追溯序列 (torch.manual_seed 0, torch.randn) 模拟 tourism-monthly 单变量时序,避免外部数据集依赖;真实业务可替换为 monash_tsf tourism-monthly CSV 加载
  • 频率: monthly (tourism), 时区: UTC, 排序: 时间递增, 轴顺序: [batch, time] (past_values), [batch, time, num_time_features] for time features
  • 归一化: MeanScaler (scaling=True) 按 past_values 均值/方差缩放,推理后 loc/scale 输出仍在缩放空间,需 inverse transform 还原(本验证比较缩放空间内 params)
  • 缺失值: past_observed_mask 全 1 表示均观测,缺失用 0 标记并零填充
  • 验证: 使用合成 batch 2, past_length 61 -> pred 24, 固定 seed 0,保证可复现;batch 4 亦验证
  • 预处理: 列顺序固定为 [past_values, past_time_features(2), future_time_features(2), static_categorical(1), static_real(1)];推理脚本固定 seed 0 与确定性生成
  • 目标列: 无显式 target 列,模型输出未来 24 步的分布参数,点预测取 loc,采样通过 generate(num_parallel_samples=100)

环境

  • NPU: Ascend910 (910_9362), 2 cards, tested on npu:0, 64GB HBM
  • CANN: 8.5.1, torch_npu 2.9.0.post1, torch 2.9.0+cpu, transformers 4.57.6
  • Python: 3.11.14, OS: Linux aarch64
  • 驱动验证: npu-smi info & torch.npu.is_available()==True, device_count>=1, device_name Ascend910_9362
  • 显存: 单卡可用 >60GB, 模型 0.13 MB + 激活 <50MB, 安全余量充足
  • 权重路径: working/models/time-series-transformer-tourism-monthly (snapshot_download 本地, HF_ENDPOINT=https://hf-mirror.com)

安装

pip install -r requirements.txt
# 模型权重已通过 snapshot_download 预下载到 working/models/time-series-transformer-tourism-monthly (HF_ENDPOINT=https://hf-mirror.com)
# 若本地不存在,inference.py 会尝试 from_pretrained(MODEL_ID) 回退联网下载

requirements.txt 包含 torch, torch-npu, transformers, numpy, huggingface-hub, accelerate。

NPU 推理

默认命令 (单卡 npu:0, 同步计时):

python inference.py
  • 输入: 合成 2×61 lookback (context 24 + lags), 输出: 24 forecast params [2,24,3]
  • 日志打印: model_name, task, backend, device, dtype, 首参数 device, 输入摘要, 输出摘要, 同步耗时, 最终状态
  • 设备验证: 模型参数 device npu:0, 输入自动迁移至 npu, 输出经 cpu 回传;日志显式检查 torch.npu.synchronize()
  • 退出码: 0 成功, 非 0 异常

真实结果

  • 首次编译+推理 (forward, batch 2, 24→24): 267.34ms (含图编译)
  • 稳定推理 avg 6.40ms, min 6.33ms, max 6.56ms, p50 6.39ms, p90 6.46ms, p95 6.51ms, 吞吐 624.7 windows/s (batch 4 -> 14993 steps/s)
  • 自回归 generate (100 samples, 24 steps): 首轮 ~150ms, 稳定 ~110ms (含 24 步循环 + Student-T 采样,aten::_standard_gamma 回退 CPU 为预期)
  • 输出 shape params [2,24,3], dtype float32
  • 输入统计: mean 0.0022, std 1.02, min -2.61, max 3.41, seed 0
  • CPU params 示例 df[0,:3]=[3.44,3.63,3.81] loc[0,:3]=[0.566,0.602,0.424] scale[0,:3]=[0.481,0.448,0.394]
  • NPU params 示例 df[0,:3]=[3.4402,3.6278,3.8129] loc[0,:3]=[0.5659,0.6017,0.4241]
  • 设备验证: 模型首参数 device npu:0, 输入/输出校验通过 (见 npu_device_call.png)
  • Generate 示例 mean[0,:5]=[0.493,0.771,0.609,0.686,0.849], 首条轨迹[0,0,:5]=[-0.237,1.401,1.886,0.028,0.771]

一致性

  • 同权重、同输入、同 dtype (float32)、同 seed (0)、同预处理下 CPU vs NPU deterministic forward:
    • 输入: 完全一致 (same torch.randn seed 0)
    • 预测 params [2,24,3]: max_abs 0.000082, mean_abs 0.000028 ; 阈值 atol=1e-4 rtol=1e-3 -> PASS
    • df: max 0.000081 mean 0.000019, loc: max 0.000082 mean 0.000055, scale: max 0.000066 mean 0.000009
    • generate 采样因 CPU/NPU 随机数生成器差异存在数值差异,属预期;故一致性以 deterministic params 为准,smoke consistency 已验证
  • 发生 CPU fallback: aten::lgamma.out (Student-T log prob) 与 aten::_standard_gamma (采样) 回退 CPU,属 NPU 不支持算子预期行为,已在日志显式警告,非静默 matmul/attention 回退,核心 Transformer 仍在 NPU 执行

性能

  • 计时方法: 每轮前后 torch.npu.synchronize(), 首轮单独报告, 稳定阶段预热 3 次、测试 10 次
  • 报告: compile/first-run、avg/min/max/p50/p90/p95、batch size、shape、dtype、峰值显存和吞吐量
  • 预处理是否计时: 否 (仅模型 forward/generate)
  • 峰值显存: 13.9 MB on npu:0 (torch.npu.max_memory_allocated)
  • 频率: 单窗口 6.4ms -> 624 windows/s (batch 4), 双窗口 6.07ms -> 329 windows/s (batch 2)

证据图

  • agent_workflow: 完整工作流日志 (环境检查->获取模型->分析->NPU推理)
  • npu_device_call: npu-smi + torch.npu.is_available + 模型参数 device npu:0 验证
  • model_result: 默认推理完整输出 (CPU/NPU params, 一致性, 性能, generate)

agent workflow npu device call model result

限制

  • 当前仅验证合成数据 (seed 0, batch 2/4) 的 smoke consistency,非完整 tourism-monthly 数据集精度评测;完整评测需 monash_tsf 官方测试集
  • lgamma / _standard_gamma 算子在 NPU 上回退 CPU,虽已验证数值一致,但对超长 horizon 或大 batch 可能影响吞吐
  • 权重仅支持 float32,未测试 FP16/BF16;context/prediction 长度固定为官方配置 (24/24),修改需重编译
  • Tokenizer-only checkpoint 不适用;本模型为完整 TimeSeriesTransformerForPrediction