|
3 | 3 | """CLI command for running the Profile subcommand.""" |
4 | 4 |
|
5 | 5 | import asyncio |
| 6 | +import logging |
6 | 7 |
|
7 | 8 | from cyclopts import App |
8 | 9 |
|
|
11 | 12 | app = App(name="profile") |
12 | 13 |
|
13 | 14 |
|
| 15 | +def _ensure_stderr_logging() -> None: |
| 16 | + if not logging.getLogger().handlers: |
| 17 | + logging.basicConfig( |
| 18 | + level=logging.INFO, |
| 19 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def _request_headers_for_endpoint(user_config: UserConfig) -> dict[str, str]: |
| 24 | + raw_headers = user_config.input.headers or [] |
| 25 | + headers = {str(k): str(v) for k, v in raw_headers} |
| 26 | + if user_config.endpoint.api_key: |
| 27 | + headers["Authorization"] = f"Bearer {user_config.endpoint.api_key}" |
| 28 | + return headers |
| 29 | + |
| 30 | + |
| 31 | +def _maybe_autodiscover_models(user_config: UserConfig) -> None: |
| 32 | + # If the user didn't provide --model/--model-names, try to discover |
| 33 | + # one from the server's OpenAI-compatible model list. |
| 34 | + if user_config.endpoint.model_names: |
| 35 | + return |
| 36 | + |
| 37 | + from aiperf.common.config.config_defaults import OutputDefaults |
| 38 | + from aiperf.common.models.model_autodetect import ( |
| 39 | + autodetect_names, |
| 40 | + ) |
| 41 | + |
| 42 | + # Install a basic stderr handler so the log message is visible even |
| 43 | + # when `--wait-for-model-timeout` is left at the default (0). |
| 44 | + _ensure_stderr_logging() |
| 45 | + |
| 46 | + user_config.endpoint.model_names = asyncio.run( |
| 47 | + autodetect_names( |
| 48 | + urls=user_config.endpoint.urls, |
| 49 | + headers=_request_headers_for_endpoint(user_config), |
| 50 | + ) |
| 51 | + ) |
| 52 | + |
| 53 | + # `UserConfig` computed an artifact directory during config-load. |
| 54 | + # If it used the default artifact directory (not overridden by the |
| 55 | + # user), update it to reflect the discovered model name. |
| 56 | + if "artifact_directory" not in user_config.output.model_fields_set: |
| 57 | + user_config.output.artifact_directory = OutputDefaults.ARTIFACT_DIRECTORY |
| 58 | + user_config.output.artifact_directory = ( |
| 59 | + user_config._compute_artifact_directory() |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def _maybe_wait_for_model(user_config: UserConfig) -> None: |
| 64 | + if user_config.endpoint.wait_for_model_timeout <= 0: |
| 65 | + return |
| 66 | + |
| 67 | + from aiperf.common.readiness_probe import wait_for_endpoint |
| 68 | + |
| 69 | + # The probe runs before `run_system_controller` (which installs |
| 70 | + # rich logging), so there are no handlers attached yet. Install |
| 71 | + # a basic stderr handler so probe log messages are visible. |
| 72 | + _ensure_stderr_logging() |
| 73 | + |
| 74 | + asyncio.run( |
| 75 | + wait_for_endpoint( |
| 76 | + urls=user_config.endpoint.urls, |
| 77 | + model_names=user_config.endpoint.model_names, |
| 78 | + mode=user_config.endpoint.wait_for_model_mode, |
| 79 | + endpoint_type=str(user_config.endpoint.type), |
| 80 | + custom_endpoint=user_config.endpoint.custom_endpoint, |
| 81 | + timeout_s=user_config.endpoint.wait_for_model_timeout, |
| 82 | + interval_s=user_config.endpoint.wait_for_model_interval, |
| 83 | + headers=_request_headers_for_endpoint(user_config), |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + |
14 | 88 | @app.default |
15 | 89 | def profile( |
16 | 90 | user_config: UserConfig, |
@@ -54,78 +128,6 @@ def profile( |
54 | 128 | from aiperf.common.config.loader import load_service_config |
55 | 129 |
|
56 | 130 | service_config = service_config or load_service_config() |
57 | | - |
58 | | - # If the user didn't provide --model/--model-names, try to discover |
59 | | - # one from the server's OpenAI-compatible model list. |
60 | | - if not user_config.endpoint.model_names: |
61 | | - import logging |
62 | | - |
63 | | - from aiperf.common.config.config_defaults import OutputDefaults |
64 | | - from aiperf.common.models.model_autodetect import ( |
65 | | - autodetect_model_names_from_v1_models, |
66 | | - ) |
67 | | - |
68 | | - # Install a basic stderr handler so the log message is visible even |
69 | | - # when `--wait-for-model-timeout` is left at the default (0). |
70 | | - if not logging.getLogger().handlers: |
71 | | - logging.basicConfig( |
72 | | - level=logging.INFO, |
73 | | - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
74 | | - ) |
75 | | - |
76 | | - raw_headers = user_config.input.headers or [] |
77 | | - headers = {str(k): str(v) for k, v in raw_headers} |
78 | | - if user_config.endpoint.api_key: |
79 | | - headers["Authorization"] = f"Bearer {user_config.endpoint.api_key}" |
80 | | - |
81 | | - user_config.endpoint.model_names = asyncio.run( |
82 | | - autodetect_model_names_from_v1_models( |
83 | | - urls=user_config.endpoint.urls, |
84 | | - headers=headers, |
85 | | - ) |
86 | | - ) |
87 | | - |
88 | | - # `UserConfig` computed an artifact directory during config-load. |
89 | | - # If it used the default artifact directory (not overridden by the |
90 | | - # user), update it to reflect the discovered model name. |
91 | | - if "artifact_directory" not in user_config.output.model_fields_set: |
92 | | - user_config.output.artifact_directory = ( |
93 | | - OutputDefaults.ARTIFACT_DIRECTORY |
94 | | - ) |
95 | | - user_config.output.artifact_directory = ( |
96 | | - user_config._compute_artifact_directory() |
97 | | - ) |
98 | | - |
99 | | - if user_config.endpoint.wait_for_model_timeout > 0: |
100 | | - import logging |
101 | | - |
102 | | - from aiperf.common.readiness_probe import wait_for_endpoint |
103 | | - |
104 | | - # The probe runs before `run_system_controller` (which installs |
105 | | - # rich logging), so there are no handlers attached yet. Install |
106 | | - # a basic stderr handler so probe log messages are visible. |
107 | | - if not logging.getLogger().handlers: |
108 | | - logging.basicConfig( |
109 | | - level=logging.INFO, |
110 | | - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
111 | | - ) |
112 | | - |
113 | | - raw_headers = user_config.input.headers or [] |
114 | | - headers = {str(k): str(v) for k, v in raw_headers} |
115 | | - if user_config.endpoint.api_key: |
116 | | - headers["Authorization"] = f"Bearer {user_config.endpoint.api_key}" |
117 | | - |
118 | | - asyncio.run( |
119 | | - wait_for_endpoint( |
120 | | - urls=user_config.endpoint.urls, |
121 | | - model_names=user_config.endpoint.model_names, |
122 | | - mode=user_config.endpoint.wait_for_model_mode, |
123 | | - endpoint_type=str(user_config.endpoint.type), |
124 | | - custom_endpoint=user_config.endpoint.custom_endpoint, |
125 | | - timeout_s=user_config.endpoint.wait_for_model_timeout, |
126 | | - interval_s=user_config.endpoint.wait_for_model_interval, |
127 | | - headers=headers, |
128 | | - ) |
129 | | - ) |
130 | | - |
| 131 | + _maybe_autodiscover_models(user_config) |
| 132 | + _maybe_wait_for_model(user_config) |
131 | 133 | run_system_controller(user_config, service_config) |
0 commit comments