-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Support video in mtmd (base API) #16910
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ae099ec
add video
tc-mb d5b832d
feat: support minicpm-v video normal speed
MrAMS f09abc0
fix: video normal speed uniform sample & check videoInfo valid
MrAMS e377686
refactor: support video in media
MrAMS 2c1d02a
refactor: add bitmap_type, instead of is_audio, is_video...
MrAMS 816b92b
feat: support loading video from buffer
MrAMS efefc2a
feat&fix: fix segmentation fault bug, add llama-server video_url api
MrAMS 32a45c4
fix: max video frames
MrAMS 5ee744f
clean up
MrAMS 6e8c9f6
refactor: get_video_info and decode_video use the same AVFormatContext
MrAMS 1e9c563
Merge upstream/master
MrAMS 113cfc2
undo the changes and submit a separate pull request to the llama-serv…
MrAMS ef68f2a
refactor: modifications made based on reviews
MrAMS 358be38
Merge remote-tracking branch 'upstream/master' into Support-video-in-…
MrAMS 9396d6e
fix: add queue header file
MrAMS 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,6 +201,7 @@ struct clip_hparams { | |
| // legacy | ||
| bool has_llava_projector = false; | ||
| int minicpmv_version = 0; | ||
| int minicpmv_max_slice_nums = 9; | ||
| int32_t minicpmv_query_num = 0; // MiniCPM-V query number | ||
|
|
||
| // custom value provided by user, can be undefined if not set | ||
|
|
@@ -3911,16 +3912,67 @@ struct llava_uhd { | |
| const bool has_slices = original_size.width > slice_size || original_size.height > slice_size; | ||
| const bool has_pinpoints = !ctx->model.hparams.image_res_candidates.empty(); | ||
|
|
||
| if (!has_slices) { | ||
| // skip slicing logic | ||
| res.overview_size = clip_image_size{slice_size, slice_size}; | ||
| res.refined_size = clip_image_size{0, 0}; | ||
| res.grid_size = clip_image_size{0, 0}; | ||
| if (clip_is_minicpmv(ctx)) { | ||
|
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. What's the change here compared to the initial version? IMO I think this is more like a duplicated logic, see |
||
| auto best_size = get_best_resize(original_size, slice_size, patch_size, !has_slices); | ||
| res.overview_size = best_size; | ||
|
|
||
| { | ||
| const int max_slice_nums = ctx->model.hparams.minicpmv_max_slice_nums; | ||
| const float log_ratio = log((float)original_width / original_height); | ||
| const float ratio = (float)original_width * original_height / (slice_size * slice_size); | ||
| const int multiple = fmin(ceil(ratio), max_slice_nums); | ||
|
|
||
| auto best_grid = get_best_grid(max_slice_nums, multiple, log_ratio); | ||
| auto refine_size = get_refine_size(original_size, best_grid, slice_size, patch_size, true); | ||
| res.grid_size = best_grid; | ||
| res.refined_size = refine_size; | ||
|
|
||
| LOG_DBG("%s: original size: %d x %d, overview size: %d x %d, refined size: %d x %d, grid size: %d x %d\n", | ||
| __func__, original_width, original_height, | ||
| res.overview_size.width, res.overview_size.height, | ||
| res.refined_size.width, res.refined_size.height, | ||
| res.grid_size.width, res.grid_size.height); | ||
|
|
||
| if (!has_slices || max_slice_nums == 0) { | ||
| return res; | ||
| } | ||
|
|
||
| int width = refine_size.width; | ||
| int height = refine_size.height; | ||
| int grid_x = int(width / best_grid.width); | ||
| int grid_y = int(height / best_grid.height); | ||
| for (int patches_y = 0, ic = 0; | ||
| patches_y < refine_size.height && ic < best_grid.height; | ||
| patches_y += grid_y, ic += 1) { | ||
| for (int patches_x = 0, jc = 0; | ||
| patches_x < refine_size.width && jc < best_grid.width; | ||
| patches_x += grid_x, jc += 1) { | ||
| slice_coordinates slice; | ||
| slice.x = patches_x; | ||
| slice.y = patches_y; | ||
| slice.size.width = grid_x; | ||
| slice.size.height = grid_y; | ||
| res.slices.push_back(slice); | ||
| LOG_DBG("%s: slice %d: x=%d, y=%d, size=%dx%d\n", | ||
| __func__, (int)res.slices.size() - 1, | ||
| slice.x, slice.y, slice.size.width, slice.size.height); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
| else { | ||
| if (!has_slices) { | ||
| // skip slicing logic | ||
| res.overview_size = clip_image_size{slice_size, slice_size}; | ||
| res.refined_size = clip_image_size{0, 0}; | ||
| res.grid_size = clip_image_size{0, 0}; | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| if (has_pinpoints) { | ||
| if (has_pinpoints) { | ||
| // has pinpoints, use them to calculate the grid size (e.g. llava-1.6) | ||
| auto refine_size = llava_uhd::select_best_resolution( | ||
| original_size, | ||
|
|
@@ -3956,53 +4008,7 @@ struct llava_uhd { | |
|
|
||
| return res; | ||
| } | ||
|
|
||
| // no pinpoints, dynamically calculate the grid size (e.g. minicpmv) | ||
|
|
||
| auto best_size = get_best_resize(original_size, slice_size, patch_size, !has_slices); | ||
| res.overview_size = best_size; | ||
|
|
||
| { | ||
| const int max_slice_nums = 9; // TODO: this is only used by minicpmv, maybe remove it | ||
| const float log_ratio = log((float)original_width / original_height); | ||
| const float ratio = (float)original_width * original_height / (slice_size * slice_size); | ||
| const int multiple = fmin(ceil(ratio), max_slice_nums); | ||
|
|
||
| auto best_grid = get_best_grid(max_slice_nums, multiple, log_ratio); | ||
| auto refine_size = get_refine_size(original_size, best_grid, slice_size, patch_size, true); | ||
| res.grid_size = best_grid; | ||
| res.refined_size = refine_size; | ||
|
|
||
| LOG_DBG("%s: original size: %d x %d, overview size: %d x %d, refined size: %d x %d, grid size: %d x %d\n", | ||
| __func__, original_width, original_height, | ||
| res.overview_size.width, res.overview_size.height, | ||
| res.refined_size.width, res.refined_size.height, | ||
| res.grid_size.width, res.grid_size.height); | ||
|
|
||
| int width = refine_size.width; | ||
| int height = refine_size.height; | ||
| int grid_x = int(width / best_grid.width); | ||
| int grid_y = int(height / best_grid.height); | ||
| for (int patches_y = 0, ic = 0; | ||
| patches_y < refine_size.height && ic < best_grid.height; | ||
| patches_y += grid_y, ic += 1) { | ||
| for (int patches_x = 0, jc = 0; | ||
| patches_x < refine_size.width && jc < best_grid.width; | ||
| patches_x += grid_x, jc += 1) { | ||
| slice_coordinates slice; | ||
| slice.x = patches_x; | ||
| slice.y = patches_y; | ||
| slice.size.width = grid_x; | ||
| slice.size.height = grid_y; | ||
| res.slices.push_back(slice); | ||
| LOG_DBG("%s: slice %d: x=%d, y=%d, size=%dx%d\n", | ||
| __func__, (int)res.slices.size() - 1, | ||
| slice.x, slice.y, slice.size.width, slice.size.height); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| static std::vector<clip_image_u8_ptr> slice_image(const clip_image_u8 * img, const slice_instructions & inst) { | ||
|
|
||
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.
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.
Search for
max_slice_nums, it's already present in the code base.