





'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import numpy
					import tensorflow as tf

					from transformers import (
					    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
					    BertConfig,
					    DPRConfig,
					    TFDPRContextEncoder,
					    TFDPRQuestionEncoder,
					    TFDPRReader,
					)






class       UpperCamelCase__  :







	def __init__(				self			:   Dict , lowerCamelCase			:   int , lowerCamelCase			:   List[str]=1_3 , lowerCamelCase			:   List[str]=7 , lowerCamelCase			:   str=True , lowerCamelCase			:   int=True , lowerCamelCase			:   Any=True , lowerCamelCase			:   Optional[int]=True , lowerCamelCase			:   List[Any]=9_9 , lowerCamelCase			:   Dict=3_2 , lowerCamelCase			:   List[Any]=2 , lowerCamelCase			:   Tuple=4 , lowerCamelCase			:   Dict=3_7 , lowerCamelCase			:   Tuple="gelu" , lowerCamelCase			:   str=0.1 , lowerCamelCase			:   List[str]=0.1 , lowerCamelCase			:   Optional[Any]=5_1_2 , lowerCamelCase			:   List[Any]=1_6 , lowerCamelCase			:   Any=2 , lowerCamelCase			:   Union[str, Any]=0.02 , lowerCamelCase			:   Tuple=3 , lowerCamelCase			:   int=4 , lowerCamelCase			:   Dict=None , lowerCamelCase			:   List[Any]=0 , ):




				'''simple docstring'''
				a__     = parent
				a__     = batch_size
				a__     = seq_length
				a__     = is_training
				a__     = use_input_mask
				a__     = use_token_type_ids
				a__     = use_labels
				a__     = vocab_size
				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__     = max_position_embeddings
				a__     = type_vocab_size
				a__     = type_sequence_label_size
				a__     = initializer_range
				a__     = num_labels
				a__     = num_choices
				a__     = scope
				a__     = projection_dim







	def 						__a   (				self			:   List[str]		):




				'''simple docstring'''
				a__     = ids_tensor([self.batch_size, self.seq_length] , self.vocab_size		)

				a__     = None
				if self.use_input_mask:
							# follow test_modeling_tf_ctrl.py
							a__     = random_attention_mask([self.batch_size, self.seq_length]		)

				a__     = None
				if self.use_token_type_ids:
							a__     = ids_tensor([self.batch_size, self.seq_length] , self.type_vocab_size		)

				a__     = None
				a__     = None
				a__     = None
				if self.use_labels:
							a__     = ids_tensor([self.batch_size] , self.type_sequence_label_size		)
							a__     = ids_tensor([self.batch_size, self.seq_length] , self.num_labels		)
							a__     = ids_tensor([self.batch_size] , self.num_choices		)

				a__     = BertConfig(
				    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 , is_decoder=lowerCamelCase , initializer_range=self.initializer_range , )
				a__     = DPRConfig(projection_dim=self.projection_dim , **config.to_dict()		)

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   List[str] , lowerCamelCase			:   Union[str, Any] , lowerCamelCase			:   List[Any] , lowerCamelCase			:   Union[str, Any] , lowerCamelCase			:   Dict , lowerCamelCase			:   Dict , lowerCamelCase			:   Tuple		):




				'''simple docstring'''
				a__     = TFDPRContextEncoder(config=lowerCamelCase		)
				a__     = model(lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase		)
				self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size)		)







	def 						__a   (				self			:   Union[str, Any] , lowerCamelCase			:   str , lowerCamelCase			:   List[str] , lowerCamelCase			:   str , lowerCamelCase			:   int , lowerCamelCase			:   List[Any] , lowerCamelCase			:   List[Any] , lowerCamelCase			:   str		):




				'''simple docstring'''
				a__     = TFDPRQuestionEncoder(config=lowerCamelCase		)
				a__     = model(lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase		)
				self.parent.assertEqual(result.pooler_output.shape , (self.batch_size, self.projection_dim or self.hidden_size)		)







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   Dict , lowerCamelCase			:   List[Any] , lowerCamelCase			:   List[Any] , lowerCamelCase			:   Tuple , lowerCamelCase			:   List[str] , lowerCamelCase			:   Union[str, Any] , lowerCamelCase			:   Any		):




				'''simple docstring'''
				a__     = TFDPRReader(config=lowerCamelCase		)
				a__     = model(lowerCamelCase , attention_mask=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)		)
				self.parent.assertEqual(result.relevance_logits.shape , (self.batch_size,)		)







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = self.prepare_config_and_inputs()
				(
				    (
				    a__				
				)   ,				(
				    a__				
				)   ,				(
				    a__				
				)   ,				(
				    a__				
				)   ,				(
				    a__				
				)   ,				(
				    a__				
				)   ,				(
				    a__				
				)   ,								
				)     = config_and_inputs
				a__     = {"input_ids": input_ids}
				return config, inputs_dict






