



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   UpperCAmelCase_    (       a):







							def __init__(   self,			__a,			__a=13,			__a=7,			__a=True,			__a=True,			__a=True,			__a=True,			__a=True,			__a=False,			__a=False,			__a=False,			__a=2,			__a=99,			__a=0,			__a=32,			__a=5,			__a=4,			__a=0.1,			__a=0.1,			__a=512,			__a=12,			__a=2,			__a=0.02,			__a=3,			__a=4,			__a="last",			__a=None,			__a=None,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    parent
														_lowerCAmelCase     :							str    =    batch_size
														_lowerCAmelCase     :							str    =    seq_length
														_lowerCAmelCase     :							Any    =    is_training
														_lowerCAmelCase     :							Union[str, Any]    =    use_input_lengths
														_lowerCAmelCase     :							Optional[Any]    =    use_token_type_ids
														_lowerCAmelCase     :							Dict    =    use_labels
														_lowerCAmelCase     :							Optional[Any]    =    gelu_activation
														_lowerCAmelCase     :							List[str]    =    sinusoidal_embeddings
														_lowerCAmelCase     :							Dict    =    causal
														_lowerCAmelCase     :							Union[str, Any]    =    asm
														_lowerCAmelCase     :							str    =    n_langs
														_lowerCAmelCase     :							Optional[Any]    =    vocab_size
														_lowerCAmelCase     :							Optional[Any]    =    n_special
														_lowerCAmelCase     :							int    =    hidden_size
														_lowerCAmelCase     :							Optional[Any]    =    num_hidden_layers
														_lowerCAmelCase     :							Dict    =    num_attention_heads
														_lowerCAmelCase     :							Tuple    =    hidden_dropout_prob
														_lowerCAmelCase     :							Optional[int]    =    attention_probs_dropout_prob
														_lowerCAmelCase     :							Any    =    max_position_embeddings
														_lowerCAmelCase     :							Union[str, Any]    =    type_vocab_size
														_lowerCAmelCase     :							Any    =    type_sequence_label_size
														_lowerCAmelCase     :							Dict    =    initializer_range
														_lowerCAmelCase     :							List[Any]    =    num_labels
														_lowerCAmelCase     :							Any    =    num_choices
														_lowerCAmelCase     :							Tuple    =    summary_type
														_lowerCAmelCase     :							Union[str, Any]    =    use_proj
														_lowerCAmelCase     :							Tuple    =    scope







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    ids_tensor([self.batch_size, self.seq_length],			self.vocab_size)
														_lowerCAmelCase     :							int    =    random_attention_mask([self.batch_size, self.seq_length])

														_lowerCAmelCase     :							str    =    None
														if self.use_input_lengths:
																					_lowerCAmelCase     :							str    =    (
																					    ids_tensor([self.batch_size],			vocab_size=2) + self.seq_length - 2
																					)  # small variation of seq_length

														_lowerCAmelCase     :							Optional[Any]    =    None
														if self.use_token_type_ids:
																					_lowerCAmelCase     :							str    =    ids_tensor([self.batch_size, self.seq_length],			self.n_langs)

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

														_lowerCAmelCase     :							Union[str, Any]    =    self.get_config()

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







							def    snake_case__      (   self):

														'''simple docstring'''

														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    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    FlaubertModel(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							int    =    model(__a,			lengths=__a,			langs=__a)
														_lowerCAmelCase     :							Optional[Any]    =    model(__a,			langs=__a)
														_lowerCAmelCase     :							List[str]    =    model(__a)
														self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    FlaubertWithLMHeadModel(__a)
														model.to(__a)
														model.eval()

														_lowerCAmelCase     :							List[Any]    =    model(__a,			token_type_ids=__a,			labels=__a)
														self.parent.assertEqual(result.loss.shape,			())
														self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.vocab_size))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    FlaubertForQuestionAnsweringSimple(__a)
														model.to(__a)
														model.eval()

														_lowerCAmelCase     :							Union[str, Any]    =    model(__a)

														_lowerCAmelCase     :							List[str]    =    model(__a,			start_positions=__a,			end_positions=__a)
														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    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    FlaubertForQuestionAnswering(__a)
														model.to(__a)
														model.eval()

														_lowerCAmelCase     :							Any    =    model(__a)

														_lowerCAmelCase     :							str    =    model(
														    __a,			start_positions=__a,			end_positions=__a,			cls_index=__a,			is_impossible=__a,			p_mask=__a,			)

														_lowerCAmelCase     :							str    =    model(
														    __a,			start_positions=__a,			end_positions=__a,			cls_index=__a,			is_impossible=__a,			)

														((_lowerCAmelCase)   ,    )     :							List[Any]    =    result_with_labels.to_tuple()

														_lowerCAmelCase     :							List[Any]    =    model(__a,			start_positions=__a,			end_positions=__a)

														((_lowerCAmelCase)   ,    )     :							int    =    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    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    FlaubertForSequenceClassification(__a)
														model.to(__a)
														model.eval()

														_lowerCAmelCase     :							Optional[int]    =    model(__a)
														_lowerCAmelCase     :							List[Any]    =    model(__a,			labels=__a)

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







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.num_labels
														_lowerCAmelCase     :							Any    =    FlaubertForTokenClassification(__a)
														model.to(__a)
														model.eval()

														_lowerCAmelCase     :							Any    =    model(__a,			attention_mask=__a,			labels=__a)
														self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.num_labels))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			__a,			):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    self.prepare_config_and_inputs()
														(
														    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    
														)     :							Optional[int]    =    config_and_inputs
														_lowerCAmelCase     :							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   UpperCAmelCase_    (       a   ,					a   ,					unittest.TestCase):
							lowerCamelCase__   			=		(
							    (
							        FlaubertModel,
							        FlaubertWithLMHeadModel,
							        FlaubertForQuestionAnswering,
							        FlaubertForQuestionAnsweringSimple,
							        FlaubertForSequenceClassification,
							        FlaubertForTokenClassification,
							        FlaubertForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase__   			=		(
							    {
							        'feature-extraction': FlaubertModel,
							        'fill-mask': FlaubertWithLMHeadModel,
							        'question-answering': FlaubertForQuestionAnsweringSimple,
							        'text-classification': FlaubertForSequenceClassification,
							        'token-classification': FlaubertForTokenClassification,
							        'zero-shot': FlaubertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														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    snake_case__      (   self,			__a,			__a,			__a=False):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    super()._prepare_for_class(__a,			__a,			return_labels=__a)

														if return_labels:
																					if model_class.__name__ == "FlaubertForQuestionAnswering":
																												_lowerCAmelCase     :							List[str]    =    torch.zeros(
																												    self.model_tester.batch_size,			dtype=torch.long,			device=__a)
																												_lowerCAmelCase     :							Optional[int]    =    torch.zeros(
																												    self.model_tester.batch_size,			dtype=torch.long,			device=__a)

														return inputs_dict







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    FlaubertModelTester(self)
														_lowerCAmelCase     :							List[str]    =    ConfigTester(self,			config_class=__a,			emb_dim=37)







							def    snake_case__      (   self):

														'''simple docstring'''

														self.config_tester.run_common_tests()







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_model(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_lm_head(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_simple_qa(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_qa(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_sequence_classif(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_token_classif(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Any    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_flaubert_multiple_choice(*__a)







							@slow
							def    snake_case__      (   self):

														'''simple docstring'''

														for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_lowerCAmelCase     :							Tuple    =    FlaubertModel.from_pretrained(__a)
																					self.assertIsNotNone(__a)







							@slow
							@require_torch_gpu
							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase   ,    _lowerCAmelCase     :							Optional[Any]    =    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     :							Tuple    =    True
																					_lowerCAmelCase     :							List[Any]    =    model_class(config=__a)

																					_lowerCAmelCase     :							Dict    =    self._prepare_for_class(__a,			__a)
																					_lowerCAmelCase     :							Any    =    torch.jit.trace(
																					    __a,			(inputs_dict["input_ids"].to("cpu"), inputs_dict["attention_mask"].to("cpu")))

																					with tempfile.TemporaryDirectory() as tmp:
																												torch.jit.save(__a,			os.path.join(__a,			"traced_model.pt"))
																												_lowerCAmelCase     :							int    =    torch.jit.load(os.path.join(__a,			"traced_model.pt"),			map_location=__a)
																												loaded(inputs_dict["input_ids"].to(__a),			inputs_dict["attention_mask"].to(__a))



@require_torch
class   UpperCAmelCase_    (       unittest.TestCase):







							@slow
							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    FlaubertModel.from_pretrained("flaubert/flaubert_base_cased")
														_lowerCAmelCase     :							List[str]    =    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]])
														with torch.no_grad():
																					_lowerCAmelCase     :							Optional[int]    =    model(__a)[0]
														_lowerCAmelCase     :							Dict    =    torch.Size((1, 11, 768))
														self.assertEqual(output.shape,			__a)
														_lowerCAmelCase     :							str    =    torch.tensor(
														    [[[-2.6_251, -1.4_298, -0.0_227], [-2.8_510, -1.6_387, 0.2_258], [-2.8_114, -1.1_832, -0.3_066]]])

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





def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							if bit_count < 0:
														raise ValueError("The given input must be positive"  )

							# get the generated string sequence
							_lowerCAmelCase     :							List[str]    =    gray_code_sequence_string(_lowerCamelCase  )
							#
							# convert them to integers
							for i in range(len(_lowerCamelCase  )  ):
														_lowerCAmelCase     :							List[str]    =    int(sequence[i]				, 2  )

							return sequence






def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							if bit_count == 0:
														return ["0"]

							if bit_count == 1:
														return ["0", "1"]

							_lowerCAmelCase     :							List[Any]    =    1 << bit_count  # defines the length of the sequence
							# 1<< n is equivalent to 2^n

							# recursive answer will generate answer for n-1 bits
							_lowerCAmelCase     :							Optional[int]    =    gray_code_sequence_string(bit_count - 1  )

							_lowerCAmelCase     :							str    =    []

							# append 0 to first half of the smaller sequence generated
							for i in range(seq_len // 2  ):
														_lowerCAmelCase     :							Dict    =    "0" + smaller_sequence[i]
														sequence.append(_lowerCamelCase  )

							# append 1 to second half ... start from the end of the list
							for i in reversed(range(seq_len // 2  )  ):
														_lowerCAmelCase     :							Optional[Any]    =    "1" + smaller_sequence[i]
														sequence.append(_lowerCamelCase  )

							return sequence


if __name__ == "__main__":
						import doctest

						doctest.testmod()
