模型适配 · 表格/时序数据零样本点预测 (Time-Series Forecasting) · 昇腾 NPU
Beta 版本头信息
{hardware NPU}model.safetensors(≈ 337 MB)[batch, horizon](horizon 为预测长度)AutoModelForCausalLM + trust_remote_code + model.generate(...) 调用,但模型实为时序预测模型(无 tokenizer、输入是原始时序数值),不适用 vLLM-Ascend 的 OpenAI 兼容 chat 服务;本项目采用 torch_npu + transformers 在昇腾 NPU 上完成适配,并以 FastAPI 提供 HTTP 服务化推理。timer/ 包(configuration_timer.py / modeling_timer.py / ts_generation_mixin.py),与官方 state_dict 键名逐一对齐。模型为 fp32、确定性点预测(_greedy_search 直接取回归输出),全程零算子替换,昇腾 NPU 原生可跑。| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| NPU | Ascend 910B (2 × 64GB HBM) |
| Python | 3.11.14 |
输入为平滑时序信号(正弦 + 线性趋势 + 小噪声),对比 CPU 与 NPU 的预测序列(lookback=1440, horizon=96)。
| 指标 | 数值 |
|---|---|
| Cosine Similarity (NPU vs CPU) | 1.0 |
| 最大绝对误差 | 2e-06 |
| 相对误差 | 1e-06(满足 < 1%) |
全程 fp32、零算子替换,NPU 与 CPU 结果几乎逐位一致。
| 指标 | 数值 |
|---|---|
| 平均延迟 (lookback=2880, horizon=96) | 7.86 ms |
| 峰值 HBM | 0.391 GB |
| CPU 参考延迟 (同配置) | 139.3 ms |
| 参数量 | 84M |
pip install -r requirements.txt注意: 本机已验证
transformers==4.57.6(官方要求~=4.40.1)。Timer 使用自定义远程代码 + Cache API,其他 transformers 大版本可能不兼容。
# 方式一:HuggingFace (hf-mirror,推荐)
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download thuml/timer-base-84m --local-dir /models/timer-base-84m
# 方式二:ModelScope(如可用)
python -m modelscope download --model thuml/timer-base-84m --local_dir /models/timer-base-84m
# 方式三:AtomGit 镜像仓
git clone https://gitcode.com/hf_mirrors/thuml/timer-base-84m.gitpython3 inference.py --model-path /models/timer-base-84m --device npu:0 --lookback 2880 --horizon 96 --n-runs 3说明:
inference.py直接使用本仓库timer/下的 vendored 模型代码(与官方 state_dict 键名一致),无需trust_remote_code。horizon 最大支持 2880(须小于输入长度)。
python3 inference.py --model-path /models/timer-base-84m --device npu:0 --lookback 1440 --horizon 96 --compare# 启动服务(默认 8000 端口)
python3 inference.py --model-path /models/timer-base-84m --device npu:0 --serve --port 8000
# 健康检查
curl -s http://127.0.0.1:8000/health
# 表格/时序数据零样本点预测(values 为一段时序数值)
curl -s http://127.0.0.1:8000/v1/forecast \
-H 'Content-Type: application/json' \
-d '{"values": [1.0, 1.1, 1.2, 1.5, 1.8, 2.0, 2.1, ...], "horizon": 96, "revin": true}'见 assets/ 目录(agent_workflow.png / npu_device_call.png / model_result.png)。
贡献者: ohhoohho | 赛道: 模型适配赛道