
"""simple docstring"""





import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


_a		       =							logging.getLogger(__name__)







def 	__a     (		__lowerCamelCase,		__lowerCamelCase  ):
   return (preds == labels).mean()


@dataclass
class A_	:




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__			:						str       =		field(
      metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}      )
  SCREAMING_SNAKE_CASE__			:						Optional[str]       =		field(
      default=lowercase__				,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}      )
  SCREAMING_SNAKE_CASE__			:						Optional[str]       =		field(
      default=lowercase__				,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}      )
  SCREAMING_SNAKE_CASE__			:						Optional[str]       =		field(
      default=lowercase__				,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}				,)




@dataclass
class A_	:




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__			:						str       =		field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys()      )}      )
  SCREAMING_SNAKE_CASE__			:						str       =		field(metadata={"""help""": """Should contain the data files for the task."""}      )
  SCREAMING_SNAKE_CASE__			:						int       =		field(
      default=128				,metadata={
          """help""": (
              """The maximum total input sequence length after tokenization. Sequences longer """
              """than this will be truncated, sequences shorter will be padded."""
          )
      }				,)
  SCREAMING_SNAKE_CASE__			:						bool       =		field(
      default=lowercase__				,metadata={"""help""": """Overwrite the cached training and evaluation sets"""}      )







def 	__a     (		):
   # See all possible arguments in src/transformers/training_args.py
   # or by passing the --help flag to this script.
   # We now keep distinct sets of args, for a cleaner separation of concerns.

   UpperCAmelCase_					:					int			     =    HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)  )
   UpperCAmelCase_     ,   UpperCAmelCase_     ,   UpperCAmelCase_					:					str			     =    parser.parse_args_into_dataclasses()

   if (
       os.path.exists(training_args.output_dir  )
       and os.listdir(training_args.output_dir  )
       and training_args.do_train
       and not training_args.overwrite_output_dir
   ):
      raise ValueError(
          f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
          " --overwrite_output_dir to overcome."  )

   # Setup logging
   logging.basicConfig(
       format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",		datefmt="%m/%d/%Y %H:%M:%S",		level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN,		)
   logger.warning(
       "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",		training_args.local_rank,		training_args.device,		training_args.n_gpu,		bool(training_args.local_rank != -1  ),		training_args.fpaa,		)
   # Set the verbosity to info of the Transformers logger (on main process only):
   if is_main_process(training_args.local_rank  ):
      transformers.utils.logging.set_verbosity_info()
      transformers.utils.logging.enable_default_handler()
      transformers.utils.logging.enable_explicit_format()
   logger.info("Training/evaluation parameters %s",		__lowerCamelCase  )

   # Set seed
   set_seed(training_args.seed  )

   try:
      UpperCAmelCase_					:					Optional[int]			     =    processors[data_args.task_name]()
      UpperCAmelCase_					:					str			     =    processor.get_labels()
      UpperCAmelCase_					:					str			     =    len(__lowerCamelCase  )
   except KeyError:
      raise ValueError("Task not found: %s" % (data_args.task_name)  )

   # Load pretrained model and tokenizer
   #
   # Distributed training:
   # The .from_pretrained methods guarantee that only one local process can concurrently
   # download model & vocab.

   UpperCAmelCase_					:					Optional[int]			     =    AutoConfig.from_pretrained(
       model_args.config_name if model_args.config_name else model_args.model_name_or_path,		num_labels=__lowerCamelCase,		finetuning_task=data_args.task_name,		cache_dir=model_args.cache_dir,		)
   UpperCAmelCase_					:					List[str]			     =    AutoTokenizer.from_pretrained(
       model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,		cache_dir=model_args.cache_dir,		)
   UpperCAmelCase_					:					Optional[int]			     =    AutoModelForMultipleChoice.from_pretrained(
       model_args.model_name_or_path,		from_tf=bool(".ckpt" in model_args.model_name_or_path  ),		config=__lowerCamelCase,		cache_dir=model_args.cache_dir,		)

   # Get datasets
   UpperCAmelCase_					:					Dict			     =    (
       MultipleChoiceDataset(
           data_dir=data_args.data_dir,		tokenizer=__lowerCamelCase,		task=data_args.task_name,		max_seq_length=data_args.max_seq_length,		overwrite_cache=data_args.overwrite_cache,		mode=Split.train,		)
       if training_args.do_train
       else None
   )
   UpperCAmelCase_					:					List[str]			     =    (
       MultipleChoiceDataset(
           data_dir=data_args.data_dir,		tokenizer=__lowerCamelCase,		task=data_args.task_name,		max_seq_length=data_args.max_seq_length,		overwrite_cache=data_args.overwrite_cache,		mode=Split.dev,		)
       if training_args.do_eval
       else None
   )

   def compute_metrics(__lowerCamelCase  ) -> Dict:
      UpperCAmelCase_					:					int			     =    np.argmax(p.predictions,		axis=1  )
      return {"acc": simple_accuracy(__lowerCamelCase,		p.label_ids  )}

   # Data collator
   UpperCAmelCase_					:					List[Any]			     =    DataCollatorWithPadding(__lowerCamelCase,		pad_to_multiple_of=8  ) if training_args.fpaa else None

   # Initialize our Trainer
   UpperCAmelCase_					:					Any			     =    Trainer(
       model=__lowerCamelCase,		args=__lowerCamelCase,		train_dataset=__lowerCamelCase,		eval_dataset=__lowerCamelCase,		compute_metrics=__lowerCamelCase,		data_collator=__lowerCamelCase,		)

   # Training
   if training_args.do_train:
      trainer.train(
          model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path  ) else None  )
      trainer.save_model()
      # For convenience, we also re-save the tokenizer to the same directory,
      # so that you can share your model easily on huggingface.co/models =)
      if trainer.is_world_master():
         tokenizer.save_pretrained(training_args.output_dir  )

    # Evaluation
   UpperCAmelCase_					:					Tuple			     =    {}
   if training_args.do_eval:
      logger.info("*** Evaluate ***"  )

      UpperCAmelCase_					:					List[Any]			     =    trainer.evaluate()

      UpperCAmelCase_					:					int			     =    os.path.join(training_args.output_dir,		"eval_results.txt"  )
      if trainer.is_world_master():
         with open(__lowerCamelCase,		"w"  ) as writer:
            logger.info("***** Eval results *****"  )
            for key, value in result.items():
               logger.info("  %s = %s",		__lowerCamelCase,		__lowerCamelCase  )
               writer.write("%s = %s\n" % (key, value)  )

            results.update(__lowerCamelCase  )

   return results







