#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.safetensors130KB /pytorch_model.bin148KB, 总参数 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 一致性校验。
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 并逆变换输出。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))"torch.npu.is_available()==True,显式 npu:0。默认命令即为真实 NPU 推理(无需参数):
python inference.py行为:
/opt/atomgit/adapt-npu-agent/models/time-series-transformer-tourism-monthly 本地加载权重,否则从 huggingface/time-series-transformer-tourism-monthly 拉取。torch.npu.set_device(0) 并 model.to("npu:0"),所有输入张量 .to("npu:0"),首参 device 校验为 npu:0。model(**inputs) (teacher-forcing) 用于一致性校验,以及 model.generate(...) (num_parallel_samples=100) 用于概率预测演示。torch.npu.synchronize(),分别报告 compile 首轮与稳定均值。确定性 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 验证。
generate (100 samples): 132 ms (含采样与分位数计算)torch.npu.synchronize()。另一组同环境实测: 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:0assets/model_result.png - python inference.py 真实输出 (含配置、输入、输出、计时与 SUCCESS)

npu:0 验证,批量 2、过去长度 61 固定,调大批量或序列长度需重新测显存。lgamma 与 _standard_gamma 当前 CPU fallback,已验证不影响整体 NPU 调度,未来算子支持后可全 NPU。