File size: 10,770 Bytes
846afb5 19e62cf 846afb5 19e62cf 846afb5 19e62cf 846afb5 19e62cf 846afb5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
from dataclasses import dataclass, field
from typing import Iterator, Optional
from functools import cached_property
import datasets
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from gluonts.dataset.field_names import FieldName
_CITATION = """\
@article{woo2023pushing,
title={Pushing the Limits of Pre-training for Time Series Forecasting in the CloudOps Domain},
author={Woo, Gerald and Liu, Chenghao and Kumar, Akshat and Sahoo, Doyen},
journal={arXiv preprint arXiv:2310.05063},
year={2023}
}
"""
_CONFIGS = {
"azure_vm_traces_2017": {
"optional_fields": (
FieldName.FEAT_STATIC_CAT,
FieldName.FEAT_STATIC_REAL,
FieldName.PAST_FEAT_DYNAMIC_REAL,
),
"prediction_length": 48,
"freq": "5T",
"stride": 48,
"univariate": True,
"multivariate": False,
"rolling_evaluations": 12,
"test_split_date": pd.Period(
year=2016, month=12, day=13, hour=15, minute=55, freq="5T"
),
"_feat_static_cat_cardinalities": {
"train_test": (
("vm_id", 17568),
("subscription_id", 2713),
("deployment_id", 3255),
("vm_category", 3),
),
"pretrain": (
("vm_id", 177040),
("subscription_id", 5514),
("deployment_id", 15208),
("vm_category", 3),
),
},
"target_dim": 1,
"feat_static_real_dim": 3,
"past_feat_dynamic_real_dim": 2,
},
"borg_cluster_data_2011": {
"optional_fields": (
FieldName.FEAT_STATIC_CAT,
FieldName.PAST_FEAT_DYNAMIC_REAL,
),
"prediction_length": 48,
"freq": "5T",
"stride": 48,
"univariate": False,
"multivariate": True,
"rolling_evaluations": 12,
"test_split_date": pd.Period(
year=2011, month=5, day=28, hour=18, minute=55, freq="5T"
),
"_feat_static_cat_cardinalities": {
"train_test": (
("job_id", 850),
("task_id", 11117),
("user", 282),
("scheduling_class", 4),
("logical_job_name", 718),
),
"pretrain": (
("job_id", 6072),
("task_id", 154503),
("user", 518),
("scheduling_class", 4),
("logical_job_name", 3899),
),
},
"target_dim": 2,
"past_feat_dynamic_real_dim": 5,
},
"alibaba_cluster_trace_2018": {
"optional_fields": (
FieldName.FEAT_STATIC_CAT,
FieldName.PAST_FEAT_DYNAMIC_REAL,
),
"prediction_length": 48,
"freq": "5T",
"stride": 48,
"univariate": False,
"multivariate": True,
"rolling_evaluations": 12,
"test_split_date": pd.Period(
year=2018, month=1, day=8, hour=11, minute=55, freq="5T"
),
"_feat_static_cat_cardinalities": {
"train_test": (
("container_id", 6048),
("app_du", 1292),
),
"pretrain": (
("container_id", 64457),
("app_du", 9484),
),
},
"target_dim": 2,
"past_feat_dynamic_real_dim": 6,
},
}
PRETRAIN = datasets.splits.NamedSplit("pretrain")
TRAIN_TEST = datasets.splits.NamedSplit("train_test")
Cardinalities = tuple[tuple[str, int], ...]
@dataclass
class CloudOpsTSFConfig(datasets.BuilderConfig):
"""BuilderConfig for CloudOpsTSF."""
# load_dataset kwargs
train_test: bool = field(default=True, init=False)
pretrain: bool = field(default=False, init=False)
_include_metadata: tuple[str, ...] = field(default_factory=tuple, init=False)
# builder kwargs
prediction_length: int = field(default=None)
freq: str = field(default=None)
stride: int = field(default=None)
univariate: bool = field(default=None)
multivariate: bool = field(default=None)
optional_fields: tuple[str, ...] = field(default=None)
rolling_evaluations: int = field(default=None)
test_split_date: pd.Period = field(default=None)
_feat_static_cat_cardinalities: dict[str, Cardinalities] = field(
default_factory=dict
)
target_dim: int = field(default=1)
feat_static_real_dim: int = field(default=0)
past_feat_dynamic_real_dim: int = field(default=0)
METADATA = [
"freq",
"prediction_length",
"stride",
"rolling_evaluations",
]
@property
def include_metadata(self) -> tuple[str, ...]:
return self._include_metadata
@include_metadata.setter
def include_metadata(self, value: tuple[str, ...]):
assert all([v in self.METADATA for v in value]), (
f"Metadata: {value} is not supported, each item should be one of"
f" {self.METADATA}"
)
self._include_metadata = value
@cached_property
def feat_static_cat_cardinalities(self) -> Optional[list[int]]:
if FieldName.FEAT_STATIC_CAT not in self.optional_fields:
return None
if self.pretrain:
split = "pretrain"
elif self.train_test:
split = "train_test"
else:
raise ValueError(
"At least one of `train_test` and `pretrain` should be True"
)
return [c[1] for c in self._feat_static_cat_cardinalities[split]]
class CloudOpsTSF(datasets.ArrowBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = []
for dataset, config in _CONFIGS.items():
BUILDER_CONFIGS.append(
CloudOpsTSFConfig(name=dataset, version=VERSION, description="", **config)
)
def _info(self) -> datasets.DatasetInfo:
def sequence_feature(dtype: str, univar: bool) -> datasets.Sequence:
if univar:
return datasets.Sequence(datasets.Value(dtype))
return datasets.Sequence(datasets.Sequence(datasets.Value(dtype)))
features = {
FieldName.START: datasets.Value("timestamp[s]"),
FieldName.TARGET: sequence_feature("float32", self.config.univariate),
FieldName.ITEM_ID: datasets.Value("string"),
}
CAT_FEATS = (
FieldName.FEAT_STATIC_CAT,
FieldName.FEAT_DYNAMIC_CAT,
FieldName.PAST_FEAT_DYNAMIC,
)
REAL_FEATS = (
FieldName.FEAT_STATIC_REAL,
FieldName.FEAT_DYNAMIC_REAL,
FieldName.PAST_FEAT_DYNAMIC_REAL,
)
STATIC_FEATS = (FieldName.FEAT_STATIC_CAT, FieldName.FEAT_STATIC_REAL)
DYNAMIC_FEATS = (
FieldName.FEAT_DYNAMIC_CAT,
FieldName.FEAT_DYNAMIC_REAL,
FieldName.PAST_FEAT_DYNAMIC,
FieldName.PAST_FEAT_DYNAMIC_REAL,
)
for ts_field in self.config.optional_fields:
# Determine field dtype
if ts_field in CAT_FEATS:
dtype = "int32"
elif ts_field in REAL_FEATS:
dtype = "float32"
else:
raise ValueError(f"Invalid field: {ts_field}")
# Determine field shape
if ts_field in STATIC_FEATS:
univar = True
elif ts_field in DYNAMIC_FEATS:
univar = False
else:
raise ValueError(f"Invalid field: {ts_field}")
features[ts_field] = sequence_feature(dtype, univar)
for metadata in self.config.include_metadata:
if metadata == "freq":
features[metadata] = datasets.Value("string")
elif metadata in ("prediction_length", "stride", "rolling_evaluations"):
features[metadata] = datasets.Value("int32")
else:
raise ValueError(f"Invalid metadata: {metadata}")
features = datasets.Features(features)
return datasets.DatasetInfo(
features=features,
citation=_CITATION,
)
def _split_generators(self, dl_manager) -> list[datasets.SplitGenerator]:
generators = []
if self.config.train_test:
downloaded_files = dl_manager.download_and_extract(f"{self.config.name}/train_test.zip")
generators.append(
datasets.SplitGenerator(
name=TRAIN_TEST if self.config.train_test else PRETRAIN,
gen_kwargs={"filepath": downloaded_files}
)
)
if self.config.pretrain:
downloaded_files = dl_manager.download_and_extract(f"{self.config.name}/pretrain.zip")
generators.append(
datasets.SplitGenerator(
name=PRETRAIN,
gen_kwargs={"filepath": downloaded_files}
)
)
return generators
def _generate_tables(self, filepath: str) -> Iterator[pa.Table]:
table = pq.read_table(filepath)
for batch in table.to_batches():
columns = batch.columns
schema = batch.schema
if self.config.include_metadata:
freq = pa.array([self.config.freq] * len(batch))
prediction_length = pa.array([self.config.prediction_length] * len(batch))
rolling_evaluations = pa.array([self.config.rolling_evaluations] * len(batch))
stride = pa.array([self.config.stride] * len(batch))
columns += [freq, prediction_length, rolling_evaluations, stride]
for pa_field in [pa.field('freq', pa.string()),
pa.field('prediction_length', pa.int32()),
pa.field('rolling_evaluations', pa.int32()),
pa.field('stride', pa.int32())]:
schema = schema.append(pa_field)
yield batch[FieldName.ITEM_ID].to_pylist(), pa.Table.from_arrays(columns, schema=schema)
|