


import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, 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, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
				    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
				    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
				    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
				    LayoutLMvaConfig,
				    LayoutLMvaForQuestionAnswering,
				    LayoutLMvaForSequenceClassification,
				    LayoutLMvaForTokenClassification,
				    LayoutLMvaModel,
				)
				from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
				from PIL import Image

				from transformers import LayoutLMvaImageProcessor







class 	__lowercase    :





							"""simple docstring"""







							def __init__(      self			,						lowerCAmelCase__			,						lowerCAmelCase__=2			,						lowerCAmelCase__=3			,						lowerCAmelCase__=4			,						lowerCAmelCase__=2			,						lowerCAmelCase__=7			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=9_9			,						lowerCAmelCase__=3_6			,						lowerCAmelCase__=3			,						lowerCAmelCase__=4			,						lowerCAmelCase__=3_7			,						lowerCAmelCase__="gelu"			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=5_1_2			,						lowerCAmelCase__=1_6			,						lowerCAmelCase__=2			,						lowerCAmelCase__=0.02			,						lowerCAmelCase__=6			,						lowerCAmelCase__=6			,						lowerCAmelCase__=3			,						lowerCAmelCase__=4			,						lowerCAmelCase__=None			,						lowerCAmelCase__=1_0_0_0			,						):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					parent
										SCREAMING_SNAKE_CASE_		:     Tuple					=					batch_size
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					num_channels
										SCREAMING_SNAKE_CASE_		:     Any					=					image_size
										SCREAMING_SNAKE_CASE_		:     int					=					patch_size
										SCREAMING_SNAKE_CASE_		:     int					=					text_seq_length
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					is_training
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					use_input_mask
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					use_token_type_ids
										SCREAMING_SNAKE_CASE_		:     int					=					use_labels
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					vocab_size
										SCREAMING_SNAKE_CASE_		:     List[str]					=					hidden_size
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					num_hidden_layers
										SCREAMING_SNAKE_CASE_		:     List[str]					=					num_attention_heads
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					intermediate_size
										SCREAMING_SNAKE_CASE_		:     int					=					hidden_act
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					hidden_dropout_prob
										SCREAMING_SNAKE_CASE_		:     int					=					attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE_		:     Dict					=					max_position_embeddings
										SCREAMING_SNAKE_CASE_		:     Tuple					=					type_vocab_size
										SCREAMING_SNAKE_CASE_		:     str					=					type_sequence_label_size
										SCREAMING_SNAKE_CASE_		:     int					=					initializer_range
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					coordinate_size
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					shape_size
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					num_labels
										SCREAMING_SNAKE_CASE_		:     int					=					num_choices
										SCREAMING_SNAKE_CASE_		:     Dict					=					scope
										SCREAMING_SNAKE_CASE_		:     str					=					range_bbox

										# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					text_seq_length
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					(image_size // patch_size) ** 2 + 1
										SCREAMING_SNAKE_CASE_		:     int					=					self.text_seq_length + self.image_seq_length






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					ids_tensor([self.batch_size, self.text_seq_length]			,						self.vocab_size       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					ids_tensor([self.batch_size, self.text_seq_length, 4]			,						self.range_bbox       )
										# Ensure that bbox is legal
										for i in range(bbox.shape[0]       ):
													for j in range(bbox.shape[1]       ):
																if bbox[i, j, 3] < bbox[i, j, 1]:
																			SCREAMING_SNAKE_CASE_		:     List[Any]					=					bbox[i, j, 3]
																			SCREAMING_SNAKE_CASE_		:     Dict					=					bbox[i, j, 1]
																			SCREAMING_SNAKE_CASE_		:     str					=					t
																if bbox[i, j, 2] < bbox[i, j, 0]:
																			SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					bbox[i, j, 2]
																			SCREAMING_SNAKE_CASE_		:     int					=					bbox[i, j, 0]
																			SCREAMING_SNAKE_CASE_		:     Optional[int]					=					t

										SCREAMING_SNAKE_CASE_		:     Tuple					=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					None
										if self.use_input_mask:
													SCREAMING_SNAKE_CASE_		:     Dict					=					random_attention_mask([self.batch_size, self.text_seq_length]       )

										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					None
										if self.use_token_type_ids:
													SCREAMING_SNAKE_CASE_		:     Dict					=					ids_tensor([self.batch_size, self.text_seq_length]			,						self.type_vocab_size       )

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					None
										SCREAMING_SNAKE_CASE_		:     str					=					None
										if self.use_labels:
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					ids_tensor([self.batch_size]			,						self.type_sequence_label_size       )
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					ids_tensor([self.batch_size, self.text_seq_length]			,						self.num_labels       )

										SCREAMING_SNAKE_CASE_		:     int					=					LayoutLMvaConfig(
										    vocab_size=self.vocab_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			,						max_position_embeddings=self.max_position_embeddings			,						type_vocab_size=self.type_vocab_size			,						initializer_range=self.initializer_range			,						coordinate_size=self.coordinate_size			,						shape_size=self.shape_size			,						input_size=self.image_size			,						patch_size=self.patch_size			,						)

										return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[str]					=					LayoutLMvaModel(config=lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()

										# text + image
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					model(lowerCAmelCase__			,						pixel_values=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(
										    lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						token_type_ids=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     str					=					model(lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						token_type_ids=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Dict					=					model(lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__       )

										self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)       )

										# text only
										SCREAMING_SNAKE_CASE_		:     List[str]					=					model(lowerCAmelCase__       )
										self.parent.assertEqual(
										    result.last_hidden_state.shape			,						(self.batch_size, self.text_seq_length, self.hidden_size)       )

										# image only
										SCREAMING_SNAKE_CASE_		:     str					=					model(pixel_values=lowerCAmelCase__       )
										self.parent.assertEqual(
										    result.last_hidden_state.shape			,						(self.batch_size, self.image_seq_length, self.hidden_size)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.num_labels
										SCREAMING_SNAKE_CASE_		:     Any					=					LayoutLMvaForSequenceClassification(lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Any					=					model(
										    lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						token_type_ids=lowerCAmelCase__			,						labels=lowerCAmelCase__			,						)
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.num_labels
										SCREAMING_SNAKE_CASE_		:     Any					=					LayoutLMvaForTokenClassification(config=lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     int					=					model(
										    lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						token_type_ids=lowerCAmelCase__			,						labels=lowerCAmelCase__			,						)
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.text_seq_length, self.num_labels)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					LayoutLMvaForQuestionAnswering(config=lowerCAmelCase__       )
										model.to(lowerCAmelCase__       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     List[str]					=					model(
										    lowerCAmelCase__			,						bbox=lowerCAmelCase__			,						pixel_values=lowerCAmelCase__			,						attention_mask=lowerCAmelCase__			,						token_type_ids=lowerCAmelCase__			,						start_positions=lowerCAmelCase__			,						end_positions=lowerCAmelCase__			,						)
										self.parent.assertEqual(result.start_logits.shape			,						(self.batch_size, self.seq_length)       )
										self.parent.assertEqual(result.end_logits.shape			,						(self.batch_size, self.seq_length)       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.prepare_config_and_inputs()
										(
										    (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      (
										    SCREAMING_SNAKE_CASE_
										)		,      
										)		:     Dict					=					config_and_inputs
										SCREAMING_SNAKE_CASE_		:     Any					=					{
										    'input_ids': input_ids,
										    'bbox': bbox,
										    'pixel_values': pixel_values,
										    'token_type_ids': token_type_ids,
										    'attention_mask': input_mask,
										}
										return config, inputs_dict







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





							"""simple docstring"""







							_UpperCAmelCase      =						False
							_UpperCAmelCase      =						False
							_UpperCAmelCase      =						False

							_UpperCAmelCase      =						(
							    (
							        LayoutLMvaModel,
							        LayoutLMvaForSequenceClassification,
							        LayoutLMvaForTokenClassification,
							        LayoutLMvaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_UpperCAmelCase      =						(
							    {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel}
							    if is_torch_available()
							    else {}
							)






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										return True






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					LayoutLMvaModelTester(self       )
										SCREAMING_SNAKE_CASE_		:     Any					=					ConfigTester(self			,						config_class=lowerCAmelCase__			,						hidden_size=3_7       )






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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					copy.deepcopy(lowerCAmelCase__       )
										if model_class in get_values(lowerCAmelCase__       ):
													SCREAMING_SNAKE_CASE_		:     Dict					=					{
													    k: v.unsqueeze(1       ).expand(-1			,						self.model_tester.num_choices			,						-1       ).contiguous()
													    if isinstance(lowerCAmelCase__			,						torch.Tensor       ) and v.ndim > 1
													    else v
													    for k, v in inputs_dict.items()
													}
										if return_labels:
													if model_class in get_values(lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     List[Any]					=					torch.ones(self.model_tester.batch_size			,						dtype=torch.long			,						device=lowerCAmelCase__       )
													elif model_class in get_values(lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     int					=					torch.zeros(
																    self.model_tester.batch_size			,						dtype=torch.long			,						device=lowerCAmelCase__       )
																SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.zeros(
																    self.model_tester.batch_size			,						dtype=torch.long			,						device=lowerCAmelCase__       )
													elif model_class in [
													    *get_values(lowerCAmelCase__       ),
													]:
																SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.zeros(
																    self.model_tester.batch_size			,						dtype=torch.long			,						device=lowerCAmelCase__       )
													elif model_class in [
													    *get_values(lowerCAmelCase__       ),
													]:
																SCREAMING_SNAKE_CASE_		:     Any					=					torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.text_seq_length)			,						dtype=torch.long			,						device=lowerCAmelCase__			,						)

										return inputs_dict






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										self.config_tester.run_common_tests()






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
													SCREAMING_SNAKE_CASE_		:     int					=					type
													self.model_tester.create_and_check_model(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__       )






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					LayoutLMvaModel.from_pretrained(lowerCAmelCase__       )
													self.assertIsNotNone(lowerCAmelCase__       )







def 						a__			(				):
			SCREAMING_SNAKE_CASE_		:     Optional[int]					=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
			return image







@require_torch
class 	__lowercase    (unittest.TestCase  ):





							"""simple docstring"""







							@cached_property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase__       ) if is_vision_available() else None






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'       ).to(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.default_image_processor
										SCREAMING_SNAKE_CASE_		:     Tuple					=					prepare_img()
										SCREAMING_SNAKE_CASE_		:     Dict					=					image_processor(images=lowerCAmelCase__			,						return_tensors='pt'       ).pixel_values.to(lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     List[Any]					=					torch.tensor([[1, 2]]       )
										SCREAMING_SNAKE_CASE_		:     List[str]					=					torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]       ).unsqueeze(0       )

										# forward pass
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					model(
										    input_ids=input_ids.to(lowerCAmelCase__       )			,						bbox=bbox.to(lowerCAmelCase__       )			,						pixel_values=pixel_values.to(lowerCAmelCase__       )			,						)

										# verify the logits
										SCREAMING_SNAKE_CASE_		:     str					=					torch.Size((1, 1_9_9, 7_6_8)       )
										self.assertEqual(outputs.last_hidden_state.shape			,						lowerCAmelCase__       )

										SCREAMING_SNAKE_CASE_		:     str					=					torch.tensor(
										    [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]       ).to(lowerCAmelCase__       )

										self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]			,						lowerCAmelCase__			,						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    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					tempfile.mkdtemp()
										SCREAMING_SNAKE_CASE_		:     Dict					=					5

										# Realm tok
										SCREAMING_SNAKE_CASE_		:     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',
										]
										SCREAMING_SNAKE_CASE_		:     Tuple					=					os.path.join(self.tmpdirname			,						'realm_tokenizer'       )
										os.makedirs(lowerCAmelCase__			,						exist_ok=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					os.path.join(lowerCAmelCase__			,						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]       )       )

										SCREAMING_SNAKE_CASE_		:     str					=					os.path.join(self.tmpdirname			,						'realm_block_records'       )
										os.makedirs(lowerCAmelCase__			,						exist_ok=lowerCAmelCase__       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										shutil.rmtree(self.tmpdirname       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					RealmConfig(num_block_records=self.num_block_records       )
										return config






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					Dataset.from_dict(
										    {
										        'id': ['0', '1'],
										        'question': ['foo', 'bar'],
										        'answers': [['Foo', 'Bar'], ['Bar']],
										    }       )
										return dataset






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					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=lowerCAmelCase__			,						)
										return block_records






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					RealmRetriever(
										    block_records=self.get_dummy_block_records()			,						tokenizer=self.get_tokenizer()			,						)
										return retriever






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.get_config()
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.get_dummy_retriever()
										SCREAMING_SNAKE_CASE_		:     List[str]					=					retriever.tokenizer

										SCREAMING_SNAKE_CASE_		:     Any					=					np.array([0, 3]			,						dtype='long'       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					tokenizer(['Test question']       ).input_ids
										SCREAMING_SNAKE_CASE_		:     int					=					tokenizer(
										    ['the fourth']			,						add_special_tokens=lowerCAmelCase__			,						return_token_type_ids=lowerCAmelCase__			,						return_attention_mask=lowerCAmelCase__			,						).input_ids
										SCREAMING_SNAKE_CASE_		:     Any					=					config.reader_seq_len

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     str					=					retriever(
										    lowerCAmelCase__			,						lowerCAmelCase__			,						answer_ids=lowerCAmelCase__			,						max_length=lowerCAmelCase__			,						return_tensors='np'       )

										self.assertEqual(len(lowerCAmelCase__       )			,						2       )
										self.assertEqual(len(lowerCAmelCase__       )			,						2       )
										self.assertEqual(len(lowerCAmelCase__       )			,						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 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.get_config()
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					self.get_dummy_retriever()
										SCREAMING_SNAKE_CASE_		:     Tuple					=					retriever.tokenizer

										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					np.array([0, 3, 5]			,						dtype='long'       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					tokenizer(['Test question']       ).input_ids
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					tokenizer(
										    ['the fourth', 'longer longer']			,						add_special_tokens=lowerCAmelCase__			,						return_token_type_ids=lowerCAmelCase__			,						return_attention_mask=lowerCAmelCase__			,						).input_ids
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					config.reader_seq_len

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Tuple					=					retriever(
										    lowerCAmelCase__			,						lowerCAmelCase__			,						answer_ids=lowerCAmelCase__			,						max_length=lowerCAmelCase__			,						return_tensors='np'       )

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






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.get_dummy_retriever()
										retriever.save_pretrained(os.path.join(self.tmpdirname			,						'realm_block_records'       )       )

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

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





