Skip to content

feat: share multimodal hash helpers#4704

Open
CUHKSZzxy wants to merge 6 commits into
InternLM:mainfrom
CUHKSZzxy:feat/share-vl-mm-hasher
Open

feat: share multimodal hash helpers#4704
CUHKSZzxy wants to merge 6 commits into
InternLM:mainfrom
CUHKSZzxy:feat/share-vl-mm-hasher

Conversation

@CUHKSZzxy

@CUHKSZzxy CUHKSZzxy commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Compute multimodal content hashes in the shared VL preprocessing path after get_expanded_mm_items, so the hash is based on processed, expanded multimodal items instead of PyTorch-engine-local data.
  • Attach content_hash to each expanded multimodal item while excluding prompt-position offset, keeping the same content reusable at different prompt positions.
  • Preserve the attached content_hash through PyTorch multimodal input processors into MultiModalData, and remove the PyTorch engine-side hash population path while keeping the scheduler fallback for defensive compatibility.

Validation

  • Focused pre-commit checks for the touched implementation and test files passed.
  • Focused VL hasher, VL preprocess, PyTorch multimodal hash preservation, and block-trie prefix-cache unit tests passed.
  • Qwen3-VL processor tests passed.
  • Real PyTorch VL server repeated-image prefix-cache check matched the baseline main branch for prompt/cached-token behavior and showed cache hits on repeated multimodal requests.

Assistance

Assisted with Codex + GPT-5.5 xHigh Fast, reviewed manually

@CUHKSZzxy CUHKSZzxy marked this pull request as ready for review June 25, 2026 09:28
Copilot AI review requested due to automatic review settings June 25, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py with 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.

@lvhan028 lvhan028 added the enhancement New feature or request label Jun 30, 2026
Comment thread lmdeploy/pytorch/engine/engine.py Outdated
DistServeInitRequest,
)
from lmdeploy.utils import get_logger, get_model
from lmdeploy.vl import hasher as mm_hasher

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

lzhangzz
lzhangzz previously approved these changes Jun 30, 2026
grimoire
grimoire previously approved these changes Jun 30, 2026
@lvhan028

lvhan028 commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

It does not match what I had in mind for multimodal fingerprints.

What I expect

  • Compute a stable content fingerprint early in the request path (before engine-specific preprocessing).
  • Attach it to each multimodal item and pass it through to all backends
  • Treat the fingerprint as general multimodal metadata, not something that only exists when enable_prefix_caching is on.

@lvhan028 lvhan028 dismissed stale reviews from grimoire and lzhangzz July 1, 2026 08:42

requirement mismatched

@CUHKSZzxy CUHKSZzxy marked this pull request as draft July 6, 2026 08:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Comment thread lmdeploy/vl/model/base.py Outdated
Comment on lines +235 to +237
# 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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@CUHKSZzxy CUHKSZzxy marked this pull request as ready for review July 6, 2026 12:15
Comment thread lmdeploy/vl/model/base.py
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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has preprocess been override in other classes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Now I have moved the hash attachment to a more general place outside the base vision model class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants