Support context argument in batch and unbatch methods#711
Open
discobot wants to merge 2 commits into
Open
Conversation
The batched loops called lit_api.batch and lit_api.unbatch directly, so user implementations declaring a context parameter failed with a TypeError. Route both calls through _inject_context, the same way decode_request, predict and encode_response are already handled, in both the regular and streaming batched loops. Document the optional context argument in the LitAPI.batch/unbatch docstrings and add regression tests covering both loops. Fixes Lightning-AI#617
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #617.
LitAPI.batchandLitAPI.unbatchare the only hooks that never receive the per-requestcontext. It is worse than a missing option: declaringbatch(self, inputs, context)withmax_batch_size > 1currently fails every batched request withTypeError: batch() missing 1 required positional argument: 'context', surfaced as HTTP 500.Root cause: the batched loops (regular and streaming) call
lit_api.batch/lit_api.unbatchdirectly instead of through the_inject_contexthelper thatdecode_request/predict/encode_responsealready go through — even though thecontextslist is in scope at all four call sites. This PR wraps those call sites with_inject_context(contexts, ...)and documents the optionalcontextargument in thebatch/unbatchdocstrings._inject_contextonly passescontextwhen the method signature declares it, so existing implementations are unaffected.Added
test_inject_context_in_batch_and_unbatchcovering both loops:batchstores each request's input in its per-request context andunbatchreads it back, so the test fails with theTypeErrorabove without the fix.tests/unit/test_lit_server.py,tests/unit/test_batch.pyandtests/unit/test_loops.pypass locally.Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in GitHub issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