






import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, 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_VIDEO_CLASSIFICATION_MAPPING,
      VideoMAEForPreTraining,
      VideoMAEForVideoClassification,
      VideoMAEModel,
  )
  from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from transformers import VideoMAEImageProcessor


class    __lowercase   :







     '''simple docstring'''




     def __init__(					self							:      str			,						UpperCamelCase_							:      Union[str, Any]			,						UpperCamelCase_							:      str=13			,						UpperCamelCase_							:      List[str]=10			,						UpperCamelCase_							:      List[Any]=3			,						UpperCamelCase_							:      int=2			,						UpperCamelCase_							:      List[str]=2			,						UpperCamelCase_							:      Dict=2			,						UpperCamelCase_							:      Tuple=True			,						UpperCamelCase_							:      Union[str, Any]=True			,						UpperCamelCase_							:      Any=32			,						UpperCamelCase_							:      Dict=5			,						UpperCamelCase_							:      Any=4			,						UpperCamelCase_							:      Optional[int]=37			,						UpperCamelCase_							:      Any="gelu"			,						UpperCamelCase_							:      List[Any]=0.1			,						UpperCamelCase_							:      Optional[Any]=0.1			,						UpperCamelCase_							:      Tuple=10			,						UpperCamelCase_							:      int=0.02			,						UpperCamelCase_							:      Any=0.9			,						UpperCamelCase_							:      Tuple=None			,						):


         """simple docstring"""



         __A       =						parent
         __A       =						batch_size
         __A       =						image_size
         __A       =						num_channels
         __A       =						patch_size
         __A       =						tubelet_size
         __A       =						num_frames
         __A       =						is_training
         __A       =						use_labels
         __A       =						hidden_size
         __A       =						num_hidden_layers
         __A       =						num_attention_heads
         __A       =						intermediate_size
         __A       =						hidden_act
         __A       =						hidden_dropout_prob
         __A       =						attention_probs_dropout_prob
         __A       =						type_sequence_label_size
         __A       =						initializer_range
         __A       =						mask_ratio
         __A       =						scope

         # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
         __A       =						(image_size // patch_size) ** 2
         __A       =						(num_frames // tubelet_size) * self.num_patches_per_frame

         # use this variable to define bool_masked_pos
         __A       =						int(mask_ratio * self.seq_length					)




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



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

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

         __A       =						self.get_config()

         return config, pixel_values, labels




     def 		lowerCAmelCase_  (					self							:      List[Any]					):


         """simple docstring"""



         return VideoMAEConfig(
             image_size=self.image_size			,						patch_size=self.patch_size			,						num_channels=self.num_channels			,						num_frames=self.num_frames			,						tubelet_size=self.tubelet_size			,						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=UpperCamelCase_			,						initializer_range=self.initializer_range			,						)




     def 		lowerCAmelCase_  (					self							:      Dict			,						UpperCamelCase_							:      List[str]			,						UpperCamelCase_							:      Any			,						UpperCamelCase_							:      List[str]					):


         """simple docstring"""



         __A       =						VideoMAEModel(config=UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         __A       =						model(UpperCamelCase_					)
         self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)					)




     def 		lowerCAmelCase_  (					self							:      Tuple			,						UpperCamelCase_							:      Optional[int]			,						UpperCamelCase_							:      str			,						UpperCamelCase_							:      Optional[Any]					):


         """simple docstring"""



         __A       =						VideoMAEForPreTraining(UpperCamelCase_					)
         model.to(UpperCamelCase_					)
         model.eval()
         # important: each video needs to have the same number of masked patches
         # hence we define a single mask, which we then repeat for each example in the batch
         __A       =						torch.ones((self.num_masks,)					)
         __A       =						torch.cat([mask, torch.zeros(self.seq_length - mask.size(0					)					)]					)
         __A       =						mask.expand(self.batch_size			,						-1					).bool()

         __A       =						model(UpperCamelCase_			,						UpperCamelCase_					)
         # model only returns predictions for masked patches
         __A       =						mask.sum().item()
         __A       =						3 * self.tubelet_size * self.patch_size**2
         self.parent.assertEqual(result.logits.shape			,						(self.batch_size, num_masked_patches, decoder_num_labels)					)





     def 		lowerCAmelCase_  (					self							:      Tuple					):


         """simple docstring"""



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


@require_torch
class    __lowercase   (						lowercase_      ,   lowercase_      ,   unittest.TestCase	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					(
         (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
     )
     SCREAMING_SNAKE_CASE     =					(
         {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
         if is_torch_available()
         else {}
     )

     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False




     def 		lowerCAmelCase_  (					self							:      int					):


         """simple docstring"""



         __A       =						VideoMAEModelTester(self					)
         __A       =						ConfigTester(self			,						config_class=UpperCamelCase_			,						has_text_modality=UpperCamelCase_			,						hidden_size=37					)




     def 		lowerCAmelCase_  (					self							:      Optional[Any]			,						UpperCamelCase_							:      str			,						UpperCamelCase_							:      Union[str, Any]			,						UpperCamelCase_							:      str=False					):


         """simple docstring"""



         __A       =						copy.deepcopy(UpperCamelCase_					)

         if model_class == VideoMAEForPreTraining:
             # important: each video needs to have the same number of masked patches
             # hence we define a single mask, which we then repeat for each example in the batch
             __A       =						torch.ones((self.model_tester.num_masks,)					)
             __A       =						torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0					)					)]					)
             __A       =						mask.expand(self.model_tester.batch_size			,						-1					).bool()
             __A       =						bool_masked_pos.to(UpperCamelCase_					)

         if return_labels:
             if model_class in [
                 *get_values(UpperCamelCase_					),
             ]:
                 __A       =						torch.zeros(
                     self.model_tester.batch_size			,						dtype=torch.long			,						device=UpperCamelCase_					)

         return inputs_dict




     def 		lowerCAmelCase_  (					self							:      List[Any]					):


         """simple docstring"""



         self.config_tester.run_common_tests()




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


         """simple docstring"""



         pass




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A       =						model_class(UpperCamelCase_					)
             self.assertIsInstance(model.get_input_embeddings()			,						(nn.Module)					)
             __A       =						model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_			,						nn.Linear					)					)




     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

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

             __A       =						["""pixel_values"""]
             self.assertListEqual(arg_names[:1]			,						UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*UpperCamelCase_					)




     @slow
     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __A       =						VideoMAEModel.from_pretrained(UpperCamelCase_					)
             self.assertIsNotNone(UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         if not self.has_attentions:
             pass

         else:
             __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()
             __A       =						True

             for model_class in self.all_model_classes:
                 __A       =						self.model_tester.seq_length - self.model_tester.num_masks
                 __A       =						(
                     num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                 )

                 __A       =						True
                 __A       =						False
                 __A       =						True
                 __A       =						model_class(UpperCamelCase_					)
                 model.to(UpperCamelCase_					)
                 model.eval()
                 with torch.no_grad():
                     __A       =						model(**self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_					)					)
                 __A       =						outputs.attentions
                 self.assertEqual(len(UpperCamelCase_					)			,						self.model_tester.num_hidden_layers					)

                 # check that output_attentions also work using config
                 del inputs_dict["output_attentions"]
                 __A       =						True
                 __A       =						model_class(UpperCamelCase_					)
                 model.to(UpperCamelCase_					)
                 model.eval()
                 with torch.no_grad():
                     __A       =						model(**self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_					)					)
                 __A       =						outputs.attentions
                 self.assertEqual(len(UpperCamelCase_					)			,						self.model_tester.num_hidden_layers					)

                 self.assertListEqual(
                     list(attentions[0].shape[-3:]					)			,						[self.model_tester.num_attention_heads, seq_len, seq_len]			,						)
                 __A       =						len(UpperCamelCase_					)

                 # Check attention is always last and order is fine
                 __A       =						True
                 __A       =						True
                 __A       =						model_class(UpperCamelCase_					)
                 model.to(UpperCamelCase_					)
                 model.eval()
                 with torch.no_grad():
                     __A       =						model(**self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_					)					)

                 self.assertEqual(out_len + 1			,						len(UpperCamelCase_					)					)

                 __A       =						outputs.attentions

                 self.assertEqual(len(UpperCamelCase_					)			,						self.model_tester.num_hidden_layers					)
                 self.assertListEqual(
                     list(self_attentions[0].shape[-3:]					)			,						[self.model_tester.num_attention_heads, seq_len, seq_len]			,						)




     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         def check_hidden_states_output(UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      Any					):
             __A       =						model_class(UpperCamelCase_					)
             model.to(UpperCamelCase_					)
             model.eval()

             with torch.no_grad():
                 __A       =						model(**self._prepare_for_class(UpperCamelCase_			,						UpperCamelCase_					)					)

             __A       =						outputs.hidden_states
             __A       =						self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(UpperCamelCase_					)			,						UpperCamelCase_					)

             __A       =						self.model_tester.seq_length - self.model_tester.num_masks
             __A       =						num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]					)			,						[seq_length, self.model_tester.hidden_size]			,						)

         __A     ,						__A       =						self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             __A       =						True
             check_hidden_states_output(UpperCamelCase_			,						UpperCamelCase_			,						UpperCamelCase_					)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             __A       =						True

             check_hidden_states_output(UpperCamelCase_			,						UpperCamelCase_			,						UpperCamelCase_					)





     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""					)
     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         pass



