Skip to content

Commit 8df74a2

Browse files
authored
Feat: Add support to create TensorboardRun (#912)
1 parent 96ce738 commit 8df74a2

5 files changed

Lines changed: 439 additions & 2 deletions

File tree

google/cloud/aiplatform/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@
4747
HyperparameterTuningJob,
4848
)
4949
from google.cloud.aiplatform.pipeline_jobs import PipelineJob
50-
from google.cloud.aiplatform.tensorboard import Tensorboard, TensorboardExperiment
50+
from google.cloud.aiplatform.tensorboard import (
51+
Tensorboard,
52+
TensorboardExperiment,
53+
TensorboardRun,
54+
)
5155
from google.cloud.aiplatform.training_jobs import (
5256
CustomTrainingJob,
5357
CustomContainerTrainingJob,
@@ -107,6 +111,7 @@
107111
"TabularDataset",
108112
"Tensorboard",
109113
"TensorboardExperiment",
114+
"TensorboardRun",
110115
"TextDataset",
111116
"TimeSeriesDataset",
112117
"VideoDataset",

google/cloud/aiplatform/tensorboard/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from google.cloud.aiplatform.tensorboard.tensorboard_resource import (
1919
Tensorboard,
2020
TensorboardExperiment,
21+
TensorboardRun,
2122
)
2223

2324

24-
__all__ = ("Tensorboard", "TensorboardExperiment")
25+
__all__ = ("Tensorboard", "TensorboardExperiment", "TensorboardRun")

google/cloud/aiplatform/tensorboard/tensorboard_resource.py

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.cloud.aiplatform.compat.types import tensorboard as gca_tensorboard
2525
from google.cloud.aiplatform.compat.types import (
2626
tensorboard_experiment as gca_tensorboard_experiment,
27+
tensorboard_run as gca_tensorboard_run,
2728
)
2829
from google.cloud.aiplatform import initializer
2930
from google.cloud.aiplatform import utils
@@ -519,3 +520,265 @@ def list(
519520
credentials=credentials,
520521
parent=parent,
521522
)
523+
524+
525+
class TensorboardRun(_TensorboardServiceResource):
526+
"""Managed tensorboard resource for Vertex AI."""
527+
528+
_resource_noun = "runs"
529+
_getter_method = "get_tensorboard_run"
530+
_list_method = "list_tensorboard_runs"
531+
_delete_method = "delete_tensorboard_run"
532+
_parse_resource_name_method = "parse_tensorboard_run_path"
533+
_format_resource_name_method = "tensorboard_run_path"
534+
535+
def __init__(
536+
self,
537+
tensorboard_run_name: str,
538+
tensorboard_id: Optional[str] = None,
539+
tensorboard_experiment_id: Optional[str] = None,
540+
project: Optional[str] = None,
541+
location: Optional[str] = None,
542+
credentials: Optional[auth_credentials.Credentials] = None,
543+
):
544+
"""Retrieves an existing tensorboard experiment given a tensorboard experiment name or ID.
545+
546+
Example Usage:
547+
548+
tb_exp = aiplatform.TensorboardRun(
549+
tensorboard_run_name= "projects/123/locations/us-central1/tensorboards/456/experiments/678/run/8910"
550+
)
551+
552+
tb_exp = aiplatform.TensorboardExperiment(
553+
tensorboard_experiment_name= "8910",
554+
tensorboard_id = "456",
555+
tensorboard_experiment_id = "678"
556+
)
557+
558+
Args:
559+
tensorboard_run_name (str):
560+
Required. A fully-qualified tensorboard run resource name or resource ID.
561+
Example: "projects/123/locations/us-central1/tensorboards/456/experiments/678/runs/8910" or
562+
"8910" when tensorboard_id and tensorboard_experiment_id are passed
563+
and project and location are initialized or passed.
564+
tensorboard_id (str):
565+
Optional. A tensorboard resource ID.
566+
tensorboard_experiment_id (str):
567+
Optional. A tensorboard experiment resource ID.
568+
project (str):
569+
Optional. Project to retrieve tensorboard from. If not set, project
570+
set in aiplatform.init will be used.
571+
location (str):
572+
Optional. Location to retrieve tensorboard from. If not set, location
573+
set in aiplatform.init will be used.
574+
credentials (auth_credentials.Credentials):
575+
Optional. Custom credentials to use to retrieve this Tensorboard. Overrides
576+
credentials set in aiplatform.init.
577+
Raises:
578+
ValueError: if only one of tensorboard_id or tensorboard_experiment_id is provided.
579+
"""
580+
if bool(tensorboard_id) != bool(tensorboard_experiment_id):
581+
raise ValueError(
582+
"Both tensorboard_id and tensorboard_experiment_id must be provided or neither should be provided."
583+
)
584+
585+
super().__init__(
586+
project=project,
587+
location=location,
588+
credentials=credentials,
589+
resource_name=tensorboard_run_name,
590+
)
591+
self._gca_resource = self._get_gca_resource(
592+
resource_name=tensorboard_run_name,
593+
parent_resource_name_fields={
594+
Tensorboard._resource_noun: tensorboard_id,
595+
TensorboardExperiment._resource_noun: tensorboard_experiment_id,
596+
}
597+
if tensorboard_id
598+
else tensorboard_id,
599+
)
600+
601+
@classmethod
602+
def create(
603+
cls,
604+
tensorboard_run_id: str,
605+
tensorboard_experiment_name: str,
606+
tensorboard_id: Optional[str] = None,
607+
display_name: Optional[str] = None,
608+
description: Optional[str] = None,
609+
labels: Optional[Dict[str, str]] = None,
610+
project: Optional[str] = None,
611+
location: Optional[str] = None,
612+
credentials: Optional[auth_credentials.Credentials] = None,
613+
request_metadata: Sequence[Tuple[str, str]] = (),
614+
) -> "TensorboardRun":
615+
"""Creates a new tensorboard.
616+
617+
Example Usage:
618+
619+
tb = aiplatform.TensorboardExperiment.create(
620+
tensorboard_experiment_id='my-experiment'
621+
tensorboard_id='456'
622+
display_name='my display name',
623+
description='my description',
624+
labels={
625+
'key1': 'value1',
626+
'key2': 'value2'
627+
}
628+
)
629+
630+
Args:
631+
tensorboard_run_id (str):
632+
Required. The ID to use for the Tensorboard run, which
633+
will become the final component of the Tensorboard run's
634+
resource name.
635+
636+
This value should be 1-128 characters, and valid:
637+
characters are /[a-z][0-9]-/.
638+
tensorboard_experiment_name (str):
639+
Required. The resource name or ID of the TensorboardExperiment
640+
to create the TensorboardRun in. Resource name format:
641+
``projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}``
642+
643+
If resource ID is provided then tensorboard_id must be provided.
644+
tensorboard_id (str):
645+
Optional. The resource ID of the Tensorboard to create
646+
the TensorboardRun in. Format of resource name.
647+
display_name (str):
648+
Optional. The user-defined name of the Tensorboard Run.
649+
This value must be unique among all TensorboardRuns belonging to the
650+
same parent TensorboardExperiment.
651+
652+
If not provided tensorboard_run_id will be used.
653+
description (str):
654+
Optional. Description of this Tensorboard Run.
655+
labels (Dict[str, str]):
656+
Optional. Labels with user-defined metadata to organize your Tensorboards.
657+
Label keys and values can be no longer than 64 characters
658+
(Unicode codepoints), can only contain lowercase letters, numeric
659+
characters, underscores and dashes. International characters are allowed.
660+
No more than 64 user labels can be associated with one Tensorboard
661+
(System labels are excluded).
662+
See https://goo.gl/xmQnxf for more information and examples of labels.
663+
System reserved label keys are prefixed with "aiplatform.googleapis.com/"
664+
and are immutable.
665+
project (str):
666+
Optional. Project to upload this model to. Overrides project set in
667+
aiplatform.init.
668+
location (str):
669+
Optional. Location to upload this model to. Overrides location set in
670+
aiplatform.init.
671+
credentials (auth_credentials.Credentials):
672+
Optional. Custom credentials to use to upload this model. Overrides
673+
credentials set in aiplatform.init.
674+
request_metadata (Sequence[Tuple[str, str]]):
675+
Optional. Strings which should be sent along with the request as metadata.
676+
Returns:
677+
TensorboardExperiment: The TensorboardExperiment resource.
678+
"""
679+
680+
if display_name:
681+
utils.validate_display_name(display_name)
682+
683+
if labels:
684+
utils.validate_labels(labels)
685+
686+
display_name = display_name or tensorboard_run_id
687+
688+
api_client = cls._instantiate_client(location=location, credentials=credentials)
689+
690+
parent = utils.full_resource_name(
691+
resource_name=tensorboard_experiment_name,
692+
resource_noun=TensorboardExperiment._resource_noun,
693+
parse_resource_name_method=TensorboardExperiment._parse_resource_name,
694+
format_resource_name_method=TensorboardExperiment._format_resource_name,
695+
parent_resource_name_fields={Tensorboard._resource_noun: tensorboard_id},
696+
project=project,
697+
location=location,
698+
)
699+
700+
gapic_tensorboard_run = gca_tensorboard_run.TensorboardRun(
701+
display_name=display_name, description=description, labels=labels,
702+
)
703+
704+
_LOGGER.log_create_with_lro(cls)
705+
706+
tensorboard_run = api_client.create_tensorboard_run(
707+
parent=parent,
708+
tensorboard_run=gapic_tensorboard_run,
709+
tensorboard_run_id=tensorboard_run_id,
710+
metadata=request_metadata,
711+
)
712+
713+
_LOGGER.log_create_complete(cls, tensorboard_run, "tb_run")
714+
715+
return cls(tensorboard_run_name=tensorboard_run.name, credentials=credentials,)
716+
717+
@classmethod
718+
def list(
719+
cls,
720+
tensorboard_experiment_name: str,
721+
tensorboard_id: Optional[str] = None,
722+
filter: Optional[str] = None,
723+
order_by: Optional[str] = None,
724+
project: Optional[str] = None,
725+
location: Optional[str] = None,
726+
credentials: Optional[auth_credentials.Credentials] = None,
727+
) -> List["TensorboardRun"]:
728+
"""List all instances of TensorboardRun in TensorboardExperiment.
729+
730+
Example Usage:
731+
732+
aiplatform.TensorboardRun.list(
733+
tensorboard_name='projects/my-project/locations/us-central1/tensorboards/123/experiments/456'
734+
)
735+
736+
Args:
737+
tensorboard_experiment_name (str):
738+
Required. The resource name or resource ID of the
739+
TensorboardExperiment to list
740+
TensorboardRun. Format, if resource name:
741+
'projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}'
742+
743+
If resource ID is provided then tensorboard_id must be provided.
744+
tensorboard_id (str):
745+
Optional. The resource ID of the Tensorboard that contains the TensorboardExperiment
746+
to list TensorboardRun.
747+
filter (str):
748+
Optional. An expression for filtering the results of the request.
749+
For field names both snake_case and camelCase are supported.
750+
order_by (str):
751+
Optional. A comma-separated list of fields to order by, sorted in
752+
ascending order. Use "desc" after a field name for descending.
753+
Supported fields: `display_name`, `create_time`, `update_time`
754+
project (str):
755+
Optional. Project to retrieve list from. If not set, project
756+
set in aiplatform.init will be used.
757+
location (str):
758+
Optional. Location to retrieve list from. If not set, location
759+
set in aiplatform.init will be used.
760+
credentials (auth_credentials.Credentials):
761+
Optional. Custom credentials to use to retrieve list. Overrides
762+
credentials set in aiplatform.init.
763+
Returns:
764+
List[TensorboardRun] - A list of TensorboardRun
765+
"""
766+
767+
parent = utils.full_resource_name(
768+
resource_name=tensorboard_experiment_name,
769+
resource_noun=TensorboardExperiment._resource_noun,
770+
parse_resource_name_method=TensorboardExperiment._parse_resource_name,
771+
format_resource_name_method=TensorboardExperiment._format_resource_name,
772+
parent_resource_name_fields={Tensorboard._resource_noun: tensorboard_id},
773+
project=project,
774+
location=location,
775+
)
776+
777+
return super()._list(
778+
filter=filter,
779+
order_by=order_by,
780+
project=project,
781+
location=location,
782+
credentials=credentials,
783+
parent=parent,
784+
)

tests/system/aiplatform/test_tensorboard.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,22 @@ def test_create_and_get_tensorboard(self, shared_state):
6464
)
6565

6666
assert len(list_tb_experiment) > 0
67+
68+
tb_run = aiplatform.TensorboardRun.create(
69+
tensorboard_run_id="test-run",
70+
tensorboard_experiment_name=tb_experiment.resource_name,
71+
description="Vertex SDK Integration test run",
72+
labels={"test": "labels"},
73+
)
74+
75+
shared_state["resources"].append(tb_run)
76+
77+
get_tb_run = aiplatform.TensorboardRun(tb_run.resource_name)
78+
79+
assert tb_run.resource_name == get_tb_run.resource_name
80+
81+
list_tb_run = aiplatform.TensorboardRun.list(
82+
tensorboard_experiment_name=tb_experiment.resource_name
83+
)
84+
85+
assert len(list_tb_run) > 0

0 commit comments

Comments
 (0)