Upload rl_code/verl/models/monkey_patch.py with huggingface_hub
Browse files
rl_code/verl/models/monkey_patch.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS
|
| 17 |
+
|
| 18 |
+
from ..utils.py_functional import is_transformers_version_greater_than
|
| 19 |
+
from .transformers.flash_attention_utils import flash_attention_forward
|
| 20 |
+
from .transformers.qwen2_vl import (
|
| 21 |
+
qwen2_vl_attn_forward,
|
| 22 |
+
qwen2_vl_base_forward_new,
|
| 23 |
+
qwen2_vl_forward_new,
|
| 24 |
+
qwen2_vl_forward_old,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def apply_ulysses_patch(model_type: str) -> None:
|
| 29 |
+
if model_type in ("llama", "gemma", "gemma2", "mistral", "qwen2", "qwen3", "qwen3_moe"):
|
| 30 |
+
ALL_ATTENTION_FUNCTIONS["flash_attention_2"] = flash_attention_forward
|
| 31 |
+
elif model_type in ("qwen2_vl", "qwen2_5_vl"):
|
| 32 |
+
if is_transformers_version_greater_than("4.53.0"):
|
| 33 |
+
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import Qwen2_5_VLAttention
|
| 34 |
+
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLAttention
|
| 35 |
+
|
| 36 |
+
Qwen2VLAttention.forward = qwen2_vl_attn_forward
|
| 37 |
+
Qwen2_5_VLAttention.forward = qwen2_vl_attn_forward
|
| 38 |
+
raise NotImplementedError("Transformers 4.53.* is not compatible with Qwen2-VL models.")
|
| 39 |
+
else:
|
| 40 |
+
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import Qwen2_5_VLFlashAttention2
|
| 41 |
+
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLFlashAttention2
|
| 42 |
+
|
| 43 |
+
Qwen2VLFlashAttention2.forward = qwen2_vl_attn_forward
|
| 44 |
+
Qwen2_5_VLFlashAttention2.forward = qwen2_vl_attn_forward
|
| 45 |
+
|
| 46 |
+
if is_transformers_version_greater_than("4.52.0"):
|
| 47 |
+
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import (
|
| 48 |
+
Qwen2_5_VLForConditionalGeneration,
|
| 49 |
+
Qwen2_5_VLModel,
|
| 50 |
+
)
|
| 51 |
+
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLForConditionalGeneration, Qwen2VLModel
|
| 52 |
+
|
| 53 |
+
Qwen2VLModel.forward = qwen2_vl_base_forward_new
|
| 54 |
+
Qwen2_5_VLModel.forward = qwen2_vl_base_forward_new
|
| 55 |
+
Qwen2VLForConditionalGeneration.forward = qwen2_vl_forward_new
|
| 56 |
+
Qwen2_5_VLForConditionalGeneration.forward = qwen2_vl_forward_new
|
| 57 |
+
else:
|
| 58 |
+
from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import Qwen2_5_VLForConditionalGeneration
|
| 59 |
+
from transformers.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLForConditionalGeneration
|
| 60 |
+
|
| 61 |
+
Qwen2VLForConditionalGeneration.forward = qwen2_vl_forward_old
|
| 62 |
+
Qwen2_5_VLForConditionalGeneration.forward = qwen2_vl_forward_old
|
| 63 |
+
else:
|
| 64 |
+
raise NotImplementedError(f"Model architecture {model_type} is not supported yet.")
|