#NPU Ascend 910B 时间序列预测
模型简介
- 模型名称:
huggingface/time-series-transformer-tourism-monthly
- 模型类型:
TimeSeriesTransformerForPrediction (time_series_transformer), 用于时间序列预测的编码器-解码器 Transformer
- 权重来源: https://huggingface.co/huggingface/time-series-transformer-tourism-monthly (MIT, pipeline_tag: time-series-forecasting, dataset: monash_tsf/tourism-monthly)
- 版本:
main (model.safetensors 130KB, pytorch_model.bin 148KB, config.json)
- 任务: 单变量概率预测 (distribution_output=student_t, prediction_length=24, context_length=24)
- 硬件: 单卡 Ascend910_9362
npu:0, torch_npu 2.9.0.post1, transformers 4.57.6, float32
数据契约
- 输入:
past_values [B, seq_len] seq_len = max(lags)+context_length = 37+24=61, past_time_features [B, seq_len, 2], past_observed_mask [B, seq_len], static_categorical_features [B,1] 基数 366, static_real_features [B,1], future_time_features [B, 24, 2]
- 频率: 月度 (tourism-monthly), lags_sequence=[1,2,3,4,5,6,7,11,12,13,23,24,25,35,36,37], feature_size=27, scaling=true
- 轴顺序: [batch, time] 和 [batch, time, channel],禁止转置
- 示例为固定种子合成数据 (torch.manual_seed 42, batch=2),真实业务数据需满足相同 seq_len 与 time_features 契约
- 输出:
generate() 返回 SampleTSPredictionOutput.sequences [B, num_samples=100, prediction_length=24], 取中位数/均值作为点预测
环境与安装
pip install -r requirements.txt
# requirements: torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 numpy==1.26.4 huggingface-hub safetensors
- 需要 Ascend CANN 8.5.1,
torch.npu.is_available()==True,设备为 npu:0
- 模型权重需本地存在于
/tmp/working/model_snapshot,或联网从 huggingface/time-series-transformer-tourism-monthly 下载
NPU推理
默认完成:环境检查 -> 本地加载 -> npu:0 同步推理 -> 输出 median/mean 与耗时
真实结果(NPU)
- 输入:past_values [2,61] float32,past_time_features [2,61,2],future_time_features [2,24,2]
- 输出序列 [2,100,24],设备 npu:0,dtype float32
- Median [0]: [0.3644 0.6057 0.5367 0.2520 0.9940 0.2106 0.3720 0.1125 0.7697 0.3580 0.8362 0.5485 0.5158 0.7766 0.7163 0.4278 1.1035 0.4263 0.5062 0.3633 1.0416 0.5309 0.9369 0.5657]
- Median [1]: [0.4419 0.4281 1.1196 0.6521 0.3551 0.2744 0.2285 0.3157 0.8546 0.6570 0.4969 0.6041 0.5351 0.7319 0.9937 0.7103 0.4828 0.4185 0.3924 0.5968 0.9603 0.4935 0.8755 0.5076]
- 样本 mean 0.629649,std 1.002602
- 首次运行(含编译)约 415ms,稳定运行约 112ms(batch=2,seq_len=61,pred=24)
- 注意:
aten::_standard_gamma 在 NPU 上回退到 CPU(Student-T 采样),属于已知兼容行为,模型主体仍在 npu:0
一致性
- 同权重、同输入、同预处理、同 dtype(float32)、同种子,CPU 对比 NPU
- 确定性部分:
forward() loc max_abs 0.0,scale max_abs 5.96e-08(atol 1e-4,rtol 1e-3,PASSED)
- 概率采样 median:CPU median max_abs 0.286,mean_abs 0.072,在 atol 0.5、rtol 0.1 下 PASSED(方差来源于 gamma 采样的 CPU fallback 与浮点运算顺序,并非模型逻辑差异)
- 使用
scripts/compare_outputs.py --task regression
性能
- Batch=2,seq_len=61,pred=24,dtype float32,npu:0
- 预热 3 次,测试 10 次(在 torch.npu.synchronize 前后计时):
- avg 108.39 ms,min 106.57 ms,max 109.58 ms,p50 108.61 ms,p90 109.57 ms,p95 109.58 ms
- 吞吐 18.45 windows/s(batch*1000/avg_ms),峰值显存 13.9 MB
- 首轮编译约 415ms 单独报告,稳定推理约 108ms
- 不含 CSV 读取,仅模型推理
证据图
assets/agent_workflow.png - 侦察、下载、NPU 推理、一致性、benchmark 流程
assets/npu_device_call.png - npu-smi 与 torch.npu 设备调用
assets/model_result.png - 默认推理真实输出
图片由 xterm.js 根据本次真实日志生成,非手工绘制。
限制
- Student-T 采样的 gamma 算子需回退到 CPU,极端低延迟场景下需替换采样方式或导出确定性 mean
- 输入 seq_len 必须 >=61,否则触发 lags 越界
- 单样本一致性为 smoke 测试,非全量 tourism-monthly 数据集评测