


'''simple docstring'''






import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 BertGenerationDecoder, BertGenerationEncoder




class 	a__       :





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_=13      ,					lowerCamelCase_=7      ,					lowerCamelCase_=True      ,					lowerCamelCase_=True      ,					lowerCamelCase_=99      ,					lowerCamelCase_=32      ,					lowerCamelCase_=5      ,					lowerCamelCase_=4      ,					lowerCamelCase_=37      ,					lowerCamelCase_="gelu"      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=50      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=True      ,					lowerCamelCase_=None      ,					) ->							str:
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			batch_size
														lowerCAmelCase__      =			seq_length
														lowerCAmelCase__      =			is_training
														lowerCAmelCase__      =			use_input_mask
														lowerCAmelCase__      =			vocab_size
														lowerCAmelCase__      =			hidden_size
														lowerCAmelCase__      =			num_hidden_layers
														lowerCAmelCase__      =			num_attention_heads
														lowerCAmelCase__      =			intermediate_size
														lowerCAmelCase__      =			hidden_act
														lowerCAmelCase__      =			hidden_dropout_prob
														lowerCAmelCase__      =			attention_probs_dropout_prob
														lowerCAmelCase__      =			max_position_embeddings
														lowerCAmelCase__      =			initializer_range
														lowerCAmelCase__      =			use_labels
														lowerCAmelCase__      =			scope







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size     )

														lowerCAmelCase__      =			None
														if self.use_input_mask:
																					lowerCAmelCase__      =			random_attention_mask([self.batch_size, self.seq_length]     )

														if self.use_labels:
																					lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size     )

														lowerCAmelCase__      =			self.get_config()

														return config, input_ids, input_mask, token_labels







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Any:
														return BertGenerationConfig(
														    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      ,					is_decoder=lowerCamelCase_      ,					initializer_range=self.initializer_range      ,					)







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														(
														    (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      
														)      =			self.prepare_config_and_inputs()

														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
														lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					vocab_size=2     )

														return (
														    config,
														    input_ids,
														    input_mask,
														    token_labels,
														    encoder_hidden_states,
														    encoder_attention_mask,
														)







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					**lowerCamelCase_      ,					) ->							Any:
														lowerCAmelCase__      =			BertGenerationEncoder(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )
														self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					**lowerCamelCase_      ,					) ->							str:
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			BertGenerationEncoder(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_      ,					encoder_attention_mask=lowerCamelCase_      ,					)
														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_      ,					)
														self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					**lowerCamelCase_      ,					) ->							List[str]:
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			BertGenerationDecoder(config=lowerCamelCase_     ).to(lowerCamelCase_     ).eval()

														# first forward pass
														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_      ,					encoder_attention_mask=lowerCamelCase_      ,					use_cache=lowerCamelCase_      ,					)
														lowerCAmelCase__      =			outputs.past_key_values

														# create hypothetical multiple next token and extent to next_input_ids
														lowerCAmelCase__      =			ids_tensor((self.batch_size, 3)      ,					config.vocab_size     )
														lowerCAmelCase__      =			ids_tensor((self.batch_size, 3)      ,					vocab_size=2     )

														# append to next input_ids and
														lowerCAmelCase__      =			torch.cat([input_ids, next_tokens]      ,					dim=-1     )
														lowerCAmelCase__      =			torch.cat([input_mask, next_mask]      ,					dim=-1     )

														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_      ,					encoder_attention_mask=lowerCamelCase_      ,					output_hidden_states=lowerCamelCase_      ,					)['''hidden_states'''][0]
														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_      ,					encoder_attention_mask=lowerCamelCase_      ,					past_key_values=lowerCamelCase_      ,					output_hidden_states=lowerCamelCase_      ,					)['''hidden_states'''][0]

														# select random slice
														lowerCAmelCase__      =			ids_tensor((1,)      ,					output_from_past.shape[-1]     ).item()
														lowerCAmelCase__      =			output_from_no_past[:, -3:, random_slice_idx].detach()
														lowerCAmelCase__      =			output_from_past[:, :, random_slice_idx].detach()

														self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

														# test that outputs are equal for slice
														self.parent.assertTrue(torch.allclose(lowerCamelCase_      ,					lowerCamelCase_      ,					atol=1e-3     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					*lowerCamelCase_      ,					) ->							List[str]:
														lowerCAmelCase__      =			BertGenerationDecoder(lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					labels=lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.vocab_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.prepare_config_and_inputs()
														lowerCAmelCase__      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														return config, inputs_dict




@require_torch
class 	a__       (							a__       ,       a__       ,       a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Any					=   (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
							lowercase__    :     int					=   (BertGenerationDecoder,) if is_torch_available() else ()
							lowercase__    :     List[Any]					=   (
							    {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
							    if is_torch_available()
							    else {}
							)







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			BertGenerationEncoderTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					hidden_size=37     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														self.config_tester.run_common_tests()







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														lowerCAmelCase__      =			'''bert'''
														self.model_tester.create_and_check_model(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														# This regression test was failing with PyTorch < 1.3
														(
														    (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      (
														    lowerCAmelCase__
														)     ,      
														)      =			self.model_tester.prepare_config_and_inputs_for_decoder()

														lowerCAmelCase__      =			None

														self.model_tester.create_and_check_model_as_decoder(
														    lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					)







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase_     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''     )
														self.assertIsNotNone(lowerCamelCase_     )




@require_torch
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''     )
														lowerCAmelCase__      =			torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]     )
														with torch.no_grad():
																					lowerCAmelCase__      =			model(lowerCamelCase_     )[0]
														lowerCAmelCase__      =			torch.Size([1, 8, 10_24]     )
														self.assertEqual(output.shape      ,					lowerCamelCase_     )
														lowerCAmelCase__      =			torch.tensor(
														    [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]     )
														self.assertTrue(torch.allclose(output[:, :3, :3]      ,					lowerCamelCase_      ,					atol=1e-4     )     )




@require_torch
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''     )
														lowerCAmelCase__      =			torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]     )
														with torch.no_grad():
																					lowerCAmelCase__      =			model(lowerCamelCase_     )[0]
														lowerCAmelCase__      =			torch.Size([1, 8, 5_03_58]     )
														self.assertEqual(output.shape      ,					lowerCamelCase_     )
														lowerCAmelCase__      =			torch.tensor(
														    [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]     )
														self.assertTrue(torch.allclose(output[:, :3, :3]      ,					lowerCamelCase_      ,					atol=1e-4     )     )



