-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c169de
commit 36b594f
Showing
6 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM python:3.11.6-slim-bookworm | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends git | ||
|
||
WORKDIR /usr/local/app | ||
|
||
ARG CHRONOS_VERSION | ||
|
||
RUN pip install --no-cache-dir --upgrade pip | ||
RUN pip install --no-cache-dir git+https://github.com/amazon-science/chronos-forecasting.git@v$CHRONOS_VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM attilabalint/enfobench-models:base-amazon-chronos-1.1.0 | ||
|
||
WORKDIR /usr/local/app | ||
|
||
COPY ./requirements.txt /usr/local/app/requirements.txt | ||
RUN pip install --no-cache-dir -r /usr/local/app/requirements.txt | ||
|
||
COPY ./models /usr/local/app/models | ||
COPY ./src /usr/local/app/src | ||
|
||
ENV MODEL_NAME="chronos-t5-tiny" | ||
ENV NUM_SAMPLES="20" | ||
|
||
EXPOSE 3000 | ||
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "3000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Chronos repository | ||
enfobench>=0.6.0,<0.7.0 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
import torch | ||
from chronos import ChronosPipeline | ||
|
||
from enfobench import AuthorInfo, ForecasterType, ModelInfo | ||
from enfobench.evaluation.server import server_factory | ||
from enfobench.evaluation.utils import create_forecast_index | ||
|
||
# Check for GPU availability | ||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
root_dir = Path(__file__).parent.parent | ||
|
||
|
||
class AmazonChronosModel: | ||
|
||
def __init__(self, model_name: str, num_samples: int): | ||
self.model_name = model_name | ||
self.num_samples = num_samples | ||
|
||
def info(self) -> ModelInfo: | ||
return ModelInfo( | ||
name=f'Amazon.{".".join(map(str.capitalize, self.model_name.split("-")))}', | ||
authors=[ | ||
AuthorInfo(name="Attila Balint", email="[email protected]"), | ||
], | ||
type=ForecasterType.quantile, | ||
params={ | ||
"num_samples": self.num_samples, | ||
}, | ||
) | ||
|
||
def forecast( | ||
self, | ||
horizon: int, | ||
history: pd.DataFrame, | ||
past_covariates: pd.DataFrame | None = None, | ||
future_covariates: pd.DataFrame | None = None, | ||
metadata: dict | None = None, | ||
level: list[int] | None = None, | ||
**kwargs, | ||
) -> pd.DataFrame: | ||
# Fill missing values | ||
history = history.fillna(history.y.mean()) | ||
|
||
model_dir = root_dir / "models" / self.model_name | ||
if not model_dir.exists(): | ||
raise FileNotFoundError( | ||
f"Model directory for {self.model_name} was not found at {model_dir}, make sure it is downloaded." | ||
) | ||
pipeline = ChronosPipeline.from_pretrained( | ||
model_dir, | ||
device_map=device, | ||
torch_dtype=torch.bfloat16, | ||
) | ||
|
||
# context must be either a 1D tensor, a list of 1D tensors, | ||
# or a left-padded 2D tensor with batch as the first dimension | ||
context = torch.tensor(history.y) | ||
prediction_length = horizon | ||
forecasts = pipeline.predict( | ||
context, | ||
prediction_length, | ||
num_samples=self.num_samples, | ||
limit_prediction_length=False, | ||
) # forecast shape: [num_series, num_samples, prediction_length] | ||
data = {"yhat": forecasts.mean(dim=1)[0]} | ||
# for lvl in level: | ||
# data[f"q{lvl}"] = forecasts.quantile(lvl / 100, dim=1)[0] # TODO: extend to quantiles | ||
|
||
# Postprocess forecast | ||
index = create_forecast_index(history=history, horizon=horizon) | ||
forecast = pd.DataFrame(index=index, data=data) | ||
return forecast | ||
|
||
|
||
model_name = os.getenv("MODEL_NAME") | ||
num_samples = int(os.getenv("NUM_SAMPLES")) | ||
|
||
# Instantiate your model | ||
model = AmazonChronosModel(model_name=model_name, num_samples=num_samples) | ||
|
||
# Create a forecast server by passing in your model | ||
app = server_factory(model) |