[core] Fix attn_implementation setter with missing sub_configs (#39855)
* fix * add sub_configs * remove case for attention setter * fix None * Add test * Fix sub-configs * fix tests_config * fix consistency * fix fsmt * fix
This commit is contained in:
committed by
GitHub
parent
2a9febd632
commit
16d6faef9a
@@ -253,6 +253,14 @@ class ConditionalDetrConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
class ConditionalDetrOnnxConfig(OnnxConfig):
|
||||
torch_onnx_minimum_version = version.parse("1.11")
|
||||
|
||||
@@ -404,6 +404,14 @@ class DFineConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_configs(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`DFineConfig`] (or a derived class) from a pre-trained backbone model configuration and DETR model
|
||||
|
||||
@@ -423,6 +423,14 @@ class DFineConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_configs(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`DFineConfig`] (or a derived class) from a pre-trained backbone model configuration and DETR model
|
||||
|
||||
@@ -256,5 +256,13 @@ class DabDetrConfig(PretrainedConfig):
|
||||
self.initializer_bias_prior_prob = initializer_bias_prior_prob
|
||||
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["DabDetrConfig"]
|
||||
|
||||
@@ -278,5 +278,13 @@ class DeformableDetrConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["DeformableDetrConfig"]
|
||||
|
||||
@@ -266,5 +266,13 @@ class DetaConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["DetaConfig"]
|
||||
|
||||
@@ -168,5 +168,13 @@ class ViTHybridConfig(PretrainedConfig):
|
||||
self.num_channels = num_channels
|
||||
self.qkv_bias = qkv_bias
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["ViTHybridConfig"]
|
||||
|
||||
@@ -151,6 +151,14 @@ class DepthAnythingConfig(PretrainedConfig):
|
||||
self.depth_estimation_type = depth_estimation_type
|
||||
self.max_depth = max_depth if max_depth else 1
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns:
|
||||
|
||||
@@ -252,6 +252,14 @@ class DetrConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_config(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`DetrConfig`] (or a derived class) from a pre-trained backbone model configuration.
|
||||
|
||||
@@ -28,8 +28,8 @@ class DecoderConfig(PretrainedConfig):
|
||||
|
||||
model_type = "fsmt_decoder"
|
||||
|
||||
def __init__(self, vocab_size=0, bos_token_id=0, is_encoder_decoder=True):
|
||||
super().__init__()
|
||||
def __init__(self, vocab_size=0, bos_token_id=0, is_encoder_decoder=True, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.vocab_size = vocab_size
|
||||
self.bos_token_id = bos_token_id
|
||||
self.is_encoder_decoder = is_encoder_decoder
|
||||
@@ -134,6 +134,7 @@ class FSMTConfig(PretrainedConfig):
|
||||
|
||||
model_type = "fsmt"
|
||||
attribute_map = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}
|
||||
sub_configs = {"decoder": DecoderConfig}
|
||||
|
||||
# update the defaults from config file
|
||||
def __init__(
|
||||
|
||||
@@ -294,5 +294,16 @@ class GroundingDinoConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
sub_configs = {}
|
||||
backbone_config = getattr(self, "backbone_config", None)
|
||||
text_config = getattr(self, "text_config", None)
|
||||
if isinstance(backbone_config, PretrainedConfig):
|
||||
sub_configs["backbone_config"] = type(backbone_config)
|
||||
if isinstance(text_config, PretrainedConfig):
|
||||
sub_configs["text_config"] = type(self.text_config)
|
||||
return sub_configs
|
||||
|
||||
|
||||
__all__ = ["GroundingDinoConfig"]
|
||||
|
||||
@@ -236,6 +236,14 @@ class Mask2FormerConfig(PretrainedConfig):
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_config(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`Mask2FormerConfig`] (or a derived class) from a pre-trained backbone model configuration.
|
||||
|
||||
@@ -200,6 +200,15 @@ class MaskFormerConfig(PretrainedConfig):
|
||||
self.backbone_kwargs = backbone_kwargs
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
sub_configs = {}
|
||||
if self.backbone_config is not None and self.backbone_config != {}:
|
||||
sub_configs["backbone_config"] = type(self.backbone_config)
|
||||
if self.decoder_config is not None and self.decoder_config != {}:
|
||||
sub_configs["decoder_config"] = type(self.decoder_config)
|
||||
return sub_configs
|
||||
|
||||
@classmethod
|
||||
def from_backbone_and_decoder_configs(
|
||||
cls, backbone_config: PretrainedConfig, decoder_config: PretrainedConfig, **kwargs
|
||||
|
||||
@@ -288,5 +288,16 @@ class MMGroundingDinoConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
sub_configs = {}
|
||||
backbone_config = getattr(self, "backbone_config", None)
|
||||
text_config = getattr(self, "text_config", None)
|
||||
if isinstance(backbone_config, PretrainedConfig):
|
||||
sub_configs["backbone_config"] = type(backbone_config)
|
||||
if isinstance(text_config, PretrainedConfig):
|
||||
sub_configs["text_config"] = type(self.text_config)
|
||||
return sub_configs
|
||||
|
||||
|
||||
__all__ = ["MMGroundingDinoConfig"]
|
||||
|
||||
@@ -289,5 +289,16 @@ class OmDetTurboConfig(PretrainedConfig):
|
||||
|
||||
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
sub_configs = {}
|
||||
backbone_config = getattr(self, "backbone_config", None)
|
||||
text_config = getattr(self, "text_config", None)
|
||||
if isinstance(backbone_config, PretrainedConfig):
|
||||
sub_configs["backbone_config"] = type(backbone_config)
|
||||
if isinstance(text_config, PretrainedConfig):
|
||||
sub_configs["text_config"] = type(text_config)
|
||||
return sub_configs
|
||||
|
||||
|
||||
__all__ = ["OmDetTurboConfig"]
|
||||
|
||||
@@ -273,5 +273,13 @@ class OneFormerConfig(PretrainedConfig):
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["OneFormerConfig"]
|
||||
|
||||
@@ -293,6 +293,7 @@ class Pix2StructConfig(PretrainedConfig):
|
||||
```"""
|
||||
|
||||
model_type = "pix2struct"
|
||||
sub_configs = {"text_config": Pix2StructTextConfig, "vision_config": Pix2StructVisionConfig}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
@@ -154,6 +154,14 @@ class PromptDepthAnythingConfig(PretrainedConfig):
|
||||
self.depth_estimation_type = depth_estimation_type
|
||||
self.max_depth = max_depth if max_depth else 1
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns:
|
||||
|
||||
@@ -343,6 +343,14 @@ class RTDetrConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_configs(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`RTDetrConfig`] (or a derived class) from a pre-trained backbone model configuration and DETR model
|
||||
|
||||
@@ -358,6 +358,14 @@ class RTDetrV2Config(PretrainedConfig):
|
||||
self.decoder_offset_scale = decoder_offset_scale
|
||||
self.decoder_method = decoder_method
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_configs(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`RTDetrV2Config`] (or a derived class) from a pre-trained backbone model configuration and DETR model
|
||||
|
||||
@@ -369,6 +369,14 @@ class RTDetrV2Config(PretrainedConfig):
|
||||
self.decoder_offset_scale = decoder_offset_scale
|
||||
self.decoder_method = decoder_method
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_configs(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`RTDetrV2Config`] (or a derived class) from a pre-trained backbone model configuration and DETR model
|
||||
|
||||
@@ -114,5 +114,9 @@ class SuperGlueConfig(PretrainedConfig):
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return {"keypoint_detector_config": type(self.keypoint_detector_config)}
|
||||
|
||||
|
||||
__all__ = ["SuperGlueConfig"]
|
||||
|
||||
@@ -253,6 +253,14 @@ class TableTransformerConfig(PretrainedConfig):
|
||||
def hidden_size(self) -> int:
|
||||
return self.d_model
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
# Copied from transformers.models.detr.configuration_detr.DetrOnnxConfig
|
||||
class TableTransformerOnnxConfig(OnnxConfig):
|
||||
|
||||
@@ -172,6 +172,14 @@ class TvpConfig(PretrainedConfig):
|
||||
self.initializer_range = initializer_range
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_backbone_config(cls, backbone_config: PretrainedConfig, **kwargs):
|
||||
"""Instantiate a [`TvpConfig`] (or a derived class) from a pre-trained backbone model configuration.
|
||||
|
||||
@@ -136,5 +136,13 @@ class UperNetConfig(PretrainedConfig):
|
||||
self.auxiliary_concat_input = auxiliary_concat_input
|
||||
self.loss_ignore_index = loss_ignore_index
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["UperNetConfig"]
|
||||
|
||||
@@ -121,6 +121,14 @@ class VitMatteConfig(PretrainedConfig):
|
||||
self.convstream_hidden_sizes = convstream_hidden_sizes
|
||||
self.fusion_hidden_sizes = fusion_hidden_sizes
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns:
|
||||
|
||||
@@ -122,5 +122,13 @@ class VitPoseConfig(PretrainedConfig):
|
||||
self.scale_factor = scale_factor
|
||||
self.use_simple_decoder = use_simple_decoder
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["VitPoseConfig"]
|
||||
|
||||
@@ -233,5 +233,13 @@ class ZoeDepthConfig(PretrainedConfig):
|
||||
self.patch_transformer_intermediate_size = patch_transformer_intermediate_size
|
||||
self.patch_transformer_num_attention_heads = patch_transformer_num_attention_heads
|
||||
|
||||
@property
|
||||
def sub_configs(self):
|
||||
return (
|
||||
{"backbone_config": type(self.backbone_config)}
|
||||
if getattr(self, "backbone_config", None) is not None
|
||||
else {}
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["ZOEDEPTH_PRETRAINED_CONFIG_ARCHIVE_MAP", "ZoeDepthConfig"]
|
||||
|
||||
@@ -141,6 +141,7 @@ class ConfigTester:
|
||||
# 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)
|
||||
general_config_dict[sub_config_key].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
|
||||
|
||||
@@ -4812,6 +4812,25 @@ class ModelTesterMixin:
|
||||
f"All parameters should be on meta device, but found {unique_devices}.",
|
||||
)
|
||||
|
||||
def test_config_attn_implementation_setter(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
def check_attn_implementation_setter(config: PretrainedConfig, attn_implementation: str):
|
||||
if not config._attn_implementation == attn_implementation:
|
||||
raise ValueError(
|
||||
f"Unexpected attn_implementation for config {config.__class__.__name__}: "
|
||||
f"{config._attn_implementation} != {attn_implementation}"
|
||||
)
|
||||
for attribute_value in config.__dict__.values():
|
||||
if isinstance(attribute_value, PretrainedConfig):
|
||||
check_attn_implementation_setter(attribute_value, attn_implementation)
|
||||
|
||||
config._attn_implementation = "eager"
|
||||
check_attn_implementation_setter(config, "eager")
|
||||
|
||||
config._attn_implementation = "sdpa"
|
||||
check_attn_implementation_setter(config, "sdpa")
|
||||
|
||||
def test_internal_model_config_and_subconfig_are_same(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
subconfig_keys = list(config.sub_configs.keys())
|
||||
|
||||
Reference in New Issue
Block a user