





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 				a			:






				def __init__(  self			, _lowerCAmelCase			, ):

										"""simple docstring"""




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






				def 			snake_case_    (  self					):

										"""simple docstring"""




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

										__SCREAMING_SNAKE_CASE:				Any           =   None
										if self.use_input_mask:
																__SCREAMING_SNAKE_CASE:				int           =   random_attention_mask([self.batch_size, self.seq_length]					)

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

										__SCREAMING_SNAKE_CASE:				Optional[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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   TFDistilBertModel(config=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}

										__SCREAMING_SNAKE_CASE:				int           =   model(_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   [input_ids, input_mask]

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   model(_lowerCAmelCase					)

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






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




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






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   TFDistilBertForQuestionAnswering(config=_lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				List[str]           =   {
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										}
										__SCREAMING_SNAKE_CASE:				List[str]           =   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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




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






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




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






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




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






				def 			snake_case_    (  self					):

										"""simple docstring"""




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





@require_tf
class 				a			(      __lowercase   ,__lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     (
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     (
				    {
				        "feature-extraction": TFDistilBertModel,
				        "fill-mask": TFDistilBertForMaskedLM,
				        "question-answering": TFDistilBertForQuestionAnswering,
				        "text-classification": TFDistilBertForSequenceClassification,
				        "token-classification": TFDistilBertForTokenClassification,
				        "zero-shot": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE__     :   int		      =     False
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     False






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   TFDistilBertModelTester(self					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   ConfigTester(self			, config_class=_lowerCAmelCase			, dim=37					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.config_tester.run_common_tests()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCAmelCase					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]					):
																__SCREAMING_SNAKE_CASE:				Dict           =   TFDistilBertModel.from_pretrained(_lowerCAmelCase					)
																self.assertIsNotNone(_lowerCAmelCase					)





@require_tf
class 				a			(      unittest.TestCase			):






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				int           =   TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''					)
										__SCREAMING_SNAKE_CASE:				Dict           =   tf.constant([[0, 1, 2, 3, 4, 5]]					)
										__SCREAMING_SNAKE_CASE:				Dict           =   model(_lowerCAmelCase					)[0]

										__SCREAMING_SNAKE_CASE:				str           =   [1, 6, 768]
										self.assertEqual(output.shape			, _lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				List[str]           =   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]			, _lowerCAmelCase			, atol=1e-4					)





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, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_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


lowerCamelCase__						=				logging.get_logger(__name__)







class 							lowerCAmelCase__   ( __lowercase						):
						UpperCamelCase_				:			str						=		["pixel_values"]

						def __init__(      self							,				a = True							,				a = None							,				a = PIL.Image.BICUBIC							,				a = True							,				a = None							,				a = 1 / 2_55							,				a = True							,				a = True							,				a = None							,				a = None							,				**a							,				)       ->   None:


								'''simple docstring'''

								super().__init__(**a					)
								_UpperCamelCase         =						size if size is not None else {"""height""": 2_56, """width""": 2_56}
								_UpperCamelCase         =						get_size_dict(a					)
								_UpperCamelCase         =						crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
								_UpperCamelCase         =						get_size_dict(a							,				param_name="""crop_size"""					)

								_UpperCamelCase         =						do_resize
								_UpperCamelCase         =						size
								_UpperCamelCase         =						resample
								_UpperCamelCase         =						do_center_crop
								_UpperCamelCase         =						crop_size
								_UpperCamelCase         =						do_rescale
								_UpperCamelCase         =						rescale_factor
								_UpperCamelCase         =						do_normalize
								_UpperCamelCase         =						image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
								_UpperCamelCase         =						image_std if image_std is not None else IMAGENET_STANDARD_STD

						def 			A_					(      self							,				a							,				a							,				a = PIL.Image.BICUBIC							,				a = None							,				**a							,				)       ->   np.ndarray:


								'''simple docstring'''

								_UpperCamelCase         =						get_size_dict(a					)
								if "height" not in size or "width" not in size:
										raise ValueError(F'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'					)
								return resize(
								    a							,				size=(size["""height"""], size["""width"""])							,				resample=a							,				data_format=a							,				**a					)

						def 			A_					(      self							,				a							,				a							,				a = None							,				**a							,				)       ->   np.ndarray:


								'''simple docstring'''

								_UpperCamelCase         =						get_size_dict(a					)
								if "height" not in size or "width" not in size:
										raise ValueError(F'The size dictionary must have keys \'height\' and \'width\'. Got {size.keys()}'					)
								return center_crop(a							,				size=(size["""height"""], size["""width"""])							,				data_format=a							,				**a					)

						def 			A_					(      self							,				a							,				a							,				a = None							,				**a							,				)       ->   List[str]:


								'''simple docstring'''

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

						def 			A_					(      self							,				a							,				a							,				a							,				a = None							,				**a							,				)       ->   np.ndarray:


								'''simple docstring'''

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







						def 			A_					(      self							,				a							,				a = None							,				a = None							,				a=None							,				a = None							,				a = None							,				a = None							,				a = None							,				a = None							,				a = None							,				a = None							,				a = None							,				a = ChannelDimension.FIRST							,				**a							,				)       ->   PIL.Image.Image:


								'''simple docstring'''

								_UpperCamelCase         =						do_resize if do_resize is not None else self.do_resize
								_UpperCamelCase         =						resample if resample is not None else self.resample
								_UpperCamelCase         =						do_center_crop if do_center_crop is not None else self.do_center_crop
								_UpperCamelCase         =						do_rescale if do_rescale is not None else self.do_rescale
								_UpperCamelCase         =						rescale_factor if rescale_factor is not None else self.rescale_factor
								_UpperCamelCase         =						do_normalize if do_normalize is not None else self.do_normalize
								_UpperCamelCase         =						image_mean if image_mean is not None else self.image_mean
								_UpperCamelCase         =						image_std if image_std is not None else self.image_std

								_UpperCamelCase         =						size if size is not None else self.size
								_UpperCamelCase         =						get_size_dict(a					)
								_UpperCamelCase         =						crop_size if crop_size is not None else self.crop_size
								_UpperCamelCase         =						get_size_dict(a							,				param_name="""crop_size"""					)

								_UpperCamelCase         =						make_list_of_images(a					)

								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."""					)

								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."""					)

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

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

								if do_center_crop:
										_UpperCamelCase         =						[self.center_crop(image=a							,				size=a					) for image in images]

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

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

								_UpperCamelCase         =						[to_channel_dimension_format(a							,				a					) for image in images]

								_UpperCamelCase         =						{"""pixel_values""": images}
								return BatchFeature(data=a							,				tensor_type=a					)



