-
Notifications
You must be signed in to change notification settings - Fork 72
/
01_build_train_deploy.py
422 lines (357 loc) · 15.4 KB
/
01_build_train_deploy.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Copyright 2018 Google Inc. All Rights Reserved.
#
# 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.
import datetime, json, re, logging
from airflow import models
from airflow.operators.python_operator import PythonOperator, BranchPythonOperator
from airflow.hooks.base_hook import BaseHook
from airflow.contrib.operators import bigquery_operator
from airflow.contrib.operators import bigquery_get_data
from airflow.contrib.operators import gcs_to_bq
from airflow.contrib.operators import bigquery_to_gcs
from airflow.contrib.operators import mlengine_operator
from airflow.contrib.operators import mlengine_operator_utils
from airflow.contrib.hooks.gcp_mlengine_hook import MLEngineHook
from airflow.contrib.hooks.gcs_hook import GoogleCloudStorageHook
from airflow.operators import bash_operator
from airflow.operators.dummy_operator import DummyOperator
from airflow.utils import trigger_rule
from google.cloud.automl_v1beta1 import AutoMlClient
from clv_automl import clv_automl
def _get_project_id():
"""Get project ID from default GCP connection."""
extras = BaseHook.get_connection('google_cloud_default').extra_dejson
key = 'extra__google_cloud_platform__project'
if key in extras:
project_id = extras[key]
else:
raise ('Must configure project_id in google_cloud_default '
'connection from Airflow Console')
return project_id
PROJECT = _get_project_id()
REGION = models.Variable.get('region')
DATASET = models.Variable.get('dataset')
COMPOSER_BUCKET_NAME = models.Variable.get('composer_bucket_name')
GCS_SQL = 'sql'
DB_DUMP_FILENAME = 'db_dump.csv'
LOCATION_TRAINING_DATA = '{}/data'.format(COMPOSER_BUCKET_NAME)
PREFIX_JOBS_EXPORT = 'jobs/clv-composer'
PREFIX_FINAL_MODEL = '{}/final'.format(PREFIX_JOBS_EXPORT)
MODEL_PACKAGE_NAME = 'clv_ml_engine-0.1.tar.gz' # Matches name in setup.py
AUTOML_DATASET = models.Variable.get('automl_dataset')
AUTOML_MODEL = models.Variable.get('automl_model')
AUTOML_TRAINING_BUDGET = int(models.Variable.get('automl_training_budget'))
#[START dag_build_train_deploy]
default_dag_args = {
'start_date': datetime.datetime(2050, 1, 1),
'schedule_interval': None,
'provide_context': True
}
dag = models.DAG(
'build_train_deploy',
default_args = default_dag_args)
#[END dag_build_train_deploy]
# Loads the database dump from Cloud Storage to BigQuery
t1 = gcs_to_bq.GoogleCloudStorageToBigQueryOperator(
task_id="db_dump_to_bigquery",
bucket=COMPOSER_BUCKET_NAME,
source_objects=[DB_DUMP_FILENAME],
schema_object="schema_source.json",
source_format="CSV",
skip_leading_rows=1,
destination_project_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'data_source'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
# Clean the data from BigQuery to BigQuery
t2 = bigquery_operator.BigQueryOperator(
task_id='bq_from_source_to_clean',
bql='{}/common/clean.sql'.format(GCS_SQL),
use_legacy_sql=False,
allow_large_results=True,
destination_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'data_cleaned'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
# Creates split between features and targets and also aggregates both sides.
# The threshold date is passed as an arg when calling the Airflow job and
# dynamically understood within the .sql file.
# We should pass query_params but we run into various problems:
# - if using BigQueryOperator, we can not pass dag_run.conf['threshold_date']
# - if using hooks, run_query does not accept a .sql file and needs the string
# So the way is to add directly {{ dag_run.conf['threshold_date'] }} into the
# .sql file which Airflow can ping up when running the operator.
t3 = bigquery_operator.BigQueryOperator(
task_id='bq_from_clean_to_features',
bql='{}/common/features_n_target.sql'.format(GCS_SQL),
use_legacy_sql=False,
allow_large_results=True,
destination_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'features_n_target'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
def get_model_type(**kwargs):
model_type = kwargs['dag_run'].conf.get('model_type')
if model_type == 'automl':
model_train_task = 'train_automl'
else:
model_train_task = 'train_ml_engine'
return model_train_task
t4_train_cond = BranchPythonOperator(task_id='train_branch', dag=dag, python_callable=get_model_type)
#
# Train the model using AutoML
#
def do_train_automl(**kwargs):
"""
Create, train and deploy automl model.
"""
# instantiate automl client
automl_client = AutoMlClient()
model_name = clv_automl.create_automl_model(automl_client,
PROJECT,
REGION,
DATASET,
'features_n_target',
AUTOML_DATASET,
AUTOML_MODEL,
AUTOML_TRAINING_BUDGET)
clv_automl.deploy_model(automl_client, model_name)
t4_automl = PythonOperator(
task_id='train_automl', dag=dag, python_callable=do_train_automl)
t4_ml_engine = DummyOperator(task_id='train_ml_engine', dag=dag)
# Split the data into a training set and evaluation set within BigQuery
t4a = bigquery_operator.BigQueryOperator(
task_id='bq_dnn_train',
bql='{}/dnn/split_train.sql'.format(GCS_SQL),
use_legacy_sql=False,
allow_large_results=True,
destination_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'dnn_train'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
t4b = bigquery_operator.BigQueryOperator(
task_id='bq_dnn_eval',
bql='{}/dnn/split_eval.sql'.format(GCS_SQL),
use_legacy_sql=False,
allow_large_results=True,
destination_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'dnn_eval'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
t4c = bigquery_operator.BigQueryOperator(
task_id='bq_dnn_test',
bql='{}/dnn/split_test.sql'.format(GCS_SQL),
use_legacy_sql=False,
allow_large_results=True,
destination_dataset_table="{}.{}.{}".format(PROJECT,
DATASET,
'dnn_test'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
# TODO: Currently all data steps are done whether BTYD or DNN are used. It would
# be better to have a condition to call only one task or the other using 'model_type'
data_btyd_location = ['gs://{}/{}'.format(LOCATION_TRAINING_DATA, 'btyd.csv')]
data_train_locations = ['gs://{}/{}'.format(LOCATION_TRAINING_DATA, 'train.csv')]
data_eval_locations = ['gs://{}/{}'.format(LOCATION_TRAINING_DATA, 'eval.csv')]
data_test_locations = ['gs://{}/{}'.format(LOCATION_TRAINING_DATA, 'test.csv')]
t5a = bigquery_to_gcs.BigQueryToCloudStorageOperator(
task_id='bq_dnn_train_to_gcs',
source_project_dataset_table="{}.{}.{}".format(PROJECT, DATASET, 'dnn_train'),
destination_cloud_storage_uris=data_train_locations,
print_header=False,
dag=dag
)
t5b = bigquery_to_gcs.BigQueryToCloudStorageOperator(
task_id='bq_dnn_eval_to_gcs',
source_project_dataset_table="{}.{}.{}".format(PROJECT, DATASET, 'dnn_eval'),
destination_cloud_storage_uris=data_eval_locations,
print_header=False,
dag=dag
)
t5c = bigquery_to_gcs.BigQueryToCloudStorageOperator(
task_id='bq_dnn_test_to_gcs',
source_project_dataset_table="{}.{}.{}".format(PROJECT, DATASET, 'dnn_test'),
destination_cloud_storage_uris=data_test_locations,
print_header=False,
dag=dag
)
t5d = bigquery_to_gcs.BigQueryToCloudStorageOperator(
task_id='bq_btyd_to_gcs',
source_project_dataset_table="{}.{}.{}".format(PROJECT, DATASET, 'features_n_target'),
destination_cloud_storage_uris=data_btyd_location,
print_header=True,
dag=dag
)
#
# Train the model using ML Engine (TensorFlow DNN or Lifetimes BTYD)
#
def do_train_ml_engine(**kwargs):
"""
"""
job_id = 'clv-{}'.format(datetime.datetime.now().strftime('%Y%m%d%H%M'))
mlengine_operator.MLEngineTrainingOperator(
task_id='train_ml_engine_job',
project_id=PROJECT,
job_id=job_id,
package_uris=['gs://{}/code/{}'.format(COMPOSER_BUCKET_NAME, MODEL_PACKAGE_NAME)],
training_python_module='trainer.task',
region=REGION,
training_args=['--job-dir', 'gs://{}/{}/{}'.format(COMPOSER_BUCKET_NAME, PREFIX_JOBS_EXPORT, job_id),
'--data-src', 'gs://{}'.format(LOCATION_TRAINING_DATA),
'--model_type', kwargs['dag_run'].conf.get('model_type')],
dag=dag
).execute(kwargs)
t6 = PythonOperator(
task_id='train_ml_engine_task', dag=dag, python_callable=do_train_ml_engine)
#
# Copies the latest model to a consistent 'final' bucket
#
def do_copy_model_to_final(**kwargs):
gcs = GoogleCloudStorageHook()
# Returns all the objects within the bucket. All sub-buckets are considered
# as prefix of the leaves. List does not differentiate files from subbuckets
all_jobs_files = gcs.list(
bucket=COMPOSER_BUCKET_NAME,
prefix='{}/export/estimate'.format(PREFIX_JOBS_EXPORT)
)
# Extract the latest model bucket parent of variables/ and saved_model.pbtxt
# The max() string contains the latest model folders in 1234567, we need to
# extract that using regex
# ex: jobs/clv-composer/export/estimate/1234567890/variables/variables.index
# returns /1234567890/
latest_model_bucket = re.findall(r'/\d+/', max(all_jobs_files))[0]
# List all the files that needs to be copied (only files in the latest bucket
# and skip the ones that are not files but sub buckets)
for c in [f for f in all_jobs_files
if latest_model_bucket in f and f[-1] != '/']:
# The model used for training is saved into a 'final' sub bucket of the
# export bucket.
dest_object = c.split(latest_model_bucket)[1]
dest_object = '{}/{}'.format(PREFIX_FINAL_MODEL, dest_object)
logging.info("Copying {} to {} ...".format(dest_object, COMPOSER_BUCKET_NAME))
gcs.copy(
source_bucket=COMPOSER_BUCKET_NAME,
source_object=c,
destination_object=dest_object
)
# Note that this could be done as well in Tensorflow using tf.gFile aftet the
# model is created but for reasons of flexibility, it was decided to do this in the
# wider workflow. This way, it is also possible pick other models.
t7 = PythonOperator(
task_id='copy_model_to_final',
python_callable=do_copy_model_to_final,
dag=dag)
#
# Model Creation
#
def do_check_model(**kwargs):
""" Check if a model with the name exists using Hooks instead of operators.
Uses xcom_push to pass it to the next step. Could use return too if no key.
"""
# pushes an XCom without a specific target, just by returning it
mle = MLEngineHook()
model_name = kwargs['dag_run'].conf.get('model_name')
# return bool(mle.get_model(PROJECT, MODEL_DNN_NAME))
project = mle.get_model(PROJECT, model_name)
kwargs['ti'].xcom_push(key='is_project', value=bool(project))
def do_create_model(**kwargs):
""" Creates a model only if one with the same name did not exist.
It leverages the check from the previous task pushed using xcom.
"""
model_params = {
'name': kwargs['dag_run'].conf.get('model_name'),
'description': 'A custom DNN regressor model',
'regions': [REGION]
}
ti = kwargs['ti']
is_model = ti.xcom_pull(key='is_project', task_ids='check_model')
if not is_model:
mle = MLEngineHook()
mle.create_model(PROJECT, model_params)
# Checks if model exists using Hook instead of GCP operators due to conditional.
t8 = PythonOperator(
task_id='check_model', dag=dag, python_callable=do_check_model)
# Creates model if it does not exist using Hook instead of GCP operators
t9 = PythonOperator(
task_id='create_model', dag=dag, python_callable=do_create_model)
#
# Version Creation
#
def do_list_versions(**kwargs):
""" Check if a version with the name exists using Hooks instead of operators.
Uses xcom_push to pass it to the next step. Could use return too if no key.
"""
mle = MLEngineHook()
model_name = kwargs['dag_run'].conf.get('model_name')
model_versions = mle.list_versions(PROJECT, model_name)
kwargs['ti'].xcom_push(key='model_versions', value=model_versions)
def do_create_version(**kwargs):
""" Creates a new version or overwrite if existing one. It leverages the
check from the previous task pushed using xcom.
"""
version_params = {
"name": kwargs['dag_run'].conf.get('model_version'),
"description": 'Version 1',
"runtimeVersion": kwargs['dag_run'].conf.get('tf_version'),
"deploymentUri": 'gs://{}/{}'.format(COMPOSER_BUCKET_NAME, PREFIX_FINAL_MODEL)
}
ti = kwargs['ti']
mle = MLEngineHook()
model_name = kwargs['dag_run'].conf.get('model_name')
model_versions = ti.xcom_pull(key='model_versions', task_ids='list_versions')
version_path = 'projects/{}/models/{}/versions/{}'.format(PROJECT,
model_name,
version_params['name'])
if version_path in [v['name'] for v in model_versions]:
logging.info("Delete previously version of the model to overwrite.")
mle.delete_version(PROJECT, model_name, version_params['name'])
mle.create_version(PROJECT, model_name, version_params)
# Checks if model exists using Hook instead of GCP operators due to conditional.
t10 = PythonOperator(
task_id='list_versions', dag=dag, python_callable=do_list_versions)
# Creates model if it does not exist using Hook instead of GCP operators
t11 = PythonOperator(
task_id='create_version', dag=dag, python_callable=do_create_version)
# Create task graph
t1.set_downstream(t2)
t2.set_downstream(t3)
t3.set_downstream(t4_train_cond)
t4_train_cond.set_downstream([t4_ml_engine, t4_automl])
t4_ml_engine.set_downstream([t4a, t4b, t4c])
t4_ml_engine.set_downstream(t5d)
t4a.set_downstream(t5a)
t4b.set_downstream(t5b)
t4c.set_downstream(t5c)
t6.set_upstream([t5a, t5b, t5c, t5d])
t6.set_downstream(t7)
t7.set_downstream(t8)
t9.set_upstream(t8)
t9.set_downstream(t10)
t10.set_downstream(t11)