[Perf] Avoid O(n) re-allocation per token in Req.append_host - #143
Open
joshfuery wants to merge 3 commits into
Open
[Perf] Avoid O(n) re-allocation per token in Req.append_host#143joshfuery wants to merge 3 commits into
joshfuery wants to merge 3 commits into
Conversation
Refactor append_host to use a pre-allocated buffer for efficiency.
Corrected a typo in the comment for clarity.
Verifies the behavior of the append_host method in the Req class, ensuring it matches expected behavior in various scenarios, including dtype preservation and overflow handling.
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.
Motivation
Currently,
Req.append_hostusestorch.catto append tokens. This requires a fresh tensor allocation and O(n) memory copy on every single decode step. On the scheduler thread, this CPU overhead scales quadratically per request, eventually causing the GPU to stall during overlap scheduling at larger context lengths.Changes
max_device_lenbuffer on the first append.input_ids = _host_buf[:new_len]to preserve views/references.Microbenchmark
(64 requests, CPU append cost per step)
Testing
Added
test_req_append.pyto verify:torch.catbehavior.Note: All CPU unit tests pass locally, but I do not have a local GPU set up to run the full end-to-end scheduler tests.