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

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

#NPU Ascend910 Time Series Transformer Tourism Monthly NPU适配

模型来源: huggingface/time-series-transformer-tourism-monthly
架构: TimeSeriesTransformerForPrediction (time_series_transformer), d_model=26, encoder_layers=2, decoder_layers=2
任务: time-series-forecasting, context_length=24, prediction_length=24, lags=[1,2,3,4,5,6,7,11,12,13,23,24,25,35,36,37], distribution=student_t
权重: model.safetensors 130KB / pytorch_model.bin 148KB, 总参数 30,537, float32
修订: 2a40ad41f6ffe61e7bef6099b08c6c2fce36ac35 (main)
硬件: 单卡 Ascend910 (npu:0) 910_9362, HBM 64GB, CANN 8.5.1, torch 2.9.0+cpu, torch-npu 2.9.0.post1, transformers 4.57.6
数据契约: 见下

本仓库为 GitCode AI model 类型公开仓,使用真实 npu:0 完成确定性前向与概率采样验证,输出已通过 torch.npu.synchronize() 同步计时与 CPU-NPU 一致性校验。

数据契约

  • 来源: 合成月度旅游类时间序列 (模拟 tourism-monthly),非真实隐私数据,seed=42 可复现。官方 tourism-monthly 数据集见 monash_tsf。
  • Schema: past_values [batch, past_length] (past_length=61), past_time_features [batch, past_length, 2] (num_time_features=2: 月份与 age), static_categorical_features [batch,1] cardinality 366, static_real_features [batch,1], future_time_features [batch, prediction_length, 2], past_observed_mask [batch,past_length] 全 1。推断时 future_values 仅用于 teacher-forcing 训练验证,真实预测通过 generate 采样获得。
  • 轴顺序: [batch, time] / [batch, time, feature],频率 monthly,past_length 必须 ≥ 61 (max_lag 37 + context 24)。
  • 缩放: 模型内部 scaling=true,基于 context 计算 loc/scale 并逆变换输出。
  • 预处理: 无额外归一化,输入直接为 float32 随机张量,演示 real NPU 调度;真实业务替换为归一化后的历史销量/游客量即可复用本脚本。
  • SHA 签名: 合成数据 seed 固定,inputs 统计: past_values mean 0.0854 std 0.99 min -2.51 max 2.21 (batch=2, past_length 61)。

环境与安装

pip install -r requirements.txt
# requirements.txt
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# numpy==1.26.4
# pandas==3.0.2
# accelerate>=0.26.0

# 确认 NPU 可用
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available(), torch.npu.device_count(), torch.npu.get_device_name(0))"
  • 设备要求: 至少 1 张 Ascend910 / 910B,torch.npu.is_available()==True,显式 npu:0。
  • 已验证: Ascend910_9362, CANN 8.5.1, Python 3.11.14。
  • 显存: 模型 0.28 MiB, 单 forward 峰值 < 4 MiB, 批量 2 时 HBM 3109 MiB 基础占用。

NPU 推理

默认命令即为真实 NPU 推理(无需参数):

python inference.py

行为:

  1. 优先从 /opt/atomgit/adapt-npu-agent/models/time-series-transformer-tourism-monthly 本地加载权重,否则从 huggingface/time-series-transformer-tourism-monthly 拉取。
  2. 显式 torch.npu.set_device(0) 并 model.to("npu:0"),所有输入张量 .to("npu:0"),首参 device 校验为 npu:0。
  3. 执行两次: 确定性 model(**inputs) (teacher-forcing) 用于一致性校验,以及 model.generate(...) (num_parallel_samples=100) 用于概率预测演示。
  4. 计时前后均 torch.npu.synchronize(),分别报告 compile 首轮与稳定均值。
  5. 打印配置、输入摘要、输出分布参数、采样中位数/均值与性能。

真实结果(NPU, npu:0, float32)

确定性 forward params (3,2,24) [df, loc, scale] 来自 model(**inputs):

