'''simple docstring'''






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


SCREAMING_SNAKE_CASE_	   =			logging.get_logger(__name__)


if is_vision_available():
				import PIL




class    a (       UpperCAmelCase__				):



		"""simple docstring"""
		__lowerCAmelCase				      =					["""pixel_values"""]







		def __init__(							self   ,    snake_case_ = True   ,    snake_case_ = None   ,    snake_case_ = PILImageResampling.BICUBIC   ,    snake_case_ = True   ,    snake_case_ = None   ,    snake_case_ = True   ,    snake_case_ = 1 / 255   ,    snake_case_ = True   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = True   ,    **snake_case_   ,    ):

				'''simple docstring'''







				super().__init__(**__lowerCAmelCase					)
				__UpperCAmelCase:					Optional[int]		= size if size is not None else {"""shortest_edge""": 224}
				__UpperCAmelCase:					List[str]		= get_size_dict(__lowerCAmelCase   ,    default_to_square=__lowerCAmelCase					)
				__UpperCAmelCase:					Optional[int]		= crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
				__UpperCAmelCase:					int		= get_size_dict(__lowerCAmelCase   ,    default_to_square=__lowerCAmelCase   ,    param_name="""crop_size"""					)

				__UpperCAmelCase:					Optional[int]		= do_resize
				__UpperCAmelCase:					List[Any]		= size
				__UpperCAmelCase:					Any		= resample
				__UpperCAmelCase:					Optional[Any]		= do_center_crop
				__UpperCAmelCase:					Any		= crop_size
				__UpperCAmelCase:					Any		= do_rescale
				__UpperCAmelCase:					List[str]		= rescale_factor
				__UpperCAmelCase:					List[str]		= do_normalize
				__UpperCAmelCase:					Union[str, Any]		= image_mean if image_mean is not None else OPENAI_CLIP_MEAN
				__UpperCAmelCase:					Tuple		= image_std if image_std is not None else OPENAI_CLIP_STD
				__UpperCAmelCase:					List[Any]		= do_convert_rgb







		def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_ = PILImageResampling.BICUBIC   ,    snake_case_ = None   ,    **snake_case_   ,    ):

				'''simple docstring'''







				__UpperCAmelCase:					Dict		= get_size_dict(__lowerCAmelCase   ,    default_to_square=__lowerCAmelCase					)
				if "shortest_edge" not in size:
						raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''					)
				__UpperCAmelCase:					Any		= get_resize_output_image_size(__lowerCAmelCase   ,    size=size["""shortest_edge"""]   ,    default_to_square=__lowerCAmelCase					)
				return resize(__lowerCAmelCase   ,    size=__lowerCAmelCase   ,    resample=__lowerCAmelCase   ,    data_format=__lowerCAmelCase   ,    **__lowerCAmelCase					)







		def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_ = None   ,    **snake_case_   ,    ):

				'''simple docstring'''







				__UpperCAmelCase:					Dict		= get_size_dict(__lowerCAmelCase					)
				if "height" not in size or "width" not in size:
						raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''					)
				return center_crop(__lowerCAmelCase   ,    size=(size["""height"""], size["""width"""])   ,    data_format=__lowerCAmelCase   ,    **__lowerCAmelCase					)







		def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_ = None   ,    **snake_case_   ,    ):

				'''simple docstring'''







				return rescale(__lowerCAmelCase   ,    scale=__lowerCAmelCase   ,    data_format=__lowerCAmelCase   ,    **__lowerCAmelCase					)







		def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_   ,    snake_case_ = None   ,    **snake_case_   ,    ):

				'''simple docstring'''







				return normalize(__lowerCAmelCase   ,    mean=__lowerCAmelCase   ,    std=__lowerCAmelCase   ,    data_format=__lowerCAmelCase   ,    **__lowerCAmelCase					)







		def    lowercase_  (							self   ,    snake_case_   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = None   ,    snake_case_ = ChannelDimension.FIRST   ,    **snake_case_   ,    ):

				'''simple docstring'''







				__UpperCAmelCase:					Union[str, Any]		= do_resize if do_resize is not None else self.do_resize
				__UpperCAmelCase:					Optional[Any]		= size if size is not None else self.size
				__UpperCAmelCase:					Tuple		= get_size_dict(__lowerCAmelCase   ,    param_name="""size"""   ,    default_to_square=__lowerCAmelCase					)
				__UpperCAmelCase:					Any		= resample if resample is not None else self.resample
				__UpperCAmelCase:					Optional[int]		= do_center_crop if do_center_crop is not None else self.do_center_crop
				__UpperCAmelCase:					List[str]		= crop_size if crop_size is not None else self.crop_size
				__UpperCAmelCase:					int		= get_size_dict(__lowerCAmelCase   ,    param_name="""crop_size"""   ,    default_to_square=__lowerCAmelCase					)
				__UpperCAmelCase:					Dict		= do_rescale if do_rescale is not None else self.do_rescale
				__UpperCAmelCase:					Optional[Any]		= rescale_factor if rescale_factor is not None else self.rescale_factor
				__UpperCAmelCase:					Tuple		= do_normalize if do_normalize is not None else self.do_normalize
				__UpperCAmelCase:					int		= image_mean if image_mean is not None else self.image_mean
				__UpperCAmelCase:					List[str]		= image_std if image_std is not None else self.image_std
				__UpperCAmelCase:					List[Any]		= do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

				__UpperCAmelCase:					Any		= make_list_of_images(__lowerCAmelCase					)

				if not valid_images(__lowerCAmelCase					):
						raise ValueError(
						    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
						    """torch.Tensor, tf.Tensor or jax.ndarray."""					)

				if do_resize and size is None:
						raise ValueError("""Size must be specified if do_resize is True."""					)

				if do_center_crop and crop_size is None:
						raise ValueError("""Crop size must be specified if do_center_crop is True."""					)

				if do_rescale and rescale_factor is None:
						raise ValueError("""Rescale factor must be specified if do_rescale is True."""					)

				if do_normalize and (image_mean is None or image_std is None):
						raise ValueError("""Image mean and std must be specified if do_normalize is True."""					)

				# PIL RGBA images are converted to RGB
				if do_convert_rgb:
						__UpperCAmelCase:					Any		= [convert_to_rgb(__lowerCAmelCase					) for image in images]

				# All transformations expect numpy arrays.
				__UpperCAmelCase:					Dict		= [to_numpy_array(__lowerCAmelCase					) for image in images]

				if do_resize:
						__UpperCAmelCase:					List[Any]		= [self.resize(image=__lowerCAmelCase   ,    size=__lowerCAmelCase   ,    resample=__lowerCAmelCase					) for image in images]

				if do_center_crop:
						__UpperCAmelCase:					Optional[Any]		= [self.center_crop(image=__lowerCAmelCase   ,    size=__lowerCAmelCase					) for image in images]

				if do_rescale:
						__UpperCAmelCase:					List[str]		= [self.rescale(image=__lowerCAmelCase   ,    scale=__lowerCAmelCase					) for image in images]

				if do_normalize:
						__UpperCAmelCase:					Tuple		= [self.normalize(image=__lowerCAmelCase   ,    mean=__lowerCAmelCase   ,    std=__lowerCAmelCase					) for image in images]

				__UpperCAmelCase:					Dict		= [to_channel_dimension_format(__lowerCAmelCase   ,    __lowerCAmelCase					) for image in images]

				__UpperCAmelCase:					Union[str, Any]		= {"""pixel_values""": images}
				return BatchFeature(data=__lowerCAmelCase   ,    tensor_type=__lowerCAmelCase					)
