






'''simple docstring'''
import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


UpperCamelCase__			=      re.compile(r'\b(a|an|the)\b', re.UNICODE)

UpperCamelCase__			=      None




def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   lowercase_						:     List[str]							     =  argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."				)
   parser.add_argument("data_file"    ,		metavar="data.json"    ,		help="Input data JSON file."				)
   parser.add_argument("pred_file"    ,		metavar="pred.json"    ,		help="Model predictions."				)
   parser.add_argument(
       "--out-file"    ,		"-o"    ,		metavar="eval.json"    ,		help="Write accuracy metrics to file (default is stdout)."				)
   parser.add_argument(
       "--na-prob-file"    ,		"-n"    ,		metavar="na_prob.json"    ,		help="Model estimates of probability of no answer."				)
   parser.add_argument(
       "--na-prob-thresh"    ,		"-t"    ,		type=_UpperCamelCase    ,		default=1.0    ,		help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."    ,		)
   parser.add_argument(
       "--out-image-dir"    ,		"-p"    ,		metavar="out_images"    ,		default=_UpperCamelCase    ,		help="Save precision-recall curves to directory."				)
   parser.add_argument("--verbose"    ,		"-v"    ,		action="store_true"				)
   if len(sys.argv				) == 1:
      parser.print_help()
      sys.exit(1				)
   return parser.parse_args()




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     int							     =  {}
   for article in dataset:
      for p in article["paragraphs"]:
         for qa in p["qas"]:
            lowercase_						:     str							     =  bool(qa["answers"]["text"]				)
   return qid_to_has_ans




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   def remove_articles(_UpperCamelCase				):
      return ARTICLES_REGEX.sub(" "    ,		_UpperCamelCase				)

   def white_space_fix(_UpperCamelCase				):
      return " ".join(text.split()				)

   def remove_punc(_UpperCamelCase				):
      lowercase_						:     List[Any]							     =  set(string.punctuation				)
      return "".join(ch for ch in text if ch not in exclude				)

   def lower(_UpperCamelCase				):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(_UpperCamelCase				)				)				)				)




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   if not s:
      return []
   return normalize_answer(_UpperCamelCase				).split()




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   return int(normalize_answer(_UpperCamelCase				) == normalize_answer(_UpperCamelCase				)				)




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     int							     =  get_tokens(_UpperCamelCase				)
   lowercase_						:     Tuple							     =  get_tokens(_UpperCamelCase				)
   lowercase_						:     str							     =  collections.Counter(_UpperCamelCase				) & collections.Counter(_UpperCamelCase				)
   lowercase_						:     Optional[int]							     =  sum(common.values()				)
   if len(_UpperCamelCase				) == 0 or len(_UpperCamelCase				) == 0:
      # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
      return int(gold_toks == pred_toks				)
   if num_same == 0:
      return 0
   lowercase_						:     Optional[Any]							     =  1.0 * num_same / len(_UpperCamelCase				)
   lowercase_						:     Optional[int]							     =  1.0 * num_same / len(_UpperCamelCase				)
   lowercase_						:     Union[str, Any]							     =  (2 * precision * recall) / (precision + recall)
   return fa




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     str							     =  {}
   lowercase_						:     List[str]							     =  {}
   for article in dataset:
      for p in article["paragraphs"]:
         for qa in p["qas"]:
            lowercase_						:     List[Any]							     =  qa["id"]
            lowercase_						:     Union[str, Any]							     =  [t for t in qa["answers"]["text"] if normalize_answer(_UpperCamelCase				)]
            if not gold_answers:
               # For unanswerable questions, only correct answer is empty string
               lowercase_						:     List[Any]							     =  [""]
            if qid not in preds:
               print(F"""Missing prediction for {qid}"""				)
               continue
            lowercase_						:     Optional[Any]							     =  preds[qid]
            # Take max over all gold answers
            lowercase_						:     str							     =  max(compute_exact(_UpperCamelCase    ,		_UpperCamelCase				) for a in gold_answers				)
            lowercase_						:     int							     =  max(compute_fa(_UpperCamelCase    ,		_UpperCamelCase				) for a in gold_answers				)
   return exact_scores, fa_scores




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Dict							     =  {}
   for qid, s in scores.items():
      lowercase_						:     int							     =  na_probs[qid] > na_prob_thresh
      if pred_na:
         lowercase_						:     Any							     =  float(not qid_to_has_ans[qid]				)
      else:
         lowercase_						:     List[str]							     =  s
   return new_scores




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase=None				):


   """simple docstring"""




   if not qid_list:
      lowercase_						:     str							     =  len(_UpperCamelCase				)
      return collections.OrderedDict(
          [
              ("exact", 100.0 * sum(exact_scores.values()				) / total),
              ("f1", 100.0 * sum(fa_scores.values()				) / total),
              ("total", total),
          ]				)
   else:
      lowercase_						:     List[Any]							     =  len(_UpperCamelCase				)
      return collections.OrderedDict(
          [
              ("exact", 100.0 * sum(exact_scores[k] for k in qid_list				) / total),
              ("f1", 100.0 * sum(fa_scores[k] for k in qid_list				) / total),
              ("total", total),
          ]				)




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   for k in new_eval:
      lowercase_						:     str							     =  new_eval[k]




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   plt.step(_UpperCamelCase    ,		_UpperCamelCase    ,		color="b"    ,		alpha=0.2    ,		where="post"				)
   plt.fill_between(_UpperCamelCase    ,		_UpperCamelCase    ,		step="post"    ,		alpha=0.2    ,		color="b"				)
   plt.xlabel("Recall"				)
   plt.ylabel("Precision"				)
   plt.xlim([0.0, 1.05]				)
   plt.ylim([0.0, 1.05]				)
   plt.title(_UpperCamelCase				)
   plt.savefig(_UpperCamelCase				)
   plt.clf()




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase=None    ,		_UpperCamelCase=None				):


   """simple docstring"""




   lowercase_						:     Any							     =  sorted(_UpperCamelCase    ,		key=lambda _UpperCamelCase				: na_probs[k]				)
   lowercase_						:     List[str]							     =  0.0
   lowercase_						:     List[Any]							     =  1.0
   lowercase_						:     str							     =  0.0
   lowercase_						:     List[Any]							     =  [1.0]
   lowercase_						:     Tuple							     =  [0.0]
   lowercase_						:     List[Any]							     =  0.0
   for i, qid in enumerate(_UpperCamelCase				):
      if qid_to_has_ans[qid]:
         true_pos += scores[qid]
      lowercase_						:     List[Any]							     =  true_pos / float(i + 1				)
      lowercase_						:     str							     =  true_pos / float(_UpperCamelCase				)
      if i == len(_UpperCamelCase				) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
         # i.e., if we can put a threshold after this point
         avg_prec += cur_p * (cur_r - recalls[-1])
         precisions.append(_UpperCamelCase				)
         recalls.append(_UpperCamelCase				)
   if out_image:
      plot_pr_curve(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				)
   return {"ap": 100.0 * avg_prec}




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   if out_image_dir and not os.path.exists(_UpperCamelCase				):
      os.makedirs(_UpperCamelCase				)
   lowercase_						:     Optional[Any]							     =  sum(1 for v in qid_to_has_ans.values() if v				)
   if num_true_pos == 0:
      return
   lowercase_						:     List[str]							     =  make_precision_recall_eval(
       _UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		out_image=os.path.join(_UpperCamelCase    ,		"pr_exact.png"				)    ,		title="Precision-Recall curve for Exact Match score"    ,		)
   lowercase_						:     int							     =  make_precision_recall_eval(
       _UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		out_image=os.path.join(_UpperCamelCase    ,		"pr_f1.png"				)    ,		title="Precision-Recall curve for F1 score"    ,		)
   lowercase_						:     str							     =  {k: float(_UpperCamelCase				) for k, v in qid_to_has_ans.items()}
   lowercase_						:     Any							     =  make_precision_recall_eval(
       _UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		out_image=os.path.join(_UpperCamelCase    ,		"pr_oracle.png"				)    ,		title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"    ,		)
   merge_eval(_UpperCamelCase    ,		_UpperCamelCase    ,		"pr_exact"				)
   merge_eval(_UpperCamelCase    ,		_UpperCamelCase    ,		"pr_f1"				)
   merge_eval(_UpperCamelCase    ,		_UpperCamelCase    ,		"pr_oracle"				)




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   if not qid_list:
      return
   lowercase_						:     int							     =  [na_probs[k] for k in qid_list]
   lowercase_						:     List[Any]							     =  np.ones_like(_UpperCamelCase				) / float(len(_UpperCamelCase				)				)
   plt.hist(_UpperCamelCase    ,		weights=_UpperCamelCase    ,		bins=20    ,		range=(0.0, 1.0)				)
   plt.xlabel("Model probability of no-answer"				)
   plt.ylabel("Proportion of dataset"				)
   plt.title(F"""Histogram of no-answer probability: {name}"""				)
   plt.savefig(os.path.join(_UpperCamelCase    ,		F"""na_prob_hist_{name}.png"""				)				)
   plt.clf()




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     List[str]							     =  sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]				)
   lowercase_						:     Tuple							     =  num_no_ans
   lowercase_						:     List[str]							     =  cur_score
   lowercase_						:     str							     =  0.0
   lowercase_						:     List[Any]							     =  sorted(_UpperCamelCase    ,		key=lambda _UpperCamelCase				: na_probs[k]				)
   for i, qid in enumerate(_UpperCamelCase				):
      if qid not in scores:
         continue
      if qid_to_has_ans[qid]:
         lowercase_						:     int							     =  scores[qid]
      else:
         if preds[qid]:
            lowercase_						:     Optional[int]							     =  -1
         else:
            lowercase_						:     Any							     =  0
      cur_score += diff
      if cur_score > best_score:
         lowercase_						:     Optional[int]							     =  cur_score
         lowercase_						:     str							     =  na_probs[qid]
   return 100.0 * best_score / len(_UpperCamelCase				), best_thresh




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				):


   """simple docstring"""




   lowercase_     ,		lowercase_						:     Any							     =  find_best_thresh(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				)
   lowercase_     ,		lowercase_						:     List[str]							     =  find_best_thresh(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				)
   lowercase_						:     Any							     =  best_exact
   lowercase_						:     Any							     =  exact_thresh
   lowercase_						:     List[Any]							     =  best_fa
   lowercase_						:     List[Any]							     =  fa_thresh




