



from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
         TFDeiTModel,
     )
     from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import DeiTImageProcessor
class    __UpperCamelCase			:

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1_3		,				SCREAMING_SNAKE_CASE=3_0		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=3		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=True		,				SCREAMING_SNAKE_CASE=3_2		,				SCREAMING_SNAKE_CASE=2		,				SCREAMING_SNAKE_CASE=4		,				SCREAMING_SNAKE_CASE=3_7		,				SCREAMING_SNAKE_CASE="gelu"		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=0.1		,				SCREAMING_SNAKE_CASE=1_0		,				SCREAMING_SNAKE_CASE=0.02		,				SCREAMING_SNAKE_CASE=3		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=2		,				) ->			List[Any]:
  a__												= parent
  a__												= batch_size
  a__												= image_size
  a__												= patch_size
  a__												= num_channels
  a__												= is_training
  a__												= use_labels
  a__												= hidden_size
  a__												= num_hidden_layers
  a__												= num_attention_heads
  a__												= intermediate_size
  a__												= hidden_act
  a__												= hidden_dropout_prob
  a__												= attention_probs_dropout_prob
  a__												= type_sequence_label_size
  a__												= initializer_range
  a__												= scope
  a__												= encoder_stride

  # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
  a__												= (image_size // patch_size) ** 2
  a__												= num_patches + 2

 def 						_UpperCAmelCase       (	self      ) ->			int:
  a__												= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

  a__												= None
  if self.use_labels:
   a__												= ids_tensor([self.batch_size]		,				self.type_sequence_label_size      )

  a__												= self.get_config()

  return config, pixel_values, labels

 def 						_UpperCAmelCase       (	self      ) ->			int:
  return DeiTConfig(
      image_size=self.image_size		,				patch_size=self.patch_size		,				num_channels=self.num_channels		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				intermediate_size=self.intermediate_size		,				hidden_act=self.hidden_act		,				hidden_dropout_prob=self.hidden_dropout_prob		,				attention_probs_dropout_prob=self.attention_probs_dropout_prob		,				is_decoder=SCREAMING_SNAKE_CASE		,				initializer_range=self.initializer_range		,				encoder_stride=self.encoder_stride		,				)

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			List[str]:
  a__												= TFDeiTModel(config=SCREAMING_SNAKE_CASE      )
  a__												= model(SCREAMING_SNAKE_CASE      )
  self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			Tuple:
  a__												= TFDeiTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE      )
  a__												= model(SCREAMING_SNAKE_CASE      )
  self.parent.assertEqual(
      result.reconstruction.shape		,				(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

  # test greyscale images
  a__												= 1
  a__												= TFDeiTForMaskedImageModeling(SCREAMING_SNAKE_CASE      )

  a__												= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
  a__												= model(SCREAMING_SNAKE_CASE      )
  self.parent.assertEqual(result.reconstruction.shape		,				(self.batch_size, 1, self.image_size, self.image_size)      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			Dict:
  a__												= self.type_sequence_label_size
  a__												= TFDeiTForImageClassification(SCREAMING_SNAKE_CASE      )
  a__												= model(SCREAMING_SNAKE_CASE		,				labels=SCREAMING_SNAKE_CASE      )
  self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)      )

  # test greyscale images
  a__												= 1
  a__												= TFDeiTForImageClassification(SCREAMING_SNAKE_CASE      )

  a__												= floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
  a__												= model(SCREAMING_SNAKE_CASE		,				labels=SCREAMING_SNAKE_CASE      )
  self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)      )



 def 						_UpperCAmelCase       (	self      ) ->			Union[str, Any]:
  a__												= self.prepare_config_and_inputs()
  a__							,			a__							,			a__												= config_and_inputs
  a__												= {'''pixel_values''': pixel_values}
  return config, inputs_dict





@require_tf
class    __UpperCamelCase			(       _lowercase	,	_lowercase	,	unittest.TestCase				):

 """simple docstring"""



 _lowercase   :    str      						=					(
     (
         TFDeiTModel,
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
     )
     if is_tf_available()
     else ()
 )
 _lowercase   :    str      						=					(
     {
         '''feature-extraction''': TFDeiTModel,
         '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
     }
     if is_tf_available()
     else {}
 )

 _lowercase   :    Any      						=					False
 _lowercase   :    Dict      						=					False
 _lowercase   :    Union[str, Any]      						=					False
 _lowercase   :    Any      						=					False

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  a__												= TFDeiTModelTester(self      )
  a__												= ConfigTester(self		,				config_class=SCREAMING_SNAKE_CASE		,				has_text_modality=SCREAMING_SNAKE_CASE		,				hidden_size=3_7      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  self.config_tester.run_common_tests()

 @unittest.skip(reason='''DeiT does not use inputs_embeds'''      )
 def 						_UpperCAmelCase       (	self      ) ->			int:
  pass

 def 						_UpperCAmelCase       (	self      ) ->			Union[str, Any]:
  a__							,			a__												= self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   a__												= model_class(SCREAMING_SNAKE_CASE      )
   self.assertIsInstance(model.get_input_embeddings()		,				(tf.keras.layers.Layer)      )
   a__												= model.get_output_embeddings()
   self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE		,				tf.keras.layers.Dense      )      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  a__							,			a__												= self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   a__												= model_class(SCREAMING_SNAKE_CASE      )
   a__												= inspect.signature(model.call      )
   # signature.parameters is an OrderedDict => so arg_names order is deterministic
   a__												= [*signature.parameters.keys()]

   a__												= ['''pixel_values''']
   self.assertListEqual(arg_names[:1]		,				SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  a__												= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  a__												= self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=False      ) ->			Optional[int]:
  a__												= super()._prepare_for_class(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				return_labels=SCREAMING_SNAKE_CASE      )

  if return_labels:
   if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call      ).parameters:
    del inputs_dict["labels"]

  return inputs_dict



 @slow
 def 						_UpperCAmelCase       (	self      ) ->			Any:
  for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   a__												= TFDeiTModel.from_pretrained(SCREAMING_SNAKE_CASE      )
   self.assertIsNotNone(SCREAMING_SNAKE_CASE      )






