feat: share multimodal hash helpers#4704
Conversation
There was a problem hiding this comment.
Pull request overview
This PR centralizes multimodal content hashing into a shared lmdeploy.vl helper module, then updates PyTorch prefix-cache code paths (and tests) to use the shared implementation while keeping existing cache-key behavior stable.
Changes:
- Added
lmdeploy/vl/hasher.pywith deterministic hashing helpers for both dataclass-style and dict-style multimodal payloads. - Rewired PyTorch prefix-cache hashing call sites to use the shared VL hasher (including unit test monkeypatch targets).
- Added focused unit tests covering hash stability, sensitivity to content/meta/mRoPE, and ignoring position-only keys for dict-style items.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
lmdeploy/vl/hasher.py |
Introduces shared deterministic multimodal hashing + “ensure content_hash” helpers for two multimodal representations. |
lmdeploy/pytorch/multimodal/data_type.py |
Removes local hashing implementation and re-exports shared hashing helpers for compatibility. |
lmdeploy/pytorch/messages.py |
Updates prefix-cache meta hashing fallback to call the shared VL hasher. |
lmdeploy/pytorch/engine/engine.py |
Ensures multimodal content hashes are populated after preprocessing when prefix caching is enabled. |
tests/test_lmdeploy/test_vl/test_hasher.py |
Adds unit tests validating hash determinism and correct inclusion/exclusion rules. |
tests/pytorch/paging/test_block_trie.py |
Adjusts monkeypatching to target the shared hasher module instead of the previous PyTorch-local symbol. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| DistServeInitRequest, | ||
| ) | ||
| from lmdeploy.utils import get_logger, get_model | ||
| from lmdeploy.vl import hasher as mm_hasher |
There was a problem hiding this comment.
Since we have alias make_multimodal_content_hash in pytorch/multimodal/data_type.py, the change in engine.py and messages.py might not be necessary.
|
It does not match what I had in mind for multimodal fingerprints. What I expect
|
requirement mismatched
4831063 to
499e70d
Compare
| # expand bundled hf processor outputs into per-image/video entry for lmdeploy to consume | ||
| expanded_mm_items = get_expanded_mm_items(collected_mm_items, self.mm_tokens) | ||
| attach_multimodal_content_hashes(expanded_mm_items) |
There was a problem hiding this comment.
This is intentional. The human review guidance for this PR is to
treat the multimodal fingerprint as general multimodal metadata, not as prefix-cache-only state.
Prefix caching is one consumer of content_hash, but the hash is attached in the shared VL path, so it can be passed through consistently and reused by other backends later.
| expanded_mm_items = get_expanded_mm_items(collected_mm_items, self.mm_tokens) | ||
| attach_multimodal_content_hashes(expanded_mm_items) | ||
|
|
||
| result = dict(input_ids=input_ids.tolist(), multimodal=expanded_mm_items) |
There was a problem hiding this comment.
has preprocess been override in other classes?
There was a problem hiding this comment.
Nice catch. Now I have moved the hash attachment to a more general place outside the base vision model class.
Summary
get_expanded_mm_items, so the hash is based on processed, expanded multimodal items instead of PyTorch-engine-local data.content_hashto each expanded multimodal item while excluding prompt-positionoffset, keeping the same content reusable at different prompt positions.content_hashthrough PyTorch multimodal input processors intoMultiModalData, and remove the PyTorch engine-side hash population path while keeping the scheduler fallback for defensive compatibility.Validation
Assistance
Assisted with Codex + GPT-5.5 xHigh Fast, reviewed manually