chronos-forecasting 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chronos/__init__.py +24 -0
- chronos/base.py +162 -0
- chronos/chronos.py +585 -0
- chronos/chronos_bolt.py +587 -0
- chronos/utils.py +20 -0
- chronos_forecasting-1.3.0.dist-info/METADATA +437 -0
- chronos_forecasting-1.3.0.dist-info/RECORD +10 -0
- chronos_forecasting-1.3.0.dist-info/WHEEL +4 -0
- chronos_forecasting-1.3.0.dist-info/licenses/LICENSE +175 -0
- chronos_forecasting-1.3.0.dist-info/licenses/NOTICE +1 -0
chronos/__init__.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
from .base import BaseChronosPipeline, ForecastType
|
|
5
|
+
from .chronos import (
|
|
6
|
+
ChronosConfig,
|
|
7
|
+
ChronosModel,
|
|
8
|
+
ChronosPipeline,
|
|
9
|
+
ChronosTokenizer,
|
|
10
|
+
MeanScaleUniformBins,
|
|
11
|
+
)
|
|
12
|
+
from .chronos_bolt import ChronosBoltConfig, ChronosBoltPipeline
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"BaseChronosPipeline",
|
|
16
|
+
"ForecastType",
|
|
17
|
+
"ChronosConfig",
|
|
18
|
+
"ChronosModel",
|
|
19
|
+
"ChronosPipeline",
|
|
20
|
+
"ChronosTokenizer",
|
|
21
|
+
"MeanScaleUniformBins",
|
|
22
|
+
"ChronosBoltConfig",
|
|
23
|
+
"ChronosBoltPipeline",
|
|
24
|
+
]
|
chronos/base.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
# Authors: Caner Turkmen <atturkm@amazon.com>, Abdul Fatir Ansari <ansarnd@amazon.com>, Lorenzo Stella <stellalo@amazon.com>
|
|
5
|
+
# Original source:
|
|
6
|
+
# https://github.com/autogluon/autogluon/blob/f57beb26cb769c6e0d484a6af2b89eab8aee73a8/timeseries/src/autogluon/timeseries/models/chronos/pipeline/base.py
|
|
7
|
+
|
|
8
|
+
from enum import Enum
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from transformers import PreTrainedModel
|
|
16
|
+
|
|
17
|
+
from .utils import left_pad_and_stack_1D
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ForecastType(Enum):
|
|
21
|
+
SAMPLES = "samples"
|
|
22
|
+
QUANTILES = "quantiles"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class PipelineRegistry(type):
|
|
26
|
+
REGISTRY: Dict[str, "PipelineRegistry"] = {}
|
|
27
|
+
|
|
28
|
+
def __new__(cls, name, bases, attrs):
|
|
29
|
+
"""See, https://github.com/faif/python-patterns."""
|
|
30
|
+
new_cls = type.__new__(cls, name, bases, attrs)
|
|
31
|
+
if name is not None:
|
|
32
|
+
cls.REGISTRY[name] = new_cls
|
|
33
|
+
|
|
34
|
+
return new_cls
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class BaseChronosPipeline(metaclass=PipelineRegistry):
|
|
38
|
+
forecast_type: ForecastType
|
|
39
|
+
dtypes = {"bfloat16": torch.bfloat16, "float32": torch.float32}
|
|
40
|
+
|
|
41
|
+
def __init__(self, inner_model: "PreTrainedModel"):
|
|
42
|
+
"""
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
inner_model : PreTrainedModel
|
|
46
|
+
A hugging-face transformers PreTrainedModel, e.g., T5ForConditionalGeneration
|
|
47
|
+
"""
|
|
48
|
+
# for easy access to the inner HF-style model
|
|
49
|
+
self.inner_model = inner_model
|
|
50
|
+
|
|
51
|
+
def _prepare_and_validate_context(
|
|
52
|
+
self, context: Union[torch.Tensor, List[torch.Tensor]]
|
|
53
|
+
):
|
|
54
|
+
if isinstance(context, list):
|
|
55
|
+
context = left_pad_and_stack_1D(context)
|
|
56
|
+
assert isinstance(context, torch.Tensor)
|
|
57
|
+
if context.ndim == 1:
|
|
58
|
+
context = context.unsqueeze(0)
|
|
59
|
+
assert context.ndim == 2
|
|
60
|
+
|
|
61
|
+
return context
|
|
62
|
+
|
|
63
|
+
def predict(
|
|
64
|
+
self,
|
|
65
|
+
context: Union[torch.Tensor, List[torch.Tensor]],
|
|
66
|
+
prediction_length: Optional[int] = None,
|
|
67
|
+
**kwargs,
|
|
68
|
+
):
|
|
69
|
+
"""
|
|
70
|
+
Get forecasts for the given time series.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
context
|
|
75
|
+
Input series. This is either a 1D tensor, or a list
|
|
76
|
+
of 1D tensors, or a 2D tensor whose first dimension
|
|
77
|
+
is batch. In the latter case, use left-padding with
|
|
78
|
+
``torch.nan`` to align series of different lengths.
|
|
79
|
+
prediction_length
|
|
80
|
+
Time steps to predict. Defaults to a model-dependent
|
|
81
|
+
value if not given.
|
|
82
|
+
|
|
83
|
+
Returns
|
|
84
|
+
-------
|
|
85
|
+
forecasts
|
|
86
|
+
Tensor containing forecasts. The layout and meaning
|
|
87
|
+
of the forecasts values depends on ``self.forecast_type``.
|
|
88
|
+
"""
|
|
89
|
+
raise NotImplementedError()
|
|
90
|
+
|
|
91
|
+
def predict_quantiles(
|
|
92
|
+
self,
|
|
93
|
+
context: Union[torch.Tensor, List[torch.Tensor]],
|
|
94
|
+
prediction_length: Optional[int] = None,
|
|
95
|
+
quantile_levels: List[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
|
|
96
|
+
**kwargs,
|
|
97
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
98
|
+
"""
|
|
99
|
+
Get quantile and mean forecasts for given time series.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
context : Union[torch.Tensor, List[torch.Tensor]]
|
|
104
|
+
Input series. This is either a 1D tensor, or a list
|
|
105
|
+
of 1D tensors, or a 2D tensor whose first dimension
|
|
106
|
+
is batch. In the latter case, use left-padding with
|
|
107
|
+
``torch.nan`` to align series of different lengths.
|
|
108
|
+
prediction_length : Optional[int], optional
|
|
109
|
+
Time steps to predict. Defaults to a model-dependent
|
|
110
|
+
value if not given.
|
|
111
|
+
quantile_levels : List[float], optional
|
|
112
|
+
Quantile levels to compute, by default [0.1, 0.2, ..., 0.9]
|
|
113
|
+
|
|
114
|
+
Returns
|
|
115
|
+
-------
|
|
116
|
+
quantiles
|
|
117
|
+
Tensor containing quantile forecasts. Shape
|
|
118
|
+
(batch_size, prediction_length, num_quantiles)
|
|
119
|
+
mean
|
|
120
|
+
Tensor containing mean (point) forecasts. Shape
|
|
121
|
+
(batch_size, prediction_length)
|
|
122
|
+
"""
|
|
123
|
+
raise NotImplementedError()
|
|
124
|
+
|
|
125
|
+
@classmethod
|
|
126
|
+
def from_pretrained(
|
|
127
|
+
cls,
|
|
128
|
+
pretrained_model_name_or_path: Union[str, Path],
|
|
129
|
+
*model_args,
|
|
130
|
+
**kwargs,
|
|
131
|
+
):
|
|
132
|
+
"""
|
|
133
|
+
Load the model, either from a local path or from the HuggingFace Hub.
|
|
134
|
+
Supports the same arguments as ``AutoConfig`` and ``AutoModel``
|
|
135
|
+
from ``transformers``.
|
|
136
|
+
"""
|
|
137
|
+
from transformers import AutoConfig
|
|
138
|
+
|
|
139
|
+
torch_dtype = kwargs.get("torch_dtype", "auto")
|
|
140
|
+
if torch_dtype != "auto" and isinstance(torch_dtype, str):
|
|
141
|
+
kwargs["torch_dtype"] = cls.dtypes[torch_dtype]
|
|
142
|
+
|
|
143
|
+
config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
|
|
144
|
+
is_valid_config = hasattr(config, "chronos_pipeline_class") or hasattr(
|
|
145
|
+
config, "chronos_config"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
if not is_valid_config:
|
|
149
|
+
raise ValueError("Not a Chronos config file")
|
|
150
|
+
|
|
151
|
+
pipeline_class_name = getattr(
|
|
152
|
+
config, "chronos_pipeline_class", "ChronosPipeline"
|
|
153
|
+
)
|
|
154
|
+
class_ = PipelineRegistry.REGISTRY.get(pipeline_class_name)
|
|
155
|
+
if class_ is None:
|
|
156
|
+
raise ValueError(
|
|
157
|
+
f"Trying to load unknown pipeline class: {pipeline_class_name}"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
return class_.from_pretrained( # type: ignore[attr-defined]
|
|
161
|
+
pretrained_model_name_or_path, *model_args, **kwargs
|
|
162
|
+
)
|