Download verl/utils/transformers_compat.py from LeTue09/arithmetic-grpo: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/LeTue09/arithmetic-grpo/resolve/main/verl/utils/transformers_compat.py
- Command line
-
hf download hf://LeTue09/arithmetic-grpo/verl/utils/transformers_compat.py
-
curl -L -o transformers_compat.py https://huggingface.co/LeTue09/arithmetic-grpo/resolve/main/verl/utils/transformers_compat.py
2.8 kB
| # Copyright 2024 Bytedance Ltd. and/or its affiliates | |
| # | |
| # 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. | |
| """ | |
| Compatibility utilities for different versions of transformers library. | |
| """ | |
| import importlib.metadata | |
| from functools import lru_cache | |
| from typing import Optional | |
| from packaging import version | |
| # Handle version compatibility for flash_attn_supports_top_left_mask | |
| # This function was added in newer versions of transformers | |
| try: | |
| from transformers.modeling_flash_attention_utils import flash_attn_supports_top_left_mask | |
| except ImportError: | |
| # For older versions of transformers that don't have this function | |
| # Default to False as a safe fallback for older versions | |
| def flash_attn_supports_top_left_mask(): | |
| """Fallback implementation for older transformers versions. | |
| Returns False to disable features that require this function. | |
| """ | |
| return False | |
| def is_transformers_version_in_range(min_version: Optional[str] = None, max_version: Optional[str] = None) -> bool: | |
| try: | |
| # Get the installed version of the transformers library | |
| transformers_version_str = importlib.metadata.version("transformers") | |
| except importlib.metadata.PackageNotFoundError as e: | |
| raise ModuleNotFoundError("The `transformers` package is not installed.") from e | |
| transformers_version = version.parse(transformers_version_str) | |
| lower_bound_check = True | |
| if min_version is not None: | |
| lower_bound_check = version.parse(min_version) <= transformers_version | |
| upper_bound_check = True | |
| if max_version is not None: | |
| upper_bound_check = transformers_version <= version.parse(max_version) | |
| return lower_bound_check and upper_bound_check | |
| def get_auto_model_for_vision2seq(): | |
| """Return the available VL auto model class across transformers versions.""" | |
| try: | |
| # Prefer the newer class when available. In transformers 4.x this class has | |
| # a broader mapping than AutoModelForVision2Seq, and AutoModelForVision2Seq | |
| # is deprecated for removal in v5. | |
| from transformers import AutoModelForImageTextToText | |
| except ImportError: | |
| from transformers import AutoModelForVision2Seq | |
| return AutoModelForVision2Seq | |
| return AutoModelForImageTextToText | |