|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 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 | +import logging |
| 18 | +from copy import deepcopy |
| 19 | +from typing import Generator, List, Optional |
| 20 | + |
| 21 | +from model_analyzer.config.input.config_command_profile import ConfigCommandProfile |
| 22 | +from model_analyzer.config.run.run_config import RunConfig |
| 23 | +from model_analyzer.constants import LOGGER_NAME |
| 24 | +from model_analyzer.result.parameter_search import ParameterSearch |
| 25 | +from model_analyzer.result.result_manager import ResultManager |
| 26 | +from model_analyzer.result.run_config_measurement import RunConfigMeasurement |
| 27 | + |
| 28 | +logger = logging.getLogger(LOGGER_NAME) |
| 29 | + |
| 30 | + |
| 31 | +class ConcurrencySweeper: |
| 32 | + """ |
| 33 | + Sweeps concurrency for the top-N model configs |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + config: ConfigCommandProfile, |
| 39 | + result_manager: ResultManager, |
| 40 | + ): |
| 41 | + self._config = config |
| 42 | + self._result_manager = result_manager |
| 43 | + self._last_measurement: Optional[RunConfigMeasurement] = None |
| 44 | + |
| 45 | + def set_last_results( |
| 46 | + self, measurements: List[Optional[RunConfigMeasurement]] |
| 47 | + ) -> None: |
| 48 | + self._last_measurement = measurements[-1] |
| 49 | + |
| 50 | + def get_configs(self) -> Generator[RunConfig, None, None]: |
| 51 | + """ |
| 52 | + A generator which creates RunConfigs based on sweeping |
| 53 | + concurrency over the top-N models |
| 54 | + """ |
| 55 | + for model_name in self._result_manager.get_model_names(): |
| 56 | + top_results = self._result_manager.top_n_results( |
| 57 | + model_name=model_name, |
| 58 | + n=self._config.num_configs_per_model, |
| 59 | + include_default=True, |
| 60 | + ) |
| 61 | + |
| 62 | + for result in top_results: |
| 63 | + run_config = deepcopy(result.run_config()) |
| 64 | + parameter_search = ParameterSearch(self._config) |
| 65 | + for concurrency in parameter_search.search_parameters(): |
| 66 | + run_config = self._create_run_config(run_config, concurrency) |
| 67 | + yield run_config |
| 68 | + parameter_search.add_run_config_measurement(self._last_measurement) |
| 69 | + |
| 70 | + def _create_run_config(self, run_config: RunConfig, concurrency: int) -> RunConfig: |
| 71 | + for model_run_config in run_config.model_run_configs(): |
| 72 | + perf_config = model_run_config.perf_config() |
| 73 | + perf_config.update_config({"concurrency-range": concurrency}) |
| 74 | + |
| 75 | + return run_config |
0 commit comments