'''simple docstring'''






import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


__UpperCAmelCase					       =							logging.getLogger(__name__)




class 	a__       (							a__						):





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_=None     ) ->							Dict:
														super().__init__(
														    lowerCamelCase_      ,					question_encoder_tokenizer=lowerCamelCase_      ,					generator_tokenizer=lowerCamelCase_      ,					index=lowerCamelCase_      ,					init_retrieval=lowerCamelCase_      ,					)
														lowerCAmelCase__      =			None







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							str:
														logger.info('''initializing retrieval'''     )

														# initializing a separate process group for retrieval as the default
														# nccl backend doesn't support gather/scatter operations while gloo
														# is too slow to replace nccl for the core gpu communication
														if dist.is_initialized():
																					logger.info('''dist initialized'''     )
																					# needs to be set manually
																					lowerCAmelCase__      =			self._infer_socket_ifname()
																					# avoid clash with the NCCL port
																					lowerCAmelCase__      =			str(distributed_port + 1     )
																					lowerCAmelCase__      =			dist.new_group(ranks=lowerCamelCase_      ,					backend='''gloo'''     )

														# initialize retriever only on the main worker
														if not dist.is_initialized() or self._is_main():
																					logger.info('''dist not initialized / main'''     )
																					self.index.init_index()

														# all processes wait untill the retriever is initialized by the main process
														if dist.is_initialized():
																					torch.distributed.barrier(group=self.process_group     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														return dist.get_rank(group=self.process_group     ) == 0







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_=torch.floataa     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			torch.empty(lowerCamelCase_      ,					dtype=lowerCamelCase_     )
														dist.scatter(lowerCamelCase_      ,					src=0      ,					scatter_list=lowerCamelCase_      ,					group=self.process_group     )
														return target_tensor







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			psutil.net_if_addrs()
														# a hacky way to deal with varying network interface names
														lowerCAmelCase__      =			next((addr for addr in addrs if addr.startswith('''e'''     ))      ,					lowerCamelCase_     )
														return ifname







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Tuple[np.ndarray, List[dict]]:

														# single GPU training
														if not dist.is_initialized():
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			self._main_retrieve(lowerCamelCase_      ,					lowerCamelCase_     )
																					return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(lowerCamelCase_     )

														# distributed training
														lowerCAmelCase__      =			dist.get_world_size(group=self.process_group     )

														# gather logic
														lowerCAmelCase__      =			None
														if self._is_main():
																					lowerCAmelCase__      =			[torch.empty(question_hidden_states.shape      ,					dtype=torch.floataa     ) for _ in range(lowerCamelCase_     )]
														dist.gather(torch.tensor(lowerCamelCase_     )      ,					dst=0      ,					gather_list=lowerCamelCase_      ,					group=self.process_group     )

														# scatter logic
														lowerCAmelCase__      =			question_hidden_states.shape[0]
														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														if self._is_main():
																					assert len(lowerCamelCase_     ) == world_size
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			self._main_retrieve(torch.cat(lowerCamelCase_     ).numpy()      ,					lowerCamelCase_     )
																					lowerCAmelCase__     ,      lowerCAmelCase__      =			torch.tensor(lowerCamelCase_     ), torch.tensor(lowerCamelCase_     )
																					lowerCAmelCase__      =			self._chunk_tensor(lowerCamelCase_      ,					lowerCamelCase_     )
																					lowerCAmelCase__      =			self._chunk_tensor(lowerCamelCase_      ,					lowerCamelCase_     )
														lowerCAmelCase__      =			self._scattered(lowerCamelCase_      ,					[n_queries, n_docs]      ,					target_type=torch.intaa     )
														lowerCAmelCase__      =			self._scattered(lowerCamelCase_      ,					[n_queries, n_docs, question_hidden_states.shape[1]]     )

														return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(lowerCamelCase_     )