


'''simple docstring'''
from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


lowerCAmelCase   : Any             =				logging.get_logger(__name__)

# General docstring
lowerCAmelCase   : List[Any]             =				"""ResNetConfig"""

# Base docstring
lowerCAmelCase   : str             =				"""microsoft/resnet-50"""
lowerCAmelCase   : List[Any]             =				[1, 2_0_4_8, 7, 7]

# Image classification docstring
lowerCAmelCase   : str             =				"""microsoft/resnet-50"""
lowerCAmelCase   : List[Any]             =				"""tiger cat"""

lowerCAmelCase   : str             =				[
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_							,      a_ = 3							,      a_ = 1							,      a_ = "relu"							)    ->  Dict:
					super().__init__()
					lowercase						:		Optional[int]   					=		nn.Convad(
					    a_							,      a_							,      kernel_size=a_							,      stride=a_							,      padding=kernel_size // 2							,      bias=a_							)
					lowercase						:		List[Any]   					=		nn.BatchNormad(a_							)
					lowercase						:		Tuple   					=		ACTaFN[activation] if activation is not None else nn.Identity()


				def 	a__		(     self							,      a_							)    ->  Tensor:
					lowercase						:		Dict   					=		self.convolution(a_							)
					lowercase						:		str   					=		self.normalization(a_							)
					lowercase						:		Tuple   					=		self.activation(a_							)
					return hidden_state




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							)    ->  Any:
					super().__init__()
					lowercase						:		List[str]   					=		ResNetConvLayer(
					    config.num_channels							,      config.embedding_size							,      kernel_size=7							,      stride=2							,      activation=config.hidden_act							)
					lowercase						:		str   					=		nn.MaxPoolad(kernel_size=3							,      stride=2							,      padding=1							)
					lowercase						:		Union[str, Any]   					=		config.num_channels


				def 	a__		(     self							,      a_							)    ->  Tensor:
					lowercase						:		List[Any]   					=		pixel_values.shape[1]
					if num_channels != self.num_channels:
						raise ValueError(
						    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."							)
					lowercase						:		List[Any]   					=		self.embedder(a_							)
					lowercase						:		Tuple   					=		self.pooler(a_							)
					return embedding




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_							,      a_ = 2							)    ->  str:
					super().__init__()
					lowercase						:		Optional[int]   					=		nn.Convad(a_							,      a_							,      kernel_size=1							,      stride=a_							,      bias=a_							)
					lowercase						:		Optional[int]   					=		nn.BatchNormad(a_							)


				def 	a__		(     self							,      a_							)    ->  Tensor:
					lowercase						:		Tuple   					=		self.convolution(a_							)
					lowercase						:		str   					=		self.normalization(a_							)
					return hidden_state




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_							,      a_ = 1							,      a_ = "relu"							)    ->  Optional[Any]:
					super().__init__()
					lowercase						:		List[Any]   					=		in_channels != out_channels or stride != 1
					lowercase						:		Optional[int]   					=		(
					    ResNetShortCut(a_							,      a_							,      stride=a_							) if should_apply_shortcut else nn.Identity()
					)
					lowercase						:		Dict   					=		nn.Sequential(
					    ResNetConvLayer(a_							,      a_							,      stride=a_							)							,      ResNetConvLayer(a_							,      a_							,      activation=a_							)							,      )
					lowercase						:		int   					=		ACTaFN[activation]


				def 	a__		(     self							,      a_							)    ->  Any:
					lowercase						:		Any   					=		hidden_state
					lowercase						:		Optional[Any]   					=		self.layer(a_							)
					lowercase						:		Union[str, Any]   					=		self.shortcut(a_							)
					hidden_state += residual
					lowercase						:		List[str]   					=		self.activation(a_							)
					return hidden_state




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_							,      a_ = 1							,      a_ = "relu"							,      a_ = 4							)    ->  Union[str, Any]:
					super().__init__()
					lowercase						:		Union[str, Any]   					=		in_channels != out_channels or stride != 1
					lowercase						:		Any   					=		out_channels // reduction
					lowercase						:		int   					=		(
					    ResNetShortCut(a_							,      a_							,      stride=a_							) if should_apply_shortcut else nn.Identity()
					)
					lowercase						:		Optional[int]   					=		nn.Sequential(
					    ResNetConvLayer(a_							,      a_							,      kernel_size=1							)							,      ResNetConvLayer(a_							,      a_							,      stride=a_							)							,      ResNetConvLayer(a_							,      a_							,      kernel_size=1							,      activation=a_							)							,      )
					lowercase						:		Dict   					=		ACTaFN[activation]


				def 	a__		(     self							,      a_							)    ->  List[str]:
					lowercase						:		Optional[Any]   					=		hidden_state
					lowercase						:		str   					=		self.layer(a_							)
					lowercase						:		List[str]   					=		self.shortcut(a_							)
					hidden_state += residual
					lowercase						:		Tuple   					=		self.activation(a_							)
					return hidden_state




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							,      a_							,      a_							,      a_ = 2							,      a_ = 2							,      )    ->  str:
					super().__init__()

					lowercase						:		List[str]   					=		ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

					lowercase						:		Tuple   					=		nn.Sequential(
					    # downsampling is done in the first layer with stride of 2
					    layer(a_							,      a_							,      stride=a_							,      activation=config.hidden_act							)							,      *[layer(a_							,      a_							,      activation=config.hidden_act							) for _ in range(depth - 1							)]							,      )


				def 	a__		(     self							,      a_							)    ->  Tensor:
					lowercase						:		str   					=		input
					for layer in self.layers:
						lowercase						:		List[Any]   					=		layer(a_							)
					return hidden_state




