





"""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 		__lowercase     (   snake_case_  :  Union[tf.Tensor, np.ndarray]						)   ->Optional[int]:



    '''simple docstring'''







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

    __A    :						str						 =     tf.shape(__SCREAMING_SNAKE_CASE						)

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

    __A    :						Dict						 =     tensor.shape.as_list()

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







def 		__lowercase     (   snake_case_  :  tf.Tensor				,snake_case_  :  Optional[int] = None				,snake_case_  :  Optional[str] = None						)   ->List[Any]:



    '''simple docstring'''







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







def 		__lowercase     (   snake_case_  :  str				,snake_case_  :  Any				,snake_case_  :  int				,snake_case_  :  Optional[Any]=1e-5				,snake_case_  :  Dict=-1						)   ->Union[str, Any]:



    '''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
    __A    :						Any						 =     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
        __A    :						Union[str, Any]						 =     [1] * inputs.shape.rank
        __A    :						Dict						 =     shape_list(__SCREAMING_SNAKE_CASE						)[axis]
        __A    :						List[Any]						 =     tf.reshape(__SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE						)
        __A    :						str						 =     tf.reshape(__SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE						)

    # Compute layer normalization using the batch_normalization
    # function.
    __A    :						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 		__lowercase     (   snake_case_  :  int				,snake_case_  :  int=0				,snake_case_  :  List[str]=-1						)   ->Tuple:



    '''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

    __A    :						int						 =     tf.shape(__SCREAMING_SNAKE_CASE						)
    __A    :						List[str]						 =     tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]						)
    __A    :						List[Any]						 =     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 		__lowercase     (   snake_case_  :  tf.Tensor						)   ->List[str]:



    '''simple docstring'''







    if not isinstance(__SCREAMING_SNAKE_CASE				,tf.Tensor						):
        __A    :						str						 =     tf.convert_to_tensor(__SCREAMING_SNAKE_CASE						)  # Catches stray NumPy inputs
    if encoder_attention_mask.shape.rank == 3:
        __A    :						Optional[int]						 =     encoder_attention_mask[:, None, :, :]
    if encoder_attention_mask.shape.rank == 2:
        __A    :						Optional[int]						 =     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))
    __A    :						Dict						 =     (
        tf.cast(1				,encoder_attention_mask.dtype						) - encoder_extended_attention_mask
    ) * encoder_extended_attention_mask.dtype.min

    return encoder_extended_attention_mask







def 		__lowercase     (   snake_case_  :  tf.Tensor				,snake_case_  :  int				,snake_case_  :  str = "input_ids"						)   ->List[Any]:



    '''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 		__lowercase     (   snake_case_  :  Dict				,snake_case_  :  Union[str, Any]				,snake_case_  :  List[str]						)   ->Optional[Any]:



    '''simple docstring'''







    __A    :						List[str]						 =     64512
    # 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.
    __A    :						List[str]						 =     [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}"""						)

    __A    :						Dict						 =     np.asarray(__SCREAMING_SNAKE_CASE						)

    __A    :						Tuple						 =     1
    __A    :						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
        __A    :						Optional[int]						 =     np.array_split(__SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE						)

    if num_chunks > 1:
        for chunk_id, chunk_data in enumerate(__SCREAMING_SNAKE_CASE						):
            __A    :						Tuple						 =     chunk_data
    else:
        __A    :						int						 =     data







def 		__lowercase     (   snake_case_  :  Dict				,snake_case_  :  List[str]						)   ->List[str]:



    '''simple docstring'''







    if name in group.attrs:
        __A    :						List[Any]						 =     [n.decode('''utf8'''						) if hasattr(__SCREAMING_SNAKE_CASE				,'''decode'''						) else n for n in group.attrs[name]]
    else:
        __A    :						Dict						 =     []
        __A    :						Dict						 =     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 		__lowercase     (   snake_case_  :  Tuple						)   ->Any:



    '''simple docstring'''







    def _expand_single_ad_tensor(snake_case_  :  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
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Any													=       vocab_size
											lowercase_	:     Tuple													=       hidden_size
											lowercase_	:     Optional[Any]													=       d_kv
											lowercase_	:     List[str]													=       d_ff
											lowercase_	:     List[str]													=       num_layers
											lowercase_	:     Optional[Any]													=       num_heads
											lowercase_	:     Union[str, Any]													=       relative_attention_num_buckets
											lowercase_	:     Optional[int]													=       relative_attention_max_distance
											lowercase_	:     Union[str, Any]													=       dropout_rate
											lowercase_	:     Dict													=       layer_norm_epsilon
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     List[Any]													=       use_cache

											lowercase_	:     Optional[int]													=       eos_token_id
											lowercase_	:     Optional[int]													=       decoder_start_token_id

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

											super().__init__(
											    pad_token_id=__SCREAMING_SNAKE_CASE				,							eos_token_id=__SCREAMING_SNAKE_CASE				,							decoder_start_token_id=__SCREAMING_SNAKE_CASE				,							tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE				,							)






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the text config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     List[Any]													=       config_dict['''text_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Union[str, Any]													=       hidden_size
											lowercase_	:     Any													=       patch_embed_hidden_size
											lowercase_	:     List[Any]													=       d_ff
											lowercase_	:     Dict													=       dropout_rate
											lowercase_	:     Any													=       num_hidden_layers
											lowercase_	:     Any													=       num_attention_heads
											lowercase_	:     int													=       initializer_range
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     Dict													=       attention_dropout
											lowercase_	:     Optional[Any]													=       layer_norm_eps
											lowercase_	:     str													=       dense_act_fn
											lowercase_	:     Dict													=       seq_len
											lowercase_	:     List[Any]													=       relative_attention_num_buckets
											lowercase_	:     int													=       relative_attention_max_distance
											lowercase_	:     Optional[int]													=       d_kv






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the vision config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     Optional[int]													=       config_dict['''vision_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_encoder_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

											if vision_config is None:
																	lowercase_	:     Dict													=       {}
																	logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return cls(text_config=text_config.to_dict()				,							vision_config=vision_config.to_dict()				,							**__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



