Repository navigation
feat(multimodal): optimize EPD encode routing #1853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d6d7940
feat(multimodal): optimize EPD encode routing
chenht2022 18079f3
refactor(multimodal): make RDMA pixel transport a first-class Transpo…
slin1237 4921987
fix(multimodal): satisfy clippy and ruff after rebase
slin1237 04aeff1
ci: re-trigger CI after infra fix
slin1237 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //! Recycling pools for the large per-image vision buffers. | ||
| //! | ||
| //! The preprocess pipeline allocates tens of MB per image (the [C, H, W] f32 | ||
| //! tensor and the batched patch buffer are each large). | ||
| //! Freshly-allocated buffers of this size bypass the allocator's reuse paths | ||
| //! (glibc caps non-main-arena chunks at 64 MB and mmaps anything larger or | ||
| //! colder), so every image pays tens of thousands of minor page faults; the | ||
| //! fault path serializes process-wide and caps the data plane's effective | ||
| //! parallelism. Recycling keeps the pages mapped and hot. | ||
| //! | ||
| //! A lock-free thread-local pool serves same-thread take/give (preprocess | ||
| //! internals run on blocking-pool threads). The pool is capped to bound | ||
| //! residency; buffers beyond the cap are dropped. | ||
|
|
||
| use std::cell::RefCell; | ||
|
|
||
| /// Max recycled buffers kept per thread per class; excess is dropped. The | ||
| /// vision path holds at most a couple of live tensors per request, so a small | ||
| /// cap captures same-thread reuse. | ||
| const MAX_THREAD_POOLED: usize = 2; | ||
|
|
||
| thread_local! { | ||
| static F32_LOCAL: RefCell<Vec<Vec<f32>>> = const { RefCell::new(Vec::new()) }; | ||
| } | ||
|
|
||
| macro_rules! pool_impl { | ||
| ($take_cap:ident, $give:ident, $ty:ty, $local:ident) => { | ||
| /// Take an empty `Vec` with at least `cap` capacity, reusing pooled storage. | ||
| pub fn $take_cap(cap: usize) -> Vec<$ty> { | ||
| let mut v = $local.with(|p| p.borrow_mut().pop()).unwrap_or_default(); | ||
| v.clear(); | ||
| v.reserve(cap); | ||
| v | ||
| } | ||
|
|
||
| /// Return a buffer for reuse by a later same-thread take. | ||
| pub fn $give(v: Vec<$ty>) { | ||
| if v.capacity() == 0 { | ||
| return; | ||
| } | ||
| $local.with(|p| { | ||
| let mut p = p.borrow_mut(); | ||
| if p.len() < MAX_THREAD_POOLED { | ||
| p.push(v); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| pool_impl!(take_f32_cap, give_f32, f32, F32_LOCAL); | ||
|
|
||
| /// Take a zero-filled `Vec<f32>` of exactly `len`, reusing pooled storage. | ||
| pub fn take_f32(len: usize) -> Vec<f32> { | ||
| let mut v = take_f32_cap(len); | ||
| v.resize(len, 0.0); | ||
| v | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Solid pooling implementation; consider adding direct unit tests for the take/give contract.
The take/clear/reserve/resize sequence is correct:
take_f32_capalways returns aVecwith capacity ≥cap, andtake_f32'sresize(len, 0.0)guarantees the returned buffer is fully zero-initialized regardless of stale pooled contents, so there's no uninitialized-data leak across reuse cycles.give_f32correctly rejects zero-capacity buffers and respects theMAX_THREAD_POOLEDcap.This module currently has no dedicated unit tests (correctness is only exercised indirectly via
kimi_k25.rs/transforms.rsconsumers and theqwen3_vl.rsequivalence test). A small test verifyingtake_f32zero-fills and thatgive_f32/take_f32_capround-trip correctly would guard this shared primitive against regressions independent of any specific processor.🤖 Prompt for AI Agents