


'''simple docstring'''
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      lowercase			:

		"""simple docstring"""

		def __init__(	self					,     UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :Optional[Any]							      =				parent
							UpperCamelCase__     :Tuple							      =				13
							UpperCamelCase__     :List[Any]							      =				7
							UpperCamelCase__     :Optional[Any]							      =				True
							UpperCamelCase__     :Optional[Any]							      =				True
							UpperCamelCase__     :str							      =				False
							UpperCamelCase__     :List[str]							      =				True
							UpperCamelCase__     :Optional[int]							      =				99
							UpperCamelCase__     :Optional[Any]							      =				32
							UpperCamelCase__     :Optional[Any]							      =				2
							UpperCamelCase__     :List[str]							      =				4
							UpperCamelCase__     :int							      =				37
							UpperCamelCase__     :Dict							      =				'''gelu'''
							UpperCamelCase__     :List[str]							      =				0.1
							UpperCamelCase__     :Union[str, Any]							      =				0.1
							UpperCamelCase__     :Optional[Any]							      =				512
							UpperCamelCase__     :Dict							      =				16
							UpperCamelCase__     :int							      =				2
							UpperCamelCase__     :List[Any]							      =				0.02
							UpperCamelCase__     :Optional[int]							      =				3
							UpperCamelCase__     :Any							      =				4
							UpperCamelCase__     :Dict							      =				None




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :str							      =				ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size							)

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

							UpperCamelCase__     :Any							      =				None
							UpperCamelCase__     :Tuple							      =				None
							UpperCamelCase__     :str							      =				None
							if self.use_labels:
												UpperCamelCase__     :Tuple							      =				ids_tensor([self.batch_size]					,     self.type_sequence_label_size							)
												UpperCamelCase__     :Tuple							      =				ids_tensor([self.batch_size, self.seq_length]					,     self.num_labels							)
												UpperCamelCase__     :List[Any]							      =				ids_tensor([self.batch_size]					,     self.num_choices							)

							UpperCamelCase__     :Any							      =				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 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :List[str]							      =				TFDistilBertModel(config=UpperCamelCase_							)
							UpperCamelCase__     :Union[str, Any]							      =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}

							UpperCamelCase__     :Any							      =				model(UpperCamelCase_							)

							UpperCamelCase__     :int							      =				[input_ids, input_mask]

							UpperCamelCase__     :Tuple							      =				model(UpperCamelCase_							)

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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :str							      =				TFDistilBertForMaskedLM(config=UpperCamelCase_							)
							UpperCamelCase__     :Optional[Any]							      =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCamelCase__     :str							      =				model(UpperCamelCase_							)
							self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :List[str]							      =				TFDistilBertForQuestionAnswering(config=UpperCamelCase_							)
							UpperCamelCase__     :Optional[Any]							      =				{
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							}
							UpperCamelCase__     :int							      =				model(UpperCamelCase_							)
							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 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :List[Any]							      =				self.num_labels
							UpperCamelCase__     :Optional[int]							      =				TFDistilBertForSequenceClassification(UpperCamelCase_							)
							UpperCamelCase__     :Tuple							      =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCamelCase__     :Optional[Any]							      =				model(UpperCamelCase_							)
							self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_labels)							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[Any]							      =				self.num_labels
							UpperCamelCase__     :Any							      =				TFDistilBertForTokenClassification(UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCamelCase__     :Optional[int]							      =				model(UpperCamelCase_							)
							self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.num_labels)							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


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



