#NPU
将 HuggingFace 模型 huggingface/time-series-transformer-tourism-monthly 适配到单卡昇腾 NPU(Ascend 910B / npu:0)真实运行的时间序列预测模型仓。
huggingface/time-series-transformer-tourism-monthly2a40ad41f6ffe61e7bef6099b08c6c2fce36ac35TimeSeriesTransformerForPrediction(Encoder-Decoder Transformer + StudentT 概率分布头)tourism_monthly 月度游客量数据集(366 条序列)npu:0(Ascend910_9362)Time Series Transformer 是将 vanilla Transformer 用于时间序列预测的模型, Encoder 读取历史窗口(context),Decoder 自回归生成未来值,分布头输出 StudentT 分布,通过采样得到预测样本。本模型在 tourism_monthly 数据集上 训练 30 epochs。
关键配置:
| 参数 | 值 |
|---|---|
context_length | 24(月) |
prediction_length | 24(月) |
input_size | 1(单变量) |
num_parallel_samples | 100 |
d_model | 26 |
| encoder / decoder layers | 2 / 2 |
lags_sequence | [1..7, 11..13, 23..25, 35..37] |
num_time_features | 2(month_of_year + age) |
num_static_categorical_features | 1(序列 ID,cardinality 366) |
num_static_real_features | 1(序列均值) |
distribution_output | student_t |
hf-internal-testing/tourism-monthly-batch
官方训练批(真实 monash_tsf/tourism_monthly 月度数据,366 条序列)。
序列数值为月度游客量(正整数,量级数百至数万)。past_values:[batch, 61](= context_length 24 + max(lags) 37),
dtype float32past_time_features:[batch, 61, 2](month_of_year 归一化 + age 特征)past_observed_mask:[batch, 61](全 1,无缺失)static_categorical_features:[batch, 1](序列 ID,int64)static_real_features:[batch, 1](序列均值)future_time_features:[batch, 24, 2]sequences 为 [batch, 100, 24](100 个样本 × 24 步预测),
取 median 得到点预测;分位数可反映不确定性。scaling=True
自动进行均值/尺度归一化。Ascend910_9362),CANN 8.5.1pip install -r requirements.txt
# 昇腾 NPU 运行时(torch_npu)需按 Ascend 官方文档安装并完成 CANN 环境配置# 默认:自动从 HuggingFace 下载权重与官方数据批(优先本地缓存)
python inference.py
# 使用本地模型快照与本地官方数据批(无网络依赖)
python inference.py \
--model-dir /path/to/model_snapshot \
--batch-path /path/to/train-batch.pt \
--num-series 4 \
--output-dir ./outputs脚本会把模型与所有参与计算的 Tensor 显式迁移到 npu:0,运行真实
StudentT 概率预测并打印未来 24 个月的游客量中位数预测。
在 npu:0 上以 4 条真实 tourism_monthly 序列推理,输出
sequences [4, 100, 24],中位数预测(未来 24 个月游客量,节选):
序列 1 (item_id=29): [25353, 32548, 32719, 35365, 30097, 39338, 34247, 32980, ...]
序列 2 (item_id=31): [25897, 34971, 49073, 58331, 45850, 51218, 48072, 34757, ...]
序列 3 (item_id=32): [1130, 1019, 1015, 1030, 999, 1313, 2538, 3083, ...]完整输出见 assets/model_result.png(由 xterm.js 根据本次真实日志生成)。
CPU 与 NPU 使用同一权重、同一输入切片、同一 dtype(float32)、同一随机种子 对比:
5.85e-5,loc/scale 最大绝对差 0.0 → 高度一致。3.2%,属概率采样的
正常统计波动。atol=1e-4, rtol=1e-3 校验通过
(compare_outputs.py 返回 0)。Ascend 后端不支持 aten::_standard_gamma(StudentT 采样所需的 gamma 随机数
生成),默认会静默回退 CPU。为满足「真实 NPU 推理、无 CPU fallback」,
本仓 inference.py 内置了 Marsaglia-Tsang 方法的纯 PyTorch 等价实现,
仅使用 NPU 原生支持的 randn/rand/log/sqrt/pow 等算子,等价替换
torch._standard_gamma,使采样全程在 npu:0 上执行。该替换不改变预测
分布,仅在设备上以等价算法生成同分布随机样本(已用 30000 样本统计验证
均值相对误差 <1%)。
单卡 npu:0,batch=16,context=24,prediction=24,num_samples=100,
dtype=float32,预热 3 次、正式 10 次:
| 指标 | 值 |
|---|---|
| 首轮(编译后首次) | 0.140 s |
| 平均耗时 | 0.143 s |
| min / max | 0.137 / 0.150 s |
| p50 / p90 / p95 | 0.145 / 0.149 / 0.149 s |
| 峰值显存 | 0.129 GB |
| 吞吐量 | 111.5 windows/s |
三张证据图均由 xterm.js 根据本次真实执行日志生成(非手工绘制):
assets/agent_workflow.png:模型侦察、数据检查、下载、加载、NPU 推理、
一致性、性能基准与提交校验的完整工作流。assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数
device/dtype、输入输出 Tensor device。assets/model_result.png:默认 python inference.py 的真实完整输出,
含窗口摘要、真实预测、同步耗时与最终状态。npu:0
可用的设备名。