time-series-transformer-tourism-monthly 是 HuggingFace 生态中的经典时序预测模型,基于 TimeSeriesTransformerForPrediction 架构,在 monash_tsf 基准的 tourism-monthly 月度旅游数据集上训练 30 个 epoch 得到。模型以"机器翻译式"的编码器-解码器 Transformer 结构处理时序数据,推理阶段通过 student_t 分布头自回归采样生成未来 24 个月的概率性预测(而非单点预测),能够同时输出预测中位数与分位区间,适合旅游客流、消费趋势等需要不确定性度量的业务场景。
本仓库为该模型在华为昇腾 Ascend NPU 上的适配部署版本。模型权重自 GitCode 镜像仓库拉取(GitCode git 协议不稳定时按序降级至 ModelScope 拉取),已在 Atlas 800 推理服务器上通过 torch_npu 后端完成加载与推理验证,并封装为可一键启动的服务化推理接口。
| 属性 | 数值 |
|---|---|
| 架构 | TimeSeriesTransformerForPrediction(Encoder-Decoder Transformer) |
| 编码器 / 解码器层数 | 2 / 2 |
| d_model | 26 |
| 上下文长度(context_length) | 24(月) |
| 预测长度(prediction_length) | 24(月) |
| 概率分布头 | Student-t |
| 并行采样数 | 100 |
| 静态类别特征 cardinality | 366(旅游序列编号) |
| 权重格式 / 大小 | SafeTensors / 约 130 KB(99 个张量,float32) |
| 训练数据集 | monash_tsf / tourism-monthly |
本次适配在以下软硬件环境中完成验证,npu-smi info 可确认设备信息。
| 组件 | 规格 |
|---|---|
| NPU 设备 | Ascend 910(Atlas 800 推理服务器,2 芯片) |
| NPU 显存 | 2 × 64 GB HBM |
| 实际使用设备 | npu:0(物理芯片 Phy-ID 4) |
| CPU 架构 | aarch64(ARM64 / 鲲鹏) |
| 组件 | 版本 |
|---|---|
| 操作系统 | Linux(华为 Cloud EulerOS) |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| CANN | 8.5.1 |
| transformers | 4.57.6 |
| vLLM-Ascend | 0.18.0(环境中提供 Ascend 运行时能力) |
| numpy / safetensors / fastapi / uvicorn | 最新稳定版 |
pip install torch torch-npu transformers numpy safetensors fastapi uvicorn pydantic# 方式一:GitCode 镜像(推荐)
git clone --depth 1 https://gitcode.com/hf_mirrors/huggingface/time-series-transformer-tourism-monthly.git
# 方式二:GitCode 不可达时使用 ModelScope
modelscope download --model huggingface/time-series-transformer-tourism-monthly --local_dir ./time-series-transformer-tourism-monthly# 指定 NPU 设备运行(默认 npu:0,自动生成 61 个月旅游特征序列作为输入)
python inference.py --model-dir ./time-series-transformer-tourism-monthly --device npu:0
# 使用自定义历史序列(.npy / .txt / .json,至少 24 个点)
python inference.py --past-values my_series.npy --device npu:0python inference.py --server --port 18000 --device npu:0服务启动后暴露两个 HTTP 接口:
| 接口 | 方法 | 说明 |
|---|---|---|
/health | GET | 健康检查,返回设备与模型状态 |
/forecast | POST | 概率预测,入参 past_values 等,返回 24 步中位数 / p10 / p90 |
curl http://localhost:18000/health
curl -X POST http://localhost:18000/forecast \
-H "Content-Type: application/json" \
-d '{"past_values": [6100, 6250, ...], "series_id": 42, "end_date": "2023-12", "samples": 100}'执行以下命令验证模型在 NPU 上的推理功能(使用确定种子生成与训练数据特征一致的旅游月度序列):
python inference.py --model-dir ./time-series-transformer-tourism-monthly --device npu:0 --seed 42实际运行输出(截取):
[load] loading model from ./time-series-transformer-tourism-monthly on npu:0 ...
[input] generated demo tourism-like series of length 85
Time Series Transformer (tourism-monthly) - NPU Forecast
past window (months): 61 forecast horizon: 24
parallel samples : 100
device : npu:0
--------------------------------------------------------------
step date median p10 p90
1 2024-01 11427.6 9131.1 14524.3
2 2024-02 11959.8 9054.7 15951.6
...
12 2024-12 13493.4 10488.3 18686.7
24 2025-12 20170.1 11778.8 27553.5
--------------------------------------------------------------
[save] results written to inference_result.json输出呈现清晰的年度季节性(夏季波谷、冬季波峰)与上行趋势,与旅游月度数据的先验特征吻合,说明模型在 NPU 上的生成链路工作正常。服务化模式下 curl /health 返回 {"status":"ok","device":"npu:0","model":"time-series-transformer-tourism-monthly"}。
在 Ascend 910B(npu:0)上使用 61 个月上下文、24 步预测、100 个并行采样路径的基准测试结果:
| 指标 | 数值 |
|---|---|
| 模型加载时间 | 约 2.5 秒 |
| 单次推理延迟(首请求) | 约 0.48 秒 |
| 单次推理延迟(热机后) | 约 0.11 秒 |
| NPU 进程内存 | 约 148 MB |
| HBM 占用增量 | 约 191 MB(3109 -> 3300 MB) |
| 推理时 AICore 利用率 | 约 13% - 14% |
| 空闲/推理功耗 | 约 181 W / 198 W |
模型权重仅约 130 KB,属于超轻量级模型,NPU 占用极低,非常适合在多用户共享的推理集群上随开随用。采样阶段(_standard_gamma)在 NPU 上自动回退至 CPU 执行,对整体延迟影响可忽略。
本次适配的核心精度验证目标是:同一权重、同一输入下,NPU 输出与 CPU 参考输出的一致性。
npu:0 与 cpu 上加载同一模型;loc(分布位置)与 scale(分布尺度)输出张量。| 检查项 | 结果 |
|---|---|
| 模型权重加载(SafeTensors -> NPU) | 通过 |
| 前向传播 loc 张量 NPU vs CPU 最大差值 | 0.000000 |
| 前向传播 scale 张量 NPU vs CPU 最大差值 | 0.000000 |
| 生成输出维度(1, 100, 24) | 通过 |
| 概率分布合法性(p10 <= median <= p90) | 通过 |
| 特征矩阵(Dummy 权重快速门禁) | 通过 |
结论:模型在昇腾 NPU 上的数值输出与 CPU 完全一致(浮点误差为 0),精度无损。
past_values 建议提供 61 个月(context_length 24 + max_lag 37)以构造完整的 lag 特征,否则会报 lags cannot go further than history length。num_time_features=2 的逐月特征([月, 年])。本仓库的 inference.py 已内置按月频率自动构造的逻辑,用户只需提供历史数值与最后观测月份。npu-smi info 中的 NPU 组编号与 PyTorch 设备号并非同一概念,需以 torch.npu.get_device_name(0) 确认实际设备。--port 更换端口即可,不影响其他用户进程。aten::_standard_gamma 当前在 NPU 后端不支持,会回退到 CPU 执行(npu_cpu_fallback),该行为由框架自动处理,不影响结果正确性。