def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   with open(OPTS.data_file				) as f:
      lowercase_						:     Optional[int]							     =  json.load(_UpperCamelCase				)
      lowercase_						:     Tuple							     =  dataset_json["data"]
   with open(OPTS.pred_file				) as f:
      lowercase_						:     Any							     =  json.load(_UpperCamelCase				)
   if OPTS.na_prob_file:
      with open(OPTS.na_prob_file				) as f:
         lowercase_						:     Optional[int]							     =  json.load(_UpperCamelCase				)
   else:
      lowercase_						:     str							     =  {k: 0.0 for k in preds}
   lowercase_						:     Union[str, Any]							     =  make_qid_to_has_ans(_UpperCamelCase				)  # maps qid to True/False
   lowercase_						:     Union[str, Any]							     =  [k for k, v in qid_to_has_ans.items() if v]
   lowercase_						:     Optional[int]							     =  [k for k, v in qid_to_has_ans.items() if not v]
   lowercase_     ,		lowercase_						:     str							     =  get_raw_scores(_UpperCamelCase    ,		_UpperCamelCase				)
   lowercase_						:     Tuple							     =  apply_no_ans_threshold(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		OPTS.na_prob_thresh				)
   lowercase_						:     str							     =  apply_no_ans_threshold(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		OPTS.na_prob_thresh				)
   lowercase_						:     Optional[Any]							     =  make_eval_dict(_UpperCamelCase    ,		_UpperCamelCase				)
   if has_ans_qids:
      lowercase_						:     Dict							     =  make_eval_dict(_UpperCamelCase    ,		_UpperCamelCase    ,		qid_list=_UpperCamelCase				)
      merge_eval(_UpperCamelCase    ,		_UpperCamelCase    ,		"HasAns"				)
   if no_ans_qids:
      lowercase_						:     Any							     =  make_eval_dict(_UpperCamelCase    ,		_UpperCamelCase    ,		qid_list=_UpperCamelCase				)
      merge_eval(_UpperCamelCase    ,		_UpperCamelCase    ,		"NoAns"				)
   if OPTS.na_prob_file:
      find_all_best_thresh(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase				)
   if OPTS.na_prob_file and OPTS.out_image_dir:
      run_precision_recall_analysis(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		OPTS.out_image_dir				)
      histogram_na_prob(_UpperCamelCase    ,		_UpperCamelCase    ,		OPTS.out_image_dir    ,		"hasAns"				)
      histogram_na_prob(_UpperCamelCase    ,		_UpperCamelCase    ,		OPTS.out_image_dir    ,		"noAns"				)
   if OPTS.out_file:
      with open(OPTS.out_file    ,		"w"				) as f:
         json.dump(_UpperCamelCase    ,		_UpperCamelCase				)
   else:
      print(json.dumps(_UpperCamelCase    ,		indent=2				)				)


