



"""simple docstring"""

from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


a_			   =					logging.get_logger(__name__)







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Union[tf.Tensor, np.ndarray]			):



					"""simple docstring"""





					if isinstance(SCREAMING_SNAKE_CASE__				,    np.ndarray			):
										return list(tensor.shape			)

					snake_case_					:		Optional[int] 					=					tf.shape(SCREAMING_SNAKE_CASE__			)

					if tensor.shape == tf.TensorShape(SCREAMING_SNAKE_CASE__			):
										return dynamic

					snake_case_					:		List[str] 					=					tensor.shape.as_list()

					return [dynamic[i] if s is None else s for i, s in enumerate(SCREAMING_SNAKE_CASE__			)]







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  tf.Tensor				,    SCREAMING_SNAKE_CASE__ :  Optional[int] = None				,    SCREAMING_SNAKE_CASE__ :  Optional[str] = None			):



					"""simple docstring"""





					return tf.nn.softmax(logits=logits + 1E-9				,    axis=SCREAMING_SNAKE_CASE__				,    name=SCREAMING_SNAKE_CASE__			)







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  Tuple				,    SCREAMING_SNAKE_CASE__ :  Optional[int]				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]=1E-5				,    SCREAMING_SNAKE_CASE__ :  Union[str, Any]=-1			):



					"""simple docstring"""





					if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			):
										raise NotImplementedError("""Only 1D weight and bias tensors are supported for now, with only a single axis."""			)

					# Get mean and variance on the axis to be normalized
					snake_case_					,			snake_case_					:		str 					=					tf.nn.moments(SCREAMING_SNAKE_CASE__				,    axes=[axis]				,    keepdims=SCREAMING_SNAKE_CASE__			)

					if axis != -1:
										# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
										# on every dimension except axis
										snake_case_					:		Optional[Any] 					=					[1] * inputs.shape.rank
										snake_case_					:		Any 					=					shape_list(SCREAMING_SNAKE_CASE__			)[axis]
										snake_case_					:		List[str] 					=					tf.reshape(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)
										snake_case_					:		List[Any] 					=					tf.reshape(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)

					# Compute layer normalization using the batch_normalization
					# function.
					snake_case_					:		List[Any] 					=					tf.nn.batch_normalization(
					    SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__				,    offset=SCREAMING_SNAKE_CASE__				,    scale=SCREAMING_SNAKE_CASE__				,    variance_epsilon=SCREAMING_SNAKE_CASE__				,    )
					return outputs







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  Dict				,    SCREAMING_SNAKE_CASE__ :  Dict=0				,    SCREAMING_SNAKE_CASE__ :  Dict=-1			):



					"""simple docstring"""





					if end_dim < 0:
										end_dim += input.shape.rank
					if start_dim < 0:
										start_dim += input.shape.rank

					if start_dim == end_dim:
										return input

					snake_case_					:		int 					=					tf.shape(SCREAMING_SNAKE_CASE__			)
					snake_case_					:		str 					=					tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]			)
					snake_case_					:		List[str] 					=					tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]				,    axis=0			)
					return tf.reshape(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  tf.Tensor			):



					"""simple docstring"""





					if not isinstance(SCREAMING_SNAKE_CASE__				,    tf.Tensor			):
										snake_case_					:		Optional[Any] 					=					tf.convert_to_tensor(SCREAMING_SNAKE_CASE__			)  # Catches stray NumPy inputs
					if encoder_attention_mask.shape.rank == 3:
										snake_case_					:		Optional[int] 					=					encoder_attention_mask[:, None, :, :]
					if encoder_attention_mask.shape.rank == 2:
										snake_case_					:		List[Any] 					=					encoder_attention_mask[:, None, None, :]
					# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
					# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
					# /transformer/transformer_layers.py#L270
					# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
					# encoder_extended_attention_mask.transpose(-1, -2))
					snake_case_					:		int 					=					(
					    tf.cast(1				,    encoder_attention_mask.dtype			) - encoder_extended_attention_mask
					) * encoder_extended_attention_mask.dtype.min

					return encoder_extended_attention_mask







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  tf.Tensor				,    SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  str = "input_ids"			):



					"""simple docstring"""





					tf.debugging.assert_less(
					    SCREAMING_SNAKE_CASE__				,    tf.cast(SCREAMING_SNAKE_CASE__				,    dtype=tensor.dtype			)				,    message=(
					        f'The maximum value of {tensor_name} ({tf.math.reduce_max(SCREAMING_SNAKE_CASE__			)}) must be smaller than the embedding '
					        f'layer\'s input dimension ({embed_dim}). The likely cause is some problem at tokenization time.'
					    )				,    )







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  Optional[Any]				,    SCREAMING_SNAKE_CASE__ :  int			):



					"""simple docstring"""





					snake_case_					:		Any 					=					6_4_5_1_2
					# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
					# because in that case even chunking the array would not make the saving
					# possible.
					snake_case_					:		Optional[int] 					=					[x for x in data if len(SCREAMING_SNAKE_CASE__			) > HDF5_OBJECT_HEADER_LIMIT]

					# Expecting this to never be true.
					if bad_attributes:
										raise RuntimeError(
										    """The following attributes cannot be saved to HDF5 file because """
										    f'they are larger than {HDF5_OBJECT_HEADER_LIMIT} '
										    f'bytes: {bad_attributes}'			)

					snake_case_					:		str 					=					np.asarray(SCREAMING_SNAKE_CASE__			)

					snake_case_					:		Union[str, Any] 					=					1
					snake_case_					:		Optional[Any] 					=					np.array_split(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)

					# This will never loop forever thanks to the test above.
					while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data			):
										num_chunks += 1
										snake_case_					:		Any 					=					np.array_split(SCREAMING_SNAKE_CASE__				,    SCREAMING_SNAKE_CASE__			)

					if num_chunks > 1:
										for chunk_id, chunk_data in enumerate(SCREAMING_SNAKE_CASE__			):
															snake_case_					:		List[Any] 					=					chunk_data
					else:
										snake_case_					:		Union[str, Any] 					=					data







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  int				,    SCREAMING_SNAKE_CASE__ :  Dict			):



					"""simple docstring"""





					if name in group.attrs:
										snake_case_					:		Tuple 					=					[n.decode("""utf8"""			) if hasattr(SCREAMING_SNAKE_CASE__				,    """decode"""			) else n for n in group.attrs[name]]
					else:
										snake_case_					:		Tuple 					=					[]
										snake_case_					:		Optional[Any] 					=					0
										while "%s%d" % (name, chunk_id) in group.attrs:
															data.extend(
															    [n.decode("""utf8"""			) if hasattr(SCREAMING_SNAKE_CASE__				,    """decode"""			) else n for n in group.attrs["""%s%d""" % (name, chunk_id)]]			)
															chunk_id += 1
					return data







