


'''simple docstring'''




import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

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 (
				    FlaubertForMultipleChoice,
				    FlaubertForQuestionAnswering,
				    FlaubertForQuestionAnsweringSimple,
				    FlaubertForSequenceClassification,
				    FlaubertForTokenClassification,
				    FlaubertModel,
				    FlaubertWithLMHeadModel,
				)
				from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class  lowerCAmelCase				(				a							):



	def __init__(					self ,					snake_case__ ,					snake_case__=13 ,					snake_case__=7 ,					snake_case__=True ,					snake_case__=True ,					snake_case__=True ,					snake_case__=True ,					snake_case__=True ,					snake_case__=False ,					snake_case__=False ,					snake_case__=False ,					snake_case__=2 ,					snake_case__=99 ,					snake_case__=0 ,					snake_case__=32 ,					snake_case__=5 ,					snake_case__=4 ,					snake_case__=0.1 ,					snake_case__=0.1 ,					snake_case__=512 ,					snake_case__=12 ,					snake_case__=2 ,					snake_case__=0.0_2 ,					snake_case__=3 ,					snake_case__=4 ,					snake_case__="last" ,					snake_case__=None ,					snake_case__=None ,					):
		lowerCAmelCase					:    Tuple      							= parent
		lowerCAmelCase					:    Any      							= batch_size
		lowerCAmelCase					:    Union[str, Any]      							= seq_length
		lowerCAmelCase					:    str      							= is_training
		lowerCAmelCase					:    List[str]      							= use_input_lengths
		lowerCAmelCase					:    str      							= use_token_type_ids
		lowerCAmelCase					:    Tuple      							= use_labels
		lowerCAmelCase					:    List[str]      							= gelu_activation
		lowerCAmelCase					:    str      							= sinusoidal_embeddings
		lowerCAmelCase					:    str      							= causal
		lowerCAmelCase					:    Dict      							= asm
		lowerCAmelCase					:    List[str]      							= n_langs
		lowerCAmelCase					:    Tuple      							= vocab_size
		lowerCAmelCase					:    Optional[Any]      							= n_special
		lowerCAmelCase					:    Optional[int]      							= hidden_size
		lowerCAmelCase					:    Optional[int]      							= num_hidden_layers
		lowerCAmelCase					:    Dict      							= num_attention_heads
		lowerCAmelCase					:    List[Any]      							= hidden_dropout_prob
		lowerCAmelCase					:    List[str]      							= attention_probs_dropout_prob
		lowerCAmelCase					:    Optional[int]      							= max_position_embeddings
		lowerCAmelCase					:    List[str]      							= type_vocab_size
		lowerCAmelCase					:    Optional[int]      							= type_sequence_label_size
		lowerCAmelCase					:    Any      							= initializer_range
		lowerCAmelCase					:    Optional[int]      							= num_labels
		lowerCAmelCase					:    Tuple      							= num_choices
		lowerCAmelCase					:    Optional[int]      							= summary_type
		lowerCAmelCase					:    Optional[Any]      							= use_proj
		lowerCAmelCase					:    str      							= scope



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Dict      							= ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size    )
		lowerCAmelCase					:    Tuple      							= random_attention_mask([self.batch_size, self.seq_length]    )

		lowerCAmelCase					:    Optional[int]      							= None
		if self.use_input_lengths:
			lowerCAmelCase					:    Dict      							= (
			    ids_tensor([self.batch_size] ,					vocab_size=2    ) + self.seq_length - 2
			)  # small variation of seq_length

		lowerCAmelCase					:    int      							= None
		if self.use_token_type_ids:
			lowerCAmelCase					:    Dict      							= ids_tensor([self.batch_size, self.seq_length] ,					self.n_langs    )

		lowerCAmelCase					:    str      							= None
		lowerCAmelCase					:    List[str]      							= None
		lowerCAmelCase					:    List[str]      							= None
		if self.use_labels:
			lowerCAmelCase					:    Dict      							= ids_tensor([self.batch_size] ,					self.type_sequence_label_size    )
			lowerCAmelCase					:    int      							= ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels    )
			lowerCAmelCase					:    Any      							= ids_tensor([self.batch_size] ,					2    ).float()
			lowerCAmelCase					:    int      							= ids_tensor([self.batch_size] ,					self.num_choices    )

		lowerCAmelCase					:    Tuple      							= self.get_config()

		return (
		    config,
		    input_ids,
		    token_type_ids,
		    input_lengths,
		    sequence_labels,
		    token_labels,
		    is_impossible_labels,
		    choice_labels,
		    input_mask,
		)



	def 				lowercase     (					self    ):
		return FlaubertConfig(
		    vocab_size=self.vocab_size ,					n_special=self.n_special ,					emb_dim=self.hidden_size ,					n_layers=self.num_hidden_layers ,					n_heads=self.num_attention_heads ,					dropout=self.hidden_dropout_prob ,					attention_dropout=self.attention_probs_dropout_prob ,					gelu_activation=self.gelu_activation ,					sinusoidal_embeddings=self.sinusoidal_embeddings ,					asm=self.asm ,					causal=self.causal ,					n_langs=self.n_langs ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					summary_type=self.summary_type ,					use_proj=self.use_proj ,					)



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    int      							= FlaubertModel(config=snake_case__    )
		model.to(snake_case__    )
		model.eval()
		lowerCAmelCase					:    Optional[int]      							= model(snake_case__ ,					lengths=snake_case__ ,					langs=snake_case__    )
		lowerCAmelCase					:    int      							= model(snake_case__ ,					langs=snake_case__    )
		lowerCAmelCase					:    Union[str, Any]      							= model(snake_case__    )
		self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    str      							= FlaubertWithLMHeadModel(snake_case__    )
		model.to(snake_case__    )
		model.eval()

		lowerCAmelCase					:    Optional[int]      							= model(snake_case__ ,					token_type_ids=snake_case__ ,					labels=snake_case__    )
		self.parent.assertEqual(result.loss.shape ,					()    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    Optional[Any]      							= FlaubertForQuestionAnsweringSimple(snake_case__    )
		model.to(snake_case__    )
		model.eval()

		lowerCAmelCase					:    int      							= model(snake_case__    )

		lowerCAmelCase					:    List[Any]      							= model(snake_case__ ,					start_positions=snake_case__ ,					end_positions=snake_case__    )
		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)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    Dict      							= FlaubertForQuestionAnswering(snake_case__    )
		model.to(snake_case__    )
		model.eval()

		lowerCAmelCase					:    Any      							= model(snake_case__    )

		lowerCAmelCase					:    List[str]      							= model(
		    snake_case__ ,					start_positions=snake_case__ ,					end_positions=snake_case__ ,					cls_index=snake_case__ ,					is_impossible=snake_case__ ,					p_mask=snake_case__ ,					)

		lowerCAmelCase					:    List[Any]      							= model(
		    snake_case__ ,					start_positions=snake_case__ ,					end_positions=snake_case__ ,					cls_index=snake_case__ ,					is_impossible=snake_case__ ,					)

		(lowerCAmelCase   )					:    Any      							= result_with_labels.to_tuple()

		lowerCAmelCase					:    Tuple      							= model(snake_case__ ,					start_positions=snake_case__ ,					end_positions=snake_case__    )

		(lowerCAmelCase   )					:    str      							= result_with_labels.to_tuple()

		self.parent.assertEqual(result_with_labels.loss.shape ,					()    )
		self.parent.assertEqual(result.start_top_log_probs.shape ,					(self.batch_size, model.config.start_n_top)    )
		self.parent.assertEqual(result.start_top_index.shape ,					(self.batch_size, model.config.start_n_top)    )
		self.parent.assertEqual(
		    result.end_top_log_probs.shape ,					(self.batch_size, model.config.start_n_top * model.config.end_n_top)    )
		self.parent.assertEqual(
		    result.end_top_index.shape ,					(self.batch_size, model.config.start_n_top * model.config.end_n_top)    )
		self.parent.assertEqual(result.cls_logits.shape ,					(self.batch_size,)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    str      							= FlaubertForSequenceClassification(snake_case__    )
		model.to(snake_case__    )
		model.eval()

		lowerCAmelCase					:    Tuple      							= model(snake_case__    )
		lowerCAmelCase					:    Dict      							= model(snake_case__ ,					labels=snake_case__    )

		self.parent.assertEqual(result.loss.shape ,					()    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    Optional[Any]      							= self.num_labels
		lowerCAmelCase					:    Any      							= FlaubertForTokenClassification(snake_case__    )
		model.to(snake_case__    )
		model.eval()

		lowerCAmelCase					:    int      							= model(snake_case__ ,					attention_mask=snake_case__ ,					labels=snake_case__    )
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)    )



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					):
		lowerCAmelCase					:    Optional[Any]      							= self.num_choices
		lowerCAmelCase					:    Any      							= FlaubertForMultipleChoice(config=snake_case__    )
		model.to(snake_case__    )
		model.eval()
		lowerCAmelCase					:    List[Any]      							= input_ids.unsqueeze(1    ).expand(-1 ,					self.num_choices ,					-1    ).contiguous()
		lowerCAmelCase					:    Tuple      							= token_type_ids.unsqueeze(1    ).expand(-1 ,					self.num_choices ,					-1    ).contiguous()
		lowerCAmelCase					:    int      							= input_mask.unsqueeze(1    ).expand(-1 ,					self.num_choices ,					-1    ).contiguous()
		lowerCAmelCase					:    Tuple      							= model(
		    snake_case__ ,					attention_mask=snake_case__ ,					token_type_ids=snake_case__ ,					labels=snake_case__ ,					)
		self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_choices)    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[Any]      							= self.prepare_config_and_inputs()
		(
		    lowerCAmelCase   
		)					:    Tuple      							= config_and_inputs
		lowerCAmelCase					:    List[str]      							= {
		    'input_ids': input_ids,
		    'token_type_ids': token_type_ids,
		    'lengths': input_lengths,
		    'attention_mask': input_mask,
		}
		return config, inputs_dict



