Measures the time it takes to run Pythonic inference code. Statement given should be the actual model inference like forward() in torch. Args: stmt (Union[Callable, str]): Callable or string for generating numbers. timing_profile (TimingProfile): The timing profile sett
(
stmt: Union[Callable, str], timing_profile: TimingProfile
)
| 149 | |
| 150 | |
| 151 | def measure_python_inference_code( |
| 152 | stmt: Union[Callable, str], timing_profile: TimingProfile |
| 153 | ) -> None: |
| 154 | """ |
| 155 | Measures the time it takes to run Pythonic inference code. |
| 156 | Statement given should be the actual model inference like forward() in torch. |
| 157 | |
| 158 | Args: |
| 159 | stmt (Union[Callable, str]): Callable or string for generating numbers. |
| 160 | timing_profile (TimingProfile): The timing profile settings with the following fields. |
| 161 | warmup (int): Number of iterations to run as warm-up before actual measurement cycles. |
| 162 | number (int): Number of times to call function per iteration. |
| 163 | iterations (int): Number of measurement cycles. |
| 164 | duration (float): Minimal duration for measurement cycles. |
| 165 | percentile (int or list of ints): key percentile number(s) for measurement. |
| 166 | """ |
| 167 | |
| 168 | def simple_percentile(data, p): |
| 169 | """ |
| 170 | Temporary replacement for numpy.percentile() because TRT CI/CD pipeline requires additional packages to be added at boot up in this general_utils.py file. |
| 171 | """ |
| 172 | assert p >= 0 and p <= 100, "Percentile must be between 1 and 99" |
| 173 | |
| 174 | rank = len(data) * p / 100 |
| 175 | if rank.is_integer(): |
| 176 | return sorted(data)[int(rank)] |
| 177 | else: |
| 178 | return sorted(data)[int(math.ceil(rank)) - 1] |
| 179 | |
| 180 | warmup = timing_profile.warmup |
| 181 | number = timing_profile.number |
| 182 | iterations = timing_profile.iterations |
| 183 | duration = timing_profile.duration |
| 184 | percentile = timing_profile.percentile |
| 185 | |
| 186 | G_LOGGER.debug( |
| 187 | "Measuring inference call with warmup: {} and number: {} and iterations {} and duration {} secs".format( |
| 188 | warmup, number, iterations, duration |
| 189 | ) |
| 190 | ) |
| 191 | # Warmup |
| 192 | warmup_mintime = timeit.repeat(stmt, number=number, repeat=warmup) |
| 193 | G_LOGGER.debug("Warmup times: {}".format(warmup_mintime)) |
| 194 | |
| 195 | # Actual measurement cycles |
| 196 | results = [] |
| 197 | start_time = datetime.now() |
| 198 | iter_idx = 0 |
| 199 | while iter_idx < iterations or (datetime.now() - start_time).total_seconds() < duration: |
| 200 | iter_idx += 1 |
| 201 | results.append(timeit.timeit(stmt, number=number)) |
| 202 | |
| 203 | if isinstance(percentile, int): |
| 204 | return simple_percentile(results, percentile) / number |
| 205 | else: |
| 206 | return [simple_percentile(results, p) / number for p in percentile] |
| 207 | |
| 208 | class NNFolderWorkspace: |
no test coverage detected