-
Notifications
You must be signed in to change notification settings - Fork 127
/
runner.py
92 lines (73 loc) · 2.87 KB
/
runner.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
# Copyright 2021 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.
"""Define KubeflowV2DagRunner to run the training pipeline using Managed Pipelines."""
import os
from kfp.v2.google.client import AIPlatformClient
from tfx.orchestration import data_types
from tfx.orchestration.kubeflow.v2 import kubeflow_v2_dag_runner
from src.tfx_pipelines import config, training_pipeline, prediction_pipeline
from src.model_training import defaults
def compile_training_pipeline(pipeline_definition_file):
pipeline_root = os.path.join(
config.ARTIFACT_STORE_URI,
config.PIPELINE_NAME,
)
managed_pipeline = training_pipeline.create_pipeline(
pipeline_root=pipeline_root,
num_epochs=data_types.RuntimeParameter(
name="num_epochs",
default=defaults.NUM_EPOCHS,
ptype=int,
),
batch_size=data_types.RuntimeParameter(
name="batch_size",
default=defaults.BATCH_SIZE,
ptype=int,
),
learning_rate=data_types.RuntimeParameter(
name="learning_rate",
default=defaults.LEARNING_RATE,
ptype=float,
),
hidden_units=data_types.RuntimeParameter(
name="hidden_units",
default=",".join(str(u) for u in defaults.HIDDEN_UNITS),
ptype=str,
),
)
runner = kubeflow_v2_dag_runner.KubeflowV2DagRunner(
config=kubeflow_v2_dag_runner.KubeflowV2DagRunnerConfig(
default_image=config.TFX_IMAGE_URI
),
output_filename=pipeline_definition_file,
)
return runner.run(managed_pipeline, write_out=True)
def compile_prediction_pipeline(pipeline_definition_file):
pipeline_root = os.path.join(
config.ARTIFACT_STORE_URI,
config.PIPELINE_NAME,
)
managed_pipeline = prediction_pipeline.create_pipeline(
pipeline_root=pipeline_root,
)
runner = kubeflow_v2_dag_runner.KubeflowV2DagRunner(
config=kubeflow_v2_dag_runner.KubeflowV2DagRunnerConfig(
default_image=config.TFX_IMAGE_URI
),
output_filename=pipeline_definition_file,
)
return runner.run(managed_pipeline, write_out=True)
def submit_pipeline(pipeline_definition_file):
pipeline_client = AIPlatformClient(project_id=config.PROJECT, region=config.REGION)
pipeline_client.create_run_from_job_spec(pipeline_definition_file)