



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  __snake_case      :







							'''simple docstring'''






							def __init__(							self  ,  a_  ,  a_=13  ,  a_=7  ,  a_=True  ,  a_=True  ,  a_=99  ,  a_=32  ,  a_=5  ,  a_=4  ,  a_=37  ,  a_="gelu"  ,  a_=0.1  ,  a_=0.1  ,  a_=50  ,  a_=0.02  ,  a_=True  ,  a_=None  ,  ):
											a__				      =		parent
											a__				      =		batch_size
											a__				      =		seq_length
											a__				      =		is_training
											a__				      =		use_input_mask
											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__				      =		initializer_range
											a__				      =		use_labels
											a__				      =		scope


							def _a      (							self		):
											a__				      =		ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size		)

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

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

											a__				      =		self.get_config()

											return config, input_ids, input_mask, token_labels


							def _a      (							self		):
											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=a_  ,  initializer_range=self.initializer_range  ,  )


							def _a      (							self		):
											(
											    (
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,						
											)				      =		self.prepare_config_and_inputs()

											a__				      =		True
											a__				      =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
											a__				      =		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 _a      (							self  ,  a_  ,  a_  ,  a_  ,  a_  ,  **a_  ,  ):
											a__				      =		BertGenerationEncoder(config=a_		)
											model.to(a_		)
											model.eval()
											a__				      =		model(a_  ,  attention_mask=a_		)
											a__				      =		model(a_		)
											self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)		)


							def _a      (							self  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  **a_  ,  ):
											a__				      =		True
											a__				      =		BertGenerationEncoder(config=a_		)
											model.to(a_		)
											model.eval()
											a__				      =		model(
											    a_  ,  attention_mask=a_  ,  encoder_hidden_states=a_  ,  encoder_attention_mask=a_  ,  )
											a__				      =		model(
											    a_  ,  attention_mask=a_  ,  encoder_hidden_states=a_  ,  )
											self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)		)


							def _a      (							self  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  **a_  ,  ):
											a__				      =		True
											a__				      =		True
											a__				      =		BertGenerationDecoder(config=a_		).to(a_		).eval()

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

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

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

											a__				      =		model(
											    a_  ,  attention_mask=a_  ,  encoder_hidden_states=a_  ,  encoder_attention_mask=a_  ,  output_hidden_states=a_  ,  )["""hidden_states"""][0]
											a__				      =		model(
											    a_  ,  attention_mask=a_  ,  encoder_hidden_states=a_  ,  encoder_attention_mask=a_  ,  past_key_values=a_  ,  output_hidden_states=a_  ,  )["""hidden_states"""][0]

											# select random slice
											a__				      =		ids_tensor((1,)  ,  output_from_past.shape[-1]		).item()
											a__				      =		output_from_no_past[:, -3:, random_slice_idx].detach()
											a__				      =		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(a_  ,  a_  ,  atol=1E-3		)		)


							def _a      (							self  ,  a_  ,  a_  ,  a_  ,  a_  ,  *a_  ,  ):
											a__				      =		BertGenerationDecoder(a_		)
											model.to(a_		)
											model.eval()
											a__				      =		model(a_  ,  attention_mask=a_  ,  labels=a_		)
											self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)		)







							def _a      (							self		):
											a__      ,			a__      ,			a__      ,			a__				      =		self.prepare_config_and_inputs()
											a__				      =		{"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict




@require_torch
class  __snake_case      (   SCREAMING_SNAKE_CASE			,SCREAMING_SNAKE_CASE			,SCREAMING_SNAKE_CASE			,unittest.TestCase):







							'''simple docstring'''






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


							def _a      (							self		):
											a__				      =		BertGenerationEncoderTester(self		)
											a__				      =		ConfigTester(self  ,  config_class=a_  ,  hidden_size=37		)


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


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*a_		)


							def _a      (							self		):
											a__      ,			a__      ,			a__      ,			a__				      =		self.model_tester.prepare_config_and_inputs()
											a__				      =		"""bert"""
											self.model_tester.create_and_check_model(a_  ,  a_  ,  a_  ,  a_		)


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*a_		)


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_decoder_model_past_large_inputs(*a_		)


							def _a      (							self		):
											# This regression test was failing with PyTorch < 1.3
											(
											    (
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,			(
											    a__			
											)      ,						
											)				      =		self.model_tester.prepare_config_and_inputs_for_decoder()

											a__				      =		None

											self.model_tester.create_and_check_model_as_decoder(
											    a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  a_  ,  )


							def _a      (							self		):
											a__				      =		self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_for_causal_lm(*a_		)







							@slow
							def _a      (							self		):
											a__				      =		BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
											self.assertIsNotNone(a_		)




@require_torch
class  __snake_case      (   unittest.TestCase):







							'''simple docstring'''






							@slow
							def _a      (							self		):
											a__				      =		BertGenerationEncoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
											a__				      =		torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]		)
											with torch.no_grad():
															a__				      =		model(a_		)[0]
											a__				      =		torch.Size([1, 8, 1_024]		)
											self.assertEqual(output.shape  ,  a_		)
											a__				      =		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]  ,  a_  ,  atol=1E-4		)		)




