


"""simple docstring"""



from typing import Dict, List, Optional, Tuple, Union

import numpy as np

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,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
  import torch


__snake_case   			=    logging.get_logger(__name__)







class _lowerCAmelCase				(      snake_case_	):
   __UpperCAmelCase :       Tuple							  =  ['''pixel_values''']







   def __init__(	self						,	UpperCamelCase__ = True						,	UpperCamelCase__ = None						,	UpperCamelCase__ = PILImageResampling.BILINEAR						,	UpperCamelCase__ = True						,	UpperCamelCase__ = None						,	UpperCamelCase__ = True						,	UpperCamelCase__ = 1 / 255						,	UpperCamelCase__ = True						,	UpperCamelCase__ = None						,	UpperCamelCase__ = None						,	**UpperCamelCase__						,	)	->		None:

       '''simple docstring'''




       super().__init__(**UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    size if size is not None else {"shortest_edge": 256}
       snake_case    :      Optional[int]      					=    get_size_dict(UpperCamelCase__						,	default_to_square=UpperCamelCase__						)
       snake_case    :      Dict      					=    crop_size if crop_size is not None else {"height": 224, "width": 224}
       snake_case    :      Tuple      					=    get_size_dict(UpperCamelCase__						,	param_name="crop_size"						)
       snake_case    :      List[str]      					=    do_resize
       snake_case    :      List[Any]      					=    size
       snake_case    :      int      					=    resample
       snake_case    :      Tuple      					=    do_center_crop
       snake_case    :      List[str]      					=    crop_size
       snake_case    :      int      					=    do_rescale
       snake_case    :      Dict      					=    rescale_factor
       snake_case    :      Union[str, Any]      					=    do_normalize
       snake_case    :      Optional[Any]      					=    image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       snake_case    :      Union[str, Any]      					=    image_std if image_std is not None else IMAGENET_STANDARD_STD







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__ = PILImageResampling.BICUBIC						,	UpperCamelCase__ = None						,	**UpperCamelCase__						,	)	->		np.ndarray:

       '''simple docstring'''




       snake_case    :      Optional[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()}'						)
       snake_case    :      Dict      					=    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__						,	)	->		np.ndarray:

       '''simple docstring'''




       snake_case    :      Tuple      					=    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` and `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__						)	->		np.ndarray:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__ = None						,	**UpperCamelCase__						,	)	->		np.ndarray:

       '''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__ = ChannelDimension.FIRST						,	**UpperCamelCase__						,	)	->		int:

       '''simple docstring'''




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

       snake_case    :      int      					=    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."						)

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

       if do_resize:
           snake_case    :      List[str]      					=    [self.resize(image=UpperCamelCase__						,	size=UpperCamelCase__						,	resample=UpperCamelCase__						) for image in images]

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

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

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

       snake_case    :      int      					=    [to_channel_dimension_format(UpperCamelCase__						,	UpperCamelCase__						) for image in images]

       snake_case    :      Tuple      					=    {"pixel_values": images}
       return BatchFeature(data=UpperCamelCase__						,	tensor_type=UpperCamelCase__						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__ = None						)	->		Any:

       '''simple docstring'''




       snake_case    :      int      					=    outputs.logits

       # Resize logits and compute semantic segmentation maps
       if target_sizes is not None:
           if len(UpperCamelCase__						) != len(UpperCamelCase__						):
               raise ValueError(
                   "Make sure that you pass in as many target sizes as the batch dimension of the logits"						)

           if is_torch_tensor(UpperCamelCase__						):
               snake_case    :      str      					=    target_sizes.numpy()

           snake_case    :      Tuple      					=    []

           for idx in range(len(UpperCamelCase__						)						):
               snake_case    :      Dict      					=    torch.nn.functional.interpolate(
                   logits[idx].unsqueeze(dim=0						)						,	size=target_sizes[idx]						,	mode="bilinear"						,	align_corners=UpperCamelCase__						)
               snake_case    :      Dict      					=    resized_logits[0].argmax(dim=0						)
               semantic_segmentation.append(UpperCamelCase__						)
       else:
           snake_case    :      str      					=    logits.argmax(dim=1						)
           snake_case    :      str      					=    [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]						)]

       return semantic_segmentation









"""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 _lowerCAmelCase				:







   def __init__(	self						,	UpperCamelCase__						,	)	->		str:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




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

       snake_case    :      Optional[Any]      					=    None
       if self.use_input_mask:
           snake_case    :      Dict      					=    random_attention_mask([self.batch_size, self.seq_length]						)

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

       snake_case    :      str      					=    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__						)	->		Union[str, Any]:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    TFDistilBertModel(config=UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}

       snake_case    :      Union[str, Any]      					=    model(UpperCamelCase__						)

       snake_case    :      Tuple      					=    [input_ids, input_mask]

       snake_case    :      Optional[Any]      					=    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__						)	->		Any:

       '''simple docstring'''




       snake_case    :      Tuple      					=    TFDistilBertForMaskedLM(config=UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[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__						)	->		str:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    TFDistilBertForQuestionAnswering(config=UpperCamelCase__						)
       snake_case    :      Optional[Any]      					=    {
           "input_ids": input_ids,
           "attention_mask": input_mask,
       }
       snake_case    :      str      					=    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__						)	->		str:

       '''simple docstring'''




       snake_case    :      int      					=    self.num_labels
       snake_case    :      List[str]      					=    TFDistilBertForSequenceClassification(UpperCamelCase__						)
       snake_case    :      List[str]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[str]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Any:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Dict:

       '''simple docstring'''




       snake_case    :      str      					=    self.num_labels
       snake_case    :      Tuple      					=    TFDistilBertForTokenClassification(UpperCamelCase__						)
       snake_case    :      Union[str, Any]      					=    {"input_ids": input_ids, "attention_mask": input_mask}
       snake_case    :      List[Any]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.num_labels)						)







   def  lowerCamelCase		(	self						)	->		Optional[int]:

       '''simple docstring'''




       snake_case    :      Any      					=    self.prepare_config_and_inputs()
       ((snake_case)					,(snake_case)					,(snake_case)					,(snake_case)					,(snake_case)					,(snake_case))    :      Union[str, Any]      					=    config_and_inputs
       snake_case    :      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 :       List[str]							  =  (
       (
           TFDistilBertModel,
           TFDistilBertForMaskedLM,
           TFDistilBertForQuestionAnswering,
           TFDistilBertForSequenceClassification,
           TFDistilBertForTokenClassification,
           TFDistilBertForMultipleChoice,
       )
       if is_tf_available()
       else None
   )
   __UpperCAmelCase :       Optional[int]							  =  (
       {
           '''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  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      int      					=    TFDistilBertModelTester(self						)
       snake_case    :      str      					=    ConfigTester(self						,	config_class=UpperCamelCase__						,	dim=37						)







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Union[str, Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case    :      int      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Union[str, Any]:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      str      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		List[str]:

       '''simple docstring'''




       snake_case    :      int      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Optional[int]:

       '''simple docstring'''




       snake_case    :      Any      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCamelCase__						)







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

       '''simple docstring'''




       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]						):
           snake_case    :      Optional[int]      					=    TFDistilBertModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







@require_tf
class _lowerCAmelCase				(      unittest.TestCase	):







   @slow
   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case    :      str      					=    TFDistilBertModel.from_pretrained("distilbert-base-uncased"						)
       snake_case    :      Optional[int]      					=    tf.constant([[0, 1, 2, 3, 4, 5]]						)
       snake_case    :      Any      					=    model(UpperCamelCase__						)[0]

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

       snake_case    :      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]						,	UpperCamelCase__						,	atol=1e-4						)





