Repository navigation
Expand file tree
/
Copy pathcserve_recipe.py
More file actions
289 lines (246 loc) · 10 KB
/
Copy pathcserve_recipe.py
File metadata and controls
289 lines (246 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import json
import sys
from functools import wraps
import click
from centml.sdk.ops import get_centml_ops_client
def handle_exception(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except ImportError as e:
click.echo(f"Error: {e}")
click.echo("Please install platform-api-ops-client to use this feature.")
return None
except Exception as e:
click.echo(f"Error: {e}")
return None
return wrapper
@click.command(help="Update CServe recipes from platform_db.json file")
@click.argument("platform_db_file", type=click.Path(exists=True))
@click.option(
"--cluster-id",
type=int,
required=True,
help="The cluster ID to associate with hardware instances",
)
@handle_exception
def update(platform_db_file, cluster_id):
"""
Update CServe recipes from platform_db.json performance data.
This command reads a platform_db.json file containing performance test results
and updates the CServe recipe configurations in the database.
Example:
centml cserve-recipe update platform_db.json --cluster-id 1001
"""
# Load platform_db.json file
try:
with open(platform_db_file, "r") as f:
platform_data = json.load(f)
except json.JSONDecodeError:
click.echo(f"Error: Invalid JSON file: {platform_db_file}")
sys.exit(1)
except Exception as e:
click.echo(f"Error reading file: {e}")
sys.exit(1)
# Validate platform_data structure
if not isinstance(platform_data, dict):
click.echo("Error: platform_db.json should contain a dictionary of models")
sys.exit(1)
click.echo(f"Processing {len(platform_data)} models from {platform_db_file}...")
click.echo(f"Target cluster ID: {cluster_id}")
with get_centml_ops_client() as ops_client:
response = ops_client.update_cserve_recipes(
cluster_id=cluster_id, platform_data=platform_data
)
# Display results
click.echo("\n" + "=" * 60)
click.echo(click.style("✓ Update Complete", fg="green", bold=True))
click.echo("=" * 60 + "\n")
click.echo(click.style(response.message, fg="green"))
if response.processed_models:
click.echo(f"\nProcessed Models ({len(response.processed_models)}):")
for model in response.processed_models:
click.echo(f" ✓ {model}")
if response.errors:
click.echo(
click.style(f"\nErrors ({len(response.errors)}):", fg="red", bold=True)
)
for error in response.errors:
click.echo(click.style(f" ✗ {error}", fg="red"))
sys.exit(1)
@click.command(help="List available clusters")
@click.option(
"--show-hardware",
is_flag=True,
help="Show hardware instances for each cluster",
)
@handle_exception
def list_clusters(show_hardware):
"""
List available clusters for the organization.
Example:
centml cserve-recipe list-clusters
centml cserve-recipe list-clusters --show-hardware
"""
with get_centml_ops_client() as ops_client:
clusters_data = ops_client.get_clusters(
include_hardware_instances=show_hardware
)
if not clusters_data:
click.echo("No clusters found.")
return
# Handle different return types
if show_hardware:
clusters = [item["cluster"] for item in clusters_data]
else:
clusters = clusters_data
click.echo(
f"\n{click.style('Available Clusters', bold=True, fg='cyan')} ({len(clusters)} found)\n"
)
for i, cluster in enumerate(clusters):
click.echo(f"{click.style('Cluster ID:', bold=True)} {cluster.id}")
click.echo(f" Display Name: {cluster.display_name}")
if cluster.region:
click.echo(f" Region: {cluster.region}")
if show_hardware:
hw_instances = clusters_data[i]["hardware_instances"]
if hw_instances:
click.echo(
f" {click.style('Hardware Instances:', fg='yellow')} ({len(hw_instances)} available)"
)
for hw in hw_instances:
gpu_info = (
f"{hw.num_accelerators}x{hw.gpu_type}"
if hw.num_accelerators
else hw.gpu_type
)
click.echo(
f" • {click.style(hw.name, fg='green')} (ID: {hw.id})"
)
click.echo(f" GPU: {gpu_info}")
click.echo(f" CPU: {hw.cpu} cores, Memory: {hw.memory} GB")
if hw.cost_per_hr:
click.echo(f" Cost: ${hw.cost_per_hr/100:.2f}/hr")
else:
click.echo(
f" {click.style('Hardware Instances:', fg='yellow')} None"
)
click.echo("")
@click.command(help="List CServe recipes")
@click.option(
"--model", help="Filter by model name (e.g., 'meta-llama/Llama-3.3-70B-Instruct')"
)
@click.option("--hf-token", help="HuggingFace token for private models")
@handle_exception
def list_recipes(model, hf_token):
"""
List CServe recipe configurations.
Example:
# List all recipes
centml cserve-recipe list
# List recipes for a specific model
centml cserve-recipe list --model "meta-llama/Llama-3.3-70B-Instruct"
"""
with get_centml_ops_client() as ops_client:
recipes = ops_client.get_cserve_recipes(model=model, hf_token=hf_token)
if not recipes:
click.echo("No recipes found.")
return
# Get all hardware instances to map IDs to names
try:
hardware_instances = ops_client.get_hardware_instances()
hw_map = {hw.id: hw for hw in hardware_instances}
except Exception as e:
click.echo(
click.style(
f"Warning: Could not fetch hardware instance details: {e}",
fg="yellow",
)
)
hw_map = {}
click.echo(
f"\n{click.style('CServe Recipes', bold=True, fg='cyan')} ({len(recipes)} found)\n"
)
def format_hw_info(hw_id):
"""Format hardware instance information"""
if hw_id in hw_map:
hw = hw_map[hw_id]
gpu_info = (
f"{hw.num_accelerators}x{hw.gpu_type}"
if hw.num_accelerators
else hw.gpu_type
)
return f"{hw.name} (ID: {hw_id}, {gpu_info})"
else:
return f"ID: {hw_id} {click.style('(details not found)', fg='yellow')}"
for recipe in recipes:
click.echo(f"{click.style('Model:', bold=True)} {recipe.model}")
# Display fastest configuration
if recipe.fastest:
click.echo(f" {click.style('Fastest:', fg='green')}")
click.echo(
f" Hardware: {format_hw_info(recipe.fastest.hardware_instance_id)}"
)
click.echo(f" Recipe: {recipe.fastest.recipe.model}")
if hasattr(recipe.fastest.recipe, "additional_properties"):
tp_size = recipe.fastest.recipe.additional_properties.get(
"tensor_parallel_size", "N/A"
)
pp_size = recipe.fastest.recipe.additional_properties.get(
"pipeline_parallel_size", "N/A"
)
click.echo(f" Parallelism: TP={tp_size}, PP={pp_size}")
# Display cheapest configuration
if recipe.cheapest:
if (
recipe.cheapest.hardware_instance_id
!= recipe.fastest.hardware_instance_id
):
click.echo(f" {click.style('Cheapest:', fg='yellow')}")
click.echo(
f" Hardware: {format_hw_info(recipe.cheapest.hardware_instance_id)}"
)
else:
click.echo(
f" {click.style('Cheapest:', fg='yellow')} Same as Fastest"
)
# Display best_value configuration
if recipe.best_value:
if (
recipe.best_value.hardware_instance_id
!= recipe.fastest.hardware_instance_id
):
click.echo(f" {click.style('Best Value:', fg='blue')}")
click.echo(
f" Hardware: {format_hw_info(recipe.best_value.hardware_instance_id)}"
)
else:
click.echo(
f" {click.style('Best Value:', fg='blue')} Same as Fastest"
)
click.echo("") # Empty line between recipes
@click.command(help="Delete CServe recipe for a specific model")
@click.argument("model")
@click.option("--confirm", is_flag=True, help="Skip confirmation prompt")
@handle_exception
def delete(model, confirm):
"""
Delete CServe recipe configurations for a specific model.
This will remove all recipe configurations (fastest, cheapest, best_value)
for the specified model.
Example:
centml cserve-recipe delete "meta-llama/Llama-3.3-70B-Instruct"
centml cserve-recipe delete "Qwen/Qwen3-0.6B" --confirm
"""
if not confirm:
if not click.confirm(
f"Are you sure you want to delete recipe for model '{model}'?"
):
click.echo("Cancelled.")
return
with get_centml_ops_client() as ops_client:
ops_client.delete_cserve_recipe(model=model)
click.echo(
click.style(f"✓ Successfully deleted recipe for model: {model}", fg="green")
)