def 	__a     (		__lowerCamelCase  ):
   # For xla_spawn (TPUs)
   main()


if __name__ == "__main__":
    main()




"""simple docstring"""





import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
        MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
        MODEL_MAPPING,
        DeiTForImageClassification,
        DeiTForImageClassificationWithTeacher,
        DeiTForMaskedImageModeling,
        DeiTModel,
    )
    from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import DeiTImageProcessor


class A_	:




  '''simple docstring'''







  def __init__(		self				,   lowercase_				,   lowercase_=13				,   lowercase_=30				,   lowercase_=2				,   lowercase_=3				,   lowercase_=True				,   lowercase_=True				,   lowercase_=32				,   lowercase_=5				,   lowercase_=4				,   lowercase_=37				,   lowercase_="gelu"				,   lowercase_=0.1				,   lowercase_=0.1				,   lowercase_=10				,   lowercase_=0.02				,   lowercase_=3				,   lowercase_=None				,   lowercase_=2				,   ):

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

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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Any			     =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

     UpperCAmelCase_					:					Tuple			     =    None
     if self.use_labels:
        UpperCAmelCase_					:					Any			     =    ids_tensor([self.batch_size]				,   self.type_sequence_label_size  )

     UpperCAmelCase_					:					Union[str, Any]			     =    self.get_config()

     return config, pixel_values, labels







  def        UpperCamelCase__					(		self  ):

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







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    DeiTModel(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					List[Any]			     =    model(lowercase_  )
     self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)  )







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    DeiTForMaskedImageModeling(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					List[Any]			     =    model(lowercase_  )
     self.parent.assertEqual(
         result.reconstruction.shape				,   (self.batch_size, self.num_channels, self.image_size, self.image_size)  )

     # test greyscale images
     UpperCAmelCase_					:					List[str]			     =    1
     UpperCAmelCase_					:					Optional[Any]			     =    DeiTForMaskedImageModeling(lowercase_  )
     model.to(lowercase_  )
     model.eval()

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







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Tuple			     =    self.type_sequence_label_size
     UpperCAmelCase_					:					Union[str, Any]			     =    DeiTForImageClassification(lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					List[str]			     =    model(lowercase_				,   labels=lowercase_  )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)  )

     # test greyscale images
     UpperCAmelCase_					:					Union[str, Any]			     =    1
     UpperCAmelCase_					:					Optional[int]			     =    DeiTForImageClassification(lowercase_  )
     model.to(lowercase_  )
     model.eval()

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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[str]			     =    self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   
     )					:					Dict			     =    config_and_inputs
     UpperCAmelCase_					:					Optional[int]			     =    {"pixel_values": pixel_values}
     return config, inputs_dict