class        _UpperCamelCase	(    nn.Module):


				'''simple docstring'''


				def __init__(     self							,      a_							)    ->  Union[str, Any]:
					super().__init__()
					lowercase						:		int   					=		nn.ModuleList([]							)
					# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
					self.stages.append(
					    ResNetStage(
					        a_							,      config.embedding_size							,      config.hidden_sizes[0]							,      stride=2 if config.downsample_in_first_stage else 1							,      depth=config.depths[0]							,      )							)
					lowercase						:		str   					=		zip(config.hidden_sizes							,      config.hidden_sizes[1:]							)
					for (in_channels, out_channels), depth in zip(a_							,      config.depths[1:]							):
						self.stages.append(ResNetStage(a_							,      a_							,      a_							,      depth=a_							)							)


				def 	a__		(     self							,      a_							,      a_ = False							,      a_ = True							)    ->  BaseModelOutputWithNoAttention:
					lowercase						:		List[Any]   					=		() if output_hidden_states else None

					for stage_module in self.stages:
						if output_hidden_states:
							lowercase						:		Tuple   					=		hidden_states + (hidden_state,)

						lowercase						:		Union[str, Any]   					=		stage_module(a_							)

					if output_hidden_states:
						lowercase						:		List[str]   					=		hidden_states + (hidden_state,)

					if not return_dict:
						return tuple(v for v in [hidden_state, hidden_states] if v is not None							)

					return BaseModelOutputWithNoAttention(
					    last_hidden_state=a_							,      hidden_states=a_							,      )




class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				_snake_case						       =				ResNetConfig
				_snake_case						       =				'''resnet'''
				_snake_case						       =				'''pixel_values'''
				_snake_case						       =				True

				def 	a__		(     self							,      a_							)    ->  int:
					if isinstance(a_							,      nn.Convad							):
						nn.init.kaiming_normal_(module.weight							,      mode="fan_out"							,      nonlinearity="relu"							)
					elif isinstance(a_							,      (nn.BatchNormad, nn.GroupNorm)							):
						nn.init.constant_(module.weight							,      1							)
						nn.init.constant_(module.bias							,      0							)


				def 	a__		(     self							,      a_							,      a_=False							)    ->  Optional[Any]:
					if isinstance(a_							,      a_							):
						lowercase						:		Union[str, Any]   					=		value


lowerCAmelCase   : Optional[Any]             =				R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

lowerCAmelCase   : List[Any]             =				R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""




@add_start_docstrings(
    '''The bare ResNet model outputting raw features without any specific head on top.'''						,			SCREAMING_SNAKE_CASE						,			)
