




"""simple docstring"""






from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
				import PIL

UpperCamelCase__						=			logging.get_logger(__name__)





def 							UpperCAmelCase		(		snake_case			:					List[Any]    ):
					if isinstance(snake_case      ,       (list, tuple)    ) and isinstance(videos[0]      ,       (list, tuple)    ) and is_valid_image(videos[0][0]    ):
										return videos

					elif isinstance(snake_case      ,       (list, tuple)    ) and is_valid_image(videos[0]    ):
										return [videos]

					elif is_valid_image(snake_case    ):
										return [[videos]]

					raise ValueError(F'Could not make batched video from {videos}'    )





class   a__			(       UpperCamelCase_  ):
						snake_case__             = ['''pixel_values''']





						def __init__(							self      :	List[Any]     ,a__      :	bool = True     ,a__      :	Dict[str, int] = None     ,a__      :	PILImageResampling = PILImageResampling.BILINEAR     ,a__      :	bool = True     ,a__      :	Dict[str, int] = None     ,a__      :	bool = True     ,a__      :	Union[int, float] = 1 / 255     ,a__      :	bool = True     ,a__      :	bool = True     ,a__      :	Optional[Union[float, List[float]]] = None     ,a__      :	Optional[Union[float, List[float]]] = None     ,**a__      :	Optional[int]     ,)						->       None:




											"""simple docstring"""







											super().__init__(**a__)
											_lowerCAmelCase:List[Any]									=  size if size is not None else {'''shortest_edge''': 256}
											_lowerCAmelCase:Tuple									=  get_size_dict(a__     ,default_to_square=a__)
											_lowerCAmelCase:Optional[Any]									=  crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
											_lowerCAmelCase:Optional[int]									=  get_size_dict(a__     ,param_name='''crop_size''')

											_lowerCAmelCase:Optional[int]									=  do_resize
											_lowerCAmelCase:str									=  size
											_lowerCAmelCase:Union[str, Any]									=  do_center_crop
											_lowerCAmelCase:Dict									=  crop_size
											_lowerCAmelCase:Any									=  resample
											_lowerCAmelCase:Union[str, Any]									=  do_rescale
											_lowerCAmelCase:Dict									=  rescale_factor
											_lowerCAmelCase:Any									=  offset
											_lowerCAmelCase:Union[str, Any]									=  do_normalize
											_lowerCAmelCase:Union[str, Any]									=  image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
											_lowerCAmelCase:Union[str, Any]									=  image_std if image_std is not None else IMAGENET_STANDARD_STD





						def       __UpperCamelCase     (							self      :	str     ,a__      :	np.ndarray     ,a__      :	Dict[str, int]     ,a__      :	PILImageResampling = PILImageResampling.BILINEAR     ,a__      :	Optional[Union[str, ChannelDimension]] = None     ,**a__      :	Optional[Any]     ,)						->       np.ndarray:




											"""simple docstring"""







											_lowerCAmelCase:Optional[Any]									=  get_size_dict(a__     ,default_to_square=a__)
											if "shortest_edge" in size:
																_lowerCAmelCase:Union[str, Any]									=  get_resize_output_image_size(a__     ,size['''shortest_edge''']     ,default_to_square=a__)
											elif "height" in size and "width" in size:
																_lowerCAmelCase:List[Any]									=  (size['''height'''], size['''width'''])
											else:
																raise ValueError(F'Size must have \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}')
											return resize(a__     ,size=a__     ,resample=a__     ,data_format=a__     ,**a__)





						def       __UpperCamelCase     (							self      :	List[Any]     ,a__      :	np.ndarray     ,a__      :	Dict[str, int]     ,a__      :	Optional[Union[str, ChannelDimension]] = None     ,**a__      :	str     ,)						->       np.ndarray:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  get_size_dict(a__)
											if "height" not in size or "width" not in size:
																raise ValueError(F'Size must have \'height\' and \'width\' as keys. Got {size.keys()}')
											return center_crop(a__     ,size=(size['''height'''], size['''width'''])     ,data_format=a__     ,**a__)





						def       __UpperCamelCase     (							self      :	List[str]     ,a__      :	np.ndarray     ,a__      :	Union[int, float]     ,a__      :	bool = True     ,a__      :	Optional[Union[str, ChannelDimension]] = None     ,**a__      :	List[str]     ,)						->       List[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Optional[Any]									=  image.astype(np.floataa)
											if offset:
																_lowerCAmelCase:Any									=  image - (scale / 2)
											return rescale(a__     ,scale=a__     ,data_format=a__     ,**a__)





						def       __UpperCamelCase     (							self      :	Dict     ,a__      :	np.ndarray     ,a__      :	Union[float, List[float]]     ,a__      :	Union[float, List[float]]     ,a__      :	Optional[Union[str, ChannelDimension]] = None     ,**a__      :	Tuple     ,)						->       np.ndarray:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	Union[str, Any]     ,a__      :	ImageInput     ,a__      :	bool = None     ,a__      :	Dict[str, int] = None     ,a__      :	PILImageResampling = None     ,a__      :	bool = None     ,a__      :	Dict[str, int] = None     ,a__      :	bool = None     ,a__      :	float = None     ,a__      :	bool = None     ,a__      :	bool = None     ,a__      :	Optional[Union[float, List[float]]] = None     ,a__      :	Optional[Union[float, List[float]]] = None     ,a__      :	Optional[ChannelDimension] = ChannelDimension.FIRST     ,)						->       np.ndarray:




											"""simple docstring"""







											if do_resize and size is None or resample is None:
																raise ValueError('''Size and resample 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.''')

											if offset and not do_rescale:
																raise ValueError('''For offset, do_rescale must also be set to True.''')

											# All transformations expect numpy arrays.
											_lowerCAmelCase:Dict									=  to_numpy_array(a__)

											if do_resize:
																_lowerCAmelCase:Tuple									=  self.resize(image=a__     ,size=a__     ,resample=a__)

											if do_center_crop:
																_lowerCAmelCase:Optional[Any]									=  self.center_crop(a__     ,size=a__)

											if do_rescale:
																_lowerCAmelCase:Optional[int]									=  self.rescale(image=a__     ,scale=a__     ,offset=a__)

											if do_normalize:
																_lowerCAmelCase:Any									=  self.normalize(image=a__     ,mean=a__     ,std=a__)

											_lowerCAmelCase:List[Any]									=  to_channel_dimension_format(a__     ,a__)
											return image







						def       __UpperCamelCase     (							self      :	str     ,a__      :	ImageInput     ,a__      :	bool = None     ,a__      :	Dict[str, int] = None     ,a__      :	PILImageResampling = None     ,a__      :	bool = None     ,a__      :	Dict[str, int] = None     ,a__      :	bool = None     ,a__      :	float = None     ,a__      :	bool = None     ,a__      :	bool = None     ,a__      :	Optional[Union[float, List[float]]] = None     ,a__      :	Optional[Union[float, List[float]]] = None     ,a__      :	Optional[Union[str, TensorType]] = None     ,a__      :	ChannelDimension = ChannelDimension.FIRST     ,**a__      :	Any     ,)						->       PIL.Image.Image:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  do_resize if do_resize is not None else self.do_resize
											_lowerCAmelCase:str									=  resample if resample is not None else self.resample
											_lowerCAmelCase:List[Any]									=  do_center_crop if do_center_crop is not None else self.do_center_crop
											_lowerCAmelCase:str									=  do_rescale if do_rescale is not None else self.do_rescale
											_lowerCAmelCase:Optional[int]									=  rescale_factor if rescale_factor is not None else self.rescale_factor
											_lowerCAmelCase:Any									=  offset if offset is not None else self.offset
											_lowerCAmelCase:Tuple									=  do_normalize if do_normalize is not None else self.do_normalize
											_lowerCAmelCase:Any									=  image_mean if image_mean is not None else self.image_mean
											_lowerCAmelCase:Optional[Any]									=  image_std if image_std is not None else self.image_std

											_lowerCAmelCase:Optional[int]									=  size if size is not None else self.size
											_lowerCAmelCase:Tuple									=  get_size_dict(a__     ,default_to_square=a__)
											_lowerCAmelCase:Any									=  crop_size if crop_size is not None else self.crop_size
											_lowerCAmelCase:Union[str, Any]									=  get_size_dict(a__     ,param_name='''crop_size''')

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

											_lowerCAmelCase:Union[str, Any]									=  make_batched(a__)

											_lowerCAmelCase:Any									=  [
											    [
											        self._preprocess_image(
											            image=a__     ,do_resize=a__     ,size=a__     ,resample=a__     ,do_center_crop=a__     ,crop_size=a__     ,do_rescale=a__     ,rescale_factor=a__     ,offset=a__     ,do_normalize=a__     ,image_mean=a__     ,image_std=a__     ,data_format=a__     ,)
											        for img in video
											    ]
											    for video in videos
											]

											_lowerCAmelCase:Dict									=  {'''pixel_values''': videos}
											return BatchFeature(data=a__     ,tensor_type=a__)












