Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/transformers/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,17 @@ def wrapped_forward(*args, **kwargs):
for name, module in self.named_modules():
for key, specs in capture_tasks:
# The second check is for multimodals where only backbone layer suffix is available
if (specs.target_class is not None and isinstance(module, specs.target_class)) or (
specs.class_name is not None and name.endswith(specs.class_name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line of check for class_name was added for AyaVision, but I think it's not needed anymore. At least tests don't complain 😄

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aah :D makes sense, wdyt of the added check though? not sure how to handle reloading more elegantly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the best we can do with importlib.reload and shouldn't backfire with false positives. Lemme approve, forgot to

):
if specs.target_class is not None:
target_cls = specs.target_class
module_cls = module.__class__
matches_target_class = isinstance(module, target_cls) or (
module_cls.__name__ == target_cls.__name__
and module_cls.__module__ == target_cls.__module__
) # this is to make sure we attach hooks in case of module reloading
else:
matches_target_class = False

if matches_target_class or (specs.class_name is not None and name.endswith(specs.class_name)):
if specs.layer_name is not None and specs.layer_name not in name:
continue
# Monkey patch forward
Expand Down
30 changes: 30 additions & 0 deletions tests/utils/test_modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import copy
import glob
import importlib
import json
import os
import os.path
Expand All @@ -34,6 +35,7 @@
from parameterized import parameterized
from pytest import mark

import transformers.models.clip.modeling_clip as modeling_clip
from transformers import (
AutoConfig,
AutoModel,
Expand All @@ -43,6 +45,8 @@
BartForConditionalGeneration,
BartModel,
CLIPTextModelWithProjection,
CLIPVisionConfig,
CLIPVisionModel,
DynamicCache,
GPT2Config,
GPT2LMHeadModel,
Expand Down Expand Up @@ -3735,3 +3739,29 @@ def test_vision_language_model(self):
assert image_encoder is model.model.vision_tower, (
f"LLaVA get_encoder(modality='image') should return vision_tower, got {type(image_encoder)}"
)


@require_torch
class TestCheckModelInputsReload(unittest.TestCase):
# See https://github.com/linkedin/Liger-Kernel/pull/1061 and
# https://github.com/huggingface/transformers/issues/43761
def test_hidden_states_after_module_reload(self):
config = CLIPVisionConfig(
hidden_size=32,
intermediate_size=64,
num_hidden_layers=2,
num_attention_heads=4,
image_size=30,
patch_size=10,
)
pixel_values = torch.randn(1, 3, 30, 30, device=torch_device)

model = CLIPVisionModel(config).to(torch_device)
outputs = model(pixel_values=pixel_values, output_hidden_states=True)
self.assertIsNotNone(outputs.hidden_states)

importlib.reload(modeling_clip)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can move it under test_modeling_common.py, for make smth similar to this test and reload the module at some point before checking if config.output_attentions works?

def test_attention_outputs(self):
if not self.has_attentions:
self.skipTest(reason="Model does not output attentions")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I hesitated, I didn't want to add yet another test for 400+ models 🫣 but you may be right

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha right!


model_after_reload = CLIPVisionModel(config).to(torch_device)
outputs_after_reload = model_after_reload(pixel_values=pixel_values, output_hidden_states=True)
self.assertIsNotNone(outputs_after_reload.hidden_states)