Refactor: Align pd_buffer_size to chunk size in PD backend#2694
Refactor: Align pd_buffer_size to chunk size in PD backend#2694deng451e merged 28 commits intoLMCache:devfrom
Conversation
- Add buffer size alignment logic to prevent assertion error - Calculate aligned_buffer_size as (origin_size // chunk_size) * chunk_size - Add informative logging when buffer size is adjusted - Release excess buffer memory that can't be aligned - Follows the same pattern as local_cpu_backend.py Signed-off-by: Tony Lin <tony.lin@intel.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the PD backend to automatically align the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the pd_backend to align the pd_buffer_size with the chunk size, which is a good improvement to prevent potential assertion errors. The added logging for buffer size adjustments is also helpful for debugging. The new unit tests effectively cover the alignment logic.
My review includes a few suggestions for improvement:
- A critical edge case where the buffer size could be aligned to zero is not handled. I've suggested adding a check to raise an error in this scenario.
- A point on code style regarding the placement of an import.
- Suggestions to improve the new tests by refactoring duplicated code and adding a test case for the edge case mentioned above.
Signed-off-by: Tony Lin <tony.lin@intel.com>
DongDongJu
left a comment
There was a problem hiding this comment.
generally LGTM,
Please change the comment and add at least one test that asserts the exact aligned size, not just divisibility.
Other test looks doesn't needed.
Signed-off-by: Tony Lin <tony.lin@intel.com>
Signed-off-by: Tony Lin <tony.lin@intel.com>
hi @DongDongJu thanks for your comments. I accepted all of them. submitted two new commits to address the log msg issue and UT issue per your advice, which is very helpful! thanks. |
Signed-off-by: Tony Lin <tony.lin@intel.com>
Hi @DongDongJu , I removed the UT because it was inexplicably causing the CI pipeline to fail(seems the underneath PD backend is not closed gracefully). Given how straightforward the underlying function is, we can safely remove the test to restore CI stability without compromising code quality. Let me know if you have any concern. thanks. |
UT issue has been resolved and restored. @DongDongJu |
hi @DongDongJu the answer is yes. the problem is still there regardless the setting of save_unfull_chunk. current logic is to mandate buffer size to be "chunk_size x kv_size" aligned, which is technically unnecessary. each time, when i change a model or chunk_size, i need to adjust the buffer size value and it's annoying as I have to do calculations before the change. from ease of use point, we can do alignment and only allocate the aligned memory part with making sure not exceeding the buffer size limites, as this PR does. |
Yes I just realized that we are require save_unfull_chunk=true for pd backend. Let me check few more code after coming back to desk. |
sure. thanks. |
|
hi @DongDongJu do you have concerns anymore? thanks. |
DongDongJu
left a comment
There was a problem hiding this comment.
Thansk for the work!.
Please address the comments.
Signed-off-by: Tony Lin <tony.lin@intel.com>
Signed-off-by: Tony Lin <tony.lin@intel.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
hi @DongDongJu all addressed. please check. thx. |

Prior to this change, reusing a single lmcache.yaml across multiple models was impossible due to strict chunk_size alignment requirements for pd_buffer_size, which vary by model KV size. Configuring this manually was difficult and often required a dry run.
With this update, pd_buffer_size acts as a maximum allocation ceiling. The system now automatically aligns the size and frees the remaining unaligned memory, ensuring robust auto-alignment regardless of user configuration.
Note
Medium Risk
Changes PD disaggregation memory allocation semantics by rounding
pd_buffer_sizedown to a model-derived chunk multiple and erroring when too small, which could affect allocated capacity and runtime behavior for existing deployments.Overview
PD backend now treats
pd_buffer_sizeas a ceiling and auto-aligns it to the KV chunk size. During allocator initialization, the requested buffer is rounded down to the nearest multiple of the per-chunk byte size, logs when truncation occurs, and raises a clear error if the configured size is smaller than a single chunk.Docs for disaggregated prefill are updated to reflect the new upper-bound/aligned meaning of
pd_buffer_size, and a unit test is added to verify alignment behavior and allocator calls.Reviewed by Cursor Bugbot for commit a1ffc5a. Bugbot is set up for automated code reviews on this repo. Configure here.