vilhess commited on
Commit
b842bff
·
verified ·
1 Parent(s): 20c77f1

Push model using huggingface_hub.

Browse files
Files changed (3) hide show
  1. README.md +6 -247
  2. config.json +1 -1
  3. model.safetensors +2 -2
README.md CHANGED
@@ -1,251 +1,10 @@
1
  ---
2
  tags:
3
- - forecasting
4
- - time-series
5
- - zero-shot
6
- - foundation-models
7
- datasets:
8
- - Salesforce/GiftEval
9
- - autogluon/chronos_datasets
10
- - Datadog/BOOM
11
- - Salesforce/GiftEvalPretrain
12
  ---
13
 
14
- Code : https://github.com/vilhess/PatchFM
15
-
16
- # A Foundation Model for Univariate Time Series Forecasting
17
-
18
- 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.
19
-
20
- Our model (with leakage) is deployed on the [TS-Arena benchmark](https://ts-arena.live) and achieves competitive performance against state-of-the-art methods under the name `LITIS/PatchFM-Large`.
21
-
22
- ## Results on the FEV benchmark:
23
-
24
- **Our model (without leakage) is competitive with the state-of-the-art notably compared to [MOIRAI2.0](https://arxiv.org/pdf/2511.11698)**
25
-
26
- | Model Name | Win Rate | Skill Score |
27
- |----------------------|----------|-------------|
28
- | (🥇) Chronos-2 | 0.9008 | 0.4728 |
29
- | (🥈) TiRex | 0.8092 | 0.4268 |
30
- | (🥉) TimesFM-2.5 | 0.8085 | 0.4668 |
31
- | (4) Toto-1.0 | 0.7069 | 0.4110 |
32
- | (5) **PatchFM (us)** | 0.6684 | 0.3984 |
33
- | (6) Moirai-2.0 | 0.6546 | 0.4026 |
34
- | (7) Chronos-Bolt | 0.6277 | 0.3889 |
35
- | (8) Sundial-Base | 0.4446 | 0.3387 |
36
- | (9) CatBoost (Recursive)| 0.3362 | 0.2301 |
37
- | (10) LightGBM (Recursive)| 0.3123 | 0.2168 |
38
- | (11) AutoTheta | 0.2938 | 0.0546 |
39
- | (12) Seasonal Naive | 0.2058 | 0.0000 |
40
- | (13) Naive | 0.1404 | -0.4540 |
41
- | (14) Drift | 0.0915 | -0.4578 |
42
-
43
-
44
- ## Highlights
45
- - Next-patch prediction objective (autoregressive, causal)
46
- - Patch-based representation of time series (tokens ↔ patches)
47
- - Causal masking self-attention with RoPE (relative positions)
48
- - Causal RevIN with $\sinh^{-1}$ transform for normalization
49
- - SwiGLU feed-forward networks
50
- - Autoregressive multi-quantile decoding [MOIRAI2.0](https://arxiv.org/pdf/2511.11698)
51
- - KV-cache for efficient long-horizon inference
52
- - flips equivariance during inference (optional) [Reverso](https://arxiv.org/pdf/2602.17634v1)
53
-
54
- ## Quick Start
55
-
56
- ### from source code
57
-
58
- > **⚠️ Important:** Two pretrained versions of PatchFM are available:
59
-
60
- >
61
-
62
- > - **`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.
63
-
64
- > - **`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.
65
-
66
- 1. Clone the repository and install dependencies
67
- ```bash
68
- git clone https://github.com/vilhess/PatchFM
69
- cd PatchFM
70
- pip install -r requirements.txt
71
- ```
72
- 2. Run inference with a pretrained model from Huggingface Hub
73
-
74
- ```python
75
- import torch
76
-
77
- from configs import PatchFMConfig
78
- from model import Forecaster
79
-
80
- # --- Instantiate model ---
81
- config = PatchFMConfig(load_from_hub=True, full_leakage=False) # set full_leakage=True to load the version trained with data leakage
82
- model = Forecaster(config)
83
-
84
- # --- Inference ---
85
- forecast_horizon = 64
86
- seq = torch.randn(1, 1024) # (batch, time)
87
- 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)
88
- ```
89
-
90
-
91
- ### from pip package
92
- 1. Install the package from PyPI
93
- ```bash
94
- pip install patchfm
95
- ```
96
- 2. Run inference with a pretrained model from Huggingface Hub
97
-
98
- ```python
99
- import torch
100
-
101
- from patchfm import Forecaster, PatchFMConfig
102
-
103
- config = PatchFMConfig(full_leakage=False)
104
- # same as above
105
- 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)
106
- ```
107
-
108
- We provide an extended quick start example in [notebooks/tutorial.ipynb](./notebooks/tutorial.ipynb).
109
- If you dont have suitable hardware you can run the the extended quick start example example also in Google Colab:
110
-
111
- <a target="_blank" href="https://colab.research.google.com/drive/17sdf-7luCkv5TaeLj3Z6kIaTDkwkz3VR?usp=share_link">
112
- <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open Quick Start In Colab"/>
113
- </a>
114
-
115
- ## Method (TL;DR)
116
- - Patching: Split a context signal of length $w$ into $P_{num} = w / P_{len}$ patches of length $P_{len}$.
117
- - Causal RevIN: Normalize input signal and denormalize outputs to the original scale without statistics leakage.
118
- - Architecture: Input residual MLP → stacked Transformer blocks (MHA + SwiGLU FFN, pre-norm, residual) → $|\mathcal{Q}|$ output heads mapping back to patch space.
119
- - Positional encoding: Rotary Position Embeddings (RoPE) applied to queries/keys.
120
- - Training: Multi-quantile (pinball) loss across positions, elements, and quantiles $\mathcal{Q}$.
121
- - Inference: Predict next patch; roll out autoregressively for long horizons.
122
- - KV-cache: during inference, cache keys/values to avoid redundant computations.
123
- - Flip-equivariance: during inference, flip input sequence and average predictions to improve robustness (at cost of doubling batch size).
124
-
125
- ## Problem Formulation
126
- 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.
127
-
128
- ## Loss: Multi-Quantile (Pinball)
129
- For residual $u = x - \hat{x}^{(q)}$:
130
- $$\rho_q(u) = \begin{cases} q\,u, & u \ge 0,\\ (q-1)\,u, & u < 0. \end{cases}$$
131
- Aggregate over positions, patch elements, and quantiles.
132
-
133
- ## Architecture
134
- - Input MLP: $\mathbb{R}^{P_{len}} \to \mathbb{R}^{dim}$ residual 2-layer MLP (ReLU)
135
- - Multi-Head Attention: causal mask, RoPE; queries/keys/values per head
136
- - FFN: SwiGLU (SiLU-gated), pre-norm + residual
137
- - Output heads: |Q| linear maps $\mathbb{R}^{dim} \to \mathbb{R}^{P_{len}}$ (one per quantile)
138
-
139
- ### Model Details
140
- - Patch size: 32
141
- - Max context: 32 patches (1024 steps)
142
- - Forecast horizon: 32 steps per forward pass
143
- - Quantiles $\mathcal{Q}$: {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}
144
- - Layers: 6
145
- - Attention heads: 32 (head dim 64)
146
- - Model dim: 2048
147
- - Parameters: ~300M
148
-
149
- ## Inference
150
- - Single step: predict next patch ($P_{len}$ values)
151
- - Long-horizon: append prediction to context and repeat (optionally drop oldest patch to keep window fixed)
152
- - 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):
153
- $$y = \frac{1}{2} \left( f(x) - f(-x) \right)$$
154
-
155
- - ### Autoregressive Inference with Quantile Forecasting ([Moirai 2.0](https://arxiv.org/pdf/2511.11698v1))
156
- 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.
157
-
158
- 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.
159
-
160
- 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.
161
-
162
- 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.
163
-
164
- <div style="display: flex; gap: 10px; align-items: flex-start;">
165
- <div>
166
- <img src="assets/classic_forecast_animation.gif" alt="Classic Autoregressive Inference" width="450">
167
- <p style="text-align:center;">Classic Autoregressive Inference</p>
168
- </div>
169
- <div>
170
- <img src="assets/quantile_forecast_animation.gif" alt="Autoregressive Multi-Quantile Decoding" width="450">
171
- <p style="text-align:center;">Autoregressive Multi-Quantile Decoding</p>
172
- </div>
173
- </div>
174
-
175
- The algorithm proceeds as follows:
176
-
177
- 1. **Initialization**
178
- Start with the initial context window of observed data
179
- **Shape:** `(BS × L)`
180
- - `BS`: batch size
181
- - `L`: context length
182
- - `P`: patch size
183
- - `Q`: number of quantiles
184
- - `H`: forecast horizon
185
- - `i=1`: current algorithm step
186
-
187
- 2. **First Quantile Prediction (Forward Pass)**
188
- Predict the quantiles for the next patch using the current context.
189
- **Output shape:** `(BS × P × Q)`
190
-
191
- 3. **Context Duplication**
192
- For each predicted quantile, create a separate context by appending the corresponding predicted patch to the current context.
193
- This increases the number of contexts by a factor of `Q` at each step.
194
- **New context shape:** `(BS × Q × i(L + P))`
195
-
196
- 4. **Next Forward Pass**
197
- For each duplicated context, predict the quantiles of the next patch.
198
- **Output shape:** `(BS × Q × P × Q)`
199
-
200
- 5. **Quantile Collapse**
201
- - Permute and reshape the predictions to aggregate all possible quantile paths:
202
- **Intermediate shape:** `(BS × P × Q²)`
203
- - Compute the quantiles across the `Q²` predictions to obtain the final quantile estimates for the next patch.
204
- **Final shape:** `(BS × P × Q)`
205
- - Increment the step counter `i ← i + 1`.
206
-
207
- 6. **Iteration**
208
- Repeat Steps 3–5 until the forecast horizon `H` is reached, i.e., until the total number of predicted time steps satisfies
209
- `i × P ≥ H`.
210
-
211
- 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.
212
-
213
- ⚠️ **Warning**
214
- 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.
215
-
216
- 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.
217
-
218
- ## Datasets
219
- - 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
220
- domains and various frequencies. After preprocessing, this yields approximately 600K univariate series.
221
- - 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.
222
- - 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).
223
- - 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.
224
-
225
- ## Repository Layout
226
-
227
- - `model/training/` — main PatchFM model class
228
-
229
- - `modules.py` - core modules (Residual Layers, MHA, SwiGLU, RoPE, Transformer Encoder, ...)
230
- - `revin.py` — causal RevIN
231
- - `loss.py` — multi-quantile (pinball) loss
232
- - `trainer.py` — PyTorch Lightning trainer class
233
-
234
- - `model/inference/` — main PatchFM model class for inference
235
- - `modules.py` — core modules with caching support
236
- - `forecaster.py` — Forecasting model and rollout logic
237
-
238
- - `dataset/` — data loading and preprocessing
239
- - `artificial.py` — synthetic dataset : artificial signals + TSMixup + KernelSynth
240
- - `gift.py` — GIFT-Eval pretraining dataset loading and preprocessing
241
- - `get_data.py` — utility to fetch and preprocess datasets
242
- - `chronosdata.py` — loading of the synthetic datasets generated with Chronos (TSMixup and KernelSynth) with download functions integrated
243
- - `boom.py` — loading of the BOOM dataset with download functions integrated
244
-
245
- - `configs/` — model and training configurations
246
- - `notebooks/inference` — how to load a trained model and generate forecasts
247
- - `training.py` — training script using PyTorch Lightning
248
-
249
- ## Acknowledgements
250
- We thank the authors of the following repositories for inspiration and code snippets:
251
- - [TiRex](https://github.com/NX-AI/tirex)
 
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]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
config.json CHANGED
@@ -1,5 +1,5 @@
1
  {
2
- "ckpt_path": "ckpts/pretrain_boom.ckpt",
3
  "compile": true,
4
  "d_model": 2048,
5
  "load_from_hub": false,
 
1
  {
2
+ "ckpt_path": "ckpts/toto.ckpt",
3
  "compile": true,
4
  "d_model": 2048,
5
  "load_from_hub": false,
model.safetensors CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:cf4663d0747faed47a686eaeab63ea87c96fcb1db4e90ae4ce2d290d77a86ced
3
- size 1275010080
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ef55a59121cebf8188f538dc970e668b56881e61321fe0bd297529651b3442b
3
+ size 1380957032