





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						:   Union[str, Any]						,lowerCamelCase__						:   Dict						,lowerCamelCase__						:   Optional[int]=13						,lowerCamelCase__						:   Optional[Any]=30						,lowerCamelCase__						:   Tuple=2						,lowerCamelCase__						:   List[Any]=3						,lowerCamelCase__						:   List[Any]=True						,lowerCamelCase__						:   List[Any]=True						,lowerCamelCase__						:   List[str]=32						,lowerCamelCase__						:   Any=2						,lowerCamelCase__						:   Optional[int]=4						,lowerCamelCase__						:   Tuple=37						,lowerCamelCase__						:   Optional[int]="gelu"						,lowerCamelCase__						:   str=0.1						,lowerCamelCase__						:   str=0.1						,lowerCamelCase__						:   Optional[Any]=10						,lowerCamelCase__						:   int=0.02						,lowerCamelCase__						:   List[Any]=3						,lowerCamelCase__						:   Optional[int]=None						,lowerCamelCase__						:   Dict=2						,)					->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						parent
           SCREAMING_SNAKE_CASE		=						batch_size
           SCREAMING_SNAKE_CASE		=						image_size
           SCREAMING_SNAKE_CASE		=						patch_size
           SCREAMING_SNAKE_CASE		=						num_channels
           SCREAMING_SNAKE_CASE		=						is_training
           SCREAMING_SNAKE_CASE		=						use_labels
           SCREAMING_SNAKE_CASE		=						hidden_size
           SCREAMING_SNAKE_CASE		=						num_hidden_layers
           SCREAMING_SNAKE_CASE		=						num_attention_heads
           SCREAMING_SNAKE_CASE		=						intermediate_size
           SCREAMING_SNAKE_CASE		=						hidden_act
           SCREAMING_SNAKE_CASE		=						hidden_dropout_prob
           SCREAMING_SNAKE_CASE		=						attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE		=						type_sequence_label_size
           SCREAMING_SNAKE_CASE		=						initializer_range
           SCREAMING_SNAKE_CASE		=						scope
           SCREAMING_SNAKE_CASE		=						encoder_stride

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






     def     SCREAMING_SNAKE_CASE__  (							self						:   Dict	)					->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

           SCREAMING_SNAKE_CASE		=						self.get_config()

           return config, pixel_values, labels






     def     SCREAMING_SNAKE_CASE__  (							self						:   str	)					->		Any:




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






     def     SCREAMING_SNAKE_CASE__  (							self						:   str						,lowerCamelCase__						:   int						,lowerCamelCase__						:   Optional[Any]						,lowerCamelCase__						:   Optional[Any]	)					->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						TFDeiTModel(config=lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__	)
           self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   Dict						,lowerCamelCase__						:   str						,lowerCamelCase__						:   List[str]						,lowerCamelCase__						:   str	)					->		Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						TFDeiTForMaskedImageModeling(config=lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__	)
           self.parent.assertEqual(
               result.reconstruction.shape						,(self.batch_size, self.num_channels, self.image_size, self.image_size)	)

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

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






     def     SCREAMING_SNAKE_CASE__  (							self						:   List[Any]						,lowerCamelCase__						:   Optional[int]						,lowerCamelCase__						:   int						,lowerCamelCase__						:   Dict	)					->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.type_sequence_label_size
           SCREAMING_SNAKE_CASE		=						TFDeiTForImageClassification(lowerCamelCase__	)
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__						,labels=lowerCamelCase__	)
           self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.type_sequence_label_size)	)

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

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






     def     SCREAMING_SNAKE_CASE__  (							self						:   Any	)					->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.prepare_config_and_inputs()
           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						config_and_inputs
           SCREAMING_SNAKE_CASE		=						{"""pixel_values""": pixel_values}
           return config, inputs_dict

