|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Regenerate YAML config templates from schema defaults + overrides. |
| 18 | +
|
| 19 | +Used by pre-commit to keep templates in sync when schema.py changes. |
| 20 | +Overrides below are to make templates more readable, rest default. |
| 21 | +""" |
| 22 | + |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import yaml |
| 26 | +from inference_endpoint.config.schema import ( |
| 27 | + BenchmarkConfig, |
| 28 | + Dataset, |
| 29 | + EndpointConfig, |
| 30 | + LoadPattern, |
| 31 | + LoadPatternType, |
| 32 | + ModelParams, |
| 33 | + OnlineSettings, |
| 34 | + TestType, |
| 35 | +) |
| 36 | +from inference_endpoint.exceptions import CLIError |
| 37 | + |
| 38 | +TEMPLATES_DIR = Path(__file__).parent.parent / "src/inference_endpoint/config/templates" |
| 39 | + |
| 40 | +_COMMON = { |
| 41 | + "model_params": ModelParams( |
| 42 | + name="<MODEL_NAME eg: meta-llama/Llama-3.1-8B-Instruct>", |
| 43 | + temperature=0.7, |
| 44 | + top_p=0.9, |
| 45 | + max_new_tokens=1024, |
| 46 | + ), |
| 47 | + "endpoint_config": EndpointConfig( |
| 48 | + endpoints=["<ENDPOINT_URL eg: http://localhost:8000>"], |
| 49 | + ), |
| 50 | + "datasets": [ |
| 51 | + Dataset( |
| 52 | + name="perf-test", |
| 53 | + type="performance", |
| 54 | + path="<DATASET_PATH eg: tests/datasets/dummy_1k.jsonl>", |
| 55 | + samples=1000, |
| 56 | + parser={"prompt": "text_input"}, |
| 57 | + ) |
| 58 | + ], |
| 59 | +} |
| 60 | + |
| 61 | +_TEMPLATES = { |
| 62 | + "offline": {**_COMMON}, |
| 63 | + "online": { |
| 64 | + **_COMMON, |
| 65 | + "settings": OnlineSettings( |
| 66 | + load_pattern=LoadPattern(type=LoadPatternType.POISSON, target_qps=10.0), |
| 67 | + ), |
| 68 | + }, |
| 69 | + "concurrency": { |
| 70 | + **_COMMON, |
| 71 | + "settings": OnlineSettings( |
| 72 | + load_pattern=LoadPattern( |
| 73 | + type=LoadPatternType.CONCURRENCY, target_concurrency=32 |
| 74 | + ), |
| 75 | + ), |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +_EXCLUDE = {"verbose", "submission_ref", "benchmark_mode"} |
| 80 | + |
| 81 | + |
| 82 | +def _clean(full: dict) -> dict: |
| 83 | + out = {k: v for k, v in full.items() if k not in _EXCLUDE} |
| 84 | + if "settings" in out and "client" in out["settings"]: |
| 85 | + out["settings"]["client"]["num_workers"] = 4 |
| 86 | + return out |
| 87 | + |
| 88 | + |
| 89 | +def main(check_only: bool = False): |
| 90 | + """Regenerate templates, or check they're up to date (--check flag).""" |
| 91 | + stale = False |
| 92 | + for name, overrides in _TEMPLATES.items(): |
| 93 | + test_type = TestType.OFFLINE if name == "offline" else TestType.ONLINE |
| 94 | + try: |
| 95 | + base = BenchmarkConfig.create_default_config(test_type) |
| 96 | + cfg = base.with_updates(**overrides) |
| 97 | + except (CLIError, ValueError) as e: |
| 98 | + print(f" FAIL: {name} ({e})") |
| 99 | + stale = True |
| 100 | + continue |
| 101 | + |
| 102 | + expected = yaml.dump( |
| 103 | + _clean(cfg.model_dump(mode="json")), |
| 104 | + default_flow_style=False, |
| 105 | + sort_keys=False, |
| 106 | + ) |
| 107 | + path = TEMPLATES_DIR / f"{name}_template.yaml" |
| 108 | + |
| 109 | + if check_only: |
| 110 | + current = path.read_text() if path.exists() else "" |
| 111 | + if current != expected: |
| 112 | + print(f" STALE: {path.name}") |
| 113 | + stale = True |
| 114 | + else: |
| 115 | + print(f" OK: {path.name}") |
| 116 | + else: |
| 117 | + path.write_text(expected) |
| 118 | + print(f" Generated: {path.name}") |
| 119 | + |
| 120 | + if stale: |
| 121 | + print("\nRun: python scripts/regenerate_templates.py") |
| 122 | + raise SystemExit(1) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + import sys |
| 127 | + |
| 128 | + main(check_only="--check" in sys.argv) |
0 commit comments