def     __a					(		):
 a__												= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
 return image

@require_tf
@require_vision
class    __UpperCamelCase			(       unittest.TestCase				):

 """simple docstring"""



 @cached_property
 def 						_UpperCAmelCase       (	self      ) ->			Tuple:
  return (
      DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''      )
      if is_vision_available()
      else None
  )



 @slow
 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''      )

  a__												= self.default_image_processor
  a__												= prepare_img()
  a__												= image_processor(images=SCREAMING_SNAKE_CASE		,				return_tensors='''tf'''      )

  # forward pass
  a__												= model(**SCREAMING_SNAKE_CASE      )

  # verify the logits
  a__												= tf.TensorShape((1, 1_0_0_0)      )
  self.assertEqual(outputs.logits.shape		,				SCREAMING_SNAKE_CASE      )

  a__												= tf.constant([-1.02_66, 0.19_12, -1.28_61]      )

  self.assertTrue(np.allclose(outputs.logits[0, :3]		,				SCREAMING_SNAKE_CASE		,				atol=1e-4      )      )










import torch
from torch import nn
class    __UpperCamelCase			(       nn.Module				):

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1		,				SCREAMING_SNAKE_CASE=False      ) ->			Tuple:
  super().__init__()

  a__												= n_token
  a__												= d_embed
  a__												= d_proj

  a__												= cutoffs + [n_token]
  a__												= [0] + self.cutoffs
  a__												= div_val

  a__												= self.cutoffs[0]
  a__												= len(self.cutoffs      ) - 1
  a__												= self.shortlist_size + self.n_clusters

  if self.n_clusters > 0:
   a__												= nn.Parameter(torch.zeros(self.n_clusters		,				self.d_embed      )      )
   a__												= nn.Parameter(torch.zeros(self.n_clusters      )      )

  a__												= nn.ModuleList()
  a__												= nn.ParameterList()

  if div_val == 1:
   for i in range(len(self.cutoffs      )      ):
    if d_proj != d_embed:
     self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )
    else:
     self.out_projs.append(SCREAMING_SNAKE_CASE      )

   self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )
  else:
   for i in range(len(self.cutoffs      )      ):
    a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
    a__												= d_embed // (div_val**i)

    self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )

    self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				r_idx - l_idx      )      )

  a__												= keep_order

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			str:
  if proj is None:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
  else:
   # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				proj.t().contiguous()      )
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
   # else:
   #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
   #     if bias is not None:
   #         logit = logit + bias

  return logit

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=False      ) ->			Optional[int]:

  if labels is not None:
   # Shift so that tokens < n predict n
   a__												= hidden[..., :-1, :].contiguous()
   a__												= labels[..., 1:].contiguous()
   a__												= hidden.view(-1		,				hidden.size(-1      )      )
   a__												= labels.view(-1      )
   if hidden.size(0      ) != labels.size(0      ):
    raise RuntimeError('''Input and labels should have the same size in the batch dimension.'''      )
  else:
   a__												= hidden.view(-1		,				hidden.size(-1      )      )

  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   if labels is not None:
    a__												= labels != -1_0_0
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )
    a__												= (
        -nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )[mask].gather(1		,				labels[mask].unsqueeze(1      )      ).squeeze(1      )
    )
   else:
    a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]

   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   if labels is None:
    a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   else:
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )

   a__												= 0
   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if labels is not None:
     a__												= (labels >= l_idx) & (labels < r_idx)
     a__												= mask_i.nonzero().squeeze()

     if indices_i.numel() == 0:
      continue

     a__												= labels.index_select(0		,				SCREAMING_SNAKE_CASE      ) - l_idx
     a__												= head_logprob.index_select(0		,				SCREAMING_SNAKE_CASE      )
     a__												= hidden.index_select(0		,				SCREAMING_SNAKE_CASE      )
    else:
     a__												= hidden

    if i == 0:
     if labels is not None:
      a__												= head_logprob_i.gather(1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )
     a__												= self.cutoffs[0] + i - 1  # No probability for the head cluster
     if labels is not None:
      a__												= head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
          1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
      a__												= logprob_i

    if labels is not None:
     if (hasattr(self		,				'''keep_order'''      ) and self.keep_order) or keep_order:
      out.index_copy_(0		,				SCREAMING_SNAKE_CASE		,				-logprob_i      )
     else:
      out[offset : offset + logprob_i.size(0      )].copy_(-logprob_i      )
     offset += logprob_i.size(0      )

  return out



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			List[str]:
  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   return nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )

   a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if i == 0:
     a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

     a__												= head_logprob[:, -i] + tail_logprob_i
     a__												= logprob_i

   return out





