


'''simple docstring'''


from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


lowerCamelCase__    	=     logging.get_logger(__name__)






def  __lowerCAmelCase (__lowerCAmelCase   ):


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

  _UpperCAmelCase		:       List[Any]								=      tf.shape(lowerCamelCase__   )

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

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

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






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase = None      , __lowerCAmelCase = None   ):
  return tf.nn.softmax(logits=logits + 1e-9      , axis=lowerCamelCase__      , name=lowerCamelCase__   )






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase=1e-5      , __lowerCAmelCase=-1   ):


  if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(lowerCamelCase__      , lowerCamelCase__   ):
    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
  _UpperCAmelCase		:       Tuple								=      tf.nn.moments(lowerCamelCase__      , axes=[axis]      , keepdims=lowerCamelCase__   )

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

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






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase=0      , __lowerCAmelCase=-1   ):


  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

  _UpperCAmelCase		:       List[Any]								=      tf.shape(lowerCamelCase__   )
  _UpperCAmelCase		:       Optional[Any]								=      tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]   )
  _UpperCAmelCase		:       List[str]								=      tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]      , axis=0   )
  return tf.reshape(lowerCamelCase__      , lowerCamelCase__   )






def  __lowerCAmelCase (__lowerCAmelCase   ):


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

  return encoder_extended_attention_mask






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






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase      , __lowerCAmelCase   ):
  _UpperCAmelCase		:       Tuple								=      64_512
  # 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.
  _UpperCAmelCase		:       List[Any]								=      [x for x in data if len(lowerCamelCase__   ) > 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}"""   )

  _UpperCAmelCase		:       Any								=      np.asarray(lowerCamelCase__   )

  _UpperCAmelCase		:       str								=      1
  _UpperCAmelCase		:       Tuple								=      np.array_split(lowerCamelCase__      , lowerCamelCase__   )

  # 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
    _UpperCAmelCase		:       Tuple								=      np.array_split(lowerCamelCase__      , lowerCamelCase__   )

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






def  __lowerCAmelCase (__lowerCAmelCase      , __lowerCAmelCase   ):


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






def  __lowerCAmelCase (__lowerCAmelCase   ):


  def _expand_single_ad_tensor(__lowerCAmelCase   ):
    if isinstance(lowerCamelCase__      , tf.Tensor   ) and t.shape.rank == 1:
      return tf.expand_dims(lowerCamelCase__      , axis=-1   )
    return t

  return tf.nest.map_structure(_expand_single_ad_tensor      , lowerCamelCase__   )







'''simple docstring'''


from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class 	lowerCAmelCase__			(			UpperCAmelCase__	):
			lowerCAmelCase :	Any          =     ["image_processor", "tokenizer"]
			lowerCAmelCase :	List[Any]          =     "BlipImageProcessor"
			lowerCAmelCase :	Union[str, Any]          =     "AutoTokenizer"




			def __init__(		self  :     Any	,  lowerCamelCase__  :     Tuple	,  lowerCamelCase__  :     int )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[int]								=      False
					super().__init__(lowerCamelCase__	,  lowerCamelCase__ )
					_UpperCAmelCase		:       Tuple								=      self.image_processor




			def __call__(		self  :     Dict	,  lowerCamelCase__  :     ImageInput = None	,  lowerCamelCase__  :     Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None	,  lowerCamelCase__  :     bool = True	,  lowerCamelCase__  :     Union[bool, str, PaddingStrategy] = False	,  lowerCamelCase__  :     Union[bool, str, TruncationStrategy] = None	,  lowerCamelCase__  :     Optional[int] = None	,  lowerCamelCase__  :     int = 0	,  lowerCamelCase__  :     Optional[int] = None	,  lowerCamelCase__  :     Optional[bool] = None	,  lowerCamelCase__  :     bool = False	,  lowerCamelCase__  :     bool = False	,  lowerCamelCase__  :     bool = False	,  lowerCamelCase__  :     bool = False	,  lowerCamelCase__  :     bool = False	,  lowerCamelCase__  :     bool = True	,  lowerCamelCase__  :     Optional[Union[str, TensorType]] = None	,  **lowerCamelCase__  :     Tuple	,  )   ->BatchEncoding:



					'''simple docstring'''







					if images is None and text is None:
							raise ValueError("You have to specify either images or text." )

					# Get only text
					if images is None:
							_UpperCAmelCase		:       Optional[int]								=      self.tokenizer
							_UpperCAmelCase		:       List[Any]								=      self.tokenizer(
							    text=lowerCamelCase__	,  add_special_tokens=lowerCamelCase__	,  padding=lowerCamelCase__	,  truncation=lowerCamelCase__	,  max_length=lowerCamelCase__	,  stride=lowerCamelCase__	,  pad_to_multiple_of=lowerCamelCase__	,  return_attention_mask=lowerCamelCase__	,  return_overflowing_tokens=lowerCamelCase__	,  return_special_tokens_mask=lowerCamelCase__	,  return_offsets_mapping=lowerCamelCase__	,  return_token_type_ids=lowerCamelCase__	,  return_length=lowerCamelCase__	,  verbose=lowerCamelCase__	,  return_tensors=lowerCamelCase__	,  **lowerCamelCase__	,  )
							return text_encoding

					# add pixel_values
					_UpperCAmelCase		:       Optional[int]								=      self.image_processor(lowerCamelCase__	,  return_tensors=lowerCamelCase__ )

					if text is not None:
							_UpperCAmelCase		:       Dict								=      self.tokenizer(
							    text=lowerCamelCase__	,  add_special_tokens=lowerCamelCase__	,  padding=lowerCamelCase__	,  truncation=lowerCamelCase__	,  max_length=lowerCamelCase__	,  stride=lowerCamelCase__	,  pad_to_multiple_of=lowerCamelCase__	,  return_attention_mask=lowerCamelCase__	,  return_overflowing_tokens=lowerCamelCase__	,  return_special_tokens_mask=lowerCamelCase__	,  return_offsets_mapping=lowerCamelCase__	,  return_token_type_ids=lowerCamelCase__	,  return_length=lowerCamelCase__	,  verbose=lowerCamelCase__	,  return_tensors=lowerCamelCase__	,  **lowerCamelCase__	,  )
					else:
							_UpperCAmelCase		:       int								=      None

					if text_encoding is not None:
							encoding_image_processor.update(lowerCamelCase__ )

					return encoding_image_processor




			def 							lowerCAmelCase__    (		self  :     List[Any]	,  *lowerCamelCase__  :     Optional[int]	,  **lowerCamelCase__  :     Dict )   ->Optional[Any]:



					'''simple docstring'''







					return self.tokenizer.batch_decode(*lowerCamelCase__	,  **lowerCamelCase__ )




			def 							lowerCAmelCase__    (		self  :     int	,  *lowerCamelCase__  :     Dict	,  **lowerCamelCase__  :     str )   ->Optional[int]:



					'''simple docstring'''







					return self.tokenizer.decode(*lowerCamelCase__	,  **lowerCamelCase__ )






			@property
			# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
			def 							lowerCAmelCase__    (		self  :     Any )   ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:       Optional[Any]								=      self.tokenizer.model_input_names
					_UpperCAmelCase		:       Dict								=      self.image_processor.model_input_names
					return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )



