-
Notifications
You must be signed in to change notification settings - Fork 327
feat(multimodal): add max_image_token_count guard with OOM risk guidance #1308
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
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -181,6 +181,17 @@ async def _alloc_resource(self, items, md5sums, token_nums, datas): | |
| self.cache_client.root.set_items_data(update_data_ids) | ||
| return | ||
|
|
||
| def _assert_image_token_count(self, token_num: int): | ||
| if token_num > self.args.max_image_token_count: | ||
| err_msg = ( | ||
| f"single image token count {token_num} exceeds max_image_token_count {self.args.max_image_token_count}." | ||
| f"You can increase this limit by setting --max_image_token_count to a larger value when starting " | ||
| f"LightLLM. Warning: increasing this limit raises runtime OOM risk." | ||
| ) | ||
| logger.warning(err_msg) | ||
| raise ValueError(err_msg) | ||
| return | ||
|
|
||
| async def _alloc_multimodal_resources(self, multimodal_params: MultimodalParams, sampling_params: SamplingParams): | ||
| # 只有 P 和 NORMAL 节点需要真的管理多模态资源 | ||
| if self.pd_mode.is_P_or_NORMAL(): | ||
|
|
@@ -190,6 +201,7 @@ async def _alloc_multimodal_resources(self, multimodal_params: MultimodalParams, | |
| data = img.read() | ||
| # must after init_imageitem_extral_params | ||
| token_num = self.tokenizer.get_image_token_length(img) | ||
| self._assert_image_token_count(token_num) | ||
|
Contributor
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. |
||
| md5sum = hashlib.md5(data).hexdigest() + "_" + str(hash(frozendict(img.extra_params))) | ||
| md5sums.append(md5sum) | ||
| img.md5 = md5sum | ||
|
|
@@ -245,7 +257,9 @@ def tokens(self, prompt, multimodal_params, samping_params: SamplingParams, kwar | |
| for img in multimodal_params.images: | ||
| img_count += 1 | ||
| self.tokenizer.init_imageitem_extral_params(img, multimodal_params, samping_params) | ||
| image_tokens += self.tokenizer.get_image_token_length(img) | ||
| token_num = self.tokenizer.get_image_token_length(img) | ||
| self._assert_image_token_count(token_num) | ||
|
Contributor
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. |
||
| image_tokens += token_num | ||
| for audio in multimodal_params.audios: | ||
| audio_count += 1 | ||
| self.tokenizer.init_audioitem_extral_params(audio, multimodal_params, samping_params) | ||
|
|
||
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.
Avoid using
assertfor runtime validation of user input or critical safety guards. Python'sassertstatements are removed when the interpreter is run with optimizations (-O), which would disable this check and potentially lead to OOM. Use an explicitifcheck and raise aValueErrorinstead. I've also renamed the method to better reflect its behavior.