[Serve] add support for custom batch size function#59059
Conversation
Signed-off-by: abrar <abrar@anyscale.com>
| # 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 <abrar@anyscale.com>
| 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 <abrar@anyscale.com>
| 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.
fixes #58956
len(batch)baselinescript
load test
results