Safetensors
forecasting
time-series
zero-shot
foundation-models
vilhess commited on
Commit
649fdb1
Β·
verified Β·
1 Parent(s): cf688eb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +211 -6
README.md CHANGED
@@ -1,10 +1,215 @@
1
  ---
2
  tags:
3
- - model_hub_mixin
4
- - pytorch_model_hub_mixin
 
 
 
 
 
 
5
  ---
6
 
7
- This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
8
- - Code: [More Information Needed]
9
- - Paper: [More Information Needed]
10
- - Docs: [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  tags:
3
+ - forecasting
4
+ - time-series
5
+ - zero-shot
6
+ - foundation-models
7
+ datasets:
8
+ - thuml/UTSD
9
+ - autogluon/chronos_datasets
10
+ - Salesforce/GiftEvalPretrain
11
  ---
12
 
13
+ Code : https://github.com/vilhess/PatchFM
14
+
15
+ # A tutorial on how to build a Foundation Model for Univariate Time Series Forecasting
16
+
17
+ 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.
18
+
19
+ ## Highlights
20
+ - Next-patch prediction objective (autoregressive, causal)
21
+ - Patch-based representation of time series (tokens ↔ patches)
22
+ - Causal masking self-attention with RoPE (relative positions)
23
+ - RevIN (Reversible Instance Normalization)
24
+ - SwiGLU feed-forward networks
25
+ - Autoregressive multi-quantile decoding [MOIRAI2.0](https://arxiv.org/pdf/2511.11698)
26
+ - KV-cache for efficient long-horizon inference
27
+ - flips equivariance during inference (optional) [Reverso](https://arxiv.org/pdf/2602.17634v1)
28
+
29
+
30
+ ## Quick Start
31
+
32
+ ### from source code
33
+
34
+ 1. Clone the repository and install dependencies
35
+ ```bash
36
+ git clone https://github.com/vilhess/PatchFM
37
+ cd PatchFM
38
+ pip install -r requirements.txt
39
+ ```
40
+ 2. Run inference with a pretrained model from Huggingface Hub
41
+
42
+ ```python
43
+ import torch
44
+
45
+ from configs import PatchFMConfig
46
+ from model import Forecaster
47
+
48
+ # --- Instantiate model ---
49
+ config = PatchFMConfig(load_from_hub=True)
50
+ model = Forecaster(config)
51
+
52
+ # --- Inference ---
53
+ forecast_horizon = 64
54
+ seq = torch.randn(1, 1024) # (batch, time)
55
+ 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)
56
+ ```
57
+
58
+
59
+ ### from pip package
60
+ 1. Install the package from PyPI
61
+ ```bash
62
+ pip install patchfm
63
+ ```
64
+ 2. Run inference with a pretrained model from Huggingface Hub
65
+
66
+ ```python
67
+ import torch
68
+
69
+ from patchfm import Forecaster, PatchFMConfig
70
+
71
+ # same as above
72
+ 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)
73
+ ```
74
+
75
+ We provide an extended quick start example in [notebooks/tutorial.ipynb](./notebooks/tutorial.ipynb).
76
+ If you dont have suitable hardware you can run the the extended quick start example example also in Google Colab:
77
+
78
+ <a target="_blank" href="https://colab.research.google.com/drive/17sdf-7luCkv5TaeLj3Z6kIaTDkwkz3VR?usp=share_link">
79
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open Quick Start In Colab"/>
80
+ </a>
81
+
82
+ ## Method (TL;DR)
83
+ - Patching: Split a context signal of length $w$ into $P_{num} = w / P_{len}$ patches of length $P_{len}$.
84
+ - Causal RevIN: Normalize input signal and denormalize outputs to the original scale without statistics leakage.
85
+ - Architecture: Input residual MLP β†’ stacked Transformer blocks (MHA + SwiGLU FFN, pre-norm, residual) β†’ $|\mathcal{Q}|$ output heads mapping back to patch space.
86
+ - Positional encoding: Rotary Position Embeddings (RoPE) applied to queries/keys.
87
+ - Training: Multi-quantile (pinball) loss across positions, elements, and quantiles $\mathcal{Q}$.
88
+ - Inference: Predict next patch; roll out autoregressively for long horizons.
89
+ - KV-cache: during inference, cache keys/values to avoid redundant computations.
90
+ - Flip-equivariance: during inference, flip input sequence and average predictions to improve robustness (at cost of doubling batch size).
91
+
92
+ ## Problem Formulation
93
+ 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.
94
+
95
+ ## Loss: Multi-Quantile (Pinball)
96
+ For residual $u = x - \hat{x}^{(q)}$:
97
+ $$\rho_q(u) = \begin{cases} q\,u, & u \ge 0,\\ (q-1)\,u, & u < 0. \end{cases}$$
98
+ Aggregate over positions, patch elements, and quantiles.
99
+
100
+ ## Architecture
101
+ - Input MLP: $\mathbb{R}^{P_{len}} \to \mathbb{R}^{dim}$ residual 2-layer MLP (ReLU)
102
+ - Multi-Head Attention: causal mask, RoPE; queries/keys/values per head
103
+ - FFN: SwiGLU (SiLU-gated), pre-norm + residual
104
+ - Output heads: |Q| linear maps $\mathbb{R}^{dim} \to \mathbb{R}^{P_{len}}$ (one per quantile)
105
+
106
+ ### Model Details
107
+ - Patch size: 32
108
+ - Max context: 32 patches (1024 steps)
109
+ - Forecast horizon: 32 steps per forward pass
110
+ - Quantiles $\mathcal{Q}$: {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
111
+ - Layers: 6
112
+ - Attention heads: 64 (head dim 32)
113
+ - Model dim: 2048
114
+ - Parameters: ~300M
115
+
116
+ ## Inference
117
+ - Single step: predict next patch ($P_{len}$ values)
118
+ - Long-horizon: append prediction to context and repeat (optionally drop oldest patch to keep window fixed)
119
+ - Flip-equivariance [Reverso](https://arxiv.org/pdf/2602.17634v1): optionally flip input sequence and average predictions to improve robustness (at cost of doubling batch size):
120
+ $$y = \frac{1}{2} \left( f(x) - f(-x) \right)$$
121
+
122
+ - ### Autoregressive Inference with Quantile Forecasting ([Moirai 2.0](https://arxiv.org/pdf/2511.11698v1))
123
+ 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.
124
+
125
+ 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.
126
+
127
+ 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.
128
+
129
+ An alternative approach is **autoregressive multi-quantile decoding**, as proposed in [Moirai 2.0](https://arxiv.org/pdf/2511.11698v1). 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.
130
+
131
+ <div style="display: flex; gap: 10px; align-items: flex-start;">
132
+ <div>
133
+ <img src="assets/classic_forecast_animation.gif" alt="Classic Autoregressive Inference" width="450">
134
+ <p style="text-align:center;">Classic Autoregressive Inference</p>
135
+ </div>
136
+ <div>
137
+ <img src="assets/quantile_forecast_animation.gif" alt="Autoregressive Multi-Quantile Decoding" width="450">
138
+ <p style="text-align:center;">Autoregressive Multi-Quantile Decoding</p>
139
+ </div>
140
+ </div>
141
+
142
+ The algorithm proceeds as follows:
143
+
144
+ 1. **Initialization**
145
+ Start with the initial context window of observed data
146
+ **Shape:** `(BS Γ— L)`
147
+ - `BS`: batch size
148
+ - `L`: context length
149
+ - `P`: patch size
150
+ - `Q`: number of quantiles
151
+ - `H`: forecast horizon
152
+ - `i=1`: current algorithm step
153
+
154
+ 2. **First Quantile Prediction (Forward Pass)**
155
+ Predict the quantiles for the next patch using the current context.
156
+ **Output shape:** `(BS Γ— P Γ— Q)`
157
+
158
+ 3. **Context Duplication**
159
+ For each predicted quantile, create a separate context by appending the corresponding predicted patch to the current context.
160
+ This increases the number of contexts by a factor of `Q` at each step.
161
+ **New context shape:** `(BS Γ— Q Γ— i(L + P))`
162
+
163
+ 4. **Next Forward Pass**
164
+ For each duplicated context, predict the quantiles of the next patch.
165
+ **Output shape:** `(BS Γ— Q Γ— P Γ— Q)`
166
+
167
+ 5. **Quantile Collapse**
168
+ - Permute and reshape the predictions to aggregate all possible quantile paths:
169
+ **Intermediate shape:** `(BS Γ— P Γ— QΒ²)`
170
+ - Compute the quantiles across the `QΒ²` predictions to obtain the final quantile estimates for the next patch.
171
+ **Final shape:** `(BS Γ— P Γ— Q)`
172
+ - Increment the step counter `i ← i + 1`.
173
+
174
+ 6. **Iteration**
175
+ Repeat Steps 3–5 until the forecast horizon `H` is reached, i.e., until the total number of predicted time steps satisfies
176
+ `i Γ— P β‰₯ H`.
177
+
178
+ 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.
179
+
180
+ ⚠️ **Warning**
181
+ 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.
182
+
183
+ 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.
184
+
185
+ ## Datasets
186
+ - UTSD (Unified Time Series Dataset) [UTSD]: seven domains (Energy, IoT, Nature, Web, Health, Transport, Environment). We work with UTSD-12G (~18M series after preprocessing).
187
+ - 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
188
+ domains and various frequencies. After preprocessing, this yields approximately 600K univariate series.
189
+ - 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.
190
+ - 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).
191
+
192
+ ## Repository Layout
193
+
194
+ - `model/training/` β€” main PatchFM model class
195
+
196
+ - `modules.py` - core modules (Residual Layers, MHA, SwiGLU, RoPE, Transformer Encoder, ...)
197
+ - `revin.py` β€” causal RevIN
198
+ - `loss.py` β€” multi-quantile (pinball) loss
199
+ - `trainer.py` β€” PyTorch Lightning trainer class
200
+
201
+ - `model/inference/` β€” main PatchFM model class for inference
202
+ - `modules.py` β€” core modules with caching support
203
+ - `forecaster.py` β€” Forecasting model and rollout logic
204
+
205
+ - `dataset/` β€” data loading and preprocessing
206
+ - `artificial.py` β€” synthetic dataset : artificial signals + TSMixup + KernelSynth
207
+ - `utsd.py` β€” Unified Time Series Dataset (UTSD) loading and preprocessing
208
+ - `gift.py` β€” GIFT-Eval pretraining dataset loading and preprocessing
209
+ - `get_data.py` β€” utility to fetch and preprocess datasets
210
+ - `chronosdata.py` β€” loading of the synthetic datasets generated with Chronos (TSMixup and KernelSynth) with download functions integrated
211
+ - `generate_data.py` β€” utility to generate and save the KernelSynth dataset (long to generate)
212
+
213
+ - `configs/` β€” model and training configurations
214
+ - `notebooks/inference` β€” how to load a trained model and generate forecasts
215
+ - `training.py` β€” training script using PyTorch Lightning