'''simple docstring'''






import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
    import jax.numpy as jnp

    from transformers.models.albert.modeling_flax_albert import (
        FlaxAlbertForMaskedLM,
        FlaxAlbertForMultipleChoice,
        FlaxAlbertForPreTraining,
        FlaxAlbertForQuestionAnswering,
        FlaxAlbertForSequenceClassification,
        FlaxAlbertForTokenClassification,
        FlaxAlbertModel,
    )




class    a (       unittest.TestCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_=13   ,    snake_case_=7   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=True   ,    snake_case_=99   ,    snake_case_=32   ,    snake_case_=5   ,    snake_case_=4   ,    snake_case_=37   ,    snake_case_="gelu"   ,    snake_case_=0.1   ,    snake_case_=0.1   ,    snake_case_=512   ,    snake_case_=16   ,    snake_case_=2   ,    snake_case_=0.0_2   ,    snake_case_=4   ,    ):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= parent
    __UpperCAmelCase:					List[str]		= batch_size
    __UpperCAmelCase:					Optional[int]		= seq_length
    __UpperCAmelCase:					Optional[Any]		= is_training
    __UpperCAmelCase:					Any		= use_attention_mask
    __UpperCAmelCase:					List[str]		= use_token_type_ids
    __UpperCAmelCase:					List[str]		= use_labels
    __UpperCAmelCase:					List[str]		= vocab_size
    __UpperCAmelCase:					Optional[Any]		= hidden_size
    __UpperCAmelCase:					List[Any]		= num_hidden_layers
    __UpperCAmelCase:					List[Any]		= num_attention_heads
    __UpperCAmelCase:					Tuple		= intermediate_size
    __UpperCAmelCase:					Dict		= hidden_act
    __UpperCAmelCase:					Dict		= hidden_dropout_prob
    __UpperCAmelCase:					Tuple		= attention_probs_dropout_prob
    __UpperCAmelCase:					List[str]		= max_position_embeddings
    __UpperCAmelCase:					List[Any]		= type_vocab_size
    __UpperCAmelCase:					List[Any]		= type_sequence_label_size
    __UpperCAmelCase:					List[str]		= initializer_range
    __UpperCAmelCase:					List[Any]		= num_choices







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size					)

    __UpperCAmelCase:					str		= None
    if self.use_attention_mask:
      __UpperCAmelCase:					Any		= random_attention_mask([self.batch_size, self.seq_length]					)

    __UpperCAmelCase:					List[Any]		= None
    if self.use_token_type_ids:
      __UpperCAmelCase:					Optional[int]		= ids_tensor([self.batch_size, self.seq_length]   ,    self.type_vocab_size					)

    __UpperCAmelCase:					List[Any]		= AlbertConfig(
        vocab_size=self.vocab_size   ,    hidden_size=self.hidden_size   ,    num_hidden_layers=self.num_hidden_layers   ,    num_attention_heads=self.num_attention_heads   ,    intermediate_size=self.intermediate_size   ,    hidden_act=self.hidden_act   ,    hidden_dropout_prob=self.hidden_dropout_prob   ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,    max_position_embeddings=self.max_position_embeddings   ,    type_vocab_size=self.type_vocab_size   ,    is_decoder=snake_case_   ,    initializer_range=self.initializer_range   ,    )

    return config, input_ids, token_type_ids, attention_mask







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Tuple		= self.prepare_config_and_inputs()
    __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase:					List[str]		= config_and_inputs
    __UpperCAmelCase:					List[Any]		= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
    return config, inputs_dict




@require_flax
class    a (       __lowerCAmelCase						,  unittest.TestCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					(
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= FlaxAlbertModelTester(self					)







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    for model_class_name in self.all_model_classes:
      __UpperCAmelCase:					str		= model_class_name.from_pretrained("""albert-base-v2"""					)
      __UpperCAmelCase:					int		= model(np.ones((1, 1)					)					)
      self.assertIsNotNone(snake_case_					)




@require_flax
class    a (       unittest.TestCase				):



  """simple docstring"""







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= FlaxAlbertModel.from_pretrained("""albert-base-v2"""					)
    __UpperCAmelCase:					Dict		= np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
    __UpperCAmelCase:					int		= np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
    __UpperCAmelCase:					Optional[int]		= model(snake_case_   ,    attention_mask=snake_case_					)[0]
    __UpperCAmelCase:					Optional[int]		= (1, 11, 768)
    self.assertEqual(output.shape   ,    snake_case_					)
    __UpperCAmelCase:					str		= np.array(
        [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]					)

    self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,    snake_case_   ,    atol=1e-4					)					)