Skip to content

Commit

Permalink
[Serve] Let batch handler yield unhashable objects (ray-project#36353)
Browse files Browse the repository at this point in the history
This change lets users yield unhashable objects in their `@serve.batch`-decorated generators.
  • Loading branch information
shrekris-anyscale authored Jun 13, 2023
1 parent cd0d3cd commit f8fcfb0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions python/ray/serve/batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ async def _consume_func_generator(
if future is FINISHED_TOKEN:
# This caller has already terminated.
next_futures.append(FINISHED_TOKEN)
elif result in {StopIteration, StopAsyncIteration}:
# User's code returned SENTINEL.VALUE. No values left
elif result in [StopIteration, StopAsyncIteration]:
# User's code returned sentinel. No values left
# for caller. Terminate iteration for caller.
future.set_exception(StopAsyncIteration)
next_futures.append(FINISHED_TOKEN)
Expand Down
10 changes: 8 additions & 2 deletions python/ray/serve/tests/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,18 @@ class Textgen:
@serve.batch(max_batch_size=4, batch_wait_timeout_s=1000)
async def batch_handler(self, prompts: List[str]):
for _ in range(NUM_YIELDS):
prompt_responses = prompts
# Check that the batch handler can yield unhashable types
prompt_responses = [{"value": prompt} for prompt in prompts]
yield prompt_responses

async def value_extractor(self, prompt_responses):
async for prompt_response in prompt_responses:
yield prompt_response["value"]

async def __call__(self, request):
prompt = request.query_params["prompt"]
return StreamingResponse(self.batch_handler(prompt))
response_values = self.value_extractor(self.batch_handler(prompt))
return StreamingResponse(response_values)

serve.run(Textgen.bind())

Expand Down

0 comments on commit f8fcfb0

Please sign in to comment.