param[0] (df) mean=3.4915 std=0.3615 min=2.789 max=4.169 sample[0,:5]=[3.42148, 3.40293, 3.40746, 3.84509, 2.82426]
param[1] (loc) mean=0.5218 std=0.4287 min=-0.165 max=1.743 sample[0,:5]=[0.4877, 0.5492, 0.5371, 0.1571, 1.4946]
param[2] (scale) mean=0.5713 std=0.231 min=0.305 max=1.291 sample[0,:5]=[0.5149, 0.5341, 0.5429, 0.3706, 1.1505]
stacked shape (3,2,24) finite True

概率采样 generate (2,100,24):

sequences shape (2,100,24) mean=0.6294 std=1.0605 min=-18.30 max=15.14
median batch0 first6 [0.3715, 0.6335, 0.5789, 0.2611, 1.0182, 0.2334]
median batch1 first6 [0.4297, 0.4184, 1.1570, 0.6441, 0.3630, 0.2745]
mean batch0 first6 [0.3222, 0.6463, 0.7202, 0.3229, 1.0944, 0.2548]

说明: 采样涉及 aten::_standard_gamma (CPU fallback) 与 aten::lgamma (CPU fallback),属已知算子限制,不代表整图 fallback,核心 Transformer 仍在 NPU。

一致性验证

同权重、同输入 (seed=42, batch=2, past_length 61)、同 dtype (float32)、同 eval 模式 分别跑 CPU 与 NPU 的确定性 forward 并保存 cpu.npy / npu.npy:

python scripts/compare_outputs.py --cpu cpu.npy --npu npu.npy --task forecasting --atol 1e-4 --rtol 1e-3

结果:

{
  "task": "forecasting",
  "cpu_shape": [3, 2, 24],
  "npu_shape": [3, 2, 24],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 7.987e-05,
  "mean_abs_error": 2.55e-05,
  "passed": true
}

阈值依据 float32 与 Transformer 数值误差确定,单窗口 smoke 一致性,非全数据集 benchmark。

生成式采样因随机种子与 NPU 采样实现差异不用于严格数值一致性比较,已另用确定性 params 验证。

性能(npu:0, float32, batch=2, past61→pred24)

  • 首次编译 (含 graph compile): 271.66 ms (synchronize 计时)
  • 稳定 forward: avg 6.39 ms, min 6.33, max 6.43, p50 6.39, p90 6.42, p95 6.42
  • 吞吐: 313.14 windows/s (batch=2, 即 626 horizons/s)
  • generate (100 samples): 132 ms (含采样与分位数计算)
  • 10 轮测试, 预热 3 轮, 每轮前后 torch.npu.synchronize()。
  • HBM: 3109 MiB 基础 + 0.28 MiB 权重,无 OOM。

另一组同环境实测: avg 6.47 ms, min 6.36 max 7.08 p50 6.41 p90 6.52 308 windows/s,波动在误差内。

证据图

三张图由 xterm.js 根据本次真实日志生成 (--style raw),提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,仅为展示标签:

  • assets/agent_workflow.png - 侦察、环境、下载、NPU 推理、一致性、性能、校验完整工作流
  • assets/npu_device_call.png - npu-smi info、NPU 可用性、模型 param/input/output device=npu:0
  • assets/model_result.png - python inference.py 真实输出 (含配置、输入、输出、计时与 SUCCESS)

workflow device result

限制与说明

  • 单卡 npu:0 验证,批量 2、过去长度 61 固定,调大批量或序列长度需重新测显存。
  • lgamma 与 _standard_gamma 当前 CPU fallback,已验证不影响整体 NPU 调度,未来算子支持后可全 NPU。
  • 合成数据仅演示调度与数值一致性,真实 tourism 业务需接入真实时间戳、频率与逆缩放。
  • 采样结果具随机性,最终业务建议取 median/mean 并多次平均。
  • 未提交权重、数据集、缓存或日志,仅代码与证据。