vllm icon indicating copy to clipboard operation
vllm copied to clipboard

Neuron cache blocks must be 1 more than max num seqs

Open ajayvohra2005 opened this issue 1 year ago • 5 comments

FILL IN THE PR DESCRIPTION HERE

FIX #8007 (link existing issues this PR will resolve)

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

ajayvohra2005 avatar Aug 29 '24 18:08 ajayvohra2005

👋 Hi! Thank you for contributing to the vLLM project. Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which consists a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of default ones by unblocking the steps in your fast-check build on Buildkite UI.

Once the PR is approved and ready to go, please make sure to run full CI as it is required to merge (or just use auto-merge).

To run full CI, you can do one of these:

  • Comment /ready on the PR
  • Add ready label to the PR
  • Enable auto-merge.

🚀

github-actions[bot] avatar Aug 29 '24 18:08 github-actions[bot]

This PR is specific to Block Manager V2.

ajayvohra2005 avatar Aug 29 '24 18:08 ajayvohra2005

This seems reasonable but an explanation for why it works would be appreciated. Requesting @liangfu for review

mgoin avatar Aug 29 '24 19:08 mgoin

I think the motivation for the proposed change is that in scheduler, 1/ we pad with 0 in block_tables, and 2/ recompute when we run out of KV cache blocks.

1/ block 0 would never be used, since there can be garbage tokens.

2/ For neuron backend support at the moment, we set block_size equal to max_model_len in order to occupy the whole block for the entire sequence. If we compile with batch size 4 and send 4 concurrent requests, we will face frequent recompute.

This would not be a problem when we introduce paged attention to neuron backend.

My questions is: How does the proposed change fix the issue?

liangfu avatar Aug 30 '24 16:08 liangfu

I think the motivation for the proposed change is that in scheduler, 1/ we pad with 0 in block_tables, and 2/ recompute when we run out of KV cache blocks.

1/ block 0 would never be used, since there can be garbage tokens.

2/ For neuron backend support at the moment, we set block_size equal to max_model_len in order to occupy the whole block for the entire sequence. If we compile with batch size 4 and send 4 concurrent requests, we will face frequent recompute.

This would not be a problem when we introduce paged attention to neuron backend.

My questions is: How does the proposed change fix the issue?

Please take a look at this code from block_manager_v2.py

def can_append_slots(self, seq_group: SequenceGroup,
                         num_lookahead_slots: int) -> bool:
        """Determine if there is enough space in the GPU KV cache to continue
        generation of the specified sequence group.

        We use a worst-case heuristic: assume each touched block will require a
        new allocation (either via CoW or new block). We can append slots if the
        number of touched blocks is less than the number of free blocks.

        "Lookahead slots" are slots that are allocated in addition to the slots
        for known tokens. The contents of the lookahead slots are not defined.
        This is used by speculative decoding when speculating future tokens.
        """

        num_touched_blocks = 0
        for seq in seq_group.get_seqs(status=SequenceStatus.RUNNING):
            block_table = self.block_tables[seq.seq_id]

            num_touched_blocks += (
                block_table.get_num_blocks_touched_by_append_slots(
                    token_ids=block_table.get_unseen_token_ids(
                        seq.get_token_ids()),
                    num_lookahead_slots=num_lookahead_slots,
                ))

        num_free_gpu_blocks = self.block_allocator.get_num_free_blocks(
            Device.GPU)
        return num_touched_blocks <= num_free_gpu_blocks

This code returns true when number of touched blocks is less than, or equal-to the number of free blocks, not less-than, as the comment says. When number of touched blocks is equal-to the number of free blocks, the method above returns true, but slot in fact can not be appended, and leads to pre-emption. My change ensures that whenever this returns true, there is no preemption. I am not completely sure of my explanation, but my testing showed the recompute stops when with batch size 4, 4 or more concurrent requests are sent. I can not rule out that the real fix should be changing above code: return num_touched_blocks < num_free_gpu_blocks to match the comment in the code. Please let me know if my explanation is incorrect.

ajayvohra2005 avatar Sep 12 '24 07:09 ajayvohra2005