main
py 39 lines 831 Bytes
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 from collections.abc import Callable, Iterable, Sequence
4 from typing import Any, TypeVar, overload
5
6 from tqdm.auto import tqdm
7
8 _T = TypeVar("_T", bound=Iterable)
9
10
11 @overload
12 def maybe_tqdm(
13 it: Sequence[_T],
14 *,
15 use_tqdm: bool | Callable[..., tqdm],
16 **tqdm_kwargs: Any,
17 ) -> Sequence[_T]: ...
18
19
20 @overload
21 def maybe_tqdm(
22 it: Iterable[_T],
23 *,
24 use_tqdm: bool | Callable[..., tqdm],
25 **tqdm_kwargs: Any,
26 ) -> Iterable[_T]: ...
27
28
29 def maybe_tqdm(
30 it: Iterable[_T],
31 *,
32 use_tqdm: bool | Callable[..., tqdm],
33 **tqdm_kwargs: Any,
34 ) -> Iterable[_T]:
35 if not use_tqdm:
36 return it
37
38 tqdm_func = use_tqdm if callable(use_tqdm) else tqdm
39 return tqdm_func(it, **tqdm_kwargs)