





import math
import unittest

from transformers import BioGptConfig, 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, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    BioGptForCausalLM,
					    BioGptForSequenceClassification,
					    BioGptForTokenClassification,
					    BioGptModel,
					    BioGptTokenizer,
					)
					from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST

class 							__SCREAMING_SNAKE_CASE       :






							def __init__(    self	,			__lowerCAmelCase	,			__lowerCAmelCase=13	,			__lowerCAmelCase=7	,			__lowerCAmelCase=True	,			__lowerCAmelCase=True	,			__lowerCAmelCase=False	,			__lowerCAmelCase=True	,			__lowerCAmelCase=99	,			__lowerCAmelCase=32	,			__lowerCAmelCase=5	,			__lowerCAmelCase=4	,			__lowerCAmelCase=37	,			__lowerCAmelCase="gelu"	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=512	,			__lowerCAmelCase=16	,			__lowerCAmelCase=2	,			__lowerCAmelCase=0.02	,			__lowerCAmelCase=3	,			__lowerCAmelCase=4	,			__lowerCAmelCase=None	,			):
														UpperCamelCase__					  =      parent
														UpperCamelCase__					  =      batch_size
														UpperCamelCase__					  =      seq_length
														UpperCamelCase__					  =      is_training
														UpperCamelCase__					  =      use_input_mask
														UpperCamelCase__					  =      use_token_type_ids
														UpperCamelCase__					  =      use_labels
														UpperCamelCase__					  =      vocab_size
														UpperCamelCase__					  =      hidden_size
														UpperCamelCase__					  =      num_hidden_layers
														UpperCamelCase__					  =      num_attention_heads
														UpperCamelCase__					  =      intermediate_size
														UpperCamelCase__					  =      hidden_act
														UpperCamelCase__					  =      hidden_dropout_prob
														UpperCamelCase__					  =      attention_probs_dropout_prob
														UpperCamelCase__					  =      max_position_embeddings
														UpperCamelCase__					  =      type_vocab_size
														UpperCamelCase__					  =      type_sequence_label_size
														UpperCamelCase__					  =      initializer_range
														UpperCamelCase__					  =      num_labels
														UpperCamelCase__					  =      num_choices
														UpperCamelCase__					  =      scope






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      ids_tensor([self.batch_size, self.seq_length]	,			self.vocab_size	)

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

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

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

														UpperCamelCase__					  =      self.get_config()

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






							def 					_lowerCamelCase		(    self	):
														return BioGptConfig(
														    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	,			)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      BioGptModel(config=__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	)
														UpperCamelCase__					  =      model(__lowerCAmelCase	)
														self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)	)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			):
														UpperCamelCase__					  =      BioGptForCausalLM(config=__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			token_type_ids=__lowerCAmelCase	,			labels=__lowerCAmelCase	)
														self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)	)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			*__lowerCAmelCase	):
														UpperCamelCase__					  =      BioGptModel(config=__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()

														# create attention mask
														UpperCamelCase__					  =      torch.ones(input_ids.shape	,			dtype=torch.long	,			device=__lowerCAmelCase	)
														UpperCamelCase__					  =      self.seq_length // 2
														UpperCamelCase__					  =      0

														# first forward pass
														UpperCamelCase__  ,  UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	).to_tuple()

														# create hypothetical next token and extent to next_input_ids
														UpperCamelCase__					  =      ids_tensor((self.batch_size, 1)	,			config.vocab_size	)

														# change a random masked slice from input_ids
														UpperCamelCase__					  =      ids_tensor((1,)	,			__lowerCAmelCase	).item() + 1
														UpperCamelCase__					  =      ids_tensor((self.batch_size, 1)	,			config.vocab_size	).squeeze(-1	)
														UpperCamelCase__					  =      random_other_next_tokens

														# append to next input_ids and attn_mask
														UpperCamelCase__					  =      torch.cat([input_ids, next_tokens]	,			dim=-1	)
														UpperCamelCase__					  =      torch.cat(
														    [attn_mask, torch.ones((attn_mask.shape[0], 1)	,			dtype=torch.long	,			device=__lowerCAmelCase	)]	,			dim=1	,			)

														# get two different outputs
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	)["""last_hidden_state"""]
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			past_key_values=__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	)["""last_hidden_state"""]

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

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






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			*__lowerCAmelCase	):
														UpperCamelCase__					  =      BioGptModel(config=__lowerCAmelCase	).to(__lowerCAmelCase	).eval()

														UpperCamelCase__					  =      torch.ones(input_ids.shape	,			dtype=torch.long	,			device=__lowerCAmelCase	)

														# first forward pass
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			use_cache=__lowerCAmelCase	)

														UpperCamelCase__  ,  UpperCamelCase__					  =      outputs.to_tuple()

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

														# append to next input_ids and
														UpperCamelCase__					  =      torch.cat([input_ids, next_tokens]	,			dim=-1	)
														UpperCamelCase__					  =      torch.cat([attention_mask, next_attn_mask]	,			dim=-1	)

														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	)["""last_hidden_state"""]
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			past_key_values=__lowerCAmelCase	)[
														    """last_hidden_state"""
														]

														# select random slice
														UpperCamelCase__					  =      ids_tensor((1,)	,			output_from_past.shape[-1]	).item()
														UpperCamelCase__					  =      output_from_no_past[:, -3:, random_slice_idx].detach()
														UpperCamelCase__					  =      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 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			*__lowerCAmelCase	,			__lowerCAmelCase=False	):
														UpperCamelCase__					  =      BioGptForCausalLM(__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														if gradient_checkpointing:
																					model.gradient_checkpointing_enable()

														UpperCamelCase__					  =      model(__lowerCAmelCase	,			labels=__lowerCAmelCase	)
														self.parent.assertEqual(result.loss.shape	,			()	)
														self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)	)
														result.loss.backward()






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			*__lowerCAmelCase	):
														UpperCamelCase__					  =      BioGptModel(__lowerCAmelCase	)
														UpperCamelCase__					  =      model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers	)
														for key in model.state_dict().keys():
																					if "c_proj" in key and "weight" in key:
																												self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key]	) - model_std	)	,			0.001	)
																												self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key]	) - 0.0	)	,			0.01	)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			*__lowerCAmelCase	):
														UpperCamelCase__					  =      self.num_labels
														UpperCamelCase__					  =      BioGptForTokenClassification(__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			token_type_ids=__lowerCAmelCase	)
														self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.num_labels)	)







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.prepare_config_and_inputs()
														(
														    (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  (
														    UpperCamelCase__
														)  ,  
														)					  =      config_and_inputs
														UpperCamelCase__					  =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
														return config, inputs_dict






@require_torch
class 							__SCREAMING_SNAKE_CASE       (  _a ,		_a ,		_a ,		unittest.TestCase							):
							snake_case  :   Optional[int]      =							(
							    (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification)
							    if is_torch_available()
							    else ()
							)
							snake_case  :   List[Any]      =							(BioGptForCausalLM,) if is_torch_available() else ()
							snake_case  :   Optional[Any]      =							(
							    {
							        """feature-extraction""": BioGptModel,
							        """text-classification""": BioGptForSequenceClassification,
							        """text-generation""": BioGptForCausalLM,
							        """token-classification""": BioGptForTokenClassification,
							        """zero-shot""": BioGptForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							snake_case  :   Dict      =							False






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      BioGptModelTester(self	)
														UpperCamelCase__					  =      ConfigTester(self	,			config_class=__lowerCAmelCase	,			hidden_size=37	)






							def 					_lowerCamelCase		(    self	):
														self.config_tester.run_common_tests()






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					UpperCamelCase__					  =      type
																					self.model_tester.create_and_check_model(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_biogpt_model_attention_mask_past(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_forward_and_backwards(*__lowerCAmelCase	,			gradient_checkpointing=__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_biogpt_model_past_large_inputs(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_biogpt_weight_initialization(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_biogpt_for_token_classification(*__lowerCAmelCase	)






							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""	)
														model.to(__lowerCAmelCase	)
														UpperCamelCase__					  =      BioGptTokenizer.from_pretrained("""microsoft/biogpt"""	)

														UpperCamelCase__					  =      """left"""

														# Define PAD Token = EOS Token = 50256
														UpperCamelCase__					  =      tokenizer.eos_token
														UpperCamelCase__					  =      model.config.eos_token_id

														# use different length sentences to test batching
														UpperCamelCase__					  =      [
														    """Hello, my dog is a little""",
														    """Today, I""",
														]

														UpperCamelCase__					  =      tokenizer(__lowerCAmelCase	,			return_tensors="""pt"""	,			padding=__lowerCAmelCase	)
														UpperCamelCase__					  =      inputs["""input_ids"""].to(__lowerCAmelCase	)

														UpperCamelCase__					  =      model.generate(
														    input_ids=__lowerCAmelCase	,			attention_mask=inputs["""attention_mask"""].to(__lowerCAmelCase	)	,			)

														UpperCamelCase__					  =      tokenizer(sentences[0]	,			return_tensors="""pt"""	).input_ids.to(__lowerCAmelCase	)
														UpperCamelCase__					  =      model.generate(input_ids=__lowerCAmelCase	)

														UpperCamelCase__					  =      inputs_non_padded.shape[-1] - inputs["""attention_mask"""][-1].long().sum().cpu().item()
														UpperCamelCase__					  =      tokenizer(sentences[1]	,			return_tensors="""pt"""	).input_ids.to(__lowerCAmelCase	)
														UpperCamelCase__					  =      model.generate(input_ids=__lowerCAmelCase	,			max_length=model.config.max_length - num_paddings	)

														UpperCamelCase__					  =      tokenizer.batch_decode(__lowerCAmelCase	,			skip_special_tokens=__lowerCAmelCase	)
														UpperCamelCase__					  =      tokenizer.decode(output_non_padded[0]	,			skip_special_tokens=__lowerCAmelCase	)
														UpperCamelCase__					  =      tokenizer.decode(output_padded[0]	,			skip_special_tokens=__lowerCAmelCase	)

														UpperCamelCase__					  =      [
														    """Hello, my dog is a little bit bigger than a little bit.""",
														    """Today, I have a good idea of how to use the information""",
														]
														self.assertListEqual(__lowerCAmelCase	,			__lowerCAmelCase	)
														self.assertListEqual(__lowerCAmelCase	,			[non_padded_sentence, padded_sentence]	)






							@slow
							def 					_lowerCamelCase		(    self	):
														for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					UpperCamelCase__					  =      BioGptModel.from_pretrained(__lowerCAmelCase	)
																					self.assertIsNotNone(__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__  ,  UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs_for_common()
														UpperCamelCase__					  =      3
														UpperCamelCase__					  =      input_dict["""input_ids"""]
														UpperCamelCase__					  =      input_ids.ne(1	).to(__lowerCAmelCase	)
														UpperCamelCase__					  =      ids_tensor([self.model_tester.batch_size]	,			self.model_tester.type_sequence_label_size	)
														UpperCamelCase__					  =      BioGptForSequenceClassification(__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			labels=__lowerCAmelCase	)
														self.assertEqual(result.logits.shape	,			(self.model_tester.batch_size, self.model_tester.num_labels)	)







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__  ,  UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs_for_common()
														UpperCamelCase__					  =      3
														UpperCamelCase__					  =      """multi_label_classification"""
														UpperCamelCase__					  =      input_dict["""input_ids"""]
														UpperCamelCase__					  =      input_ids.ne(1	).to(__lowerCAmelCase	)
														UpperCamelCase__					  =      ids_tensor(
														    [self.model_tester.batch_size, config.num_labels]	,			self.model_tester.type_sequence_label_size	).to(torch.float	)
														UpperCamelCase__					  =      BioGptForSequenceClassification(__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			attention_mask=__lowerCAmelCase	,			labels=__lowerCAmelCase	)
														self.assertEqual(result.logits.shape	,			(self.model_tester.batch_size, self.model_tester.num_labels)	)






@require_torch
class 							__SCREAMING_SNAKE_CASE       (  unittest.TestCase							):






							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""	)
														UpperCamelCase__					  =      torch.tensor([[2, 4805, 9, 656, 21]]	)
														UpperCamelCase__					  =      model(__lowerCAmelCase	)[0]

														UpperCamelCase__					  =      42384

														UpperCamelCase__					  =      torch.Size((1, 5, vocab_size)	)
														self.assertEqual(output.shape	,			__lowerCAmelCase	)

														UpperCamelCase__					  =      torch.tensor(
														    [[[-9.5236, -9.8918, 10.4557], [-11.0469, -9.6423, 8.1022], [-8.8664, -7.8826, 5.5325]]]	)

														self.assertTrue(torch.allclose(output[:, :3, :3]	,			__lowerCAmelCase	,			atol=1E-4	)	)







							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      BioGptTokenizer.from_pretrained("""microsoft/biogpt"""	)
														UpperCamelCase__					  =      BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""	)
														model.to(__lowerCAmelCase	)

														torch.manual_seed(0	)
														UpperCamelCase__					  =      tokenizer("""COVID-19 is"""	,			return_tensors="""pt"""	).to(__lowerCAmelCase	)
														UpperCamelCase__					  =      model.generate(
														    **__lowerCAmelCase	,			min_length=100	,			max_length=1024	,			num_beams=5	,			early_stopping=__lowerCAmelCase	,			)
														UpperCamelCase__					  =      tokenizer.decode(output_ids[0]	,			skip_special_tokens=__lowerCAmelCase	)

														UpperCamelCase__					  =      (
														    """COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the"""
														    """ causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and"""
														    """ territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),"""
														    """ and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and"""
														    """ more than 800,000 deaths."""
														)
														self.assertEqual(__lowerCAmelCase	,			__lowerCAmelCase	)











import json
import os
import shutil
import tempfile
from unittest import TestCase

from transformers import BartTokenizer, BartTokenizerFast, DPRQuestionEncoderTokenizer, DPRQuestionEncoderTokenizerFast
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_tokenizers, require_torch, slow
from transformers.utils import is_datasets_available, is_faiss_available, is_torch_available


if is_torch_available() and is_datasets_available() and is_faiss_available():
					from transformers.models.rag.configuration_rag import RagConfig
					from transformers.models.rag.tokenization_rag import RagTokenizer

@require_faiss
@require_torch
class 							__SCREAMING_SNAKE_CASE       (  _a							):






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      tempfile.mkdtemp()
														UpperCamelCase__					  =      8

														# DPR tok
														UpperCamelCase__					  =      [
														    """[UNK]""",
														    """[CLS]""",
														    """[SEP]""",
														    """[PAD]""",
														    """[MASK]""",
														    """want""",
														    """##want""",
														    """##ed""",
														    """wa""",
														    """un""",
														    """runn""",
														    """##ing""",
														    """,""",
														    """low""",
														    """lowest""",
														]
														UpperCamelCase__					  =      os.path.join(self.tmpdirname	,			"""dpr_tokenizer"""	)
														os.makedirs(__lowerCAmelCase	,			exist_ok=__lowerCAmelCase	)
														UpperCamelCase__					  =      os.path.join(__lowerCAmelCase	,			DPR_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]	)	)

														# BART tok
														UpperCamelCase__					  =      [
														    """l""",
														    """o""",
														    """w""",
														    """e""",
														    """r""",
														    """s""",
														    """t""",
														    """i""",
														    """d""",
														    """n""",
														    """\u0120""",
														    """\u0120l""",
														    """\u0120n""",
														    """\u0120lo""",
														    """\u0120low""",
														    """er""",
														    """\u0120lowest""",
														    """\u0120newer""",
														    """\u0120wider""",
														    """<unk>""",
														]
														UpperCamelCase__					  =      dict(zip(__lowerCAmelCase	,			range(len(__lowerCAmelCase	)	)	)	)
														UpperCamelCase__					  =      ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
														UpperCamelCase__					  =      {"""unk_token""": """<unk>"""}

														UpperCamelCase__					  =      os.path.join(self.tmpdirname	,			"""bart_tokenizer"""	)
														os.makedirs(__lowerCAmelCase	,			exist_ok=__lowerCAmelCase	)
														UpperCamelCase__					  =      os.path.join(__lowerCAmelCase	,			BART_VOCAB_FILES_NAMES["""vocab_file"""]	)
														UpperCamelCase__					  =      os.path.join(__lowerCAmelCase	,			BART_VOCAB_FILES_NAMES["""merges_file"""]	)
														with open(self.vocab_file	,			"""w"""	,			encoding="""utf-8"""	) as fp:
																					fp.write(json.dumps(__lowerCAmelCase	) + """\n"""	)
														with open(self.merges_file	,			"""w"""	,			encoding="""utf-8"""	) as fp:
																					fp.write("""\n""".join(__lowerCAmelCase	)	)






							def 					_lowerCamelCase		(    self	):
														return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname	,			"""dpr_tokenizer"""	)	)






							def 					_lowerCamelCase		(    self	):
														return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname	,			"""bart_tokenizer"""	)	)






							def 					_lowerCamelCase		(    self	):
														shutil.rmtree(self.tmpdirname	)






							@require_tokenizers
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      os.path.join(self.tmpdirname	,			"""rag_tokenizer"""	)
														UpperCamelCase__					  =      RagConfig(question_encoder=DPRConfig().to_dict()	,			generator=BartConfig().to_dict()	)
														UpperCamelCase__					  =      RagTokenizer(question_encoder=self.get_dpr_tokenizer()	,			generator=self.get_bart_tokenizer()	)
														rag_config.save_pretrained(__lowerCAmelCase	)
														rag_tokenizer.save_pretrained(__lowerCAmelCase	)
														UpperCamelCase__					  =      RagTokenizer.from_pretrained(__lowerCAmelCase	,			config=__lowerCAmelCase	)
														self.assertIsInstance(new_rag_tokenizer.question_encoder	,			__lowerCAmelCase	)
														self.assertEqual(new_rag_tokenizer.question_encoder.get_vocab()	,			rag_tokenizer.question_encoder.get_vocab()	)
														self.assertIsInstance(new_rag_tokenizer.generator	,			__lowerCAmelCase	)
														self.assertEqual(new_rag_tokenizer.generator.get_vocab()	,			rag_tokenizer.generator.get_vocab()	)






							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      RagTokenizer.from_pretrained("""facebook/rag-token-nq"""	)
														UpperCamelCase__					  =      [
														    """who got the first nobel prize in physics""",
														    """when is the next deadpool movie being released""",
														    """which mode is used for short wave broadcast service""",
														    """who is the owner of reading football club""",
														    """when is the next scandal episode coming out""",
														    """when is the last time the philadelphia won the superbowl""",
														    """what is the most current adobe flash player version""",
														    """how many episodes are there in dragon ball z""",
														    """what is the first step in the evolution of the eye""",
														    """where is gall bladder situated in human body""",
														    """what is the main mineral in lithium batteries""",
														    """who is the president of usa right now""",
														    """where do the greasers live in the outsiders""",
														    """panda is a national animal of which country""",
														    """what is the name of manchester united stadium""",
														]
														UpperCamelCase__					  =      tokenizer(__lowerCAmelCase	)
														self.assertIsNotNone(__lowerCAmelCase	)







							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      RagTokenizer.from_pretrained("""facebook/rag-sequence-nq"""	)
														UpperCamelCase__					  =      [
														    """who got the first nobel prize in physics""",
														    """when is the next deadpool movie being released""",
														    """which mode is used for short wave broadcast service""",
														    """who is the owner of reading football club""",
														    """when is the next scandal episode coming out""",
														    """when is the last time the philadelphia won the superbowl""",
														    """what is the most current adobe flash player version""",
														    """how many episodes are there in dragon ball z""",
														    """what is the first step in the evolution of the eye""",
														    """where is gall bladder situated in human body""",
														    """what is the main mineral in lithium batteries""",
														    """who is the president of usa right now""",
														    """where do the greasers live in the outsiders""",
														    """panda is a national animal of which country""",
														    """what is the name of manchester united stadium""",
														]
														UpperCamelCase__					  =      tokenizer(__lowerCAmelCase	)
														self.assertIsNotNone(__lowerCAmelCase	)