@require_torch
class A_	(lowercase__				,lowercase__				,unittest.TestCase      ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__			:						Any       =		(
      (
          DeiTModel,
          DeiTForImageClassification,
          DeiTForImageClassificationWithTeacher,
          DeiTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__			:						Tuple       =		(
      {
          """feature-extraction""": DeiTModel,
          """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
      }
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE__			:						List[Any]       =		False
  SCREAMING_SNAKE_CASE__			:						Optional[Any]       =		False
  SCREAMING_SNAKE_CASE__			:						List[str]       =		False







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Any			     =    DeiTModelTester(self  )
     UpperCAmelCase_					:					Optional[int]			     =    ConfigTester(self				,   config_class=lowercase_				,   has_text_modality=lowercase_				,   hidden_size=37  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     self.config_tester.run_common_tests()







  @unittest.skip(reason="DeiT does not use inputs_embeds"  )
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     pass







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					Any			     =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        UpperCAmelCase_					:					List[Any]			     =    model_class(lowercase_  )
        self.assertIsInstance(model.get_input_embeddings()				,   (nn.Module)  )
        UpperCAmelCase_					:					Any			     =    model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(lowercase_				,   nn.Linear  )  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					Dict			     =    self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase_					:					Optional[int]			     =    ["pixel_values"]
        self.assertListEqual(arg_names[:1]				,   lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_  )







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_=False  ):

     """simple docstring"""
     UpperCAmelCase_					:					Tuple			     =    super()._prepare_for_class(lowercase_				,   lowercase_				,   return_labels=lowercase_  )

     if return_labels:
        if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
           del inputs_dict["labels"]

     return inputs_dict







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     if not self.model_tester.is_training:
        return

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

     for model_class in self.all_model_classes:
        # DeiTForImageClassificationWithTeacher supports inference-only
        if (
            model_class in get_values(lowercase_  )
            or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
        ):
           continue
        UpperCAmelCase_					:					Optional[int]			     =    model_class(lowercase_  )
        model.to(lowercase_  )
        model.train()
        UpperCAmelCase_					:					List[Any]			     =    self._prepare_for_class(lowercase_				,   lowercase_				,   return_labels=lowercase_  )
        UpperCAmelCase_					:					Dict			     =    model(**lowercase_  ).loss
        loss.backward()







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					int			     =    self.model_tester.prepare_config_and_inputs_for_common()
     if not self.model_tester.is_training:
        return

     UpperCAmelCase_					:					Dict			     =    False
     UpperCAmelCase_					:					Optional[int]			     =    True

     for model_class in self.all_model_classes:
        if model_class in get_values(lowercase_  ) or not model_class.supports_gradient_checkpointing:
           continue
        # DeiTForImageClassificationWithTeacher supports inference-only
        if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
           continue
        UpperCAmelCase_					:					List[str]			     =    model_class(lowercase_  )
        model.gradient_checkpointing_enable()
        model.to(lowercase_  )
        model.train()
        UpperCAmelCase_					:					Optional[int]			     =    self._prepare_for_class(lowercase_				,   lowercase_				,   return_labels=lowercase_  )
        UpperCAmelCase_					:					Any			     =    model(**lowercase_  ).loss
        loss.backward()







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_     ,   UpperCAmelCase_					:					Tuple			     =    self.model_tester.prepare_config_and_inputs_for_common()

     UpperCAmelCase_					:					Dict			     =    [
         {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
         {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
         {"title": "regression", "num_labels": 1, "dtype": torch.float},
     ]

     for model_class in self.all_model_classes:
        if (
            model_class
            not in [
                *get_values(lowercase_  ),
                *get_values(lowercase_  ),
            ]
            or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
        ):
           continue

        for problem_type in problem_types:
           with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}"""  ):
              UpperCAmelCase_					:					str			     =    problem_type["title"]
              UpperCAmelCase_					:					List[Any]			     =    problem_type["num_labels"]

              UpperCAmelCase_					:					Union[str, Any]			     =    model_class(lowercase_  )
              model.to(lowercase_  )
              model.train()

              UpperCAmelCase_					:					int			     =    self._prepare_for_class(lowercase_				,   lowercase_				,   return_labels=lowercase_  )

              if problem_type["num_labels"] > 1:
                 UpperCAmelCase_					:					List[Any]			     =    inputs["labels"].unsqueeze(1  ).repeat(1				,   problem_type["num_labels"]  )

              UpperCAmelCase_					:					Tuple			     =    inputs["labels"].to(problem_type["dtype"]  )

              # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
              # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
              # they have the same size." which is a symptom something in wrong for the regression problem.
              # See https://github.com/huggingface/transformers/issues/11780
              with warnings.catch_warnings(record=lowercase_  ) as warning_list:
                 UpperCAmelCase_					:					List[str]			     =    model(**lowercase_  ).loss
              for w in warning_list:
                 if "Using a target size that is different to the input size" in str(w.message  ):
                    raise ValueError(
                        F"""Something is going wrong in the regression problem: intercepted {w.message}"""  )

              loss.backward()







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase_					:					Union[str, Any]			     =    DeiTModel.from_pretrained(lowercase_  )
        self.assertIsNotNone(lowercase_  )







def 	__a     (		):
   UpperCAmelCase_					:					Any			     =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
   return image


@require_torch
@require_vision
class A_	(unittest.TestCase      ):




  '''simple docstring'''







  @cached_property
  def        UpperCamelCase__					(		self  ):

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







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Tuple			     =    DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"  ).to(
         lowercase_  )

     UpperCAmelCase_					:					List[str]			     =    self.default_image_processor
     UpperCAmelCase_					:					List[str]			     =    prepare_img()
     UpperCAmelCase_					:					int			     =    image_processor(images=lowercase_				,   return_tensors="pt"  ).to(lowercase_  )

     # forward pass
     with torch.no_grad():
        UpperCAmelCase_					:					Dict			     =    model(**lowercase_  )

     # verify the logits
     UpperCAmelCase_					:					List[str]			     =    torch.Size((1, 1000)  )
     self.assertEqual(outputs.logits.shape				,   lowercase_  )

     UpperCAmelCase_					:					str			     =    torch.tensor([-1.02_66, 0.19_12, -1.28_61]  ).to(lowercase_  )

     self.assertTrue(torch.allclose(outputs.logits[0, :3]				,   lowercase_				,   atol=1E-4  )  )







  @slow
  @require_accelerate
  @require_torch_gpu
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[str]			     =    DeiTModel.from_pretrained(
         "facebook/deit-base-distilled-patch16-224"				,   torch_dtype=torch.floataa				,   device_map="auto"  )
     UpperCAmelCase_					:					str			     =    self.default_image_processor

     UpperCAmelCase_					:					Union[str, Any]			     =    prepare_img()
     UpperCAmelCase_					:					List[Any]			     =    image_processor(images=lowercase_				,   return_tensors="pt"  )
     UpperCAmelCase_					:					List[str]			     =    inputs.pixel_values.to(lowercase_  )

     # forward pass to make sure inference works in fp16
     with torch.no_grad():
        UpperCAmelCase_					:					int			     =    model(lowercase_  )