def       _SCREAMING_SNAKE_CASE    (			)    ->   Optional[int]:





    """simple docstring"""

    __A       =						hf_hub_download(
        repo_id="""hf-internal-testing/spaghetti-video"""      ,					filename="""eating_spaghetti.npy"""      ,					repo_type="""dataset"""     )
    __A       =						np.load(__lowercase     )
    return list(__lowercase     )






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







     '''simple docstring'''




     @cached_property
     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         return (
             VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]			,						image_std=[0.5, 0.5, 0.5]					)
             if is_vision_available()
             else None
         )




     @slow
     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         __A       =						VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics"""					).to(
             UpperCamelCase_					)

         __A       =						self.default_image_processor
         __A       =						prepare_video()
         __A       =						image_processor(UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)

         # verify the logits
         __A       =						torch.Size((1, 400)					)
         self.assertEqual(outputs.logits.shape			,						UpperCamelCase_					)

         __A       =						torch.tensor([0.3669, -0.0688, -0.2421]					).to(UpperCamelCase_					)

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





     @slow
     def 		lowerCAmelCase_  (					self							:      Any					):


         """simple docstring"""



         __A       =						VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""					).to(UpperCamelCase_					)

         __A       =						self.default_image_processor
         __A       =						prepare_video()
         __A       =						image_processor(UpperCamelCase_			,						return_tensors="""pt"""					).to(UpperCamelCase_					)

         # add boolean mask, indicating which patches to mask
         __A       =						hf_hub_download(repo_id="""hf-internal-testing/bool-masked-pos"""			,						filename="""bool_masked_pos.pt"""					)
         __A       =						torch.load(UpperCamelCase_					)

         # forward pass
         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)

         # verify the logits
         __A       =						torch.Size([1, 1_408, 1_536]					)
         __A       =						torch.tensor(
             [[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]			,						device=UpperCamelCase_					)
         self.assertEqual(outputs.logits.shape			,						UpperCamelCase_					)
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]			,						UpperCamelCase_			,						atol=1e-4					)					)

         # verify the loss (`config.norm_pix_loss` = `True`)
         __A       =						torch.tensor([0.5142]			,						device=UpperCamelCase_					)
         self.assertTrue(torch.allclose(outputs.loss			,						UpperCamelCase_			,						atol=1e-4					)					)

         # verify the loss (`config.norm_pix_loss` = `False`)
         __A       =						VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""			,						norm_pix_loss=UpperCamelCase_					).to(
             UpperCamelCase_					)

         with torch.no_grad():
             __A       =						model(**UpperCamelCase_					)

         __A       =						torch.tensor(torch.tensor([0.6469]					)			,						device=UpperCamelCase_					)
         self.assertTrue(torch.allclose(outputs.loss			,						UpperCamelCase_			,						atol=1e-4					)					)












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   (						lowercase_	):







     '''simple docstring'''




     def 		lowerCAmelCase_  (					self							:      int					):


         """simple docstring"""



         __A       =						tempfile.mkdtemp()
         __A       =						5

         # Realm tok
         __A       =						[
             """[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""",
         ]
         __A       =						os.path.join(self.tmpdirname			,						"""realm_tokenizer"""					)
         os.makedirs(UpperCamelCase_			,						exist_ok=UpperCamelCase_					)
         __A       =						os.path.join(UpperCamelCase_			,						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]					)					)

         __A       =						os.path.join(self.tmpdirname			,						"""realm_block_records"""					)
         os.makedirs(UpperCamelCase_			,						exist_ok=UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      List[Any]					):


         """simple docstring"""



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




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         shutil.rmtree(self.tmpdirname					)




     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						RealmConfig(num_block_records=self.num_block_records					)
         return config




     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



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




     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						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=UpperCamelCase_			,						)
         return block_records




     def 		lowerCAmelCase_  (					self							:      List[Any]					):


         """simple docstring"""



         __A       =						RealmRetriever(
             block_records=self.get_dummy_block_records()			,						tokenizer=self.get_tokenizer()			,						)
         return retriever




     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						self.get_config()
         __A       =						self.get_dummy_retriever()
         __A       =						retriever.tokenizer

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

         __A     ,						__A     ,						__A     ,						__A       =						retriever(
             UpperCamelCase_			,						UpperCamelCase_			,						answer_ids=UpperCamelCase_			,						max_length=UpperCamelCase_			,						return_tensors="""np"""					)

         self.assertEqual(len(UpperCamelCase_					)			,						2					)
         self.assertEqual(len(UpperCamelCase_					)			,						2					)
         self.assertEqual(len(UpperCamelCase_					)			,						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 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						self.get_config()
         __A       =						self.get_dummy_retriever()
         __A       =						retriever.tokenizer

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

         __A     ,						__A     ,						__A     ,						__A       =						retriever(
             UpperCamelCase_			,						UpperCamelCase_			,						answer_ids=UpperCamelCase_			,						max_length=UpperCamelCase_			,						return_tensors="""np"""					)

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





     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						self.get_dummy_retriever()
         retriever.save_pretrained(os.path.join(self.tmpdirname			,						"""realm_block_records"""					)					)

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

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




