g
gcw_coj3XaOd/huggingface_time-series-transformer-tourism-monthly
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

TimeSeriesTransformer (Tourism Monthly) - 昇腾 NPU 推理部署

1. 模型简介

模型名称: huggingface/time-series-transformer-tourism-monthly

模型链接: HuggingFace

模型描述: 基于 Transformer 编码器-解码器架构的时间序列预测模型,专为月度旅游数据 forecasting 训练。输入历史时间序列及时间特征,输出未来 24 个月的预测值(Student-T 分布采样)。

模型架构: TimeSeriesTransformer (Encoder-Decoder Transformer)

参数规模: 30,537 参数

输入规格:

  • past_values: (batch_size, 61) — 过去 61 个时间步的值
  • past_time_features: (batch_size, 61, 2) — 过去时间步的时间特征
  • past_observed_mask: (batch_size, 61) — 观测掩码
  • static_categorical_features: (batch_size, 1) — 静态类别特征
  • static_real_features: (batch_size, 1) — 静态实数特征
  • future_time_features: (batch_size, 24, 2) — 未来时间步的时间特征

输出规格:

  • sequences: (batch_size, num_samples, prediction_length) — 预测采样序列

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.27.0HuggingFace Transformers
huggingface_hub>= 0.20.0模型下载
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu>=2.1.0 transformers>=4.27.0 huggingface_hub>=0.20.0 numpy>=1.24.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

huggingface-cli download huggingface/time-series-transformer-tourism-monthly --local-dir ./model_weights

3.3 运行推理

# 基础推理(batch_size=2)
python inference.py --model-path ./model_weights --batch-size 2

# 自定义参数
python inference.py --model-path ./model_weights --batch-size 4 --num-samples 200

3.3 启动服务

本模型为标准 PyTorch 模型,直接通过 inference.py 脚本启动推理,无需额外服务进程:

python inference.py --model-path ./model_weights --batch-size 2

3.4 推理调用

推理脚本支持单批次推理和自回归生成,自动完成模型加载、NPU 迁移、前向传播和序列采样全流程。

基础调用:

python inference.py --model-path ./model_weights --batch-size 2

自定义参数调用:

python inference.py --model-path ./model_weights --batch-size 4 --num-samples 200

使用自定义测试数据调用:

python inference.py --model-path ./model_weights --batch-path ./my_data.pt --batch-size 8

3.5 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--batch-pathstr内置路径测试数据 .pt 文件路径
--batch-sizeint2推理批次大小
--num-samplesint100自回归采样数量

4. 推理成功日志

4.1 单条推理日志

[模型] huggingface/time-series-transformer-tourism-monthly
[设备] Ascend910_9362 (npu:0)
[输入] past_values: torch.Size([2, 61]), future_time_features: torch.Size([2, 24, 2])
[输出] sequences: torch.Size([2, 100, 24])
[前向传播耗时] 0.2766s
[自回归生成耗时] 0.1441s
[状态] SUCCESS

5. 测试样例及输出结果

样例 1:基础推理(batch_size=2)

运行命令:

python inference.py --model-path ./model_weights --batch-size 2

输出:

============================================================
Time Series Transformer - Ascend NPU Inference
============================================================

[INFO] Checking NPU device availability...
[OK] NPU device count: 2
[OK] NPU device name: Ascend910_9362

[INFO] Loading model from: ./model_weights
[OK] Model loaded in 0.03s
[INFO] Total parameters: 30,537

[INFO] Moving model to NPU device...
[OK] Model moved to NPU in 1.14s

[INFO] Running forward pass...
[OK] Forward pass completed in 0.28s
[OK] Loss: 9.638490

[INFO] Generating predictions...
[OK] Generate completed in 0.14s
[OK] Prediction sequences shape: torch.Size([2, 100, 24])

[INFO] Mean predictions (first sample, first 5 steps):
  [24700.682 32572.451 32531.746 36262.902 30570.146]

============================================================
[SUMMARY] Inference Results
============================================================
[MODEL]     huggingface/time-series-transformer-tourism-monthly
[DEVICE]    Ascend910_9362 (npu:0)
[BATCH]     2
[INPUT]     past_values: torch.Size([2, 61])
[OUTPUT]    sequences: torch.Size([2, 100, 24])
[PARAMS]    30,537
[FORWARD]   0.2766s
[GENERATE]  0.1441s
[STATUS]    SUCCESS
============================================================

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: hf-internal-testing/tourism-monthly-batch (64 samples)

指标结果说明
训练 Loss (NLL)9.638490使用官方测试 batch 前向传播
参数量30,537模型总参数量
前向传播耗时0.28sbatch_size=2, Ascend910
自回归生成耗时0.14s100 parallel samples, 24 步

评测命令:

python inference.py --model-path ./model_weights --batch-size 2

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 卡数: 2 卡
  • 显存占用: ~3.1 GB HBM(单卡)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.1.0+

9. 已知问题

  • aten::lgamma.out 和 aten::_standard_gamma 算子当前在 NPU 后端回退到 CPU 执行(用于 Student-T 分布计算),对性能有一定影响,但功能正确。
  • 模型为标准 PyTorch 模型,完全兼容 Ascend NPU,无框架级兼容性问题。