-
Notifications
You must be signed in to change notification settings - Fork 743
[DataProcessor]merge processor #7747
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
luukunn
wants to merge
10
commits into
PaddlePaddle:develop
Choose a base branch
from
luukunn:merge_2
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
22f2415
first commit
luukunn ad338f4
fix unit test
luukunn a0c6826
fix pre-commit
luukunn 6a0c829
fix jinja
luukunn 59041ad
fix unit test
luukunn d3237e1
update
luukunn 109a7c8
fix hash
luukunn 19b7ed5
fix
luukunn de1be76
add unit test
luukunn 50586bc
update covered
luukunn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Multimodal processors for FastDeploy.""" | ||
|
|
||
| from fastdeploy.input.multimodal.ernie_vl import ErnieVLProcessor | ||
| from fastdeploy.input.multimodal.mm_processor import MMProcessor | ||
| from fastdeploy.input.multimodal.paddleocr_vl import PaddleOCRVLProcessor | ||
| from fastdeploy.input.multimodal.qwen3_vl import Qwen3VLProcessor | ||
| from fastdeploy.input.multimodal.qwen_vl import QwenVLProcessor | ||
|
|
||
| __all__ = [ | ||
| "MMProcessor", | ||
| "QwenVLProcessor", | ||
| "Qwen3VLProcessor", | ||
| "ErnieVLProcessor", | ||
| "PaddleOCRVLProcessor", | ||
| ] |
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Shared image utility functions for all VL image processors.""" | ||
|
|
||
| import math | ||
|
|
||
| import numpy as np | ||
|
|
||
| from fastdeploy.utils import data_processor_logger | ||
|
|
||
| __all__ = [ | ||
| "round_by_factor", | ||
| "ceil_by_factor", | ||
| "floor_by_factor", | ||
| "is_scaled_image", | ||
| "smart_resize", | ||
| "smart_resize_qwen", | ||
| "smart_resize_paddleocr", | ||
| ] | ||
|
|
||
|
|
||
| def round_by_factor(number: int, factor: int) -> int: | ||
| """Returns the closest integer to 'number' that is divisible by 'factor'.""" | ||
| return round(number / factor) * factor | ||
|
|
||
|
|
||
| def ceil_by_factor(number: int, factor: int) -> int: | ||
| """Returns the smallest integer >= 'number' that is divisible by 'factor'.""" | ||
| return math.ceil(number / factor) * factor | ||
|
|
||
|
|
||
| def floor_by_factor(number: int, factor: int) -> int: | ||
| """Returns the largest integer <= 'number' that is divisible by 'factor'.""" | ||
| return math.floor(number / factor) * factor | ||
|
|
||
|
|
||
| def is_scaled_image(image: np.ndarray) -> bool: | ||
| """Check if image pixel values are already normalized to [0, 1] range.""" | ||
| if image.dtype == np.uint8: | ||
| return False | ||
| return np.min(image) >= 0 and np.max(image) <= 1 | ||
|
|
||
|
|
||
| def smart_resize_qwen( | ||
| height: int, | ||
| width: int, | ||
| factor: int, | ||
| min_pixels: int, | ||
| max_pixels: int, | ||
| max_ratio: int = 200, | ||
| ) -> tuple: | ||
| """Smart image resizing for ERNIE / Qwen2.5 / Qwen3 models.""" | ||
| if max(height, width) / min(height, width) > max_ratio: | ||
| if height > width: | ||
| new_width = max(factor, round_by_factor(width, factor)) | ||
| new_height = floor_by_factor(new_width * max_ratio, factor) | ||
| else: | ||
| new_height = max(factor, round_by_factor(height, factor)) | ||
| new_width = floor_by_factor(new_height * max_ratio, factor) | ||
|
|
||
| data_processor_logger.info( | ||
| f"absolute aspect ratio must be smaller than {max_ratio}, " | ||
| f"got {max(height, width) / min(height, width)}, " | ||
| f"resize to {max(new_height, new_width) / min(new_height, new_width)}" | ||
| ) | ||
| height = new_height | ||
| width = new_width | ||
|
|
||
| h_bar = max(factor, round_by_factor(height, factor)) | ||
| w_bar = max(factor, round_by_factor(width, factor)) | ||
| if h_bar * w_bar > max_pixels: | ||
| beta = math.sqrt((height * width) / max_pixels) | ||
| h_bar = floor_by_factor(height / beta, factor) | ||
| w_bar = floor_by_factor(width / beta, factor) | ||
| elif h_bar * w_bar < min_pixels: | ||
| beta = math.sqrt(min_pixels / (height * width)) | ||
| h_bar = ceil_by_factor(height * beta, factor) | ||
| w_bar = ceil_by_factor(width * beta, factor) | ||
|
|
||
| if min_pixels > h_bar * w_bar or h_bar * w_bar > max_pixels: | ||
| raise ValueError(f"encounter invalid h_bar: {h_bar}, w_bar: {w_bar}") | ||
|
|
||
| return h_bar, w_bar | ||
|
|
||
|
|
||
| def smart_resize_paddleocr( | ||
| height: int, | ||
| width: int, | ||
| factor: int = 28, | ||
| min_pixels: int = 28 * 28 * 130, | ||
| max_pixels: int = 28 * 28 * 1280, | ||
| ) -> tuple: | ||
| """Smart image resizing for PaddleOCR-VL model.""" | ||
| if height < factor: | ||
| data_processor_logger.debug(f"smart_resize_paddleocr: height={height} < factor={factor}, reset height=factor") | ||
| width = round((width * factor) / height) | ||
| height = factor | ||
|
|
||
| if width < factor: | ||
| data_processor_logger.debug(f"smart_resize_paddleocr: width={width} < factor={factor}, reset width=factor") | ||
| height = round((height * factor) / width) | ||
|
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. 🟡 建议
if min_pixels > h_bar * w_bar or h_bar * w_bar > max_pixels:
raise ValueError(f"encounter invalid h_bar: {h_bar}, w_bar: {w_bar}")而 |
||
| width = factor | ||
|
|
||
| if max(height, width) / min(height, width) > 200: | ||
| raise ValueError( | ||
| f"absolute aspect ratio must be smaller than 200, " f"got {max(height, width) / min(height, width)}" | ||
| ) | ||
|
|
||
| h_bar = round(height / factor) * factor | ||
| w_bar = round(width / factor) * factor | ||
| if h_bar * w_bar > max_pixels: | ||
| beta = math.sqrt((height * width) / max_pixels) | ||
This comment was marked as outdated.
Sorry, something went wrong. |
||
| h_bar = math.floor(height / beta / factor) * factor | ||
| w_bar = math.floor(width / beta / factor) * factor | ||
| elif h_bar * w_bar < min_pixels: | ||
| beta = math.sqrt(min_pixels / (height * width)) | ||
| h_bar = math.ceil(height * beta / factor) * factor | ||
| w_bar = math.ceil(width * beta / factor) * factor | ||
|
|
||
| return h_bar, w_bar | ||
|
|
||
|
|
||
| def smart_resize( | ||
| height: int, | ||
| width: int, | ||
| factor: int, | ||
| min_pixels: int, | ||
| max_pixels: int, | ||
| max_ratio: int = 200, | ||
| variant: str = "qwen", | ||
| ) -> tuple: | ||
| """Unified smart_resize dispatcher.""" | ||
| if variant == "paddleocr": | ||
| return smart_resize_paddleocr(height, width, factor, min_pixels, max_pixels) | ||
| return smart_resize_qwen(height, width, factor, min_pixels, max_pixels, max_ratio) | ||
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.
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.
🟡 建议
content类型从固定list改为str | None | list,是接口行为变更。原实现保证
parsed_content始终为list(包括空列表和[{"type":"text",...}]),下游所有消费方(如Processor.process_messages)可安全迭代。改动后content=None时返回None,content=str时返回裸字符串,若下游存在for part in content:等迭代逻辑则会引发TypeError(None 不可迭代)或错误地逐字符迭代字符串。请确认
parse_chat_messages所有消费方(Processor.process_messages等)均已更新,能正确处理None和str类型。