|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions |
| 7 | +# are met: |
| 8 | +# * Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived |
| 15 | +# from this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 18 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 20 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 25 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +from typing import Dict, List, Optional, Union |
| 30 | + |
| 31 | +import genai_perf.logging as logging |
| 32 | +from genai_perf.goodput_calculator.goodput_calculator import GoodputCalculator |
| 33 | +from genai_perf.metrics.llm_metrics import LLMMetrics |
| 34 | +from genai_perf.metrics.metrics import Metrics |
| 35 | + |
| 36 | +logger = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +class LLMGoodputCalculator(GoodputCalculator): |
| 40 | + """ |
| 41 | + A subclass to calculate goodput for LLMs according to |
| 42 | + LLM-related goodput constraints. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + goodput_constraints: Dict[str, float], |
| 48 | + metric: Union[LLMMetrics, Metrics], |
| 49 | + benchmark_duration: float, |
| 50 | + ) -> None: |
| 51 | + super().__init__(goodput_constraints, metric, benchmark_duration) |
| 52 | + |
| 53 | + self._set_valid_metric_names() |
| 54 | + |
| 55 | + self._has_time_target = False |
| 56 | + self._has_throughput_target = False |
| 57 | + |
| 58 | + self._add_slo_mapping() |
| 59 | + |
| 60 | + def _set_valid_metric_names(self) -> None: |
| 61 | + self._valid_time_related_names = [ |
| 62 | + item.name for item in self._metric.request_time_metrics |
| 63 | + ] |
| 64 | + self._valid_throughput_related_names = [ |
| 65 | + item.name for item in self._metric.request_throughput_metrics |
| 66 | + ] |
| 67 | + self._valid_metric_names = ( |
| 68 | + self._valid_time_related_names + self._valid_throughput_related_names |
| 69 | + ) |
| 70 | + |
| 71 | + def _add_slo_mapping(self) -> None: |
| 72 | + self._slo_names["time_to_first_token"] = "time_to_first_tokens" |
| 73 | + self._slo_names["inter_token_latency"] = "inter_token_latencies" |
| 74 | + self._slo_names["output_token_throughput_per_request"] = ( |
| 75 | + "output_token_throughputs_per_request" |
| 76 | + ) |
| 77 | + |
| 78 | + def _set_valid_slos(self) -> None: |
| 79 | + invalid_slos = [] |
| 80 | + self._valid_time_related_slos = {} |
| 81 | + self._valid_throughput_related_slos = {} |
| 82 | + for slo_name, slo_value in self._goodput_constraints.items(): |
| 83 | + if slo_name in self._valid_time_related_names: |
| 84 | + self._valid_time_related_slos[slo_name] = ( |
| 85 | + slo_value * self.MS_TO_NS_CONVERSION |
| 86 | + ) |
| 87 | + self._has_time_target = True |
| 88 | + elif slo_name in self._valid_throughput_related_names: |
| 89 | + self._valid_throughput_related_slos[slo_name] = slo_value |
| 90 | + self._has_throughput_target = True |
| 91 | + else: |
| 92 | + invalid_slos.append(slo_name) |
| 93 | + |
| 94 | + if invalid_slos: |
| 95 | + valid_slos_list = ", ".join(self._valid_metric_names) |
| 96 | + logger.info( |
| 97 | + f"Invalid Service Level Objectives found: {', '.join(invalid_slos)}. " |
| 98 | + f"Valid Service Level Objectives are: {valid_slos_list}." |
| 99 | + ) |
| 100 | + self._goodput_val = self.INVALID_GOODPUT |
| 101 | + |
| 102 | + def _combine_requests_metric_values(self) -> None: |
| 103 | + if self.goodput == self.INVALID_GOODPUT: |
| 104 | + return |
| 105 | + |
| 106 | + if self._has_time_target: |
| 107 | + time_names = [ |
| 108 | + self.get_slo_name(key) for key in self._valid_time_related_slos |
| 109 | + ] |
| 110 | + requests_time_metric_values = [ |
| 111 | + self._metric.data[name] for name in time_names |
| 112 | + ] |
| 113 | + |
| 114 | + self._combined_requests_time_metric_values = list( |
| 115 | + zip(*requests_time_metric_values) |
| 116 | + ) |
| 117 | + |
| 118 | + if self._has_throughput_target: |
| 119 | + throughput_names = [ |
| 120 | + self.get_slo_name(key) for key in self._valid_throughput_related_slos |
| 121 | + ] |
| 122 | + requests_throughput_metric_values = [ |
| 123 | + self._metric.data[name] for name in throughput_names |
| 124 | + ] |
| 125 | + |
| 126 | + self._combined_requests_throughput_metric_values = list( |
| 127 | + zip(*requests_throughput_metric_values) |
| 128 | + ) |
| 129 | + |
| 130 | + def _count_good_reqs(self) -> Optional[int]: |
| 131 | + if self.goodput == self.INVALID_GOODPUT: |
| 132 | + return None |
| 133 | + target_time_metric_values = [] |
| 134 | + target_throughput_metric_values = [] |
| 135 | + if self._has_time_target: |
| 136 | + num_of_requests = len(self._combined_requests_time_metric_values) |
| 137 | + target_time_metric_values = list(self._valid_time_related_slos.values()) |
| 138 | + if self._has_throughput_target: |
| 139 | + num_of_requests = len(self._combined_requests_throughput_metric_values) |
| 140 | + target_throughput_metric_values = list( |
| 141 | + self._valid_throughput_related_slos.values() |
| 142 | + ) |
| 143 | + |
| 144 | + good_req_count = 0 |
| 145 | + for idx in range(num_of_requests): |
| 146 | + is_good_request = True |
| 147 | + request_time_metric_values: List[float] = [] |
| 148 | + request_throughput_metric_values: List[float] = [] |
| 149 | + if self._has_time_target: |
| 150 | + request_time_metric_values = list( |
| 151 | + self._combined_requests_time_metric_values[idx] |
| 152 | + ) |
| 153 | + if self._has_throughput_target: |
| 154 | + request_throughput_metric_values = list( |
| 155 | + self._combined_requests_throughput_metric_values[idx] |
| 156 | + ) |
| 157 | + |
| 158 | + for val, slo in zip(request_time_metric_values, target_time_metric_values): |
| 159 | + if val > slo: |
| 160 | + is_good_request = False |
| 161 | + break |
| 162 | + if is_good_request: |
| 163 | + for val, slo in zip( |
| 164 | + request_throughput_metric_values, target_throughput_metric_values |
| 165 | + ): |
| 166 | + if val < slo: |
| 167 | + is_good_request = False |
| 168 | + break |
| 169 | + |
| 170 | + if is_good_request: |
| 171 | + good_req_count += 1 |
| 172 | + |
| 173 | + return good_req_count |
| 174 | + |
| 175 | + def _compute_goodput(self, good_count) -> None: |
| 176 | + if self.goodput == self.INVALID_GOODPUT: |
| 177 | + return |
| 178 | + else: |
| 179 | + self._goodput_val = [good_count / self._benchmark_duration] |
0 commit comments