Skip to content

Commit be3fa93

Browse files
authored
Subconfig is a class attribute (#41308)
* delete * fix this test * fix copies * oke, more tests to fix * fix last tests on DPT * deleted accidentally
1 parent 8137dbd commit be3fa93

32 files changed

+291
-616
lines changed

src/transformers/configuration_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,6 @@ def to_diff_dict(self) -> dict[str, Any]:
880880
isinstance(getattr(self, key, None), PreTrainedConfig)
881881
and key in class_config_dict
882882
and isinstance(class_config_dict[key], dict)
883-
or key in self.sub_configs
884883
):
885884
# For nested configs we need to clean the diff recursively
886885
diff = recursive_diff_dict(value, default_config_dict, config_obj=getattr(self, key, None))

src/transformers/modeling_utils.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,13 +1219,13 @@ def _get_dtype(
12191219
dtype = getattr(torch, dtype)
12201220
config.dtype = dtype
12211221
for sub_config_key in config.sub_configs:
1222-
sub_config = getattr(config, sub_config_key)
1223-
sub_config.dtype = dtype
1222+
if (sub_config := getattr(config, sub_config_key)) is not None:
1223+
sub_config.dtype = dtype
12241224
elif isinstance(dtype, torch.dtype):
12251225
config.dtype = dtype
12261226
for sub_config_key in config.sub_configs:
1227-
sub_config = getattr(config, sub_config_key)
1228-
sub_config.dtype = dtype
1227+
if (sub_config := getattr(config, sub_config_key)) is not None:
1228+
sub_config.dtype = dtype
12291229
elif isinstance(dtype, dict):
12301230
for key, curr_dtype in dtype.items():
12311231
if hasattr(config, key):
@@ -1250,8 +1250,8 @@ def _get_dtype(
12501250
default_dtype = torch.get_default_dtype()
12511251
config.dtype = default_dtype
12521252
for key in config.sub_configs:
1253-
value = getattr(config, key)
1254-
value.dtype = default_dtype
1253+
if (sub_config := getattr(config, key)) is not None:
1254+
sub_config.dtype = default_dtype
12551255

12561256
return config, dtype, dtype_orig
12571257

@@ -2700,34 +2700,34 @@ def set_attn_implementation(self, attn_implementation: Union[str, dict]):
27002700

27012701
# We need this as some old and badly designed models use subconfigs without declaring the corresponding modules as PreTrainedModel
27022702
for subconfig_key in self.config.sub_configs:
2703-
subconfig = getattr(self.config, subconfig_key)
2704-
sub_implementation = (
2705-
requested_implementation
2706-
if not isinstance(attn_implementation, dict)
2707-
else attn_implementation.get(subconfig_key, subconfig._attn_implementation)
2708-
)
2709-
# This means we did not perform any check above for this particular subconfig -> set it in the dark if it is registered
2710-
if (
2711-
not hasattr(subconfig, "_attn_was_changed")
2712-
# If it's already the same, then no need to enter here and raise warnings
2713-
and sub_implementation != subconfig._attn_implementation
2714-
):
2715-
if sub_implementation not in ["eager"] + ALL_ATTENTION_FUNCTIONS.valid_keys():
2716-
raise ValueError(
2717-
f'Specified `attn_implementation="{sub_implementation}"` is not supported for {subconfig_key}. '
2718-
'The only possible arguments are "eager" (manual attention implementation)'
2719-
f"or one of the following: {list(ALL_ATTENTION_FUNCTIONS.valid_keys())}"
2720-
)
2721-
subconfig._attn_implementation_internal = sub_implementation
2722-
logger.warning(
2723-
f"We set the attention implementation for the sub-config `{subconfig_key}` to `{sub_implementation}` "
2724-
"without finding the associated sub-model. For this reason we could not check if the model supports it. "
2725-
"You may encounter undefined behavior."
2703+
if (subconfig := getattr(self.config, subconfig_key)) is not None:
2704+
sub_implementation = (
2705+
requested_implementation
2706+
if not isinstance(attn_implementation, dict)
2707+
else attn_implementation.get(subconfig_key, subconfig._attn_implementation)
27262708
)
2727-
# Unset the attribute in this case, to avoid issues in the future
2728-
else:
2729-
if hasattr(subconfig, "_attn_was_changed"):
2730-
del subconfig._attn_was_changed
2709+
# This means we did not perform any check above for this particular subconfig -> set it in the dark if it is registered
2710+
if (
2711+
not hasattr(subconfig, "_attn_was_changed")
2712+
# If it's already the same, then no need to enter here and raise warnings
2713+
and sub_implementation != subconfig._attn_implementation
2714+
):
2715+
if sub_implementation not in ["eager"] + ALL_ATTENTION_FUNCTIONS.valid_keys():
2716+
raise ValueError(
2717+
f'Specified `attn_implementation="{sub_implementation}"` is not supported for {subconfig_key}. '
2718+
'The only possible arguments are "eager" (manual attention implementation)'
2719+
f"or one of the following: {list(ALL_ATTENTION_FUNCTIONS.valid_keys())}"
2720+
)
2721+
subconfig._attn_implementation_internal = sub_implementation
2722+
logger.warning(
2723+
f"We set the attention implementation for the sub-config `{subconfig_key}` to `{sub_implementation}` "
2724+
"without finding the associated sub-model. For this reason we could not check if the model supports it. "
2725+
"You may encounter undefined behavior."
2726+
)
2727+
# Unset the attribute in this case, to avoid issues in the future
2728+
else:
2729+
if hasattr(subconfig, "_attn_was_changed"):
2730+
del subconfig._attn_was_changed
27312731

27322732
def enable_input_require_grads(self):
27332733
"""

src/transformers/models/conditional_detr/configuration_conditional_detr.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from ...onnx import OnnxConfig
2424
from ...utils import logging
2525
from ...utils.backbone_utils import verify_backbone_config_arguments
26-
from ..auto import CONFIG_MAPPING
26+
from ..auto import CONFIG_MAPPING, AutoConfig
2727

2828

2929
logger = logging.get_logger(__name__)
@@ -135,6 +135,7 @@ class ConditionalDetrConfig(PreTrainedConfig):
135135
```"""
136136

137137
model_type = "conditional_detr"
138+
sub_configs = {"backbone_config": AutoConfig}
138139
keys_to_ignore_at_inference = ["past_key_values"]
139140
attribute_map = {
140141
"hidden_size": "d_model",
@@ -245,22 +246,6 @@ def __init__(
245246
self.focal_alpha = focal_alpha
246247
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
247248

248-
@property
249-
def num_attention_heads(self) -> int:
250-
return self.encoder_attention_heads
251-
252-
@property
253-
def hidden_size(self) -> int:
254-
return self.d_model
255-
256-
@property
257-
def sub_configs(self):
258-
return (
259-
{"backbone_config": type(self.backbone_config)}
260-
if getattr(self, "backbone_config", None) is not None
261-
else {}
262-
)
263-
264249

265250
class ConditionalDetrOnnxConfig(OnnxConfig):
266251
torch_onnx_minimum_version = version.parse("1.11")

src/transformers/models/d_fine/configuration_d_fine.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ...configuration_utils import PreTrainedConfig
2222
from ...utils import logging
2323
from ...utils.backbone_utils import verify_backbone_config_arguments
24-
from ..auto import CONFIG_MAPPING
24+
from ..auto import CONFIG_MAPPING, AutoConfig
2525

2626

2727
logger = logging.get_logger(__name__)
@@ -194,6 +194,7 @@ class DFineConfig(PreTrainedConfig):
194194
"""
195195

196196
model_type = "d_fine"
197+
sub_configs = {"backbone_config": AutoConfig}
197198
layer_types = ["basic", "bottleneck"]
198199
attribute_map = {
199200
"hidden_size": "d_model",
@@ -396,22 +397,6 @@ def __init__(
396397
)
397398
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
398399

399-
@property
400-
def num_attention_heads(self) -> int:
401-
return self.encoder_attention_heads
402-
403-
@property
404-
def hidden_size(self) -> int:
405-
return self.d_model
406-
407-
@property
408-
def sub_configs(self):
409-
return (
410-
{"backbone_config": type(self.backbone_config)}
411-
if getattr(self, "backbone_config", None) is not None
412-
else {}
413-
)
414-
415400
@classmethod
416401
def from_backbone_configs(cls, backbone_config: PreTrainedConfig, **kwargs):
417402
"""Instantiate a [`DFineConfig`] (or a derived class) from a pre-trained backbone model configuration and DETR model

src/transformers/models/d_fine/modular_d_fine.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ...image_transforms import corners_to_center_format
2626
from ...utils import is_torchdynamo_compiling, logging
2727
from ...utils.backbone_utils import verify_backbone_config_arguments
28-
from ..auto import CONFIG_MAPPING
28+
from ..auto import CONFIG_MAPPING, AutoConfig
2929
from ..rt_detr.modeling_rt_detr import (
3030
RTDetrConvNormLayer,
3131
RTDetrDecoder,
@@ -213,6 +213,7 @@ class DFineConfig(PreTrainedConfig):
213213
"""
214214

215215
model_type = "d_fine"
216+
sub_configs = {"backbone_config": AutoConfig}
216217
layer_types = ["basic", "bottleneck"]
217218
attribute_map = {
218219
"hidden_size": "d_model",
@@ -415,22 +416,6 @@ def __init__(
415416
)
416417
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
417418

418-
@property
419-
def num_attention_heads(self) -> int:
420-
return self.encoder_attention_heads
421-
422-
@property
423-
def hidden_size(self) -> int:
424-
return self.d_model
425-
426-
@property
427-
def sub_configs(self):
428-
return (
429-
{"backbone_config": type(self.backbone_config)}
430-
if getattr(self, "backbone_config", None) is not None
431-
else {}
432-
)
433-
434419
@classmethod
435420
def from_backbone_configs(cls, backbone_config: PreTrainedConfig, **kwargs):
436421
"""Instantiate a [`DFineConfig`] (or a derived class) from a pre-trained backbone model configuration and DETR model

src/transformers/models/dab_detr/configuration_dab_detr.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ...configuration_utils import PreTrainedConfig
1818
from ...utils import logging
1919
from ...utils.backbone_utils import verify_backbone_config_arguments
20-
from ..auto import CONFIG_MAPPING
20+
from ..auto import CONFIG_MAPPING, AutoConfig
2121

2222

2323
logger = logging.get_logger(__name__)
@@ -136,6 +136,7 @@ class DabDetrConfig(PreTrainedConfig):
136136
```"""
137137

138138
model_type = "dab-detr"
139+
sub_configs = {"backbone_config": AutoConfig}
139140
keys_to_ignore_at_inference = ["past_key_values"]
140141
attribute_map = {
141142
"num_attention_heads": "encoder_attention_heads",
@@ -256,13 +257,5 @@ def __init__(
256257
self.initializer_bias_prior_prob = initializer_bias_prior_prob
257258
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
258259

259-
@property
260-
def sub_configs(self):
261-
return (
262-
{"backbone_config": type(self.backbone_config)}
263-
if getattr(self, "backbone_config", None) is not None
264-
else {}
265-
)
266-
267260

268261
__all__ = ["DabDetrConfig"]

src/transformers/models/deformable_detr/configuration_deformable_detr.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from ...configuration_utils import PreTrainedConfig
1818
from ...utils import logging
1919
from ...utils.backbone_utils import verify_backbone_config_arguments
20-
from ..auto import CONFIG_MAPPING
20+
from ..auto import CONFIG_MAPPING, AutoConfig
2121

2222

2323
logger = logging.get_logger(__name__)
@@ -144,6 +144,7 @@ class DeformableDetrConfig(PreTrainedConfig):
144144
```"""
145145

146146
model_type = "deformable_detr"
147+
sub_configs = {"backbone_config": AutoConfig}
147148
attribute_map = {
148149
"hidden_size": "d_model",
149150
"num_attention_heads": "encoder_attention_heads",
@@ -270,21 +271,5 @@ def __init__(
270271
self.disable_custom_kernels = disable_custom_kernels
271272
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
272273

273-
@property
274-
def num_attention_heads(self) -> int:
275-
return self.encoder_attention_heads
276-
277-
@property
278-
def hidden_size(self) -> int:
279-
return self.d_model
280-
281-
@property
282-
def sub_configs(self):
283-
return (
284-
{"backbone_config": type(self.backbone_config)}
285-
if getattr(self, "backbone_config", None) is not None
286-
else {}
287-
)
288-
289274

290275
__all__ = ["DeformableDetrConfig"]

src/transformers/models/depth_anything/configuration_depth_anything.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
# limitations under the License.
1515
"""DepthAnything model configuration"""
1616

17-
import copy
18-
1917
from ...configuration_utils import PreTrainedConfig
2018
from ...utils import logging
2119
from ...utils.backbone_utils import verify_backbone_config_arguments
22-
from ..auto.configuration_auto import CONFIG_MAPPING
20+
from ..auto.configuration_auto import CONFIG_MAPPING, AutoConfig
2321

2422

2523
logger = logging.get_logger(__name__)
@@ -89,6 +87,7 @@ class DepthAnythingConfig(PreTrainedConfig):
8987
```"""
9088

9189
model_type = "depth_anything"
90+
sub_configs = {"backbone_config": AutoConfig}
9291

9392
def __init__(
9493
self,
@@ -151,26 +150,5 @@ def __init__(
151150
self.depth_estimation_type = depth_estimation_type
152151
self.max_depth = max_depth if max_depth else 1
153152

154-
@property
155-
def sub_configs(self):
156-
return (
157-
{"backbone_config": type(self.backbone_config)}
158-
if getattr(self, "backbone_config", None) is not None
159-
else {}
160-
)
161-
162-
def to_dict(self):
163-
"""
164-
Serializes this instance to a Python dictionary. Override the default [`~PreTrainedConfig.to_dict`]. Returns:
165-
`dict[str, any]`: Dictionary of all the attributes that make up this configuration instance,
166-
"""
167-
output = copy.deepcopy(self.__dict__)
168-
169-
if output["backbone_config"] is not None:
170-
output["backbone_config"] = self.backbone_config.to_dict()
171-
172-
output["model_type"] = self.__class__.model_type
173-
return output
174-
175153

176154
__all__ = ["DepthAnythingConfig"]

src/transformers/models/detr/configuration_detr.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from ...onnx import OnnxConfig
2424
from ...utils import logging
2525
from ...utils.backbone_utils import verify_backbone_config_arguments
26-
from ..auto import CONFIG_MAPPING
26+
from ..auto import CONFIG_MAPPING, AutoConfig
2727

2828

2929
logger = logging.get_logger(__name__)
@@ -133,6 +133,7 @@ class DetrConfig(PreTrainedConfig):
133133
```"""
134134

135135
model_type = "detr"
136+
sub_configs = {"backbone_config": AutoConfig}
136137
keys_to_ignore_at_inference = ["past_key_values"]
137138
attribute_map = {
138139
"hidden_size": "d_model",
@@ -244,22 +245,6 @@ def __init__(
244245
self.eos_coefficient = eos_coefficient
245246
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
246247

247-
@property
248-
def num_attention_heads(self) -> int:
249-
return self.encoder_attention_heads
250-
251-
@property
252-
def hidden_size(self) -> int:
253-
return self.d_model
254-
255-
@property
256-
def sub_configs(self):
257-
return (
258-
{"backbone_config": type(self.backbone_config)}
259-
if getattr(self, "backbone_config", None) is not None
260-
else {}
261-
)
262-
263248
@classmethod
264249
def from_backbone_config(cls, backbone_config: PreTrainedConfig, **kwargs):
265250
"""Instantiate a [`DetrConfig`] (or a derived class) from a pre-trained backbone model configuration.

0 commit comments

Comments
 (0)