Add loan-advisor-agent
Browse files- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/__init__.py +68 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/bigquery_datasink.py +161 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/bigquery_datasource.py +151 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/client_builder.py +201 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/cluster_init.py +575 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/dashboard_sdk.py +79 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/data.py +193 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/__init__.py +18 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/sklearn/__init__.py +22 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/sklearn/register.py +173 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/tensorflow/__init__.py +22 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/tensorflow/register.py +161 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/torch/__init__.py +22 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/torch/register.py +113 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/util/constants.py +28 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/util/predict_utils.py +23 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/xgboost/__init__.py +22 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/xgboost/register.py +189 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/render.py +27 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/templates/context_shellurirow.html.j2 +4 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/templates/context_table.html.j2 +24 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/_gapic_utils.py +300 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/_validation_utils.py +172 -0
- loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/resources.py +219 -0
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/__init__.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2025 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
import sys
|
| 20 |
+
|
| 21 |
+
from google.cloud.aiplatform.vertex_ray.bigquery_datasource import (
|
| 22 |
+
_BigQueryDatasource,
|
| 23 |
+
)
|
| 24 |
+
from google.cloud.aiplatform.vertex_ray.client_builder import (
|
| 25 |
+
VertexRayClientBuilder as ClientBuilder,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
from google.cloud.aiplatform.vertex_ray.cluster_init import (
|
| 29 |
+
create_ray_cluster,
|
| 30 |
+
delete_ray_cluster,
|
| 31 |
+
get_ray_cluster,
|
| 32 |
+
list_ray_clusters,
|
| 33 |
+
update_ray_cluster,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
from google.cloud.aiplatform.vertex_ray import data
|
| 37 |
+
|
| 38 |
+
from google.cloud.aiplatform.vertex_ray.util.resources import (
|
| 39 |
+
AutoscalingSpec,
|
| 40 |
+
Resources,
|
| 41 |
+
NodeImages,
|
| 42 |
+
PscIConfig,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
from google.cloud.aiplatform.vertex_ray.dashboard_sdk import (
|
| 46 |
+
get_job_submission_client_cluster_info,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if sys.version_info[1] not in (10, 11):
|
| 50 |
+
print(
|
| 51 |
+
"[Ray on Vertex]: The client environment with Python version 3.10 or 3.11 is required."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
__all__ = (
|
| 55 |
+
"_BigQueryDatasource",
|
| 56 |
+
"data",
|
| 57 |
+
"ClientBuilder",
|
| 58 |
+
"get_job_submission_client_cluster_info",
|
| 59 |
+
"create_ray_cluster",
|
| 60 |
+
"delete_ray_cluster",
|
| 61 |
+
"get_ray_cluster",
|
| 62 |
+
"list_ray_clusters",
|
| 63 |
+
"update_ray_cluster",
|
| 64 |
+
"AutoscalingSpec",
|
| 65 |
+
"Resources",
|
| 66 |
+
"NodeImages",
|
| 67 |
+
"PscIConfig",
|
| 68 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/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-*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2024 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 |
+
#
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
import logging
|
| 20 |
+
import os
|
| 21 |
+
import tempfile
|
| 22 |
+
import time
|
| 23 |
+
import uuid
|
| 24 |
+
from typing import Any, Iterable, Optional
|
| 25 |
+
|
| 26 |
+
import pyarrow.parquet as pq
|
| 27 |
+
|
| 28 |
+
from google.api_core import client_info
|
| 29 |
+
from google.api_core import exceptions
|
| 30 |
+
from google.cloud import bigquery
|
| 31 |
+
from google.cloud.aiplatform import initializer
|
| 32 |
+
|
| 33 |
+
import ray
|
| 34 |
+
from ray.data._internal.execution.interfaces import TaskContext
|
| 35 |
+
from ray.data._internal.remote_fn import cached_remote_fn
|
| 36 |
+
from ray.data.block import Block, BlockAccessor
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
from ray.data.datasource.datasink import Datasink
|
| 40 |
+
except ImportError:
|
| 41 |
+
# If datasink cannot be imported, Ray >=2.9.3 is not installed
|
| 42 |
+
Datasink = None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
DEFAULT_MAX_RETRY_CNT = 10
|
| 46 |
+
RATE_LIMIT_EXCEEDED_SLEEP_TIME = 11
|
| 47 |
+
|
| 48 |
+
_BQ_GAPIC_VERSION = bigquery.__version__ + "+vertex_ray"
|
| 49 |
+
bq_info = client_info.ClientInfo(
|
| 50 |
+
gapic_version=_BQ_GAPIC_VERSION, user_agent=f"ray-on-vertex/{_BQ_GAPIC_VERSION}"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# BigQuery write for Ray 2.47.1, 2.42.0, 2.33.0, and 2.9.3
|
| 55 |
+
if Datasink is None:
|
| 56 |
+
_BigQueryDatasink = None
|
| 57 |
+
else:
|
| 58 |
+
|
| 59 |
+
class _BigQueryDatasink(Datasink):
|
| 60 |
+
def __init__(
|
| 61 |
+
self,
|
| 62 |
+
dataset: str,
|
| 63 |
+
project_id: Optional[str] = None,
|
| 64 |
+
max_retry_cnt: int = DEFAULT_MAX_RETRY_CNT,
|
| 65 |
+
overwrite_table: Optional[bool] = True,
|
| 66 |
+
) -> None:
|
| 67 |
+
self.dataset = dataset
|
| 68 |
+
self.project_id = project_id or initializer.global_config.project
|
| 69 |
+
self.max_retry_cnt = max_retry_cnt
|
| 70 |
+
self.overwrite_table = overwrite_table
|
| 71 |
+
|
| 72 |
+
def on_write_start(self) -> None:
|
| 73 |
+
# Set up datasets to write
|
| 74 |
+
client = bigquery.Client(project=self.project_id, client_info=bq_info)
|
| 75 |
+
dataset_id = self.dataset.split(".", 1)[0]
|
| 76 |
+
try:
|
| 77 |
+
client.get_dataset(dataset_id)
|
| 78 |
+
except exceptions.NotFound:
|
| 79 |
+
client.create_dataset(f"{self.project_id}.{dataset_id}", timeout=30)
|
| 80 |
+
print("[Ray on Vertex AI]: Created dataset " + dataset_id)
|
| 81 |
+
|
| 82 |
+
# Delete table if overwrite_table is True
|
| 83 |
+
if self.overwrite_table:
|
| 84 |
+
print(
|
| 85 |
+
f"[Ray on Vertex AI]: Attempting to delete table {self.dataset}"
|
| 86 |
+
+ " if it already exists since kwarg overwrite_table = True."
|
| 87 |
+
)
|
| 88 |
+
client.delete_table(
|
| 89 |
+
f"{self.project_id}.{self.dataset}", not_found_ok=True
|
| 90 |
+
)
|
| 91 |
+
else:
|
| 92 |
+
print(
|
| 93 |
+
"[Ray on Vertex AI]: The write will append to table "
|
| 94 |
+
+ f"{self.dataset} if it already exists "
|
| 95 |
+
+ "since kwarg overwrite_table = False."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
def write(
|
| 99 |
+
self,
|
| 100 |
+
blocks: Iterable[Block],
|
| 101 |
+
ctx: TaskContext,
|
| 102 |
+
) -> Any:
|
| 103 |
+
def _write_single_block(
|
| 104 |
+
block: Block, project_id: str, dataset: str
|
| 105 |
+
) -> None:
|
| 106 |
+
block = BlockAccessor.for_block(block).to_arrow()
|
| 107 |
+
|
| 108 |
+
client = bigquery.Client(project=project_id, client_info=bq_info)
|
| 109 |
+
job_config = bigquery.LoadJobConfig(autodetect=True)
|
| 110 |
+
job_config.source_format = bigquery.SourceFormat.PARQUET
|
| 111 |
+
job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND
|
| 112 |
+
|
| 113 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 114 |
+
fp = os.path.join(temp_dir, f"block_{uuid.uuid4()}.parquet")
|
| 115 |
+
pq.write_table(block, fp, compression="SNAPPY")
|
| 116 |
+
|
| 117 |
+
retry_cnt = 0
|
| 118 |
+
while retry_cnt <= self.max_retry_cnt:
|
| 119 |
+
with open(fp, "rb") as source_file:
|
| 120 |
+
job = client.load_table_from_file(
|
| 121 |
+
source_file, dataset, job_config=job_config
|
| 122 |
+
)
|
| 123 |
+
try:
|
| 124 |
+
logging.info(job.result())
|
| 125 |
+
break
|
| 126 |
+
except exceptions.Forbidden as e:
|
| 127 |
+
retry_cnt += 1
|
| 128 |
+
if retry_cnt > self.max_retry_cnt:
|
| 129 |
+
break
|
| 130 |
+
print(
|
| 131 |
+
"[Ray on Vertex AI]: A block write encountered"
|
| 132 |
+
+ f" a rate limit exceeded error {retry_cnt} time(s)."
|
| 133 |
+
+ " Sleeping to try again."
|
| 134 |
+
)
|
| 135 |
+
logging.debug(e)
|
| 136 |
+
time.sleep(RATE_LIMIT_EXCEEDED_SLEEP_TIME)
|
| 137 |
+
|
| 138 |
+
# Raise exception if retry_cnt exceeds max_retry_cnt
|
| 139 |
+
if retry_cnt > self.max_retry_cnt:
|
| 140 |
+
print(
|
| 141 |
+
f"[Ray on Vertex AI]: Maximum ({self.max_retry_cnt}) retry count exceeded."
|
| 142 |
+
+ " Ray will attempt to retry the block write via fault tolerance."
|
| 143 |
+
+ " For more information, see https://docs.ray.io/en/latest/ray-core/fault_tolerance/tasks.html"
|
| 144 |
+
)
|
| 145 |
+
raise RuntimeError(
|
| 146 |
+
f"[Ray on Vertex AI]: Write failed due to {retry_cnt}"
|
| 147 |
+
+ " repeated API rate limit exceeded responses. Consider"
|
| 148 |
+
+ " specifiying the max_retry_cnt kwarg with a higher value."
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
_write_single_block = cached_remote_fn(_write_single_block)
|
| 152 |
+
|
| 153 |
+
# Launch a remote task for each block within this write task
|
| 154 |
+
ray.get(
|
| 155 |
+
[
|
| 156 |
+
_write_single_block.remote(block, self.project_id, self.dataset)
|
| 157 |
+
for block in blocks
|
| 158 |
+
]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
return "ok"
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/bigquery_datasource.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
#
|
| 17 |
+
|
| 18 |
+
from typing import List, Optional
|
| 19 |
+
|
| 20 |
+
from google.api_core import client_info
|
| 21 |
+
from google.api_core import exceptions
|
| 22 |
+
from google.api_core.gapic_v1 import client_info as v1_client_info
|
| 23 |
+
from google.cloud import bigquery
|
| 24 |
+
from google.cloud import bigquery_storage
|
| 25 |
+
from google.cloud.aiplatform import initializer
|
| 26 |
+
from google.cloud.bigquery_storage import types
|
| 27 |
+
from ray.data.block import Block
|
| 28 |
+
from ray.data.block import BlockMetadata
|
| 29 |
+
from ray.data.datasource.datasource import Datasource
|
| 30 |
+
from ray.data.datasource.datasource import ReadTask
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
_BQ_GAPIC_VERSION = bigquery.__version__ + "+vertex_ray"
|
| 34 |
+
_BQS_GAPIC_VERSION = bigquery_storage.__version__ + "+vertex_ray"
|
| 35 |
+
bq_info = client_info.ClientInfo(
|
| 36 |
+
gapic_version=_BQ_GAPIC_VERSION, user_agent=f"ray-on-vertex/{_BQ_GAPIC_VERSION}"
|
| 37 |
+
)
|
| 38 |
+
bqstorage_info = v1_client_info.ClientInfo(
|
| 39 |
+
gapic_version=_BQS_GAPIC_VERSION, user_agent=f"ray-on-vertex/{_BQS_GAPIC_VERSION}"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
DEFAULT_MAX_RETRY_CNT = 10
|
| 43 |
+
RATE_LIMIT_EXCEEDED_SLEEP_TIME = 11
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class _BigQueryDatasource(Datasource):
|
| 47 |
+
def __init__(
|
| 48 |
+
self,
|
| 49 |
+
project_id: Optional[str] = None,
|
| 50 |
+
dataset: Optional[str] = None,
|
| 51 |
+
query: Optional[str] = None,
|
| 52 |
+
):
|
| 53 |
+
self._project_id = project_id or initializer.global_config.project
|
| 54 |
+
self._dataset = dataset
|
| 55 |
+
self._query = query
|
| 56 |
+
|
| 57 |
+
if query is not None and dataset is not None:
|
| 58 |
+
raise ValueError(
|
| 59 |
+
"[Ray on Vertex AI]: Query and dataset kwargs cannot both be provided (must be mutually exclusive)."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def get_read_tasks(self, parallelism: int) -> List[ReadTask]:
|
| 63 |
+
# Executed by a worker node
|
| 64 |
+
def _read_single_partition(stream) -> Block:
|
| 65 |
+
client = bigquery_storage.BigQueryReadClient(client_info=bqstorage_info)
|
| 66 |
+
reader = client.read_rows(stream.name)
|
| 67 |
+
return reader.to_arrow()
|
| 68 |
+
|
| 69 |
+
if self._query:
|
| 70 |
+
query_client = bigquery.Client(
|
| 71 |
+
project=self._project_id, client_info=bq_info
|
| 72 |
+
)
|
| 73 |
+
query_job = query_client.query(self._query)
|
| 74 |
+
query_job.result()
|
| 75 |
+
destination = str(query_job.destination)
|
| 76 |
+
dataset_id = destination.split(".")[-2]
|
| 77 |
+
table_id = destination.split(".")[-1]
|
| 78 |
+
else:
|
| 79 |
+
self._validate_dataset_table_exist(self._project_id, self._dataset)
|
| 80 |
+
dataset_id = self._dataset.split(".")[0]
|
| 81 |
+
table_id = self._dataset.split(".")[1]
|
| 82 |
+
|
| 83 |
+
bqs_client = bigquery_storage.BigQueryReadClient(client_info=bqstorage_info)
|
| 84 |
+
table = f"projects/{self._project_id}/datasets/{dataset_id}/tables/{table_id}"
|
| 85 |
+
|
| 86 |
+
if parallelism == -1:
|
| 87 |
+
parallelism = None
|
| 88 |
+
requested_session = types.ReadSession(
|
| 89 |
+
table=table,
|
| 90 |
+
data_format=types.DataFormat.ARROW,
|
| 91 |
+
)
|
| 92 |
+
read_session = bqs_client.create_read_session(
|
| 93 |
+
parent=f"projects/{self._project_id}",
|
| 94 |
+
read_session=requested_session,
|
| 95 |
+
max_stream_count=parallelism,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
read_tasks = []
|
| 99 |
+
print("[Ray on Vertex AI]: Created streams:", len(read_session.streams))
|
| 100 |
+
if len(read_session.streams) < parallelism:
|
| 101 |
+
print(
|
| 102 |
+
"[Ray on Vertex AI]: The number of streams created by the "
|
| 103 |
+
+ "BigQuery Storage Read API is less than the requested "
|
| 104 |
+
+ "parallelism due to the size of the dataset."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
for stream in read_session.streams:
|
| 108 |
+
# Create a metadata block object to store schema, etc.
|
| 109 |
+
metadata = BlockMetadata(
|
| 110 |
+
num_rows=None,
|
| 111 |
+
size_bytes=None,
|
| 112 |
+
schema=None,
|
| 113 |
+
input_files=None,
|
| 114 |
+
exec_stats=None,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Create a no-arg wrapper read function which returns a block
|
| 118 |
+
read_single_partition = lambda stream=stream: [ # noqa: E731
|
| 119 |
+
_read_single_partition(stream)
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
# Create the read task and pass the wrapper and metadata in
|
| 123 |
+
read_task = ReadTask(read_single_partition, metadata)
|
| 124 |
+
read_tasks.append(read_task)
|
| 125 |
+
|
| 126 |
+
return read_tasks
|
| 127 |
+
|
| 128 |
+
def estimate_inmemory_data_size(self) -> Optional[int]:
|
| 129 |
+
# TODO(b/281891467): Implement this method
|
| 130 |
+
return None
|
| 131 |
+
|
| 132 |
+
def _validate_dataset_table_exist(self, project_id: str, dataset: str) -> None:
|
| 133 |
+
client = bigquery.Client(project=project_id, client_info=bq_info)
|
| 134 |
+
dataset_id = dataset.split(".")[0]
|
| 135 |
+
try:
|
| 136 |
+
client.get_dataset(dataset_id)
|
| 137 |
+
except exceptions.NotFound:
|
| 138 |
+
raise ValueError(
|
| 139 |
+
"[Ray on Vertex AI]: Dataset {} is not found. Please ensure that it exists.".format(
|
| 140 |
+
dataset_id
|
| 141 |
+
)
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
client.get_table(dataset)
|
| 146 |
+
except exceptions.NotFound:
|
| 147 |
+
raise ValueError(
|
| 148 |
+
"[Ray on Vertex AI]: Table {} is not found. Please ensure that it exists.".format(
|
| 149 |
+
dataset
|
| 150 |
+
)
|
| 151 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2025 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 |
+
#
|
| 17 |
+
import grpc
|
| 18 |
+
import logging
|
| 19 |
+
import ray
|
| 20 |
+
|
| 21 |
+
from typing import Dict
|
| 22 |
+
from typing import Optional
|
| 23 |
+
from google.cloud import aiplatform
|
| 24 |
+
from google.cloud.aiplatform import initializer
|
| 25 |
+
from ray import client_builder
|
| 26 |
+
from .render import VertexRayTemplate
|
| 27 |
+
from .util import _validation_utils
|
| 28 |
+
from .util import _gapic_utils
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
VERTEX_SDK_VERSION = aiplatform.__version__
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class _VertexRayClientContext(client_builder.ClientContext):
|
| 35 |
+
"""Custom ClientContext."""
|
| 36 |
+
|
| 37 |
+
def __init__(
|
| 38 |
+
self,
|
| 39 |
+
persistent_resource_id: str,
|
| 40 |
+
ray_head_uris: Dict[str, str],
|
| 41 |
+
ray_client_context: client_builder.ClientContext,
|
| 42 |
+
) -> None:
|
| 43 |
+
dashboard_uri = ray_head_uris.get("RAY_DASHBOARD_URI")
|
| 44 |
+
if dashboard_uri is None:
|
| 45 |
+
raise ValueError(
|
| 46 |
+
"Ray Cluster ",
|
| 47 |
+
persistent_resource_id,
|
| 48 |
+
" failed to start Head node properly.",
|
| 49 |
+
)
|
| 50 |
+
if ray.__version__ in ("2.47.1", "2.42.0", "2.33.0"):
|
| 51 |
+
super().__init__(
|
| 52 |
+
dashboard_url=dashboard_uri,
|
| 53 |
+
python_version=ray_client_context.python_version,
|
| 54 |
+
ray_version=ray_client_context.ray_version,
|
| 55 |
+
ray_commit=ray_client_context.ray_commit,
|
| 56 |
+
_num_clients=ray_client_context._num_clients,
|
| 57 |
+
_context_to_restore=ray_client_context._context_to_restore,
|
| 58 |
+
)
|
| 59 |
+
elif ray.__version__ == "2.9.3":
|
| 60 |
+
super().__init__(
|
| 61 |
+
dashboard_url=dashboard_uri,
|
| 62 |
+
python_version=ray_client_context.python_version,
|
| 63 |
+
ray_version=ray_client_context.ray_version,
|
| 64 |
+
ray_commit=ray_client_context.ray_commit,
|
| 65 |
+
protocol_version=ray_client_context.protocol_version,
|
| 66 |
+
_num_clients=ray_client_context._num_clients,
|
| 67 |
+
_context_to_restore=ray_client_context._context_to_restore,
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
raise ImportError(
|
| 71 |
+
f"[Ray on Vertex AI]: Unsupported version {ray.__version__}."
|
| 72 |
+
+ "Only 2.47.1, 2.42.0, 2.33.0, and 2.9.3 are supported."
|
| 73 |
+
)
|
| 74 |
+
self.persistent_resource_id = persistent_resource_id
|
| 75 |
+
self.vertex_sdk_version = str(VERTEX_SDK_VERSION)
|
| 76 |
+
self.shell_uri = ray_head_uris.get("RAY_HEAD_NODE_INTERACTIVE_SHELL_URI")
|
| 77 |
+
|
| 78 |
+
def _context_table_template(self):
|
| 79 |
+
shell_uri_row = None
|
| 80 |
+
if self.shell_uri is not None:
|
| 81 |
+
shell_uri_row = VertexRayTemplate("context_shellurirow.html.j2").render(
|
| 82 |
+
shell_uri=self.shell_uri
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
return VertexRayTemplate("context_table.html.j2").render(
|
| 86 |
+
python_version=self.python_version,
|
| 87 |
+
ray_version=self.ray_version,
|
| 88 |
+
vertex_sdk_version=self.vertex_sdk_version,
|
| 89 |
+
dashboard_url=self.dashboard_url,
|
| 90 |
+
persistent_resource_id=self.persistent_resource_id,
|
| 91 |
+
shell_uri_row=shell_uri_row,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class VertexRayClientBuilder(client_builder.ClientBuilder):
|
| 96 |
+
"""Class to initialize a Ray client with vertex on ray capabilities."""
|
| 97 |
+
|
| 98 |
+
def __init__(self, address: Optional[str]) -> None:
|
| 99 |
+
address = _validation_utils.maybe_reconstruct_resource_name(address)
|
| 100 |
+
_validation_utils.valid_resource_name(address)
|
| 101 |
+
self._credentials = None
|
| 102 |
+
self._metadata = None
|
| 103 |
+
self.vertex_address = address
|
| 104 |
+
logging.info(
|
| 105 |
+
"[Ray on Vertex AI]: Using cluster resource name to access head address with GAPIC API"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
self.resource_name = address
|
| 109 |
+
|
| 110 |
+
self.response = _gapic_utils.get_persistent_resource(self.resource_name)
|
| 111 |
+
private_address = self.response.resource_runtime.access_uris.get(
|
| 112 |
+
"RAY_HEAD_NODE_INTERNAL_IP"
|
| 113 |
+
)
|
| 114 |
+
public_address = self.response.resource_runtime.access_uris.get(
|
| 115 |
+
"RAY_CLIENT_ENDPOINT"
|
| 116 |
+
)
|
| 117 |
+
service_account = (
|
| 118 |
+
self.response.resource_runtime_spec.service_account_spec.service_account
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if public_address is None:
|
| 122 |
+
address = private_address
|
| 123 |
+
if service_account:
|
| 124 |
+
raise ValueError(
|
| 125 |
+
"[Ray on Vertex AI]: Ray Cluster ",
|
| 126 |
+
address,
|
| 127 |
+
" failed to start Head node properly because custom service"
|
| 128 |
+
" account isn't supported in peered VPC network. Use public"
|
| 129 |
+
" endpoint instead (createa a cluster without specifying"
|
| 130 |
+
" VPC network).",
|
| 131 |
+
)
|
| 132 |
+
else:
|
| 133 |
+
address = public_address
|
| 134 |
+
|
| 135 |
+
if address is None:
|
| 136 |
+
persistent_resource_id = self.resource_name.split("/")[5]
|
| 137 |
+
raise ValueError(
|
| 138 |
+
"[Ray on Vertex AI]: Ray Cluster ",
|
| 139 |
+
persistent_resource_id,
|
| 140 |
+
" Head node is not reachable. Please ensure that a valid VPC network has been specified.",
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
logging.debug("[Ray on Vertex AI]: Resolved head node ip: %s", address)
|
| 144 |
+
cluster = _gapic_utils.persistent_resource_to_cluster(
|
| 145 |
+
persistent_resource=self.response
|
| 146 |
+
)
|
| 147 |
+
if cluster is None:
|
| 148 |
+
raise ValueError(
|
| 149 |
+
"[Ray on Vertex AI]: Please delete and recreate the cluster (The cluster is not a Ray cluster or the cluster image is outdated)."
|
| 150 |
+
)
|
| 151 |
+
local_ray_verion = _validation_utils.get_local_ray_version()
|
| 152 |
+
if cluster.ray_version != local_ray_verion:
|
| 153 |
+
if cluster.head_node_type.custom_image is None:
|
| 154 |
+
install_ray_version = _validation_utils.SUPPORTED_RAY_VERSIONS.get(
|
| 155 |
+
cluster.ray_version
|
| 156 |
+
)
|
| 157 |
+
logging.info(
|
| 158 |
+
"[Ray on Vertex]: Local runtime has Ray version %s"
|
| 159 |
+
", but the requested cluster runtime has %s. Please "
|
| 160 |
+
"ensure that the Ray versions match for client connectivity. You may "
|
| 161 |
+
'"pip install --user --force-reinstall ray[default]==%s"'
|
| 162 |
+
" and restart runtime before cluster connection."
|
| 163 |
+
% (local_ray_verion, cluster.ray_version, install_ray_version)
|
| 164 |
+
)
|
| 165 |
+
else:
|
| 166 |
+
logging.info(
|
| 167 |
+
"[Ray on Vertex]: Local runtime has Ray version %s."
|
| 168 |
+
"Please ensure that the Ray versions match for client connectivity."
|
| 169 |
+
% local_ray_verion
|
| 170 |
+
)
|
| 171 |
+
super().__init__(address)
|
| 172 |
+
|
| 173 |
+
def connect(self) -> _VertexRayClientContext:
|
| 174 |
+
# Can send any other params to ray cluster here
|
| 175 |
+
logging.info("[Ray on Vertex AI]: Connecting...")
|
| 176 |
+
|
| 177 |
+
public_address = self.response.resource_runtime.access_uris.get(
|
| 178 |
+
"RAY_CLIENT_ENDPOINT"
|
| 179 |
+
)
|
| 180 |
+
private_address = self.response.resource_runtime.access_uris.get(
|
| 181 |
+
"RAY_HEAD_NODE_INTERNAL_IP"
|
| 182 |
+
)
|
| 183 |
+
if public_address and not private_address:
|
| 184 |
+
self._credentials = grpc.ssl_channel_credentials()
|
| 185 |
+
bearer_token = _validation_utils.get_bearer_token()
|
| 186 |
+
self._metadata = [
|
| 187 |
+
("authorization", "Bearer {}".format(bearer_token)),
|
| 188 |
+
("x-goog-user-project", "{}".format(initializer.global_config.project)),
|
| 189 |
+
]
|
| 190 |
+
ray_client_context = super().connect()
|
| 191 |
+
ray_head_uris = self.response.resource_runtime.access_uris
|
| 192 |
+
|
| 193 |
+
# Valid resource name (reference public doc for public release):
|
| 194 |
+
# "projects/<project_num>/locations/<region>/persistentResources/<pr_id>"
|
| 195 |
+
persistent_resource_id = self.resource_name.split("/")[5]
|
| 196 |
+
|
| 197 |
+
return _VertexRayClientContext(
|
| 198 |
+
persistent_resource_id=persistent_resource_id,
|
| 199 |
+
ray_head_uris=ray_head_uris,
|
| 200 |
+
ray_client_context=ray_client_context,
|
| 201 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/cluster_init.py
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2023 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 |
+
#
|
| 17 |
+
|
| 18 |
+
import copy
|
| 19 |
+
import logging
|
| 20 |
+
import time
|
| 21 |
+
from typing import Dict, List, Optional
|
| 22 |
+
import warnings
|
| 23 |
+
|
| 24 |
+
from google.cloud.aiplatform import initializer
|
| 25 |
+
from google.cloud.aiplatform import utils
|
| 26 |
+
from google.cloud.aiplatform.utils import resource_manager_utils
|
| 27 |
+
from google.cloud.aiplatform_v1beta1.types import persistent_resource_service
|
| 28 |
+
from google.cloud.aiplatform_v1beta1.types.machine_resources import NfsMount
|
| 29 |
+
from google.cloud.aiplatform_v1beta1.types.persistent_resource import (
|
| 30 |
+
PersistentResource,
|
| 31 |
+
RayLogsSpec,
|
| 32 |
+
RaySpec,
|
| 33 |
+
RayMetricSpec,
|
| 34 |
+
ResourcePool,
|
| 35 |
+
ResourceRuntimeSpec,
|
| 36 |
+
ServiceAccountSpec,
|
| 37 |
+
)
|
| 38 |
+
from google.cloud.aiplatform_v1beta1.types.service_networking import (
|
| 39 |
+
PscInterfaceConfig,
|
| 40 |
+
)
|
| 41 |
+
from google.cloud.aiplatform.vertex_ray.util import (
|
| 42 |
+
_gapic_utils,
|
| 43 |
+
_validation_utils,
|
| 44 |
+
resources,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
from google.protobuf import field_mask_pb2 # type: ignore
|
| 48 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 49 |
+
_V2_4_WARNING_MESSAGE,
|
| 50 |
+
_V2_9_WARNING_MESSAGE,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def create_ray_cluster(
|
| 55 |
+
head_node_type: Optional[resources.Resources] = resources.Resources(),
|
| 56 |
+
python_version: Optional[str] = None,
|
| 57 |
+
ray_version: Optional[str] = "2.47",
|
| 58 |
+
network: Optional[str] = None,
|
| 59 |
+
service_account: Optional[str] = None,
|
| 60 |
+
cluster_name: Optional[str] = None,
|
| 61 |
+
worker_node_types: Optional[List[resources.Resources]] = [resources.Resources()],
|
| 62 |
+
custom_images: Optional[resources.NodeImages] = None,
|
| 63 |
+
enable_metrics_collection: Optional[bool] = True,
|
| 64 |
+
enable_logging: Optional[bool] = True,
|
| 65 |
+
psc_interface_config: Optional[resources.PscIConfig] = None,
|
| 66 |
+
reserved_ip_ranges: Optional[List[str]] = None,
|
| 67 |
+
nfs_mounts: Optional[List[resources.NfsMount]] = None,
|
| 68 |
+
labels: Optional[Dict[str, str]] = None,
|
| 69 |
+
) -> str:
|
| 70 |
+
"""Create a ray cluster on the Vertex AI.
|
| 71 |
+
|
| 72 |
+
Sample usage:
|
| 73 |
+
|
| 74 |
+
from vertex_ray import Resources
|
| 75 |
+
|
| 76 |
+
head_node_type = Resources(
|
| 77 |
+
machine_type="n1-standard-8",
|
| 78 |
+
node_count=1,
|
| 79 |
+
accelerator_type="NVIDIA_TESLA_T4",
|
| 80 |
+
accelerator_count=1,
|
| 81 |
+
custom_image="us-docker.pkg.dev/my-project/ray-2-47-cpu-py3.11:latest", # Optional
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
worker_node_types = [Resources(
|
| 85 |
+
machine_type="n1-standard-8",
|
| 86 |
+
node_count=2,
|
| 87 |
+
accelerator_type="NVIDIA_TESLA_T4",
|
| 88 |
+
accelerator_count=1,
|
| 89 |
+
custom_image="us-docker.pkg.dev/my-project/ray-2-47-gpu-py3.11:latest", # Optional
|
| 90 |
+
)]
|
| 91 |
+
|
| 92 |
+
cluster_resource_name = vertex_ray.create_ray_cluster(
|
| 93 |
+
head_node_type=head_node_type,
|
| 94 |
+
network="projects/my-project-number/global/networks/my-vpc-name", # Optional
|
| 95 |
+
service_account="my-service-account@my-project-number.iam.gserviceaccount.com", # Optional
|
| 96 |
+
cluster_name="my-cluster-name", # Optional
|
| 97 |
+
worker_node_types=worker_node_types,
|
| 98 |
+
ray_version="2.47",
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
After a ray cluster is set up, you can call
|
| 102 |
+
`ray.init(f"vertex_ray://{cluster_resource_name}", runtime_env=...)` without
|
| 103 |
+
specifying ray cluster address to connect to the cluster. To shut down the
|
| 104 |
+
cluster you can call `ray.delete_ray_cluster()`.
|
| 105 |
+
Note: If the active ray cluster has not finished shutting down, you cannot
|
| 106 |
+
create a new ray cluster with the same cluster_name.
|
| 107 |
+
|
| 108 |
+
Args:
|
| 109 |
+
head_node_type: The head node resource. Resources.node_count must be 1.
|
| 110 |
+
If not set, default value of Resources() class will be used.
|
| 111 |
+
python_version: Python version for the ray cluster.
|
| 112 |
+
ray_version: Ray version for the ray cluster. Default is 2.47.1.
|
| 113 |
+
network: Virtual private cloud (VPC) network. For Ray Client, VPC
|
| 114 |
+
peering is required to connect to the Ray Cluster managed in the
|
| 115 |
+
Vertex API service. For Ray Job API, VPC network is not required
|
| 116 |
+
because Ray Cluster connection can be accessed through dashboard
|
| 117 |
+
address.
|
| 118 |
+
service_account: Service account to be used for running Ray programs on
|
| 119 |
+
the cluster.
|
| 120 |
+
cluster_name: This value may be up to 63 characters, and valid
|
| 121 |
+
characters are `[a-z0-9_-]`. The first character cannot be a number
|
| 122 |
+
or hyphen.
|
| 123 |
+
worker_node_types: The list of Resources of the worker nodes. The same
|
| 124 |
+
Resources object should not appear multiple times in the list.
|
| 125 |
+
custom_images: The NodeImages which specifies head node and worker nodes
|
| 126 |
+
images. All the workers will share the same image. If each Resource
|
| 127 |
+
has a specific custom image, use `Resources.custom_image` for
|
| 128 |
+
head/worker_node_type(s). Note that configuring `Resources.custom_image`
|
| 129 |
+
will override `custom_images` here. Allowlist only.
|
| 130 |
+
enable_metrics_collection: Enable Ray metrics collection for visualization.
|
| 131 |
+
enable_logging: Enable exporting Ray logs to Cloud Logging.
|
| 132 |
+
psc_interface_config: PSC-I config.
|
| 133 |
+
reserved_ip_ranges: A list of names for the reserved IP ranges under
|
| 134 |
+
the VPC network that can be used for this cluster. If set, we will
|
| 135 |
+
deploy the cluster within the provided IP ranges. Otherwise, the
|
| 136 |
+
cluster is deployed to any IP ranges under the provided VPC network.
|
| 137 |
+
Example: ["vertex-ai-ip-range"].
|
| 138 |
+
labels:
|
| 139 |
+
The labels with user-defined metadata to organize Ray cluster.
|
| 140 |
+
|
| 141 |
+
Label keys and values can be no longer than 64 characters (Unicode
|
| 142 |
+
codepoints), can only contain lowercase letters, numeric characters,
|
| 143 |
+
underscores and dashes. International characters are allowed.
|
| 144 |
+
|
| 145 |
+
See https://goo.gl/xmQnxf for more information and examples of labels.
|
| 146 |
+
|
| 147 |
+
Returns:
|
| 148 |
+
The cluster_resource_name of the initiated Ray cluster on Vertex.
|
| 149 |
+
Raise:
|
| 150 |
+
ValueError: If the cluster is not created successfully.
|
| 151 |
+
RuntimeError: If the ray_version is 2.4.
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
if network is None:
|
| 155 |
+
logging.info(
|
| 156 |
+
"[Ray on Vertex]: No VPC network configured. It is required for client connection."
|
| 157 |
+
)
|
| 158 |
+
if ray_version == "2.4":
|
| 159 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 160 |
+
if ray_version == "2.9.3":
|
| 161 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 162 |
+
local_ray_verion = _validation_utils.get_local_ray_version()
|
| 163 |
+
if ray_version != local_ray_verion:
|
| 164 |
+
if custom_images is None and head_node_type.custom_image is None:
|
| 165 |
+
install_ray_version = "2.47.1"
|
| 166 |
+
logging.info(
|
| 167 |
+
"[Ray on Vertex]: Local runtime has Ray version %s"
|
| 168 |
+
", but the requested cluster runtime has %s. Please "
|
| 169 |
+
"ensure that the Ray versions match for client connectivity. You may "
|
| 170 |
+
'"pip install --user --force-reinstall ray[default]==%s"'
|
| 171 |
+
" and restart runtime before cluster connection."
|
| 172 |
+
% (local_ray_verion, ray_version, install_ray_version)
|
| 173 |
+
)
|
| 174 |
+
else:
|
| 175 |
+
logging.info(
|
| 176 |
+
"[Ray on Vertex]: Local runtime has Ray version %s."
|
| 177 |
+
"Please ensure that the Ray versions match for client connectivity."
|
| 178 |
+
% local_ray_verion
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
if cluster_name is None:
|
| 182 |
+
cluster_name = "ray-cluster-" + utils.timestamped_unique_name()
|
| 183 |
+
|
| 184 |
+
if head_node_type:
|
| 185 |
+
if head_node_type.node_count != 1:
|
| 186 |
+
raise ValueError(
|
| 187 |
+
"[Ray on Vertex AI]: For head_node_type, "
|
| 188 |
+
+ "Resources.node_count must be 1."
|
| 189 |
+
)
|
| 190 |
+
if head_node_type.autoscaling_spec is not None:
|
| 191 |
+
raise ValueError(
|
| 192 |
+
"[Ray on Vertex AI]: For head_node_type, "
|
| 193 |
+
+ "Resources.autoscaling_spec must be None."
|
| 194 |
+
)
|
| 195 |
+
if (
|
| 196 |
+
head_node_type.accelerator_type is None
|
| 197 |
+
and head_node_type.accelerator_count > 0
|
| 198 |
+
):
|
| 199 |
+
raise ValueError(
|
| 200 |
+
"[Ray on Vertex]: accelerator_type must be specified when"
|
| 201 |
+
+ " accelerator_count is set to a value other than 0."
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
resource_pool_images = {}
|
| 205 |
+
|
| 206 |
+
# head node
|
| 207 |
+
resource_pool_0 = ResourcePool()
|
| 208 |
+
resource_pool_0.id = "head-node"
|
| 209 |
+
resource_pool_0.replica_count = head_node_type.node_count
|
| 210 |
+
resource_pool_0.machine_spec.machine_type = head_node_type.machine_type
|
| 211 |
+
resource_pool_0.machine_spec.accelerator_count = head_node_type.accelerator_count
|
| 212 |
+
resource_pool_0.machine_spec.accelerator_type = head_node_type.accelerator_type
|
| 213 |
+
resource_pool_0.disk_spec.boot_disk_type = head_node_type.boot_disk_type
|
| 214 |
+
resource_pool_0.disk_spec.boot_disk_size_gb = head_node_type.boot_disk_size_gb
|
| 215 |
+
|
| 216 |
+
enable_cuda = True if head_node_type.accelerator_count > 0 else False
|
| 217 |
+
if head_node_type.custom_image is not None:
|
| 218 |
+
image_uri = head_node_type.custom_image
|
| 219 |
+
elif custom_images is None:
|
| 220 |
+
image_uri = _validation_utils.get_image_uri(
|
| 221 |
+
ray_version, python_version, enable_cuda
|
| 222 |
+
)
|
| 223 |
+
elif custom_images.head is not None and custom_images.worker is not None:
|
| 224 |
+
image_uri = custom_images.head
|
| 225 |
+
else:
|
| 226 |
+
raise ValueError(
|
| 227 |
+
"[Ray on Vertex AI]: custom_images.head and custom_images.worker must be specified when custom_images is set."
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
resource_pool_images[resource_pool_0.id] = image_uri
|
| 231 |
+
|
| 232 |
+
worker_pools = []
|
| 233 |
+
i = 0
|
| 234 |
+
if worker_node_types:
|
| 235 |
+
for worker_node_type in worker_node_types:
|
| 236 |
+
if (
|
| 237 |
+
worker_node_type.accelerator_type is None
|
| 238 |
+
and worker_node_type.accelerator_count > 0
|
| 239 |
+
):
|
| 240 |
+
raise ValueError(
|
| 241 |
+
"[Ray on Vertex]: accelerator_type must be specified when"
|
| 242 |
+
+ " accelerator_count is set to a value other than 0."
|
| 243 |
+
)
|
| 244 |
+
additional_replica_count = resources._check_machine_spec_identical(
|
| 245 |
+
head_node_type, worker_node_type
|
| 246 |
+
)
|
| 247 |
+
if worker_node_type.autoscaling_spec is None:
|
| 248 |
+
# Worker and head share the same MachineSpec, merge them into the
|
| 249 |
+
# same ResourcePool
|
| 250 |
+
resource_pool_0.replica_count = (
|
| 251 |
+
resource_pool_0.replica_count + additional_replica_count
|
| 252 |
+
)
|
| 253 |
+
else:
|
| 254 |
+
if additional_replica_count > 0:
|
| 255 |
+
# Autoscaling for single ResourcePool (homogeneous cluster).
|
| 256 |
+
resource_pool_0.replica_count = None
|
| 257 |
+
resource_pool_0.autoscaling_spec.min_replica_count = (
|
| 258 |
+
worker_node_type.autoscaling_spec.min_replica_count
|
| 259 |
+
)
|
| 260 |
+
resource_pool_0.autoscaling_spec.max_replica_count = (
|
| 261 |
+
worker_node_type.autoscaling_spec.max_replica_count
|
| 262 |
+
)
|
| 263 |
+
if additional_replica_count == 0:
|
| 264 |
+
resource_pool = ResourcePool()
|
| 265 |
+
resource_pool.id = f"worker-pool{i+1}"
|
| 266 |
+
if worker_node_type.autoscaling_spec is None:
|
| 267 |
+
resource_pool.replica_count = worker_node_type.node_count
|
| 268 |
+
else:
|
| 269 |
+
# Autoscaling for worker ResourcePool.
|
| 270 |
+
resource_pool.autoscaling_spec.min_replica_count = (
|
| 271 |
+
worker_node_type.autoscaling_spec.min_replica_count
|
| 272 |
+
)
|
| 273 |
+
resource_pool.autoscaling_spec.max_replica_count = (
|
| 274 |
+
worker_node_type.autoscaling_spec.max_replica_count
|
| 275 |
+
)
|
| 276 |
+
resource_pool.machine_spec.machine_type = worker_node_type.machine_type
|
| 277 |
+
resource_pool.machine_spec.accelerator_count = (
|
| 278 |
+
worker_node_type.accelerator_count
|
| 279 |
+
)
|
| 280 |
+
resource_pool.machine_spec.accelerator_type = (
|
| 281 |
+
worker_node_type.accelerator_type
|
| 282 |
+
)
|
| 283 |
+
resource_pool.disk_spec.boot_disk_type = worker_node_type.boot_disk_type
|
| 284 |
+
resource_pool.disk_spec.boot_disk_size_gb = (
|
| 285 |
+
worker_node_type.boot_disk_size_gb
|
| 286 |
+
)
|
| 287 |
+
worker_pools.append(resource_pool)
|
| 288 |
+
enable_cuda = True if worker_node_type.accelerator_count > 0 else False
|
| 289 |
+
|
| 290 |
+
if worker_node_type.custom_image is not None:
|
| 291 |
+
image_uri = worker_node_type.custom_image
|
| 292 |
+
elif custom_images is None:
|
| 293 |
+
image_uri = _validation_utils.get_image_uri(
|
| 294 |
+
ray_version, python_version, enable_cuda
|
| 295 |
+
)
|
| 296 |
+
else:
|
| 297 |
+
image_uri = custom_images.worker
|
| 298 |
+
|
| 299 |
+
resource_pool_images[resource_pool.id] = image_uri
|
| 300 |
+
|
| 301 |
+
i += 1
|
| 302 |
+
|
| 303 |
+
resource_pools = [resource_pool_0] + worker_pools
|
| 304 |
+
|
| 305 |
+
metrics_collection_disabled = not enable_metrics_collection
|
| 306 |
+
ray_metric_spec = RayMetricSpec(disabled=metrics_collection_disabled)
|
| 307 |
+
|
| 308 |
+
logging_disabled = not enable_logging
|
| 309 |
+
ray_logs_spec = RayLogsSpec(disabled=logging_disabled)
|
| 310 |
+
|
| 311 |
+
ray_spec = RaySpec(
|
| 312 |
+
resource_pool_images=resource_pool_images,
|
| 313 |
+
ray_metric_spec=ray_metric_spec,
|
| 314 |
+
ray_logs_spec=ray_logs_spec,
|
| 315 |
+
)
|
| 316 |
+
if nfs_mounts:
|
| 317 |
+
gapic_nfs_mounts = []
|
| 318 |
+
for nfs_mount in nfs_mounts:
|
| 319 |
+
gapic_nfs_mounts.append(
|
| 320 |
+
NfsMount(
|
| 321 |
+
server=nfs_mount.server,
|
| 322 |
+
path=nfs_mount.path,
|
| 323 |
+
mount_point=nfs_mount.mount_point,
|
| 324 |
+
)
|
| 325 |
+
)
|
| 326 |
+
ray_spec.nfs_mounts = gapic_nfs_mounts
|
| 327 |
+
if service_account:
|
| 328 |
+
service_account_spec = ServiceAccountSpec(
|
| 329 |
+
enable_custom_service_account=True,
|
| 330 |
+
service_account=service_account,
|
| 331 |
+
)
|
| 332 |
+
resource_runtime_spec = ResourceRuntimeSpec(
|
| 333 |
+
ray_spec=ray_spec,
|
| 334 |
+
service_account_spec=service_account_spec,
|
| 335 |
+
)
|
| 336 |
+
else:
|
| 337 |
+
resource_runtime_spec = ResourceRuntimeSpec(ray_spec=ray_spec)
|
| 338 |
+
if psc_interface_config:
|
| 339 |
+
gapic_psc_interface_config = PscInterfaceConfig(
|
| 340 |
+
network_attachment=psc_interface_config.network_attachment,
|
| 341 |
+
)
|
| 342 |
+
else:
|
| 343 |
+
gapic_psc_interface_config = None
|
| 344 |
+
|
| 345 |
+
persistent_resource = PersistentResource(
|
| 346 |
+
resource_pools=resource_pools,
|
| 347 |
+
network=network,
|
| 348 |
+
labels=labels,
|
| 349 |
+
resource_runtime_spec=resource_runtime_spec,
|
| 350 |
+
psc_interface_config=gapic_psc_interface_config,
|
| 351 |
+
reserved_ip_ranges=reserved_ip_ranges,
|
| 352 |
+
)
|
| 353 |
+
|
| 354 |
+
location = initializer.global_config.location
|
| 355 |
+
project_id = initializer.global_config.project
|
| 356 |
+
project_number = resource_manager_utils.get_project_number(project_id)
|
| 357 |
+
|
| 358 |
+
parent = f"projects/{project_number}/locations/{location}"
|
| 359 |
+
request = persistent_resource_service.CreatePersistentResourceRequest(
|
| 360 |
+
parent=parent,
|
| 361 |
+
persistent_resource=persistent_resource,
|
| 362 |
+
persistent_resource_id=cluster_name,
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
client = _gapic_utils.create_persistent_resource_client()
|
| 366 |
+
try:
|
| 367 |
+
_ = client.create_persistent_resource(request)
|
| 368 |
+
except Exception as e:
|
| 369 |
+
raise ValueError("Failed in cluster creation due to: ", e) from e
|
| 370 |
+
|
| 371 |
+
# Get persisent resource
|
| 372 |
+
cluster_resource_name = f"{parent}/persistentResources/{cluster_name}"
|
| 373 |
+
response = _gapic_utils.get_persistent_resource(
|
| 374 |
+
persistent_resource_name=cluster_resource_name,
|
| 375 |
+
tolerance=1, # allow 1 retry to avoid get request before creation
|
| 376 |
+
)
|
| 377 |
+
return response.name
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def delete_ray_cluster(cluster_resource_name: str) -> None:
|
| 381 |
+
"""Delete Ray Cluster.
|
| 382 |
+
|
| 383 |
+
Args:
|
| 384 |
+
cluster_resource_name: Cluster resource name.
|
| 385 |
+
Raises:
|
| 386 |
+
FailedPrecondition: If the cluster is deleted already.
|
| 387 |
+
"""
|
| 388 |
+
client = _gapic_utils.create_persistent_resource_client()
|
| 389 |
+
request = persistent_resource_service.DeletePersistentResourceRequest(
|
| 390 |
+
name=cluster_resource_name
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
try:
|
| 394 |
+
client.delete_persistent_resource(request)
|
| 395 |
+
print("[Ray on Vertex AI]: Successfully deleted the cluster.")
|
| 396 |
+
except Exception as e:
|
| 397 |
+
raise ValueError(
|
| 398 |
+
"[Ray on Vertex AI]: Failed in cluster deletion due to: ", e
|
| 399 |
+
) from e
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def get_ray_cluster(cluster_resource_name: str) -> resources.Cluster:
|
| 403 |
+
"""Get Ray Cluster.
|
| 404 |
+
|
| 405 |
+
Args:
|
| 406 |
+
cluster_resource_name: Cluster resource name.
|
| 407 |
+
Returns:
|
| 408 |
+
A Cluster object.
|
| 409 |
+
"""
|
| 410 |
+
client = _gapic_utils.create_persistent_resource_client()
|
| 411 |
+
request = persistent_resource_service.GetPersistentResourceRequest(
|
| 412 |
+
name=cluster_resource_name
|
| 413 |
+
)
|
| 414 |
+
try:
|
| 415 |
+
response = client.get_persistent_resource(request)
|
| 416 |
+
except Exception as e:
|
| 417 |
+
raise ValueError(
|
| 418 |
+
"[Ray on Vertex AI]: Failed in getting the cluster due to: ", e
|
| 419 |
+
) from e
|
| 420 |
+
|
| 421 |
+
cluster = _gapic_utils.persistent_resource_to_cluster(persistent_resource=response)
|
| 422 |
+
if cluster:
|
| 423 |
+
return cluster
|
| 424 |
+
raise ValueError(
|
| 425 |
+
"[Ray on Vertex AI]: Please delete and recreate the cluster (The cluster is not a Ray cluster or the cluster image is outdated)."
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
def list_ray_clusters() -> List[resources.Cluster]:
|
| 430 |
+
"""List Ray Clusters under the currently authenticated project.
|
| 431 |
+
|
| 432 |
+
Returns:
|
| 433 |
+
List of Cluster objects that exists in the current authorized project.
|
| 434 |
+
"""
|
| 435 |
+
location = initializer.global_config.location
|
| 436 |
+
project_id = initializer.global_config.project
|
| 437 |
+
project_number = resource_manager_utils.get_project_number(project_id)
|
| 438 |
+
parent = f"projects/{project_number}/locations/{location}"
|
| 439 |
+
request = persistent_resource_service.ListPersistentResourcesRequest(
|
| 440 |
+
parent=parent,
|
| 441 |
+
)
|
| 442 |
+
client = _gapic_utils.create_persistent_resource_client()
|
| 443 |
+
try:
|
| 444 |
+
response = client.list_persistent_resources(request)
|
| 445 |
+
except Exception as e:
|
| 446 |
+
raise ValueError(
|
| 447 |
+
"[Ray on Vertex AI]: Failed in listing the clusters due to: ", e
|
| 448 |
+
) from e
|
| 449 |
+
|
| 450 |
+
ray_clusters = []
|
| 451 |
+
for persistent_resource in response:
|
| 452 |
+
ray_cluster = _gapic_utils.persistent_resource_to_cluster(
|
| 453 |
+
persistent_resource=persistent_resource
|
| 454 |
+
)
|
| 455 |
+
if ray_cluster:
|
| 456 |
+
ray_clusters.append(ray_cluster)
|
| 457 |
+
|
| 458 |
+
return ray_clusters
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def update_ray_cluster(
|
| 462 |
+
cluster_resource_name: str, worker_node_types: List[resources.Resources]
|
| 463 |
+
) -> str:
|
| 464 |
+
"""Update Ray Cluster (currently support resizing node counts for worker nodes).
|
| 465 |
+
|
| 466 |
+
Sample usage:
|
| 467 |
+
|
| 468 |
+
my_cluster = vertex_ray.get_ray_cluster(
|
| 469 |
+
cluster_resource_name=my_existing_cluster_resource_name,
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
# Declaration to resize all the worker_node_type to node_count=1
|
| 473 |
+
new_worker_node_types = []
|
| 474 |
+
for worker_node_type in my_cluster.worker_node_types:
|
| 475 |
+
worker_node_type.node_count = 1
|
| 476 |
+
new_worker_node_types.append(worker_node_type)
|
| 477 |
+
|
| 478 |
+
# Execution to update new node_count (block until complete)
|
| 479 |
+
vertex_ray.update_ray_cluster(
|
| 480 |
+
cluster_resource_name=my_cluster.cluster_resource_name,
|
| 481 |
+
worker_node_types=new_worker_node_types,
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
Args:
|
| 485 |
+
cluster_resource_name:
|
| 486 |
+
worker_node_types: The list of Resources of the resized worker nodes.
|
| 487 |
+
The same Resources object should not appear multiple times in the list.
|
| 488 |
+
Returns:
|
| 489 |
+
The cluster_resource_name of the Ray cluster on Vertex.
|
| 490 |
+
"""
|
| 491 |
+
# worker_node_types should not be duplicated.
|
| 492 |
+
for i in range(len(worker_node_types)):
|
| 493 |
+
for j in range(len(worker_node_types)):
|
| 494 |
+
additional_replica_count = resources._check_machine_spec_identical(
|
| 495 |
+
worker_node_types[i], worker_node_types[j]
|
| 496 |
+
)
|
| 497 |
+
if additional_replica_count > 0 and i != j:
|
| 498 |
+
raise ValueError(
|
| 499 |
+
"[Ray on Vertex AI]: Worker_node_types have duplicate "
|
| 500 |
+
+ f"machine specs: {worker_node_types[i]} "
|
| 501 |
+
+ f"and {worker_node_types[j]}"
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
persistent_resource = _gapic_utils.get_persistent_resource(
|
| 505 |
+
persistent_resource_name=cluster_resource_name
|
| 506 |
+
)
|
| 507 |
+
|
| 508 |
+
current_persistent_resource = copy.deepcopy(persistent_resource)
|
| 509 |
+
current_persistent_resource.resource_pools[0].replica_count = 1
|
| 510 |
+
|
| 511 |
+
previous_ray_cluster = get_ray_cluster(cluster_resource_name)
|
| 512 |
+
head_node_type = previous_ray_cluster.head_node_type
|
| 513 |
+
previous_worker_node_types = previous_ray_cluster.worker_node_types
|
| 514 |
+
|
| 515 |
+
# new worker_node_types and previous_worker_node_types should be the same length.
|
| 516 |
+
if len(worker_node_types) != len(previous_worker_node_types):
|
| 517 |
+
raise ValueError(
|
| 518 |
+
"[Ray on Vertex AI]: Desired number of worker_node_types "
|
| 519 |
+
+ "(%i) does not match the number of the "
|
| 520 |
+
+ "existing worker_node_type(%i).",
|
| 521 |
+
len(worker_node_types),
|
| 522 |
+
len(previous_worker_node_types),
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
# Merge worker_node_type and head_node_type if they share
|
| 526 |
+
# the same machine spec.
|
| 527 |
+
not_merged = 1
|
| 528 |
+
for i in range(len(worker_node_types)):
|
| 529 |
+
additional_replica_count = resources._check_machine_spec_identical(
|
| 530 |
+
head_node_type, worker_node_types[i]
|
| 531 |
+
)
|
| 532 |
+
if additional_replica_count != 0 or (
|
| 533 |
+
additional_replica_count == 0 and worker_node_types[i].node_count == 0
|
| 534 |
+
):
|
| 535 |
+
# Merge the 1st duplicated worker with head, allow scale down to 0 worker
|
| 536 |
+
current_persistent_resource.resource_pools[0].replica_count = (
|
| 537 |
+
1 + additional_replica_count
|
| 538 |
+
)
|
| 539 |
+
# Reset not_merged
|
| 540 |
+
not_merged = 0
|
| 541 |
+
else:
|
| 542 |
+
# No duplication w/ head node, write the 2nd worker node to the 2nd resource pool.
|
| 543 |
+
current_persistent_resource.resource_pools[i + not_merged].replica_count = (
|
| 544 |
+
worker_node_types[i].node_count
|
| 545 |
+
)
|
| 546 |
+
# New worker_node_type.node_count should be >=1 unless the worker_node_type
|
| 547 |
+
# and head_node_type are merged due to the same machine specs.
|
| 548 |
+
if worker_node_types[i].node_count == 0:
|
| 549 |
+
raise ValueError(
|
| 550 |
+
"[Ray on Vertex AI]: Worker_node_type "
|
| 551 |
+
+ f"({worker_node_types[i]}) must update to >= 1 nodes",
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
request = persistent_resource_service.UpdatePersistentResourceRequest(
|
| 555 |
+
persistent_resource=current_persistent_resource,
|
| 556 |
+
update_mask=field_mask_pb2.FieldMask(paths=["resource_pools.replica_count"]),
|
| 557 |
+
)
|
| 558 |
+
client = _gapic_utils.create_persistent_resource_client()
|
| 559 |
+
try:
|
| 560 |
+
operation_future = client.update_persistent_resource(request)
|
| 561 |
+
except Exception as e:
|
| 562 |
+
raise ValueError(
|
| 563 |
+
"[Ray on Vertex AI]: Failed in updating the cluster due to: ", e
|
| 564 |
+
) from e
|
| 565 |
+
|
| 566 |
+
# block before returning
|
| 567 |
+
start_time = time.time()
|
| 568 |
+
response = operation_future.result()
|
| 569 |
+
duration = (time.time() - start_time) // 60
|
| 570 |
+
print(
|
| 571 |
+
"[Ray on Vertex AI]: Successfully updated the cluster ({} mininutes elapsed).".format(
|
| 572 |
+
duration
|
| 573 |
+
)
|
| 574 |
+
)
|
| 575 |
+
return response.name
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/dashboard_sdk.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2025 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 |
+
#
|
| 17 |
+
|
| 18 |
+
"""Utility to interact with Ray-on-Vertex dashboard."""
|
| 19 |
+
|
| 20 |
+
from ray.dashboard.modules import dashboard_sdk as oss_dashboard_sdk
|
| 21 |
+
|
| 22 |
+
from .util import _gapic_utils
|
| 23 |
+
from .util import _validation_utils
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_job_submission_client_cluster_info(
|
| 27 |
+
address: str, *args, **kwargs
|
| 28 |
+
) -> oss_dashboard_sdk.ClusterInfo:
|
| 29 |
+
"""A vertex_ray implementation of get_job_submission_client_cluster_info().
|
| 30 |
+
|
| 31 |
+
Implements
|
| 32 |
+
https://github.com/ray-project/ray/blob/ray-2.47.1/python/ray/dashboard/modules/dashboard_sdk.py#L84
|
| 33 |
+
This will be called in from Ray Job API Python client.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
address: Address without the module prefix `vertex_ray` but otherwise
|
| 37 |
+
the same format as passed to ray.init(address="vertex_ray://...").
|
| 38 |
+
*args: Remainder of positional args that might be passed down from
|
| 39 |
+
the framework.
|
| 40 |
+
**kwargs: Remainder of keyword args that might be passed down from
|
| 41 |
+
the framework.
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
An instance of ClusterInfo that contains address, cookies and
|
| 45 |
+
metadata for SubmissionClient to use.
|
| 46 |
+
|
| 47 |
+
Raises:
|
| 48 |
+
RuntimeError if head_address is None.
|
| 49 |
+
"""
|
| 50 |
+
if _validation_utils.valid_dashboard_address(address):
|
| 51 |
+
dashboard_address = address
|
| 52 |
+
else:
|
| 53 |
+
address = _validation_utils.maybe_reconstruct_resource_name(address)
|
| 54 |
+
_validation_utils.valid_resource_name(address)
|
| 55 |
+
|
| 56 |
+
resource_name = address
|
| 57 |
+
response = _gapic_utils.get_persistent_resource(resource_name)
|
| 58 |
+
|
| 59 |
+
dashboard_address = response.resource_runtime.access_uris.get(
|
| 60 |
+
"RAY_DASHBOARD_URI", None
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if dashboard_address is None:
|
| 64 |
+
raise RuntimeError(
|
| 65 |
+
"[Ray on Vertex AI]: Unable to obtain a response from the backend."
|
| 66 |
+
)
|
| 67 |
+
# If passing the dashboard uri, programmatically get headers
|
| 68 |
+
bearer_token = _validation_utils.get_bearer_token()
|
| 69 |
+
if kwargs.get("headers", None) is None:
|
| 70 |
+
kwargs["headers"] = {
|
| 71 |
+
"Content-Type": "application/json",
|
| 72 |
+
"Authorization": "Bearer {}".format(bearer_token),
|
| 73 |
+
}
|
| 74 |
+
return oss_dashboard_sdk.get_job_submission_client_cluster_info(
|
| 75 |
+
address=dashboard_address,
|
| 76 |
+
_use_tls=True,
|
| 77 |
+
*args,
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2024 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 |
+
#
|
| 17 |
+
import warnings
|
| 18 |
+
import ray.data
|
| 19 |
+
from ray.data.dataset import Dataset
|
| 20 |
+
from typing import Any, Dict, Optional
|
| 21 |
+
|
| 22 |
+
from google.cloud.aiplatform.vertex_ray.bigquery_datasource import (
|
| 23 |
+
_BigQueryDatasource,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
from google.cloud.aiplatform.vertex_ray.bigquery_datasink import (
|
| 28 |
+
_BigQueryDatasink,
|
| 29 |
+
)
|
| 30 |
+
except ImportError:
|
| 31 |
+
_BigQueryDatasink = None
|
| 32 |
+
|
| 33 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 34 |
+
_V2_4_WARNING_MESSAGE,
|
| 35 |
+
_V2_9_WARNING_MESSAGE,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def read_bigquery(
|
| 40 |
+
project_id: Optional[str] = None,
|
| 41 |
+
dataset: Optional[str] = None,
|
| 42 |
+
query: Optional[str] = None,
|
| 43 |
+
*,
|
| 44 |
+
parallelism: int = -1,
|
| 45 |
+
ray_remote_args: Dict[str, Any] = None,
|
| 46 |
+
concurrency: Optional[int] = None,
|
| 47 |
+
override_num_blocks: Optional[int] = None,
|
| 48 |
+
) -> Dataset:
|
| 49 |
+
"""Create a dataset from BigQuery.
|
| 50 |
+
|
| 51 |
+
The data to read from is specified via the ``project_id``, ``dataset``
|
| 52 |
+
and/or ``query`` parameters.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
project_id: The name of the associated Google Cloud Project that hosts
|
| 56 |
+
the dataset to read.
|
| 57 |
+
dataset: The name of the dataset hosted in BigQuery in the format of
|
| 58 |
+
``dataset_id.table_id``. Both the dataset_id and table_id must exist
|
| 59 |
+
otherwise an exception will be raised.
|
| 60 |
+
query: The query to execute. The dataset is created from the results of
|
| 61 |
+
executing the query if provided. Otherwise, the entire dataset is read.
|
| 62 |
+
For query syntax guidelines, see
|
| 63 |
+
https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
|
| 64 |
+
parallelism: 2.33.0, 2.42.0, 2.47.1: This argument is deprecated. Use
|
| 65 |
+
``override_num_blocks`` argument. 2.9.3: The requested parallelism of
|
| 66 |
+
the read. If -1, it will be automatically chosen based on the available
|
| 67 |
+
cluster resources and estimated in-memory data size.
|
| 68 |
+
ray_remote_args: kwargs passed to ray.remote in the read tasks.
|
| 69 |
+
concurrency: Supported for 2.33.0, 2.42.0 and 2.47.1 only: The maximum number of
|
| 70 |
+
Ray tasks to run concurrently. Set this to control number of tasks to
|
| 71 |
+
run concurrently. This doesn't change the total number of tasks run or
|
| 72 |
+
the total number of output blocks. By default, concurrency is
|
| 73 |
+
dynamically decided based on the available resources.
|
| 74 |
+
override_num_blocks: Supported for 2.33.0, 2.42.0 and 2.47.1 only: Override the
|
| 75 |
+
number of output blocks from all read tasks. By default, the number of
|
| 76 |
+
output blocks is dynamically decided based on input data size and
|
| 77 |
+
available resources. You shouldn't manually set this value in most
|
| 78 |
+
cases.
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
Dataset producing rows from the results of executing the query
|
| 82 |
+
or reading the entire dataset on the specified BigQuery dataset.
|
| 83 |
+
"""
|
| 84 |
+
datasource = _BigQueryDatasource(
|
| 85 |
+
project_id=project_id,
|
| 86 |
+
dataset=dataset,
|
| 87 |
+
query=query,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if ray.__version__ == "2.9.3":
|
| 91 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 92 |
+
# Concurrency and override_num_blocks are not supported in 2.9.3
|
| 93 |
+
return ray.data.read_datasource(
|
| 94 |
+
datasource=datasource,
|
| 95 |
+
parallelism=parallelism,
|
| 96 |
+
ray_remote_args=ray_remote_args,
|
| 97 |
+
)
|
| 98 |
+
elif ray.__version__ in ("2.33.0", "2.42.0", "2.47.1"):
|
| 99 |
+
return ray.data.read_datasource(
|
| 100 |
+
datasource=datasource,
|
| 101 |
+
parallelism=parallelism,
|
| 102 |
+
ray_remote_args=ray_remote_args,
|
| 103 |
+
concurrency=concurrency,
|
| 104 |
+
override_num_blocks=override_num_blocks,
|
| 105 |
+
)
|
| 106 |
+
else:
|
| 107 |
+
raise ImportError(
|
| 108 |
+
f"[Ray on Vertex AI]: Unsupported version {ray.__version__}."
|
| 109 |
+
+ "Only 2.47.1, 2.42.0, 2.33.0, and 2.9.3 are supported."
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def write_bigquery(
|
| 114 |
+
ds: Dataset,
|
| 115 |
+
project_id: Optional[str] = None,
|
| 116 |
+
dataset: Optional[str] = None,
|
| 117 |
+
max_retry_cnt: int = 10,
|
| 118 |
+
ray_remote_args: Dict[str, Any] = None,
|
| 119 |
+
overwrite_table: Optional[bool] = True,
|
| 120 |
+
concurrency: Optional[int] = None,
|
| 121 |
+
) -> Any:
|
| 122 |
+
"""Write the dataset to a BigQuery dataset table.
|
| 123 |
+
|
| 124 |
+
Args:
|
| 125 |
+
ds: The dataset to write.
|
| 126 |
+
project_id: The name of the associated Google Cloud Project that hosts
|
| 127 |
+
the dataset table to write to.
|
| 128 |
+
dataset: The name of the dataset table hosted in BigQuery in the format of
|
| 129 |
+
``dataset_id.table_id``.
|
| 130 |
+
The dataset table is created if it doesn't already exist.
|
| 131 |
+
In 2.9.3, the table_id is overwritten if it exists.
|
| 132 |
+
max_retry_cnt: The maximum number of retries that an individual block write
|
| 133 |
+
is retried due to BigQuery rate limiting errors.
|
| 134 |
+
The default number of retries is 10.
|
| 135 |
+
ray_remote_args: kwargs passed to ray.remote in the write tasks.
|
| 136 |
+
overwrite_table: Not supported in 2.9.3.
|
| 137 |
+
2.33.0, 2.42.0, 2.47.1: Whether the write will overwrite the table
|
| 138 |
+
if it already exists. The default behavior is to overwrite the table.
|
| 139 |
+
If false, will append to the table if it exists.
|
| 140 |
+
concurrency: Not supported in 2.9.3.
|
| 141 |
+
2.33.0, 2.42.0, 2.47.1: The maximum number of Ray tasks to run concurrently.
|
| 142 |
+
Set this to control number of tasks to run concurrently. This doesn't
|
| 143 |
+
change the total number of tasks run or the total number of output blocks.
|
| 144 |
+
By default, concurrency is dynamically decided based on the available
|
| 145 |
+
resources.
|
| 146 |
+
"""
|
| 147 |
+
if ray.__version__ == "2.4.0":
|
| 148 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 149 |
+
|
| 150 |
+
elif ray.__version__ in ("2.9.3", "2.33.0", "2.42.0", "2.47.1"):
|
| 151 |
+
if ray.__version__ == "2.9.3":
|
| 152 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 153 |
+
if ray_remote_args is None:
|
| 154 |
+
ray_remote_args = {}
|
| 155 |
+
|
| 156 |
+
# Each write task will launch individual remote tasks to write each block
|
| 157 |
+
# To avoid duplicate block writes, the write task should not be retried
|
| 158 |
+
if ray_remote_args.get("max_retries", 0) != 0:
|
| 159 |
+
print(
|
| 160 |
+
"[Ray on Vertex AI]: The max_retries of a BigQuery Write "
|
| 161 |
+
"Task should be set to 0 to avoid duplicate writes."
|
| 162 |
+
)
|
| 163 |
+
else:
|
| 164 |
+
ray_remote_args["max_retries"] = 0
|
| 165 |
+
|
| 166 |
+
if ray.__version__ == "2.9.3":
|
| 167 |
+
# Concurrency and overwrite_table are not supported in 2.9.3
|
| 168 |
+
datasink = _BigQueryDatasink(
|
| 169 |
+
project_id=project_id,
|
| 170 |
+
dataset=dataset,
|
| 171 |
+
max_retry_cnt=max_retry_cnt,
|
| 172 |
+
)
|
| 173 |
+
return ds.write_datasink(
|
| 174 |
+
datasink=datasink,
|
| 175 |
+
ray_remote_args=ray_remote_args,
|
| 176 |
+
)
|
| 177 |
+
elif ray.__version__ in ("2.33.0", "2.42.0", "2.47.1"):
|
| 178 |
+
datasink = _BigQueryDatasink(
|
| 179 |
+
project_id=project_id,
|
| 180 |
+
dataset=dataset,
|
| 181 |
+
max_retry_cnt=max_retry_cnt,
|
| 182 |
+
overwrite_table=overwrite_table,
|
| 183 |
+
)
|
| 184 |
+
return ds.write_datasink(
|
| 185 |
+
datasink=datasink,
|
| 186 |
+
ray_remote_args=ray_remote_args,
|
| 187 |
+
concurrency=concurrency,
|
| 188 |
+
)
|
| 189 |
+
else:
|
| 190 |
+
raise ImportError(
|
| 191 |
+
f"[Ray on Vertex AI]: Unsupported version {ray.__version__}."
|
| 192 |
+
+ "Only 2.47.1, 2.42.0, 2.33.0 and 2.9.3 are supported."
|
| 193 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI Prediction."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/sklearn/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI Prediction Tensorflow."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
from .register import register_sklearn
|
| 21 |
+
|
| 22 |
+
__all__ = ("register_sklearn",)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/sklearn/register.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regsiter Scikit Learn for Ray on Vertex AI."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import pickle
|
| 22 |
+
import warnings
|
| 23 |
+
import ray
|
| 24 |
+
import ray.cloudpickle as cpickle
|
| 25 |
+
import tempfile
|
| 26 |
+
from typing import Optional, TYPE_CHECKING
|
| 27 |
+
|
| 28 |
+
from google.cloud import aiplatform
|
| 29 |
+
from google.cloud.aiplatform import initializer
|
| 30 |
+
from google.cloud.aiplatform import utils
|
| 31 |
+
from google.cloud.aiplatform.utils import gcs_utils
|
| 32 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import constants
|
| 33 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import (
|
| 34 |
+
predict_utils,
|
| 35 |
+
)
|
| 36 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 37 |
+
_V2_4_WARNING_MESSAGE,
|
| 38 |
+
_V2_9_WARNING_MESSAGE,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
from ray.train import sklearn as ray_sklearn
|
| 44 |
+
|
| 45 |
+
if TYPE_CHECKING:
|
| 46 |
+
import sklearn
|
| 47 |
+
|
| 48 |
+
except ImportError as ie:
|
| 49 |
+
if ray.__version__ < "2.42.0":
|
| 50 |
+
raise ModuleNotFoundError("Sklearn isn't installed.") from ie
|
| 51 |
+
else:
|
| 52 |
+
sklearn = None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def register_sklearn(
|
| 56 |
+
checkpoint: "ray_sklearn.SklearnCheckpoint",
|
| 57 |
+
artifact_uri: Optional[str] = None,
|
| 58 |
+
display_name: Optional[str] = None,
|
| 59 |
+
**kwargs,
|
| 60 |
+
) -> aiplatform.Model:
|
| 61 |
+
"""Uploads a Ray Sklearn Checkpoint as Sklearn Model to Model Registry.
|
| 62 |
+
|
| 63 |
+
Example usage:
|
| 64 |
+
from vertex_ray.predict import sklearn
|
| 65 |
+
from ray.train.sklearn import SklearnCheckpoint
|
| 66 |
+
|
| 67 |
+
trainer = SklearnTrainer(estimator=RandomForestClassifier, ...)
|
| 68 |
+
result = trainer.fit()
|
| 69 |
+
sklearn_checkpoint = SklearnCheckpoint.from_checkpoint(result.checkpoint)
|
| 70 |
+
|
| 71 |
+
my_model = sklearn.register_sklearn(
|
| 72 |
+
checkpoint=sklearn_checkpoint,
|
| 73 |
+
artifact_uri="gs://{gcs-bucket-name}/path/to/store"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
checkpoint: SklearnCheckpoint instance.
|
| 79 |
+
artifact_uri (str):
|
| 80 |
+
Optional. The path to the directory where Model Artifacts will be saved. If
|
| 81 |
+
not set, will use staging bucket set in aiplatform.init().
|
| 82 |
+
display_name (str):
|
| 83 |
+
Optional. The display name of the Model. The name can be up to 128
|
| 84 |
+
characters long and can be consist of any UTF-8 characters.
|
| 85 |
+
**kwargs:
|
| 86 |
+
Any kwargs will be passed to aiplatform.Model registration.
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
model (aiplatform.Model):
|
| 90 |
+
Instantiated representation of the uploaded model resource.
|
| 91 |
+
|
| 92 |
+
Raises:
|
| 93 |
+
ValueError: Invalid Argument.
|
| 94 |
+
RuntimeError: Only Ray version 2.9.3 is supported.
|
| 95 |
+
"""
|
| 96 |
+
ray_version = ray.__version__
|
| 97 |
+
if ray_version != "2.9.3":
|
| 98 |
+
raise RuntimeError(
|
| 99 |
+
f"Ray version {ray_version} is not supported to upload Sklearn"
|
| 100 |
+
" model to Vertex Model Registry yet. Please use Ray 2.9.3."
|
| 101 |
+
)
|
| 102 |
+
if ray_version == "2.9.3":
|
| 103 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 104 |
+
|
| 105 |
+
artifact_uri = artifact_uri or initializer.global_config.staging_bucket
|
| 106 |
+
predict_utils.validate_artifact_uri(artifact_uri)
|
| 107 |
+
display_model_name = (
|
| 108 |
+
(f"ray-on-vertex-registered-sklearn-model-{utils.timestamped_unique_name()}")
|
| 109 |
+
if display_name is None
|
| 110 |
+
else display_name
|
| 111 |
+
)
|
| 112 |
+
estimator = _get_estimator_from(checkpoint)
|
| 113 |
+
|
| 114 |
+
model_dir = os.path.join(artifact_uri, display_model_name)
|
| 115 |
+
file_path = os.path.join(model_dir, constants._PICKLE_FILE_NAME)
|
| 116 |
+
|
| 117 |
+
with tempfile.NamedTemporaryFile(suffix=constants._PICKLE_EXTENTION) as temp_file:
|
| 118 |
+
pickle.dump(estimator, temp_file)
|
| 119 |
+
gcs_utils.upload_to_gcs(temp_file.name, file_path)
|
| 120 |
+
return aiplatform.Model.upload_scikit_learn_model_file(
|
| 121 |
+
model_file_path=temp_file.name, display_name=display_model_name, **kwargs
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def _get_estimator_from(
|
| 126 |
+
checkpoint: "ray_sklearn.SklearnCheckpoint",
|
| 127 |
+
) -> "sklearn.base.BaseEstimator":
|
| 128 |
+
"""Converts a SklearnCheckpoint to sklearn estimator.
|
| 129 |
+
|
| 130 |
+
Args:
|
| 131 |
+
checkpoint: SklearnCheckpoint instance.
|
| 132 |
+
|
| 133 |
+
Returns:
|
| 134 |
+
A Sklearn BaseEstimator
|
| 135 |
+
|
| 136 |
+
Raises:
|
| 137 |
+
ValueError: Invalid Argument.
|
| 138 |
+
RuntimeError: Model not found.
|
| 139 |
+
RuntimeError: Ray version 2.4 is not supported.
|
| 140 |
+
RuntimeError: Only Ray version 2.9.3 is supported.
|
| 141 |
+
"""
|
| 142 |
+
|
| 143 |
+
ray_version = ray.__version__
|
| 144 |
+
if ray_version == "2.4.0":
|
| 145 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 146 |
+
if ray_version != "2.9.3":
|
| 147 |
+
raise RuntimeError(
|
| 148 |
+
f"Ray version {ray_version} is not supported to convert a Sklearn"
|
| 149 |
+
" checkpoint to sklearn estimator on Vertex yet. Please use Ray 2.9.3."
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
return checkpoint.get_model()
|
| 154 |
+
except AttributeError:
|
| 155 |
+
model_file_name = ray.train.sklearn.SklearnCheckpoint.MODEL_FILENAME
|
| 156 |
+
|
| 157 |
+
model_path = os.path.join(checkpoint.path, model_file_name)
|
| 158 |
+
|
| 159 |
+
if os.path.exists(model_path):
|
| 160 |
+
with open(model_path, mode="rb") as f:
|
| 161 |
+
obj = pickle.load(f)
|
| 162 |
+
else:
|
| 163 |
+
try:
|
| 164 |
+
# Download from GCS to temp and then load_model
|
| 165 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 166 |
+
gcs_utils.download_from_gcs("gs://" + checkpoint.path, temp_dir)
|
| 167 |
+
with open(f"{temp_dir}/{model_file_name}", mode="rb") as f:
|
| 168 |
+
obj = cpickle.load(f)
|
| 169 |
+
except Exception as e:
|
| 170 |
+
raise RuntimeError(
|
| 171 |
+
f"{model_file_name} not found in this checkpoint due to: {e}."
|
| 172 |
+
)
|
| 173 |
+
return obj
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/tensorflow/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI Prediction Tensorflow."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
from .register import register_tensorflow
|
| 21 |
+
|
| 22 |
+
__all__ = ("register_tensorflow",)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/tensorflow/register.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
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 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
import os
|
| 20 |
+
import logging
|
| 21 |
+
import ray
|
| 22 |
+
from typing import Callable, Optional, Union, TYPE_CHECKING
|
| 23 |
+
import warnings
|
| 24 |
+
from google.cloud import aiplatform
|
| 25 |
+
from google.cloud.aiplatform import initializer
|
| 26 |
+
from google.cloud.aiplatform import utils
|
| 27 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import constants
|
| 28 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import (
|
| 29 |
+
predict_utils,
|
| 30 |
+
)
|
| 31 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 32 |
+
_V2_4_WARNING_MESSAGE,
|
| 33 |
+
_V2_9_WARNING_MESSAGE,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
from ray.train import tensorflow as ray_tensorflow
|
| 39 |
+
|
| 40 |
+
if TYPE_CHECKING:
|
| 41 |
+
import tensorflow as tf
|
| 42 |
+
|
| 43 |
+
except ModuleNotFoundError as mnfe:
|
| 44 |
+
raise ModuleNotFoundError("Tensorflow isn't installed.") from mnfe
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def register_tensorflow(
|
| 48 |
+
checkpoint: ray_tensorflow.TensorflowCheckpoint,
|
| 49 |
+
artifact_uri: Optional[str] = None,
|
| 50 |
+
_model: Optional[Union["tf.keras.Model", Callable[[], "tf.keras.Model"]]] = None,
|
| 51 |
+
display_name: Optional[str] = None,
|
| 52 |
+
tensorflow_version: Optional[str] = None,
|
| 53 |
+
**kwargs,
|
| 54 |
+
) -> aiplatform.Model:
|
| 55 |
+
"""Uploads a Ray Tensorflow Checkpoint as Tensorflow Model to Model Registry.
|
| 56 |
+
|
| 57 |
+
Example usage:
|
| 58 |
+
from vertex_ray.predict import tensorflow
|
| 59 |
+
|
| 60 |
+
def create_model():
|
| 61 |
+
model = tf.keras.Sequential(...)
|
| 62 |
+
...
|
| 63 |
+
return model
|
| 64 |
+
|
| 65 |
+
result = trainer.fit()
|
| 66 |
+
my_model = tensorflow.register_tensorflow(
|
| 67 |
+
checkpoint=result.checkpoint,
|
| 68 |
+
_model=create_model,
|
| 69 |
+
artifact_uri="gs://{gcs-bucket-name}/path/to/store",
|
| 70 |
+
use_gpu=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
1. `use_gpu` will be passed to aiplatform.Model.upload_tensorflow_saved_model()
|
| 74 |
+
2. The `create_model` provides the model_definition which is required if
|
| 75 |
+
you create the TensorflowCheckpoint using `from_model` method.
|
| 76 |
+
More here, https://docs.ray.io/en/latest/train/api/doc/ray.train.tensorflow.TensorflowCheckpoint.get_model.html#ray.train.tensorflow.TensorflowCheckpoint.get_model
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
checkpoint: TensorflowCheckpoint instance.
|
| 80 |
+
artifact_uri (str):
|
| 81 |
+
Optional. The path to the directory where Model Artifacts will be saved. If
|
| 82 |
+
not set, will use staging bucket set in aiplatform.init().
|
| 83 |
+
_model: Tensorflow Model Definition. Refer
|
| 84 |
+
https://docs.ray.io/en/latest/train/api/doc/ray.train.tensorflow.TensorflowCheckpoint.get_model.html#ray.train.tensorflow.TensorflowCheckpoint.get_model
|
| 85 |
+
display_name (str):
|
| 86 |
+
Optional. The display name of the Model. The name can be up to 128
|
| 87 |
+
characters long and can be consist of any UTF-8 characters.
|
| 88 |
+
tensorflow_version (str):
|
| 89 |
+
Optional. The version of the Tensorflow serving container.
|
| 90 |
+
Supported versions:
|
| 91 |
+
https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers
|
| 92 |
+
If the version is not specified, the latest version is used.
|
| 93 |
+
**kwargs:
|
| 94 |
+
Any kwargs will be passed to aiplatform.Model registration.
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
model (aiplatform.Model):
|
| 98 |
+
Instantiated representation of the uploaded model resource.
|
| 99 |
+
|
| 100 |
+
Raises:
|
| 101 |
+
ValueError: Invalid Argument.
|
| 102 |
+
"""
|
| 103 |
+
if ray.__version__ == "2.9.3":
|
| 104 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 105 |
+
if tensorflow_version is None:
|
| 106 |
+
tensorflow_version = constants._TENSORFLOW_VERSION
|
| 107 |
+
artifact_uri = artifact_uri or initializer.global_config.staging_bucket
|
| 108 |
+
predict_utils.validate_artifact_uri(artifact_uri)
|
| 109 |
+
prefix = "ray-on-vertex-registered-tensorflow-model"
|
| 110 |
+
display_model_name = (
|
| 111 |
+
(f"{prefix}-{utils.timestamped_unique_name()}")
|
| 112 |
+
if display_name is None
|
| 113 |
+
else display_name
|
| 114 |
+
)
|
| 115 |
+
tf_model = _get_tensorflow_model_from(checkpoint, model=_model)
|
| 116 |
+
model_dir = os.path.join(artifact_uri, prefix)
|
| 117 |
+
try:
|
| 118 |
+
import tensorflow as tf
|
| 119 |
+
|
| 120 |
+
tf.saved_model.save(tf_model, model_dir)
|
| 121 |
+
except ImportError:
|
| 122 |
+
logging.warning("TensorFlow must be installed to save the trained model.")
|
| 123 |
+
return aiplatform.Model.upload_tensorflow_saved_model(
|
| 124 |
+
saved_model_dir=model_dir,
|
| 125 |
+
display_name=display_model_name,
|
| 126 |
+
tensorflow_version=tensorflow_version,
|
| 127 |
+
**kwargs,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def _get_tensorflow_model_from(
|
| 132 |
+
checkpoint: ray_tensorflow.TensorflowCheckpoint,
|
| 133 |
+
model: Optional[Union["tf.keras.Model", Callable[[], "tf.keras.Model"]]] = None,
|
| 134 |
+
) -> "tf.keras.Model":
|
| 135 |
+
"""Converts a TensorflowCheckpoint to Tensorflow Model.
|
| 136 |
+
|
| 137 |
+
Args:
|
| 138 |
+
checkpoint: TensorflowCheckpoint instance.
|
| 139 |
+
model: Tensorflow Model Defination.
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
A Tensorflow Native Framework Model.
|
| 143 |
+
|
| 144 |
+
Raises:
|
| 145 |
+
ValueError: Invalid Argument.
|
| 146 |
+
RuntimeError: Ray version 2.4.0 is not supported.
|
| 147 |
+
"""
|
| 148 |
+
ray_version = ray.__version__
|
| 149 |
+
if ray_version == "2.4.0":
|
| 150 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
import tensorflow as tf
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
return tf.saved_model.load(checkpoint.path)
|
| 157 |
+
except OSError:
|
| 158 |
+
return tf.saved_model.load("gs://" + checkpoint.path)
|
| 159 |
+
|
| 160 |
+
except ImportError:
|
| 161 |
+
logging.warning("TensorFlow must be installed to load the trained model.")
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/torch/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI Prediction Tensorflow."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
from .register import get_pytorch_model_from
|
| 21 |
+
|
| 22 |
+
__all__ = ("get_pytorch_model_from",)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/torch/register.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regsiter Torch for Ray on Vertex AI."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
import warnings
|
| 21 |
+
import ray
|
| 22 |
+
from ray.air._internal.torch_utils import load_torch_model
|
| 23 |
+
import tempfile
|
| 24 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 25 |
+
_V2_4_WARNING_MESSAGE,
|
| 26 |
+
_V2_9_WARNING_MESSAGE,
|
| 27 |
+
)
|
| 28 |
+
from google.cloud.aiplatform.utils import gcs_utils
|
| 29 |
+
from typing import Optional
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
from ray.train import torch as ray_torch
|
| 34 |
+
import torch
|
| 35 |
+
except ModuleNotFoundError as mnfe:
|
| 36 |
+
raise ModuleNotFoundError("Torch isn't installed.") from mnfe
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def get_pytorch_model_from(
|
| 40 |
+
checkpoint: ray_torch.TorchCheckpoint,
|
| 41 |
+
model: Optional[torch.nn.Module] = None,
|
| 42 |
+
) -> torch.nn.Module:
|
| 43 |
+
"""Converts a TorchCheckpoint to Pytorch Model.
|
| 44 |
+
|
| 45 |
+
Example:
|
| 46 |
+
from vertex_ray.predict import torch
|
| 47 |
+
result = TorchTrainer.fit(...)
|
| 48 |
+
|
| 49 |
+
pytorch_model = torch.get_pytorch_model_from(
|
| 50 |
+
checkpoint=result.checkpoint
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
checkpoint: TorchCheckpoint instance.
|
| 55 |
+
model: If the checkpoint contains a model state dict, and not the model
|
| 56 |
+
itself, then the state dict will be loaded to this `model`. Otherwise,
|
| 57 |
+
the model will be discarded.
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
A Pytorch Native Framework Model.
|
| 61 |
+
|
| 62 |
+
Raises:
|
| 63 |
+
ValueError: Invalid Argument.
|
| 64 |
+
ModuleNotFoundError: PyTorch isn't installed.
|
| 65 |
+
RuntimeError: Model not found.
|
| 66 |
+
RuntimeError: Ray version 2.4 is not supported.
|
| 67 |
+
RuntimeError: Only Ray version 2.9.3 is supported.
|
| 68 |
+
"""
|
| 69 |
+
ray_version = ray.__version__
|
| 70 |
+
if ray_version == "2.4.0":
|
| 71 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 72 |
+
if ray_version != "2.9.3":
|
| 73 |
+
raise RuntimeError(
|
| 74 |
+
f"Ray on Vertex does not support Ray version {ray_version} to"
|
| 75 |
+
" convert PyTorch model artifacts yet. Please use Ray 2.9.3."
|
| 76 |
+
)
|
| 77 |
+
if ray_version == "2.9.3":
|
| 78 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
return checkpoint.get_model()
|
| 82 |
+
except AttributeError:
|
| 83 |
+
model_file_name = ray.train.torch.TorchCheckpoint.MODEL_FILENAME
|
| 84 |
+
|
| 85 |
+
model_path = os.path.join(checkpoint.path, model_file_name)
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
import torch
|
| 89 |
+
|
| 90 |
+
except ModuleNotFoundError as mnfe:
|
| 91 |
+
raise ModuleNotFoundError("PyTorch isn't installed.") from mnfe
|
| 92 |
+
|
| 93 |
+
if os.path.exists(model_path):
|
| 94 |
+
model_or_state_dict = torch.load(
|
| 95 |
+
model_path, map_location="cpu", weights_only=True
|
| 96 |
+
)
|
| 97 |
+
else:
|
| 98 |
+
try:
|
| 99 |
+
# Download from GCS to temp and then load_model
|
| 100 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 101 |
+
gcs_utils.download_from_gcs("gs://" + checkpoint.path, temp_dir)
|
| 102 |
+
model_or_state_dict = torch.load(
|
| 103 |
+
f"{temp_dir}/{model_file_name}",
|
| 104 |
+
map_location="cpu",
|
| 105 |
+
weights_only=True,
|
| 106 |
+
)
|
| 107 |
+
except Exception as e:
|
| 108 |
+
raise RuntimeError(
|
| 109 |
+
f"{model_file_name} not found in this checkpoint due to: {e}."
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
model = load_torch_model(saved_model=model_or_state_dict, model_definition=model)
|
| 113 |
+
return model
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/util/constants.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# Copyright 2023 Google LLC
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
"""Constants."""
|
| 17 |
+
|
| 18 |
+
# Required Names for model files are specified here
|
| 19 |
+
# https://cloud.google.com/vertex-ai/docs/training/exporting-model-artifacts#framework-specific_requirements
|
| 20 |
+
_PICKLE_FILE_NAME = "model.pkl"
|
| 21 |
+
_PICKLE_EXTENTION = ".pkl"
|
| 22 |
+
|
| 23 |
+
_XGBOOST_VERSION = "1.6"
|
| 24 |
+
# TensorFlow 2.13 requires typing_extensions<4.6 and will cause errors in Ray.
|
| 25 |
+
# https://github.com/tensorflow/tensorflow/blob/v2.13.0/tensorflow/tools/pip_package/setup.py#L100
|
| 26 |
+
# 2.13 is the latest supported version of Vertex prebuilt prediction container.
|
| 27 |
+
# Set 2.12 as default here since 2.13 cause errors.
|
| 28 |
+
_TENSORFLOW_VERSION = "2.12"
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/util/predict_utils.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2023 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 |
+
#
|
| 17 |
+
|
| 18 |
+
"""Predict Utils."""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def validate_artifact_uri(artifact_uri: str) -> None:
|
| 22 |
+
if artifact_uri is None or not artifact_uri.startswith("gs://"):
|
| 23 |
+
raise ValueError("Argument 'artifact_uri' should start with 'gs://'.")
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/xgboost/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ray on Vertex AI Prediction Tensorflow."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
from .register import register_xgboost
|
| 21 |
+
|
| 22 |
+
__all__ = ("register_xgboost",)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/predict/xgboost/register.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Regsiter XGBoost for Ray on Vertex AI."""
|
| 2 |
+
|
| 3 |
+
# -*- coding: utf-8 -*-
|
| 4 |
+
|
| 5 |
+
# Copyright 2023 Google LLC
|
| 6 |
+
#
|
| 7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 |
+
# you may not use this file except in compliance with the License.
|
| 9 |
+
# You may obtain a copy of the License at
|
| 10 |
+
#
|
| 11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 12 |
+
#
|
| 13 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 |
+
# See the License for the specific language governing permissions and
|
| 17 |
+
# limitations under the License.
|
| 18 |
+
#
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
import pickle
|
| 22 |
+
import ray
|
| 23 |
+
import tempfile
|
| 24 |
+
from typing import Optional, TYPE_CHECKING
|
| 25 |
+
import warnings
|
| 26 |
+
from google.cloud import aiplatform
|
| 27 |
+
from google.cloud.aiplatform import initializer
|
| 28 |
+
from google.cloud.aiplatform import utils
|
| 29 |
+
from google.cloud.aiplatform.utils import gcs_utils
|
| 30 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import constants
|
| 31 |
+
from google.cloud.aiplatform.vertex_ray.predict.util import (
|
| 32 |
+
predict_utils,
|
| 33 |
+
)
|
| 34 |
+
from google.cloud.aiplatform.vertex_ray.util._validation_utils import (
|
| 35 |
+
_V2_4_WARNING_MESSAGE,
|
| 36 |
+
_V2_9_WARNING_MESSAGE,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
from ray.train import xgboost as ray_xgboost
|
| 42 |
+
|
| 43 |
+
if TYPE_CHECKING:
|
| 44 |
+
import xgboost
|
| 45 |
+
|
| 46 |
+
except ModuleNotFoundError as mnfe:
|
| 47 |
+
if ray.__version__ == "2.9.3":
|
| 48 |
+
raise ModuleNotFoundError("XGBoost isn't installed.") from mnfe
|
| 49 |
+
else:
|
| 50 |
+
xgboost = None
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def register_xgboost(
|
| 54 |
+
checkpoint: "ray_xgboost.XGBoostCheckpoint",
|
| 55 |
+
artifact_uri: Optional[str] = None,
|
| 56 |
+
display_name: Optional[str] = None,
|
| 57 |
+
xgboost_version: Optional[str] = None,
|
| 58 |
+
**kwargs,
|
| 59 |
+
) -> aiplatform.Model:
|
| 60 |
+
"""Uploads a Ray XGBoost Checkpoint as XGBoost Model to Model Registry.
|
| 61 |
+
|
| 62 |
+
Example usage:
|
| 63 |
+
from vertex_ray.predict import xgboost
|
| 64 |
+
from ray.train.xgboost import XGBoostCheckpoint
|
| 65 |
+
|
| 66 |
+
trainer = XGBoostTrainer(...)
|
| 67 |
+
result = trainer.fit()
|
| 68 |
+
xgboost_checkpoint = XGBoostCheckpoint.from_checkpoint(result.checkpoint)
|
| 69 |
+
|
| 70 |
+
my_model = xgboost.register_xgboost(
|
| 71 |
+
checkpoint=xgboost_checkpoint,
|
| 72 |
+
artifact_uri="gs://{gcs-bucket-name}/path/to/store",
|
| 73 |
+
display_name="my-ray-on-vertex-xgboost-model",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
checkpoint: XGBoostCheckpoint instance.
|
| 79 |
+
artifact_uri (str):
|
| 80 |
+
The path to the directory where Model Artifacts will be saved. If
|
| 81 |
+
not set, will use staging bucket set in aiplatform.init().
|
| 82 |
+
display_name (str):
|
| 83 |
+
Optional. The display name of the Model. The name can be up to 128
|
| 84 |
+
characters long and can be consist of any UTF-8 characters.
|
| 85 |
+
xgboost_version (str): Optional. The version of the XGBoost serving container.
|
| 86 |
+
Supported versions:
|
| 87 |
+
https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers
|
| 88 |
+
If the version is not specified, the version 1.6 is used.
|
| 89 |
+
**kwargs:
|
| 90 |
+
Any kwargs will be passed to aiplatform.Model registration.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
model (aiplatform.Model):
|
| 94 |
+
Instantiated representation of the uploaded model resource.
|
| 95 |
+
|
| 96 |
+
Raises:
|
| 97 |
+
ValueError: Invalid Argument.
|
| 98 |
+
RuntimeError: Only Ray version 2.9.3 is supported.
|
| 99 |
+
"""
|
| 100 |
+
ray_version = ray.__version__
|
| 101 |
+
if ray_version != "2.9.3":
|
| 102 |
+
raise RuntimeError(
|
| 103 |
+
f"Ray version {ray_version} is not supported to upload XGBoost"
|
| 104 |
+
" model to Vertex Model Registry yet. Please use Ray 2.9.3."
|
| 105 |
+
)
|
| 106 |
+
if ray_version == "2.9.3":
|
| 107 |
+
warnings.warn(_V2_9_WARNING_MESSAGE, DeprecationWarning, stacklevel=1)
|
| 108 |
+
artifact_uri = artifact_uri or initializer.global_config.staging_bucket
|
| 109 |
+
predict_utils.validate_artifact_uri(artifact_uri)
|
| 110 |
+
display_model_name = (
|
| 111 |
+
(f"ray-on-vertex-registered-xgboost-model-{utils.timestamped_unique_name()}")
|
| 112 |
+
if display_name is None
|
| 113 |
+
else display_name
|
| 114 |
+
)
|
| 115 |
+
model = _get_xgboost_model_from(checkpoint)
|
| 116 |
+
|
| 117 |
+
model_dir = os.path.join(artifact_uri, display_model_name)
|
| 118 |
+
file_path = os.path.join(model_dir, constants._PICKLE_FILE_NAME)
|
| 119 |
+
if xgboost_version is None:
|
| 120 |
+
xgboost_version = constants._XGBOOST_VERSION
|
| 121 |
+
|
| 122 |
+
with tempfile.NamedTemporaryFile(suffix=constants._PICKLE_EXTENTION) as temp_file:
|
| 123 |
+
pickle.dump(model, temp_file)
|
| 124 |
+
gcs_utils.upload_to_gcs(temp_file.name, file_path)
|
| 125 |
+
return aiplatform.Model.upload_xgboost_model_file(
|
| 126 |
+
model_file_path=temp_file.name,
|
| 127 |
+
display_name=display_model_name,
|
| 128 |
+
xgboost_version=xgboost_version,
|
| 129 |
+
**kwargs,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _get_xgboost_model_from(
|
| 134 |
+
checkpoint: "ray_xgboost.XGBoostCheckpoint",
|
| 135 |
+
) -> "xgboost.Booster":
|
| 136 |
+
"""Converts a XGBoostCheckpoint to XGBoost model.
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
checkpoint: XGBoostCheckpoint instance.
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
A XGBoost core Booster
|
| 143 |
+
|
| 144 |
+
Raises:
|
| 145 |
+
ValueError: Invalid Argument.
|
| 146 |
+
ModuleNotFoundError: XGBoost isn't installed.
|
| 147 |
+
RuntimeError: Model not found.
|
| 148 |
+
RuntimeError: Ray version 2.4 is not supported.
|
| 149 |
+
RuntimeError: Only Ray version 2.9.3 is supported.
|
| 150 |
+
"""
|
| 151 |
+
ray_version = ray.__version__
|
| 152 |
+
if ray_version == "2.4.0":
|
| 153 |
+
raise RuntimeError(_V2_4_WARNING_MESSAGE)
|
| 154 |
+
if ray_version != "2.9.3":
|
| 155 |
+
raise RuntimeError(
|
| 156 |
+
f"Ray version {ray_version} is not supported to convert a XGBoost"
|
| 157 |
+
" checkpoint to XGBoost model on Vertex yet. Please use Ray 2.9.3."
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
try:
|
| 161 |
+
# This works for Ray v2.5
|
| 162 |
+
return checkpoint.get_model()
|
| 163 |
+
except AttributeError:
|
| 164 |
+
# This works for Ray v2.9
|
| 165 |
+
model_file_name = ray.train.xgboost.XGBoostCheckpoint.MODEL_FILENAME
|
| 166 |
+
|
| 167 |
+
model_path = os.path.join(checkpoint.path, model_file_name)
|
| 168 |
+
|
| 169 |
+
try:
|
| 170 |
+
import xgboost
|
| 171 |
+
|
| 172 |
+
except ModuleNotFoundError as mnfe:
|
| 173 |
+
raise ModuleNotFoundError("XGBoost isn't installed.") from mnfe
|
| 174 |
+
|
| 175 |
+
booster = xgboost.Booster()
|
| 176 |
+
if os.path.exists(model_path):
|
| 177 |
+
booster.load_model(model_path)
|
| 178 |
+
return booster
|
| 179 |
+
|
| 180 |
+
try:
|
| 181 |
+
# Download from GCS to temp and then load_model
|
| 182 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 183 |
+
gcs_utils.download_from_gcs("gs://" + checkpoint.path, temp_dir)
|
| 184 |
+
booster.load_model(f"{temp_dir}/{model_file_name}")
|
| 185 |
+
return booster
|
| 186 |
+
except Exception as e:
|
| 187 |
+
raise RuntimeError(
|
| 188 |
+
f"{model_file_name} not found in this checkpoint due to: {e}."
|
| 189 |
+
)
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/render.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2023 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 |
+
#
|
| 17 |
+
|
| 18 |
+
import pathlib
|
| 19 |
+
from ray.widgets import Template
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class VertexRayTemplate(Template):
|
| 23 |
+
"""Class which provides basic HTML templating."""
|
| 24 |
+
|
| 25 |
+
def __init__(self, file: str):
|
| 26 |
+
with open(pathlib.Path(__file__).parent / "templates" / file, "r") as f:
|
| 27 |
+
self.template = f.read()
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/templates/context_shellurirow.html.j2
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<tr>
|
| 2 |
+
<td style="text-align: left"><b>Interactive Terminal Uri:</b></td>
|
| 3 |
+
<td style="text-align: left"><b><a href="https://{{ shell_uri }}" target="_blank">{{ shell_uri }}</a></b></td>
|
| 4 |
+
</tr>
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/templates/context_table.html.j2
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
<table class="jp-RenderedHTMLCommon" style="border-collapse: collapse;color: var(--jp-ui-font-color1);font-size: var(--jp-ui-font-size1);">
|
| 3 |
+
<tr>
|
| 4 |
+
<td style="text-align: left"><b>Python version:</b></td>
|
| 5 |
+
<td style="text-align: left"><b>{{ python_version }}</b></td>
|
| 6 |
+
</tr>
|
| 7 |
+
<tr>
|
| 8 |
+
<td style="text-align: left"><b>Ray version:</b></td>
|
| 9 |
+
<td style="text-align: left"><b> {{ ray_version }}</b></td>
|
| 10 |
+
</tr>
|
| 11 |
+
<tr>
|
| 12 |
+
<td style="text-align: left"><b>Vertex SDK version:</b></td>
|
| 13 |
+
<td style="text-align: left"><b> {{ vertex_sdk_version }}</b></td>
|
| 14 |
+
</tr>
|
| 15 |
+
<tr>
|
| 16 |
+
<td style="text-align: left"><b>Dashboard:</b></td>
|
| 17 |
+
<td style="text-align: left"><b><a href="https://{{ dashboard_url }}" target="_blank">{{ dashboard_url }}</a></b></td>
|
| 18 |
+
</tr>
|
| 19 |
+
{{ shell_uri_row }}
|
| 20 |
+
<tr>
|
| 21 |
+
<td style="text-align: left"><b>Cluster Name:</b></td>
|
| 22 |
+
<td style="text-align: left"><b> {{ persistent_resource_id }}</b></td>
|
| 23 |
+
</tr>
|
| 24 |
+
</table>
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/_gapic_utils.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2023 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 |
+
#
|
| 17 |
+
|
| 18 |
+
import datetime
|
| 19 |
+
import logging
|
| 20 |
+
import time
|
| 21 |
+
from typing import Optional
|
| 22 |
+
|
| 23 |
+
from google.api_core import exceptions
|
| 24 |
+
from google.cloud.aiplatform import initializer
|
| 25 |
+
from google.cloud.aiplatform.utils import (
|
| 26 |
+
PersistentResourceClientWithOverride,
|
| 27 |
+
)
|
| 28 |
+
from google.cloud.aiplatform.vertex_ray.util import _validation_utils
|
| 29 |
+
from google.cloud.aiplatform.vertex_ray.util.resources import (
|
| 30 |
+
AutoscalingSpec,
|
| 31 |
+
Cluster,
|
| 32 |
+
PscIConfig,
|
| 33 |
+
Resources,
|
| 34 |
+
)
|
| 35 |
+
from google.cloud.aiplatform_v1beta1.types.persistent_resource import (
|
| 36 |
+
PersistentResource,
|
| 37 |
+
)
|
| 38 |
+
from google.cloud.aiplatform_v1beta1.types.persistent_resource_service import (
|
| 39 |
+
GetPersistentResourceRequest,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
_PRIVATE_PREVIEW_IMAGE = "-docker.pkg.dev/vertex-ai/training/tf-"
|
| 44 |
+
_OFFICIAL_IMAGE = "-docker.pkg.dev/vertex-ai/training/ray-"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def create_persistent_resource_client():
|
| 48 |
+
# location is inhereted from the global configuration at aiplatform.init().
|
| 49 |
+
return initializer.global_config.create_client(
|
| 50 |
+
client_class=PersistentResourceClientWithOverride,
|
| 51 |
+
appended_gapic_version="vertex_ray",
|
| 52 |
+
).select_version("v1beta1")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def polling_delay(num_attempts: int, time_scale: float) -> datetime.timedelta:
|
| 56 |
+
"""Computes a delay to the next attempt to poll the Vertex service.
|
| 57 |
+
|
| 58 |
+
This does bounded exponential backoff, starting with $time_scale.
|
| 59 |
+
If $time_scale == 0, it starts with a small time interval, less than
|
| 60 |
+
1 second.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
num_attempts: The number of times have we polled and found that the
|
| 64 |
+
desired result was not yet available.
|
| 65 |
+
time_scale: The shortest polling interval, in seconds, or zero. Zero is
|
| 66 |
+
treated as a small interval, less than 1 second.
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
A recommended delay interval, in seconds.
|
| 70 |
+
"""
|
| 71 |
+
# The polling schedule is slow initially , and then gets faster until 6
|
| 72 |
+
# attempts (after that the sleeping time remains the same).
|
| 73 |
+
small_interval = 30.0 # Seconds
|
| 74 |
+
interval = max(time_scale, small_interval) * 0.765 ** min(num_attempts, 6)
|
| 75 |
+
return datetime.timedelta(seconds=interval)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def get_persistent_resource(
|
| 79 |
+
persistent_resource_name: str, tolerance: Optional[int] = 0
|
| 80 |
+
):
|
| 81 |
+
"""Get persistent resource.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
persistent_resource_name:
|
| 85 |
+
"projects/<project_num>/locations/<region>/persistentResources/<pr_id>".
|
| 86 |
+
tolerance: number of attemps to get persistent resource.
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
aiplatform_v1.PersistentResource if state is RUNNING.
|
| 90 |
+
|
| 91 |
+
Raises:
|
| 92 |
+
ValueError: Invalid cluster resource name.
|
| 93 |
+
RuntimeError: Service returns error.
|
| 94 |
+
RuntimeError: Cluster resource state is STOPPING.
|
| 95 |
+
RuntimeError: Cluster resource state is ERROR.
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
client = create_persistent_resource_client()
|
| 99 |
+
request = GetPersistentResourceRequest(name=persistent_resource_name)
|
| 100 |
+
|
| 101 |
+
# TODO(b/277117901): Add test cases for polling and error handling
|
| 102 |
+
num_attempts = 0
|
| 103 |
+
while True:
|
| 104 |
+
try:
|
| 105 |
+
response = client.get_persistent_resource(request)
|
| 106 |
+
except exceptions.NotFound:
|
| 107 |
+
response = None
|
| 108 |
+
if num_attempts >= tolerance:
|
| 109 |
+
raise ValueError(
|
| 110 |
+
"[Ray on Vertex AI]: Invalid cluster_resource_name %s (404 not"
|
| 111 |
+
" found)." % persistent_resource_name
|
| 112 |
+
)
|
| 113 |
+
if response:
|
| 114 |
+
if response.error.message:
|
| 115 |
+
logging.error(
|
| 116 |
+
"[Ray on Vertex AI]: %s - %s",
|
| 117 |
+
persistent_resource_name,
|
| 118 |
+
response.error.message,
|
| 119 |
+
)
|
| 120 |
+
raise RuntimeError(
|
| 121 |
+
"[Ray on Vertex AI]: Cluster %s returned an error."
|
| 122 |
+
% persistent_resource_name
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
print("[Ray on Vertex AI]: Cluster State =", response.state)
|
| 126 |
+
if response.state == PersistentResource.State.RUNNING:
|
| 127 |
+
return response
|
| 128 |
+
elif response.state == PersistentResource.State.STOPPING:
|
| 129 |
+
raise RuntimeError(
|
| 130 |
+
"[Ray on Vertex AI]: Cluster %s is stopping."
|
| 131 |
+
% persistent_resource_name
|
| 132 |
+
)
|
| 133 |
+
elif response.state == PersistentResource.State.ERROR:
|
| 134 |
+
raise RuntimeError(
|
| 135 |
+
"[Ray on Vertex AI]: Cluster %s encountered an error."
|
| 136 |
+
% persistent_resource_name
|
| 137 |
+
)
|
| 138 |
+
# Polling decay
|
| 139 |
+
sleep_time = polling_delay(num_attempts=num_attempts, time_scale=150.0)
|
| 140 |
+
num_attempts += 1
|
| 141 |
+
print(
|
| 142 |
+
"Waiting for cluster provisioning; attempt {}; sleeping for {} seconds".format(
|
| 143 |
+
num_attempts, sleep_time
|
| 144 |
+
)
|
| 145 |
+
)
|
| 146 |
+
time.sleep(sleep_time.total_seconds())
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def persistent_resource_to_cluster(
|
| 150 |
+
persistent_resource: PersistentResource,
|
| 151 |
+
) -> Optional[Cluster]:
|
| 152 |
+
"""Format a PersistentResource to a dictionary.
|
| 153 |
+
|
| 154 |
+
Args:
|
| 155 |
+
persistent_resource: PersistentResource.
|
| 156 |
+
Returns:
|
| 157 |
+
Cluster.
|
| 158 |
+
"""
|
| 159 |
+
dashboard_address = persistent_resource.resource_runtime.access_uris.get(
|
| 160 |
+
"RAY_DASHBOARD_URI"
|
| 161 |
+
)
|
| 162 |
+
cluster = Cluster(
|
| 163 |
+
cluster_resource_name=persistent_resource.name,
|
| 164 |
+
network=persistent_resource.network,
|
| 165 |
+
reserved_ip_ranges=persistent_resource.reserved_ip_ranges,
|
| 166 |
+
state=persistent_resource.state.name,
|
| 167 |
+
labels=persistent_resource.labels,
|
| 168 |
+
dashboard_address=dashboard_address,
|
| 169 |
+
)
|
| 170 |
+
if not persistent_resource.resource_runtime_spec.ray_spec:
|
| 171 |
+
# skip PersistentResource without RaySpec
|
| 172 |
+
logging.info(
|
| 173 |
+
"[Ray on Vertex AI]: Cluster %s does not have Ray installed."
|
| 174 |
+
% persistent_resource.name,
|
| 175 |
+
)
|
| 176 |
+
return
|
| 177 |
+
if persistent_resource.psc_interface_config:
|
| 178 |
+
cluster.psc_interface_config = PscIConfig(
|
| 179 |
+
network_attachment=persistent_resource.psc_interface_config.network_attachment
|
| 180 |
+
)
|
| 181 |
+
resource_pools = persistent_resource.resource_pools
|
| 182 |
+
|
| 183 |
+
head_resource_pool = resource_pools[0]
|
| 184 |
+
head_id = head_resource_pool.id
|
| 185 |
+
head_image_uri = (
|
| 186 |
+
persistent_resource.resource_runtime_spec.ray_spec.resource_pool_images[head_id]
|
| 187 |
+
)
|
| 188 |
+
if persistent_resource.resource_runtime_spec.service_account_spec.service_account:
|
| 189 |
+
cluster.service_account = (
|
| 190 |
+
persistent_resource.resource_runtime_spec.service_account_spec.service_account
|
| 191 |
+
)
|
| 192 |
+
if not head_image_uri:
|
| 193 |
+
head_image_uri = persistent_resource.resource_runtime_spec.ray_spec.image_uri
|
| 194 |
+
|
| 195 |
+
try:
|
| 196 |
+
python_version, ray_version = _validation_utils.get_versions_from_image_uri(
|
| 197 |
+
head_image_uri
|
| 198 |
+
)
|
| 199 |
+
except IndexError:
|
| 200 |
+
if _PRIVATE_PREVIEW_IMAGE in head_image_uri:
|
| 201 |
+
# If using outdated images
|
| 202 |
+
logging.info(
|
| 203 |
+
"[Ray on Vertex AI]: The image of cluster %s is outdated."
|
| 204 |
+
" It is recommended to delete and recreate the cluster to obtain"
|
| 205 |
+
" the latest image." % persistent_resource.name
|
| 206 |
+
)
|
| 207 |
+
return None
|
| 208 |
+
else:
|
| 209 |
+
# Custom image might also cause IndexError
|
| 210 |
+
python_version = None
|
| 211 |
+
ray_version = None
|
| 212 |
+
cluster.python_version = python_version
|
| 213 |
+
cluster.ray_version = ray_version
|
| 214 |
+
cluster.ray_metric_enabled = not (
|
| 215 |
+
persistent_resource.resource_runtime_spec.ray_spec.ray_metric_spec.disabled
|
| 216 |
+
)
|
| 217 |
+
cluster.ray_logs_enabled = not (
|
| 218 |
+
persistent_resource.resource_runtime_spec.ray_spec.ray_logs_spec.disabled
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
accelerator_type = head_resource_pool.machine_spec.accelerator_type
|
| 222 |
+
if accelerator_type.value != 0:
|
| 223 |
+
accelerator_type = accelerator_type.name
|
| 224 |
+
else:
|
| 225 |
+
accelerator_type = None
|
| 226 |
+
if _OFFICIAL_IMAGE in head_image_uri:
|
| 227 |
+
# Official training image is not custom
|
| 228 |
+
head_image_uri = None
|
| 229 |
+
head_node_type = Resources(
|
| 230 |
+
machine_type=head_resource_pool.machine_spec.machine_type,
|
| 231 |
+
accelerator_type=accelerator_type,
|
| 232 |
+
accelerator_count=head_resource_pool.machine_spec.accelerator_count,
|
| 233 |
+
boot_disk_type=head_resource_pool.disk_spec.boot_disk_type,
|
| 234 |
+
boot_disk_size_gb=head_resource_pool.disk_spec.boot_disk_size_gb,
|
| 235 |
+
node_count=1,
|
| 236 |
+
custom_image=head_image_uri,
|
| 237 |
+
)
|
| 238 |
+
worker_node_types = []
|
| 239 |
+
if head_resource_pool.replica_count > 1:
|
| 240 |
+
# head_node_type.node_count must be 1. If the head_resource_pool (the first
|
| 241 |
+
# resource pool) has replica_count > 1, the rest replica are worker nodes.
|
| 242 |
+
worker_node_count = head_resource_pool.replica_count - 1
|
| 243 |
+
worker_node_types.append(
|
| 244 |
+
Resources(
|
| 245 |
+
machine_type=head_resource_pool.machine_spec.machine_type,
|
| 246 |
+
accelerator_type=accelerator_type,
|
| 247 |
+
accelerator_count=head_resource_pool.machine_spec.accelerator_count,
|
| 248 |
+
boot_disk_type=head_resource_pool.disk_spec.boot_disk_type,
|
| 249 |
+
boot_disk_size_gb=head_resource_pool.disk_spec.boot_disk_size_gb,
|
| 250 |
+
node_count=worker_node_count,
|
| 251 |
+
custom_image=head_image_uri,
|
| 252 |
+
)
|
| 253 |
+
)
|
| 254 |
+
if head_resource_pool.autoscaling_spec:
|
| 255 |
+
worker_node_types[0].autoscaling_spec = AutoscalingSpec(
|
| 256 |
+
min_replica_count=head_resource_pool.autoscaling_spec.min_replica_count,
|
| 257 |
+
max_replica_count=head_resource_pool.autoscaling_spec.max_replica_count,
|
| 258 |
+
)
|
| 259 |
+
for i in range(len(resource_pools) - 1):
|
| 260 |
+
# Convert the second and more resource pools to vertex_ray.Resources,
|
| 261 |
+
# and append then to worker_node_types.
|
| 262 |
+
accelerator_type = resource_pools[i + 1].machine_spec.accelerator_type
|
| 263 |
+
if accelerator_type.value != 0:
|
| 264 |
+
accelerator_type = accelerator_type.name
|
| 265 |
+
else:
|
| 266 |
+
accelerator_type = None
|
| 267 |
+
worker_image_uri = (
|
| 268 |
+
persistent_resource.resource_runtime_spec.ray_spec.resource_pool_images[
|
| 269 |
+
resource_pools[i + 1].id
|
| 270 |
+
]
|
| 271 |
+
)
|
| 272 |
+
if _OFFICIAL_IMAGE in worker_image_uri:
|
| 273 |
+
# Official training image is not custom
|
| 274 |
+
worker_image_uri = None
|
| 275 |
+
|
| 276 |
+
resource = Resources(
|
| 277 |
+
machine_type=resource_pools[i + 1].machine_spec.machine_type,
|
| 278 |
+
accelerator_type=accelerator_type,
|
| 279 |
+
accelerator_count=resource_pools[i + 1].machine_spec.accelerator_count,
|
| 280 |
+
boot_disk_type=resource_pools[i + 1].disk_spec.boot_disk_type,
|
| 281 |
+
boot_disk_size_gb=resource_pools[i + 1].disk_spec.boot_disk_size_gb,
|
| 282 |
+
node_count=resource_pools[i + 1].replica_count,
|
| 283 |
+
custom_image=worker_image_uri,
|
| 284 |
+
)
|
| 285 |
+
if resource_pools[i + 1].autoscaling_spec:
|
| 286 |
+
resource.autoscaling_spec = AutoscalingSpec(
|
| 287 |
+
min_replica_count=resource_pools[
|
| 288 |
+
i + 1
|
| 289 |
+
].autoscaling_spec.min_replica_count,
|
| 290 |
+
max_replica_count=resource_pools[
|
| 291 |
+
i + 1
|
| 292 |
+
].autoscaling_spec.max_replica_count,
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
worker_node_types.append(resource)
|
| 296 |
+
|
| 297 |
+
cluster.head_node_type = head_node_type
|
| 298 |
+
cluster.worker_node_types = worker_node_types
|
| 299 |
+
|
| 300 |
+
return cluster
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/_validation_utils.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
|
| 3 |
+
# Copyright 2025 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 |
+
#
|
| 17 |
+
|
| 18 |
+
import google.auth
|
| 19 |
+
import google.auth.transport.requests
|
| 20 |
+
import logging
|
| 21 |
+
import ray
|
| 22 |
+
import re
|
| 23 |
+
from immutabledict import immutabledict
|
| 24 |
+
|
| 25 |
+
from google.cloud.aiplatform import initializer
|
| 26 |
+
from google.cloud.aiplatform.utils import resource_manager_utils
|
| 27 |
+
|
| 28 |
+
SUPPORTED_RAY_VERSIONS = immutabledict(
|
| 29 |
+
{"2.9": "2.9.3", "2.33": "2.33.0", "2.42": "2.42.0", "2.47": "2.47.1"}
|
| 30 |
+
)
|
| 31 |
+
SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS = immutabledict(
|
| 32 |
+
{
|
| 33 |
+
"3.10": ("2.9", "2.33", "2.42"),
|
| 34 |
+
"3.11": ("2.42", "2.47"),
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
_V2_4_WARNING_MESSAGE = (
|
| 38 |
+
"After google-cloud-aiplatform>1.53.0, using Ray version = 2.4 will result in "
|
| 39 |
+
"an error. Please use Ray version = 2.33.0, 2.42.0 or 2.47.1 (default) instead."
|
| 40 |
+
)
|
| 41 |
+
_V2_9_WARNING_MESSAGE = (
|
| 42 |
+
"In March 2025, using Ray version = 2.9 will result in an error. "
|
| 43 |
+
"Please use Ray version = 2.33.0, 2.42.0 or 2.47.1 (default) instead."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# Artifact Repository available regions.
|
| 48 |
+
_AVAILABLE_REGIONS = ["us", "europe", "asia"]
|
| 49 |
+
# If region is not available, assume using the default region.
|
| 50 |
+
_DEFAULT_REGION = "us"
|
| 51 |
+
|
| 52 |
+
_PERSISTENT_RESOURCE_NAME_PATTERN = "projects/{}/locations/{}/persistentResources/{}"
|
| 53 |
+
_VALID_RESOURCE_NAME_REGEX = "[a-z][a-zA-Z0-9._-]{0,127}"
|
| 54 |
+
_DASHBOARD_URI_SUFFIX = "aiplatform-training.googleusercontent.com"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def valid_resource_name(resource_name):
|
| 58 |
+
"""Check if address is a valid resource name."""
|
| 59 |
+
resource_name_split = resource_name.split("/")
|
| 60 |
+
if not (
|
| 61 |
+
len(resource_name_split) == 6
|
| 62 |
+
and resource_name_split[0] == "projects"
|
| 63 |
+
and resource_name_split[2] == "locations"
|
| 64 |
+
and resource_name_split[4] == "persistentResources"
|
| 65 |
+
):
|
| 66 |
+
raise ValueError(
|
| 67 |
+
"[Ray on Vertex AI]: Address must be in the following "
|
| 68 |
+
"format: vertex_ray://projects/<project_num>/locations/<region>/persistentResources/<pr_id> "
|
| 69 |
+
"or vertex_ray://<pr_id>."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def maybe_reconstruct_resource_name(address) -> str:
|
| 74 |
+
"""Reconstruct full persistent resource name if only id was given."""
|
| 75 |
+
if re.match("^{}$".format(_VALID_RESOURCE_NAME_REGEX), address):
|
| 76 |
+
# Assume only cluster name (persistent resource id) was given.
|
| 77 |
+
logging.info(
|
| 78 |
+
"[Ray on Vertex AI]: Cluster name was given as address, reconstructing full resource name"
|
| 79 |
+
)
|
| 80 |
+
return _PERSISTENT_RESOURCE_NAME_PATTERN.format(
|
| 81 |
+
resource_manager_utils.get_project_number(
|
| 82 |
+
initializer.global_config.project
|
| 83 |
+
),
|
| 84 |
+
initializer.global_config.location,
|
| 85 |
+
address,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return address
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def get_local_ray_version():
|
| 92 |
+
ray_version = ray.__version__.split(".")
|
| 93 |
+
if len(ray_version) == 3:
|
| 94 |
+
ray_version = ray_version[:2]
|
| 95 |
+
return ".".join(ray_version)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def get_image_uri(ray_version, python_version, enable_cuda):
|
| 99 |
+
"""Image uri for a given ray version and python version."""
|
| 100 |
+
if ray_version not in SUPPORTED_RAY_VERSIONS:
|
| 101 |
+
raise ValueError(
|
| 102 |
+
"[Ray on Vertex AI]: The supported Ray versions are %s (%s) and %s (%s)."
|
| 103 |
+
% (
|
| 104 |
+
list(SUPPORTED_RAY_VERSIONS.keys())[0],
|
| 105 |
+
list(SUPPORTED_RAY_VERSIONS.values())[0],
|
| 106 |
+
list(SUPPORTED_RAY_VERSIONS.keys())[1],
|
| 107 |
+
list(SUPPORTED_RAY_VERSIONS.values())[1],
|
| 108 |
+
)
|
| 109 |
+
)
|
| 110 |
+
if python_version is None:
|
| 111 |
+
for pv, ray_versions in SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS.items():
|
| 112 |
+
if ray_version in ray_versions:
|
| 113 |
+
python_version = pv
|
| 114 |
+
break
|
| 115 |
+
if python_version not in SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS:
|
| 116 |
+
raise ValueError(
|
| 117 |
+
"[Ray on Vertex AI]: The supported Python versions are 3.10 or 3.11."
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
if ray_version not in SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS[python_version]:
|
| 121 |
+
raise ValueError(
|
| 122 |
+
"[Ray on Vertex AI]: The supported Ray version(s) for Python version %s: %s."
|
| 123 |
+
% (
|
| 124 |
+
python_version,
|
| 125 |
+
SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS[python_version],
|
| 126 |
+
)
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
location = initializer.global_config.location
|
| 130 |
+
region = location.split("-")[0]
|
| 131 |
+
if region not in _AVAILABLE_REGIONS:
|
| 132 |
+
region = _DEFAULT_REGION
|
| 133 |
+
ray_version = ray_version.replace(".", "-")
|
| 134 |
+
python_version = python_version.replace(".", "")
|
| 135 |
+
if enable_cuda:
|
| 136 |
+
return f"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.{ray_version}.py{python_version}:latest"
|
| 137 |
+
else:
|
| 138 |
+
return f"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.{ray_version}.py{python_version}:latest"
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def get_versions_from_image_uri(image_uri):
|
| 142 |
+
"""Get ray version and python version from image uri."""
|
| 143 |
+
logging.info(f"[Ray on Vertex AI]: Getting versions from image uri: {image_uri}")
|
| 144 |
+
image_label = image_uri.split("/")[-1].split(":")[0]
|
| 145 |
+
py_version = image_label[-3] + "." + image_label[-2:]
|
| 146 |
+
ray_version = image_label.split(".")[1].replace("-", ".")
|
| 147 |
+
if (
|
| 148 |
+
py_version in SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS
|
| 149 |
+
and ray_version in SUPPORTED_RAY_VERSIONS_FROM_PYTHON_VERSIONS[py_version]
|
| 150 |
+
):
|
| 151 |
+
return py_version, ray_version
|
| 152 |
+
else:
|
| 153 |
+
# May not parse custom image and get the versions correctly
|
| 154 |
+
return None, None
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def valid_dashboard_address(address):
|
| 158 |
+
"""Check if address is a valid dashboard uri."""
|
| 159 |
+
return address.endswith(_DASHBOARD_URI_SUFFIX)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def get_bearer_token():
|
| 163 |
+
"""Get bearer token through Application Default Credentials."""
|
| 164 |
+
creds, _ = google.auth.default(
|
| 165 |
+
scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# creds.valid is False, and creds.token is None
|
| 169 |
+
# Need to refresh credentials to populate those
|
| 170 |
+
auth_req = google.auth.transport.requests.Request()
|
| 171 |
+
creds.refresh(auth_req)
|
| 172 |
+
return creds.token
|
loan-advisor-agent/.venv/Lib/site-packages/vertex_ray/util/resources.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 (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 |
+
#
|
| 17 |
+
import dataclasses
|
| 18 |
+
from typing import Dict, List, Optional
|
| 19 |
+
from google.cloud.aiplatform_v1beta1.types import PersistentResource
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@dataclasses.dataclass
|
| 23 |
+
class AutoscalingSpec:
|
| 24 |
+
"""Autoscaling spec for a ray cluster node.
|
| 25 |
+
|
| 26 |
+
Attributes:
|
| 27 |
+
min_replica_count: The minimum number of replicas in the cluster.
|
| 28 |
+
max_replica_count: The maximum number of replicas in the cluster.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
min_replica_count: int = 1
|
| 32 |
+
max_replica_count: int = 2
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dataclasses.dataclass
|
| 36 |
+
class Resources:
|
| 37 |
+
"""Resources for a ray cluster node.
|
| 38 |
+
|
| 39 |
+
Attributes:
|
| 40 |
+
machine_type: See the list of machine types:
|
| 41 |
+
https://cloud.google.com/vertex-ai/docs/training/configure-compute#machine-types
|
| 42 |
+
node_count: This argument represents how many nodes to start for the
|
| 43 |
+
ray cluster.
|
| 44 |
+
accelerator_type: e.g. "NVIDIA_TESLA_P4".
|
| 45 |
+
Vertex AI supports the following types of GPU:
|
| 46 |
+
https://cloud.google.com/vertex-ai/docs/training/configure-compute#specifying_gpus
|
| 47 |
+
accelerator_count: The number of accelerators to attach to the machine.
|
| 48 |
+
boot_disk_type: Type of the boot disk (default is "pd-ssd").
|
| 49 |
+
Valid values: "pd-ssd" (Persistent Disk Solid State Drive) or
|
| 50 |
+
"pd-standard" (Persistent Disk Hard Disk Drive).
|
| 51 |
+
boot_disk_size_gb: Size in GB of the boot disk (default is 100GB). Must
|
| 52 |
+
be either unspecified or within the range of [100, 64000].
|
| 53 |
+
custom_image: Custom image for this resource (e.g.
|
| 54 |
+
us-docker.pkg.dev/my-project/ray-gpu.2-9.py310-tf:latest).
|
| 55 |
+
autoscaling_spec: Autoscaling spec for this resource.
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
machine_type: Optional[str] = "n1-standard-16"
|
| 59 |
+
node_count: Optional[int] = 1
|
| 60 |
+
accelerator_type: Optional[str] = None
|
| 61 |
+
accelerator_count: Optional[int] = 0
|
| 62 |
+
boot_disk_type: Optional[str] = "pd-ssd"
|
| 63 |
+
boot_disk_size_gb: Optional[int] = 100
|
| 64 |
+
custom_image: Optional[str] = None
|
| 65 |
+
autoscaling_spec: Optional[AutoscalingSpec] = None
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@dataclasses.dataclass
|
| 69 |
+
class NodeImages:
|
| 70 |
+
"""Custom images for a ray cluster.
|
| 71 |
+
|
| 72 |
+
We currently support Ray v2.9, v2.33, v2.42 and python v3.10.
|
| 73 |
+
We also support python v3.11 for Ray v2.42 and v2.47.
|
| 74 |
+
The custom images must be extended from the following base images:
|
| 75 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.2-9.py310:latest",
|
| 76 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.2-9.py310:latest",
|
| 77 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.2-33.py310:latest",
|
| 78 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.2-33.py310:latest",
|
| 79 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.2-42.py310:latest",
|
| 80 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.2-42.py310:latest",
|
| 81 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.2-42.py311:latest",
|
| 82 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.2-42.py311:latest".
|
| 83 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-cpu.2-47.py311:latest", or
|
| 84 |
+
"{region}-docker.pkg.dev/vertex-ai/training/ray-gpu.2-47.py311:latest". In
|
| 85 |
+
order to use custom images, need to specify both head and worker images.
|
| 86 |
+
|
| 87 |
+
Attributes:
|
| 88 |
+
head: image for head node (eg. us-docker.pkg.dev/my-project/ray-cpu.2-47.py311:latest).
|
| 89 |
+
worker: image for all worker nodes (eg. us-docker.pkg.dev/my-project/ray-gpu.2-47.py311:latest).
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
head: str = None
|
| 93 |
+
worker: str = None
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@dataclasses.dataclass
|
| 97 |
+
class PscIConfig:
|
| 98 |
+
"""PSC-I config.
|
| 99 |
+
|
| 100 |
+
Attributes:
|
| 101 |
+
network_attachment: Optional. The name or full name of the Compute Engine
|
| 102 |
+
`network attachment <https://cloud.google.com/vpc/docs/about-network-attachments>`
|
| 103 |
+
to attach to the resource. It has a format:
|
| 104 |
+
``projects/{project}/regions/{region}/networkAttachments/{networkAttachment}``.
|
| 105 |
+
Where {project} is a project number, as in ``12345``, and
|
| 106 |
+
{networkAttachment} is a network attachment name. To specify
|
| 107 |
+
this field, you must have already [created a network
|
| 108 |
+
attachment]
|
| 109 |
+
(https://cloud.google.com/vpc/docs/create-manage-network-attachments#create-network-attachments).
|
| 110 |
+
This field is only used for resources using PSC-I. Make sure you do not
|
| 111 |
+
specify the network here for VPC peering.
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
network_attachment: str = None
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@dataclasses.dataclass
|
| 118 |
+
class NfsMount:
|
| 119 |
+
"""NFS mount.
|
| 120 |
+
|
| 121 |
+
Attributes:
|
| 122 |
+
server: Required. IP address of the NFS server.
|
| 123 |
+
path: Required. Source path exported from NFS server. Has to start
|
| 124 |
+
with '/', and combined with the ip address, it indicates the
|
| 125 |
+
source mount path in the form of ``server:path``.
|
| 126 |
+
mount_point: Required. Destination mount path. The NFS will be mounted
|
| 127 |
+
for the user under /mnt/nfs/<mount_point>.
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
server: str = None
|
| 131 |
+
path: str = None
|
| 132 |
+
mount_point: str = None
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
@dataclasses.dataclass
|
| 136 |
+
class Cluster:
|
| 137 |
+
"""Ray cluster (output only).
|
| 138 |
+
|
| 139 |
+
Attributes:
|
| 140 |
+
cluster_resource_name: It has a format:
|
| 141 |
+
"projects/<project_num>/locations/<region>/persistentResources/<pr_id>".
|
| 142 |
+
network: Virtual private cloud (VPC) network. It has a format:
|
| 143 |
+
"projects/<project_num>/global/networks/<network_name>".
|
| 144 |
+
For Ray Client, VPC peering is required to connect to the cluster
|
| 145 |
+
managed in the Vertex API service. For Ray Job API, VPC network is
|
| 146 |
+
not required because cluster connection can be accessed through
|
| 147 |
+
dashboard address.
|
| 148 |
+
reserved_ip_ranges: A list of names for the reserved IP ranges under
|
| 149 |
+
the VPC network that can be used for this cluster. If set, we will
|
| 150 |
+
deploy the cluster within the provided IP ranges. Otherwise, the
|
| 151 |
+
cluster is deployed to any IP ranges under the provided VPC network.
|
| 152 |
+
Example: ["vertex-ai-ip-range"].
|
| 153 |
+
service_account: Service account to be used for running Ray programs on
|
| 154 |
+
the cluster.
|
| 155 |
+
state: Describes the cluster state (defined in PersistentResource.State).
|
| 156 |
+
python_version: Python version for the ray cluster (e.g. "3.11").
|
| 157 |
+
ray_version: Ray version for the ray cluster (e.g. "2.47").
|
| 158 |
+
head_node_type: The head node resource. Resources.node_count must be 1.
|
| 159 |
+
If not set, by default it is a CPU node with machine_type of n1-standard-8.
|
| 160 |
+
worker_node_types: The list of Resources of the worker nodes. Should not
|
| 161 |
+
duplicate the elements in the list.
|
| 162 |
+
dashboard_address: For Ray Job API (JobSubmissionClient), with this
|
| 163 |
+
cluster connection doesn't require VPC peering.
|
| 164 |
+
labels:
|
| 165 |
+
The labels with user-defined metadata to organize Ray cluster.
|
| 166 |
+
|
| 167 |
+
Label keys and values can be no longer than 64 characters (Unicode
|
| 168 |
+
codepoints), can only contain lowercase letters, numeric characters,
|
| 169 |
+
underscores and dashes. International characters are allowed.
|
| 170 |
+
|
| 171 |
+
See https://goo.gl/xmQnxf for more information and examples of labels.
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
cluster_resource_name: str = None
|
| 175 |
+
network: str = None
|
| 176 |
+
reserved_ip_ranges: List[str] = None
|
| 177 |
+
service_account: str = None
|
| 178 |
+
state: PersistentResource.State = None
|
| 179 |
+
python_version: str = None
|
| 180 |
+
ray_version: str = None
|
| 181 |
+
head_node_type: Resources = None
|
| 182 |
+
worker_node_types: List[Resources] = None
|
| 183 |
+
dashboard_address: str = None
|
| 184 |
+
ray_metric_enabled: bool = True
|
| 185 |
+
ray_logs_enabled: bool = True
|
| 186 |
+
psc_interface_config: PscIConfig = None
|
| 187 |
+
labels: Dict[str, str] = None
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def _check_machine_spec_identical(
|
| 191 |
+
node_type_1: Resources,
|
| 192 |
+
node_type_2: Resources,
|
| 193 |
+
) -> int:
|
| 194 |
+
"""Check if node_type_1 and node_type_2 have the same machine_spec.
|
| 195 |
+
If they are identical, return additional_replica_count."""
|
| 196 |
+
additional_replica_count = 0
|
| 197 |
+
|
| 198 |
+
# Check if machine_spec are the same
|
| 199 |
+
if (
|
| 200 |
+
node_type_1.machine_type == node_type_2.machine_type
|
| 201 |
+
and node_type_1.accelerator_type == node_type_2.accelerator_type
|
| 202 |
+
and node_type_1.accelerator_count == node_type_2.accelerator_count
|
| 203 |
+
):
|
| 204 |
+
if node_type_1.boot_disk_type != node_type_2.boot_disk_type:
|
| 205 |
+
raise ValueError(
|
| 206 |
+
"Worker disk type must match the head node's disk type if"
|
| 207 |
+
" sharing the same machine_type, accelerator_type, and"
|
| 208 |
+
" accelerator_count"
|
| 209 |
+
)
|
| 210 |
+
if node_type_1.boot_disk_size_gb != node_type_2.boot_disk_size_gb:
|
| 211 |
+
raise ValueError(
|
| 212 |
+
"Worker disk size must match the head node's disk size if"
|
| 213 |
+
" sharing the same machine_type, accelerator_type, and"
|
| 214 |
+
" accelerator_count"
|
| 215 |
+
)
|
| 216 |
+
additional_replica_count = node_type_2.node_count
|
| 217 |
+
return additional_replica_count
|
| 218 |
+
|
| 219 |
+
return additional_replica_count
|