






from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers.models.distilbert.modeling_tf_distilbert import (
							    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFDistilBertForMaskedLM,
							    TFDistilBertForMultipleChoice,
							    TFDistilBertForQuestionAnswering,
							    TFDistilBertForSequenceClassification,
							    TFDistilBertForTokenClassification,
							    TFDistilBertModel,
							)

class 				lowerCAmelCase_	:


				def __init__(					self ,						_lowerCAmelCase ,						):
											_lowercase							:   Optional[int] 							=   parent
											_lowercase							:   List[str] 							=   1_3
											_lowercase							:   Union[str, Any] 							=   7
											_lowercase							:   Optional[Any] 							=   True
											_lowercase							:   Optional[Any] 							=   True
											_lowercase							:   Any 							=   False
											_lowercase							:   int 							=   True
											_lowercase							:   int 							=   9_9
											_lowercase							:   Tuple 							=   3_2
											_lowercase							:   List[str] 							=   2
											_lowercase							:   Union[str, Any] 							=   4
											_lowercase							:   int 							=   3_7
											_lowercase							:   Optional[Any] 							=   'gelu'
											_lowercase							:   Optional[Any] 							=   0.1
											_lowercase							:   Dict 							=   0.1
											_lowercase							:   Tuple 							=   5_1_2
											_lowercase							:   Optional[int] 							=   1_6
											_lowercase							:   Any 							=   2
											_lowercase							:   Union[str, Any] 							=   0.02
											_lowercase							:   str 							=   3
											_lowercase							:   Any 							=   4
											_lowercase							:   List[Any] 							=   None


				def 						__a					(					self      ):
											_lowercase							:   List[str] 							=   ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size      )

											_lowercase							:   Tuple 							=   None
											if self.use_input_mask:
																		_lowercase							:   Dict 							=   random_attention_mask([self.batch_size, self.seq_length]      )

											_lowercase							:   Any 							=   None
											_lowercase							:   int 							=   None
											_lowercase							:   Dict 							=   None
											if self.use_labels:
																		_lowercase							:   Any 							=   ids_tensor([self.batch_size] ,						self.type_sequence_label_size      )
																		_lowercase							:   Optional[Any] 							=   ids_tensor([self.batch_size, self.seq_length] ,						self.num_labels      )
																		_lowercase							:   Optional[Any] 							=   ids_tensor([self.batch_size] ,						self.num_choices      )

											_lowercase							:   Optional[int] 							=   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 ,						)

											return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   List[str] 							=   TFDistilBertModel(config=_lowerCAmelCase      )
											_lowercase							:   List[Any] 							=   {'input_ids': input_ids, 'attention_mask': input_mask}

											_lowercase							:   Tuple 							=   model(_lowerCAmelCase      )

											_lowercase							:   List[str] 							=   [input_ids, input_mask]

											_lowercase							:   Dict 							=   model(_lowerCAmelCase      )

											self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, self.seq_length, self.hidden_size)      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   List[str] 							=   TFDistilBertForMaskedLM(config=_lowerCAmelCase      )
											_lowercase							:   str 							=   {'input_ids': input_ids, 'attention_mask': input_mask}
											_lowercase							:   Dict 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size)      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   Optional[Any] 							=   TFDistilBertForQuestionAnswering(config=_lowerCAmelCase      )
											_lowercase							:   Union[str, Any] 							=   {
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											}
											_lowercase							:   Any 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.start_logits.shape ,						(self.batch_size, self.seq_length)      )
											self.parent.assertEqual(result.end_logits.shape ,						(self.batch_size, self.seq_length)      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   str 							=   self.num_labels
											_lowercase							:   Optional[Any] 							=   TFDistilBertForSequenceClassification(_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   {'input_ids': input_ids, 'attention_mask': input_mask}
											_lowercase							:   Tuple 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_labels)      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   Optional[int] 							=   self.num_choices
											_lowercase							:   List[Any] 							=   TFDistilBertForMultipleChoice(_lowerCAmelCase      )
											_lowercase							:   str 							=   tf.tile(tf.expand_dims(_lowerCAmelCase ,						1      ) ,						(1, self.num_choices, 1)      )
											_lowercase							:   List[Any] 							=   tf.tile(tf.expand_dims(_lowerCAmelCase ,						1      ) ,						(1, self.num_choices, 1)      )
											_lowercase							:   List[str] 							=   {
											    'input_ids': multiple_choice_inputs_ids,
											    'attention_mask': multiple_choice_input_mask,
											}
											_lowercase							:   List[str] 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_choices)      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   Optional[Any] 							=   self.num_labels
											_lowercase							:   Optional[int] 							=   TFDistilBertForTokenClassification(_lowerCAmelCase      )
											_lowercase							:   Dict 							=   {'input_ids': input_ids, 'attention_mask': input_mask}
											_lowercase							:   int 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.num_labels)      )


				def 						__a					(					self      ):
											_lowercase							:   Optional[int] 							=   self.prepare_config_and_inputs()
											((_lowercase)							,							(_lowercase)							,							(_lowercase)							,							(_lowercase)							,							(_lowercase)							,							(_lowercase))							:   int 							=   config_and_inputs
											_lowercase							:   Union[str, Any] 							=   {'input_ids': input_ids, 'attention_mask': input_mask}
											return config, inputs_dict




