TimesFM 2.5: Time Series Forecasting with Transformers
TimesFM 2.5 is a pretrained time-series foundation model from Google Research. It provides accurate zero-shot forecasts across different domains, forecasting horizons, and temporal granularities.
This notebook demonstrates:
- Zero-shot forecasting on synthetic and real-world time series
- Quantile (probabilistic) predictions with uncertainty estimation
- Attention backend comparison (eager, SDPA, Flash Attention 2/3, Flex Attention)
- Fine-tuning with PEFT (LoRA) on a custom dataset
- Forecasting with exogenous covariates using in-context linear regression (XReg)
Key advantages of using TimesFM 2.5 via Transformers:
- Native support for all attention backends (SDPA, Flash Attention 2, Flex Attention) through the standard
attn_implementationAPI - Standard
from_pretrained/save_pretrainedworkflow - Easy fine-tuning with PEFT adapters (LoRA, QLoRA, etc.)
- Compatible with the broader Transformers ecosystem
Setup
Install the required packages. We need the latest transformers from main for TimesFM 2.5 support.
Using device: cuda
1. Zero-Shot Forecasting
TimesFM 2.5 can forecast time series out-of-the-box without any training. Let's load the pretrained model and run inference on some synthetic signals.
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Loading weights: 0%| | 0/272 [00:00<?, ?it/s]
Model loaded with 231,289,280 parameters Horizon length: 128 Context length: 16384 Quantiles: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
Forecasting synthetic signals
The model accepts a list of 1D tensors as input (variable-length time series) and returns:
mean_predictions: point forecasts of shape(batch_size, horizon_length)full_predictions: quantile forecasts of shape(batch_size, horizon_length, num_quantiles)
Mean predictions shape: torch.Size([3, 128]) Full predictions shape: torch.Size([3, 128, 10])
Forecasting real-world data
Let's try the model on the Electricity Transformer Temperature (ETTh1) dataset, a commonly used benchmark for time series forecasting.
Dataset shape: (17420, 8)
MAE: 1.3000
Batch forecasting multiple columns
TimesFM 2.5 accepts a list of variable-length time series, making it easy to forecast multiple series at once.
2. Attention Backend Comparison
One of the key benefits of using TimesFM 2.5 via Transformers is native support for different attention implementations:
- Eager: Standard PyTorch attention (baseline)
- SDPA: PyTorch's
scaled_dot_product_attention(fused kernels) - Flash Attention 2: Memory-efficient attention via
kernels-community/flash-attn2 - Flash Attention 3: Latest Flash Attention via
kernels-community/flash-attn3 - Flex Attention: PyTorch's
flex_attentionwithtorch.compile(first call incurs a one-time compilation cost)
All backends produce numerically equivalent outputs but differ in speed and memory usage. We benchmark each on a batch of 8 series of length 2048.
Max diff (eager vs sdpa): 5.47e-03 Max diff (eager vs kernels-community/flash-attn2): 5.47e-03 Max diff (eager vs kernels-community/flash-attn3): 5.47e-03 Max diff (eager vs flex_attention): 5.47e-03
3. Fine-Tuning with PEFT (LoRA)
TimesFM 2.5 can be fine-tuned on custom datasets using parameter-efficient methods like LoRA. This is especially useful when you have domain-specific data and want to adapt the model without updating all 200M parameters.
We'll fine-tune on a retail demand forecasting dataset (weekly store sales), the same dataset used in the Chronos-2 quickstart notebook. The goal is to forecast the next 13 weeks of sales for each store.
Train shape: (133800, 8), Test shape: (14495, 7) Number of stores: 1115 Data points per store: 120
Valid stores: 1115 (need >= 77 data points) Train samples: 5000, Val windows: 1115
Loading weights: 0%| | 0/272 [00:00<?, ?it/s]
trainable params: 1,382,912 || all params: 232,672,192 || trainable%: 0.5944
Epoch 1/20 (157 steps) - Train loss: 1.8673, Val loss: 0.3481 Epoch 2/20 (157 steps) - Train loss: 1.2732, Val loss: 0.3238 Epoch 3/20 (157 steps) - Train loss: 1.0454, Val loss: 0.2815 Epoch 4/20 (157 steps) - Train loss: 0.8334, Val loss: 0.2682 Epoch 5/20 (157 steps) - Train loss: 0.6838, Val loss: 0.2541 Epoch 6/20 (157 steps) - Train loss: 0.5916, Val loss: 0.2480 Epoch 7/20 (157 steps) - Train loss: 0.5343, Val loss: 0.2394 Epoch 8/20 (157 steps) - Train loss: 0.4679, Val loss: 0.2379 Epoch 9/20 (157 steps) - Train loss: 0.4205, Val loss: 0.2347 Epoch 10/20 (157 steps) - Train loss: 0.3841, Val loss: 0.2286 Epoch 11/20 (157 steps) - Train loss: 0.3524, Val loss: 0.2257 Epoch 12/20 (157 steps) - Train loss: 0.3329, Val loss: 0.2234 Epoch 13/20 (157 steps) - Train loss: 0.3120, Val loss: 0.2201 Epoch 14/20 (157 steps) - Train loss: 0.2991, Val loss: 0.2194 Epoch 15/20 (157 steps) - Train loss: 0.2888, Val loss: 0.2185 Epoch 16/20 (157 steps) - Train loss: 0.2826, Val loss: 0.2180 Epoch 17/20 (157 steps) - Train loss: 0.2749, Val loss: 0.2178 Epoch 18/20 (157 steps) - Train loss: 0.2721, Val loss: 0.2170 Epoch 19/20 (157 steps) - Train loss: 0.2701, Val loss: 0.2171 Epoch 20/20 (157 steps) - Train loss: 0.2695, Val loss: 0.2169
Compare zero-shot vs fine-tuned
Let's compare the zero-shot model with the LoRA fine-tuned model on the held-out test set for a few stores.
Loading weights: 0%| | 0/272 [00:00<?, ?it/s]
Average zero-shot MAE: 5608.10 Average LoRA MAE: 3624.42 Improvement: 35.4%
Loading and reusing a saved LoRA adapter
The LoRA adapter weights can be saved and loaded separately from the base model, making them easy to share and version.
Loading weights: 0%| | 0/272 [00:00<?, ?it/s]
Max diff between saved and loaded adapter: 0.00e+00
4. Forecasting with Exogenous Covariates (XReg)
TimesFM 2.5 can incorporate exogenous covariates (known future variables) to improve forecasts using a post-hoc in-context linear regression approach (xreg_lib):
- Fit a ridge regression on the context:
target ~ covariates - Subtract the regression fit from the context to get residuals
- Feed the residuals to TimesFM for forecasting
- Combine:
final_forecast = regression_horizon_prediction + TimesFM_residual_forecast
This works best when covariates have an approximately linear relationship with the target (e.g., price → sales, marketing spend → demand). For complex nonlinear relationships (e.g., temperature → electricity load), neural approaches like Toto that pass covariates through the network are more appropriate.
We demonstrate this using a synthetic retail demand dataset with a known price effect, adapted from the TimesFM covariates PR.
Generated 4 series, each 640 points Context: 512, Horizon: 128 Price sensitivity per series: [8, 10, 12, 14]
Loading weights: 0%| | 0/272 [00:00<?, ?it/s]
Average zero-shot MAE: 14.78 Average XReg MAE: 4.32 Improvement: 70.8%
Summary
In this notebook, we demonstrated the key capabilities of TimesFM 2.5 in Transformers:
- Zero-shot forecasting: The model provides accurate forecasts on unseen data without any training, including quantile uncertainty estimates.
- Batch forecasting: Multiple time series of different lengths can be forecast in a single call.
- Attention backends: Native support for eager, SDPA, Flash Attention 2/3, and Flex Attention, with numerically equivalent outputs and different performance characteristics.
- PEFT fine-tuning: LoRA adapters can be applied to adapt the model to domain-specific data (retail sales) with minimal trainable parameters.
- Exogenous covariates (XReg): Known future variables (e.g., price) can be incorporated via in-context ridge regression to improve forecast accuracy without retraining. The linear regression can be extended with polynomial features (e.g.,
price²,price³) or interaction terms to capture non-linear covariate effects while keeping the closed-form solve. For highly non-linear relationships (e.g., temperature → electricity load), neural approaches like Toto that pass covariates through the network are more appropriate.
For more information, see: