Load sub-configs from composite configs (#34410)
* save/load sub-configs * nit forgot these * fix copies * move test to common * use dict for sub-configs * add load-save-laod test * clean up modeling check * oops this are correct keys * fix some tests, missed some composite configs * this model was missed
This commit is contained in:
committed by
GitHub
parent
5e1fd4e204
commit
893ad04fad
@@ -457,11 +457,20 @@ class AlignModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = AlignModelTester(self)
|
||||
self.config_tester = ConfigTester(
|
||||
self,
|
||||
config_class=AlignConfig,
|
||||
has_text_modality=False,
|
||||
common_properties=["projection_dim", "temperature_init_value"],
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Start to fail after using torch `cu118`.")
|
||||
def test_multi_gpu_data_parallel_forward(self):
|
||||
super().test_multi_gpu_data_parallel_forward()
|
||||
|
||||
@@ -452,11 +452,20 @@ class AltCLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = AltCLIPModelTester(self)
|
||||
self.config_tester = ConfigTester(
|
||||
self,
|
||||
config_class=AltCLIPConfig,
|
||||
has_text_modality=False,
|
||||
common_properties=["projection_dim", "logit_scale_init_value"],
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Hidden_states is tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -449,11 +449,18 @@ class BlipModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = BlipModelTester(self)
|
||||
common_properties = ["logit_scale_init_value", "image_text_hidden_size", "projection_dim", "label_smoothing"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=BlipConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Hidden_states is tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -482,6 +482,13 @@ class Blip2ForConditionalGenerationDecoderOnlyTest(ModelTesterMixin, GenerationT
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = Blip2ForConditionalGenerationDecoderOnlyModelTester(self)
|
||||
common_properties = ["image_token_index", "num_query_tokens", "image_text_hidden_size"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=Blip2Config, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_for_conditional_generation(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
|
||||
@@ -515,11 +515,18 @@ class ClapModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = ClapModelTester(self)
|
||||
common_properties = ["logit_scale_init_value", "projection_hidden_act", "projection_dim"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=ClapConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Hidden_states is tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -745,11 +745,18 @@ class CLIPModelTest(CLIPModelTesterMixin, PipelineTesterMixin, unittest.TestCase
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = CLIPModelTester(self)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=CLIPConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Hidden_states is tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -472,11 +472,18 @@ class CLIPSegModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = CLIPSegModelTester(self)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=CLIPSegConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model_for_image_segmentation(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model_for_image_segmentation(*config_and_inputs)
|
||||
|
||||
@@ -414,7 +414,13 @@ class ClvpModelForConditionalGenerationTest(ModelTesterMixin, unittest.TestCase)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = ClvpModelForConditionalGenerationTester(self)
|
||||
self.clvp_config_tester = ConfigTester(self, config_class=ClvpConfig, hidden_size=32)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value"]
|
||||
self.clvp_config_tester = ConfigTester(
|
||||
self, config_class=ClvpConfig, has_text_modality=False, common_properties=common_properties, hidden_size=32
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.clvp_config_tester.run_common_tests()
|
||||
|
||||
def tearDown(self):
|
||||
super().tearDown()
|
||||
|
||||
@@ -931,11 +931,18 @@ class FlavaModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = self.class_for_tester(self)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value", "init_codebook"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=FlavaConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -559,11 +559,18 @@ class GroupViTModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = GroupViTModelTester(self)
|
||||
common_properties = ["projection_dim", "projection_intermediate_dim", "logit_scale_init_value"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=GroupViTConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="hidden_states are tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -185,7 +185,12 @@ class Idefics2ModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = Idefics2VisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=Idefics2Config, has_text_modality=False)
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=Idefics2Config, has_text_modality=False, common_properties=["image_token_id"]
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="input_embeds cannot be passed in without input_ids")
|
||||
def test_inputs_embeds():
|
||||
|
||||
@@ -168,7 +168,12 @@ class Idefics3ModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = Idefics3VisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=Idefics3Config, has_text_modality=False)
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=Idefics3Config, has_text_modality=False, common_properties=["image_token_id"]
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="input_embeds cannot be passed in without input_ids")
|
||||
def test_inputs_embeds():
|
||||
|
||||
@@ -486,6 +486,15 @@ class InstructBlipForConditionalGenerationDecoderOnlyTest(ModelTesterMixin, Gene
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = InstructBlipForConditionalGenerationDecoderOnlyModelTester(self)
|
||||
self.config_tester = ConfigTester(
|
||||
self,
|
||||
config_class=InstructBlipConfig,
|
||||
has_text_modality=False,
|
||||
common_properties=["num_query_tokens", "image_token_index"],
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_for_conditional_generation(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
|
||||
@@ -510,11 +510,18 @@ class InstructBlipVideoForConditionalGenerationDecoderOnlyTest(
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = InstructBlipVideoForConditionalGenerationDecoderOnlyModelTester(self)
|
||||
common_properties = ["num_query_tokens", "video_token_index"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=InstructBlipVideoConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_for_conditional_generation(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_for_conditional_generation(*config_and_inputs)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="Hidden_states is tested in individual model tests")
|
||||
def test_hidden_states_output(self):
|
||||
pass
|
||||
|
||||
@@ -304,7 +304,12 @@ class Kosmos2ModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = Kosmos2ModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=Kosmos2Config, hidden_size=37)
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=Kosmos2Config, has_text_modality=False, common_properties=["latent_query_num"]
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
# overwrite from common to skip `image_to_text_projection.latent_query`
|
||||
def test_initialization(self):
|
||||
|
||||
@@ -194,7 +194,13 @@ class LlavaForConditionalGenerationModelTest(ModelTesterMixin, GenerationTesterM
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = LlavaVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=LlavaConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "vision_feature_layer", "image_seq_length"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=LlavaConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
# overwrite inputs_embeds tests because we need to delete "pixel values" for LVLMs
|
||||
def test_inputs_embeds(self):
|
||||
|
||||
@@ -223,7 +223,13 @@ class LlavaNextForConditionalGenerationModelTest(ModelTesterMixin, GenerationTes
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = LlavaNextVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=LlavaNextConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "vision_feature_layer", "image_seq_length"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=LlavaNextConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_initialization(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
@@ -240,7 +240,13 @@ class LlavaNextVideoForConditionalGenerationModelTest(ModelTesterMixin, Generati
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = LlavaNextVideoVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=LlavaNextVideoConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "video_token_index", "vision_feature_layer", "image_seq_length"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=LlavaNextVideoConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_initialization(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
@@ -226,7 +226,13 @@ class LlavaOnevisionForConditionalGenerationModelTest(ModelTesterMixin, Generati
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = LlavaOnevisionVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=LlavaOnevisionConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "video_token_index", "vision_feature_layer"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=LlavaOnevisionConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_initialization(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
@@ -272,7 +272,12 @@ class MllamaForConditionalGenerationModelTest(ModelTesterMixin, GenerationTester
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = MllamaVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=MllamaConfig, has_text_modality=False)
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=MllamaConfig, has_text_modality=False, common_properties=["image_token_index"]
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
# overwrite inputs_embeds tests because we need to delete "pixel values" for LVLMs
|
||||
def test_inputs_embeds(self):
|
||||
|
||||
@@ -447,6 +447,13 @@ class Owlv2ModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = Owlv2ModelTester(self)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=Owlv2Config, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
|
||||
@@ -442,6 +442,13 @@ class OwlViTModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = OwlViTModelTester(self)
|
||||
common_properties = ["projection_dim", "logit_scale_init_value"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=OwlViTConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
|
||||
@@ -232,6 +232,9 @@ class Qwen2VLModelTest(ModelTesterMixin, GenerationTesterMixin, unittest.TestCas
|
||||
self.model_tester = Qwen2VLVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=Qwen2VLConfig, has_text_modality=False)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_initialization(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
|
||||
@@ -667,9 +667,12 @@ class SiglipModelTest(SiglipModelTesterMixin, PipelineTesterMixin, unittest.Test
|
||||
test_disk_offload_bin = False
|
||||
_is_composite = True
|
||||
|
||||
# Copied from tests.models.clip.test_modeling_clip.CLIPModelTest.setUp with CLIP->Siglip
|
||||
def setUp(self):
|
||||
self.model_tester = SiglipModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=SiglipConfig, has_text_modality=False)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
# Copied from tests.models.clip.test_modeling_clip.CLIPModelTest.test_model
|
||||
def test_model(self):
|
||||
|
||||
@@ -217,7 +217,13 @@ class VideoLlavaForConditionalGenerationModelTest(ModelTesterMixin, GenerationTe
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = VideoLlavaVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=VideoLlavaConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "video_token_index", "vision_feature_layer", "image_seq_length"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=VideoLlavaConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(
|
||||
reason="This architecure seem to not compute gradients properly when using GC, check: https://github.com/huggingface/transformers/pull/27124"
|
||||
|
||||
@@ -179,7 +179,13 @@ class VipLlavaForConditionalGenerationModelTest(ModelTesterMixin, GenerationTest
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = VipLlavaVisionText2TextModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=VipLlavaConfig, has_text_modality=False)
|
||||
common_properties = ["image_token_index", "vision_feature_layers", "image_seq_length"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=VipLlavaConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
# overwrite inputs_embeds tests because we need to delete "pixel values" for LVLMs
|
||||
def test_inputs_embeds(self):
|
||||
|
||||
@@ -547,6 +547,13 @@ class XCLIPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = XCLIPModelTester(self)
|
||||
common_properties = ["projection_dim", "prompt_layers", "prompt_num_attention_heads"]
|
||||
self.config_tester = ConfigTester(
|
||||
self, config_class=XCLIPConfig, has_text_modality=False, common_properties=common_properties
|
||||
)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
|
||||
@@ -17,12 +17,17 @@ import copy
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from transformers import is_torch_available
|
||||
from transformers.utils import direct_transformers_import
|
||||
|
||||
from .utils.test_configuration_utils import config_common_kwargs
|
||||
|
||||
|
||||
transformers_module = direct_transformers_import(Path(__file__).parent)
|
||||
|
||||
|
||||
class ConfigTester:
|
||||
def __init__(self, parent, config_class=None, has_text_modality=True, common_properties=None, **kwargs):
|
||||
self.parent = parent
|
||||
@@ -35,9 +40,10 @@ class ConfigTester:
|
||||
config = self.config_class(**self.inputs_dict)
|
||||
common_properties = (
|
||||
["hidden_size", "num_attention_heads", "num_hidden_layers"]
|
||||
if self.common_properties is None
|
||||
if self.common_properties is None and not self.config_class.sub_configs
|
||||
else self.common_properties
|
||||
)
|
||||
common_properties = [] if common_properties is None else common_properties
|
||||
|
||||
# Add common fields for text models
|
||||
if self.has_text_modality:
|
||||
@@ -110,6 +116,44 @@ class ConfigTester:
|
||||
|
||||
self.parent.assertEqual(config_second.to_dict(), config_first.to_dict())
|
||||
|
||||
def create_and_test_config_from_and_save_pretrained_composite(self):
|
||||
"""
|
||||
Tests that composite or nested cofigs can be loaded and saved correctly. In case the config
|
||||
has a sub-config, we should be able to call `sub_config.from_pretrained('general_config_file')`
|
||||
and get a result same as if we loaded the whole config and obtained `config.sub_config` from it.
|
||||
"""
|
||||
config = self.config_class(**self.inputs_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
config.save_pretrained(tmpdirname)
|
||||
general_config_loaded = self.config_class.from_pretrained(tmpdirname)
|
||||
general_config_dict = config.to_dict()
|
||||
|
||||
# Iterate over all sub_configs if there are any and load them with their own classes
|
||||
sub_configs = self.config_class.sub_configs
|
||||
for sub_config_key, sub_class in sub_configs.items():
|
||||
if sub_class.__name__ == "AutoConfig":
|
||||
sub_class = sub_class.for_model(**general_config_dict[sub_config_key]).__class__
|
||||
sub_config_loaded = sub_class.from_pretrained(tmpdirname)
|
||||
else:
|
||||
sub_config_loaded = sub_class.from_pretrained(tmpdirname)
|
||||
|
||||
# Pop `transformers_version`, it never exists when a config is part of a general composite config
|
||||
# Verify that loading with subconfig class results in same dict as if we loaded with general composite config class
|
||||
sub_config_loaded_dict = sub_config_loaded.to_dict()
|
||||
sub_config_loaded_dict.pop("transformers_version", None)
|
||||
self.parent.assertEqual(sub_config_loaded_dict, general_config_dict[sub_config_key])
|
||||
|
||||
# Verify that the loaded config type is same as in the general config
|
||||
type_from_general_config = type(getattr(general_config_loaded, sub_config_key))
|
||||
self.parent.assertTrue(isinstance(sub_config_loaded, type_from_general_config))
|
||||
|
||||
# Now save only the sub-config and load it back to make sure the whole load-save-load pipeline works
|
||||
with tempfile.TemporaryDirectory() as tmpdirname2:
|
||||
sub_config_loaded.save_pretrained(tmpdirname2)
|
||||
sub_config_loaded_2 = sub_class.from_pretrained(tmpdirname2)
|
||||
self.parent.assertEqual(sub_config_loaded.to_dict(), sub_config_loaded_2.to_dict())
|
||||
|
||||
def create_and_test_config_with_num_labels(self):
|
||||
config = self.config_class(**self.inputs_dict, num_labels=5)
|
||||
self.parent.assertEqual(len(config.id2label), 5)
|
||||
@@ -128,6 +172,9 @@ class ConfigTester:
|
||||
self.parent.assertIsNotNone(config)
|
||||
|
||||
def check_config_arguments_init(self):
|
||||
if self.config_class.sub_configs:
|
||||
return # TODO: @raushan composite models are not consistent in how they set general params
|
||||
|
||||
kwargs = copy.deepcopy(config_common_kwargs)
|
||||
config = self.config_class(**kwargs)
|
||||
wrong_values = []
|
||||
@@ -153,6 +200,7 @@ class ConfigTester:
|
||||
self.create_and_test_config_to_json_file()
|
||||
self.create_and_test_config_from_and_save_pretrained()
|
||||
self.create_and_test_config_from_and_save_pretrained_subfolder()
|
||||
self.create_and_test_config_from_and_save_pretrained_composite()
|
||||
self.create_and_test_config_with_num_labels()
|
||||
self.check_config_can_be_init_without_params()
|
||||
self.check_config_arguments_init()
|
||||
|
||||
@@ -3802,22 +3802,18 @@ class ModelTesterMixin:
|
||||
self.skipTest("Model is not a composite model.")
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
sub_configs = {
|
||||
key: getattr(config, key) for key in config if isinstance(getattr(config, key), PretrainedConfig)
|
||||
}
|
||||
|
||||
# set eager as it will be the one supported in all models
|
||||
# we just need to test if passing 'attn_implementation' as a dict fails or not
|
||||
attn_implementation_per_subconfig = {}
|
||||
for key, sub_config in sub_configs.items():
|
||||
for key in config.sub_configs.keys():
|
||||
attn_implementation_per_subconfig[key] = "eager"
|
||||
|
||||
config._attn_implementation = attn_implementation_per_subconfig
|
||||
model = model_class(config)
|
||||
for key in model.config:
|
||||
if isinstance(getattr(model.config, key), PretrainedConfig):
|
||||
sub_config = getattr(model.config, key)
|
||||
self.assertTrue(sub_config._attn_implementation == "eager")
|
||||
for key in config.sub_configs.keys():
|
||||
sub_config = getattr(model.config, key)
|
||||
self.assertTrue(sub_config._attn_implementation == "eager")
|
||||
|
||||
for name, submodule in model.named_modules():
|
||||
class_name = submodule.__class__.__name__
|
||||
@@ -3826,7 +3822,7 @@ class ModelTesterMixin:
|
||||
or "SdpaSelfAttention" in class_name
|
||||
or "FlashAttention" in class_name
|
||||
):
|
||||
raise ValueError("The eager model should not have SDPA/FA2 attention layers")
|
||||
raise ValueError(f"The eager model should not have SDPA/FA2 attention layers but got {class_name}")
|
||||
|
||||
@require_torch_sdpa
|
||||
def test_sdpa_can_dispatch_non_composite_models(self):
|
||||
|
||||
Reference in New Issue
Block a user