@require_torch
class  lowerCAmelCase				(				a      ,				a      ,				unittest.TestCase							):
	_lowerCamelCase   :					Union[str, Any]			      = (
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowerCamelCase   :					Any			      = (
	    {
	        """feature-extraction""": FlaubertModel,
	        """fill-mask""": FlaubertWithLMHeadModel,
	        """question-answering""": FlaubertForQuestionAnsweringSimple,
	        """text-classification""": FlaubertForSequenceClassification,
	        """token-classification""": FlaubertForTokenClassification,
	        """zero-shot""": FlaubertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__ ,					snake_case__    ):
		if (
		    pipeline_test_casse_name == "QAPipelineTests"
		    and tokenizer_name is not None
		    and not tokenizer_name.endswith('Fast'    )
		):
			# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
			# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
			# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
			return True

		return False



	def 				lowercase     (					self ,					snake_case__ ,					snake_case__ ,					snake_case__=False    ):
		lowerCAmelCase					:    Optional[int]      							= super()._prepare_for_class(snake_case__ ,					snake_case__ ,					return_labels=snake_case__    )

		if return_labels:
			if model_class.__name__ == "FlaubertForQuestionAnswering":
				lowerCAmelCase					:    Union[str, Any]      							= torch.zeros(
				    self.model_tester.batch_size ,					dtype=torch.long ,					device=snake_case__    )
				lowerCAmelCase					:    Tuple      							= torch.zeros(
				    self.model_tester.batch_size ,					dtype=torch.long ,					device=snake_case__    )

		return inputs_dict



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[Any]      							= FlaubertModelTester(self    )
		lowerCAmelCase					:    List[Any]      							= ConfigTester(self ,					config_class=snake_case__ ,					emb_dim=37    )



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



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    List[Any]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_model(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Tuple      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_lm_head(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Dict      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_simple_qa(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    List[str]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_qa(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Union[str, Any]      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_token_classif(*snake_case__    )



	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Dict      							= self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case__    )



	@slow
	def 				lowercase     (					self    ):
		for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowerCAmelCase					:    Union[str, Any]      							= FlaubertModel.from_pretrained(snake_case__    )
			self.assertIsNotNone(snake_case__    )



	@slow
	@require_torch_gpu
	def 				lowercase     (					self    ):
		lowerCAmelCase					:    Optional[int]      							= self.model_tester.prepare_config_and_inputs_for_common()
		for model_class in self.all_model_classes:
			# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
			if model_class == FlaubertForMultipleChoice:
				return

			lowerCAmelCase					:    Dict      							= True
			lowerCAmelCase					:    Optional[Any]      							= model_class(config=snake_case__    )

			lowerCAmelCase					:    int      							= self._prepare_for_class(snake_case__ ,					snake_case__    )
			lowerCAmelCase					:    str      							= torch.jit.trace(
			    snake_case__ ,					(inputs_dict['input_ids'].to('cpu'    ), inputs_dict['attention_mask'].to('cpu'    ))    )

			with tempfile.TemporaryDirectory() as tmp:
				torch.jit.save(snake_case__ ,					os.path.join(snake_case__ ,					'traced_model.pt'    )    )
				lowerCAmelCase					:    List[Any]      							= torch.jit.load(os.path.join(snake_case__ ,					'traced_model.pt'    ) ,					map_location=snake_case__    )
				loaded(inputs_dict['input_ids'].to(snake_case__    ) ,					inputs_dict['attention_mask'].to(snake_case__    )    )



@require_torch
class  lowerCAmelCase				(				unittest.TestCase							):



	@slow
	def 				lowercase     (					self    ):
		lowerCAmelCase					:    int      							= FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'    )
		lowerCAmelCase					:    Tuple      							= torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
		with torch.no_grad():
			lowerCAmelCase					:    List[Any]      							= model(snake_case__    )[0]
		lowerCAmelCase					:    List[str]      							= torch.Size((1, 11, 768)    )
		self.assertEqual(output.shape ,					snake_case__    )
		lowerCAmelCase					:    str      							= torch.tensor(
		    [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]    )

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




'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCAmelCase  :		Any		     =	logging.get_logger(__name__)

_lowerCAmelCase  :		List[str]		     =	{
    'xlm-roberta-base': 'https://huggingface.co/xlm-roberta-base/resolve/main/config.json',
    'xlm-roberta-large': 'https://huggingface.co/xlm-roberta-large/resolve/main/config.json',
    'xlm-roberta-large-finetuned-conll02-dutch': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll02-spanish': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll03-english': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/config.json'
    ),
    'xlm-roberta-large-finetuned-conll03-german': (
        'https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/config.json'
    ),
}



class  lowerCAmelCase				(				a							):
	_lowerCamelCase   :					List[str]			      = """xlm-roberta"""



	def __init__(					self ,					snake_case__=3_0522 ,					snake_case__=768 ,					snake_case__=12 ,					snake_case__=12 ,					snake_case__=3072 ,					snake_case__="gelu" ,					snake_case__=0.1 ,					snake_case__=0.1 ,					snake_case__=512 ,					snake_case__=2 ,					snake_case__=0.0_2 ,					snake_case__=1e-1_2 ,					snake_case__=1 ,					snake_case__=0 ,					snake_case__=2 ,					snake_case__="absolute" ,					snake_case__=True ,					snake_case__=None ,					**snake_case__ ,					):
		super().__init__(pad_token_id=snake_case__ ,					bos_token_id=snake_case__ ,					eos_token_id=snake_case__ ,					**snake_case__    )

		lowerCAmelCase					:    Optional[Any]      							= vocab_size
		lowerCAmelCase					:    Optional[Any]      							= hidden_size
		lowerCAmelCase					:    Optional[Any]      							= num_hidden_layers
		lowerCAmelCase					:    Any      							= num_attention_heads
		lowerCAmelCase					:    Optional[int]      							= hidden_act
		lowerCAmelCase					:    Optional[int]      							= intermediate_size
		lowerCAmelCase					:    Dict      							= hidden_dropout_prob
		lowerCAmelCase					:    Union[str, Any]      							= attention_probs_dropout_prob
		lowerCAmelCase					:    Optional[Any]      							= max_position_embeddings
		lowerCAmelCase					:    Optional[int]      							= type_vocab_size
		lowerCAmelCase					:    int      							= initializer_range
		lowerCAmelCase					:    List[Any]      							= layer_norm_eps
		lowerCAmelCase					:    Union[str, Any]      							= position_embedding_type
		lowerCAmelCase					:    Union[str, Any]      							= use_cache
		lowerCAmelCase					:    List[str]      							= classifier_dropout



class  lowerCAmelCase				(				a							):



	@property
	def 				lowercase     (					self    ):
		if self.task == "multiple-choice":
			lowerCAmelCase					:    str      							= {0: 'batch', 1: 'choice', 2: 'sequence'}
		else:
			lowerCAmelCase					:    Optional[int]      							= {0: 'batch', 1: 'sequence'}
		return OrderedDict(
		    [
		        ('input_ids', dynamic_axis),
		        ('attention_mask', dynamic_axis),
		    ]    )
