Code : https://github.com/vilhess/PatchFM
A Foundation Model for Univariate Time Series Forecasting
A concise, reproducible recipe for training a transformer-based, patch-to-patch forecasting model for univariate time series. The approach mirrors Large Language Model (LLM) practices (next-token β next-patch) while remaining lightweight compared to a classic LLM and practical.
Our model (with leakage) is deployed on the TS-Arena benchmark and achieves competitive performance against state-of-the-art methods under the name LITIS/PatchFM-Large.
Results on the FEV benchmark:
Our model (without leakage) is competitive with the state-of-the-art notably compared to MOIRAI2.0
| Model Name | Win Rate | Skill Score |
|---|---|---|
| (π₯) Chronos-2 | 0.9008 | 0.4728 |
| (π₯) TiRex | 0.8092 | 0.4268 |
| (π₯) TimesFM-2.5 | 0.8085 | 0.4668 |
| (4) Toto-1.0 | 0.7069 | 0.4110 |
| (5) PatchFM (us) | 0.6684 | 0.3984 |
| (6) Moirai-2.0 | 0.6546 | 0.4026 |
| (7) Chronos-Bolt | 0.6277 | 0.3889 |
| (8) Sundial-Base | 0.4446 | 0.3387 |
| (9) CatBoost (Recursive) | 0.3362 | 0.2301 |
| (10) LightGBM (Recursive) | 0.3123 | 0.2168 |
| (11) AutoTheta | 0.2938 | 0.0546 |
| (12) Seasonal Naive | 0.2058 | 0.0000 |
| (13) Naive | 0.1404 | -0.4540 |
| (14) Drift | 0.0915 | -0.4578 |
Highlights
- Next-patch prediction objective (autoregressive, causal)
- Patch-based representation of time series (tokens β patches)
- Causal masking self-attention with RoPE (relative positions)
- Causal RevIN with $\sinh^{-1}$ transform for normalization
- SwiGLU feed-forward networks
- Autoregressive multi-quantile decoding MOIRAI2.0
- KV-cache for efficient long-horizon inference
- flips equivariance during inference (optional) Reverso
Quick Start
from source code
β οΈ Important: Two pretrained versions of PatchFM are available:
PatchFM-Leakage: trained on the GIFT-Eval datasets. This version may provide higher benchmark performance but includes training data that overlaps with the commonly used evaluation datasets.
PatchFM(recommended for fair evaluation): trained without any commonly used benchmark (except BOOM), preventing potential data leakage and ensuring a more reliable assessment of generalization performance.
- Clone the repository and install dependencies
git clone https://github.com/vilhess/PatchFM
cd PatchFM
pip install -r requirements.txt
- Run inference with a pretrained model from Huggingface Hub
import torch
from configs import PatchFMConfig
from model import Forecaster
# --- Instantiate model ---
config = PatchFMConfig(load_from_hub=True, full_leakage=False) # set full_leakage=True to load the version trained with data leakage
model = Forecaster(config)
# --- Inference ---
forecast_horizon = 64
seq = torch.randn(1, 1024) # (batch, time)
pred_median, pred_quantiles = model(seq, forecast_horizon=forecast_horizon, quantiles=[0.1, 0.5, 0.9], flip_equivariance=True) # (batch, time), (batch, time, quantiles)
from pip package
- Install the package from PyPI
pip install patchfm
- Run inference with a pretrained model from Huggingface Hub
import torch
from patchfm import Forecaster, PatchFMConfig
config = PatchFMConfig(full_leakage=False)
# same as above
pred_median, pred_quantiles = model(seq, forecast_horizon=forecast_horizon, quantiles=[0.1, 0.5, 0.9], flip_equivariance=True) # (batch, time), (batch, time, quantiles)
We provide an extended quick start example in notebooks/tutorial.ipynb. If you dont have suitable hardware you can run the the extended quick start example example also in Google Colab:
Method (TL;DR)
- Patching: Split a context signal of length $w$ into $P_{num} = w / P_{len}$ patches of length $P_{len}$.
- Causal RevIN: Normalize input signal and denormalize outputs to the original scale without statistics leakage.
- Architecture: Input residual MLP β stacked Transformer blocks (MHA + SwiGLU FFN, pre-norm, residual) β $|\mathcal{Q}|$ output heads mapping back to patch space.
- Positional encoding: Rotary Position Embeddings (RoPE) applied to queries/keys.
- Training: Multi-quantile (pinball) loss across positions, elements, and quantiles $\mathcal{Q}$.
- Inference: Predict next patch; roll out autoregressively for long horizons.
- KV-cache: during inference, cache keys/values to avoid redundant computations.
- Flip-equivariance: during inference, flip input sequence and average predictions to improve robustness (at cost of doubling batch size).
Problem Formulation
Given context patches $x_{p_1}, \ldots, x_{p_n}$, predict the next patch $x_{p_{i+1}}$ for each position $i$ using only past patches (causality). The model outputs quantiles ${\hat{x}{p{i+1}}^{(q)}: q \in \mathcal{Q}}$ with median (q=0.5) as the point forecast.
Loss: Multi-Quantile (Pinball)
For residual $u = x - \hat{x}^{(q)}$: Aggregate over positions, patch elements, and quantiles.
Architecture
- Input MLP: $\mathbb{R}^{P_{len}} \to \mathbb{R}^{dim}$ residual 2-layer MLP (ReLU)
- Multi-Head Attention: causal mask, RoPE; queries/keys/values per head
- FFN: SwiGLU (SiLU-gated), pre-norm + residual
- Output heads: |Q| linear maps $\mathbb{R}^{dim} \to \mathbb{R}^{P_{len}}$ (one per quantile)
Model Details
- Patch size: 32
- Max context: 32 patches (1024 steps)
- Forecast horizon: 32 steps per forward pass
- Quantiles $\mathcal{Q}$: {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
- Layers: 6
- Attention heads: 32 (head dim 64)
- Model dim: 2048
- Parameters: ~300M
Inference
Single step: predict next patch ($P_{len}$ values)
Long-horizon: append prediction to context and repeat (optionally drop oldest patch to keep window fixed)
Flip-equivariance Reverso: optionally flip input sequence and average predictions to improve robustness (at cost of doubling batch size):
Autoregressive Inference with Quantile Forecasting (Moirai 2.0)
During autoregressive inference, the model generates forecasted values patch by patch. At each time step, the predicted patch is fed back into the model as input for the next step. This iterative process continues until the desired forecast horizon is reached.
When performing quantile forecasting, the situation becomes more complex. Instead of producing a single patch per step, the model outputs multiple patches corresponding to different quantiles (e.g., 0.1, 0.5, 0.9). Since the model expects a single patch for the next time step, it is not straightforward to feed all quantile predictions back into the model simultaneously.
A common workaround is to feed only the median prediction (the 0.5 quantile) back into the model at each step. While this approach preserves the autoregressive structure, it discards the uncertainty information captured by the other quantiles.
An alternative approach is autoregressive multi-quantile decoding, as proposed in Moirai 2.0. This method enables consistent autoregressive generation while preserving the full predictive distribution across quantiles. However, it is computationally more expensive than the median-only approach as it requires duplicating the context for each quantile.
Classic Autoregressive Inference
Autoregressive Multi-Quantile Decoding
The algorithm proceeds as follows:
Initialization
Start with the initial context window of observed data
Shape:(BS Γ L)BS: batch sizeL: context lengthP: patch sizeQ: number of quantilesH: forecast horizoni=1: current algorithm step
First Quantile Prediction (Forward Pass)
Predict the quantiles for the next patch using the current context.
Output shape:(BS Γ P Γ Q)Context Duplication
For each predicted quantile, create a separate context by appending the corresponding predicted patch to the current context.
This increases the number of contexts by a factor ofQat each step.
New context shape:(BS Γ Q Γ i(L + P))Next Forward Pass
For each duplicated context, predict the quantiles of the next patch.
Output shape:(BS Γ Q Γ P Γ Q)Quantile Collapse
- Permute and reshape the predictions to aggregate all possible quantile paths:
Intermediate shape:(BS Γ P Γ QΒ²) - Compute the quantiles across the
QΒ²predictions to obtain the final quantile estimates for the next patch.
Final shape:(BS Γ P Γ Q) - Increment the step counter
i β i + 1.
- Permute and reshape the predictions to aggregate all possible quantile paths:
Iteration
Repeat Steps 3β5 until the forecast horizonHis reached, i.e., until the total number of predicted time steps satisfiesi Γ P β₯ H.
This procedure preserves predictive uncertainty across quantiles while maintaining the autoregressive structure of the model. Although it is computationally more expensive than feeding only the median prediction (0.5 quantile) back into the model, it remains tractable in practice and enables consistent multi-quantile forecasting.
β οΈ Warning
With this strategy, the median prediction (0.5 quantile) does not necessarily match the prediction obtained by autoregressively feeding only the median patch back into the model at each step.
This discrepancy arises because the quantile collapse step aggregates predictions across all possible quantile paths. As a result, the median is computed from the combined multi-path distribution rather than from a single deterministic trajectory, which can lead to different estimates compared to the single-path (median-only) autoregressive approach.
Datasets
- GIFT-Eval pretraining dataset [GIFT]: aligned with the GIFT-Eval dataset but without data leakage issue with the benchmark. The dataset contains approximately 71 univariate and 17 multivariate time series datasets from various domains and various frequencies. After preprocessing, this yields approximately 600K univariate series.
- Chronos synthetic datasets [Chronos]: two large synthetic datasets generated with Chronos, one with TSMixup and one with KernelSynth. Each contains approximately 10 million univariate series and 1 million respectively, each signal of length 1024.
- Artificial: ~1M synthetic series (sinusoidal, linear, polynomial, logarithmic) plus mixtures via TSMixup [Chronos]; Gaussian Process samples via KernelSynth (mixtures of RBF/periodic/linear kernels with swept hyperparameters).
- BOOM: Approximately 5 million time series of length 1024 sampled from the BOOM dataset [BOOM]. It is important to note that the BOOM dataset, which is also used as an evaluation benchmark, is included in the training data of both PatchFM variants (with and without benchmark leakage) to increase data diversity. Consequently, neither version can be fairly evaluated on the BOOM benchmark. Upon request, we can provide an alternative checkpoint trained without any BOOM data, enabling a fair evaluation on this benchmark.
Repository Layout
model/training/β main PatchFM model classmodules.py- core modules (Residual Layers, MHA, SwiGLU, RoPE, Transformer Encoder, ...)revin.pyβ causal RevINloss.pyβ multi-quantile (pinball) losstrainer.pyβ PyTorch Lightning trainer class
model/inference/β main PatchFM model class for inferencemodules.pyβ core modules with caching supportforecaster.pyβ Forecasting model and rollout logic
dataset/β data loading and preprocessingartificial.pyβ synthetic dataset : artificial signals + TSMixup + KernelSynthgift.pyβ GIFT-Eval pretraining dataset loading and preprocessingget_data.pyβ utility to fetch and preprocess datasetschronosdata.pyβ loading of the synthetic datasets generated with Chronos (TSMixup and KernelSynth) with download functions integratedboom.pyβ loading of the BOOM dataset with download functions integrated
configs/β model and training configurationsnotebooks/inferenceβ how to load a trained model and generate forecaststraining.pyβ training script using PyTorch Lightning
Acknowledgements
We thank the authors of the following repositories for inspiration and code snippets:
- Downloads last month
- 324