@require_torch
class  __snake_case      (   unittest.TestCase):







							'''simple docstring'''






							@slow
							def _a      (							self		):
											a__				      =		BertGenerationDecoder.from_pretrained("""google/bert_for_seq_generation_L-24_bbc_encoder"""		)
											a__				      =		torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]		)
											with torch.no_grad():
															a__				      =		model(a_		)[0]
											a__				      =		torch.Size([1, 8, 50_358]		)
											self.assertEqual(output.shape  ,  a_		)
											a__				      =		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]  ,  a_  ,  atol=1E-4		)		)






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase							 =					{
    """configuration_xlm_roberta""": [
        """XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """XLMRobertaConfig""",
        """XLMRobertaOnnxConfig""",
    ],
}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					["""XLMRobertaTokenizer"""]

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					["""XLMRobertaTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """XLMRobertaForCausalLM""",
	    """XLMRobertaForMaskedLM""",
	    """XLMRobertaForMultipleChoice""",
	    """XLMRobertaForQuestionAnswering""",
	    """XLMRobertaForSequenceClassification""",
	    """XLMRobertaForTokenClassification""",
	    """XLMRobertaModel""",
	    """XLMRobertaPreTrainedModel""",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TFXLMRobertaForCausalLM""",
	    """TFXLMRobertaForMaskedLM""",
	    """TFXLMRobertaForMultipleChoice""",
	    """TFXLMRobertaForQuestionAnswering""",
	    """TFXLMRobertaForSequenceClassification""",
	    """TFXLMRobertaForTokenClassification""",
	    """TFXLMRobertaModel""",
	    """TFXLMRobertaPreTrainedModel""",
	]

try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """FlaxXLMRobertaForMaskedLM""",
	    """FlaxXLMRobertaForCausalLM""",
	    """FlaxXLMRobertaForMultipleChoice""",
	    """FlaxXLMRobertaForQuestionAnswering""",
	    """FlaxXLMRobertaForSequenceClassification""",
	    """FlaxXLMRobertaForTokenClassification""",
	    """FlaxXLMRobertaModel""",
	    """FlaxXLMRobertaPreTrainedModel""",
	]

if TYPE_CHECKING:
	from .configuration_xlm_roberta import (
	    XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    XLMRobertaConfig,
	    XLMRobertaOnnxConfig,
	)

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlm_roberta import XLMRobertaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xlm_roberta import (
		    XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XLMRobertaForCausalLM,
		    XLMRobertaForMaskedLM,
		    XLMRobertaForMultipleChoice,
		    XLMRobertaForQuestionAnswering,
		    XLMRobertaForSequenceClassification,
		    XLMRobertaForTokenClassification,
		    XLMRobertaModel,
		    XLMRobertaPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_xlm_roberta import (
		    TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXLMRobertaForCausalLM,
		    TFXLMRobertaForMaskedLM,
		    TFXLMRobertaForMultipleChoice,
		    TFXLMRobertaForQuestionAnswering,
		    TFXLMRobertaForSequenceClassification,
		    TFXLMRobertaForTokenClassification,
		    TFXLMRobertaModel,
		    TFXLMRobertaPreTrainedModel,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_xlm_roberta import (
		    FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    FlaxXLMRobertaForCausalLM,
		    FlaxXLMRobertaForMaskedLM,
		    FlaxXLMRobertaForMultipleChoice,
		    FlaxXLMRobertaForQuestionAnswering,
		    FlaxXLMRobertaForSequenceClassification,
		    FlaxXLMRobertaForTokenClassification,
		    FlaxXLMRobertaModel,
		    FlaxXLMRobertaPreTrainedModel,
		)

else:
	import sys

	UpperCAmelCase							 =					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