"""simple docstring"""






import unittest

import numpy as np

from transformers import DistilBertConfig, 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.distilbert.modeling_flax_distilbert import (
				    FlaxDistilBertForMaskedLM,
				    FlaxDistilBertForMultipleChoice,
				    FlaxDistilBertForQuestionAnswering,
				    FlaxDistilBertForSequenceClassification,
				    FlaxDistilBertForTokenClassification,
				    FlaxDistilBertModel,
				)




class   a__			(       unittest.TestCase  ):





						def __init__(							self      :	Dict     ,a__      :	List[Any]     ,a__      :	Optional[int]=13     ,a__      :	Dict=7     ,a__      :	Any=True     ,a__      :	Tuple=True     ,a__      :	Union[str, Any]=True     ,a__      :	Dict=True     ,a__      :	Dict=99     ,a__      :	Optional[Any]=32     ,a__      :	Dict=5     ,a__      :	List[Any]=4     ,a__      :	Union[str, Any]=37     ,a__      :	str="gelu"     ,a__      :	Any=0.1     ,a__      :	Optional[int]=0.1     ,a__      :	Tuple=512     ,a__      :	Any=16     ,a__      :	Any=2     ,a__      :	Dict=0.02     ,a__      :	int=4     ,)						->       Union[str, Any]:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	List[str])						->       int:




											"""simple docstring"""







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

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

											_lowerCAmelCase:Dict									=  DistilBertConfig(
											    vocab_size=self.vocab_size     ,dim=self.hidden_size     ,n_layers=self.num_hidden_layers     ,n_heads=self.num_attention_heads     ,hidden_dim=self.intermediate_size     ,hidden_act=self.hidden_act     ,dropout=self.hidden_dropout_prob     ,attention_dropout=self.attention_probs_dropout_prob     ,max_position_embeddings=self.max_position_embeddings     ,initializer_range=self.initializer_range     ,tie_weights_=a__     ,)

											return config, input_ids, attention_mask







						def       __UpperCamelCase     (							self      :	Any)						->       Any:




											"""simple docstring"""







											_lowerCAmelCase:Optional[Any]									=  self.prepare_config_and_inputs()
											_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase:Optional[Any]									=  config_and_inputs
											_lowerCAmelCase:Any									=  {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
											return config, inputs_dict




@require_flax
class   a__			(       UpperCamelCase_					,	unittest.TestCase  ):
						snake_case__             = (
						    (
						        FlaxDistilBertModel,
						        FlaxDistilBertForMaskedLM,
						        FlaxDistilBertForMultipleChoice,
						        FlaxDistilBertForQuestionAnswering,
						        FlaxDistilBertForSequenceClassification,
						        FlaxDistilBertForTokenClassification,
						        FlaxDistilBertForQuestionAnswering,
						    )
						    if is_flax_available()
						    else ()
						)





						def       __UpperCamelCase     (							self      :	str)						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:str									=  FlaxDistilBertModelTester(self)







						@slow
						def       __UpperCamelCase     (							self      :	List[str])						->       int:




											"""simple docstring"""







											for model_class_name in self.all_model_classes:
																_lowerCAmelCase:List[str]									=  model_class_name.from_pretrained('''distilbert-base-uncased''')
																_lowerCAmelCase:Tuple									=  model(np.ones((1, 1)))
																self.assertIsNotNone(a__)




@require_flax
class   a__			(       unittest.TestCase  ):





						@slow
						def       __UpperCamelCase     (							self      :	Union[str, Any])						->       Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Any									=  FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased''')
											_lowerCAmelCase:Optional[int]									=  np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]])
											_lowerCAmelCase:Tuple									=  np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
											_lowerCAmelCase:List[str]									=  model(a__     ,attention_mask=a__)[0]
											_lowerCAmelCase:List[str]									=  (1, 11, 768)
											self.assertEqual(output.shape     ,a__)
											_lowerCAmelCase:int									=  np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]])

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






