





"""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 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			tempfile.mkdtemp()
									UpperCamelCase				:		str				   =			5

									# Realm tok
									UpperCamelCase				:		Dict				   =			[
									    """[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				:		str				   =			os.path.join(self.tmpdirname						,   """realm_tokenizer"""    )
									os.makedirs(_A						,   exist_ok=_A    )
									UpperCamelCase				:		Optional[Any]				   =			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				:		int				   =			os.path.join(self.tmpdirname						,   """realm_block_records"""    )
									os.makedirs(_A						,   exist_ok=_A    )







			def   _a      (      self    ):






									'''simple docstring'''







									return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname						,   """realm_tokenizer"""    )    )







			def   _a      (      self    ):






									'''simple docstring'''







									shutil.rmtree(self.tmpdirname    )







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Dict				   =			RealmConfig(num_block_records=self.num_block_records    )
									return config







			def   _a      (      self    ):






									'''simple docstring'''







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







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Tuple				   =			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   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			RealmRetriever(
									    block_records=self.get_dummy_block_records()						,   tokenizer=self.get_tokenizer()						,   )
									return retriever







			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		int				   =			self.get_config()
									UpperCamelCase				:		Optional[int]				   =			self.get_dummy_retriever()
									UpperCamelCase				:		Any				   =			retriever.tokenizer

									UpperCamelCase				:		Any				   =			np.array([0, 3]						,   dtype="""long"""    )
									UpperCamelCase				:		Optional[Any]				   =			tokenizer(["""Test question"""]    ).input_ids
									UpperCamelCase				:		Tuple				   =			tokenizer(
									    ["""the fourth"""]						,   add_special_tokens=_A						,   return_token_type_ids=_A						,   return_attention_mask=_A						,   ).input_ids
									UpperCamelCase				:		str				   =			config.reader_seq_len

									UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		Any				   =			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, 1_0)    )
									self.assertEqual(concat_inputs.attention_mask.shape						,   (2, 1_0)    )
									self.assertEqual(concat_inputs.token_type_ids.shape						,   (2, 1_0)    )
									self.assertEqual(concat_inputs.special_tokens_mask.shape						,   (2, 1_0)    )
									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   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		Union[str, Any]				   =			self.get_config()
									UpperCamelCase				:		List[str]				   =			self.get_dummy_retriever()
									UpperCamelCase				:		List[Any]				   =			retriever.tokenizer

									UpperCamelCase				:		Optional[int]				   =			np.array([0, 3, 5]						,   dtype="""long"""    )
									UpperCamelCase				:		Any				   =			tokenizer(["""Test question"""]    ).input_ids
									UpperCamelCase				:		str				   =			tokenizer(
									    ["""the fourth""", """longer longer"""]						,   add_special_tokens=_A						,   return_token_type_ids=_A						,   return_attention_mask=_A						,   ).input_ids
									UpperCamelCase				:		Dict				   =			config.reader_seq_len

									UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase				:		Optional[int]				   =			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   _a      (      self    ):






									'''simple docstring'''







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

									# Test local path
									UpperCamelCase				:		Optional[int]				   =			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				:		Optional[Any]				   =			os.path.join(
															    os.path.join(self.tmpdirname						,   """realm_block_records"""    )						,   _REALM_BLOCK_RECORDS_FILENAME    )
															UpperCamelCase				:		str				   =			RealmRetriever.from_pretrained("""google/realm-cc-news-pretrained-openqa"""    )

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










"""simple docstring"""


from ...configuration_utils import PretrainedConfig
from ...utils import logging


__magic_name__       :					Dict						=			logging.get_logger(__name__)

__magic_name__       :					List[str]						=			{
    """microsoft/swinv2-tiny-patch4-window8-256""": (
        """https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json"""
    ),
}






class 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""
			__lowerCAmelCase     :				List[str]										=  """swinv2"""

			__lowerCAmelCase     :				Optional[Any]										=  {
			    """num_attention_heads""": """num_heads""",
			    """num_hidden_layers""": """num_layers""",
			}







			def __init__(      self						,   _A=2_2_4						,   _A=4						,   _A=3						,   _A=9_6						,   _A=[2, 2, 6, 2]						,   _A=[3, 6, 1_2, 2_4]						,   _A=7						,   _A=4.0						,   _A=True						,   _A=0.0						,   _A=0.0						,   _A=0.1						,   _A="gelu"						,   _A=False						,   _A=0.02						,   _A=1e-5						,   _A=3_2						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(**_A    )

									UpperCamelCase				:		Optional[Any]				   =			image_size
									UpperCamelCase				:		Tuple				   =			patch_size
									UpperCamelCase				:		str				   =			num_channels
									UpperCamelCase				:		Dict				   =			embed_dim
									UpperCamelCase				:		List[str]				   =			depths
									UpperCamelCase				:		Optional[Any]				   =			len(_A    )
									UpperCamelCase				:		Optional[int]				   =			num_heads
									UpperCamelCase				:		Optional[int]				   =			window_size
									UpperCamelCase				:		List[Any]				   =			mlp_ratio
									UpperCamelCase				:		str				   =			qkv_bias
									UpperCamelCase				:		str				   =			hidden_dropout_prob
									UpperCamelCase				:		Tuple				   =			attention_probs_dropout_prob
									UpperCamelCase				:		List[Any]				   =			drop_path_rate
									UpperCamelCase				:		str				   =			hidden_act
									UpperCamelCase				:		Optional[Any]				   =			use_absolute_embeddings
									UpperCamelCase				:		Dict				   =			layer_norm_eps
									UpperCamelCase				:		Dict				   =			initializer_range
									UpperCamelCase				:		str				   =			encoder_stride
									# we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
									# this indicates the channel dimension after the last stage of the model
									UpperCamelCase				:		Any				   =			int(embed_dim * 2 ** (len(_A    ) - 1)    )
									UpperCamelCase				:		str				   =			(0, 0, 0, 0)