@require_tf
class 		UpperCamelCase__			(				lowerCAmelCase_							,				lowerCAmelCase_							,				unittest.TestCase       ):



     '''simple docstring'''

     __snake_case   :     Optional[Any]		     =      (
         (
             TFDeiTModel,
             TFDeiTForImageClassification,
             TFDeiTForImageClassificationWithTeacher,
             TFDeiTForMaskedImageModeling,
         )
         if is_tf_available()
         else ()
     )
     __snake_case   :     int		     =      (
         {
             "feature-extraction": TFDeiTModel,
             "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
         }
         if is_tf_available()
         else {}
     )

     __snake_case   :     int		     =      False
     __snake_case   :     Union[str, Any]		     =      False
     __snake_case   :     List[Any]		     =      False
     __snake_case   :     Any		     =      False






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[Any]	)					->		Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						TFDeiTModelTester(self	)
           SCREAMING_SNAKE_CASE		=						ConfigTester(self						,config_class=lowerCamelCase__						,has_text_modality=lowerCamelCase__						,hidden_size=37	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[int]	)					->		Dict:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     @unittest.skip(reason="""DeiT does not use inputs_embeds"""	)
     def     SCREAMING_SNAKE_CASE__  (							self						:   Tuple	)					->		Union[str, Any]:




           '''simple docstring'''


           pass






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()

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






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[int]	)					->		Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()

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

                 SCREAMING_SNAKE_CASE		=						["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]						,lowerCamelCase__	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[int]	)					->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase__	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[Any]	)					->		Tuple:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   List[Any]	)					->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__	)






     def     SCREAMING_SNAKE_CASE__  (							self						:   str						,lowerCamelCase__						:   List[Any]						,lowerCamelCase__						:   Optional[Any]						,lowerCamelCase__						:   List[Any]=False	)					->		Union[str, Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						super()._prepare_for_class(lowerCamelCase__						,lowerCamelCase__						,return_labels=lowerCamelCase__	)

           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     SCREAMING_SNAKE_CASE__  (							self						:   str	)					->		int:




           '''simple docstring'''


           for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE		=						TFDeiTModel.from_pretrained(lowerCamelCase__	)
                 self.assertIsNotNone(lowerCamelCase__	)



def        __lowercase       (				)			->       List[str]:


      '''simple docstring'''


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






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



     '''simple docstring'''

     @cached_property
     def     SCREAMING_SNAKE_CASE__  (							self						:   List[str]	)					->		Tuple:




           '''simple docstring'''


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






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""	)

           SCREAMING_SNAKE_CASE		=						self.default_image_processor
           SCREAMING_SNAKE_CASE		=						prepare_img()
           SCREAMING_SNAKE_CASE		=						image_processor(images=lowerCamelCase__						,return_tensors="""tf"""	)

           # forward pass
           SCREAMING_SNAKE_CASE		=						model(**lowerCamelCase__	)

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

           SCREAMING_SNAKE_CASE		=						tf.constant([-1.0266, 0.1912, -1.2861]	)

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









import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


SCREAMING_SNAKE_CASE_						       =	"""\
    Text data.
    Second line of data."""

SCREAMING_SNAKE_CASE_						       =	"""file"""



@pytest.fixture(scope="""session"""     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       Union[str, Any]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						tmp_path_factory.mktemp("""data"""     ) / (FILE_PATH + """.zstd""")
      SCREAMING_SNAKE_CASE		=						bytes(_SCREAMING_SNAKE_CASE			,						"""utf-8"""     )
      with zstd.open(_SCREAMING_SNAKE_CASE			,						"""wb"""     ) as f:
            f.write(_SCREAMING_SNAKE_CASE     )
      return path



@pytest.fixture
def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       List[Any]:


      '''simple docstring'''


      with open(os.path.join(tmpfs.local_root_dir			,						_SCREAMING_SNAKE_CASE     )			,						"""w"""     ) as f:
            f.write(_SCREAMING_SNAKE_CASE     )
      return FILE_PATH



@pytest.mark.parametrize("""compression_format"""			,						["""gzip""", """xz""", """zstd"""]     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     )			->       Tuple:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						{"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
      SCREAMING_SNAKE_CASE		=						input_paths[compression_format]
      SCREAMING_SNAKE_CASE		=						tmp_path / """cache"""
      SCREAMING_SNAKE_CASE		=						DownloadConfig(cache_dir=_SCREAMING_SNAKE_CASE			,						extract_compressed_file=_SCREAMING_SNAKE_CASE     )
      SCREAMING_SNAKE_CASE		=						cached_path(_SCREAMING_SNAKE_CASE			,						download_config=_SCREAMING_SNAKE_CASE     )
      with open(_SCREAMING_SNAKE_CASE     ) as f:
            SCREAMING_SNAKE_CASE		=						f.read()
      with open(_SCREAMING_SNAKE_CASE     ) as f:
            SCREAMING_SNAKE_CASE		=						f.read()
      assert extracted_file_content == expected_file_content



@pytest.mark.parametrize("""default_extracted"""			,						[True, False]     )
@pytest.mark.parametrize("""default_cache_dir"""			,						[True, False]     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     )			->       List[str]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						"""custom_cache"""
      SCREAMING_SNAKE_CASE		=						"""custom_extracted_dir"""
      SCREAMING_SNAKE_CASE		=						tmp_path / """custom_extracted_path"""
      if default_extracted:
            SCREAMING_SNAKE_CASE		=						("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
      else:
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""			,						_SCREAMING_SNAKE_CASE     )
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""			,						str(_SCREAMING_SNAKE_CASE     )     )
            SCREAMING_SNAKE_CASE		=						custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

      SCREAMING_SNAKE_CASE		=						xz_file
      SCREAMING_SNAKE_CASE		=						(
          DownloadConfig(extract_compressed_file=_SCREAMING_SNAKE_CASE     )
          if default_cache_dir
          else DownloadConfig(cache_dir=tmp_path / custom_cache_dir			,						extract_compressed_file=_SCREAMING_SNAKE_CASE     )
      )
      SCREAMING_SNAKE_CASE		=						cached_path(_SCREAMING_SNAKE_CASE			,						download_config=_SCREAMING_SNAKE_CASE     )
      assert Path(_SCREAMING_SNAKE_CASE     ).parent.parts[-2:] == expected



def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       int:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						str(Path(_SCREAMING_SNAKE_CASE     ).resolve()     )
      assert cached_path(_SCREAMING_SNAKE_CASE     ) == text_file
      # relative path
      SCREAMING_SNAKE_CASE		=						str(Path(_SCREAMING_SNAKE_CASE     ).resolve().relative_to(Path(os.getcwd()     )     )     )
      assert cached_path(_SCREAMING_SNAKE_CASE     ) == text_file



def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       Dict:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						str(tmp_path.resolve() / """__missing_file__.txt"""     )
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            cached_path(_SCREAMING_SNAKE_CASE     )
      # relative path
      SCREAMING_SNAKE_CASE		=						"""./__missing_file__.txt"""
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            cached_path(_SCREAMING_SNAKE_CASE     )



def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       str:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						get_from_cache(F"""tmp://{tmpfs_file}"""     )
      with open(_SCREAMING_SNAKE_CASE     ) as f:
            SCREAMING_SNAKE_CASE		=						f.read()
      assert output_file_content == FILE_CONTENT



@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,						_SCREAMING_SNAKE_CASE     )
def        __lowercase       (				)			->       Dict:


      '''simple docstring'''


      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            cached_path("""https://huggingface.co"""     )



@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,						_SCREAMING_SNAKE_CASE     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       Union[str, Any]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						tmp_path_factory.mktemp("""data"""     ) / """file.html"""
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            http_get("""https://huggingface.co"""			,						temp_file=_SCREAMING_SNAKE_CASE     )
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            http_head("""https://huggingface.co"""     )



@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,						_SCREAMING_SNAKE_CASE     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       Optional[int]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						tmp_path_factory.mktemp("""data"""     ) / """file.html"""
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            ftp_get("""ftp://huggingface.co"""			,						temp_file=_SCREAMING_SNAKE_CASE     )
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            ftp_head("""ftp://huggingface.co"""     )



@patch("""datasets.config.HF_DATASETS_OFFLINE"""			,						_SCREAMING_SNAKE_CASE     )
def        __lowercase       (				_SCREAMING_SNAKE_CASE     )			->       int:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						tmp_path_factory.mktemp("""data"""     ) / """file.html"""
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            fsspec_get("""s3://huggingface.co"""			,						temp_file=_SCREAMING_SNAKE_CASE     )
      with pytest.raises(_SCREAMING_SNAKE_CASE     ):
            fsspec_head("""s3://huggingface.co"""     )


