|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any, Dict, Literal, Optional, Self, Union |
| 3 | + |
| 4 | +from pydantic import Field |
| 5 | + |
| 6 | +from guidellm.backend import BackendType |
| 7 | +from guidellm.core import Serializable |
| 8 | +from guidellm.executor import ProfileGenerationMode |
| 9 | + |
| 10 | +__ALL__ = ["Scenario", "ScenarioManager"] |
| 11 | + |
| 12 | +scenarios_path = Path(__name__).parent / "scenarios" |
| 13 | + |
| 14 | + |
| 15 | +class Scenario(Serializable): |
| 16 | + backend: BackendType = "openai_http" |
| 17 | + backend_kwargs: Optional[Dict[str, Any]] = None |
| 18 | + model: Optional[str] = None |
| 19 | + tokenizer: Optional[str] = None |
| 20 | + data: Union[str, Dict[str, Any]] = Field(default_factory=dict) |
| 21 | + data_type: Literal["emulated", "file", "transformers"] = "emulated" |
| 22 | + rate_type: ProfileGenerationMode = "sweep" |
| 23 | + rate: Optional[float] = None |
| 24 | + max_seconds: int = 120 |
| 25 | + max_requests: Optional[Union[int, Literal["dataset"]]] = None |
| 26 | + |
| 27 | + def _update(self, **fields: Mapping[str, Any]) -> Self: |
| 28 | + for k, v in fields.items(): |
| 29 | + if not hasattr(self, k): |
| 30 | + raise ValueError(f"Invalid field {k}") |
| 31 | + setattr(self, k, v) |
| 32 | + |
| 33 | + return self |
| 34 | + |
| 35 | + def update(self, **fields: Mapping[str, Any]) -> Self: |
| 36 | + return self._update(**{k: v for k, v in fields.items() if v is not None}) |
| 37 | + |
| 38 | + |
| 39 | +class ScenarioManager: |
| 40 | + def __init__(self, scenarios_dir: Optional[str] = None): |
| 41 | + self.scenarios: Dict[str, Scenario] = {} |
| 42 | + |
| 43 | + if scenarios_dir is None: |
| 44 | + global scenarios_path |
| 45 | + else: |
| 46 | + scenarios_path = Path(scenarios_dir) |
| 47 | + |
| 48 | + # Load built-in scenarios |
| 49 | + for scenario_path in scenarios_path.glob("*.json"): |
| 50 | + scenario = Scenario.from_json(scenario_path.read_text()) |
| 51 | + self[scenario_path.stem] = scenario |
| 52 | + |
| 53 | + def __getitem__(self, scenario_name: str) -> Scenario: |
| 54 | + return self.scenarios[scenario_name] |
| 55 | + |
| 56 | + def __setitem__(self, scenario_name: str, scenario: Scenario): |
| 57 | + if scenario_name in self.scenarios: |
| 58 | + raise ValueError(f"Scenario {scenario_name} already exists") |
| 59 | + |
| 60 | + self.scenarios[scenario_name] = scenario |
| 61 | + |
| 62 | + def list(self): |
| 63 | + return tuple(self.scenarios.keys()) |
0 commit comments