-
Notifications
You must be signed in to change notification settings - Fork 664
Support video inputs #4360
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
Open
CUHKSZzxy
wants to merge
24
commits into
InternLM:main
Choose a base branch
from
CUHKSZzxy:support-video-inputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,686
−815
Open
Support video inputs #4360
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3b089eb
support video inputs
CUHKSZzxy 3a76064
refactor; fix video mrope
CUHKSZzxy 29fc130
reorganize media io
CUHKSZzxy 3480934
rename media
CUHKSZzxy 137fdc4
Merge branch 'main' into support-video-inputs
CUHKSZzxy c3b777f
cleanups; support qwen3.5 video inputs
CUHKSZzxy dbdca96
fix ut
CUHKSZzxy aac169e
better modality
CUHKSZzxy 341350d
refactor media io; add opencv video loader; remove qwen-vl-utils codes
CUHKSZzxy 753544d
Merge branch 'main' into support-video-inputs
CUHKSZzxy becdead
support interns1-pro video inputs
CUHKSZzxy 5032683
fix and add more ut; unify video loader outputs
CUHKSZzxy be0acdd
minor
CUHKSZzxy 0822b0a
make copilot happy
CUHKSZzxy 4c13495
support image_data inputs
CUHKSZzxy 90e532f
fix ut
CUHKSZzxy 03f4b2b
add init file
CUHKSZzxy 1a01fe2
Merge branch 'main' into support-video-inputs
CUHKSZzxy a12d3b9
fix video/jpeg loading
CUHKSZzxy d59d0ba
abstract make data func
CUHKSZzxy 61f0ca5
media io kwargs for video
CUHKSZzxy e93125a
fix kwargs for load_file
CUHKSZzxy 620daf6
remove role assertion
CUHKSZzxy 5938f28
Merge branch 'main' into support-video-inputs
CUHKSZzxy 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
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
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
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 |
|---|---|---|
|
|
@@ -7,8 +7,9 @@ | |
|
|
||
| from lmdeploy.pytorch.engine.input_process import BaseModelInputProcessor, PreprocessInputResult | ||
| from lmdeploy.pytorch.model_inputs import StepContext, StepContextManager | ||
| from lmdeploy.pytorch.multimodal.data_type import MultiModalTensor | ||
| from lmdeploy.pytorch.multimodal.data_type import MultiModalData | ||
| from lmdeploy.pytorch.weight_loader.model_weight_loader import load_weight | ||
| from lmdeploy.vl.constants import Modality | ||
|
|
||
| from .interns1_pro_ts import InternS1ProTimeSeriesModel | ||
| from .patch import add_prefix, get_build_model_context | ||
|
|
@@ -173,25 +174,28 @@ def prepare_inputs_for_generation( | |
| ts_sr = None | ||
| ts_mask = None | ||
| if context.input_multimodals is not None: | ||
| mm_data = [input_mm.get('image', []) for input_mm in context.input_multimodals] | ||
| mm_inputs = [input_mm.get('mm_data', []) for input_mm in context.input_multimodals] | ||
| # flatten batch | ||
| mm_data = [data for im_data in mm_data for data in im_data] | ||
| mm_inputs = [item for sublist in mm_inputs for item in sublist] | ||
|
|
||
| if len(mm_data) > 0: | ||
| is_time_series = mm_data[0].meta.get('ts_token_id', False) | ||
| if len(mm_inputs) > 0: | ||
| modality = mm_inputs[0].modality | ||
| image_token_id = mm_inputs[0].meta.get('image_token_id') | ||
| video_token_id = mm_inputs[0].meta.get('video_token_id') | ||
| ts_token_id = mm_inputs[0].meta.get('ts_token_id') | ||
|
|
||
| if is_time_series: | ||
| ts_values = mm_data | ||
| ts_token_id = ts_values[0].meta['ts_token_id'] | ||
| ts_lens = ts_values[0].meta['ts_lens'] | ||
| ts_sr = ts_values[0].meta['ts_sr'] | ||
| if modality == Modality.TIME_SERIES: | ||
| ts_values = torch.cat([inp.data for inp in mm_inputs]) | ||
| ts_mask = input_ids == ts_token_id | ||
| ts_values = torch.cat([data.data for data in ts_values]) | ||
|
|
||
| ts_lens = mm_inputs[0].meta['ts_lens'] | ||
| ts_sr = mm_inputs[0].meta['ts_sr'] | ||
| else: | ||
| pixel_values = torch.cat([data.data for data in mm_data]) | ||
| image_token_id = mm_data[0].meta['image_token_id'] | ||
| image_mask = input_ids == image_token_id | ||
| grid_thw = torch.cat([data.meta['grid_thw'] for data in mm_data]).cpu() | ||
| pixel_values = torch.cat([inp.data for inp in mm_inputs]) | ||
| mm_token_id = image_token_id if modality == Modality.IMAGE else video_token_id | ||
| image_mask = (input_ids == mm_token_id) | ||
|
|
||
| grid_thw = torch.cat([data.meta['grid_thw'] for data in mm_inputs]).cpu() | ||
| vis_pos_emb = self.visual.rot_pos_emb(grid_thw) | ||
| pos_embeds = self.visual.fast_pos_embed_interpolate(grid_thw) | ||
| vis_cu_seqlens = torch.repeat_interleave(grid_thw[:, 1] * grid_thw[:, 2], | ||
|
|
@@ -365,6 +369,63 @@ def __init__(self, config: PretrainedConfig, dtype: torch.dtype) -> None: | |
| self.config = config | ||
| self.dtype = dtype | ||
|
|
||
| def _make_image_mm_data(self, input_mm: Dict[str, Any]) -> MultiModalData: | ||
| """Make image MultiModalData.""" | ||
| pixel_values = input_mm['pixel_values'].to(self.dtype) | ||
| image_grid_thw = input_mm['image_grid_thw'] | ||
| offset = input_mm['offset'] | ||
| start = offset | ||
| image_token_id = input_mm['image_token_id'] | ||
| num_pad = input_mm['image_tokens'] | ||
| if isinstance(num_pad, torch.Tensor): | ||
| num_pad = num_pad.item() | ||
|
|
||
| mm_data = MultiModalData(modality=Modality.IMAGE, | ||
| data=pixel_values, | ||
| start=start, | ||
| end=start + num_pad, | ||
| meta=dict(grid_thw=image_grid_thw, image_token_id=image_token_id)) | ||
| return mm_data | ||
|
|
||
| def _make_video_mm_data(self, input_mm: Dict[str, Any]) -> MultiModalData: | ||
| """Make video MultiModalData.""" | ||
| pixel_values_videos = input_mm['pixel_values_videos'].to(self.dtype) | ||
| video_grid_thw = input_mm['video_grid_thw'] | ||
| offset = input_mm['offset'] | ||
| start = offset | ||
| video_token_id = input_mm['video_token_id'] | ||
| num_pad = input_mm['video_tokens'] | ||
| if isinstance(num_pad, torch.Tensor): | ||
| num_pad = num_pad.item() | ||
|
|
||
| mm_data = MultiModalData(modality=Modality.VIDEO, | ||
| data=pixel_values_videos, | ||
| start=start, | ||
| end=start + num_pad, | ||
| meta=dict( | ||
| grid_thw=video_grid_thw, | ||
| video_token_id=video_token_id, | ||
| )) | ||
| return mm_data | ||
|
|
||
| def _make_time_series_mm_data(self, input_mm: Dict[str, Any]) -> MultiModalData: | ||
| """Make time series MultiModalData.""" | ||
| ts_values = input_mm['ts_values'].to(self.dtype) | ||
| offset = input_mm['offset'] | ||
| ts_token_id = input_mm['ts_token_id'] | ||
| ts_lens = input_mm['ts_lens'] | ||
| ts_sr = input_mm['ts_sr'] | ||
| num_pad = input_mm['ts_tokens'] | ||
| if isinstance(num_pad, torch.Tensor): | ||
| num_pad = num_pad.item() | ||
|
|
||
| mm_data = MultiModalData(modality=Modality.TIME_SERIES, | ||
| data=ts_values, | ||
| start=offset, | ||
| end=offset + num_pad, | ||
| meta=dict(ts_lens=ts_lens, ts_sr=ts_sr, ts_token_id=ts_token_id)) | ||
| return mm_data | ||
|
|
||
| def preprocess_input(self, | ||
| input_ids: List[int], | ||
| input_multimodals: List[Dict[str, Any]] = None, | ||
|
|
@@ -373,38 +434,17 @@ def preprocess_input(self, | |
| if input_multimodals is None or len(input_multimodals) == 0: | ||
| return input_ids, input_multimodals | ||
|
|
||
| input_imgs = [] | ||
| input_mm_data = [] | ||
| for input_mm in input_multimodals: | ||
| if 'ts_values' in input_mm: | ||
| ts_values = input_mm['ts_values'].to(self.dtype) | ||
| offset = input_mm['offset'] | ||
| ts_token_id = input_mm['ts_token_id'] | ||
| ts_lens = input_mm['ts_lens'] | ||
| ts_sr = input_mm['ts_sr'] | ||
| num_pad = input_mm['num_ts_tokens'] | ||
|
|
||
| mm_data = MultiModalTensor(data=ts_values, | ||
| start=offset, | ||
| end=offset + num_pad, | ||
| meta=dict(ts_token_id=ts_token_id, ts_lens=ts_lens, ts_sr=ts_sr)) | ||
| else: | ||
| pixel_values = input_mm['pixel_values'].to(self.dtype) | ||
| image_grid_thw = input_mm['image_grid_thw'] | ||
| offset = input_mm['offset'] | ||
| start = offset | ||
| image_token_id = input_mm['image_token_id'] | ||
| num_pad = input_mm['image_tokens'] | ||
| if isinstance(num_pad, torch.Tensor): | ||
| num_pad = num_pad.item() | ||
|
|
||
| mm_data = MultiModalTensor(data=pixel_values, | ||
| start=start, | ||
| end=start + num_pad, | ||
| meta=dict(grid_thw=image_grid_thw, image_token_id=image_token_id)) | ||
| input_imgs.append(mm_data) | ||
|
|
||
| result = PreprocessInputResult( | ||
| input_ids=input_ids, | ||
| input_multimodals=dict(image=input_imgs), | ||
| ) | ||
| modality = input_mm.get('modality') | ||
| if modality == Modality.IMAGE: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract code in branch into a function would make the code more readable, for example: |
||
| mm_data = self._make_image_mm_data(input_mm) | ||
| elif modality == Modality.VIDEO: | ||
| mm_data = self._make_video_mm_data(input_mm) | ||
| elif modality == Modality.TIME_SERIES: | ||
| mm_data = self._make_time_series_mm_data(input_mm) | ||
| input_mm_data.append(mm_data) | ||
|
|
||
| result = PreprocessInputResult(input_ids=input_ids, input_multimodals=dict(mm_data=input_mm_data)) | ||
|
|
||
| return result | ||
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.
Uh oh!
There was an error while loading. Please reload this page.