1
gcw_xUvjaUyi/t0-alpha-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

t0-alpha — 昇腾 Ascend NPU 推理适配

theforecastingcompany/t0-alpha 是 Nixtla / The Forecasting Company 的开源时间序列预测基础模型 t0 的首个公开迭代 t0-alpha。

  • 架构: decoder-style patch transformer (约 102M 参数, 24 层, embed_dim=512, patch_size=32)
  • 任务: 概率多步时间序列预测 (probabilistic multi-horizon forecasting)
  • 官方库: tfc-t0 (PyPI) 提供 T0Forecaster / T0Config / Forecast
  • 本仓库: 将该模型的推理入口适配到昇腾 Ascend 910 (torch_npu), 全程不使用 CUDA, 模型与输入 Tensor 全部运行在 npu:0。

昇腾 NPU 适配要点

  1. 代码全域不含任何 CUDA 语句; init_npu() 在入口用 try-except 捕获异常:
    • torch_npu import 失败 → 打印清晰中文错误并优雅退出
    • torch.npu.is_available() == False 或 device_count() < 1 → 同样优雅退出
  2. 模型权重从 HF 镜像 (HF_ENDPOINT=https://hf-mirror.com) 自动下载, 本地缓存优先:
    • 缓存路径: ./model_weights/theforecastingcompany__t0-alpha (模型全名 / 换成 __)
    • 启动时命中缓存则跳过下载; 未命中才走 snapshot_download
  3. t0-alpha 为自定义 remote-code 模型, 官方 tfc-t0 库提供全部模型代码 (基于 PyTorchModelHubMixin), 因此无需 trust_remote_code, 直接 T0Forecaster.from_pretrained(<本地目录>, local_files_only=True)。
  4. config.json 若因受限下载不到, 程序会用官方 T0Config.medium() 自动重建 (即已发布 checkpoint 的公开超参数)。

权重可得性说明: theforecastingcompany/t0-alpha 在 HuggingFace 上是 gated(受限)模型, 匿名访问会被拒绝 (403)。当前镜像 hf-mirror.com、 ModelScope、GitCode 均无可用镜像, 需要 HF token 才能下载 model.safetensors。 若运行时出现 权重文件缺失且无法从镜像下载, 请将权重手动放置到 ./model_weights/theforecastingcompany__t0-alpha/model.safetensors 后重试。

环境与依赖

  • 本机: Python 3.11, torch 2.9.0+cpu, torch_npu 2.9.0.post1, CANN 8.5.1, Ascend 910 x2
  • 依赖见 requirements.txt。
  • 注意: transformers 要求 huggingface-hub<1.0, 而 tfc-t0 声明 huggingface-hub>=1.0; 请用 --no-deps 安装 tfc-t0, 避免破坏 transformers。
pip install "huggingface-hub>=0.34,<1.0" jaxtyping rotary-embedding-torch einops safetensors
pip install --no-deps "tfc-t0==0.3.0"
pip install fastapi uvicorn

CLI 用法

python3 inference.py --text "[1.0,2.1,3.2,4.0,5.1,6.0,7.2,8.3]" --horizon 8 --quantiles "0.1,0.5,0.9"

输出为推理 JSON, 含 median(中位数点预测)、quantile_values(各分位数预测) 与设备信息。

FastAPI 用法

uvicorn inference:app --host 0.0.0.0 --port 8003
curl -s -X POST http://127.0.0.1:8003/predict \
  -H 'Content-Type: application/json' \
  -d '{"text":[1.0,2.1,3.2,4.0,5.1,6.0,7.2,8.3],"horizon":8}'

返回推理 JSON; GET / 可查看服务信息与 /docs。

输入 / 输出契约

  • 输入: 数值时间序列数组, 例如 [1.0, 2.1, 3.2, 4.0]; 支持 (T,)、(B, T) 或 (B, V, T)。
  • 输出: median 为点预测序列, quantile_values 为各分位数的概率区间预测 (模型原生分位数为 0.1/0.25/0.5/0.75/0.9, 其余分位数由插值得到)。

权重缓存路径

model_weights/theforecastingcompany__t0-alpha/
├── config.json
└── model.safetensors

截图

  • assets/agent_workflow.png — NPU 环境部署、依赖安装、任务启动日志
  • assets/npu_device_call.png — 命令行 python 推理运行 (真实 stdout)
  • assets/model_result.png — FastAPI curl 上传输入返回推理 JSON (真实 HTTP 响应)