forked from llm-d-incubation/llm-d-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecification.py
More file actions
230 lines (186 loc) · 8.38 KB
/
specification.py
File metadata and controls
230 lines (186 loc) · 8.38 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
"""Specification-related endpoints (SLO defaults, workload profiles)."""
import json
import logging
from pathlib import Path
from fastapi import APIRouter, HTTPException, status
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/v1", tags=["specification"])
def _round_to_nearest(value: float, nearest: int = 5) -> int:
"""Round a value to the nearest multiple of `nearest`."""
return int(round(value / nearest) * nearest)
def _calculate_percentile_value(min_val: int, max_val: int, percentile: float = 0.75) -> int:
"""Calculate value at given percentile between min and max, rounded to nearest 5."""
value = min_val + (max_val - min_val) * percentile
return _round_to_nearest(value, 5)
def _get_slo_workload_path() -> Path:
"""Get path to the SLO workload config file."""
return (
Path(__file__).parent.parent.parent.parent.parent
/ "data"
/ "configuration"
/ "usecase_slo_workload.json"
)
@router.get("/slo-defaults/{use_case}")
async def get_slo_defaults(use_case: str):
"""Get default SLO values for a use case.
Returns SLO targets at the 75th percentile between min and max,
rounded to the nearest 5.
"""
try:
json_path = _get_slo_workload_path()
if not json_path.exists():
logger.error(f"SLO workload config not found at: {json_path}")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="SLO workload configuration not found"
)
with open(json_path) as f:
data = json.load(f)
use_case_data = data.get("use_case_slo_workload", {}).get(use_case)
if not use_case_data:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=f"Use case '{use_case}' not found"
)
slo_targets = use_case_data.get("slo_targets", {})
# Get SLO ranges - fail if data is missing
ttft = slo_targets["ttft_ms"]
itl = slo_targets["itl_ms"]
e2e = slo_targets["e2e_ms"]
defaults = {
"use_case": use_case,
"description": use_case_data.get("description", ""),
"ttft_ms": {
"min": ttft["min"],
"max": ttft["max"],
"default": _calculate_percentile_value(ttft["min"], ttft["max"], 0.75),
},
"itl_ms": {
"min": itl["min"],
"max": itl["max"],
"default": _calculate_percentile_value(itl["min"], itl["max"], 0.75),
},
"e2e_ms": {
"min": e2e["min"],
"max": e2e["max"],
"default": _calculate_percentile_value(e2e["min"], e2e["max"], 0.75),
},
}
return {"success": True, "slo_defaults": defaults}
except HTTPException:
raise
except KeyError as e:
logger.error(f"Missing SLO data for {use_case}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Missing SLO data: {e}"
) from e
except Exception as e:
logger.error(f"Failed to get SLO defaults for {use_case}: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
@router.get("/workload-profile/{use_case}")
async def get_workload_profile(use_case: str):
"""Get workload profile for a use case.
Returns the token configuration and peak multiplier used for
capacity planning and recommendation generation.
"""
try:
json_path = _get_slo_workload_path()
if not json_path.exists():
logger.error(f"SLO workload config not found at: {json_path}")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="SLO workload configuration not found"
)
with open(json_path) as f:
data = json.load(f)
use_case_data = data.get("use_case_slo_workload", {}).get(use_case)
if not use_case_data:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=f"Use case '{use_case}' not found"
)
workload = use_case_data.get("workload", {})
# Require all workload fields - fail if missing
prompt_tokens = workload["prompt_tokens"]
output_tokens = workload["output_tokens"]
peak_multiplier = workload["peak_multiplier"]
distribution = workload["distribution"]
active_fraction = workload["active_fraction"]
requests_per_active_user_per_min = workload["requests_per_active_user_per_min"]
return {
"success": True,
"use_case": use_case,
"description": use_case_data.get("description", ""),
"workload_profile": {
"prompt_tokens": prompt_tokens,
"output_tokens": output_tokens,
"peak_multiplier": peak_multiplier,
"distribution": distribution,
"active_fraction": active_fraction,
"requests_per_active_user_per_min": requests_per_active_user_per_min,
},
}
except HTTPException:
raise
except KeyError as e:
logger.error(f"Missing workload data for {use_case}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Missing workload data: {e}"
) from e
except Exception as e:
logger.error(f"Failed to get workload profile for {use_case}: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e
@router.get("/expected-rps/{use_case}")
async def get_expected_rps(use_case: str, user_count: int = 1000):
"""Calculate expected RPS for a use case based on workload patterns.
Uses research-backed workload distribution parameters:
- active_fraction: percentage of users active at any time
- requests_per_active_user_per_min: request rate per active user
Formula: expected_rps = (user_count * active_fraction * requests_per_min) / 60
"""
try:
json_path = _get_slo_workload_path()
if not json_path.exists():
logger.error(f"SLO workload config not found at: {json_path}")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="SLO workload configuration not found"
)
with open(json_path) as f:
data = json.load(f)
use_case_data = data.get("use_case_slo_workload", {}).get(use_case)
if not use_case_data:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=f"Use case '{use_case}' not found"
)
workload = use_case_data.get("workload", {})
# Get workload parameters - fail if missing
active_fraction = workload["active_fraction"]
requests_per_min = workload["requests_per_active_user_per_min"]
peak_multiplier = workload.get("peak_multiplier", 2.0)
distribution = workload.get("distribution", "poisson")
# Calculate expected RPS using research-based formula
expected_concurrent = int(user_count * active_fraction)
expected_rps = (expected_concurrent * requests_per_min) / 60
expected_rps = max(1, round(expected_rps, 2)) # Minimum 1 RPS, round to 2 decimals
# Calculate peak RPS for capacity planning
peak_rps = expected_rps * peak_multiplier
return {
"success": True,
"use_case": use_case,
"user_count": user_count,
"workload_params": {
"active_fraction": active_fraction,
"requests_per_active_user_per_min": requests_per_min,
"peak_multiplier": peak_multiplier,
"distribution": distribution,
},
"expected_rps": expected_rps,
"expected_concurrent_users": expected_concurrent,
"peak_rps": round(peak_rps, 2),
}
except HTTPException:
raise
except KeyError as e:
logger.error(f"Missing workload data for {use_case}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Missing workload data: {e}"
) from e
except Exception as e:
logger.error(f"Failed to calculate expected RPS for {use_case}: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e