TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google Research for time-series forecasting.
Python
fix(torch): apply quantile flip to autoregressive outputs in force_flip_invariance
winklemadaf8f5952e291
diff --git a/src/timesfm/timesfm_2p5/timesfm_2p5_torch.py b/src/timesfm/timesfm_2p5/timesfm_2p5_torch.pyindex 2930c7d..2b801f0 100644--- a/src/timesfm/timesfm_2p5/timesfm_2p5_torch.py+++ b/src/timesfm/timesfm_2p5/timesfm_2p5_torch.py@@ -461,7 +461,10 @@ class TimesFM_2p5_200M_torch(flipped_pf_outputs = flip_quantile_fn(flipped_pf_outputs)to_cat = [flipped_pf_outputs[:, -1, ...]]if flipped_ar_outputs is not None:- to_cat.append(flipped_ar_outputs.reshape(batch_size, -1, self.model.q))+ flipped_ar_outputs = flip_quantile_fn(+ flipped_ar_outputs.reshape(batch_size, -1, self.model.q)+ )+ to_cat.append(flipped_ar_outputs)flipped_full_forecast = torch.cat(to_cat, dim=1)quantile_spreads = (quantile_spreads - flipped_quantile_spreads) / 2pf_outputs = (pf_outputs - flipped_pf_outputs) / 2diff --git a/tests/test_force_flip_invariance.py b/tests/test_force_flip_invariance.pynew file mode 100644index 0000000..00806af--- /dev/null+++ b/tests/test_force_flip_invariance.py@@ -0,0 +1,73 @@+# Copyright 2025 Google LLC+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++"""Tests for force_flip_invariance in the PyTorch TimesFM 2.5 model."""++import numpy as np+import torch++from timesfm import configs+from timesfm.timesfm_2p5.timesfm_2p5_torch import TimesFM_2p5_200M_torch+++def _flip_quantile(x):+ # Keep the mean channel (index 0), reverse the nine quantile channels.+ return torch.cat([x[..., :1], torch.flip(x[..., 1:], dims=(-1,))], dim=-1)+++class TestForceFlipInvariance:+ """force_flip_invariance must hold over the whole horizon, not just prefill."""++ def test_flip_invariance_holds_in_autoregressive_region(self):+ # With force_flip_invariance the model is symmetrized so that+ # forecast(-x) == -flip_quantile(forecast(x)) at every horizon step. The+ # property is independent of the weights, so a random init is enough to+ # check it. A missing quantile flip on the autoregressive branch breaks it+ # past the first output patch (128 steps).+ torch.manual_seed(0)+ tfm = TimesFM_2p5_200M_torch(torch_compile=False)+ tfm.model.eval()++ # max_horizon 256 > output patch length 128 so the AR branch is exercised.+ fc = configs.ForecastConfig(+ max_context=256,+ max_horizon=256,+ force_flip_invariance=True,+ use_continuous_quantile_head=False,+ infer_is_positive=False, # skip the nonneg clamp so the identity is exact+ normalize_inputs=False,+ fix_quantile_crossing=False,+ return_backcast=False,+ )+ tfm.compile(fc)++ rng = np.random.RandomState(0)+ context = rng.standard_normal((1, 256)).astype(np.float32) # mixed sign+ masks = np.zeros((1, 256), dtype=bool)++ with torch.no_grad():+ _, forecast_pos = tfm.compiled_decode(256, context, masks)+ _, forecast_neg = tfm.compiled_decode(256, -context, masks)++ forecast_pos = torch.from_numpy(np.asarray(forecast_pos))+ forecast_neg = torch.from_numpy(np.asarray(forecast_neg))+ expected = -_flip_quantile(forecast_pos)++ # The prefill region (first 128 steps) matched even before the fix; the AR+ # region (128:256) is where the missing flip showed up.+ ar_region = slice(128, 256)+ torch.testing.assert_close(+ forecast_neg[:, ar_region, :], expected[:, ar_region, :],+ atol=1e-4, rtol=1e-4,+ )