项目简介
TimesFM 是谷歌研究团队开发的基于解码器的预训练时序基础模型,支持长上下文和连续分位数预测,提供 PyTorch 与 Flax 版本。
README 预览
# TimesFM\n\nTimesFM (Time Series Foundation Model) is a pretrained time-series foundation\nmodel developed by Google Research for time-series forecasting.\n\n* Paper:\n [A decoder-only foundation model for time-series forecasting](https://arxiv.org/abs/2310.10688),\n ICML 2024.\n* All checkpoints:\n [TimesFM Hugging Face Collection](https://huggingface.co/collections/google/timesfm-release-66e4be5fdb56e960c1e482a6).\n* [Google Research blog](https://research.google/blog/a-decoder-only-foundation-model-for-time-series-forecasting/).\n* TimesFM in Google 1P Products:\n * [BigQuery ML](https://cloud.google.com/bigquery/docs/timesfm-model): Enterprise level SQL queries for scalability and reliability.\n * [Google Sheets](https://workspaceupdates.googleblog.com/2026/02/forecast-data-in-connected-sheets-BigQueryML-TimesFM.html): For your daily spreadsheet. \n * [Vertex Model Garden](https://pantheon.corp.google.com/vertex-ai/publishers/google/model-garden/timesfm): Dockerized endpoint for agentic calling.\n\nThis open version is not an officially supported Google product.\n\n**Latest Model Version:** TimesFM 2.5\n\n**Archived Model Versions:**\n\n- 1.0 and 2.0: relevant code archived in the sub directory `v1`. You can `pip\n install timesfm==1.3.0` to install an older version of this package to load\n them.\n## Update - June 5, 2026\n\nUpdated PyPI to `timesfm=2.0.0`. See [Install](https://github.com/google-research/timesfm#from-pypi).\n\n## Update - Apr. 9, 2026\n\nAdded fine-tuning example using HuggingFace Transformers + PEFT (LoRA) — see\n[`timesfm-forecasting/examples/finetuning/`](timesfm-forecasting/examples/finetuning/).\nAlso added unit tests (`tests/`) and incorporated several community fixes.\n\nShoutout to [@kashif](https://github.com/kashif) and [@darkpowerxo](https://github.com/darkpowerxo). \n\n## Update - Mar. 19, 2026\n\nHuge shoutout to [@borealBytes](https://github.com/borealBytes) for adding the support for [AGE
常见问题 (4)
故障排除为什么 torch_compile=True 在 TimesFM PyTorch 中不起作用,以及如何让 torch.compile 加速推理?
这个 no-op 是由 load_checkpoint 内部的 self = torch.compile(self) 引起的:self 是一个局部变量,因此重新绑定它并不会改变 from_pretrained 返回的模型对象。
应用 PR #459 中的修复,或者在加载完成后直接编译 forward:
model = timesfm.TimesFM_2p5_200M_torch.from_pretrained(
"google/timesfm-2.5-200m-pytorch"
)
model.model.forward = torch.compile(model.model.forward)
等效地,将 timesfm_2p5_torch.py 中无效的那一行替换为:
self.model.forward = torch.compile(self.model.forward)
KV-cache 的切片赋值仍会导致 graph break,但 attention 和 MLP 的矩阵乘法已被编译,因此推理速度更快。在 A100 上,当 batch=64、context=512、horizon=128 时,延迟从 108.8 ms 降至 73.7 ms(1.48x)。在所报告的基准测试中,修复重新绑定问题后再编译整个模块并没有带来可测量的加速。
故障排除为什么 TimesFM 的 `xreg_mode` 在 `xreg + timesfm` 和 `timesfm + xreg` 下的行为与 docstring 中描述的相反?
受影响的实现中 docstring 写反了。请根据代码而非文档来使用模式:
- 若要先在目标值上拟合 xreg,然后用 TimesFM 预测残差,请使用:
xreg_mode="xreg + timesfm"- 若要先生成 TimesFM 预测,然后在其残差上拟合 xreg,请使用:
xreg_mode="timesfm + xreg"
这一点可以从 L488–L494 附近的分支得到证实:timesfm + xreg 使用 max(0, input_len - self.input_patch_len),注释为"For fitting residuals",而 xreg + timesfm 使用 input_len。在 docstring 被修正之前,应将文档中的描述视为相互颠倒。
故障排除如何修复 TimesFM 2.5 PyTorch 的 from_pretrained 报错 unexpected keyword argument 'proxies',以及 load_checkpoint 的 NotImplementedError?
此问题已由 timesfm PR #435 修复。请升级到包含该修复的版本,或安装最新源码:
pip install -U git+https://github.com/google-research/timesfm.git
然后重试:
import timesfm
tfm = timesfm.TimesFM_2p5_200M_torch.from_pretrained(
"google/timesfm-2.5-200m-pytorch"
)
如果无法升级,可暂时固定使用较旧版本的 Hub 客户端:
pip install "huggingface_hub<0.34"
# 例如:
pip install huggingface_hub==0.33.5
这可能与较新版本的 transformers/accelerate 发生冲突。在受影响的 timesfm 版本中,不要使用 load_checkpoint(local_path) 作为变通方法,因为其 PyTorch 子类会抛出 NotImplementedError;请改用已打补丁的 timesfm 构建版本。源码层面的修复方法是让模型构造函数接受并忽略新版 Mixin 的关键字参数,例如 def __init__(self, config, **_ignored):。
how_to如何在不使用 PyTorch 的情况下在 Apple Silicon 上运行 TimesFM 3.0?
使用 MLX 后端。它在 PR #476 中提供,位于 src/timesfm3/mlx/ 下,同时将现有的 PyTorch 后端保留在 src/timesfm3/torch/。MLX 版本暴露相同的 timesfm3_forecaster 接口,加载相同的检查点,并且与 PyTorch 输出的最大绝对误差约为 1e-6。独立实现可在 https://github.com/rachittshah/mlx-tsfm 获取。
快速设置:
python -m pip install -U mlx
git clone https://github.com/rachittshah/mlx-tsfm.git
cd mlx-tsfm
pytest tests/test_timesfm.py
在使用包含 PR #476 的 TimesFM 仓库时,从 timesfm3.mlx.timesfm3_forecaster 而不是 Torch 模块导入 forecaster,并使用相同的初始化和预测调用。据报道,在 M4 Max 上,330M 模型单次预测约需 20 毫秒,批处理时约可达 475 次预测/秒。