'''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        __A      :



    def __init__(self    :    Any   ,			__a    :    str   ,			__a    :    List[str]=13   ,			__a    :    Optional[int]=30   ,			__a    :    Tuple=2   ,			__a    :    str=3   ,			__a    :    Tuple=True   ,			__a    :    List[Any]=True   ,			__a    :    Optional[int]=32   ,			__a    :    Optional[int]=2   ,			__a    :    int=4   ,			__a    :    Optional[Any]=37   ,			__a    :    Optional[Any]="gelu"   ,			__a    :    Optional[Any]=0.1   ,			__a    :    int=0.1   ,			__a    :    int=10   ,			__a    :    Optional[int]=0.02   ,			__a    :    Dict=3   ,			__a    :    Optional[int]=None   ,			__a    :    List[str]=2   ,			):
         UpperCAmelCase_           =   parent
         UpperCAmelCase_           =   batch_size
         UpperCAmelCase_           =   image_size
         UpperCAmelCase_           =   patch_size
         UpperCAmelCase_           =   num_channels
         UpperCAmelCase_           =   is_training
         UpperCAmelCase_           =   use_labels
         UpperCAmelCase_           =   hidden_size
         UpperCAmelCase_           =   num_hidden_layers
         UpperCAmelCase_           =   num_attention_heads
         UpperCAmelCase_           =   intermediate_size
         UpperCAmelCase_           =   hidden_act
         UpperCAmelCase_           =   hidden_dropout_prob
         UpperCAmelCase_           =   attention_probs_dropout_prob
         UpperCAmelCase_           =   type_sequence_label_size
         UpperCAmelCase_           =   initializer_range
         UpperCAmelCase_           =   scope
         UpperCAmelCase_           =   encoder_stride

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



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

         UpperCAmelCase_           =   self.get_config()

         return config, pixel_values, labels



    def 					_lowercase						(self    :    List[str] ):
         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 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    str   ,			__a    :    Any   ,			__a    :    Dict ):
         UpperCAmelCase_           =   TFDeiTModel(config=__a )
         UpperCAmelCase_           =   model(__a )
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size) )



    def 					_lowercase						(self    :    str   ,			__a    :    Optional[int]   ,			__a    :    List[str]   ,			__a    :    Tuple ):
         UpperCAmelCase_           =   TFDeiTForMaskedImageModeling(config=__a )
         UpperCAmelCase_           =   model(__a )
         self.parent.assertEqual(
             result.reconstruction.shape   ,			(self.batch_size, self.num_channels, self.image_size, self.image_size) )

         # test greyscale images
         UpperCAmelCase_           =   1
         UpperCAmelCase_           =   TFDeiTForMaskedImageModeling(__a )

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



    def 					_lowercase						(self    :    List[Any]   ,			__a    :    List[str]   ,			__a    :    int   ,			__a    :    int ):
         UpperCAmelCase_           =   self.type_sequence_label_size
         UpperCAmelCase_           =   TFDeiTForImageClassification(__a )
         UpperCAmelCase_           =   model(__a   ,			labels=__a )
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.type_sequence_label_size) )

         # test greyscale images
         UpperCAmelCase_           =   1
         UpperCAmelCase_           =   TFDeiTForImageClassification(__a )

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






    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self.prepare_config_and_inputs()
         UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   config_and_inputs
         UpperCAmelCase_           =   {"pixel_values": pixel_values}
         return config, inputs_dict




@require_tf
class        __A      ( UpperCamelCase__		,	UpperCamelCase__		,	unittest.TestCase	):
    a__       :						Tuple          =  (
        (
            TFDeiTModel,
            TFDeiTForImageClassification,
            TFDeiTForImageClassificationWithTeacher,
            TFDeiTForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    a__       :						Union[str, Any]          =  (
        {
            """feature-extraction""": TFDeiTModel,
            """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
        }
        if is_tf_available()
        else {}
    )

    a__       :						Tuple          =  False
    a__       :						List[Any]          =  False
    a__       :						Any          =  False
    a__       :						Dict          =  False



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_           =   TFDeiTModelTester(self )
         UpperCAmelCase_           =   ConfigTester(self   ,			config_class=__a   ,			has_text_modality=__a   ,			hidden_size=37 )



    def 					_lowercase						(self    :    Union[str, Any] ):
         self.config_tester.run_common_tests()



    @unittest.skip(reason="DeiT does not use inputs_embeds" )
    def 					_lowercase						(self    :    List[str] ):
         pass



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

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



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_    , UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCAmelCase_           =   ["pixel_values"]
              self.assertListEqual(arg_names[:1]   ,			__a )



    def 					_lowercase						(self    :    Dict ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__a )



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__a )



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__a )



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    Tuple   ,			__a    :    Tuple   ,			__a    :    Union[str, Any]=False ):
         UpperCAmelCase_           =   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 					_lowercase						(self    :    int ):
         for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase_           =   TFDeiTModel.from_pretrained(__a )
              self.assertIsNotNone(__a )





def 	lowerCAmelCase_				(					)    ->      Dict:


     '''simple docstring'''




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






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



    @cached_property
    def 					_lowercase						(self    :    Union[str, Any] ):
         return (
             DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" )
             if is_vision_available()
             else None
         )






    @slow
    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" )

         UpperCAmelCase_           =   self.default_image_processor
         UpperCAmelCase_           =   prepare_img()
         UpperCAmelCase_           =   image_processor(images=__a   ,			return_tensors="tf" )

         # forward pass
         UpperCAmelCase_           =   model(**__a )

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

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

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





'''simple docstring'''





import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer




class        __A      ( UpperCamelCase__	):



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   tempfile.mkdtemp()
         UpperCAmelCase_           =   5

         # Realm tok
         UpperCAmelCase_           =   [
             "[UNK]",
             "[CLS]",
             "[SEP]",
             "[PAD]",
             "[MASK]",
             "test",
             "question",
             "this",
             "is",
             "the",
             "first",
             "second",
             "third",
             "fourth",
             "fifth",
             "record",
             "want",
             "##want",
             "##ed",
             "wa",
             "un",
             "runn",
             "##ing",
             ",",
             "low",
             "lowest",
         ]
         UpperCAmelCase_           =   os.path.join(self.tmpdirname   ,			"realm_tokenizer" )
         os.makedirs(__a   ,			exist_ok=__a )
         UpperCAmelCase_           =   os.path.join(__a   ,			VOCAB_FILES_NAMES["vocab_file"] )
         with open(self.vocab_file   ,			"w"   ,			encoding="utf-8" ) as vocab_writer:
              vocab_writer.write("".join([x + "\n" for x in vocab_tokens] ) )

         UpperCAmelCase_           =   os.path.join(self.tmpdirname   ,			"realm_block_records" )
         os.makedirs(__a   ,			exist_ok=__a )



    def 					_lowercase						(self    :    Optional[Any] ):
         return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname   ,			"realm_tokenizer" ) )



    def 					_lowercase						(self    :    Any ):
         shutil.rmtree(self.tmpdirname )



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_           =   RealmConfig(num_block_records=self.num_block_records )
         return config



    def 					_lowercase						(self    :    List[str] ):
         UpperCAmelCase_           =   Dataset.from_dict(
             {
                 "id": ["0", "1"],
                 "question": ["foo", "bar"],
                 "answers": [["Foo", "Bar"], ["Bar"]],
             } )
         return dataset



    def 					_lowercase						(self    :    Any ):
         UpperCAmelCase_           =   np.array(
             [
                 B"This is the first record",
                 B"This is the second record",
                 B"This is the third record",
                 B"This is the fourth record",
                 B"This is the fifth record",
                 B"This is a longer longer longer record",
             ]   ,			dtype=__a   ,			)
         return block_records



    def 					_lowercase						(self    :    Union[str, Any] ):
         UpperCAmelCase_           =   RealmRetriever(
             block_records=self.get_dummy_block_records()   ,			tokenizer=self.get_tokenizer()   ,			)
         return retriever



    def 					_lowercase						(self    :    int ):
         UpperCAmelCase_           =   self.get_config()
         UpperCAmelCase_           =   self.get_dummy_retriever()
         UpperCAmelCase_           =   retriever.tokenizer

         UpperCAmelCase_           =   np.array([0, 3]   ,			dtype="long" )
         UpperCAmelCase_           =   tokenizer(["Test question"] ).input_ids
         UpperCAmelCase_           =   tokenizer(
             ["the fourth"]   ,			add_special_tokens=__a   ,			return_token_type_ids=__a   ,			return_attention_mask=__a   ,			).input_ids
         UpperCAmelCase_           =   config.reader_seq_len

         UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   retriever(
             __a   ,			__a   ,			answer_ids=__a   ,			max_length=__a   ,			return_tensors="np" )

         self.assertEqual(len(__a )   ,			2 )
         self.assertEqual(len(__a )   ,			2 )
         self.assertEqual(len(__a )   ,			2 )
         self.assertEqual(concat_inputs.input_ids.shape   ,			(2, 10) )
         self.assertEqual(concat_inputs.attention_mask.shape   ,			(2, 10) )
         self.assertEqual(concat_inputs.token_type_ids.shape   ,			(2, 10) )
         self.assertEqual(concat_inputs.special_tokens_mask.shape   ,			(2, 10) )
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0] )   ,			["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "first", "record", "[SEP]"]   ,			)
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1] )   ,			["[CLS]", "test", "question", "[SEP]", "this", "is", "the", "fourth", "record", "[SEP]"]   ,			)



    def 					_lowercase						(self    :    List[Any] ):
         UpperCAmelCase_           =   self.get_config()
         UpperCAmelCase_           =   self.get_dummy_retriever()
         UpperCAmelCase_           =   retriever.tokenizer

         UpperCAmelCase_           =   np.array([0, 3, 5]   ,			dtype="long" )
         UpperCAmelCase_           =   tokenizer(["Test question"] ).input_ids
         UpperCAmelCase_           =   tokenizer(
             ["the fourth", "longer longer"]   ,			add_special_tokens=__a   ,			return_token_type_ids=__a   ,			return_attention_mask=__a   ,			).input_ids
         UpperCAmelCase_           =   config.reader_seq_len

         UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_    , UpperCAmelCase_           =   retriever(
             __a   ,			__a   ,			answer_ids=__a   ,			max_length=__a   ,			return_tensors="np" )

         self.assertEqual([False, True, True]   ,			__a )
         self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]   ,			__a )
         self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]   ,			__a )






    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   self.get_dummy_retriever()
         retriever.save_pretrained(os.path.join(self.tmpdirname   ,			"realm_block_records" ) )

         # Test local path
         UpperCAmelCase_           =   retriever.from_pretrained(os.path.join(self.tmpdirname   ,			"realm_block_records" ) )
         self.assertEqual(retriever.block_records[0]   ,			B"This is the first record" )

         # Test mocked remote path
         with patch("transformers.models.realm.retrieval_realm.hf_hub_download" ) as mock_hf_hub_download:
              UpperCAmelCase_           =   os.path.join(
                  os.path.join(self.tmpdirname   ,			"realm_block_records" )   ,			_REALM_BLOCK_RECORDS_FILENAME )
              UpperCAmelCase_           =   RealmRetriever.from_pretrained("google/realm-cc-news-pretrained-openqa" )

         self.assertEqual(retriever.block_records[0]   ,			B"This is the first record" )




