Repository navigation
[Serve] add support for custom batch size function - #59059
Conversation
Signed-off-by: abrar <[email protected]>
| # Put deferred item back in queue for next batch | ||
| if deferred_item is not None: | ||
| self.queue.put_nowait(deferred_item) |
There was a problem hiding this comment.
this would put the request it in the back of the queue correct?
Signed-off-by: abrar <[email protected]>
| batch.append(self.queue.get_nowait()) | ||
|
|
||
| # Put deferred item back in queue for next batch | ||
| if deferred_item is not None: |
There was a problem hiding this comment.
nit: we can put this block after
while not self.queue.empty():
next_item = self.queue.get_nowait()
# Temporarily add to check size
batch.append(next_item)
new_size = self._compute_batch_size(batch)
if new_size > max_batch_size:
# Would exceed limit, remove it and save for later
batch.pop()
deferred_item = next_item
break
# Size is OK, keep it in the batch (already added above)
Signed-off-by: abrar <[email protected]>
| args, kwargs = recover_args(request.flattened_args) | ||
| # The batch function expects a single positional argument (the item) | ||
| # after 'self' has been extracted (if it was a method) | ||
| items.append(args[0]) |
There was a problem hiding this comment.
Bug: Potential IndexError when using keyword arguments with batch_size_fn
The _compute_batch_size method assumes that requests are always passed as positional arguments, accessing args[0] without checking if args is empty. If a user defines a batch method with keyword-only parameters (e.g., async def handle_batch(self, *, request)) and calls it with keyword arguments, recover_args will return an empty args list, causing an IndexError: list index out of range. This would result in a confusing error message rather than a clear explanation that batch_size_fn requires the batched argument to be passed positionally.
## Description When a single-input batched handler uses `batch_size_fn`, passing its input by keyword leaves no positional argument for `_compute_batch_size` to read. The resulting `IndexError` escapes batch assembly. A local Serve deployment reproduces positional success, keyword timeout, then another positional timeout because the batching task has stopped. Read the first payload value from the existing flattened argument representation, after `self` has already been removed. This preserves successful positional calls and supports a single explicitly supplied input passed by keyword, including keyword-only parameters. Existing multi-input positional calls remain supported. Multi-input keyword calls and variadic keyword (`**kwargs`) handlers are rejected before queueing because their flattened keyword order is not stable across a batch, and omitting a defaulted input is rejected because there is no explicit value to size. These errors affect only the caller and do not stop the batching worker. General callback-error isolation remains outside this change. The tests exercise functions and bound methods, coroutine and streaming handlers, mixed positional/keyword arrivals in both orders, keyword-only signatures, oversized keyword input recovery, omitted defaults, second-keyword-first ordering, variadic keyword signatures, positional multi-input compatibility, and worker survival after invalid requests. The batching guide describes the supported call forms. ## Related work and attribution This addresses the finding already raised in [the review of #59059](#59059 (comment)); it is not a new discovery. Searches for custom sizing and keyword/kwargs fixes found no matching open PR. #66141 fixes callback-error isolation and explicitly excludes keyword compatibility. #64595 restructures per-model queues but leaves this payload extraction unchanged; it touches the same file and may require rebasing. ## Validation macOS arm64, Python 3.12.14, official Ray `3.0.0.dev0` wheel at `39882c660a6d7b579e39d724ce85d9074d6180fb`, with the branch's current Python Serve sources linked into the environment. Because the branch includes newer master changes, the source and wheel revisions are not exact; the focused suite required a runner-local shim for a newer test-fixture helper. ```bash .venv/bin/python - <<'PYTEST' import ray, pytest raise SystemExit(pytest.main([ '--import-mode=importlib', 'python/ray/serve/tests/unit/test_batching.py', '--tb=short', '-q', ])) PYTEST ``` - 15 keyword regressions from the original change fail on the original implementation; seven review-fix cases fail before their respective fixes. - Full batching unit file: **87 passed**, including 26 cases added by this PR. - Real CPU-only local Serve deployment: positional and keyword requests succeed; invalid omitted-default, multi-input-keyword, and variadic-keyword calls return bounded `TypeError`s; a subsequent valid request succeeds and all batching tasks remain alive. - All hooks applicable to the changed files pass; `git diff --check` passes. Vale is excluded for this documentation path and its environment separately failed to compile on this Mac because the C++ `cassert` header was unavailable. - The test runner prints pending-task teardown diagnostics that were already present in the earlier 61-test baseline; the run exits successfully. The local demo logs include the existing batch-size/ongoing-request advisory and Ray shutdown messages. - Independent Astra Low reviews of each exact review-fix diff found no actionable issues in argument mapping, exception propagation, unresolved futures, cancellation, streaming, or worker survival. No GPU performance, full-Ray suite, docs build, or upstream CI result is claimed. --------- Signed-off-by: Jayesh Suyal <[email protected]> Signed-off-by: YashwanthRanjanSingaravel <[email protected]> Co-authored-by: YashwanthRanjanSingaravel <[email protected]>
fixes #58956
len(batch)baselinescript
load test
results