if __name__ == "__main__":
 UpperCamelCase__			=      parse_args()
 if OPTS.out_image_dir:
  import matplotlib

  matplotlib.use('Agg')
  import matplotlib.pyplot as plt
 main()












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




    def __init__(      self	:							int	,  a	:							Dict	,  a	:							Optional[Any]=1_3	,  a	:							str=3_0	,  a	:							Dict=2	,  a	:							List[Any]=3	,  a	:							Optional[int]=True	,  a	:							Tuple=True	,  a	:							List[Any]=3_2	,  a	:							int=2	,  a	:							List[str]=4	,  a	:							Dict=3_7	,  a	:							Optional[Any]="gelu"	,  a	:							List[str]=0.1	,  a	:							Dict=0.1	,  a	:							List[Any]=1_0	,  a	:							Union[str, Any]=0.02	,  a	:							Union[str, Any]=3	,  a	:							int=None	,  a	:							Any=2	,  ):






       '''simple docstring'''






       lowercase_						:     Any							     =  parent
       lowercase_						:     Union[str, Any]							     =  batch_size
       lowercase_						:     Any							     =  image_size
       lowercase_						:     str							     =  patch_size
       lowercase_						:     int							     =  num_channels
       lowercase_						:     str							     =  is_training
       lowercase_						:     str							     =  use_labels
       lowercase_						:     List[Any]							     =  hidden_size
       lowercase_						:     List[str]							     =  num_hidden_layers
       lowercase_						:     Dict							     =  num_attention_heads
       lowercase_						:     Optional[int]							     =  intermediate_size
       lowercase_						:     str							     =  hidden_act
       lowercase_						:     Union[str, Any]							     =  hidden_dropout_prob
       lowercase_						:     List[str]							     =  attention_probs_dropout_prob
       lowercase_						:     List[str]							     =  type_sequence_label_size
       lowercase_						:     List[Any]							     =  initializer_range
       lowercase_						:     str							     =  scope
       lowercase_						:     Tuple							     =  encoder_stride

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




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

       lowercase_						:     List[Any]							     =  None
       if self.use_labels:
          lowercase_						:     int							     =  ids_tensor([self.batch_size]	,  self.type_sequence_label_size      )

       lowercase_						:     Optional[Any]							     =  self.get_config()

       return config, pixel_values, labels




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       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=a	,  initializer_range=self.initializer_range	,  encoder_stride=self.encoder_stride	,  )




    def 					lowerCAmelCase__					(      self	:							Any	,  a	:							List[str]	,  a	:							str	,  a	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  TFDeiTModel(config=a      )
       lowercase_						:     int							     =  model(a      )
       self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)      )




    def 					lowerCAmelCase__					(      self	:							Dict	,  a	:							Optional[int]	,  a	:							List[Any]	,  a	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  TFDeiTForMaskedImageModeling(config=a      )
       lowercase_						:     Union[str, Any]							     =  model(a      )
       self.parent.assertEqual(
           result.reconstruction.shape	,  (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

       # test greyscale images
       lowercase_						:     Dict							     =  1
       lowercase_						:     Tuple							     =  TFDeiTForMaskedImageModeling(a      )

       lowercase_						:     Tuple							     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       lowercase_						:     Dict							     =  model(a      )
       self.parent.assertEqual(result.reconstruction.shape	,  (self.batch_size, 1, self.image_size, self.image_size)      )




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							List[str]	,  a	:							List[str]	,  a	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     str							     =  self.type_sequence_label_size
       lowercase_						:     Union[str, Any]							     =  TFDeiTForImageClassification(a      )
       lowercase_						:     int							     =  model(a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )

       # test greyscale images
       lowercase_						:     Dict							     =  1
       lowercase_						:     str							     =  TFDeiTForImageClassification(a      )

       lowercase_						:     Tuple							     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       lowercase_						:     Any							     =  model(a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )





    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  self.prepare_config_and_inputs()
       lowercase_     ,		lowercase_     ,		lowercase_						:     Optional[int]							     =  config_and_inputs
       lowercase_						:     Optional[Any]							     =  {"pixel_values": pixel_values}
       return config, inputs_dict


@require_tf
class 							_UpperCAmelCase    (		snake_case					,       snake_case					,       unittest.TestCase      ):
    __lowerCamelCase: Tuple									=     (
        (
            TFDeiTModel,
            TFDeiTForImageClassification,
            TFDeiTForImageClassificationWithTeacher,
            TFDeiTForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    __lowerCamelCase: Tuple									=     (
        {
            'feature-extraction': TFDeiTModel,
            'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
        }
        if is_tf_available()
        else {}
    )

    __lowerCamelCase: Union[str, Any]									=     False
    __lowerCamelCase: Any									=     False
    __lowerCamelCase: List[str]									=     False
    __lowerCamelCase: Optional[Any]									=     False




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     int							     =  TFDeiTModelTester(self      )
       lowercase_						:     Any							     =  ConfigTester(self	,  config_class=a	,  has_text_modality=a	,  hidden_size=3_7      )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       self.config_tester.run_common_tests()




    @unittest.skip(reason="DeiT does not use inputs_embeds"      )
    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       pass




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Tuple							     =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase_						:     Dict							     =  model_class(a      )
          self.assertIsInstance(model.get_input_embeddings()	,  (tf.keras.layers.Layer)      )
          lowercase_						:     Optional[int]							     =  model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(a	,  tf.keras.layers.Dense      )      )




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Union[str, Any]							     =  self.model_tester.prepare_config_and_inputs_for_common()

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

          lowercase_						:     Dict							     =  ["pixel_values"]
          self.assertListEqual(arg_names[:1]	,  a      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a      )




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*a      )




    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a      )




    def 					lowerCAmelCase__					(      self	:							Any	,  a	:							int	,  a	:							Union[str, Any]	,  a	:							Any=False      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  super()._prepare_for_class(a	,  a	,  return_labels=a      )

       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 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase_						:     List[Any]							     =  TFDeiTModel.from_pretrained(a      )
          self.assertIsNotNone(a      )




def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   lowercase_						:     str							     =  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
   return image





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




    @cached_property
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       return (
           DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"      )
           if is_vision_available()
           else None
       )





    @slow
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"      )

       lowercase_						:     Union[str, Any]							     =  self.default_image_processor
       lowercase_						:     Tuple							     =  prepare_img()
       lowercase_						:     Optional[Any]							     =  image_processor(images=a	,  return_tensors="tf"      )

       # forward pass
       lowercase_						:     int							     =  model(**a      )

       # verify the logits
       lowercase_						:     Union[str, Any]							     =  tf.TensorShape((1, 1_0_0_0)      )
       self.assertEqual(outputs.logits.shape	,  a      )

       lowercase_						:     Optional[Any]							     =  tf.constant([-1.0266, 0.1912, -1.2861]      )

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




