Skip to content

Commit eefa09c

Browse files
[Bugfix][0.7.3] Update transformer utils patch (#918)
### What this PR does / why we need it? Update transformer utils patch for `v0.7.3`. This PR is a supplement to #858, adding more patch methods adapted from vllm-project/vllm#17948 to solve `revision` problems in more scenarios besides #829. ### Does this PR introduce _any_ user-facing change? no. ### How was this patch tested? ```bash vllm serve Qwen/Qwen2.5-VL-7B-Instruct \ --dtype bfloat16 \ --max_model_len 32768 \ --max-num-batched-tokens 32768 ``` Signed-off-by: shen-shanshan <467638484@qq.com>
1 parent 9f01afc commit eefa09c

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

vllm_ascend/patch/patch_transformers_utils.py

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
# limitations under the License.
1616
#
1717

18-
from typing import TYPE_CHECKING, Any, Optional, Union
18+
from typing import TYPE_CHECKING, Any, Optional, Union, cast
1919

2020
from transformers.processing_utils import ProcessorMixin
2121
from typing_extensions import TypeVar
2222
from vllm.transformers_utils import processor
2323
from vllm.transformers_utils.processor import (_merge_mm_kwargs,
24-
cached_get_processor)
24+
cached_get_image_processor,
25+
cached_get_processor,
26+
cached_get_video_processor)
2527

2628
if TYPE_CHECKING:
2729
from vllm.config import ModelConfig
@@ -90,9 +92,99 @@ def cached_processor_from_config(
9092
)
9193

9294

95+
def get_image_processor(
96+
processor_name: str,
97+
*args: Any,
98+
revision: Optional[str] = None,
99+
trust_remote_code: bool = False,
100+
**kwargs: Any,
101+
):
102+
"""Load an image processor for the given model name via HuggingFace."""
103+
# don't put this import at the top level
104+
# it will call torch.cuda.device_count()
105+
from transformers import AutoImageProcessor
106+
from transformers.image_processing_utils import BaseImageProcessor
107+
108+
try:
109+
processor = AutoImageProcessor.from_pretrained(
110+
processor_name,
111+
*args,
112+
revision=revision,
113+
trust_remote_code=trust_remote_code,
114+
**kwargs)
115+
except ValueError as e:
116+
# If the error pertains to the processor class not existing or not
117+
# currently being imported, suggest using the --trust-remote-code flag.
118+
# Unlike AutoTokenizer, AutoImageProcessor does not separate such errors
119+
if not trust_remote_code:
120+
err_msg = (
121+
"Failed to load the image processor. If the image processor is "
122+
"a custom processor not yet available in the HuggingFace "
123+
"transformers library, consider setting "
124+
"`trust_remote_code=True` in LLM or using the "
125+
"`--trust-remote-code` flag in the CLI.")
126+
raise RuntimeError(err_msg) from e
127+
else:
128+
raise e
129+
130+
return cast(BaseImageProcessor, processor)
131+
132+
133+
def cached_image_processor_from_config(
134+
model_config: "ModelConfig",
135+
**kwargs: Any,
136+
):
137+
return cached_get_image_processor(
138+
model_config.model,
139+
revision=model_config.revision,
140+
trust_remote_code=model_config.trust_remote_code,
141+
**_merge_mm_kwargs(model_config, **kwargs),
142+
)
143+
144+
145+
def get_video_processor(
146+
processor_name: str,
147+
*args: Any,
148+
revision: Optional[str] = None,
149+
trust_remote_code: bool = False,
150+
**kwargs: Any,
151+
):
152+
"""Load a video processor for the given model name via HuggingFace."""
153+
# don't put this import at the top level
154+
# it will call torch.cuda.device_count()
155+
from transformers.image_processing_utils import BaseImageProcessor
156+
157+
processor = get_processor(
158+
processor_name,
159+
*args,
160+
revision=revision,
161+
trust_remote_code=trust_remote_code,
162+
**kwargs,
163+
)
164+
165+
return cast(BaseImageProcessor, processor.video_processor)
166+
167+
168+
def cached_video_processor_from_config(
169+
model_config: "ModelConfig",
170+
**kwargs: Any,
171+
):
172+
return cached_get_video_processor(
173+
model_config.model,
174+
revision=model_config.revision,
175+
trust_remote_code=model_config.trust_remote_code,
176+
**_merge_mm_kwargs(model_config, **kwargs),
177+
)
178+
179+
180+
# Adapted from vllm: https://github.com/vllm-project/vllm/pull/17948
93181
# Pass `revision` param to transformer processor to avoid using `main` as
94182
# default branch when using modelscope.
95183
# Find more details at:
96184
# https://github.com/vllm-project/vllm-ascend/issues/829
97185
processor.get_processor = get_processor
98186
processor.cached_processor_from_config = cached_processor_from_config
187+
processor.get_image_processor = get_image_processor
188+
processor.cached_image_processor_from_config = cached_image_processor_from_config
189+
processor.get_video_processor = get_video_processor
190+
processor.cached_video_processor_from_config = cached_video_processor_from_config

0 commit comments

Comments
 (0)