|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2022 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Cross-platform Vizier client interfaces. |
| 17 | +
|
| 18 | +Aside from "materialize_" methods, code written using these interfaces are |
| 19 | +compatible with OSS and Cloud Vertex Vizier. Note importantly that subclasses |
| 20 | +may have more methods than what is required by interfaces, and such methods |
| 21 | +are not cross compatible. Our recommendation is to explicitly type your objects |
| 22 | +to be `StudyInterface` or `TrialInterface` when you want to guarantee that |
| 23 | +a code block is cross-platform. |
| 24 | +
|
| 25 | +Keywords: |
| 26 | +
|
| 27 | +#Materialize: The method returns a deep copy of the underlying pyvizier object. |
| 28 | +Modifying the returned object does not update the Vizier service. |
| 29 | +""" |
| 30 | + |
| 31 | +from __future__ import annotations |
| 32 | + |
| 33 | +from typing import Optional, Collection, Type, TypeVar, Mapping, Any |
| 34 | +import abc |
| 35 | + |
| 36 | +from google.cloud.aiplatform.vizier import pyvizier as vz |
| 37 | + |
| 38 | +_T = TypeVar("_T") |
| 39 | + |
| 40 | + |
| 41 | +class ResourceNotFoundError(LookupError): |
| 42 | + """Error raised by Vizier clients when resource is not found.""" |
| 43 | + |
| 44 | + pass |
| 45 | + |
| 46 | + |
| 47 | +class TrialInterface(abc.ABC): |
| 48 | + """Responsible for trial-level operations.""" |
| 49 | + |
| 50 | + @property |
| 51 | + @abc.abstractmethod |
| 52 | + def uid(self) -> int: |
| 53 | + """Unique identifier of the trial.""" |
| 54 | + |
| 55 | + @property |
| 56 | + @abc.abstractmethod |
| 57 | + def parameters(self) -> Mapping[str, Any]: |
| 58 | + """Parameters of the trial.""" |
| 59 | + |
| 60 | + @property |
| 61 | + @abc.abstractmethod |
| 62 | + def status(self) -> vz.TrialStatus: |
| 63 | + """Trial's status.""" |
| 64 | + |
| 65 | + @abc.abstractmethod |
| 66 | + def delete(self) -> None: |
| 67 | + """Delete the Trial in Vizier service. |
| 68 | +
|
| 69 | + There is currently no promise on how this object behaves after `delete()`. |
| 70 | + If you are sharing a Trial object in parallel processes, proceed with |
| 71 | + caution. |
| 72 | + """ |
| 73 | + |
| 74 | + @abc.abstractmethod |
| 75 | + def complete( |
| 76 | + self, |
| 77 | + measurement: Optional[vz.Measurement] = None, |
| 78 | + *, |
| 79 | + infeasible_reason: Optional[str] = None, |
| 80 | + ) -> Optional[vz.Measurement]: |
| 81 | + """Completes the trial and #materializes the measurement. |
| 82 | +
|
| 83 | + * If `measurement` is provided, then Vizier writes it as the trial's final |
| 84 | + measurement and returns it. |
| 85 | + * If `infeasible_reason` is provided, `measurement` is not needed. |
| 86 | + * If neither is provided, then Vizier selects an existing (intermediate) |
| 87 | + measurement to be the final measurement and returns it. |
| 88 | +
|
| 89 | + Args: |
| 90 | + measurement: Final measurement. |
| 91 | + infeasible_reason: Infeasible reason for missing final measurement. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + The final measurement of the trial, or None if the trial is marked |
| 95 | + infeasible. |
| 96 | +
|
| 97 | + Raises: |
| 98 | + ValueError: If neither `measurement` nor `infeasible_reason` is provided |
| 99 | + but the trial does not contain any intermediate measurements. |
| 100 | + """ |
| 101 | + |
| 102 | + @abc.abstractmethod |
| 103 | + def should_stop(self) -> bool: |
| 104 | + """Returns true if the trial should stop.""" |
| 105 | + |
| 106 | + @abc.abstractmethod |
| 107 | + def add_measurement(self, measurement: vz.Measurement) -> None: |
| 108 | + """Adds an intermediate measurement.""" |
| 109 | + |
| 110 | + @abc.abstractmethod |
| 111 | + def materialize(self, *, include_all_measurements: bool = True) -> vz.Trial: |
| 112 | + """#Materializes the Trial. |
| 113 | +
|
| 114 | + Args: |
| 115 | + include_all_measurements: If True, returned Trial includes all |
| 116 | + intermediate measurements. The final measurement is always provided. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + Trial object. |
| 120 | + """ |
| 121 | + |
| 122 | + |
| 123 | +class StudyInterface(abc.ABC): |
| 124 | + """Responsible for study-level operations.""" |
| 125 | + |
| 126 | + @abc.abstractmethod |
| 127 | + def create_or_load( |
| 128 | + self, display_name: str, problem: vz.ProblemStatement |
| 129 | + ) -> StudyInterface: |
| 130 | + """ """ |
| 131 | + |
| 132 | + @abc.abstractmethod |
| 133 | + def suggest( |
| 134 | + self, *, count: Optional[int] = None, worker: str = "" |
| 135 | + ) -> Collection[TrialInterface]: |
| 136 | + """Returns Trials to be evaluated by worker. |
| 137 | +
|
| 138 | + Args: |
| 139 | + count: Number of suggestions. |
| 140 | + worker: When new Trials are generated, their `assigned_worker` field is |
| 141 | + populated with this worker. suggest() first looks for existing Trials |
| 142 | + that are assigned to `worker`, before generating new ones. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + Trials. |
| 146 | + """ |
| 147 | + |
| 148 | + @abc.abstractmethod |
| 149 | + def delete(self) -> None: |
| 150 | + """Deletes the study.""" |
| 151 | + |
| 152 | + @abc.abstractmethod |
| 153 | + def trials( |
| 154 | + self, trial_filter: Optional[vz.TrialFilter] = None |
| 155 | + ) -> Collection[TrialInterface]: |
| 156 | + """Fetches a collection of trials.""" |
| 157 | + |
| 158 | + @abc.abstractmethod |
| 159 | + def get_trial(self, uid: int) -> TrialInterface: |
| 160 | + """Fetches a single trial. |
| 161 | +
|
| 162 | + Args: |
| 163 | + uid: Unique identifier of the trial within study. |
| 164 | +
|
| 165 | + Returns: |
| 166 | + Trial. |
| 167 | +
|
| 168 | + Raises: |
| 169 | + ResourceNotFoundError: If trial does not exist. |
| 170 | + """ |
| 171 | + |
| 172 | + @abc.abstractmethod |
| 173 | + def optimal_trials(self) -> Collection[TrialInterface]: |
| 174 | + """Returns optimal trial(s).""" |
| 175 | + |
| 176 | + @abc.abstractmethod |
| 177 | + def materialize_study_config(self) -> vz.StudyConfig: |
| 178 | + """#Materializes the study config.""" |
| 179 | + |
| 180 | + @abc.abstractclassmethod |
| 181 | + def from_uid(cls: Type[_T], uid: str) -> _T: |
| 182 | + """Fetches an existing study from the Vizier service. |
| 183 | +
|
| 184 | + Args: |
| 185 | + uid: Unique identifier of the study. |
| 186 | +
|
| 187 | + Returns: |
| 188 | + Study. |
| 189 | +
|
| 190 | + Raises: |
| 191 | + ResourceNotFoundError: If study does not exist. |
| 192 | + """ |
0 commit comments