L
Lumiire/time-series-transformer-tourism-monthly
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

time-series-transformer-tourism-monthly - 昇腾 NPU 适配

简介

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_model26
上下文长度(context_length)24(月)
预测长度(prediction_length)24(月)
概率分布头Student-t
并行采样数100
静态类别特征 cardinality366(旅游序列编号)
权重格式 / 大小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)
Python3.11.14
PyTorch2.9.0
torch_npu2.9.0.post1
CANN8.5.1
transformers4.57.6
vLLM-Ascend0.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:0

服务化部署

python inference.py --server --port 18000 --device npu:0

服务启动后暴露两个 HTTP 接口:

接口方法说明
/healthGET健康检查,返回设备与模型状态
/forecastPOST概率预测,入参 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}'

Smoke 验证

执行以下命令验证模型在 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 参考输出的一致性。

验证方法

  1. 使用真实权重分别在 npu:0 与 cpu 上加载同一模型;
  2. 构造完全相同的输入(61 个月历史 + 时间特征 + 静态特征);
  3. 执行模型前向传播,对比 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),精度无损。

注意事项

  1. 历史长度要求:模型的 lags_sequence 最大回看为 37,past_values 建议提供 61 个月(context_length 24 + max_lag 37)以构造完整的 lag 特征,否则会报 lags cannot go further than history length。
  2. 时间特征构造:模型需要 num_time_features=2 的逐月特征([月, 年])。本仓库的 inference.py 已内置按月频率自动构造的逻辑,用户只需提供历史数值与最后观测月份。
  3. NPU 设备编号:npu-smi info 中的 NPU 组编号与 PyTorch 设备号并非同一概念,需以 torch.npu.get_device_name(0) 确认实际设备。
  4. 端口冲突:服务化部署如遇端口占用,通过 --port 更换端口即可,不影响其他用户进程。
  5. 采样算子回退:aten::_standard_gamma 当前在 NPU 后端不支持,会回退到 CPU 执行(npu_cpu_fallback),该行为由框架自动处理,不影响结果正确性。
  6. 数据说明:demo 输入为按旅游月度数据统计特征(年季节性 + 上行趋势 + 噪声)确定性生成的模拟序列,用于验证部署链路;实际使用时请替换为真实历史数据。