-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_deployer.py
379 lines (326 loc) · 15.2 KB
/
pipeline_deployer.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
from __future__ import annotations
import os
from datetime import datetime
from pathlib import Path
from typing import Callable, List, Optional
from google.cloud import aiplatform
from google.cloud.aiplatform import PipelineJobSchedule
from kfp import compiler
from kfp.registry import RegistryClient
from loguru import logger
from requests import HTTPError
from deployer import constants
from deployer.utils.exceptions import (
MissingGoogleArtifactRegistryHostError,
TagNotFoundError,
)
class VertexPipelineDeployer:
"""Deployer for Vertex Pipelines"""
def __init__(
self,
pipeline_name: str,
pipeline_func: Callable,
run_name: Optional[str] = None,
project_id: Optional[str] = None,
region: Optional[str] = None,
staging_bucket_name: Optional[str] = None,
service_account: Optional[str] = None,
gar_location: Optional[str] = None,
gar_repo_id: Optional[str] = None,
local_package_path: Optional[Path] = None,
) -> None:
"""I don't want to write a dostring here but ruff wants me to"""
self.project_id = project_id
self.region = region
self.staging_bucket_name = staging_bucket_name
self.service_account = service_account
self.pipeline_name = pipeline_name
self.run_name = run_name
self.pipeline_func = pipeline_func
self.gar_location = gar_location
self.gar_repo_id = gar_repo_id
self.local_package_path = Path(local_package_path)
self.template_name = None
self.version_name = None
aiplatform.init(
project=self.project_id,
staging_bucket=f"gs://{self.staging_bucket_name}",
)
@property
def gar_host(self) -> Optional[str]:
"""Return the Artifact Registry host if the location and repo ID are provided"""
if self.gar_location is not None and self.gar_repo_id is not None:
return os.path.join(
f"https://{self.gar_location}-kfp.pkg.dev", self.project_id, self.gar_repo_id
)
logger.debug(
"No Artifact Registry location or repo ID provided: not using Artifact Registry"
)
return None
@property
def staging_bucket_uri(self) -> str: # noqa: D102
return f"gs://{self.staging_bucket_name}/root"
def _get_template_path(self, tag: Optional[str] = None) -> str:
"""Return the path to the pipeline template
If the Artifact Registry host is provided, return the path to the pipeline template in
the Artifact Registry. Otherwise, return the path to the pipeline template in the
local package.
"""
if self.gar_host is not None:
if self.template_name is not None and self.version_name is not None:
return os.path.join(self.gar_host, self.template_name, self.version_name)
if tag:
return os.path.join(self.gar_host, self.pipeline_name.replace("_", "-"), tag)
logger.warning(
"tag or template_name and version_name not provided."
" Falling back to local package."
)
return os.path.join(str(self.local_package_path), f"{self.pipeline_name}.yaml")
def _check_gar_host(self) -> None:
if self.gar_host is None:
raise MissingGoogleArtifactRegistryHostError(
"Google Artifact Registry host is missing. "
"Please provide gar_location and gar_repo_id."
)
def _check_experiment_name(self, experiment_name: Optional[str] = None) -> str:
if experiment_name is None:
experiment_name = f"{self.pipeline_name}-experiment".replace("_", "-")
logger.info(f"Experiment name not provided, using {experiment_name}")
else:
experiment_name = experiment_name.replace("_", "-")
return experiment_name
def _check_run_name(self, tag: Optional[str] = None) -> None:
"""Each run name (job_id) must be unique.
We thus always add a timestamp to ensure uniqueness.
"""
now_str = datetime.now().strftime("%Y%m%d-%H%M%S")
if self.run_name is None:
self.run_name = f"{self.pipeline_name}"
if tag:
self.run_name += f"-{tag}"
self.run_name = self.run_name.replace("_", "-")
self.run_name += f"-{now_str}"
if not constants.VALID_RUN_NAME_PATTERN.match(self.run_name):
raise ValueError(
f"Run name {self.run_name} does not match the pattern"
f" {constants.VALID_RUN_NAME_PATTERN.pattern}"
)
logger.debug(f"run_name is: {self.run_name}")
def _create_pipeline_job(
self,
template_path: str,
enable_caching: Optional[bool] = None,
parameter_values: Optional[dict] = None,
input_artifacts: Optional[dict] = None,
) -> aiplatform.PipelineJob:
"""Create a pipeline job object
Args:
template_path (str): The path of PipelineJob or PipelineSpec JSON or YAML file. If the
Artifact Registry host is provided, this is the path to the pipeline template in
the Artifact Registry. Otherwise, this is the path to the pipeline template in
the local package.
enable_caching (Optional[bool], optional): Whether to turn on caching for the run.
If this is not set, defaults to the compile time settings, which are True for all
tasks by default, while users may specify different caching options for individual
tasks.
If this is set, the setting applies to all tasks in the pipeline.
Overrides the compile time settings. Defaults to None.
parameter_values (Optional[dict], optional): The mapping from runtime parameter names
to its values that control the pipeline run. Defaults to None.
input_artifacts (Optional[dict], optional): The mapping from the runtime parameter
name for this artifact to its resource id.
For example: "vertex_model":"456".
Note: full resource name ("projects/123/locations/us-central1/metadataStores/default/artifacts/456")
cannot be used. Defaults to None.
Returns:
aiplatform.PipelineJob: The pipeline job object
""" # noqa: E501
job = aiplatform.PipelineJob(
display_name=self.pipeline_name,
job_id=self.run_name,
template_path=template_path,
pipeline_root=self.staging_bucket_uri,
location=self.region,
enable_caching=enable_caching,
parameter_values=parameter_values,
input_artifacts=input_artifacts,
)
return job
def compile(self) -> VertexPipelineDeployer:
"""Compile pipeline and save it to the local package path using kfp compiler"""
self.local_package_path.mkdir(parents=True, exist_ok=True)
pipeline_filepath = self.local_package_path / f"{self.pipeline_name}.yaml"
compiler.Compiler().compile(
pipeline_func=self.pipeline_func,
package_path=str(pipeline_filepath),
)
logger.info(f"Pipeline {self.pipeline_name} compiled to {pipeline_filepath}")
return self
def upload_to_registry(
self,
tags: List[str] = ["latest"], # noqa: B006
) -> VertexPipelineDeployer:
"""Upload pipeline to Artifact Registry"""
self._check_gar_host()
client = RegistryClient(host=self.gar_host)
template_name, version_name = client.upload_pipeline(
file_name=self.local_package_path / f"{self.pipeline_name}.yaml",
tags=tags,
)
logger.info(f"Pipeline {self.pipeline_name} uploaded to {self.gar_host} with tags {tags}")
self.template_name = template_name
self.version_name = version_name
return self
def run(
self,
enable_caching: Optional[bool] = None,
parameter_values: Optional[dict] = None,
input_artifacts: Optional[dict] = None,
experiment_name: Optional[str] = None,
tag: Optional[str] = None,
) -> VertexPipelineDeployer:
"""Run pipeline on Vertex AI Pipelines
If the experiment name is not provided, use the pipeline name with the suffix
"-experiment". Compiled pipeline file is the one uploaded on artifact registry if the
host is provided, and if either the tag or the template_name and version_name are
provided. Otherwise, use the pipeline file in the local package.
Args:
enable_caching (Optional[bool], optional): Whether to turn on caching for the run.
If this is not set, defaults to the compile time settings, which are True for all
tasks by default, while users may specify different caching options for individual
tasks.
If this is set, the setting applies to all tasks in the pipeline.
Overrides the compile time settings. Defaults to None.
parameter_values (Optional[dict], optional): The mapping from runtime parameter names
to its values that control the pipeline run. Defaults to None.
input_artifacts (Optional[dict], optional): The mapping from the runtime parameter
name for this artifact to its resource id.
For example: "vertex_model":"456".
Note: full resource name ("projects/123/locations/us-central1/metadataStores/default/artifacts/456")
cannot be used. Defaults to None.
experiment_name (str, optional): Experiment name. Defaults to None.
tag (str, optional): Tag of the pipeline template. Defaults to None.
""" # noqa: E501
experiment_name = self._check_experiment_name(experiment_name)
self._check_run_name(tag=tag)
template_path = self._get_template_path(tag)
logger.debug(
f"Running pipeline '{self.pipeline_name}' with settings:"
f"\n {'template_path':<20} {template_path:<30}"
f"\n {'enable_caching':<20} {enable_caching!s:<30}"
f"\n {'experiment_name':<20} {experiment_name:<30}"
)
job = self._create_pipeline_job(
template_path=template_path,
enable_caching=enable_caching,
parameter_values=parameter_values,
input_artifacts=input_artifacts,
)
try:
job.submit(
experiment=experiment_name,
service_account=self.service_account,
)
except RuntimeError as e: # HACK: This is a temporary fix
if "could not be associated with Experiment" in str(e):
logger.warning(
f"Encountered an error while linking your job {job.job_id}"
f" with experiment {experiment_name}."
" This is likely due to a bug in the AI Platform Pipelines client."
" Your job should be running anyway. Try to link it manually."
)
else:
raise e
return self
def compile_upload_run(
self,
enable_caching: Optional[bool] = None,
parameter_values: Optional[dict] = None,
experiment_name: Optional[str] = None,
tags: Optional[List[str]] = None,
) -> VertexPipelineDeployer:
"""Compile, upload and run pipeline on Vertex AI Pipelines"""
self.compile()
if self.gar_host is not None:
self.upload_to_registry(tags)
self.run(
enable_caching=enable_caching,
parameter_values=parameter_values,
experiment_name=experiment_name,
tag=tags[0] if tags else None,
)
return self
def schedule(
self,
cron: str,
enable_caching: Optional[bool] = None,
parameter_values: Optional[dict] = None,
tag: Optional[str] = None,
delete_last_schedule: bool = False,
scheduler_timezone: str = "Europe/Paris",
) -> VertexPipelineDeployer:
"""Create pipeline schedule on Vertex AI Pipelines
Compiled pipeline file is the one uploaded on artifact registry if the host is provided,
and if either the tag or the template_name and version_name are provided.
Args:
cron (str): Cron expression without TZ.
enable_caching (bool, optional): Whether to enable caching. Defaults to False.
parameter_values (dict, optional): Pipeline parameter values. Defaults to None.
tag (str, optional): Tag of the pipeline template. Defaults to None.
delete_last_schedule (bool, optional): Whether to delete previous schedule.
Defaults to False.
scheduler_timezone (str, optional): Scheduler timezone. Must be a valid string from
IANA time zone database. Defaults to 'Europe/Paris'.
"""
self._check_gar_host()
schedule_display_name = f"schedule-{self.pipeline_name}"
schedules_list = PipelineJobSchedule.list(
filter=f'display_name="{schedule_display_name}"',
order_by="create_time desc",
location=self.region,
)
logger.info(
f"There are {len(schedules_list)} schedules defined for pipeline {self.pipeline_name}"
)
if len(schedules_list) > 0 and delete_last_schedule:
logger.info(
f"Deleting schedule {schedules_list[0].display_name}"
f" for pipeline {self.pipeline_name} at {schedules_list[0].cron}"
)
schedules_list[0].delete()
if tag:
client = RegistryClient(host=self.gar_host)
package_name = self.pipeline_name.replace("_", "-")
try:
tag_metadata = client.get_tag(package_name=package_name, tag=tag)
except HTTPError as e:
tags_list = client.list_tags(package_name)
tags_list_parsed = [x["name"].split("/")[-1] for x in tags_list]
raise TagNotFoundError(
f"Tag {tag} not found for package {self.gar_host}/{package_name}.\
Available tags: {tags_list_parsed}"
) from e
pipeline_version_sha = tag_metadata["version"].split("/")[-1]
template_path = self._get_template_path(pipeline_version_sha)
else:
template_path = self._get_template_path()
logger.info(
f"Creating schedule for pipeline {self.pipeline_name} at {cron}"
f" with template {template_path}"
)
job = self._create_pipeline_job(
template_path=template_path,
enable_caching=enable_caching,
parameter_values=parameter_values,
)
# HACK: Must set location or it will default to "us-central1" (or project default)
pipeline_job_schedule = PipelineJobSchedule(
pipeline_job=job,
display_name=schedule_display_name,
location=self.region,
)
pipeline_job_schedule.create(
cron=f"TZ={scheduler_timezone} {cron}",
service_account=self.service_account,
)
return self