Skip to content

Commit

Permalink
Use self.config_tester.run_common_tests() (huggingface#31431)
Browse files Browse the repository at this point in the history
* First testing updating config tests

* Use run_common_tests
  • Loading branch information
amyeroberts authored Jun 19, 2024
1 parent 7c71b61 commit 609e662
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 187 deletions.
2 changes: 1 addition & 1 deletion src/transformers/models/align/configuration_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class AlignVisionConfig(PretrainedConfig):
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
The non-linear activation function (function or string) in each block. If string, `"gelu"`, `"relu"`,
`"selu", `"gelu_new"`, `"silu"` and `"mish"` are supported.
hiddem_dim (`int`, *optional*, defaults to 1280):
hidden_dim (`int`, *optional*, defaults to 1280):
The hidden dimension of the layer before the classification head.
pooling_type (`str` or `function`, *optional*, defaults to `"mean"`):
Type of final pooling to be applied before the dense classification head. Available options are [`"mean"`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ def __init__(
self.features_only = features_only
self.use_pretrained_backbone = use_pretrained_backbone
self.use_timm_backbone = True
self.out_indices = out_indices if out_indices is not None else (-1,)
self.out_indices = out_indices if out_indices is not None else [-1]
self.freeze_batch_norm_2d = freeze_batch_norm_2d
13 changes: 6 additions & 7 deletions tests/models/align/test_modeling_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,15 @@ class AlignVisionModelTest(ModelTesterMixin, unittest.TestCase):
def setUp(self):
self.model_tester = AlignVisionModelTester(self)
self.config_tester = ConfigTester(
self, config_class=AlignVisionConfig, has_text_modality=False, hidden_size=37
self,
config_class=AlignVisionConfig,
has_text_modality=False,
hidden_size=37,
common_properties=["num_channels", "image_size"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="AlignVisionModel does not use inputs_embeds")
def test_inputs_embeds(self):
Expand Down
11 changes: 4 additions & 7 deletions tests/models/bit/test_modeling_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,12 @@ class BitModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):

def setUp(self):
self.model_tester = BitModelTester(self)
self.config_tester = ConfigTester(self, config_class=BitConfig, has_text_modality=False)
self.config_tester = ConfigTester(
self, config_class=BitConfig, has_text_modality=False, common_properties=["num_channels"]
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="Bit does not output attentions")
def test_attention_outputs(self):
Expand Down
15 changes: 8 additions & 7 deletions tests/models/convnext/test_modeling_convnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,16 @@ class ConvNextModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase

def setUp(self):
self.model_tester = ConvNextModelTester(self)
self.config_tester = ConfigTester(self, config_class=ConvNextConfig, has_text_modality=False, hidden_size=37)
self.config_tester = ConfigTester(
self,
config_class=ConvNextConfig,
has_text_modality=False,
hidden_size=37,
common_properties=["num_channels", "hidden_sizes"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="ConvNext does not use inputs_embeds")
def test_inputs_embeds(self):
Expand Down
15 changes: 8 additions & 7 deletions tests/models/convnextv2/test_modeling_convnextv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,16 @@ class ConvNextV2ModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCa

def setUp(self):
self.model_tester = ConvNextV2ModelTester(self)
self.config_tester = ConfigTester(self, config_class=ConvNextV2Config, has_text_modality=False, hidden_size=37)
self.config_tester = ConfigTester(
self,
config_class=ConvNextV2Config,
has_text_modality=False,
hidden_size=37,
common_properties=["hidden_sizes", "num_channels"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="ConvNextV2 does not use inputs_embeds")
def test_inputs_embeds(self):
Expand Down
7 changes: 1 addition & 6 deletions tests/models/cpmant/test_modeling_cpmant.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,7 @@ def setUp(self):
self.config_tester = ConfigTester(self, config_class=CpmAntConfig)

def test_config(self):
self.config_tester.create_and_test_config_common_properties()
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

def test_inputs_embeds(self):
unittest.skip("CPMAnt doesn't support input_embeds.")(self.test_inputs_embeds)
Expand Down
15 changes: 8 additions & 7 deletions tests/models/cvt/test_modeling_cvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ class CvtModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):

def setUp(self):
self.model_tester = CvtModelTester(self)
self.config_tester = ConfigTester(self, config_class=CvtConfig, has_text_modality=False, hidden_size=37)
self.config_tester = ConfigTester(
self,
config_class=CvtConfig,
has_text_modality=False,
hidden_size=37,
common_properties=["hidden_size", "num_channels"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="Cvt does not output attentions")
def test_attention_outputs(self):
Expand Down
14 changes: 7 additions & 7 deletions tests/models/deformable_detr/test_modeling_deformable_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):

def setUp(self):
self.model_tester = DeformableDetrModelTester(self)
self.config_tester = ConfigTester(self, config_class=DeformableDetrConfig, has_text_modality=False)
self.config_tester = ConfigTester(
self,
config_class=DeformableDetrConfig,
has_text_modality=False,
common_properties=["num_channels", "d_model", "encoder_attention_heads", "decoder_attention_heads"],
)

def test_config(self):
# we don't test common_properties and arguments_init as these don't apply for Deformable DETR
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.run_common_tests()

def test_deformable_detr_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
14 changes: 6 additions & 8 deletions tests/models/depth_anything/test_modeling_depth_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,15 @@ class DepthAnythingModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Tes
def setUp(self):
self.model_tester = DepthAnythingModelTester(self)
self.config_tester = ConfigTester(
self, config_class=DepthAnythingConfig, has_text_modality=False, hidden_size=37
self,
config_class=DepthAnythingConfig,
has_text_modality=False,
hidden_size=37,
common_properties=["patch_size"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_from_and_save_pretrained_subfolder()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="Depth Anything with AutoBackbone does not have a base model and hence no input_embeddings")
def test_inputs_embeds(self):
Expand Down
11 changes: 4 additions & 7 deletions tests/models/dinat/test_modeling_dinat.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,12 @@ class DinatModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):

def setUp(self):
self.model_tester = DinatModelTester(self)
self.config_tester = ConfigTester(self, config_class=DinatConfig, embed_dim=37)
self.config_tester = ConfigTester(
self, config_class=DinatConfig, embed_dim=37, common_properties=["patch_size", "num_channels"]
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

def test_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
15 changes: 8 additions & 7 deletions tests/models/donut/test_modeling_donut_swin.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ class DonutSwinModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCas

def setUp(self):
self.model_tester = DonutSwinModelTester(self)
self.config_tester = ConfigTester(self, config_class=DonutSwinConfig, embed_dim=37)
self.config_tester = ConfigTester(
self,
config_class=DonutSwinConfig,
has_text_modality=False,
embed_dim=37,
common_properties=["image_size", "patch_size", "num_channels"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

def test_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
13 changes: 6 additions & 7 deletions tests/models/efficientnet/test_modeling_efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,15 @@ class EfficientNetModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Test
def setUp(self):
self.model_tester = EfficientNetModelTester(self)
self.config_tester = ConfigTester(
self, config_class=EfficientNetConfig, has_text_modality=False, hidden_size=37
self,
config_class=EfficientNetConfig,
has_text_modality=False,
hidden_size=37,
common_properties=["num_channels", "image_size", "hidden_dim"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="EfficientNet does not use inputs_embeds")
def test_inputs_embeds(self):
Expand Down
15 changes: 8 additions & 7 deletions tests/models/focalnet/test_modeling_focalnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,16 @@ class FocalNetModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase

def setUp(self):
self.model_tester = FocalNetModelTester(self)
self.config_tester = ConfigTester(self, config_class=FocalNetConfig, embed_dim=37, has_text_modality=False)
self.config_tester = ConfigTester(
self,
config_class=FocalNetConfig,
embed_dim=37,
has_text_modality=False,
common_properties=["image_size", "patch_size", "num_channels", "hidden_sizes"],
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

def test_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
14 changes: 7 additions & 7 deletions tests/models/grounding_dino/test_modeling_grounding_dino.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):

def setUp(self):
self.model_tester = GroundingDinoModelTester(self)
self.config_tester = ConfigTester(self, config_class=GroundingDinoConfig, has_text_modality=False)
self.config_tester = ConfigTester(
self,
config_class=GroundingDinoConfig,
has_text_modality=False,
common_properties=["d_model", "encoder_attention_heads", "decoder_attention_heads"],
)

def test_config(self):
# we don't test common_properties and arguments_init as these don't apply for Grounding DINO
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.run_common_tests()

def test_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
11 changes: 4 additions & 7 deletions tests/models/levit/test_modeling_levit.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,12 @@ class LevitModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):

def setUp(self):
self.model_tester = LevitModelTester(self)
self.config_tester = ConfigTester(self, config_class=LevitConfig, has_text_modality=False, hidden_size=37)
self.config_tester = ConfigTester(
self, config_class=LevitConfig, has_text_modality=False, common_properties=["image_size", "num_channels"]
)

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

@unittest.skip(reason="Levit does not use inputs_embeds")
def test_inputs_embeds(self):
Expand Down
15 changes: 8 additions & 7 deletions tests/models/maskformer/test_modeling_maskformer_swin.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ class MaskFormerSwinModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Te

def setUp(self):
self.model_tester = MaskFormerSwinModelTester(self)
self.config_tester = ConfigTester(self, config_class=MaskFormerSwinConfig, embed_dim=37)
self.config_tester = ConfigTester(
self,
config_class=MaskFormerSwinConfig,
has_text_modality=False,
embed_dim=37,
common_properties=["image_size", "patch_size", "num_channels"],
)

@require_torch_multi_gpu
@unittest.skip(
Expand All @@ -197,12 +203,7 @@ def test_multi_gpu_data_parallel_forward(self):
pass

def test_config(self):
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.check_config_arguments_init()
self.config_tester.run_common_tests()

def test_model(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs()
Expand Down
14 changes: 7 additions & 7 deletions tests/models/perceiver/test_modeling_perceiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ class PerceiverModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCas

def setUp(self):
self.model_tester = PerceiverModelTester(self)
self.config_tester = ConfigTester(self, config_class=PerceiverConfig, hidden_size=37)
self.config_tester = ConfigTester(
self,
config_class=PerceiverConfig,
hidden_size=37,
common_properties=["d_model", "num_self_attention_heads", "num_cross_attention_heads"],
)

def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):
inputs_dict = copy.deepcopy(inputs_dict)
Expand Down Expand Up @@ -344,12 +349,7 @@ def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):
return inputs_dict

def test_config(self):
# we don't test common_properties and arguments_init as these don't apply for Perceiver
self.config_tester.create_and_test_config_to_json_string()
self.config_tester.create_and_test_config_to_json_file()
self.config_tester.create_and_test_config_from_and_save_pretrained()
self.config_tester.create_and_test_config_with_num_labels()
self.config_tester.check_config_can_be_init_without_params()
self.config_tester.run_common_tests()

def test_for_masked_lm(self):
config_and_inputs = self.model_tester.prepare_config_and_inputs(model_class=PerceiverForMaskedLM)
Expand Down
Loading

0 comments on commit 609e662

Please sign in to comment.