





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING


lowercase_		:				Optional[Any]				=						logging.get_logger(__name__)


class 			lowercase				(							a_     ):



						"""simple docstring"""

						_UpperCamelCase  :	Dict								=	"upernet"







						def __init__(    self			:		Optional[int] ,				lowerCamelCase_			:		Union[str, Any]=None ,				lowerCamelCase_			:		Union[str, Any]=5_12 ,				lowerCamelCase_			:		List[str]=0.02 ,				lowerCamelCase_			:		Union[str, Any]=[1, 2, 3, 6] ,				lowerCamelCase_			:		Optional[Any]=True ,				lowerCamelCase_			:		int=0.4 ,				lowerCamelCase_			:		List[Any]=3_84 ,				lowerCamelCase_			:		Optional[int]=2_56 ,				lowerCamelCase_			:		List[Any]=1 ,				lowerCamelCase_			:		List[str]=False ,				lowerCamelCase_			:		List[str]=2_55 ,				**lowerCamelCase_			:		Tuple ,				):





										'''simple docstring'''







										super().__init__(**lowerCamelCase_					)

										if backbone_config is None:
														logger.info('`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'					)
														_snake_case :			Tuple  						=   CONFIG_MAPPING['resnet'](out_features=['stage1', 'stage2', 'stage3', 'stage4']					)
										elif isinstance(lowerCamelCase_ ,				lowerCamelCase_					):
														_snake_case :			List[Any]  						=   backbone_config.get('model_type'					)
														_snake_case :			Optional[Any]  						=   CONFIG_MAPPING[backbone_model_type]
														_snake_case :			str  						=   config_class.from_dict(lowerCamelCase_					)

										_snake_case :			Optional[int]  						=   backbone_config
										_snake_case :			List[Any]  						=   hidden_size
										_snake_case :			Any  						=   initializer_range
										_snake_case :			List[Any]  						=   pool_scales
										_snake_case :			str  						=   use_auxiliary_head
										_snake_case :			Union[str, Any]  						=   auxiliary_loss_weight
										_snake_case :			Any  						=   auxiliary_in_channels
										_snake_case :			Union[str, Any]  						=   auxiliary_channels
										_snake_case :			List[str]  						=   auxiliary_num_convs
										_snake_case :			Dict  						=   auxiliary_concat_input
										_snake_case :			Optional[int]  						=   loss_ignore_index







						def 		__UpperCAmelCase       (    self			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			List[Any]  						=   copy.deepcopy(self.__dict__					)
										_snake_case :			Dict  						=   self.backbone_config.to_dict()
										_snake_case :			Dict  						=   self.__class__.model_type
										return output













import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowercase_		:				Optional[int]				=						object()

# For specifying empty leaf dict `{}`
lowercase_		:				List[Any]				=						object()

def       A__(		__lowerCAmelCase	,			__lowerCAmelCase ):
				_snake_case :			Optional[Any]  						=   tuple((re.compile(x + '$' ) for x in qs) )
				for i in range(len(__lowerCAmelCase ) - len(__lowerCAmelCase ) + 1 ):
								_snake_case :			Tuple  						=   [x.match(__lowerCAmelCase ) for x, y in zip(__lowerCAmelCase	,			ks[i:] )]
								if matches and all(__lowerCAmelCase ):
												return True
				return False

def       A__(		__lowerCAmelCase ):
				def replace(__lowerCAmelCase	,			__lowerCAmelCase ):
								for rule, replacement in rules:
												if _match(__lowerCAmelCase	,			__lowerCAmelCase ):
																return replacement
								return val

				return replace

def       A__(		):
				return [
				    # embeddings
				    (("transformer", "wpe", "embedding"), P('mp'	,			__lowerCAmelCase )),
				    (("transformer", "wte", "embedding"), P('mp'	,			__lowerCAmelCase )),
				    # atention
				    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__lowerCAmelCase	,			'mp' )),
				    (("attention", "out_proj", "kernel"), P('mp'	,			__lowerCAmelCase )),
				    (("attention", "out_proj", "bias"), None),
				    # mlp
				    (("mlp", "c_fc", "kernel"), P(__lowerCAmelCase	,			'mp' )),
				    (("mlp", "c_fc", "bias"), P('mp' )),
				    (("mlp", "c_proj", "kernel"), P('mp'	,			__lowerCAmelCase )),
				    (("mlp", "c_proj", "bias"), None),
				    # layer norms
				    ((r"ln_\d+", "bias"), None),
				    ((r"\d+", r"ln_\d+", "scale"), None),
				    (("ln_f", "bias"), None),
				    (("ln_f", "scale"), None),
				]

def       A__(		__lowerCAmelCase ):
				_snake_case :			Optional[Any]  						=   _get_partition_rules()
				_snake_case :			Optional[Any]  						=   _replacement_rules(__lowerCAmelCase )
				_snake_case :			str  						=   {k: _unmatched for k in flatten_dict(__lowerCAmelCase )}
				_snake_case :			str  						=   {k: replace(__lowerCAmelCase	,			__lowerCAmelCase ) for k, v in initd.items()}
				assert _unmatched not in result.values(), "Incomplete partition spec."
				return freeze(unflatten_dict(__lowerCAmelCase ) )