@require_tf
class       UpperCamelCase__  (		__lowerCAmelCase	,__lowerCAmelCase	,unittest.TestCase     ):
	lowerCAmelCase__					:			Any								=						(
	    (
	        TFDPRContextEncoder,
	        TFDPRQuestionEncoder,
	        TFDPRReader,
	    )
	    if is_tf_available()
	    else ()
	)
	lowerCAmelCase__					:			List[str]								=						{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

	lowerCAmelCase__					:			Optional[Any]								=						False
	lowerCAmelCase__					:			str								=						False
	lowerCAmelCase__					:			Optional[int]								=						False
	lowerCAmelCase__					:			Union[str, Any]								=						False
	lowerCAmelCase__					:			Tuple								=						False







	def 						__a   (				self			:   Any		):




				'''simple docstring'''
				a__     = TFDPRModelTester(self		)
				a__     = ConfigTester(self , config_class=lowerCamelCase , hidden_size=3_7		)







	def 						__a   (				self			:   Optional[int]		):




				'''simple docstring'''
				self.config_tester.run_common_tests()







	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_context_encoder(*lowerCamelCase		)







	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_question_encoder(*lowerCamelCase		)







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_reader(*lowerCamelCase		)







	@slow
	def 						__a   (				self			:   Any		):




				'''simple docstring'''
				for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							a__     = TFDPRContextEncoder.from_pretrained(lowerCamelCase		)
							self.assertIsNotNone(lowerCamelCase		)

				for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							a__     = TFDPRContextEncoder.from_pretrained(lowerCamelCase		)
							self.assertIsNotNone(lowerCamelCase		)

				for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							a__     = TFDPRQuestionEncoder.from_pretrained(lowerCamelCase		)
							self.assertIsNotNone(lowerCamelCase		)

				for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							a__     = TFDPRReader.from_pretrained(lowerCamelCase		)
							self.assertIsNotNone(lowerCamelCase		)






@require_tf
class       UpperCamelCase__  (		unittest.TestCase     ):







	@slow
	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"		)

				a__     = tf.constant(
				    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]		)  # [CLS] hello, is my dog cute? [SEP]
				a__     = model(lowerCamelCase		)[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				a__     = tf.constant(
				    [
				        [
				            0.03236253,
				            0.12753335,
				            0.16818509,
				            0.00279786,
				            0.3896933,
				            0.24264945,
				            0.2178971,
				            -0.02335227,
				            -0.08481959,
				            -0.14324117,
				        ]
				    ]		)
				self.assertTrue(numpy.allclose(output[:, :1_0].numpy() , expected_slice.numpy() , atol=1e-4		)		)










'''simple docstring'''


from manim import *






class       UpperCamelCase__  (		__lowerCAmelCase     ):







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = Rectangle(height=0.5 , width=0.5		)
				a__     = Rectangle(height=0.25 , width=0.25		)
				a__     = Rectangle(height=0.46 , width=0.46		).set_stroke(width=0		)

				a__     = [mem.copy() for i in range(6		)]
				a__     = [mem.copy() for i in range(6		)]
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = VGroup(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = Text("CPU" , font_size=2_4		)
				a__     = Group(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0.5 , aligned_edge=lowerCamelCase		)
				cpu.move_to([-2.5, -0.5, 0]		)
				self.add(lowerCamelCase		)

				a__     = [mem.copy() for i in range(4		)]
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = Text("GPU" , font_size=2_4		)
				a__     = Group(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0.5 , aligned_edge=lowerCamelCase		)
				gpu.move_to([-1, -1, 0]		)
				self.add(lowerCamelCase		)

				a__     = [mem.copy() for i in range(6		)]
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)

				a__     = Text("Model" , font_size=2_4		)
				a__     = Group(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0.5 , aligned_edge=lowerCamelCase		)
				model.move_to([3, -1.0, 0]		)
				self.add(lowerCamelCase		)

				a__     = []
				a__     = []
				a__     = []

				for i, rect in enumerate(lowerCamelCase		):
							rect.set_stroke(lowerCamelCase		)

							a__     = Rectangle(height=0.46 / 4 , width=0.46 / 3		).set_stroke(width=0.0		).set_fill(lowerCamelCase , opacity=0.7		)

							if i == 0:
										cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		) , buff=0.02 , direction=lowerCamelCase		)
										cpu_target.set_x(cpu_target.get_x() + 0.1		)
							elif i == 3:
										cpu_target.next_to(model_cpu_arr[0] , direction=lowerCamelCase , buff=0.0		)
							else:
										cpu_target.next_to(model_cpu_arr[i - 1] , direction=lowerCamelCase , buff=0.0		)
							self.add(lowerCamelCase		)
							model_cpu_arr.append(lowerCamelCase		)

				self.add(*lowerCamelCase , *lowerCamelCase , *lowerCamelCase		)

				a__     = [mem.copy() for i in range(6		)]
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)

				a__     = Text("Loaded Checkpoint" , font_size=2_4		)
				a__     = Group(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0.5 , aligned_edge=lowerCamelCase		)
				checkpoint.move_to([3, 0.5, 0]		)

				self.add(lowerCamelCase		)

				a__     = []
				a__     = []

				for i, rect in enumerate(lowerCamelCase		):
							a__     = fill.copy().set_fill(lowerCamelCase , opacity=0.7		)
							target.move_to(lowerCamelCase		)
							ckpt_arr.append(lowerCamelCase		)

							a__     = target.copy()
							if i < 5:
										cpu_target.move_to(cpu_left_col_base[i + 1]		)
							else:
										cpu_target.move_to(cpu_right_col_base[i - 5]		)
							ckpt_cpu_arr.append(lowerCamelCase		)
				self.add(*lowerCamelCase , *lowerCamelCase		)

				a__     = Square(side_length=2.2		)
				key.move_to([-5, 2, 0]		)

				a__     = MarkupText(
				    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model''' , font_size=1_8 , )

				key_text.move_to([-5, 2.4, 0]		)

				self.add(lowerCamelCase , lowerCamelCase		)

				a__     = MarkupText(
				    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint''' , font_size=1_8 , )

				blue_text.next_to(lowerCamelCase , DOWN * 2.4 , aligned_edge=key_text.get_left()		)
				self.add(lowerCamelCase		)

				a__     = MarkupText(
				    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.''' , font_size=2_4 , )
				step_a.move_to([2, 2, 0]		)

				a__     = [meta_mem.copy() for i in range(6		)]
				a__     = [meta_mem.copy() for i in range(6		)]
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = VGroup(*lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = VGroup(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0		)
				a__     = Text("Disk" , font_size=2_4		)
				a__     = Group(lowerCamelCase , lowerCamelCase		).arrange(lowerCamelCase , buff=0.5 , aligned_edge=lowerCamelCase		)
				disk.move_to([-4.0, -1.25, 0]		)
				self.play(Write(lowerCamelCase , run_time=3		) , Write(lowerCamelCase , run_time=1		) , Create(lowerCamelCase , run_time=1		)		)

				a__     = []
				for i, rect in enumerate(lowerCamelCase		):
							a__     = rect.copy()
							target.generate_target()
							target.target.move_to(disk_left_col_base[i]		).scale(0.5		)
							animations.append(MoveToTarget(lowerCamelCase , run_time=1.5		)		)
				self.play(*lowerCamelCase		)

				self.play(FadeOut(lowerCamelCase		)		)

				a__     = MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.''' , font_size=2_4		)
				step_a.move_to([2, 2, 0]		)

				self.play(Write(lowerCamelCase , run_time=3		)		)

				self.play(
				    FadeOut(lowerCamelCase , lowerCamelCase , *lowerCamelCase , *lowerCamelCase		) , )

				self.wait()



