-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcore.py
346 lines (281 loc) · 12.6 KB
/
core.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2023 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.
"""Core operations for BQML based models"""
from __future__ import annotations
import datetime
from typing import Callable, cast, Iterable, Mapping, Optional, Union
import uuid
from google.cloud import bigquery
import bigframes
import bigframes.constants as constants
from bigframes.ml import sql as ml_sql
import bigframes.pandas as bpd
class BqmlModel:
"""Represents an existing BQML model in BigQuery.
Wraps the BQML API and SQL interface to expose the functionality needed for
BigQuery DataFrames ML.
"""
def __init__(self, session: bigframes.Session, model: bigquery.Model):
self._session = session
self._model = model
self._model_manipulation_sql_generator = ml_sql.ModelManipulationSqlGenerator(
self.model_name
)
@property
def session(self) -> bigframes.Session:
"""Get the BigQuery DataFrames session that this BQML model wrapper is tied to"""
return self._session
@property
def model_name(self) -> str:
"""Get the fully qualified name of the model, i.e. project_id.dataset_id.model_id"""
return f"{self._model.project}.{self._model.dataset_id}.{self._model.model_id}"
@property
def model(self) -> bigquery.Model:
"""Get the BQML model associated with this wrapper"""
return self._model
def _apply_sql(
self,
input_data: bpd.DataFrame,
func: Callable[[bpd.DataFrame], str],
) -> bpd.DataFrame:
"""Helper to wrap a dataframe in a SQL query, keeping the index intact.
Args:
session (bigframes.Session):
the active bigframes.Session
input_data (bigframes.dataframe.DataFrame):
the dataframe to be wrapped
func (function):
a function that will accept a SQL string and produce a new SQL
string from which to construct the output dataframe. It must
include the index columns of the input SQL.
"""
_, index_col_ids, index_labels = input_data._to_sql_query(include_index=True)
sql = func(input_data)
df = self._session.read_gbq(sql, index_col=index_col_ids)
df.index.names = index_labels
return df
def predict(self, input_data: bpd.DataFrame) -> bpd.DataFrame:
# TODO: validate input data schema
return self._apply_sql(
input_data,
self._model_manipulation_sql_generator.ml_predict,
)
def transform(self, input_data: bpd.DataFrame) -> bpd.DataFrame:
# TODO: validate input data schema
return self._apply_sql(
input_data,
self._model_manipulation_sql_generator.ml_transform,
)
def generate_text(
self,
input_data: bpd.DataFrame,
options: Mapping[str, int | float],
) -> bpd.DataFrame:
# TODO: validate input data schema
return self._apply_sql(
input_data,
lambda source_df: self._model_manipulation_sql_generator.ml_generate_text(
source_df=source_df,
struct_options=options,
),
)
def generate_text_embedding(
self,
input_data: bpd.DataFrame,
options: Mapping[str, int | float],
) -> bpd.DataFrame:
# TODO: validate input data schema
return self._apply_sql(
input_data,
lambda source_df: self._model_manipulation_sql_generator.ml_generate_text_embedding(
source_df=source_df,
struct_options=options,
),
)
def forecast(self) -> bpd.DataFrame:
sql = self._model_manipulation_sql_generator.ml_forecast()
return self._session.read_gbq(sql, index_col="forecast_timestamp").reset_index()
def evaluate(self, input_data: Optional[bpd.DataFrame] = None):
# TODO: validate input data schema
sql = self._model_manipulation_sql_generator.ml_evaluate(input_data)
return self._session.read_gbq(sql)
def centroids(self) -> bpd.DataFrame:
assert self._model.model_type == "KMEANS"
sql = self._model_manipulation_sql_generator.ml_centroids()
return self._session.read_gbq(
sql, index_col=["centroid_id", "feature"]
).reset_index()
def principal_components(self) -> bpd.DataFrame:
assert self._model.model_type == "PCA"
sql = self._model_manipulation_sql_generator.ml_principal_components()
return self._session.read_gbq(
sql, index_col=["principal_component_id", "feature"]
).reset_index()
def principal_component_info(self) -> bpd.DataFrame:
assert self._model.model_type == "PCA"
sql = self._model_manipulation_sql_generator.ml_principal_component_info()
return self._session.read_gbq(sql)
def copy(self, new_model_name: str, replace: bool = False) -> BqmlModel:
job_config = bigquery.job.CopyJobConfig()
if replace:
job_config.write_disposition = "WRITE_TRUNCATE"
copy_job = self._session.bqclient.copy_table(
self.model_name, new_model_name, job_config=job_config
)
self._session._start_generic_job(copy_job)
new_model = self._session.bqclient.get_model(new_model_name)
return BqmlModel(self._session, new_model)
def register(self, vertex_ai_model_id: Optional[str] = None) -> BqmlModel:
if vertex_ai_model_id is None:
# vertex id needs to start with letters. https://cloud.google.com/vertex-ai/docs/general/resource-naming
vertex_ai_model_id = "bigframes_" + cast(str, self._model.model_id)
# truncate as Vertex ID only accepts 63 characters, easily exceeding the limit for temp models.
# The possibility of conflicts should be low.
vertex_ai_model_id = vertex_ai_model_id[:63]
sql = self._model_manipulation_sql_generator.alter_model(
options={"vertex_ai_model_id": vertex_ai_model_id}
)
# Register the model and wait it to finish
self._session._start_query(sql)
self._model = self._session.bqclient.get_model(self.model_name)
return self
class BqmlModelFactory:
def __init__(self):
self._model_creation_sql_generator = ml_sql.ModelCreationSqlGenerator()
def _create_model_ref(
self, dataset: bigquery.DatasetReference
) -> bigquery.ModelReference:
return bigquery.ModelReference.from_string(
f"{dataset.project}.{dataset.dataset_id}.{uuid.uuid4().hex}"
)
def _create_model_with_sql(self, session: bigframes.Session, sql: str) -> BqmlModel:
# fit the model, synchronously
_, job = session._start_query(sql)
# real model path in the session specific hidden dataset and table prefix
model_name_full = f"{job.destination.project}.{job.destination.dataset_id}.{job.destination.table_id}"
model = bigquery.Model(model_name_full)
model.expires = (
datetime.datetime.now(datetime.timezone.utc) + constants.DEFAULT_EXPIRATION
)
model = session.bqclient.update_model(model, ["expires"])
return BqmlModel(session, model)
def create_model(
self,
X_train: bpd.DataFrame,
y_train: Optional[bpd.DataFrame] = None,
transforms: Optional[Iterable[str]] = None,
options: Mapping[str, Union[str, int, float, Iterable[str]]] = {},
) -> BqmlModel:
"""Create a session-temporary BQML model with the CREATE OR REPLACE MODEL statement
Args:
X_train: features columns for training
y_train: labels columns for training, if applicable
transforms: an optional list of SQL expressions that implement preprocessing
on top of the input data. Generates a BQML TRANSFORM clause
options: a dict of options to configure the model. Generates a BQML OPTIONS
clause
Returns: a BqmlModel, wrapping a trained model in BigQuery
"""
options = dict(options)
# Cache dataframes to make sure base table is not a snapshot
# cached dataframe creates a full copy, never uses snapshot
if y_train is None:
input_data = X_train._cached()
else:
input_data = X_train._cached().join(y_train._cached(), how="outer")
options.update({"INPUT_LABEL_COLS": y_train.columns.tolist()})
session = X_train._session
model_ref = self._create_model_ref(session._anonymous_dataset)
sql = self._model_creation_sql_generator.create_model(
source_df=input_data,
model_ref=model_ref,
transforms=transforms,
options=options,
)
return self._create_model_with_sql(session=session, sql=sql)
def create_time_series_model(
self,
X_train: bpd.DataFrame,
y_train: bpd.DataFrame,
transforms: Optional[Iterable[str]] = None,
options: Mapping[str, Union[str, int, float, Iterable[str]]] = {},
) -> BqmlModel:
assert (
X_train.columns.size == 1
), "Time series timestamp input must only contain 1 column."
assert (
y_train.columns.size == 1
), "Time stamp data input must only contain 1 column."
options = dict(options)
# Cache dataframes to make sure base table is not a snapshot
# cached dataframe creates a full copy, never uses snapshot
input_data = X_train._cached().join(y_train._cached(), how="outer")
options.update({"TIME_SERIES_TIMESTAMP_COL": X_train.columns.tolist()[0]})
options.update({"TIME_SERIES_DATA_COL": y_train.columns.tolist()[0]})
session = X_train._session
model_ref = self._create_model_ref(session._anonymous_dataset)
sql = self._model_creation_sql_generator.create_model(
source_df=input_data,
model_ref=model_ref,
transforms=transforms,
options=options,
)
return self._create_model_with_sql(session=session, sql=sql)
def create_remote_model(
self,
session: bigframes.Session,
connection_name: str,
input: Mapping[str, str] = {},
output: Mapping[str, str] = {},
options: Mapping[str, Union[str, int, float, Iterable[str]]] = {},
) -> BqmlModel:
"""Create a session-temporary BQML remote model with the CREATE OR REPLACE MODEL statement
Args:
connection_name:
a BQ connection to talk with Vertex AI, of the format <PROJECT_NUMBER>.<REGION>.<CONNECTION_NAME>. https://cloud.google.com/bigquery/docs/create-cloud-resource-connection
input:
input schema for general remote models
output:
output schema for general remote models
options:
a dict of options to configure the model. Generates a BQML OPTIONS clause
Returns:
BqmlModel: a BqmlModel wrapping a trained model in BigQuery
"""
model_ref = self._create_model_ref(session._anonymous_dataset)
sql = self._model_creation_sql_generator.create_remote_model(
connection_name=connection_name,
model_ref=model_ref,
input=input,
output=output,
options=options,
)
return self._create_model_with_sql(session=session, sql=sql)
def create_imported_model(
self,
session: bigframes.Session,
options: Mapping[str, Union[str, int, float, Iterable[str]]] = {},
) -> BqmlModel:
"""Create a session-temporary BQML imported model with the CREATE OR REPLACE MODEL statement
Args:
options: a dict of options to configure the model. Generates a BQML OPTIONS
clause
Returns: a BqmlModel, wrapping a trained model in BigQuery
"""
model_ref = self._create_model_ref(session._anonymous_dataset)
sql = self._model_creation_sql_generator.create_imported_model(
model_ref=model_ref,
options=options,
)
return self._create_model_with_sql(session=session, sql=sql)