def 							SCREAMING_SNAKE_CASE__      (		SCREAMING_SNAKE_CASE__ :  List[str]			):



					"""simple docstring"""





					def _expand_single_ad_tensor(SCREAMING_SNAKE_CASE__ :  Optional[Any]			):
										if isinstance(SCREAMING_SNAKE_CASE__				,    tf.Tensor			) and t.shape.rank == 1:
															return tf.expand_dims(SCREAMING_SNAKE_CASE__				,    axis=-1			)
										return t

					return tf.nest.map_structure(_expand_single_ad_tensor				,    SCREAMING_SNAKE_CASE__			)









"""simple docstring"""

import copy

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


a_			   =					logging.get_logger(__name__)







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Optional[int]							       =		"""upernet"""







						def __init__(self   ,		lowercase__=None   ,		lowercase__=5_12   ,		lowercase__=0.02   ,		lowercase__=[1, 2, 3, 6]   ,		lowercase__=True   ,		lowercase__=0.4   ,		lowercase__=3_84   ,		lowercase__=2_56   ,		lowercase__=1   ,		lowercase__=False   ,		lowercase__=2_55   ,		**lowercase__   ,		):
											super().__init__(**lowercase__       )

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

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







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Dict 					=					copy.deepcopy(self.__dict__       )
											snake_case_					:		Union[str, Any] 					=					self.backbone_config.to_dict()
											snake_case_					:		Any 					=					self.__class__.model_type
											return output