@require_tf
class 				lowerCAmelCase_	(		__snake_case   ,		__snake_case   ,		unittest.TestCase			):
				_UpperCamelCase					:       Tuple							       = (
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				_UpperCamelCase					:       List[Any]							       = (
				    {
				        "feature-extraction": TFDistilBertModel,
				        "fill-mask": TFDistilBertForMaskedLM,
				        "question-answering": TFDistilBertForQuestionAnswering,
				        "text-classification": TFDistilBertForSequenceClassification,
				        "token-classification": TFDistilBertForTokenClassification,
				        "zero-shot": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				_UpperCamelCase					:       Optional[int]							       = False
				_UpperCamelCase					:       Optional[Any]							       = False


				def 						__a					(					self      ):
											_lowercase							:   Dict 							=   TFDistilBertModelTester(self      )
											_lowercase							:   Optional[Any] 							=   ConfigTester(self ,						config_class=_lowerCAmelCase ,						dim=3_7      )


				def 						__a					(					self      ):
											self.config_tester.run_common_tests()


				def 						__a					(					self      ):
											_lowercase							:   Dict 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_model(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   Optional[Any] 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   Optional[Any] 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   str 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   int 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   int 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCAmelCase      )


				@slow
				def 						__a					(					self      ):
											for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]      ):
																		_lowercase							:   str 							=   TFDistilBertModel.from_pretrained(_lowerCAmelCase      )
																		self.assertIsNotNone(_lowerCAmelCase      )




@require_tf
class 				lowerCAmelCase_	(		unittest.TestCase			):


				@slow
				def 						__a					(					self      ):
											_lowercase							:   List[Any] 							=   TFDistilBertModel.from_pretrained('distilbert-base-uncased'      )
											_lowercase							:   Optional[int] 							=   tf.constant([[0, 1, 2, 3, 4, 5]]      )
											_lowercase							:   Union[str, Any] 							=   model(_lowerCAmelCase      )[0]

											_lowercase							:   Any 							=   [1, 6, 7_6_8]
											self.assertEqual(output.shape ,						_lowerCAmelCase      )

											_lowercase							:   str 							=   tf.constant(
											    [
											        [
											            [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
											            [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
											            [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
											        ]
											    ]      )
											tf.debugging.assert_near(output[:, :3, :3] ,						_lowerCAmelCase ,						atol=1E-4      )








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


if is_vision_available():
							import PIL


__SCREAMING_SNAKE_CASE					:				List[str]									=		logging.get_logger(__name__)






class 					__lowerCAmelCase   (   lowercase						):

						"""simple docstring"""






						_UpperCAmelCase		:       Union[str, Any]   						=["pixel_values"]






						def __init__(  self							:      Optional[int] ,  lowerCAmelCase							:      bool = True ,  lowerCAmelCase							:      Dict[str, int] = None ,  lowerCAmelCase							:      PILImageResampling = PILImageResampling.BICUBIC ,  lowerCAmelCase							:      bool = True ,  lowerCAmelCase							:      Union[int, float] = 1 / 2_55 ,  lowerCAmelCase							:      bool = True ,  lowerCAmelCase							:      Optional[Union[float, List[float]]] = None ,  lowerCAmelCase							:      Optional[Union[float, List[float]]] = None ,  lowerCAmelCase							:      bool = True ,  **lowerCAmelCase							:      List[Any] ,  ):
													super().__init__(**lowerCAmelCase	)
													A_			    =		size if size is not None else {"height": 3_84, "width": 3_84}
													A_			    =		get_size_dict(lowerCAmelCase ,  default_to_square=lowerCAmelCase	)

													A_			    =		do_resize
													A_			    =		size
													A_			    =		resample
													A_			    =		do_rescale
													A_			    =		rescale_factor
													A_			    =		do_normalize
													A_			    =		image_mean if image_mean is not None else OPENAI_CLIP_MEAN
													A_			    =		image_std if image_std is not None else OPENAI_CLIP_STD
													A_			    =		do_convert_rgb






						def 				_UpperCAmelCase       (  self							:      Optional[Any] ,  lowerCAmelCase							:      np.ndarray ,  lowerCAmelCase							:      Dict[str, int] ,  lowerCAmelCase							:      PILImageResampling = PILImageResampling.BICUBIC ,  lowerCAmelCase							:      Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase							:      Tuple ,  ):
													A_			    =		get_size_dict(lowerCAmelCase ,  default_to_square=lowerCAmelCase	)
													if "height" not in size or "width" not in size:
																				raise ValueError(F"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"	)
													A_			    =		(size["height"], size["width"])
													return resize(lowerCAmelCase ,  size=lowerCAmelCase ,  resample=lowerCAmelCase ,  data_format=lowerCAmelCase ,  **lowerCAmelCase	)






						def 				_UpperCAmelCase       (  self							:      str ,  lowerCAmelCase							:      np.ndarray ,  lowerCAmelCase							:      Union[int, float] ,  lowerCAmelCase							:      Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase							:      str ,  ):
													return rescale(lowerCAmelCase ,  scale=lowerCAmelCase ,  data_format=lowerCAmelCase ,  **lowerCAmelCase	)






						def 				_UpperCAmelCase       (  self							:      Optional[int] ,  lowerCAmelCase							:      np.ndarray ,  lowerCAmelCase							:      Union[float, List[float]] ,  lowerCAmelCase							:      Union[float, List[float]] ,  lowerCAmelCase							:      Optional[Union[str, ChannelDimension]] = None ,  **lowerCAmelCase							:      Dict ,  ):
													return normalize(lowerCAmelCase ,  mean=lowerCAmelCase ,  std=lowerCAmelCase ,  data_format=lowerCAmelCase ,  **lowerCAmelCase	)






						def 				_UpperCAmelCase       (  self							:      Tuple ,  lowerCAmelCase							:      ImageInput ,  lowerCAmelCase							:      Optional[bool] = None ,  lowerCAmelCase							:      Optional[Dict[str, int]] = None ,  lowerCAmelCase							:      PILImageResampling = None ,  lowerCAmelCase							:      Optional[bool] = None ,  lowerCAmelCase							:      Optional[float] = None ,  lowerCAmelCase							:      Optional[bool] = None ,  lowerCAmelCase							:      Optional[Union[float, List[float]]] = None ,  lowerCAmelCase							:      Optional[Union[float, List[float]]] = None ,  lowerCAmelCase							:      Optional[Union[str, TensorType]] = None ,  lowerCAmelCase							:      bool = None ,  lowerCAmelCase							:      ChannelDimension = ChannelDimension.FIRST ,  **lowerCAmelCase							:      List[str] ,  ):
													A_			    =		do_resize if do_resize is not None else self.do_resize
													A_			    =		resample if resample is not None else self.resample
													A_			    =		do_rescale if do_rescale is not None else self.do_rescale
													A_			    =		rescale_factor if rescale_factor is not None else self.rescale_factor
													A_			    =		do_normalize if do_normalize is not None else self.do_normalize
													A_			    =		image_mean if image_mean is not None else self.image_mean
													A_			    =		image_std if image_std is not None else self.image_std
													A_			    =		do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

													A_			    =		size if size is not None else self.size
													A_			    =		get_size_dict(lowerCAmelCase ,  default_to_square=lowerCAmelCase	)

													A_			    =		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 or resample is None:
																				raise ValueError("Size and resample must be specified if do_resize 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:
																				A_			    =		[convert_to_rgb(lowerCAmelCase	) for image in images]

													# All transformations expect numpy arrays.
													A_			    =		[to_numpy_array(lowerCAmelCase	) for image in images]

													if do_resize:
																				A_			    =		[self.resize(image=lowerCAmelCase ,  size=lowerCAmelCase ,  resample=lowerCAmelCase	) for image in images]

													if do_rescale:
																				A_			    =		[self.rescale(image=lowerCAmelCase ,  scale=lowerCAmelCase	) for image in images]

													if do_normalize:
																				A_			    =		[self.normalize(image=lowerCAmelCase ,  mean=lowerCAmelCase ,  std=lowerCAmelCase	) for image in images]

													A_			    =		[to_channel_dimension_format(lowerCAmelCase ,  lowerCAmelCase	) for image in images]

													A_			    =		BatchFeature(data={"pixel_values": images} ,  tensor_type=lowerCAmelCase	)

													return encoded_outputs





