Skip to content

TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google Research for time-series forecasting.

Python

Move the TimesFM3 PyTorch backend into a torch/ subpackage

rachittshah9de33f6f487b
diff --git a/src/timesfm3/__init__.py b/src/timesfm3/__init__.py
index da70af8..67390a5 100644
--- a/src/timesfm3/__init__.py
+++ b/src/timesfm3/__init__.py
@@ -12,19 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-"""TimesFM3 PyTorch API."""
+"""TimesFM3 API.
-from .configs import ResidualBlockConfig, StackedTransformersConfig, TransformerConfig
-from .evaluator import TimesFM3Evaluator
-from .model import TimesFM3Torch
-from .timesfm3_forecaster import (
- ForecastOutput,
- ModelConfig,
- TimesFM3Forecaster,
- _ModelConfig,
-)
+The PyTorch backend lives in ``timesfm3.torch`` and the MLX (Apple Silicon) backend in
+``timesfm3.mlx``. For backward compatibility the PyTorch API is also re-exported at the top level
+(``from timesfm3 import TimesFM3Forecaster``); these names are resolved lazily so that importing
+``timesfm3`` — or ``timesfm3.mlx`` — does not require PyTorch to be installed.
+"""
-__all__ = [
+_TORCH_EXPORTS = frozenset({
"ForecastOutput",
"ModelConfig",
"ResidualBlockConfig",
@@ -34,4 +30,14 @@ __all__ = [
"TimesFM3Torch",
"TransformerConfig",
"_ModelConfig",
-]
+})
+
+__all__ = sorted(_TORCH_EXPORTS)
+
+
+def __getattr__(name): # PEP 562: lazily re-export the torch backend at the top level
+ if name in _TORCH_EXPORTS:
+ from . import torch as _torch_backend
+
+ return getattr(_torch_backend, name)
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/src/timesfm3/torch/__init__.py b/src/timesfm3/torch/__init__.py
new file mode 100644
index 0000000..eaf6e6e
--- /dev/null
+++ b/src/timesfm3/torch/__init__.py
@@ -0,0 +1,37 @@
+# Copyright 2026 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.
+
+"""TimesFM3 PyTorch backend."""
+
+from .configs import ResidualBlockConfig, StackedTransformersConfig, TransformerConfig
+from .evaluator import TimesFM3Evaluator
+from .model import TimesFM3Torch
+from .timesfm3_forecaster import (
+ ForecastOutput,
+ ModelConfig,
+ TimesFM3Forecaster,
+ _ModelConfig,
+)
+
+__all__ = [
+ "ForecastOutput",
+ "ModelConfig",
+ "ResidualBlockConfig",
+ "StackedTransformersConfig",
+ "TimesFM3Evaluator",
+ "TimesFM3Forecaster",
+ "TimesFM3Torch",
+ "TransformerConfig",
+ "_ModelConfig",
+]
diff --git a/src/timesfm3/configs.py b/src/timesfm3/torch/configs.py
similarity index 100%
rename from src/timesfm3/configs.py
rename to src/timesfm3/torch/configs.py
diff --git a/src/timesfm3/cpm_revin_refine.py b/src/timesfm3/torch/cpm_revin_refine.py
similarity index 100%
rename from src/timesfm3/cpm_revin_refine.py
rename to src/timesfm3/torch/cpm_revin_refine.py
diff --git a/src/timesfm3/cpm_revin_refine_test.py b/src/timesfm3/torch/cpm_revin_refine_test.py
similarity index 100%
rename from src/timesfm3/cpm_revin_refine_test.py
rename to src/timesfm3/torch/cpm_revin_refine_test.py
diff --git a/src/timesfm3/dense.py b/src/timesfm3/torch/dense.py
similarity index 100%
rename from src/timesfm3/dense.py
rename to src/timesfm3/torch/dense.py
diff --git a/src/timesfm3/evaluator.py b/src/timesfm3/torch/evaluator.py
similarity index 100%
rename from src/timesfm3/evaluator.py
rename to src/timesfm3/torch/evaluator.py
diff --git a/src/timesfm3/model.py b/src/timesfm3/torch/model.py
similarity index 100%
rename from src/timesfm3/model.py
rename to src/timesfm3/torch/model.py
diff --git a/src/timesfm3/model_test.py b/src/timesfm3/torch/model_test.py
similarity index 100%
rename from src/timesfm3/model_test.py
rename to src/timesfm3/torch/model_test.py
diff --git a/src/timesfm3/normalization.py b/src/timesfm3/torch/normalization.py
similarity index 100%
rename from src/timesfm3/normalization.py
rename to src/timesfm3/torch/normalization.py
diff --git a/src/timesfm3/primitives_test.py b/src/timesfm3/torch/primitives_test.py
similarity index 100%
rename from src/timesfm3/primitives_test.py
rename to src/timesfm3/torch/primitives_test.py
diff --git a/src/timesfm3/timesfm3_forecaster.py b/src/timesfm3/torch/timesfm3_forecaster.py
similarity index 100%
rename from src/timesfm3/timesfm3_forecaster.py
rename to src/timesfm3/torch/timesfm3_forecaster.py
diff --git a/src/timesfm3/timesfm3_forecaster_test.py b/src/timesfm3/torch/timesfm3_forecaster_test.py
similarity index 100%
rename from src/timesfm3/timesfm3_forecaster_test.py
rename to src/timesfm3/torch/timesfm3_forecaster_test.py
diff --git a/src/timesfm3/transformations.py b/src/timesfm3/torch/transformations.py
similarity index 100%
rename from src/timesfm3/transformations.py
rename to src/timesfm3/torch/transformations.py
diff --git a/src/timesfm3/transformer.py b/src/timesfm3/torch/transformer.py
similarity index 100%
rename from src/timesfm3/transformer.py
rename to src/timesfm3/torch/transformer.py
diff --git a/src/timesfm3/transformer_test.py b/src/timesfm3/torch/transformer_test.py
similarity index 100%
rename from src/timesfm3/transformer_test.py
rename to src/timesfm3/torch/transformer_test.py
diff --git a/src/timesfm3/util.py b/src/timesfm3/torch/util.py
similarity index 100%
rename from src/timesfm3/util.py
rename to src/timesfm3/torch/util.py