-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathentities.py
266 lines (219 loc) · 8.71 KB
/
entities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# Copyright (c) 2024, 2025 Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
import re
from typing import Any, Dict, Optional
from oci.data_science.models import Model
from pydantic import BaseModel, Field, model_validator
from ads.aqua import logger
from ads.aqua.config.utils.serializer import Serializable
class ContainerSpec:
"""
Class to hold to hold keys within the container spec.
"""
CONTAINER_SPEC = "containerSpec"
CLI_PARM = "cliParam"
SERVER_PORT = "serverPort"
HEALTH_CHECK_PORT = "healthCheckPort"
ENV_VARS = "envVars"
RESTRICTED_PARAMS = "restrictedParams"
EVALUATION_CONFIGURATION = "evaluationConfiguration"
class ModelConfigResult(BaseModel):
"""
Represents the result of getting the AQUA model configuration.
Attributes:
model_details (Dict[str, Any]): A dictionary containing model details extracted from OCI.
config (Dict[str, Any]): A dictionary of the loaded configuration.
"""
config: Optional[Dict[str, Any]] = Field(
None, description="Loaded configuration dictionary."
)
model_details: Optional[Model] = Field(
None, description="Details of the model from OCI."
)
class Config:
extra = "ignore"
arbitrary_types_allowed = True
protected_namespaces = ()
class GPUSpecs(Serializable):
"""
Represents the GPU specifications for a compute instance.
"""
gpu_memory_in_gbs: Optional[int] = Field(
default=None, description="The amount of GPU memory available (in GB)."
)
gpu_count: Optional[int] = Field(
default=None, description="The number of GPUs available."
)
gpu_type: Optional[str] = Field(
default=None, description="The type of GPU (e.g., 'V100, A100, H100')."
)
class GPUShapesIndex(Serializable):
"""
Represents the index of GPU shapes.
Attributes
----------
shapes (Dict[str, GPUSpecs]): A mapping of compute shape names to their GPU specifications.
"""
shapes: Dict[str, GPUSpecs] = Field(
default_factory=dict,
description="Mapping of shape names to GPU specifications.",
)
class ComputeShapeSummary(Serializable):
"""
Represents the specifications of a compute instance's shape.
"""
core_count: Optional[int] = Field(
default=None, description="The number of CPU cores available."
)
memory_in_gbs: Optional[int] = Field(
default=None, description="The amount of memory (in GB) available."
)
name: Optional[str] = Field(
default=None, description="The name identifier of the compute shape."
)
shape_series: Optional[str] = Field(
default=None, description="The series or category of the compute shape."
)
gpu_specs: Optional[GPUSpecs] = Field(
default=None,
description="The GPU specifications associated with the compute shape.",
)
@model_validator(mode="after")
@classmethod
def set_gpu_specs(cls, model: "ComputeShapeSummary") -> "ComputeShapeSummary":
"""
Validates and populates GPU specifications if the shape_series indicates a GPU-based shape.
- If the shape_series contains "GPU", the validator first checks if the shape name exists
in the GPU_SPECS dictionary. If found, it creates a GPUSpecs instance with the corresponding data.
- If the shape is not found in the GPU_SPECS, it attempts to extract the GPU count from the shape name
using a regex pattern (looking for a number following a dot at the end of the name).
The information about shapes is taken from: https://docs.oracle.com/en-us/iaas/data-science/using/supported-shapes.htm
Returns:
ComputeShapeSummary: The updated instance with gpu_specs populated if applicable.
"""
try:
if (
model.shape_series
and "GPU" in model.shape_series.upper()
and model.name
and not model.gpu_specs
):
# Try to extract gpu_count from the shape name using a regex (e.g., "VM.GPU3.2" -> gpu_count=2)
match = re.search(r"\.(\d+)$", model.name)
if match:
gpu_count = int(match.group(1))
model.gpu_specs = GPUSpecs(gpu_count=gpu_count)
except Exception as err:
logger.debug(
f"Error occurred in attempt to extract GPU specification for the f{model.name}. "
f"Details: {err}"
)
return model
class AquaMultiModelRef(Serializable):
"""
Lightweight model descriptor used for multi-model deployment.
This class only contains essential details
required to fetch complete model metadata and deploy models.
Attributes
----------
model_id : str
The unique identifier of the model.
model_name : Optional[str]
The name of the model.
gpu_count : Optional[int]
Number of GPUs required for deployment.
env_var : Optional[Dict[str, Any]]
Optional environment variables to override during deployment.
artifact_location : Optional[str]
Artifact path of model in the multimodel group.
"""
model_id: str = Field(..., description="The model OCID to deploy.")
model_name: Optional[str] = Field(None, description="The name of model.")
gpu_count: Optional[int] = Field(
None, description="The gpu count allocation for the model."
)
env_var: Optional[dict] = Field(
default_factory=dict, description="The environment variables of the model."
)
artifact_location: Optional[str] = Field(
None, description="Artifact path of model in the multimodel group."
)
class Config:
extra = "ignore"
protected_namespaces = ()
class ContainerPath(Serializable):
"""
Represents a parsed container path, extracting the path, name, and version.
This model is designed to parse a container path string of the format
'<image_path>:<version>'. It extracts the following components:
- `path`: The full path up to the version.
- `name`: The last segment of the path, representing the image name.
- `version`: The version number following the final colon.
Example Usage:
--------------
>>> container = ContainerPath(full_path="iad.ocir.io/ociodscdev/odsc-llm-evaluate:0.1.2.9")
>>> container.path
'iad.ocir.io/ociodscdev/odsc-llm-evaluate'
>>> container.name
'odsc-llm-evaluate'
>>> container.version
'0.1.2.9'
>>> container = ContainerPath(full_path="custom-scheme://path/to/versioned-model:2.5.1")
>>> container.path
'custom-scheme://path/to/versioned-model'
>>> container.name
'versioned-model'
>>> container.version
'2.5.1'
Attributes
----------
full_path : str
The complete container path string to be parsed.
path : Optional[str]
The full path up to the version (e.g., 'iad.ocir.io/ociodscdev/odsc-llm-evaluate').
name : Optional[str]
The image name, which is the last segment of `path` (e.g., 'odsc-llm-evaluate').
version : Optional[str]
The version number following the final colon in the path (e.g., '0.1.2.9').
Methods
-------
validate(values: Any) -> Any
Validates and parses the `full_path`, extracting `path`, `name`, and `version`.
"""
full_path: str
path: Optional[str] = None
name: Optional[str] = None
version: Optional[str] = None
@model_validator(mode="before")
@classmethod
def validate(cls, values: Any) -> Any:
"""
Validates and parses the full container path, extracting the image path, image name, and version.
Parameters
----------
values : dict
The dictionary of values being validated, containing 'full_path'.
Returns
-------
dict
Updated values dictionary with extracted 'path', 'name', and 'version'.
"""
full_path = values.get("full_path", "").strip()
# Regex to parse <image_path>:<version>
match = re.match(
r"^(?P<image_path>.+?)(?::(?P<image_version>[\w\.]+))?$", full_path
)
if not match:
raise ValueError(
"Invalid container path format. Expected format: '<image_path>:<version>'"
)
# Extract image_path and version
values["path"] = match.group("image_path")
values["version"] = match.group("image_version")
# Extract image_name as the last segment of image_path
values["name"] = values["path"].split("/")[-1]
return values
class Config:
extra = "ignore"
protected_namespaces = ()