


'''simple docstring'''




from transformers import (
    DPRContextEncoderTokenizer,
    DPRContextEncoderTokenizerFast,
    DPRQuestionEncoderTokenizer,
    DPRQuestionEncoderTokenizerFast,
    DPRReaderOutput,
    DPRReaderTokenizer,
    DPRReaderTokenizerFast,
)
from transformers.testing_utils import require_tokenizers, slow
from transformers.tokenization_utils_base import BatchEncoding

from ..bert.test_tokenization_bert import BertTokenizationTest



@require_tokenizers
class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       List[str]         =			DPRContextEncoderTokenizer
	_lowerCamelCase :       Tuple         =			DPRContextEncoderTokenizerFast
	_lowerCamelCase :       int         =			True



@require_tokenizers
class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       int         =			DPRQuestionEncoderTokenizer
	_lowerCamelCase :       List[Any]         =			DPRQuestionEncoderTokenizerFast
	_lowerCamelCase :       Tuple         =			True



@require_tokenizers
class 			lowerCAmelCase					(					a				):
	_lowerCamelCase :       List[str]         =			DPRReaderTokenizer
	_lowerCamelCase :       Optional[int]         =			DPRReaderTokenizerFast
	_lowerCamelCase :       Optional[Any]         =			True



	@slow
	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[str]   				=			self.tokenizer_class.from_pretrained('bert-base-uncased'     )

		lowerCAmelCase       :     Tuple   				=			tokenizer.encode('question sequence'			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     List[str]   				=			tokenizer.encode('title sequence'			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     int   				=			tokenizer.encode('text sequence ' * 4			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     Dict   				=			[[101] + text_a + [102] + text_a + [102] + text_a]
		lowerCAmelCase       :     Optional[Any]   				=			BatchEncoding({'input_ids': input_ids}     )

		lowerCAmelCase       :     Optional[Any]   				=			[[0] * len(input_ids[0]     )]
		lowerCAmelCase       :     Optional[int]   				=			[[0] * len(input_ids[0]     )]
		lowerCAmelCase       :     List[str]   				=			[0]
		lowerCAmelCase       :     str   				=			DPRReaderOutput(snake_case__			,       snake_case__			,       snake_case__     )

		lowerCAmelCase				, lowerCAmelCase       :     List[Any]   				=			8, 9
		lowerCAmelCase       :     Optional[int]   				=			10
		lowerCAmelCase       :     Dict   				=			10
		lowerCAmelCase       :     str   				=			tokenizer.decode_best_spans(snake_case__			,       snake_case__     )
		self.assertEqual(predicted_spans[0].start_index			,       snake_case__     )
		self.assertEqual(predicted_spans[0].end_index			,       snake_case__     )
		self.assertEqual(predicted_spans[0].doc_id			,       0     )



	@slow
	def 					lowercase  (       self     ):
		lowerCAmelCase       :     str   				=			self.tokenizer_class.from_pretrained('bert-base-uncased'     )

		lowerCAmelCase       :     str   				=			tokenizer.encode('question sequence'			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     List[Any]   				=			tokenizer.encode('title sequence'			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     List[Any]   				=			tokenizer.encode('text sequence'			,       add_special_tokens=snake_case__     )
		lowerCAmelCase       :     Dict   				=			[101] + text_a + [102] + text_a + [102] + text_a
		lowerCAmelCase       :     List[Any]   				=			tokenizer(questions=['question sequence']			,       titles=['title sequence']			,       texts=['text sequence']     )
		self.assertIn('input_ids'			,       snake_case__     )
		self.assertIn('attention_mask'			,       snake_case__     )
		self.assertListEqual(encoded_input['input_ids'][0]			,       snake_case__     )




'''simple docstring'''




from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available

def       __UpperCamelCase     (     _A     :					List[str]					)     ->   Any:




	"""simple docstring"""





	if is_torch_available():
		import torch

		if isinstance(_A       , torch.Tensor					):
			return True

	return isinstance(_A       , np.ndarray					)






class 			lowerCAmelCase					(					a				):



	def 					lowercase  (       self     ):
		lowerCAmelCase       :     List[str]   				=			fields(self     )

		# Safety and consistency checks
		if not len(snake_case__     ):
			raise ValueError(f"{self.__class__.__name__} has no fields."     )

		lowerCAmelCase       :     List[str]   				=			getattr(self			,       class_fields[0].name     )
		lowerCAmelCase       :     List[Any]   				=			all(getattr(self			,       field.name     ) is None for field in class_fields[1:]     )

		if other_fields_are_none and isinstance(snake_case__			,       snake_case__     ):
			for key, value in first_field.items():
				lowerCAmelCase       :     Optional[int]   				=			value
		else:
			for field in class_fields:
				lowerCAmelCase       :     str   				=			getattr(self			,       field.name     )
				if v is not None:
					lowerCAmelCase       :     Optional[Any]   				=			v



	def __delitem__(       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``__delitem__`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``setdefault`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``pop`` on a {self.__class__.__name__} instance."     )



	def 					lowercase  (       self			,       *snake_case__			,       **snake_case__     ):
		raise Exception(f"You cannot use ``update`` on a {self.__class__.__name__} instance."     )



	def __getitem__(       self			,       snake_case__     ):
		if isinstance(snake_case__			,       snake_case__     ):
			lowerCAmelCase       :     Dict   				=			dict(self.items()     )
			return inner_dict[k]
		else:
			return self.to_tuple()[k]



	def __setattr__(       self			,       snake_case__			,       snake_case__     ):
		if name in self.keys() and value is not None:
			# Don't call self.__setitem__ to avoid recursion errors
			super().__setitem__(snake_case__			,       snake_case__     )
		super().__setattr__(snake_case__			,       snake_case__     )



	def __setitem__(       self			,       snake_case__			,       snake_case__     ):
		# Will raise a KeyException if needed
		super().__setitem__(snake_case__			,       snake_case__     )
		# Don't call self.__setattr__ to avoid recursion errors
		super().__setattr__(snake_case__			,       snake_case__     )



	def 					lowercase  (       self     ):
		return tuple(self[k] for k in self.keys()     )