class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				def __init__(     self							,      a_							)    ->  Optional[int]:
					super().__init__(a_							)
					lowercase						:		int   					=		config
					lowercase						:		int   					=		ResNetEmbeddings(a_							)
					lowercase						:		List[str]   					=		ResNetEncoder(a_							)
					lowercase						:		Union[str, Any]   					=		nn.AdaptiveAvgPoolad((1, 1)							)
					# Initialize weights and apply final processing
					self.post_init()


				@add_start_docstrings_to_model_forward(a_							)
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC							,      output_type=a_							,      config_class=_CONFIG_FOR_DOC							,      modality="vision"							,      expected_output=_EXPECTED_OUTPUT_SHAPE							,      )
				def 	a__		(     self							,      a_							,      a_ = None							,      a_ = None							)    ->  BaseModelOutputWithPoolingAndNoAttention:
					lowercase						:		Tuple   					=		(
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					lowercase						:		List[Any]   					=		return_dict if return_dict is not None else self.config.use_return_dict

					lowercase						:		List[Any]   					=		self.embedder(a_							)

					lowercase						:		str   					=		self.encoder(
					    a_							,      output_hidden_states=a_							,      return_dict=a_							)

					lowercase						:		Dict   					=		encoder_outputs[0]

					lowercase						:		int   					=		self.pooler(a_							)

					if not return_dict:
						return (last_hidden_state, pooled_output) + encoder_outputs[1:]

					return BaseModelOutputWithPoolingAndNoAttention(
					    last_hidden_state=a_							,      pooler_output=a_							,      hidden_states=encoder_outputs.hidden_states							,      )




@add_start_docstrings(
    '''
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''						,			SCREAMING_SNAKE_CASE						,			)
class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				def __init__(     self							,      a_							)    ->  str:
					super().__init__(a_							)
					lowercase						:		List[str]   					=		config.num_labels
					lowercase						:		str   					=		ResNetModel(a_							)
					# classification head
					lowercase						:		Union[str, Any]   					=		nn.Sequential(
					    nn.Flatten()							,      nn.Linear(config.hidden_sizes[-1]							,      config.num_labels							) if config.num_labels > 0 else nn.Identity()							,      )
					# initialize weights and apply final processing
					self.post_init()


				@add_start_docstrings_to_model_forward(a_							)
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT							,      output_type=a_							,      config_class=_CONFIG_FOR_DOC							,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,      )
				def 	a__		(     self							,      a_ = None							,      a_ = None							,      a_ = None							,      a_ = None							,      )    ->  ImageClassifierOutputWithNoAttention:
					lowercase						:		Tuple   					=		return_dict if return_dict is not None else self.config.use_return_dict

					lowercase						:		str   					=		self.resnet(a_							,      output_hidden_states=a_							,      return_dict=a_							)

					lowercase						:		List[Any]   					=		outputs.pooler_output if return_dict else outputs[1]

					lowercase						:		Optional[int]   					=		self.classifier(a_							)

					lowercase						:		Optional[Any]   					=		None

					if labels is not None:
						if self.config.problem_type is None:
							if self.num_labels == 1:
								lowercase						:		Union[str, Any]   					=		"regression"
							elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
								lowercase						:		Optional[int]   					=		"single_label_classification"
							else:
								lowercase						:		str   					=		"multi_label_classification"
						if self.config.problem_type == "regression":
							lowercase						:		Optional[Any]   					=		MSELoss()
							if self.num_labels == 1:
								lowercase						:		Tuple   					=		loss_fct(logits.squeeze()							,      labels.squeeze()							)
							else:
								lowercase						:		int   					=		loss_fct(a_							,      a_							)
						elif self.config.problem_type == "single_label_classification":
							lowercase						:		List[str]   					=		CrossEntropyLoss()
							lowercase						:		int   					=		loss_fct(logits.view(-1							,      self.num_labels							)							,      labels.view(-1							)							)
						elif self.config.problem_type == "multi_label_classification":
							lowercase						:		Tuple   					=		BCEWithLogitsLoss()
							lowercase						:		Optional[int]   					=		loss_fct(a_							,      a_							)

					if not return_dict:
						lowercase						:		List[Any]   					=		(logits,) + outputs[2:]
						return (loss,) + output if loss is not None else output

					return ImageClassifierOutputWithNoAttention(loss=a_							,      logits=a_							,      hidden_states=outputs.hidden_states							)




@add_start_docstrings(
    '''
    ResNet backbone, to be used with frameworks like DETR and MaskFormer.
    '''						,			SCREAMING_SNAKE_CASE						,			)
class        _UpperCamelCase	(    SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE):


				'''simple docstring'''


				def __init__(     self							,      a_							)    ->  Optional[int]:
					super().__init__(a_							)
					super()._init_backbone(a_							)

					lowercase						:		Tuple   					=		[config.embedding_size] + config.hidden_sizes
					lowercase						:		int   					=		ResNetEmbeddings(a_							)
					lowercase						:		int   					=		ResNetEncoder(a_							)

					# initialize weights and apply final processing
					self.post_init()


				@add_start_docstrings_to_model_forward(a_							)
				@replace_return_docstrings(output_type=a_							,      config_class=_CONFIG_FOR_DOC							)
				def 	a__		(     self							,      a_							,      a_ = None							,      a_ = None							)    ->  BackboneOutput:
					lowercase						:		Tuple   					=		return_dict if return_dict is not None else self.config.use_return_dict
					lowercase						:		int   					=		(
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)

					lowercase						:		List[str]   					=		self.embedder(a_							)

					lowercase						:		Dict   					=		self.encoder(a_							,      output_hidden_states=a_							,      return_dict=a_							)

					lowercase						:		Optional[int]   					=		outputs.hidden_states

					lowercase						:		Optional[int]   					=		()
					for idx, stage in enumerate(self.stage_names							):
						if stage in self.out_features:
							feature_maps += (hidden_states[idx],)

					if not return_dict:
						lowercase						:		List[str]   					=		(feature_maps,)
						if output_hidden_states:
							output += (outputs.hidden_states,)
						return output

					return BackboneOutput(
					    feature_maps=a_							,      hidden_states=outputs.hidden_states if output_hidden_states else None							,      attentions=a_							,      )






'''simple docstring'''
import collections
import importlib.util
import os
import re
from pathlib import Path


lowerCAmelCase   : List[str]             =				"""src/transformers"""


# Matches is_xxx_available()
lowerCAmelCase   : List[str]             =				re.compile(R"""is\_([a-z_]*)_available()""")
# Catches a one-line _import_struct = {xxx}
lowerCAmelCase   : str             =				re.compile(R"""^_import_structure\s+=\s+\{([^\}]+)\}""")
# Catches a line with a key-values pattern: "bla": ["foo", "bar"]
lowerCAmelCase   : Optional[int]             =				re.compile(R"""\s+\"\S*\":\s+\[([^\]]*)\]""")
# Catches a line if not is_foo_available
lowerCAmelCase   : Optional[int]             =				re.compile(R"""^\s*if\s+not\s+is\_[a-z_]*\_available\(\)""")
# Catches a line _import_struct["bla"].append("foo")
lowerCAmelCase   : str             =				re.compile(R"""^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)""")
# Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"]
lowerCAmelCase   : List[Any]             =				re.compile(R"""^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]""")
# Catches a line with an object between quotes and a comma:     "MyModel",
lowerCAmelCase   : Dict             =				re.compile("""^\s+\"([^\"]+)\",""")
# Catches a line with objects between brackets only:    ["foo", "bar"],
lowerCAmelCase   : Tuple             =				re.compile("""^\s+\[([^\]]+)\]""")
# Catches a line with from foo import bar, bla, boo
lowerCAmelCase   : Optional[int]             =				re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""")
# Catches a line with try:
lowerCAmelCase   : Optional[int]             =				re.compile(R"""^\s*try:""")
# Catches a line with else:
lowerCAmelCase   : Optional[int]             =				re.compile(R"""^\s*else:""")




def 	_A  (       A							)  ->				List[Any]:
	if _re_test_backend.search(A							) is None:
		return None
	lowercase						:		Union[str, Any]   					=		[b[0] for b in _re_backend.findall(A							)]
	backends.sort()
	return "_and_".join(A							)




def 	_A  (       A							)  ->				Optional[Any]:
	with open(A							,"r"							,encoding="utf-8"							,newline="\n"							) as f:
		lowercase						:		Tuple   					=		f.readlines()

	lowercase						:		List[str]   					=		0
	while line_index < len(A							) and not lines[line_index].startswith("_import_structure = {"							):
		line_index += 1

	# If this is a traditional init, just return.
	if line_index >= len(A							):
		return None

	# First grab the objects without a specific backend in _import_structure
	lowercase						:		Union[str, Any]   					=		[]
	while not lines[line_index].startswith("if TYPE_CHECKING"							) and find_backend(lines[line_index]							) is None:
		lowercase						:		Any   					=		lines[line_index]
		# If we have everything on a single line, let's deal with it.
		if _re_one_line_import_struct.search(A							):
			lowercase						:		int   					=		_re_one_line_import_struct.search(A							).groups()[0]
			lowercase						:		Union[str, Any]   					=		re.findall("\[([^\]]+)\]"							,A							)
			for imp in imports:
				objects.extend([obj[1:-1] for obj in imp.split(", "							)]							)
			line_index += 1
			continue
		lowercase						:		Optional[Any]   					=		_re_import_struct_key_value.search(A							)
		if single_line_import_search is not None:
			lowercase						:		List[Any]   					=		[obj[1:-1] for obj in single_line_import_search.groups()[0].split(", "							) if len(A							) > 0]
			objects.extend(A							)
		elif line.startswith(" " * 8 + "\""							):
			objects.append(line[9:-3]							)
		line_index += 1

	lowercase						:		Tuple   					=		{"none": objects}
	# Let's continue with backend-specific objects in _import_structure
	while not lines[line_index].startswith("if TYPE_CHECKING"							):
		# If the line is an if not is_backend_available, we grab all objects associated.
		lowercase						:		int   					=		find_backend(lines[line_index]							)
		# Check if the backend declaration is inside a try block:
		if _re_try.search(lines[line_index - 1]							) is None:
			lowercase						:		str   					=		None

		if backend is not None:
			line_index += 1

			# Scroll until we hit the else block of try-except-else
			while _re_else.search(lines[line_index]							) is None:
				line_index += 1

			line_index += 1

			lowercase						:		Any   					=		[]
			# Until we unindent, add backend objects to the list
			while len(lines[line_index]							) <= 1 or lines[line_index].startswith(" " * 4							):
				lowercase						:		List[Any]   					=		lines[line_index]
				if _re_import_struct_add_one.search(A							) is not None:
					objects.append(_re_import_struct_add_one.search(A							).groups()[0]							)
				elif _re_import_struct_add_many.search(A							) is not None:
					lowercase						:		Dict   					=		_re_import_struct_add_many.search(A							).groups()[0].split(", "							)
					lowercase						:		Any   					=		[obj[1:-1] for obj in imports if len(A							) > 0]
					objects.extend(A							)
				elif _re_between_brackets.search(A							) is not None:
					lowercase						:		Optional[Any]   					=		_re_between_brackets.search(A							).groups()[0].split(", "							)
					lowercase						:		Tuple   					=		[obj[1:-1] for obj in imports if len(A							) > 0]
					objects.extend(A							)
				elif _re_quote_object.search(A							) is not None:
					objects.append(_re_quote_object.search(A							).groups()[0]							)
				elif line.startswith(" " * 8 + "\""							):
					objects.append(line[9:-3]							)
				elif line.startswith(" " * 1_2 + "\""							):
					objects.append(line[1_3:-3]							)
				line_index += 1

			lowercase						:		Optional[int]   					=		objects
		else:
			line_index += 1

    # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend
	lowercase						:		str   					=		[]
	while (
	    line_index < len(A							)
	    and find_backend(lines[line_index]							) is None
	    and not lines[line_index].startswith("else"							)
	):
		lowercase						:		int   					=		lines[line_index]
		lowercase						:		Any   					=		_re_import.search(A							)
		if single_line_import_search is not None:
			objects.extend(single_line_import_search.groups()[0].split(", "							)							)
		elif line.startswith(" " * 8							):
			objects.append(line[8:-2]							)
		line_index += 1

	lowercase						:		Optional[Any]   					=		{"none": objects}
	# Let's continue with backend-specific objects
	while line_index < len(A							):
		# If the line is an if is_backend_available, we grab all objects associated.
		lowercase						:		List[Any]   					=		find_backend(lines[line_index]							)
		# Check if the backend declaration is inside a try block:
		if _re_try.search(lines[line_index - 1]							) is None:
			lowercase						:		int   					=		None

		if backend is not None:
			line_index += 1

			# Scroll until we hit the else block of try-except-else
			while _re_else.search(lines[line_index]							) is None:
				line_index += 1

			line_index += 1

			lowercase						:		Optional[Any]   					=		[]
			# Until we unindent, add backend objects to the list
			while len(lines[line_index]							) <= 1 or lines[line_index].startswith(" " * 8							):
				lowercase						:		List[Any]   					=		lines[line_index]
				lowercase						:		Tuple   					=		_re_import.search(A							)
				if single_line_import_search is not None:
					objects.extend(single_line_import_search.groups()[0].split(", "							)							)
				elif line.startswith(" " * 1_2							):
					objects.append(line[1_2:-2]							)
				line_index += 1

			lowercase						:		Dict   					=		objects
		else:
			line_index += 1

	return import_dict_objects, type_hint_objects




def 	_A  (       A							,A							)  ->				List[str]:

	def find_duplicates(A							):
		return [k for k, v in collections.Counter(A							).items() if v > 1]

	if list(import_dict_objects.keys()							) != list(type_hint_objects.keys()							):
		return ["Both sides of the init do not have the same backends!"]

	lowercase						:		str   					=		[]
	for key in import_dict_objects.keys():
		lowercase						:		List[str]   					=		find_duplicates(import_dict_objects[key]							)
		if duplicate_imports:
			errors.append(F'''Duplicate _import_structure definitions for: {duplicate_imports}'''							)
		lowercase						:		List[Any]   					=		find_duplicates(type_hint_objects[key]							)
		if duplicate_type_hints:
			errors.append(F'''Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}'''							)

		if sorted(set(import_dict_objects[key]							)							) != sorted(set(type_hint_objects[key]							)							):
			lowercase						:		Optional[int]   					=		"base imports" if key == "none" else F'''{key} backend'''
			errors.append(F'''Differences for {name}:'''							)
			for a in type_hint_objects[key]:
				if a not in import_dict_objects[key]:
					errors.append(F'''  {a} in TYPE_HINT but not in _import_structure.'''							)
			for a in import_dict_objects[key]:
				if a not in type_hint_objects[key]:
					errors.append(F'''  {a} in _import_structure but not in TYPE_HINT.'''							)
	return errors




def 	_A  (       )  ->				List[Any]:
	lowercase						:		Any   					=		[]
	for root, _, files in os.walk(A							):
		if "__init__.py" in files:
			lowercase						:		Optional[Any]   					=		os.path.join(A							,"__init__.py"							)
			lowercase						:		Tuple   					=		parse_init(A							)
			if objects is not None:
				lowercase						:		Tuple   					=		analyze_results(*A							)
				if len(A							) > 0:
					lowercase						:		Tuple   					=		F'''Problem in {fname}, both halves do not define the same objects.\n{errors[0]}'''
					failures.append("\n".join(A							)							)
	if len(A							) > 0:
		raise ValueError("\n\n".join(A							)							)




def 	_A  (       )  ->				Union[str, Any]:
	lowercase						:		Dict   					=		[]
	for path, directories, files in os.walk(A							):
		for folder in directories:
			# Ignore private modules
			if folder.startswith("_"							):
				directories.remove(A							)
				continue
			# Ignore leftovers from branches (empty folders apart from pycache)
			if len(list((Path(A							) / folder).glob("*.py"							)							)							) == 0:
				continue
			lowercase						:		str   					=		str((Path(A							) / folder).relative_to(A							)							)
			lowercase						:		Optional[Any]   					=		short_path.replace(os.path.sep							,"."							)
			submodules.append(A							)
		for fname in files:
			if fname == "__init__.py":
				continue
			lowercase						:		Tuple   					=		str((Path(A							) / fname).relative_to(A							)							)
			lowercase						:		int   					=		short_path.replace(".py"							,""							).replace(os.path.sep							,"."							)
			if len(submodule.split("."							)							) == 1:
				submodules.append(A							)
	return submodules


lowerCAmelCase   : Dict             =				[
    """convert_pytorch_checkpoint_to_tf2""",
    """modeling_flax_pytorch_utils""",
]




def 	_A  (       )  ->				Optional[int]:
	# This is to make sure the transformers module imported is the one in the repo.
	lowercase						:		int   					=		importlib.util.spec_from_file_location(
	    "transformers"							,os.path.join(A							,"__init__.py"							)							,submodule_search_locations=[PATH_TO_TRANSFORMERS]							,)
	lowercase						:		Optional[int]   					=		spec.loader.load_module()

	lowercase						:		Dict   					=		[
	    module
	    for module in get_transformers_submodules()
	    if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys()
	]
	if len(A							) > 0:
		lowercase						:		Optional[int]   					=		"\n".join(F'''- {module}''' for module in module_not_registered							)
		raise ValueError(
		    "The following submodules are not properly registered in the main init of Transformers:\n"
		    F'''{list_of_modules}\n'''
		    "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value."							)


if __name__ == "__main__":
					check_all_inits()
					check_submodules()


