Skip to content

Commit

Permalink
avoid timeout errors with high concurrency in api_model (#2307)
Browse files Browse the repository at this point in the history
* avoid timeout errors with high concurrency in api_model

* style

* add timeout

* add docs

---------

Co-authored-by: Baber <[email protected]>
  • Loading branch information
dtrawins and baberabb authored Dec 3, 2024
1 parent f49b037 commit 9632b34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/API_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ When initializing a `TemplateAPI` instance or a subclass, you can provide severa
- Useful for APIs that support parallel processing.
- Default is 1 (sequential processing).

- `timeout` (int, optional):
- Timeout for API requests in seconds.
- Default is 30.

- `tokenized_requests` (bool):
- Determines whether the input is pre-tokenized. Defaults to `True`.
- Requests can be sent in either tokenized form (`list[list[int]]`) or as text (`list[str]`, or `str` for batch_size=1).
Expand Down
9 changes: 7 additions & 2 deletions lm_eval/models/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

try:
import requests
from aiohttp import ClientSession, TCPConnector
from aiohttp import ClientSession, ClientTimeout, TCPConnector
from tenacity import RetryError, retry, stop_after_attempt, wait_exponential
from tqdm import tqdm
from tqdm.asyncio import tqdm_asyncio
Expand Down Expand Up @@ -81,6 +81,8 @@ def __init__(
use_fast_tokenizer: bool = True,
verify_certificate: bool = True,
eos_string: str = None,
# timeout in seconds
timeout: int = 300,
**kwargs,
) -> None:
super().__init__()
Expand Down Expand Up @@ -126,6 +128,7 @@ def __init__(
self.max_retries = int(max_retries)
self.verify_certificate = verify_certificate
self._eos_string = eos_string
self.timeout = int(timeout)

eval_logger.info(f"Using tokenizer {self.tokenizer_backend}")
if self.tokenizer_backend is None:
Expand Down Expand Up @@ -466,7 +469,9 @@ async def get_batched_requests(
) -> Union[List[List[str]], List[List[Tuple[float, bool]]]]:
ctxlens = ctxlens if ctxlens else [None] * len(requests)
conn = TCPConnector(limit=self._concurrent)
async with ClientSession(connector=conn) as session:
async with ClientSession(
connector=conn, timeout=ClientTimeout(total=self.timeout)
) as session:
retry_: Callable[..., Awaitable[Any]] = retry(
stop=stop_after_attempt(self.max_retries),
wait=wait_exponential(multiplier=0.5, min=1, max=10),
Expand Down

0 comments on commit 9632b34

Please sign in to comment.