|
| 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 Dict, Generator, List, Optional |
| 20 | + |
| 21 | +from model_analyzer.config.generate.model_profile_spec import ModelProfileSpec |
| 22 | +from model_analyzer.config.generate.model_variant_name_manager import ( |
| 23 | + ModelVariantNameManager, |
| 24 | +) |
| 25 | +from model_analyzer.config.generate.optuna_run_config_generator import ( |
| 26 | + OptunaRunConfigGenerator, |
| 27 | +) |
| 28 | +from model_analyzer.config.generate.search_parameters import SearchParameters |
| 29 | +from model_analyzer.config.input.config_command_profile import ConfigCommandProfile |
| 30 | +from model_analyzer.config.run.run_config import RunConfig |
| 31 | +from model_analyzer.constants import LOGGER_NAME |
| 32 | +from model_analyzer.result.parameter_search import ParameterSearch |
| 33 | +from model_analyzer.result.result_manager import ResultManager |
| 34 | +from model_analyzer.result.run_config_measurement import RunConfigMeasurement |
| 35 | + |
| 36 | +from .config_generator_interface import ConfigGeneratorInterface |
| 37 | + |
| 38 | +logger = logging.getLogger(LOGGER_NAME) |
| 39 | + |
| 40 | + |
| 41 | +class OptunaPlusConcurrencySweepRunConfigGenerator(ConfigGeneratorInterface): |
| 42 | + """ |
| 43 | + First run OptunaConfigGenerator for an Optuna search, then use |
| 44 | + ParameterSearch for a concurrency sweep + binary search of the default |
| 45 | + and Top N results |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + config: ConfigCommandProfile, |
| 51 | + gpu_count: int, |
| 52 | + models: List[ModelProfileSpec], |
| 53 | + result_manager: ResultManager, |
| 54 | + model_variant_name_manager: ModelVariantNameManager, |
| 55 | + search_parameters: Dict[str, SearchParameters], |
| 56 | + ): |
| 57 | + """ |
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + config: ConfigCommandProfile |
| 61 | + Profile configuration information |
| 62 | + gpu_count: Number of gpus in the system |
| 63 | + models: List of ModelProfileSpec |
| 64 | + List of models to profile |
| 65 | + result_manager: ResultManager |
| 66 | + The object that handles storing and sorting the results from the perf analyzer |
| 67 | + model_variant_name_manager: ModelVariantNameManager |
| 68 | + Maps model variants to config names |
| 69 | + search_parameters: SearchParameters |
| 70 | + The object that handles the users configuration search parameters |
| 71 | + """ |
| 72 | + self._config = config |
| 73 | + self._gpu_count = gpu_count |
| 74 | + self._models = models |
| 75 | + self._result_manager = result_manager |
| 76 | + self._model_variant_name_manager = model_variant_name_manager |
| 77 | + self._search_parameters = search_parameters |
| 78 | + |
| 79 | + def set_last_results( |
| 80 | + self, measurements: List[Optional[RunConfigMeasurement]] |
| 81 | + ) -> None: |
| 82 | + self._last_measurement = measurements[-1] |
| 83 | + self._rcg.set_last_results(measurements) |
| 84 | + |
| 85 | + def get_configs(self) -> Generator[RunConfig, None, None]: |
| 86 | + """ |
| 87 | + Returns |
| 88 | + ------- |
| 89 | + RunConfig |
| 90 | + The next RunConfig generated by this class |
| 91 | + """ |
| 92 | + |
| 93 | + logger.info("") |
| 94 | + logger.info("Starting Optuna mode search to find optimal configs") |
| 95 | + logger.info("") |
| 96 | + yield from self._execute_optuna_search() |
| 97 | + logger.info("") |
| 98 | + if self._config.concurrency_sweep_disable: |
| 99 | + logger.info("Done with Optuna mode search.") |
| 100 | + else: |
| 101 | + logger.info( |
| 102 | + "Done with Optuna mode search. Gathering concurrency sweep measurements for reports" |
| 103 | + ) |
| 104 | + logger.info("") |
| 105 | + yield from self._sweep_concurrency_over_top_results() |
| 106 | + logger.info("") |
| 107 | + logger.info("Done gathering concurrency sweep measurements for reports") |
| 108 | + logger.info("") |
| 109 | + |
| 110 | + def _execute_optuna_search(self) -> Generator[RunConfig, None, None]: |
| 111 | + self._rcg: ConfigGeneratorInterface = self._create_optuna_run_config_generator() |
| 112 | + |
| 113 | + yield from self._rcg.get_configs() |
| 114 | + |
| 115 | + def _create_optuna_run_config_generator(self) -> OptunaRunConfigGenerator: |
| 116 | + return OptunaRunConfigGenerator( |
| 117 | + config=self._config, |
| 118 | + gpu_count=self._gpu_count, |
| 119 | + models=self._models, |
| 120 | + model_variant_name_manager=self._model_variant_name_manager, |
| 121 | + search_parameters=self._search_parameters, |
| 122 | + ) |
| 123 | + |
| 124 | + def _sweep_concurrency_over_top_results(self) -> Generator[RunConfig, None, None]: |
| 125 | + for model_name in self._result_manager.get_model_names(): |
| 126 | + top_results = self._result_manager.top_n_results( |
| 127 | + model_name=model_name, |
| 128 | + n=self._config.num_configs_per_model, |
| 129 | + include_default=True, |
| 130 | + ) |
| 131 | + |
| 132 | + for result in top_results: |
| 133 | + run_config = deepcopy(result.run_config()) |
| 134 | + parameter_search = ParameterSearch(self._config) |
| 135 | + for concurrency in parameter_search.search_parameters(): |
| 136 | + run_config = self._set_concurrency(run_config, concurrency) |
| 137 | + yield run_config |
| 138 | + parameter_search.add_run_config_measurement(self._last_measurement) |
| 139 | + |
| 140 | + def _set_concurrency(self, run_config: RunConfig, concurrency: int) -> RunConfig: |
| 141 | + for model_run_config in run_config.model_run_configs(): |
| 142 | + perf_config = model_run_config.perf_config() |
| 143 | + perf_config.update_config({"concurrency-range": concurrency}) |
| 144 | + |
| 145 | + return run_config |
0 commit comments