@require_tf
class      lowercase			(							A__			,	A__			,	unittest.TestCase      ):

		"""simple docstring"""

		_a									=       (
		    (
		        TFDistilBertModel,
		        TFDistilBertForMaskedLM,
		        TFDistilBertForQuestionAnswering,
		        TFDistilBertForSequenceClassification,
		        TFDistilBertForTokenClassification,
		        TFDistilBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else None
		)
		_a									=       (
		    {
		        'feature-extraction': TFDistilBertModel,
		        'fill-mask': TFDistilBertForMaskedLM,
		        'question-answering': TFDistilBertForQuestionAnswering,
		        'text-classification': TFDistilBertForSequenceClassification,
		        'token-classification': TFDistilBertForTokenClassification,
		        'zero-shot': TFDistilBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_a									=       False
		_a									=       False




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Any							      =				TFDistilBertModelTester(self							)
							UpperCamelCase__     :List[str]							      =				ConfigTester(self					,     config_class=UpperCamelCase_					,     dim=37							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :str							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[Any]							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Dict							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Any							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Tuple							      =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase_							)




		@slow
		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]							):
												UpperCamelCase__     :List[Any]							      =				TFDistilBertModel.from_pretrained(UpperCamelCase_							)
												self.assertIsNotNone(UpperCamelCase_							)



@require_tf
class      lowercase			(							unittest.TestCase      ):

		"""simple docstring"""

		@slow
		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Dict							      =				TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''							)
							UpperCamelCase__     :Tuple							      =				tf.constant([[0, 1, 2, 3, 4, 5]]							)
							UpperCamelCase__     :List[Any]							      =				model(UpperCamelCase_							)[0]

							UpperCamelCase__     :Any							      =				[1, 6, 768]
							self.assertEqual(output.shape					,     UpperCamelCase_							)

							UpperCamelCase__     :Tuple							      =				tf.constant(
							    [
							        [
							            [0.19261885, -0.13732955, 0.4119799],
							            [0.22150156, -0.07422661, 0.39037204],
							            [0.22756018, -0.0896414, 0.3701467],
							        ]
							    ]							)
							tf.debugging.assert_near(output[:, :3, :3]					,     UpperCamelCase_					,     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 (
    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


__snake_case							   =  logging.get_logger(__name__)


if is_vision_available():
		import PIL
class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       ['pixel_values']




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

							'''simple docstring'''


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

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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = PILImageResampling.BICUBIC					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :int							      =				get_size_dict(UpperCamelCase_							)
							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(UpperCamelCase_					,     size=(size['''height'''], size['''width'''])					,     data_format=UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


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




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = None					,     UpperCamelCase_ = ChannelDimension.FIRST					,     **UpperCamelCase_					,     ):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				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__     :Optional[int]							      =				get_size_dict(UpperCamelCase_					,     param_name='''size'''					,     default_to_square=UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				resample if resample is not None else self.resample
							UpperCamelCase__     :int							      =				do_center_crop if do_center_crop is not None else self.do_center_crop
							UpperCamelCase__     :Any							      =				crop_size if crop_size is not None else self.crop_size
							UpperCamelCase__     :Any							      =				get_size_dict(UpperCamelCase_					,     param_name='''crop_size'''					,     default_to_square=UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				do_rescale if do_rescale is not None else self.do_rescale
							UpperCamelCase__     :List[Any]							      =				rescale_factor if rescale_factor is not None else self.rescale_factor
							UpperCamelCase__     :Optional[int]							      =				do_normalize if do_normalize is not None else self.do_normalize
							UpperCamelCase__     :Tuple							      =				image_mean if image_mean is not None else self.image_mean
							UpperCamelCase__     :str							      =				image_std if image_std is not None else self.image_std
							UpperCamelCase__     :Optional[Any]							      =				do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

							UpperCamelCase__     :str							      =				make_list_of_images(UpperCamelCase_							)

							if not valid_images(UpperCamelCase_							):
												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(UpperCamelCase_							) for image in images]

							# All transformations expect numpy arrays.
							UpperCamelCase__     :str							      =				[to_numpy_array(UpperCamelCase_							) for image in images]

							if do_resize:
												UpperCamelCase__     :Optional[Any]							      =				[self.resize(image=UpperCamelCase_					,     size=UpperCamelCase_					,     resample=UpperCamelCase_							) for image in images]

							if do_center_crop:
												UpperCamelCase__     :Dict							      =				[self.center_crop(image=UpperCamelCase_					,     size=UpperCamelCase_							) for image in images]

							if do_rescale:
												UpperCamelCase__     :Optional[Any]							      =				[self.rescale(image=UpperCamelCase_					,     scale=UpperCamelCase_							) for image in images]

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

							UpperCamelCase__     :List[str]							      =				[to_channel_dimension_format(UpperCamelCase_					,     UpperCamelCase_							) for image in images]

							UpperCamelCase__     :Optional[Any]							      =				{'''pixel_values''': images}
							return BatchFeature(data=UpperCamelCase_					,     tensor_type=UpperCamelCase_							)