This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
aiplatform.py
422 lines (373 loc) · 15.4 KB
/
aiplatform.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
"""
<span class="badge-api experimental"/>
Integrations with Google AI Platform.
Note this module is experimental. The intefaces within may change without notice.
Examples:
Run a job using Vertex AI Custom Training:
```python
from prefect_gcp.credentials import GcpCredentials
from prefect_gcp.aiplatform import VertexAICustomTrainingJob
gcp_credentials = GcpCredentials.load("BLOCK_NAME")
job = VertexAICustomTrainingJob(
region="us-east1",
image="us-docker.pkg.dev/cloudrun/container/job:latest",
gcp_credentials=gcp_credentials,
)
job.run()
```
Run a job that runs the command `echo hello world` using Google Cloud Run Jobs:
```python
from prefect_gcp.credentials import GcpCredentials
from prefect_gcp.aiplatform import VertexAICustomTrainingJob
gcp_credentials = GcpCredentials.load("BLOCK_NAME")
job = VertexAICustomTrainingJob(
command=["echo", "hello world"],
region="us-east1",
image="us-docker.pkg.dev/cloudrun/container/job:latest",
gcp_credentials=gcp_credentials,
)
job.run()
```
Preview job specs:
```python
from prefect_gcp.credentials import GcpCredentials
from prefect_gcp.aiplatform import VertexAICustomTrainingJob
gcp_credentials = GcpCredentials.load("BLOCK_NAME")
job = VertexAICustomTrainingJob(
command=["echo", "hello world"],
region="us-east1",
image="us-docker.pkg.dev/cloudrun/container/job:latest",
gcp_credentials=gcp_credentials,
)
job.preview()
```
"""
import datetime
import time
from typing import Dict, List, Optional, Tuple
from uuid import uuid4
from anyio.abc import TaskStatus
from prefect.exceptions import InfrastructureNotFound
from prefect.infrastructure import Infrastructure, InfrastructureResult
from prefect.utilities.asyncutils import run_sync_in_worker_thread, sync_compatible
from pydantic import Field
from typing_extensions import Literal
# to prevent "Failed to load collection" from surfacing
# if google-cloud-aiplatform is not installed
try:
from google.api_core.client_options import ClientOptions
from google.cloud.aiplatform.gapic import JobServiceClient
from google.cloud.aiplatform_v1.types.custom_job import (
ContainerSpec,
CustomJob,
CustomJobSpec,
Scheduling,
WorkerPoolSpec,
)
from google.cloud.aiplatform_v1.types.job_service import CancelCustomJobRequest
from google.cloud.aiplatform_v1.types.job_state import JobState
from google.cloud.aiplatform_v1.types.machine_resources import MachineSpec
from google.protobuf.duration_pb2 import Duration
except ModuleNotFoundError:
pass
from prefect_gcp.credentials import GcpCredentials
class VertexAICustomTrainingJobResult(InfrastructureResult):
"""Result from a Vertex AI custom training job."""
class VertexAICustomTrainingJob(Infrastructure):
"""
Infrastructure block used to run Vertex AI custom training jobs.
"""
_block_type_name = "Vertex AI Custom Training Job"
_block_type_slug = "vertex-ai-custom-training-job"
_logo_url = "https://images.ctfassets.net/gm98wzqotmnx/4CD4wwbiIKPkZDt4U3TEuW/c112fe85653da054b6d5334ef662bec4/gcp.png?h=250" # noqa
_documentation_url = "https://prefecthq.github.io/prefect-gcp/aiplatform/#prefect_gcp.aiplatform.VertexAICustomTrainingJob" # noqa: E501
type: Literal["vertex-ai-custom-training-job"] = Field(
"vertex-ai-custom-training-job", description="The slug for this task type."
)
gcp_credentials: GcpCredentials = Field(
default_factory=GcpCredentials,
description=(
"GCP credentials to use when running the configured Vertex AI custom "
"training job. If not provided, credentials will be inferred from the "
"environment. See `GcpCredentials` for details."
),
)
region: str = Field(
default=...,
description="The region where the Vertex AI custom training job resides.",
)
image: str = Field(
default=...,
title="Image Name",
description=(
"The image to use for a new Vertex AI custom training job. This value must "
"refer to an image within either Google Container Registry "
"or Google Artifact Registry, like `gcr.io/<project_name>/<repo>/`."
),
)
env: Dict[str, str] = Field(
default_factory=dict,
title="Environment Variables",
description="Environment variables to be passed to your Cloud Run Job.",
)
machine_type: str = Field(
default="n1-standard-4",
description="The machine type to use for the run, which controls the available "
"CPU and memory.",
)
accelerator_type: Optional[str] = Field(
default=None, description="The type of accelerator to attach to the machine."
)
maximum_run_time: datetime.timedelta = Field(
default=datetime.timedelta(days=7), description="The maximum job running time."
)
network: Optional[str] = Field(
default=None,
description="The full name of the Compute Engine network"
"to which the Job should be peered. Private services access must "
"already be configured for the network. If left unspecified, the job "
"is not peered with any network.",
)
reserved_ip_ranges: Optional[List[str]] = Field(
default=None,
description="A list of names for the reserved ip ranges under the VPC "
"network that can be used for this job. If set, we will deploy the job "
"within the provided ip ranges. Otherwise, the job will be deployed to "
"any ip ranges under the provided VPC network.",
)
service_account: Optional[str] = Field(
default=None,
description=(
"Specifies the service account to use "
"as the run-as account in Vertex AI. The agent submitting jobs must have "
"act-as permission on this run-as account. If unspecified, the AI "
"Platform Custom Code Service Agent for the CustomJob's project is "
"used. Takes precedence over the service account found in gcp_credentials, "
"and required if a service account cannot be detected in gcp_credentials."
),
)
job_watch_poll_interval: float = Field(
default=5.0,
description=(
"The amount of time to wait between GCP API calls while monitoring the "
"state of a Vertex AI Job."
),
)
@property
def job_name(self):
"""
The name can be up to 128 characters long and can be consist of any UTF-8 characters. Reference:
https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomJob#google_cloud_aiplatform_CustomJob_display_name
""" # noqa
try:
repo_name = self.image.split("/")[2] # `gcr.io/<project_name>/<repo>/`"
except IndexError:
raise ValueError(
"The provided image must be from either Google Container Registry "
"or Google Artifact Registry"
)
unique_suffix = uuid4().hex
job_name = f"{repo_name}-{unique_suffix}"
return job_name
def preview(self) -> str:
"""Generate a preview of the job definition that will be sent to GCP."""
job_spec = self._build_job_spec()
custom_job = CustomJob(display_name=self.job_name, job_spec=job_spec)
return str(custom_job) # outputs a json string
def _build_job_spec(self) -> "CustomJobSpec":
"""
Builds a job spec by gathering details.
"""
# gather worker pool spec
env_list = [{"name": name, "value": value} for name, value in self.env.items()]
container_spec = ContainerSpec(
image_uri=self.image, command=self.command, args=[], env=env_list
)
machine_spec = MachineSpec(
machine_type=self.machine_type, accelerator_type=self.accelerator_type
)
worker_pool_spec = WorkerPoolSpec(
container_spec=container_spec, machine_spec=machine_spec, replica_count=1
)
# look for service account
service_account = (
self.service_account or self.gcp_credentials._service_account_email
)
if service_account is None:
raise ValueError(
"A service account is required for the Vertex job. "
"A service account could not be detected in the attached credentials; "
"please set a service account explicitly, e.g. "
'`VertexAICustomTrainingJob(service_acount="...")`'
)
# build custom job specs
timeout = Duration().FromTimedelta(td=self.maximum_run_time)
scheduling = Scheduling(timeout=timeout)
job_spec = CustomJobSpec(
worker_pool_specs=[worker_pool_spec],
service_account=service_account,
scheduling=scheduling,
network=self.network,
reserved_ip_ranges=self.reserved_ip_ranges,
)
return job_spec
async def _create_and_begin_job(
self, job_spec: "CustomJobSpec", job_service_client: "JobServiceClient"
) -> "CustomJob":
"""
Builds a custom job and begins running it.
"""
# create custom job
custom_job = CustomJob(display_name=self.job_name, job_spec=job_spec)
# run job
self.logger.info(
f"{self._log_prefix}: Job {self.job_name!r} starting to run "
f"the command {' '.join(self.command)!r} in region "
f"{self.region!r} using image {self.image!r}"
)
project = self.gcp_credentials.project
resource_name = f"projects/{project}/locations/{self.region}"
custom_job_run = await run_sync_in_worker_thread(
job_service_client.create_custom_job,
parent=resource_name,
custom_job=custom_job,
)
self.logger.info(
f"{self._log_prefix}: Job {self.job_name!r} has successfully started; "
f"the full job name is {custom_job_run.name!r}"
)
return custom_job_run
async def _watch_job_run(
self,
full_job_name: str, # different from self.job_name
job_service_client: "JobServiceClient",
current_state: "JobState",
until_states: Tuple["JobState"],
timeout: int = None,
) -> "CustomJob":
"""
Polls job run to see if status changed.
"""
state = JobState.JOB_STATE_UNSPECIFIED
last_state = current_state
t0 = time.time()
while state not in until_states:
job_run = await run_sync_in_worker_thread(
job_service_client.get_custom_job,
name=full_job_name,
)
state = job_run.state
if state != last_state:
state_label = (
state.name.replace("_", " ")
.lower()
.replace("state", "state is now:")
)
# results in "New job state is now: succeeded"
self.logger.info(
f"{self._log_prefix}: {self.job_name} has new {state_label}"
)
last_state = state
else:
# Intermittently, the job will not be described. We want to respect the
# watch timeout though.
self.logger.debug(f"{self._log_prefix}: Job not found.")
elapsed_time = time.time() - t0
if timeout is not None and elapsed_time > timeout:
raise RuntimeError(
f"Timed out after {elapsed_time}s while watching job for states "
"{until_states!r}"
)
time.sleep(self.job_watch_poll_interval)
return job_run
@sync_compatible
async def run(
self, task_status: Optional["TaskStatus"] = None
) -> VertexAICustomTrainingJobResult:
"""
Run the configured task on VertexAI.
Args:
task_status: An optional `TaskStatus` to update when the container starts.
Returns:
The `VertexAICustomTrainingJobResult`.
"""
client_options = ClientOptions(
api_endpoint=f"{self.region}-aiplatform.googleapis.com"
)
job_spec = self._build_job_spec()
with self.gcp_credentials.get_job_service_client(
client_options=client_options
) as job_service_client:
job_run = await self._create_and_begin_job(job_spec, job_service_client)
if task_status:
task_status.started(self.job_name)
final_job_run = await self._watch_job_run(
full_job_name=job_run.name,
job_service_client=job_service_client,
current_state=job_run.state,
until_states=(
JobState.JOB_STATE_SUCCEEDED,
JobState.JOB_STATE_FAILED,
JobState.JOB_STATE_CANCELLED,
JobState.JOB_STATE_EXPIRED,
),
timeout=self.maximum_run_time.total_seconds(),
)
error_msg = final_job_run.error.message
if error_msg:
raise RuntimeError(f"{self._log_prefix}: {error_msg}")
status_code = 0 if final_job_run.state == JobState.JOB_STATE_SUCCEEDED else 1
return VertexAICustomTrainingJobResult(
identifier=final_job_run.display_name, status_code=status_code
)
@sync_compatible
async def kill(self, identifier: str, grace_seconds: int = 30) -> None:
"""
Kill a job running Cloud Run.
Args:
identifier: The Vertex AI full job name, formatted like
"projects/{project}/locations/{location}/customJobs/{custom_job}".
Returns:
The `VertexAICustomTrainingJobResult`.
"""
client_options = ClientOptions(
api_endpoint=f"{self.region}-aiplatform.googleapis.com"
)
with self.gcp_credentials.get_job_service_client(
client_options=client_options
) as job_service_client:
await run_sync_in_worker_thread(
self._kill_job,
job_service_client=job_service_client,
full_job_name=identifier,
)
self.logger.info(f"Requested to cancel {identifier}...")
def _kill_job(
self, job_service_client: "JobServiceClient", full_job_name: str
) -> None:
"""
Thin wrapper around Job.delete, wrapping a try/except since
Job is an independent class that doesn't have knowledge of
CloudRunJob and its associated logic.
"""
cancel_custom_job_request = CancelCustomJobRequest(name=full_job_name)
try:
job_service_client.cancel_custom_job(
request=cancel_custom_job_request,
)
except Exception as exc:
if "does not exist" in str(exc):
raise InfrastructureNotFound(
f"Cannot stop Vertex AI job; the job name {full_job_name!r} "
"could not be found."
) from exc
raise
@property
def _log_prefix(self) -> str:
"""
Internal property for generating a prefix for logs where `name` may be null
"""
if self.name is not None:
return f"VertexAICustomTrainingJob {self.name!r}"
else:
return "VertexAICustomTrainingJob"