|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -import typer |
| 5 | +from datetime import timedelta |
| 6 | +from enum import StrEnum, auto |
| 7 | +from typing import Annotated |
6 | 8 |
|
7 | | -app = typer.Typer() |
| 9 | +import mlperf_loadgen as lg |
| 10 | +from loguru import logger |
| 11 | +from openai import AsyncOpenAI, DefaultAioHttpClient |
| 12 | +from pydantic import BaseModel, Field |
| 13 | +from pydantic_typer import Typer |
| 14 | + |
| 15 | +from .task import MMMU, ShopifyGlobalCatalogue, Task |
| 16 | + |
| 17 | +app = Typer() |
| 18 | + |
| 19 | + |
| 20 | +class TestScenario(StrEnum): |
| 21 | + """The test scenario for the MLPerf inference LoadGen.""" |
| 22 | + |
| 23 | + SERVER = auto() |
| 24 | + """Run the benchmark in server/interactive scenario.""" |
| 25 | + |
| 26 | + OFFLINE = auto() |
| 27 | + """Run the benchmark in offline/batch scenario.""" |
| 28 | + |
| 29 | + class UnknownValueError(ValueError): |
| 30 | + """The exception raised when an unknown test scenario is encountered.""" |
| 31 | + |
| 32 | + def __init__(self, test_scenario: TestScenario) -> None: |
| 33 | + """Initialize the exception.""" |
| 34 | + super().__init__(f"Unknown test scenario: {test_scenario}") |
| 35 | + |
| 36 | + def to_lgtype(self) -> lg.TestScenario: |
| 37 | + """Convert the test scenario to its corresponding LoadGen type.""" |
| 38 | + match self: |
| 39 | + case TestScenario.SERVER: |
| 40 | + return lg.TestScenario.Server |
| 41 | + case TestScenario.OFFLINE: |
| 42 | + return lg.TestScenario.Offline |
| 43 | + case _: |
| 44 | + raise TestScenario.UnknownValueError(self) |
| 45 | + |
| 46 | + |
| 47 | +class TestMode(StrEnum): |
| 48 | + """The test mode for the MLPerf inference LoadGen.""" |
| 49 | + |
| 50 | + PERFORMANCE_ONLY = auto() |
| 51 | + """Run the benchmark to evaluate performance.""" |
| 52 | + |
| 53 | + ACCURACY_ONLY = auto() |
| 54 | + """Run the benchmark to evaluate model quality.""" |
| 55 | + |
| 56 | + class UnknownValueError(ValueError): |
| 57 | + """The exception raised when an unknown test mode is encountered.""" |
| 58 | + |
| 59 | + def __init__(self, test_mode: TestMode) -> None: |
| 60 | + """Initialize the exception.""" |
| 61 | + super().__init__(f"Unknown test mode: {test_mode}") |
| 62 | + |
| 63 | + def to_lgtype(self) -> lg.TestMode: |
| 64 | + """Convert the test mode to its corresponding LoadGen type.""" |
| 65 | + match self: |
| 66 | + case TestMode.PERFORMANCE_ONLY: |
| 67 | + return lg.TestMode.PerformanceOnly |
| 68 | + case TestMode.ACCURACY_ONLY: |
| 69 | + return lg.TestMode.AccuracyOnly |
| 70 | + case _: |
| 71 | + raise TestMode.UnknownValueError(self) |
| 72 | + |
| 73 | + |
| 74 | +class TestSettings(BaseModel): |
| 75 | + """The test settings for the MLPerf inference LoadGen.""" |
| 76 | + |
| 77 | + senario: Annotated[ |
| 78 | + TestScenario, |
| 79 | + Field( |
| 80 | + description=( |
| 81 | + "The MLPerf inference benchmarking scenario to run the benchmark in." |
| 82 | + ), |
| 83 | + ), |
| 84 | + ] = TestScenario.OFFLINE |
| 85 | + |
| 86 | + mode: Annotated[ |
| 87 | + TestMode, |
| 88 | + Field( |
| 89 | + description=( |
| 90 | + "Whether you want to run the benchmark for performance or accuracy." |
| 91 | + ), |
| 92 | + ), |
| 93 | + ] = TestMode.PERFORMANCE_ONLY |
| 94 | + |
| 95 | + offline_expected_qps: Annotated[ |
| 96 | + float, |
| 97 | + Field( |
| 98 | + description="The expected QPS for the offline scenario.", |
| 99 | + ), |
| 100 | + ] = 10 |
| 101 | + |
| 102 | + min_duration: Annotated[ |
| 103 | + timedelta, |
| 104 | + Field( |
| 105 | + description="The minimum testing duration.", |
| 106 | + ), |
| 107 | + ] = timedelta(seconds=5) |
| 108 | + |
| 109 | + def to_lgtype(self) -> lg.TestSettings: |
| 110 | + """Convert the test settings to its corresponding LoadGen type.""" |
| 111 | + settings = lg.TestSettings() |
| 112 | + settings.scenario = self.senario.to_lgtype() |
| 113 | + settings.mode = self.mode.to_lgtype() |
| 114 | + settings.offline_expected_qps = self.offline_expected_qps |
| 115 | + settings.min_duration_ms = round(self.min_duration.total_seconds() * 1000) |
| 116 | + settings.use_token_latencies = True |
| 117 | + return settings |
| 118 | + |
| 119 | + |
| 120 | +class Model(BaseModel): |
| 121 | + """Specifies the model to use for the VL2L benchmark.""" |
| 122 | + |
| 123 | + repo_id: Annotated[ |
| 124 | + str, |
| 125 | + Field(description="The HuggingFace repository ID of the model."), |
| 126 | + ] = "Qwen/Qwen3-VL-30B-A3B-Instruct" |
| 127 | + |
| 128 | + |
| 129 | +class Dataset(BaseModel): |
| 130 | + """Specifies a dataset on HuggingFace.""" |
| 131 | + |
| 132 | + class Task(StrEnum): |
| 133 | + """The task for the VL2L benchmark.""" |
| 134 | + |
| 135 | + SHOPIFY_GLOBAL_CATALOG = auto() |
| 136 | + MMMU = auto() |
| 137 | + |
| 138 | + class UnknownTaskError(ValueError): |
| 139 | + """The exception raised when an unknown task is encountered.""" |
| 140 | + |
| 141 | + def __init__(self, task: Dataset.Task) -> None: |
| 142 | + """Initialize the exception.""" |
| 143 | + super().__init__(f"Unknown task: {task}") |
| 144 | + |
| 145 | + task: Annotated[ |
| 146 | + Dataset.Task | None, |
| 147 | + Field( |
| 148 | + description=( |
| 149 | + "The vision-language-to-language task to run the benchmark for. If not " |
| 150 | + "specified, the task will be derived from the HuggingFace repository ID" |
| 151 | + " of the dataset." |
| 152 | + ), |
| 153 | + ), |
| 154 | + ] = None |
| 155 | + |
| 156 | + repo_id: Annotated[ |
| 157 | + str, |
| 158 | + Field(description="The HuggingFace repository ID of the dataset."), |
| 159 | + ] = "Shopify/the-catalogue-public-beta" |
| 160 | + |
| 161 | + token: Annotated[ |
| 162 | + str | None, |
| 163 | + Field( |
| 164 | + description=( |
| 165 | + "The token to access the HuggingFace repository of the dataset." |
| 166 | + ), |
| 167 | + ), |
| 168 | + ] = None |
| 169 | + |
| 170 | + |
| 171 | +def create_task(dataset: Dataset, model: Model, openai_api_client: AsyncOpenAI) -> Task: |
| 172 | + """Convert the dataset configuration to its corresponding task.""" |
| 173 | + match dataset.task: |
| 174 | + case Dataset.Task.MMMU: |
| 175 | + return MMMU(dataset, model, openai_api_client) |
| 176 | + case Dataset.Task.SHOPIFY_GLOBAL_CATALOG: |
| 177 | + return ShopifyGlobalCatalogue(dataset, model, openai_api_client) |
| 178 | + case None: |
| 179 | + match dataset.repo_id: |
| 180 | + case "MMMU/MMMU": |
| 181 | + return MMMU(dataset, model, openai_api_client) |
| 182 | + case "Shopify/the-catalogue-public-beta": |
| 183 | + return ShopifyGlobalCatalogue(dataset, model, openai_api_client) |
| 184 | + case _: |
| 185 | + raise Dataset.UnknownTaskError(dataset.task) |
| 186 | + case _: |
| 187 | + raise Dataset.UnknownTaskError(dataset.task) |
8 | 188 |
|
9 | 189 |
|
10 | 190 | @app.command() |
11 | | -def main(): |
12 | | - """VL2L benchmark CLI""" |
13 | | - typer.echo("Hello, World!") |
| 191 | +def main( |
| 192 | + *, |
| 193 | + settings: TestSettings, |
| 194 | + model: Model, |
| 195 | + dataset: Dataset, |
| 196 | + endpoint: str = "http://localhost:8000/v1", |
| 197 | + openai_api_key: str = "", |
| 198 | +) -> None: |
| 199 | + """Main CLI for running the VL2L benchmark.""" |
| 200 | + logger.info("Running VL2L benchmark with settings: {}", settings) |
| 201 | + logger.info("Running VL2L benchmark with dataset: {}", dataset) |
| 202 | + logger.info("Running VL2L benchmark with endpoint: {}", endpoint) |
| 203 | + lg_settings = settings.to_lgtype() |
| 204 | + task = create_task( |
| 205 | + dataset, |
| 206 | + model, |
| 207 | + AsyncOpenAI( |
| 208 | + base_url=endpoint, |
| 209 | + http_client=DefaultAioHttpClient(), |
| 210 | + api_key=openai_api_key, |
| 211 | + ), |
| 212 | + ) |
| 213 | + sut = task.construct_sut() |
| 214 | + qsl = task.construct_qsl() |
| 215 | + lg.StartTest(sut, qsl, lg_settings) |
| 216 | + lg.DestroyQSL(qsl) |
| 217 | + lg.DestroySUT(sut) |
0 commit comments