/
initializer.py
294 lines (252 loc) · 10.8 KB
/
initializer.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
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
from concurrent import futures
import logging
import pkg_resources
import os
from typing import Optional, Type, Union
from google.api_core import client_options
from google.api_core import gapic_v1
import google.auth
from google.auth import credentials as auth_credentials
from google.auth.exceptions import GoogleAuthError
from google.cloud.aiplatform import compat
from google.cloud.aiplatform import constants
from google.cloud.aiplatform import utils
from google.cloud.aiplatform.metadata import metadata
from google.cloud.aiplatform.compat.types import (
encryption_spec as gca_encryption_spec_compat,
encryption_spec_v1 as gca_encryption_spec_v1,
encryption_spec_v1beta1 as gca_encryption_spec_v1beta1,
)
class _Config:
"""Stores common parameters and options for API calls."""
def __init__(self):
self._project = None
self._location = None
self._staging_bucket = None
self._credentials = None
self._encryption_spec_key_name = None
def init(
self,
*,
project: Optional[str] = None,
location: Optional[str] = None,
experiment: Optional[str] = None,
experiment_description: Optional[str] = None,
staging_bucket: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
encryption_spec_key_name: Optional[str] = None,
):
"""Updates common initialization parameters with provided options.
Args:
project (str): The default project to use when making API calls.
location (str): The default location to use when making API calls. If not
set defaults to us-central-1.
experiment (str): The experiment name.
experiment_description (str): The description of the experiment.
staging_bucket (str): The default staging bucket to use to stage artifacts
when making API calls. In the form gs://...
credentials (google.auth.credentials.Credentials): The default custom
credentials to use when making API calls. If not provided credentials
will be ascertained from the environment.
encryption_spec_key_name (Optional[str]):
Optional. The Cloud KMS resource identifier of the customer
managed encryption key used to protect a resource. Has the
form:
``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``.
The key needs to be in the same region as where the compute
resource is created.
If set, this resource and all sub-resources will be secured by this key.
"""
# reset metadata_service config if project or location is updated.
if (project and project != self._project) or (
location and location != self._location
):
if metadata.metadata_service.experiment_name:
logging.info("project/location updated, reset Metadata config.")
metadata.metadata_service.reset()
if project:
self._project = project
if location:
utils.validate_region(location)
self._location = location
if staging_bucket:
self._staging_bucket = staging_bucket
if credentials:
self._credentials = credentials
if encryption_spec_key_name:
self._encryption_spec_key_name = encryption_spec_key_name
if experiment:
metadata.metadata_service.set_experiment(
experiment=experiment, description=experiment_description
)
if experiment_description and experiment is None:
raise ValueError(
"Experiment name needs to be set in `init` in order to add experiment descriptions."
)
def get_encryption_spec(
self,
encryption_spec_key_name: Optional[str],
select_version: Optional[str] = compat.DEFAULT_VERSION,
) -> Optional[
Union[
gca_encryption_spec_v1.EncryptionSpec,
gca_encryption_spec_v1beta1.EncryptionSpec,
]
]:
"""Creates a gca_encryption_spec.EncryptionSpec instance from the given
key name. If the provided key name is None, it uses the default key
name if provided.
Args:
encryption_spec_key_name (Optional[str]): The default encryption key name to use when creating resources.
select_version: The default version is set to compat.DEFAULT_VERSION
"""
kms_key_name = encryption_spec_key_name or self.encryption_spec_key_name
encryption_spec = None
if kms_key_name:
gca_encryption_spec = gca_encryption_spec_compat
if select_version == compat.V1BETA1:
gca_encryption_spec = gca_encryption_spec_v1beta1
encryption_spec = gca_encryption_spec.EncryptionSpec(
kms_key_name=kms_key_name
)
return encryption_spec
@property
def project(self) -> str:
"""Default project."""
if self._project:
return self._project
project_not_found_exception_str = (
"Unable to find your project. Please provide a project ID by:"
"\n- Passing a constructor argument"
"\n- Using aiplatform.init()"
"\n- Setting a GCP environment variable"
)
try:
_, project_id = google.auth.default()
except GoogleAuthError:
raise GoogleAuthError(project_not_found_exception_str)
if not project_id:
raise ValueError(project_not_found_exception_str)
return project_id
@property
def location(self) -> str:
"""Default location."""
return self._location or constants.DEFAULT_REGION
@property
def staging_bucket(self) -> Optional[str]:
"""Default staging bucket, if provided."""
return self._staging_bucket
@property
def credentials(self) -> Optional[auth_credentials.Credentials]:
"""Default credentials."""
if self._credentials:
return self._credentials
logger = logging.getLogger("google.auth._default")
logging_warning_filter = utils.LoggingFilter(logging.WARNING)
logger.addFilter(logging_warning_filter)
credentials, _ = google.auth.default()
logger.removeFilter(logging_warning_filter)
return credentials
@property
def encryption_spec_key_name(self) -> Optional[str]:
"""Default encryption spec key name, if provided."""
return self._encryption_spec_key_name
def get_client_options(
self, location_override: Optional[str] = None
) -> client_options.ClientOptions:
"""Creates GAPIC client_options using location and type.
Args:
location_override (str):
Set this parameter to get client options for a location different from
location set by initializer. Must be a GCP region supported by AI
Platform (Unified).
Returns:
clients_options (google.api_core.client_options.ClientOptions):
A ClientOptions object set with regionalized API endpoint, i.e.
{ "api_endpoint": "us-central1-aiplatform.googleapis.com" } or
{ "api_endpoint": "asia-east1-aiplatform.googleapis.com" }
"""
if not (self.location or location_override):
raise ValueError(
"No location found. Provide or initialize SDK with a location."
)
region = location_override or self.location
region = region.lower()
utils.validate_region(region)
return client_options.ClientOptions(
api_endpoint=f"{region}-{constants.API_BASE_PATH}"
)
def common_location_path(
self, project: Optional[str] = None, location: Optional[str] = None
) -> str:
"""Get parent resource with optional project and location override.
Args:
project (str): GCP project. If not provided will use the current project.
location (str): Location. If not provided will use the current location.
Returns:
resource_parent: Formatted parent resource string.
"""
if location:
utils.validate_region(location)
return "/".join(
[
"projects",
project or self.project,
"locations",
location or self.location,
]
)
def create_client(
self,
client_class: Type[utils.VertexAiServiceClientWithOverride],
credentials: Optional[auth_credentials.Credentials] = None,
location_override: Optional[str] = None,
prediction_client: bool = False,
) -> utils.VertexAiServiceClientWithOverride:
"""Instantiates a given VertexAiServiceClient with optional
overrides.
Args:
client_class (utils.VertexAiServiceClientWithOverride):
(Required) An Vertex AI Service Client with optional overrides.
credentials (auth_credentials.Credentials):
Custom auth credentials. If not provided will use the current config.
location_override (str): Optional location override.
prediction_client (str): Optional flag to use a prediction endpoint.
Returns:
client: Instantiated Vertex AI Service client with optional overrides
"""
gapic_version = pkg_resources.get_distribution(
"google-cloud-aiplatform",
).version
client_info = gapic_v1.client_info.ClientInfo(
gapic_version=gapic_version,
user_agent=f"{constants.USER_AGENT_PRODUCT}/{gapic_version}",
)
kwargs = {
"credentials": credentials or self.credentials,
"client_options": self.get_client_options(
location_override=location_override
),
"client_info": client_info,
}
return client_class(**kwargs)
# global config to store init parameters: ie, aiplatform.init(project=..., location=...)
global_config = _Config()
global_pool = futures.ThreadPoolExecutor(
max_workers=min(32, max(4, (os.cpu_count() or 0) * 5))
)