/
base.py
1057 lines (878 loc) · 38 KB
/
base.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- 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.
#
import abc
from concurrent import futures
import datetime
import functools
import inspect
import logging
import sys
import threading
from typing import (
Any,
Callable,
Dict,
List,
Iterable,
Optional,
Sequence,
Tuple,
Type,
Union,
)
import proto
from google.api_core import operation
from google.auth import credentials as auth_credentials
from google.cloud.aiplatform import initializer
from google.cloud.aiplatform import utils
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
class Logger:
"""Logging wrapper class with high level helper methods."""
def __init__(self, name: str = ""):
"""Initializes logger with name.
Args:
name (str): Name to associate with logger.
"""
self._logger = logging.getLogger(name)
def log_create_with_lro(
self,
cls: Type["VertexAiResourceNoun"],
lro: Optional[operation.Operation] = None,
):
"""Logs create event with LRO.
Args:
cls (VertexAiResourceNoun):
Vertex AI Resource Noun class that is being created.
lro (operation.Operation):
Optional. Backing LRO for creation.
"""
self._logger.info(f"Creating {cls.__name__}")
if lro:
self._logger.info(
f"Create {cls.__name__} backing LRO: {lro.operation.name}"
)
def log_create_complete(
self,
cls: Type["VertexAiResourceNoun"],
resource: proto.Message,
variable_name: str,
):
"""Logs create event is complete.
Will also include code snippet to instantiate resource in SDK.
Args:
cls (VertexAiResourceNoun):
Vertex AI Resource Noun class that is being created.
resource (proto.Message):
Vertex AI Resourc proto.Message
variable_name (str): Name of variable to use for code snippet
"""
self._logger.info(f"{cls.__name__} created. Resource name: {resource.name}")
self._logger.info(f"To use this {cls.__name__} in another session:")
self._logger.info(
f"{variable_name} = aiplatform.{cls.__name__}('{resource.name}')"
)
def log_create_complete_with_getter(
self,
cls: Type["VertexAiResourceNoun"],
resource: proto.Message,
variable_name: str,
):
"""Logs create event is complete.
Will also include code snippet to instantiate resource in SDK.
Args:
cls (VertexAiResourceNoun):
Vertex AI Resource Noun class that is being created.
resource (proto.Message):
Vertex AI Resourc proto.Message
variable_name (str): Name of variable to use for code snippet
"""
self._logger.info(f"{cls.__name__} created. Resource name: {resource.name}")
self._logger.info(f"To use this {cls.__name__} in another session:")
self._logger.info(
f"{variable_name} = aiplatform.{cls.__name__}.get('{resource.name}')"
)
def log_action_start_against_resource(
self, action: str, noun: str, resource_noun_obj: "VertexAiResourceNoun"
):
"""Logs intention to start an action against a resource.
Args:
action (str): Action to complete against the resource ie: "Deploying". Can be empty string.
noun (str): Noun the action acts on against the resource. Can be empty string.
resource_noun_obj (VertexAiResourceNoun):
Resource noun object the action is acting against.
"""
self._logger.info(
f"{action} {resource_noun_obj.__class__.__name__} {noun}: {resource_noun_obj.resource_name}"
)
def log_action_started_against_resource_with_lro(
self,
action: str,
noun: str,
cls: Type["VertexAiResourceNoun"],
lro: operation.Operation,
):
"""Logs an action started against a resource with lro.
Args:
action (str): Action started against resource. ie: "Deploy". Can be empty string.
noun (str): Noun the action acts on against the resource. Can be empty string.
cls (VertexAiResourceNoun):
Resource noun object the action is acting against.
lro (operation.Operation): Backing LRO for action.
"""
self._logger.info(
f"{action} {cls.__name__} {noun} backing LRO: {lro.operation.name}"
)
def log_action_completed_against_resource(
self, noun: str, action: str, resource_noun_obj: "VertexAiResourceNoun"
):
"""Logs action completed against resource.
Args:
noun (str): Noun the action acts on against the resource. Can be empty string.
action (str): Action started against resource. ie: "Deployed". Can be empty string.
resource_noun_obj (VertexAiResourceNoun):
Resource noun object the action is acting against
"""
self._logger.info(
f"{resource_noun_obj.__class__.__name__} {noun} {action}. Resource name: {resource_noun_obj.resource_name}"
)
def __getattr__(self, attr: str):
"""Forward remainder of logging to underlying logger."""
return getattr(self._logger, attr)
_LOGGER = Logger(__name__)
class FutureManager(metaclass=abc.ABCMeta):
"""Tracks concurrent futures against this object."""
def __init__(self):
self.__latest_future_lock = threading.Lock()
# Always points to the latest future. All submitted futures will always
# form a dependency on the latest future.
self.__latest_future = None
# Caches Exception of any executed future. Once one exception occurs
# all additional futures should fail and any additional invocations will block.
self._exception = None
def _raise_future_exception(self):
"""Raises exception if one of the object's futures has raised."""
with self.__latest_future_lock:
if self._exception:
raise self._exception
def _complete_future(self, future: futures.Future):
"""Checks for exception of future and removes the pointer if it's still
latest.
Args:
future (futures.Future): Required. A future to complete.
"""
with self.__latest_future_lock:
try:
future.result() # raises
except Exception as e:
self._exception = e
if self.__latest_future is future:
self.__latest_future = None
def _are_futures_done(self) -> bool:
"""Helper method to check to all futures are complete.
Returns:
True if no latest future.
"""
with self.__latest_future_lock:
return self.__latest_future is None
def wait(self):
"""Helper method to that blocks until all futures are complete."""
future = self.__latest_future
if future:
futures.wait([future], return_when=futures.FIRST_EXCEPTION)
self._raise_future_exception()
@property
def _latest_future(self) -> Optional[futures.Future]:
"""Get the latest future if it exists."""
with self.__latest_future_lock:
return self.__latest_future
@_latest_future.setter
def _latest_future(self, future: Optional[futures.Future]):
"""Optionally set the latest future and add a complete_future
callback."""
with self.__latest_future_lock:
self.__latest_future = future
if future:
future.add_done_callback(self._complete_future)
def _submit(
self,
method: Callable[..., Any],
args: Sequence[Any],
kwargs: Dict[str, Any],
additional_dependencies: Optional[Sequence[futures.Future]] = None,
callbacks: Optional[Sequence[Callable[[futures.Future], Any]]] = None,
internal_callbacks: Iterable[Callable[[Any], Any]] = None,
) -> futures.Future:
"""Submit a method as a future against this object.
Args:
method (Callable): Required. The method to submit.
args (Sequence): Required. The arguments to call the method with.
kwargs (dict): Required. The keyword arguments to call the method with.
additional_dependencies (Optional[Sequence[futures.Future]]):
Optional. Additional dependent futures to wait on before executing
method. Note: No validation is done on the dependencies.
callbacks (Optional[Sequence[Callable[[futures.Future], Any]]]):
Optional. Additional Future callbacks to execute once this created
Future is complete.
Returns:
future (Future): Future of the submitted method call.
"""
def wait_for_dependencies_and_invoke(
deps: Sequence[futures.Future],
method: Callable[..., Any],
args: Sequence[Any],
kwargs: Dict[str, Any],
internal_callbacks: Iterable[Callable[[Any], Any]],
) -> Any:
"""Wrapper method to wait on any dependencies before submitting
method.
Args:
deps (Sequence[futures.Future]):
Required. Dependent futures to wait on before executing method.
Note: No validation is done on the dependencies.
method (Callable): Required. The method to submit.
args (Sequence[Any]): Required. The arguments to call the method with.
kwargs (Dict[str, Any]):
Required. The keyword arguments to call the method with.
internal_callbacks: (Callable[[Any], Any]):
Callbacks that take the result of method.
"""
for future in set(deps):
future.result()
result = method(*args, **kwargs)
# call callbacks from within future
if internal_callbacks:
for callback in internal_callbacks:
callback(result)
return result
# Retrieves any dependencies from arguments.
deps = [
arg._latest_future
for arg in list(args) + list(kwargs.values())
if isinstance(arg, FutureManager)
]
# Retrieves exceptions and raises
# if any upstream dependency has an exception
exceptions = [
arg._exception
for arg in list(args) + list(kwargs.values())
if isinstance(arg, FutureManager) and arg._exception
]
if exceptions:
raise exceptions[0]
# filter out objects that do not have pending tasks
deps = [dep for dep in deps if dep]
if additional_dependencies:
deps.extend(additional_dependencies)
with self.__latest_future_lock:
# form a dependency on the latest future of this object
if self.__latest_future:
deps.append(self.__latest_future)
self.__latest_future = initializer.global_pool.submit(
wait_for_dependencies_and_invoke,
deps=deps,
method=method,
args=args,
kwargs=kwargs,
internal_callbacks=internal_callbacks,
)
future = self.__latest_future
# Clean up callback captures exception as well as removes future.
# May execute immediately and take lock.
future.add_done_callback(self._complete_future)
if callbacks:
for c in callbacks:
future.add_done_callback(c)
return future
@classmethod
@abc.abstractmethod
def _empty_constructor(cls) -> "FutureManager":
"""Should construct object with all non FutureManager attributes as
None."""
pass
@abc.abstractmethod
def _sync_object_with_future_result(self, result: "FutureManager"):
"""Should sync the object from _empty_constructor with result of
future."""
def __repr__(self) -> str:
if self._exception:
return f"{object.__repr__(self)} failed with {str(self._exception)}"
if self.__latest_future:
return f"{object.__repr__(self)} is waiting for upstream dependencies to complete."
return object.__repr__(self)
class VertexAiResourceNoun(metaclass=abc.ABCMeta):
"""Base class the Vertex AI resource nouns.
Subclasses require two class attributes:
client_class: The client to instantiate to interact with this resource noun.
_is_client_prediction_client: Flag to indicate if the client requires a prediction endpoint.
Subclass is required to populate private attribute _gca_resource which is the
service representation of the resource noun.
"""
@property
@classmethod
@abc.abstractmethod
def client_class(cls) -> Type[utils.VertexAiServiceClientWithOverride]:
"""Client class required to interact with resource with optional
overrides."""
pass
@property
@classmethod
@abc.abstractmethod
def _is_client_prediction_client(cls) -> bool:
"""Flag to indicate whether to use prediction endpoint with client."""
pass
@property
@abc.abstractmethod
def _getter_method(cls) -> str:
"""Name of getter method of client class for retrieving the
resource."""
pass
@property
@abc.abstractmethod
def _delete_method(cls) -> str:
"""Name of delete method of client class for deleting the resource."""
pass
@property
@abc.abstractmethod
def _resource_noun(cls) -> str:
"""Resource noun."""
pass
def __init__(
self,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
resource_name: Optional[str] = None,
):
"""Initializes class with project, location, and api_client.
Args:
project(str): Project of the resource noun.
location(str): The location of the resource noun.
credentials(google.auth.crendentials.Crendentials): Optional custom
credentials to use when accessing interacting with resource noun.
resource_name(str): A fully-qualified resource name or ID.
"""
if resource_name:
project, location = self._get_and_validate_project_location(
resource_name=resource_name, project=project, location=location
)
self.project = project or initializer.global_config.project
self.location = location or initializer.global_config.location
self.credentials = credentials or initializer.global_config.credentials
self.api_client = self._instantiate_client(self.location, self.credentials)
@classmethod
def _instantiate_client(
cls,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> utils.VertexAiServiceClientWithOverride:
"""Helper method to instantiate service client for resource noun.
Args:
location (str): The location of the resource noun.
credentials (google.auth.credentials.Credentials):
Optional custom credentials to use when accessing interacting with
resource noun.
Returns:
client (utils.VertexAiServiceClientWithOverride):
Initialized service client for this service noun with optional overrides.
"""
return initializer.global_config.create_client(
client_class=cls.client_class,
credentials=credentials,
location_override=location,
prediction_client=cls._is_client_prediction_client,
)
def _get_and_validate_project_location(
self,
resource_name: str,
project: Optional[str] = None,
location: Optional[str] = None,
) -> Tuple:
"""Validate the project and location for the resource.
Args:
resource_name(str): Required. A fully-qualified resource name or ID.
project(str): Project of the resource noun.
location(str): The location of the resource noun.
Raises:
RuntimeError if location is different from resource location
"""
fields = utils.extract_fields_from_resource_name(
resource_name, self._resource_noun
)
if not fields:
return project, location
if location and fields.location != location:
raise RuntimeError(
f"location {location} is provided, but different from "
f"the resource location {fields.location}"
)
return fields.project, fields.location
def _get_gca_resource(self, resource_name: str) -> proto.Message:
"""Returns GAPIC service representation of client class resource."""
"""
Args:
resource_name (str):
Required. A fully-qualified resource name or ID.
"""
resource_name = utils.full_resource_name(
resource_name=resource_name,
resource_noun=self._resource_noun,
project=self.project,
location=self.location,
)
return getattr(self.api_client, self._getter_method)(name=resource_name)
def _sync_gca_resource(self):
"""Sync GAPIC service representation of client class resource."""
self._gca_resource = self._get_gca_resource(resource_name=self.resource_name)
@property
def name(self) -> str:
"""Name of this resource."""
return self._gca_resource.name.split("/")[-1]
@property
def resource_name(self) -> str:
"""Full qualified resource name."""
return self._gca_resource.name
@property
def display_name(self) -> str:
"""Display name of this resource."""
return self._gca_resource.display_name
@property
def create_time(self) -> datetime.datetime:
"""Time this resource was created."""
return self._gca_resource.create_time
@property
def update_time(self) -> datetime.datetime:
"""Time this resource was last updated."""
self._sync_gca_resource()
return self._gca_resource.update_time
@property
def gca_resource(self) -> proto.Message:
"""The underlying resource proto represenation."""
return self._gca_resource
def __repr__(self) -> str:
return f"{object.__repr__(self)} \nresource name: {self.resource_name}"
def optional_sync(
construct_object_on_arg: Optional[str] = None,
return_input_arg: Optional[str] = None,
bind_future_to_self: bool = True,
):
"""Decorator for VertexAiResourceNounWithFutureManager with optional sync
support.
Methods with this decorator should include a "sync" argument that defaults to
True. If called with sync=False this decorator will launch the method as a
concurrent Future in a separate Thread.
Note that this is only robust enough to support our current end to end patterns
and may not be suitable for new patterns.
Args:
construct_object_on_arg (str):
Optional. If provided, will only construct output object if arg is present.
Example: If custom training does not produce a model.
return_input_arg (str):
Optional. If provided will return passed in argument instead of
constructing.
Example: Model.deploy(Endpoint) returns the passed in Endpoint
bind_future_to_self (bool):
Whether to add this future to the calling object.
Example: Model.deploy(Endpoint) would be set to False because we only
want the deployment Future to be associated with Endpoint.
"""
def optional_run_in_thread(method: Callable[..., Any]):
"""Optionally run this method concurrently in separate Thread.
Args:
method (Callable[..., Any]): Method to optionally run in separate Thread.
"""
@functools.wraps(method)
def wrapper(*args, **kwargs):
"""Wraps method."""
sync = kwargs.pop("sync", True)
bound_args = inspect.signature(method).bind(*args, **kwargs)
self = bound_args.arguments.get("self")
calling_object_latest_future = None
# check to see if this object has any exceptions
if self:
calling_object_latest_future = self._latest_future
self._raise_future_exception()
# if sync then wait for any Futures to complete and execute
if sync:
if self:
self.wait()
return method(*args, **kwargs)
# callbacks to call within the Future (in same Thread)
internal_callbacks = []
# callbacks to add to the Future (may or may not be in same Thread)
callbacks = []
# additional Future dependencies to capture
dependencies = []
# all methods should have type signatures
return_type = get_annotation_class(
inspect.getfullargspec(method).annotations["return"]
)
# is a classmethod that creates the object and returns it
if args and inspect.isclass(args[0]):
# assumes classmethod is our resource noun
returned_object = args[0]._empty_constructor()
self = returned_object
else: # instance method
# object produced by the method
returned_object = bound_args.arguments.get(return_input_arg)
# if we're returning an input object
if returned_object and returned_object is not self:
# make sure the input object doesn't have any exceptions
# from previous futures
returned_object._raise_future_exception()
# if the future will be associated with both the returned object
# and calling object then we need to add additional callback
# to remove the future from the returned object
# if we need to construct a new empty returned object
should_construct = not returned_object and bound_args.arguments.get(
construct_object_on_arg, not construct_object_on_arg
)
if should_construct:
if return_type is not None:
returned_object = return_type._empty_constructor()
# if the future will be associated with both the returned object
# and calling object then we need to add additional callback
# to remove the future from the returned object
if returned_object and bind_future_to_self:
callbacks.append(returned_object._complete_future)
if returned_object:
# sync objects after future completes
internal_callbacks.append(
returned_object._sync_object_with_future_result
)
# If the future is not associated with the calling object
# then the return object future needs to form a dependency on the
# the latest future in the calling object.
if not bind_future_to_self:
if calling_object_latest_future:
dependencies.append(calling_object_latest_future)
self = returned_object
future = self._submit(
method=method,
callbacks=callbacks,
internal_callbacks=internal_callbacks,
additional_dependencies=dependencies,
args=[],
kwargs=bound_args.arguments,
)
# if the calling object is the one that submitted then add it's future
# to the returned object
if returned_object and returned_object is not self:
returned_object._latest_future = future
return returned_object
return wrapper
return optional_run_in_thread
class VertexAiResourceNounWithFutureManager(VertexAiResourceNoun, FutureManager):
"""Allows optional asynchronous calls to this Vertex AI Resource
Nouns."""
def __init__(
self,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
resource_name: Optional[str] = None,
):
"""Initializes class with project, location, and api_client.
Args:
project (str): Optional. Project of the resource noun.
location (str): Optional. The location of the resource noun.
credentials(google.auth.crendentials.Crendentials):
Optional. custom credentials to use when accessing interacting with
resource noun.
resource_name(str): A fully-qualified resource name or ID.
"""
VertexAiResourceNoun.__init__(
self,
project=project,
location=location,
credentials=credentials,
resource_name=resource_name,
)
FutureManager.__init__(self)
@classmethod
def _empty_constructor(
cls,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
resource_name: Optional[str] = None,
) -> "VertexAiResourceNounWithFutureManager":
"""Initializes with all attributes set to None.
The attributes should be populated after a future is complete. This allows
scheduling of additional API calls before the resource is created.
Args:
project (str): Optional. Project of the resource noun.
location (str): Optional. The location of the resource noun.
credentials(google.auth.crendentials.Crendentials):
Optional. custom credentials to use when accessing interacting with
resource noun.
resource_name(str): A fully-qualified resource name or ID.
Returns:
An instance of this class with attributes set to None.
"""
self = cls.__new__(cls)
VertexAiResourceNoun.__init__(
self,
project=project,
location=location,
credentials=credentials,
resource_name=resource_name,
)
FutureManager.__init__(self)
self._gca_resource = None
return self
def _sync_object_with_future_result(
self, result: "VertexAiResourceNounWithFutureManager"
):
"""Populates attributes from a Future result to this object.
Args:
result: VertexAiResourceNounWithFutureManager
Required. Result of future with same type as this object.
"""
sync_attributes = [
"project",
"location",
"api_client",
"_gca_resource",
"credentials",
]
optional_sync_attributes = ["_prediction_client"]
for attribute in sync_attributes:
setattr(self, attribute, getattr(result, attribute))
for attribute in optional_sync_attributes:
value = getattr(result, attribute, None)
if value:
setattr(self, attribute, value)
def _construct_sdk_resource_from_gapic(
self,
gapic_resource: proto.Message,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> VertexAiResourceNoun:
"""Given a GAPIC resource object, return the SDK representation.
Args:
gapic_resource (proto.Message):
A GAPIC representation of an Vertex AI resource, usually
retrieved by a get_* or in a list_* API call.
project (str):
Optional. Project to construct SDK object from. If not set,
project set in aiplatform.init will be used.
location (str):
Optional. Location to construct SDK object from. If not set,
location set in aiplatform.init will be used.
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to construct SDK object.
Overrides credentials set in aiplatform.init.
Returns:
VertexAiResourceNoun:
An initialized SDK object that represents GAPIC type.
"""
sdk_resource = self._empty_constructor(
project=project, location=location, credentials=credentials
)
sdk_resource._gca_resource = gapic_resource
return sdk_resource
# TODO(b/144545165): Improve documentation for list filtering once available
# TODO(b/184910159): Expose `page_size` field in list method
@classmethod
def _list(
cls,
cls_filter: Callable[[proto.Message], bool] = lambda _: True,
filter: Optional[str] = None,
order_by: Optional[str] = None,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> List[VertexAiResourceNoun]:
"""Private method to list all instances of this Vertex AI Resource,
takes a `cls_filter` arg to filter to a particular SDK resource
subclass.
Args:
cls_filter (Callable[[proto.Message], bool]):
A function that takes one argument, a GAPIC resource, and returns
a bool. If the function returns False, that resource will be
excluded from the returned list. Example usage:
cls_filter = lambda obj: obj.metadata in cls.valid_metadatas
filter (str):
Optional. An expression for filtering the results of the request.
For field names both snake_case and camelCase are supported.
order_by (str):
Optional. A comma-separated list of fields to order by, sorted in
ascending order. Use "desc" after a field name for descending.
Supported fields: `display_name`, `create_time`, `update_time`
project (str):
Optional. Project to retrieve list from. If not set, project
set in aiplatform.init will be used.
location (str):
Optional. Location to retrieve list from. If not set, location
set in aiplatform.init will be used.
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve list. Overrides
credentials set in aiplatform.init.
Returns:
List[VertexAiResourceNoun] - A list of SDK resource objects
"""
self = cls._empty_constructor(
project=project, location=location, credentials=credentials
)
# Fetch credentials once and re-use for all `_empty_constructor()` calls
creds = initializer.global_config.credentials
resource_list_method = getattr(self.api_client, self._list_method)
list_request = {
"parent": initializer.global_config.common_location_path(
project=project, location=location
),
"filter": filter,
}
if order_by:
list_request["order_by"] = order_by
resource_list = resource_list_method(request=list_request) or []
return [
self._construct_sdk_resource_from_gapic(
gapic_resource, project=project, location=location, credentials=creds
)
for gapic_resource in resource_list
if cls_filter(gapic_resource)
]
@classmethod
def _list_with_local_order(
cls,
cls_filter: Callable[[proto.Message], bool] = lambda _: True,
filter: Optional[str] = None,
order_by: Optional[str] = None,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> List[VertexAiResourceNoun]:
"""Private method to list all instances of this Vertex AI Resource,
takes a `cls_filter` arg to filter to a particular SDK resource
subclass. Provides client-side sorting when a list API doesn't support
`order_by`.
Args:
cls_filter (Callable[[proto.Message], bool]):
A function that takes one argument, a GAPIC resource, and returns
a bool. If the function returns False, that resource will be
excluded from the returned list. Example usage:
cls_filter = lambda obj: obj.metadata in cls.valid_metadatas
filter (str):
Optional. An expression for filtering the results of the request.
For field names both snake_case and camelCase are supported.
order_by (str):
Optional. A comma-separated list of fields to order by, sorted in
ascending order. Use "desc" after a field name for descending.
Supported fields: `display_name`, `create_time`, `update_time`
project (str):
Optional. Project to retrieve list from. If not set, project
set in aiplatform.init will be used.
location (str):
Optional. Location to retrieve list from. If not set, location
set in aiplatform.init will be used.
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve list. Overrides
credentials set in aiplatform.init.
Returns:
List[VertexAiResourceNoun] - A list of SDK resource objects
"""
li = cls._list(
cls_filter=cls_filter,
filter=filter,
order_by=None, # This method will handle the ordering locally
project=project,
location=location,
credentials=credentials,
)
if order_by:
desc = "desc" in order_by
order_by = order_by.replace("desc", "")
order_by = order_by.split(",")
li.sort(
key=lambda x: tuple(getattr(x, field.strip()) for field in order_by),
reverse=desc,
)
return li
@classmethod
def list(
cls,
filter: Optional[str] = None,
order_by: Optional[str] = None,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> List[VertexAiResourceNoun]:
"""List all instances of this Vertex AI Resource.
Example Usage:
aiplatform.BatchPredictionJobs.list(
filter='state="JOB_STATE_SUCCEEDED" AND display_name="my_job"',
)
aiplatform.Model.list(order_by="create_time desc, display_name")
Args:
filter (str):
Optional. An expression for filtering the results of the request.
For field names both snake_case and camelCase are supported.
order_by (str):
Optional. A comma-separated list of fields to order by, sorted in
ascending order. Use "desc" after a field name for descending.
Supported fields: `display_name`, `create_time`, `update_time`
project (str):
Optional. Project to retrieve list from. If not set, project