w
weasonlee/t0-alpha
模型介绍
文件和版本
Pull Requests
讨论
分析

t0-alpha — Ascend NPU Adaptation

模型简介

t0-alpha 是 The Forecasting Company 发布的开放权重时序预测基础模型(~102M 参数,Apache-2.0):decoder-style patch transformer,原生支持多协变量(历史协变量 + 已知未来协变量),输出概率型多步预测(分位数)。在 fev-bench(Skill 42.2)与 GIFT-Eval(CRPS 0.4941 / MASE 0.7240)榜单居前。

原始模型:theforecastingcompany/t0-alpha 官方代码/包:theforecastingcompany/tfc-t0(pip install tfc-t0,本仓库已验证 0.x 可用)

架构(官方 T0Config.medium(),embed_dim=512、24 层、8 heads、MLP 2048、patch_size=32、GROUP 注意力每 3 层一次、arcsinh scaler、分位数头 (0.1, 0.25, 0.5, 0.75, 0.9))。

⚠️ 权重获取说明(重要):上游 Hugging Face 仓库为 gated repo(需向作者申请访问授权)。本容器内 models/ 目录仅有 README/LICENSE,多次用 snapshot_download(hf-mirror)重下均被拒绝:Access to model theforecastingcompany/t0-alpha is restricted。因此本仓库的推理演示采用官方发布架构 + 固定种子随机初始化的同构模型(101.6M 参数),完整验证 NPU/CPU 双端执行路径(架构、arcsinh scaler、自回归 rollout、分位数头);不代表官方权重的预测质量。获得授权后,将 safetensors 放入 models/ 即自动切换为 T0Forecaster.from_pretrained(models/) 加载真实权重,无需改代码。

环境要求

  • 昇腾 Ascend 910(本仓库在 910 ×2 上验证,单卡即可推理)
  • Python 3.11
  • torch 2.9.0 + torch-npu 2.9.0.post1
  • tfc-t0(官方包)

部署步骤

# 1. 克隆仓库
git clone https://gitcode.com/weasonlee/t0-alpha.git
cd t0-alpha

# 2. 安装依赖
pip install -r requirements.txt

# 3. 下载权重到 models/(已 .gitignore 排除)
#    注意:上游为 gated repo,需先在 HF 页面申请访问授权并配置 token
mkdir -p models
HF_ENDPOINT=https://hf-mirror.com python -c "
from huggingface_hub import snapshot_download
snapshot_download('theforecastingcompany/t0-alpha', local_dir='models')"

# 4. 推理(NPU;无权重时自动回退为同架构随机初始化并明确标注)
python inference.py --device npu
# 或 CPU
python inference.py --device cpu

推理示例

$ python inference.py --device npu
torch.npu.is_available() = True
torch.npu.device_count() = 2
[info] device = npu:0, torch = 2.9.0+cpu
[load] random init, published T0Config.medium() architecture
[load] parameters = 101.6M
[info] context = (4, 512) (seasonal+trend+noise), horizon = 64, quantiles = [0.1, 0.5, 0.9]
[time] run 0: 41 ms
[time] avg: 40 ms (context 512 -> horizon 64)
[out ] quantiles: shape=(4, 64, 3) mean=3.0467 std=4.5856 min=-1.169 max=30.956
[out ] median: mean=1.5651 std=0.9316

===== CPU vs NPU comparison (batch=4, horizon=64) =====
  quantiles  cos_sim=1.00000000 max_abs_err=6.104e-05 mean_abs_err=2.076e-06
  median     cos_sim=1.00000000 max_abs_err=4.053e-06 mean_abs_err=8.160e-07

输入:固定种子(seed=42)合成的季节+趋势+噪声时序(batch=4 × 512 步上下文),预测 horizon=64、分位数 [0.1, 0.5, 0.9];CPU 与 NPU 的模型初始化种子与输入完全一致。

CPU vs NPU 精度对比

输出cos_simmax_abs_errmean_abs_err
quantiles(4×64×3 分位数预测)1.000000006.104e-052.076e-06
median(4×64 中位数预测)1.000000004.053e-068.160e-07
单次 rollout 耗时(含同步,3 次均值)NPU ~40 ms vs CPU ~351 ms(~9× 加速)——

结论:NPU(Ascend 910, torch-npu 2.9.0.post1)与 CPU 的自回归 rollout 与分位数输出完全一致(误差 1e-5 量级,fp32),执行路径适配成功。待获得官方权重授权后可直接复跑真实推理。

仓库结构

├── inference.py        # 推理脚本(T0Forecaster + 合成时序 + NPU/CPU 对比)
├── readme.md
├── requirements.txt
├── models/             # 权重目录(gated,需自行下载,已 gitignore)
├── assets/             # 运行截图
│   ├── agent_workflow.png
│   ├── npu_device_call.png
│   └── model_result.png
└── results/            # 每次运行的输出/耗时产物(gitignore)

引用

The Forecasting Company. "t0-alpha: an open-weights time-series forecasting foundation model." 2025. https://huggingface.co/theforecastingcompany/t0-alpha