kumoai 2.13.0.dev202512041731__cp310-cp310-win_amd64.whl → 2.15.0.dev202601141731__cp310-cp310-win_amd64.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.
- kumoai/__init__.py +23 -26
- kumoai/_version.py +1 -1
- kumoai/client/client.py +6 -0
- kumoai/client/jobs.py +26 -0
- kumoai/client/pquery.py +6 -2
- kumoai/connector/utils.py +21 -7
- kumoai/experimental/rfm/__init__.py +51 -24
- kumoai/experimental/rfm/authenticate.py +3 -4
- kumoai/experimental/rfm/backend/local/__init__.py +4 -0
- kumoai/experimental/rfm/{local_graph_store.py → backend/local/graph_store.py} +62 -110
- kumoai/experimental/rfm/backend/local/sampler.py +312 -0
- kumoai/experimental/rfm/backend/local/table.py +35 -31
- kumoai/experimental/rfm/backend/snow/__init__.py +2 -0
- kumoai/experimental/rfm/backend/snow/sampler.py +407 -0
- kumoai/experimental/rfm/backend/snow/table.py +178 -50
- kumoai/experimental/rfm/backend/sqlite/__init__.py +4 -2
- kumoai/experimental/rfm/backend/sqlite/sampler.py +456 -0
- kumoai/experimental/rfm/backend/sqlite/table.py +131 -48
- kumoai/experimental/rfm/base/__init__.py +22 -4
- kumoai/experimental/rfm/base/column.py +96 -10
- kumoai/experimental/rfm/base/expression.py +44 -0
- kumoai/experimental/rfm/base/mapper.py +69 -0
- kumoai/experimental/rfm/base/sampler.py +696 -47
- kumoai/experimental/rfm/base/source.py +2 -1
- kumoai/experimental/rfm/base/sql_sampler.py +385 -0
- kumoai/experimental/rfm/base/table.py +384 -207
- kumoai/experimental/rfm/base/utils.py +36 -0
- kumoai/experimental/rfm/graph.py +359 -187
- kumoai/experimental/rfm/infer/__init__.py +6 -4
- kumoai/experimental/rfm/infer/dtype.py +10 -5
- kumoai/experimental/rfm/infer/multicategorical.py +1 -1
- kumoai/experimental/rfm/infer/pkey.py +4 -2
- kumoai/experimental/rfm/infer/stype.py +35 -0
- kumoai/experimental/rfm/infer/time_col.py +5 -4
- kumoai/experimental/rfm/pquery/executor.py +27 -27
- kumoai/experimental/rfm/pquery/pandas_executor.py +30 -32
- kumoai/experimental/rfm/relbench.py +76 -0
- kumoai/experimental/rfm/rfm.py +770 -467
- kumoai/experimental/rfm/sagemaker.py +4 -4
- kumoai/experimental/rfm/task_table.py +292 -0
- kumoai/kumolib.cp310-win_amd64.pyd +0 -0
- kumoai/pquery/predictive_query.py +10 -6
- kumoai/pquery/training_table.py +16 -2
- kumoai/testing/snow.py +50 -0
- kumoai/trainer/distilled_trainer.py +175 -0
- kumoai/utils/__init__.py +3 -2
- kumoai/utils/display.py +87 -0
- kumoai/utils/progress_logger.py +192 -13
- kumoai/utils/sql.py +3 -0
- {kumoai-2.13.0.dev202512041731.dist-info → kumoai-2.15.0.dev202601141731.dist-info}/METADATA +3 -2
- {kumoai-2.13.0.dev202512041731.dist-info → kumoai-2.15.0.dev202601141731.dist-info}/RECORD +54 -42
- kumoai/experimental/rfm/local_graph_sampler.py +0 -223
- kumoai/experimental/rfm/local_pquery_driver.py +0 -689
- {kumoai-2.13.0.dev202512041731.dist-info → kumoai-2.15.0.dev202601141731.dist-info}/WHEEL +0 -0
- {kumoai-2.13.0.dev202512041731.dist-info → kumoai-2.15.0.dev202601141731.dist-info}/licenses/LICENSE +0 -0
- {kumoai-2.13.0.dev202512041731.dist-info → kumoai-2.15.0.dev202601141731.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import base64
|
|
2
2
|
import json
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Any
|
|
4
4
|
|
|
5
5
|
import requests
|
|
6
6
|
|
|
@@ -48,8 +48,8 @@ class KumoClient_SageMakerAdapter(KumoClient):
|
|
|
48
48
|
|
|
49
49
|
# Recording buffers.
|
|
50
50
|
self._recording_active = False
|
|
51
|
-
self._recorded_reqs:
|
|
52
|
-
self._recorded_resps:
|
|
51
|
+
self._recorded_reqs: list[dict[str, Any]] = []
|
|
52
|
+
self._recorded_resps: list[dict[str, Any]] = []
|
|
53
53
|
|
|
54
54
|
def authenticate(self) -> None:
|
|
55
55
|
# TODO(siyang): call /ping to verify?
|
|
@@ -92,7 +92,7 @@ class KumoClient_SageMakerAdapter(KumoClient):
|
|
|
92
92
|
self._recorded_reqs.clear()
|
|
93
93
|
self._recorded_resps.clear()
|
|
94
94
|
|
|
95
|
-
def end_recording(self) ->
|
|
95
|
+
def end_recording(self) -> list[tuple[dict[str, Any], dict[str, Any]]]:
|
|
96
96
|
"""Stop recording and return recorded requests/responses."""
|
|
97
97
|
assert self._recording_active
|
|
98
98
|
self._recording_active = False
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
from collections.abc import Sequence
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from kumoapi.task import TaskType
|
|
6
|
+
from kumoapi.typing import Dtype, Stype
|
|
7
|
+
from typing_extensions import Self
|
|
8
|
+
|
|
9
|
+
from kumoai.experimental.rfm.base import Column
|
|
10
|
+
from kumoai.experimental.rfm.infer import contains_timestamp, infer_dtype
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TaskTable:
|
|
14
|
+
r"""A :class:`TaskTable` fully specifies the task, *i.e.* its context and
|
|
15
|
+
prediction examples with entity IDs, targets and timestamps.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
task_type: The task type.
|
|
19
|
+
context_df: The data frame holding context examples.
|
|
20
|
+
pred_df: The data frame holding prediction examples.
|
|
21
|
+
entity_table_name: The entity table to predict for. For link prediction
|
|
22
|
+
tasks, needs to hold both entity and target table names.
|
|
23
|
+
entity_column: The name of the entity column.
|
|
24
|
+
target_column: The name of the target column.
|
|
25
|
+
time_column: The name of the time column to use as anchor time. If
|
|
26
|
+
``TaskTable.ENTITY_TIME``, use the timestamp of the entity table
|
|
27
|
+
as anchor time.
|
|
28
|
+
"""
|
|
29
|
+
ENTITY_TIME = '__entity_time__'
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
task_type: TaskType,
|
|
34
|
+
context_df: pd.DataFrame,
|
|
35
|
+
pred_df: pd.DataFrame,
|
|
36
|
+
entity_table_name: str | Sequence[str],
|
|
37
|
+
entity_column: str,
|
|
38
|
+
target_column: str,
|
|
39
|
+
time_column: str | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
|
|
42
|
+
task_type = TaskType(task_type)
|
|
43
|
+
if task_type not in { # Currently supported task types:
|
|
44
|
+
TaskType.BINARY_CLASSIFICATION,
|
|
45
|
+
TaskType.MULTICLASS_CLASSIFICATION,
|
|
46
|
+
TaskType.REGRESSION,
|
|
47
|
+
TaskType.TEMPORAL_LINK_PREDICTION,
|
|
48
|
+
}:
|
|
49
|
+
raise ValueError # TODO
|
|
50
|
+
self._task_type = task_type
|
|
51
|
+
|
|
52
|
+
# TODO Binary classification and regression checks
|
|
53
|
+
|
|
54
|
+
# TODO Check dfs (unify from local table)
|
|
55
|
+
if context_df.empty:
|
|
56
|
+
raise ValueError("No context examples given")
|
|
57
|
+
self._context_df = context_df.copy(deep=False)
|
|
58
|
+
|
|
59
|
+
if pred_df.empty:
|
|
60
|
+
raise ValueError("Provide at least one entity to predict for")
|
|
61
|
+
self._pred_df = pred_df.copy(deep=False)
|
|
62
|
+
|
|
63
|
+
self._dtype_dict: dict[str, Dtype] = {
|
|
64
|
+
column_name: infer_dtype(context_df[column_name])
|
|
65
|
+
for column_name in context_df.columns
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
self._entity_table_names: tuple[str] | tuple[str, str]
|
|
69
|
+
if isinstance(entity_table_name, str):
|
|
70
|
+
self._entity_table_names = (entity_table_name, )
|
|
71
|
+
elif len(entity_table_name) == 1:
|
|
72
|
+
self._entity_table_names = (entity_table_name[0], )
|
|
73
|
+
elif len(entity_table_name) == 2:
|
|
74
|
+
self._entity_table_names = (
|
|
75
|
+
entity_table_name[0],
|
|
76
|
+
entity_table_name[1],
|
|
77
|
+
)
|
|
78
|
+
else:
|
|
79
|
+
raise ValueError # TODO
|
|
80
|
+
|
|
81
|
+
self._entity_column: str = ''
|
|
82
|
+
self._target_column: str = ''
|
|
83
|
+
self._time_column: str | None = None
|
|
84
|
+
|
|
85
|
+
self.entity_column = entity_column
|
|
86
|
+
self.target_column = target_column
|
|
87
|
+
if time_column is not None:
|
|
88
|
+
self.time_column = time_column
|
|
89
|
+
|
|
90
|
+
self._query: str = '' # A description of the task, e.g., for XAI.
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def num_context_examples(self) -> int:
|
|
94
|
+
return len(self._context_df)
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def num_prediction_examples(self) -> int:
|
|
98
|
+
return len(self._pred_df)
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def task_type(self) -> TaskType:
|
|
102
|
+
r"""The task type."""
|
|
103
|
+
return self._task_type
|
|
104
|
+
|
|
105
|
+
def narrow_context(self, start: int, length: int) -> Self:
|
|
106
|
+
r"""Returns a new :class:`TaskTable` that holds a narrowed version of
|
|
107
|
+
context examples.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
start: Index of the prediction examples to start narrowing.
|
|
111
|
+
length: Length of the prediction examples.
|
|
112
|
+
"""
|
|
113
|
+
out = copy.copy(self)
|
|
114
|
+
df = out._context_df.iloc[start:start + length].reset_index(drop=True)
|
|
115
|
+
out._context_df = df
|
|
116
|
+
return out
|
|
117
|
+
|
|
118
|
+
def narrow_prediction(self, start: int, length: int) -> Self:
|
|
119
|
+
r"""Returns a new :class:`TaskTable` that holds a narrowed version of
|
|
120
|
+
prediction examples.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
start: Index of the prediction examples to start narrowing.
|
|
124
|
+
length: Length of the prediction examples.
|
|
125
|
+
"""
|
|
126
|
+
out = copy.copy(self)
|
|
127
|
+
df = out._pred_df.iloc[start:start + length].reset_index(drop=True)
|
|
128
|
+
out._pred_df = df
|
|
129
|
+
return out
|
|
130
|
+
|
|
131
|
+
# Entity column ###########################################################
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def entity_table_name(self) -> str:
|
|
135
|
+
return self._entity_table_names[0]
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def entity_table_names(self) -> tuple[str] | tuple[str, str]:
|
|
139
|
+
return self._entity_table_names
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def entity_column(self) -> Column:
|
|
143
|
+
return Column(
|
|
144
|
+
name=self._entity_column,
|
|
145
|
+
expr=None,
|
|
146
|
+
dtype=self._dtype_dict[self._entity_column],
|
|
147
|
+
stype=Stype.ID,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
@entity_column.setter
|
|
151
|
+
def entity_column(self, name: str) -> None:
|
|
152
|
+
if name not in self._context_df:
|
|
153
|
+
raise ValueError # TODO
|
|
154
|
+
if name not in self._pred_df:
|
|
155
|
+
raise ValueError # TODO
|
|
156
|
+
if not Stype.ID.supports_dtype(self._dtype_dict[name]):
|
|
157
|
+
raise ValueError # TODO
|
|
158
|
+
|
|
159
|
+
self._entity_column = name
|
|
160
|
+
|
|
161
|
+
# Target column ###########################################################
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def evaluate(self) -> bool:
|
|
165
|
+
r"""Returns ``True`` if this task can be used for model evaluation."""
|
|
166
|
+
return self._target_column in self._pred_df
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def _target_stype(self) -> Stype:
|
|
170
|
+
if self.task_type in {
|
|
171
|
+
TaskType.BINARY_CLASSIFICATION,
|
|
172
|
+
TaskType.MULTICLASS_CLASSIFICATION,
|
|
173
|
+
}:
|
|
174
|
+
return Stype.categorical
|
|
175
|
+
if self.task_type in {TaskType.REGRESSION}:
|
|
176
|
+
return Stype.numerical
|
|
177
|
+
if self.task_type.is_link_pred:
|
|
178
|
+
return Stype.multicategorical
|
|
179
|
+
raise ValueError
|
|
180
|
+
|
|
181
|
+
@property
|
|
182
|
+
def target_column(self) -> Column:
|
|
183
|
+
return Column(
|
|
184
|
+
name=self._target_column,
|
|
185
|
+
expr=None,
|
|
186
|
+
dtype=self._dtype_dict[self._target_column],
|
|
187
|
+
stype=self._target_stype,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
@target_column.setter
|
|
191
|
+
def target_column(self, name: str) -> None:
|
|
192
|
+
if name not in self._context_df:
|
|
193
|
+
raise ValueError # TODO
|
|
194
|
+
if not self._target_stype.supports_dtype(self._dtype_dict[name]):
|
|
195
|
+
raise ValueError # TODO
|
|
196
|
+
|
|
197
|
+
self._target_column = name
|
|
198
|
+
|
|
199
|
+
# Time column #############################################################
|
|
200
|
+
|
|
201
|
+
def has_time_column(self) -> bool:
|
|
202
|
+
r"""Returns ``True`` if this task has a time column; ``False``
|
|
203
|
+
otherwise.
|
|
204
|
+
"""
|
|
205
|
+
return self._time_column not in {None, self.ENTITY_TIME}
|
|
206
|
+
|
|
207
|
+
@property
|
|
208
|
+
def use_entity_time(self) -> bool:
|
|
209
|
+
r"""Whether to use the timestamp of the entity table as anchor time."""
|
|
210
|
+
return self._time_column == self.ENTITY_TIME
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def time_column(self) -> Column | None:
|
|
214
|
+
r"""The time column of this task.
|
|
215
|
+
|
|
216
|
+
The getter returns the time column of this task, or ``None`` if no
|
|
217
|
+
such time column is present.
|
|
218
|
+
|
|
219
|
+
The setter sets a column as a time column for this task, and raises a
|
|
220
|
+
:class:`ValueError` if the time column has a non-timestamp compatible
|
|
221
|
+
data type or if the column name does not match a column in the data
|
|
222
|
+
frame.
|
|
223
|
+
"""
|
|
224
|
+
if not self.has_time_column():
|
|
225
|
+
return None
|
|
226
|
+
assert self._time_column is not None
|
|
227
|
+
return Column(
|
|
228
|
+
name=self._time_column,
|
|
229
|
+
expr=None,
|
|
230
|
+
dtype=self._dtype_dict[self._time_column],
|
|
231
|
+
stype=Stype.timestamp,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
@time_column.setter
|
|
235
|
+
def time_column(self, name: str | None) -> None:
|
|
236
|
+
if name is None or name == self.ENTITY_TIME:
|
|
237
|
+
self._time_column = name
|
|
238
|
+
return
|
|
239
|
+
|
|
240
|
+
if name not in self._context_df:
|
|
241
|
+
raise ValueError # TODO
|
|
242
|
+
if name not in self._pred_df:
|
|
243
|
+
raise ValueError # TODO
|
|
244
|
+
if not contains_timestamp(
|
|
245
|
+
ser=self._context_df[name],
|
|
246
|
+
column_name=name,
|
|
247
|
+
dtype=self._dtype_dict[name],
|
|
248
|
+
):
|
|
249
|
+
raise ValueError # TODO
|
|
250
|
+
|
|
251
|
+
self._time_column = name
|
|
252
|
+
|
|
253
|
+
# Metadata ################################################################
|
|
254
|
+
|
|
255
|
+
@property
|
|
256
|
+
def metadata(self) -> pd.DataFrame:
|
|
257
|
+
raise NotImplementedError
|
|
258
|
+
|
|
259
|
+
def print_metadata(self) -> None:
|
|
260
|
+
raise NotImplementedError
|
|
261
|
+
|
|
262
|
+
# Python builtins #########################################################
|
|
263
|
+
|
|
264
|
+
def __hash__(self) -> int:
|
|
265
|
+
return hash((
|
|
266
|
+
self.task_type,
|
|
267
|
+
self.entity_table_names,
|
|
268
|
+
self._entity_column,
|
|
269
|
+
self._target_column,
|
|
270
|
+
self._time_column,
|
|
271
|
+
))
|
|
272
|
+
|
|
273
|
+
def __repr__(self) -> str:
|
|
274
|
+
if self.task_type.is_link_pred:
|
|
275
|
+
entity_table_repr = f'entity_table_names={self.entity_table_names}'
|
|
276
|
+
else:
|
|
277
|
+
entity_table_repr = f'entity_table_name={self.entity_table_name}'
|
|
278
|
+
|
|
279
|
+
if self.use_entity_time:
|
|
280
|
+
time_repr = 'use_entity_time=True'
|
|
281
|
+
else:
|
|
282
|
+
time_repr = f'time_column={self._time_column}'
|
|
283
|
+
|
|
284
|
+
return (f'{self.__class__.__name__}(\n'
|
|
285
|
+
f' task_type={self.task_type},\n'
|
|
286
|
+
f' num_context_examples={self.num_context_examples},\n'
|
|
287
|
+
f' num_prediction_examples={self.num_prediction_examples},\n'
|
|
288
|
+
f' {entity_table_repr},\n'
|
|
289
|
+
f' entity_column={self._entity_column},\n'
|
|
290
|
+
f' target_column={self._target_column},\n'
|
|
291
|
+
f' {time_repr},\n'
|
|
292
|
+
f')')
|
|
Binary file
|
|
@@ -370,9 +370,11 @@ class PredictiveQuery:
|
|
|
370
370
|
train_table_job_api = global_state.client.generate_train_table_job_api
|
|
371
371
|
job_id: GenerateTrainTableJobID = train_table_job_api.create(
|
|
372
372
|
GenerateTrainTableRequest(
|
|
373
|
-
dict(custom_tags),
|
|
374
|
-
|
|
375
|
-
|
|
373
|
+
dict(custom_tags),
|
|
374
|
+
pq_id,
|
|
375
|
+
plan,
|
|
376
|
+
None,
|
|
377
|
+
))
|
|
376
378
|
|
|
377
379
|
self._train_table = TrainingTableJob(job_id=job_id)
|
|
378
380
|
if non_blocking:
|
|
@@ -451,9 +453,11 @@ class PredictiveQuery:
|
|
|
451
453
|
bp_table_api = global_state.client.generate_prediction_table_job_api
|
|
452
454
|
job_id: GeneratePredictionTableJobID = bp_table_api.create(
|
|
453
455
|
GeneratePredictionTableRequest(
|
|
454
|
-
dict(custom_tags),
|
|
455
|
-
|
|
456
|
-
|
|
456
|
+
dict(custom_tags),
|
|
457
|
+
pq_id,
|
|
458
|
+
plan,
|
|
459
|
+
None,
|
|
460
|
+
))
|
|
457
461
|
|
|
458
462
|
self._prediction_table = PredictionTableJob(job_id=job_id)
|
|
459
463
|
if non_blocking:
|
kumoai/pquery/training_table.py
CHANGED
|
@@ -199,6 +199,7 @@ class TrainingTable:
|
|
|
199
199
|
self,
|
|
200
200
|
source_table_type: SourceTableType,
|
|
201
201
|
train_table_mod: TrainingTableSpec,
|
|
202
|
+
extensive_validation: bool = False,
|
|
202
203
|
) -> None:
|
|
203
204
|
r"""Validates the modified training table.
|
|
204
205
|
|
|
@@ -206,6 +207,8 @@ class TrainingTable:
|
|
|
206
207
|
source_table_type: The source table to be used as the modified
|
|
207
208
|
training table.
|
|
208
209
|
train_table_mod: The modification specification.
|
|
210
|
+
extensive_validation: Enable extensive validation for custom
|
|
211
|
+
table.
|
|
209
212
|
|
|
210
213
|
Raises:
|
|
211
214
|
ValueError: If the modified training table is invalid.
|
|
@@ -215,7 +218,8 @@ class TrainingTable:
|
|
|
215
218
|
global_state.client.generate_train_table_job_api)
|
|
216
219
|
response = api.validate_custom_train_table(self.job_id,
|
|
217
220
|
source_table_type,
|
|
218
|
-
train_table_mod
|
|
221
|
+
train_table_mod,
|
|
222
|
+
extensive_validation)
|
|
219
223
|
if not response.ok:
|
|
220
224
|
raise ValueError("Invalid weighted train table",
|
|
221
225
|
response.error_message)
|
|
@@ -225,6 +229,7 @@ class TrainingTable:
|
|
|
225
229
|
source_table: SourceTable,
|
|
226
230
|
train_table_mod: TrainingTableSpec,
|
|
227
231
|
validate: bool = True,
|
|
232
|
+
extensive_validation: bool = False,
|
|
228
233
|
) -> Self:
|
|
229
234
|
r"""Sets the `source_table` as the modified training table.
|
|
230
235
|
|
|
@@ -243,6 +248,9 @@ class TrainingTable:
|
|
|
243
248
|
train_table_mod: The modification specification.
|
|
244
249
|
validate: Whether to validate the modified training table. This can
|
|
245
250
|
be slow for large tables.
|
|
251
|
+
extensive_validation: Whether to validate number of rows in
|
|
252
|
+
existing and modified training table.
|
|
253
|
+
It can be slow for large tables.
|
|
246
254
|
"""
|
|
247
255
|
if isinstance(source_table.connector, S3Connector):
|
|
248
256
|
# Special handling for s3 as `source_table._to_api_source_table`
|
|
@@ -252,7 +260,13 @@ class TrainingTable:
|
|
|
252
260
|
else:
|
|
253
261
|
source_table_type = source_table._to_api_source_table()
|
|
254
262
|
if validate:
|
|
255
|
-
|
|
263
|
+
if extensive_validation:
|
|
264
|
+
logger.warning(
|
|
265
|
+
"You have opted in to perform extensive validation on"
|
|
266
|
+
" your custom training table."
|
|
267
|
+
" This operation can be slow for large tables.")
|
|
268
|
+
self.validate_custom_table(source_table_type, train_table_mod,
|
|
269
|
+
extensive_validation)
|
|
256
270
|
self._custom_train_table = CustomTrainingTable(
|
|
257
271
|
source_table=source_table_type, table_mod_spec=train_table_mod,
|
|
258
272
|
validated=validate)
|
kumoai/testing/snow.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from kumoai.experimental.rfm.backend.snow import Connection
|
|
5
|
+
from kumoai.experimental.rfm.backend.snow import connect as _connect
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def connect(
|
|
9
|
+
region: str,
|
|
10
|
+
id: str,
|
|
11
|
+
account: str,
|
|
12
|
+
user: str,
|
|
13
|
+
warehouse: str | None = None,
|
|
14
|
+
database: str | None = None,
|
|
15
|
+
schema: str | None = None,
|
|
16
|
+
) -> Connection:
|
|
17
|
+
|
|
18
|
+
kwargs = dict(password=os.getenv('SNOWFLAKE_PASSWORD'))
|
|
19
|
+
if kwargs['password'] is None:
|
|
20
|
+
import boto3
|
|
21
|
+
from cryptography.hazmat.primitives import serialization
|
|
22
|
+
|
|
23
|
+
client = boto3.client(
|
|
24
|
+
service_name='secretsmanager',
|
|
25
|
+
region_name=region,
|
|
26
|
+
)
|
|
27
|
+
secret_id = (f'arn:aws:secretsmanager:{region}:{id}:secret:'
|
|
28
|
+
f'{account}.snowflakecomputing.com')
|
|
29
|
+
response = client.get_secret_value(SecretId=secret_id)['SecretString']
|
|
30
|
+
secret = json.loads(response)
|
|
31
|
+
|
|
32
|
+
private_key = serialization.load_pem_private_key(
|
|
33
|
+
secret['kumo_user_secretkey'].encode(),
|
|
34
|
+
password=None,
|
|
35
|
+
)
|
|
36
|
+
kwargs['private_key'] = private_key.private_bytes(
|
|
37
|
+
encoding=serialization.Encoding.DER,
|
|
38
|
+
format=serialization.PrivateFormat.PKCS8,
|
|
39
|
+
encryption_algorithm=serialization.NoEncryption(),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return _connect(
|
|
43
|
+
account=account,
|
|
44
|
+
user=user,
|
|
45
|
+
warehouse=warehouse or 'WH_XS',
|
|
46
|
+
database=database or 'KUMO',
|
|
47
|
+
schema=schema,
|
|
48
|
+
session_parameters=dict(CLIENT_TELEMETRY_ENABLED=False),
|
|
49
|
+
**kwargs,
|
|
50
|
+
)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Literal, Mapping, Optional, Union, overload
|
|
3
|
+
|
|
4
|
+
from kumoapi.distilled_model_plan import DistilledModelPlan
|
|
5
|
+
from kumoapi.jobs import DistillationJobRequest, DistillationJobResource
|
|
6
|
+
|
|
7
|
+
from kumoai import global_state
|
|
8
|
+
from kumoai.client.jobs import TrainingJobID
|
|
9
|
+
from kumoai.graph import Graph
|
|
10
|
+
from kumoai.pquery.training_table import TrainingTable, TrainingTableJob
|
|
11
|
+
from kumoai.trainer.job import TrainingJob, TrainingJobResult
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DistillationTrainer:
|
|
17
|
+
r"""A trainer supports creating a Kumo machine learning model
|
|
18
|
+
for use in an online serving endpoint. The distllation process involes
|
|
19
|
+
training a shallow model on a :class:`~kumoai.pquery.PredictiveQuery` using
|
|
20
|
+
the embeddings generated by a base model :args:`base_training_job_id`.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
model_plan: The distilled model plan to use for the distillation process.
|
|
24
|
+
base_training_job_id: The ID of the base training job to use for the distillation process.
|
|
25
|
+
""" # noqa: E501
|
|
26
|
+
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
model_plan: DistilledModelPlan,
|
|
30
|
+
base_training_job_id: TrainingJobID,
|
|
31
|
+
) -> None:
|
|
32
|
+
self.model_plan: DistilledModelPlan = model_plan
|
|
33
|
+
self.base_training_job_id: TrainingJobID = base_training_job_id
|
|
34
|
+
|
|
35
|
+
# Cached from backend:
|
|
36
|
+
self._training_job_id: Optional[TrainingJobID] = None
|
|
37
|
+
|
|
38
|
+
# Metadata ################################################################
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def is_trained(self) -> bool:
|
|
42
|
+
r"""Returns ``True`` if this trainer instance has successfully been
|
|
43
|
+
trained (and is therefore ready for prediction); ``False`` otherwise.
|
|
44
|
+
"""
|
|
45
|
+
raise NotImplementedError(
|
|
46
|
+
"Checking if a distilled trainer is trained is not "
|
|
47
|
+
"implemented yet.")
|
|
48
|
+
|
|
49
|
+
@overload
|
|
50
|
+
def fit(
|
|
51
|
+
self,
|
|
52
|
+
graph: Graph,
|
|
53
|
+
train_table: Union[TrainingTable, TrainingTableJob],
|
|
54
|
+
) -> TrainingJobResult:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
@overload
|
|
58
|
+
def fit(
|
|
59
|
+
self,
|
|
60
|
+
graph: Graph,
|
|
61
|
+
train_table: Union[TrainingTable, TrainingTableJob],
|
|
62
|
+
*,
|
|
63
|
+
non_blocking: Literal[False],
|
|
64
|
+
) -> TrainingJobResult:
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
@overload
|
|
68
|
+
def fit(
|
|
69
|
+
self,
|
|
70
|
+
graph: Graph,
|
|
71
|
+
train_table: Union[TrainingTable, TrainingTableJob],
|
|
72
|
+
*,
|
|
73
|
+
non_blocking: Literal[True],
|
|
74
|
+
) -> TrainingJob:
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
@overload
|
|
78
|
+
def fit(
|
|
79
|
+
self,
|
|
80
|
+
graph: Graph,
|
|
81
|
+
train_table: Union[TrainingTable, TrainingTableJob],
|
|
82
|
+
*,
|
|
83
|
+
non_blocking: bool,
|
|
84
|
+
) -> Union[TrainingJob, TrainingJobResult]:
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
def fit(
|
|
88
|
+
self,
|
|
89
|
+
graph: Graph,
|
|
90
|
+
train_table: Union[TrainingTable, TrainingTableJob],
|
|
91
|
+
*,
|
|
92
|
+
non_blocking: bool = False,
|
|
93
|
+
custom_tags: Mapping[str, str] = {},
|
|
94
|
+
) -> Union[TrainingJob, TrainingJobResult]:
|
|
95
|
+
r"""Fits a model to the specified graph and training table, with the
|
|
96
|
+
strategy defined by :class:`DistilledTrainer`'s :obj:`model_plan`.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
graph: The :class:`~kumoai.graph.Graph` object that represents the
|
|
100
|
+
tables and relationships that Kumo will learn from.
|
|
101
|
+
train_table: The :class:`~kumoai.pquery.TrainingTable`, or
|
|
102
|
+
in-progress :class:`~kumoai.pquery.TrainingTableJob`, that
|
|
103
|
+
represents the training data produced by a
|
|
104
|
+
:class:`~kumoai.pquery.PredictiveQuery` on :obj:`graph`.
|
|
105
|
+
non_blocking: Whether this operation should return immediately
|
|
106
|
+
after launching the training job, or await completion of the
|
|
107
|
+
training job.
|
|
108
|
+
custom_tags: Additional, customer defined k-v tags to be associated
|
|
109
|
+
with the job to be launched. Job tags are useful for grouping
|
|
110
|
+
and searching jobs.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
Union[TrainingJobResult, TrainingJob]:
|
|
114
|
+
If ``non_blocking=False``, returns a training job object. If
|
|
115
|
+
``non_blocking=True``, returns a training job future object.
|
|
116
|
+
"""
|
|
117
|
+
# TODO(manan, siyang): remove soon:
|
|
118
|
+
job_id = train_table.job_id
|
|
119
|
+
assert job_id is not None
|
|
120
|
+
|
|
121
|
+
train_table_job_api = global_state.client.generate_train_table_job_api
|
|
122
|
+
pq_id = train_table_job_api.get(job_id).config.pquery_id
|
|
123
|
+
assert pq_id is not None
|
|
124
|
+
|
|
125
|
+
custom_table = None
|
|
126
|
+
if isinstance(train_table, TrainingTable):
|
|
127
|
+
custom_table = train_table._custom_train_table
|
|
128
|
+
|
|
129
|
+
# NOTE the backend implementation currently handles sequentialization
|
|
130
|
+
# between a training table future and a training job; that is, if the
|
|
131
|
+
# training table future is still executing, the backend will wait on
|
|
132
|
+
# the job ID completion before executing a training job. This preserves
|
|
133
|
+
# semantics for both futures, ensures that Kumo works as expected if
|
|
134
|
+
# used only via REST API, and allows us to avoid chaining calllbacks
|
|
135
|
+
# in an ugly way here:
|
|
136
|
+
api = global_state.client.distillation_job_api
|
|
137
|
+
self._training_job_id = api.create(
|
|
138
|
+
DistillationJobRequest(
|
|
139
|
+
dict(custom_tags),
|
|
140
|
+
pquery_id=pq_id,
|
|
141
|
+
base_training_job_id=self.base_training_job_id,
|
|
142
|
+
distilled_model_plan=self.model_plan,
|
|
143
|
+
graph_snapshot_id=graph.snapshot(non_blocking=non_blocking),
|
|
144
|
+
train_table_job_id=job_id,
|
|
145
|
+
custom_train_table=custom_table,
|
|
146
|
+
))
|
|
147
|
+
|
|
148
|
+
out = TrainingJob(job_id=self._training_job_id)
|
|
149
|
+
if non_blocking:
|
|
150
|
+
return out
|
|
151
|
+
return out.attach()
|
|
152
|
+
|
|
153
|
+
@classmethod
|
|
154
|
+
def _load_from_job(
|
|
155
|
+
cls,
|
|
156
|
+
job: DistillationJobResource,
|
|
157
|
+
) -> 'DistillationTrainer':
|
|
158
|
+
trainer = cls(job.config.distilled_model_plan,
|
|
159
|
+
job.config.base_training_job_id)
|
|
160
|
+
trainer._training_job_id = job.job_id
|
|
161
|
+
return trainer
|
|
162
|
+
|
|
163
|
+
@classmethod
|
|
164
|
+
def load(cls, job_id: TrainingJobID) -> 'DistillationTrainer':
|
|
165
|
+
r"""Creates a :class:`~kumoai.trainer.Trainer` instance from a training
|
|
166
|
+
job ID.
|
|
167
|
+
"""
|
|
168
|
+
raise NotImplementedError(
|
|
169
|
+
"Loading a distilled trainer from a job ID is not implemented yet."
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
@classmethod
|
|
173
|
+
def load_from_tags(cls, tags: Mapping[str, str]) -> 'DistillationTrainer':
|
|
174
|
+
raise NotImplementedError(
|
|
175
|
+
"Loading a distilled trainer from tags is not implemented yet.")
|
kumoai/utils/__init__.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
from .
|
|
1
|
+
from .sql import quote_ident
|
|
2
|
+
from .progress_logger import ProgressLogger
|
|
2
3
|
from .forecasting import ForecastVisualizer
|
|
3
4
|
from .datasets import from_relbench
|
|
4
5
|
|
|
5
6
|
__all__ = [
|
|
7
|
+
'quote_ident',
|
|
6
8
|
'ProgressLogger',
|
|
7
|
-
'InteractiveProgressLogger',
|
|
8
9
|
'ForecastVisualizer',
|
|
9
10
|
'from_relbench',
|
|
10
11
|
]
|