





'''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   UpperCAmelCase      (			UpperCamelCase__							):
 def __init__(							self :List[str]		,			lowercase_ :int		,			lowercase_ :int=13		,			lowercase_ :Optional[Any]=7		,			lowercase_ :int=True		,			lowercase_ :Any=True		,			lowercase_ :Optional[Any]=True		,			lowercase_ :int=True		,			lowercase_ :List[Any]=True		,			lowercase_ :Optional[Any]=False		,			lowercase_ :Dict=False		,			lowercase_ :List[Any]=False		,			lowercase_ :int=2		,			lowercase_ :Tuple=99		,			lowercase_ :Optional[Any]=0		,			lowercase_ :Dict=32		,			lowercase_ :Tuple=5		,			lowercase_ :Optional[int]=4		,			lowercase_ :Union[str, Any]=0.1		,			lowercase_ :Optional[Any]=0.1		,			lowercase_ :List[str]=5_12		,			lowercase_ :int=12		,			lowercase_ :Optional[int]=2		,			lowercase_ :Optional[Any]=0.0_2		,			lowercase_ :int=3		,			lowercase_ :Union[str, Any]=4		,			lowercase_ :Optional[Any]="last"		,			lowercase_ :Tuple=None		,			lowercase_ :Tuple=None		,			)->				Dict:
     A__							=     parent
     A__							=     batch_size
     A__							=     seq_length
     A__							=     is_training
     A__							=     use_input_lengths
     A__							=     use_token_type_ids
     A__							=     use_labels
     A__							=     gelu_activation
     A__							=     sinusoidal_embeddings
     A__							=     causal
     A__							=     asm
     A__							=     n_langs
     A__							=     vocab_size
     A__							=     n_special
     A__							=     hidden_size
     A__							=     num_hidden_layers
     A__							=     num_attention_heads
     A__							=     hidden_dropout_prob
     A__							=     attention_probs_dropout_prob
     A__							=     max_position_embeddings
     A__							=     type_vocab_size
     A__							=     type_sequence_label_size
     A__							=     initializer_range
     A__							=     num_labels
     A__							=     num_choices
     A__							=     summary_type
     A__							=     use_proj
     A__							=     scope
 def 	UpperCAmelCase_				(							self :List[Any]				)->				Dict:
     A__							=     ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size				)
     A__							=     random_attention_mask([self.batch_size, self.seq_length]				)

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

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

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

     A__							=     self.get_config()

     return (
         config,
         input_ids,
         token_type_ids,
         input_lengths,
         sequence_labels,
         token_labels,
         is_impossible_labels,
         choice_labels,
         input_mask,
     )
 def 	UpperCAmelCase_				(							self :Dict				)->				List[str]:
     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 	UpperCAmelCase_				(							self :List[str]		,			lowercase_ :List[Any]		,			lowercase_ :Tuple		,			lowercase_ :List[str]		,			lowercase_ :int		,			lowercase_ :List[Any]		,			lowercase_ :Optional[Any]		,			lowercase_ :int		,			lowercase_ :List[str]		,			lowercase_ :Tuple		,			)->				Union[str, Any]:
     A__							=     FlaubertModel(config=lowercase_				)
     model.to(lowercase_				)
     model.eval()
     A__							=     model(lowercase_		,			lengths=lowercase_		,			langs=lowercase_				)
     A__							=     model(lowercase_		,			langs=lowercase_				)
     A__							=     model(lowercase_				)
     self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size)				)
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :Any		,			lowercase_ :Tuple		,			lowercase_ :Optional[Any]		,			lowercase_ :Optional[Any]		,			lowercase_ :Tuple		,			lowercase_ :Union[str, Any]		,			lowercase_ :List[Any]		,			lowercase_ :Optional[int]		,			lowercase_ :Tuple		,			)->				Optional[int]:
     A__							=     FlaubertWithLMHeadModel(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     A__							=     model(lowercase_		,			token_type_ids=lowercase_		,			labels=lowercase_				)
     self.parent.assertEqual(result.loss.shape		,			()				)
     self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)				)
 def 	UpperCAmelCase_				(							self :Tuple		,			lowercase_ :Union[str, Any]		,			lowercase_ :Union[str, Any]		,			lowercase_ :Optional[Any]		,			lowercase_ :str		,			lowercase_ :str		,			lowercase_ :Dict		,			lowercase_ :Tuple		,			lowercase_ :str		,			lowercase_ :str		,			)->				List[Any]:
     A__							=     FlaubertForQuestionAnsweringSimple(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     A__							=     model(lowercase_				)

     A__							=     model(lowercase_		,			start_positions=lowercase_		,			end_positions=lowercase_				)
     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 	UpperCAmelCase_				(							self :Any		,			lowercase_ :List[Any]		,			lowercase_ :Union[str, Any]		,			lowercase_ :Tuple		,			lowercase_ :str		,			lowercase_ :Tuple		,			lowercase_ :Optional[int]		,			lowercase_ :Any		,			lowercase_ :List[str]		,			lowercase_ :int		,			)->				Tuple:
     A__							=     FlaubertForQuestionAnswering(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     A__							=     model(lowercase_				)

     A__							=     model(
         lowercase_		,			start_positions=lowercase_		,			end_positions=lowercase_		,			cls_index=lowercase_		,			is_impossible=lowercase_		,			p_mask=lowercase_		,			)

     A__							=     model(
         lowercase_		,			start_positions=lowercase_		,			end_positions=lowercase_		,			cls_index=lowercase_		,			is_impossible=lowercase_		,			)

     ((A__),      )							=     result_with_labels.to_tuple()

     A__							=     model(lowercase_		,			start_positions=lowercase_		,			end_positions=lowercase_				)

     ((A__),      )							=     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 	UpperCAmelCase_				(							self :List[Any]		,			lowercase_ :Union[str, Any]		,			lowercase_ :int		,			lowercase_ :str		,			lowercase_ :Optional[Any]		,			lowercase_ :List[Any]		,			lowercase_ :Optional[int]		,			lowercase_ :Tuple		,			lowercase_ :Optional[int]		,			lowercase_ :Tuple		,			)->				Dict:
     A__							=     FlaubertForSequenceClassification(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     A__							=     model(lowercase_				)
     A__							=     model(lowercase_		,			labels=lowercase_				)

     self.parent.assertEqual(result.loss.shape		,			()				)
     self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.type_sequence_label_size)				)
 def 	UpperCAmelCase_				(							self :List[str]		,			lowercase_ :str		,			lowercase_ :str		,			lowercase_ :str		,			lowercase_ :Optional[Any]		,			lowercase_ :Dict		,			lowercase_ :str		,			lowercase_ :Tuple		,			lowercase_ :Union[str, Any]		,			lowercase_ :Any		,			)->				Union[str, Any]:
     A__							=     self.num_labels
     A__							=     FlaubertForTokenClassification(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     A__							=     model(lowercase_		,			attention_mask=lowercase_		,			labels=lowercase_				)
     self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.num_labels)				)
 def 	UpperCAmelCase_				(							self :Union[str, Any]		,			lowercase_ :List[str]		,			lowercase_ :List[Any]		,			lowercase_ :Tuple		,			lowercase_ :Optional[Any]		,			lowercase_ :List[Any]		,			lowercase_ :Optional[Any]		,			lowercase_ :Any		,			lowercase_ :Any		,			lowercase_ :str		,			)->				List[str]:
     A__							=     self.num_choices
     A__							=     FlaubertForMultipleChoice(config=lowercase_				)
     model.to(lowercase_				)
     model.eval()
     A__							=     input_ids.unsqueeze(1				).expand(-1		,			self.num_choices		,			-1				).contiguous()
     A__							=     token_type_ids.unsqueeze(1				).expand(-1		,			self.num_choices		,			-1				).contiguous()
     A__							=     input_mask.unsqueeze(1				).expand(-1		,			self.num_choices		,			-1				).contiguous()
     A__							=     model(
         lowercase_		,			attention_mask=lowercase_		,			token_type_ids=lowercase_		,			labels=lowercase_		,			)
     self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_choices)				)



 def 	UpperCAmelCase_				(							self :Optional[int]				)->				List[str]:
     A__							=     self.prepare_config_and_inputs()
     (
         (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      (
         A__
     ),      
     )							=     config_and_inputs
     A__							=     {
         "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      (			UpperCamelCase__     ,			UpperCamelCase__     ,			unittest.TestCase							):
 __lowercase  						=							(
     (
         FlaubertModel,
         FlaubertWithLMHeadModel,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __lowercase  						=							(
     {
         """feature-extraction""": FlaubertModel,
         """fill-mask""": FlaubertWithLMHeadModel,
         """question-answering""": FlaubertForQuestionAnsweringSimple,
         """text-classification""": FlaubertForSequenceClassification,
         """token-classification""": FlaubertForTokenClassification,
         """zero-shot""": FlaubertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def 	UpperCAmelCase_				(							self :Optional[int]		,			lowercase_ :Optional[int]		,			lowercase_ :Optional[Any]		,			lowercase_ :Optional[int]		,			lowercase_ :str		,			lowercase_ :str				)->				Dict:
     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 	UpperCAmelCase_				(							self :Dict		,			lowercase_ :int		,			lowercase_ :str		,			lowercase_ :str=False				)->				Union[str, Any]:
     A__							=     super()._prepare_for_class(lowercase_		,			lowercase_		,			return_labels=lowercase_				)

     if return_labels:
         if model_class.__name__ == "FlaubertForQuestionAnswering":
             A__							=     torch.zeros(
                 self.model_tester.batch_size		,			dtype=torch.long		,			device=lowercase_				)
             A__							=     torch.zeros(
                 self.model_tester.batch_size		,			dtype=torch.long		,			device=lowercase_				)

     return inputs_dict
 def 	UpperCAmelCase_				(							self :List[Any]				)->				Dict:
     A__							=     FlaubertModelTester(self				)
     A__							=     ConfigTester(self		,			config_class=lowercase_		,			emb_dim=37				)
 def 	UpperCAmelCase_				(							self :Optional[int]				)->				Dict:
     self.config_tester.run_common_tests()
 def 	UpperCAmelCase_				(							self :str				)->				List[Any]:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*lowercase_				)
 def 	UpperCAmelCase_				(							self :int				)->				str:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*lowercase_				)
 def 	UpperCAmelCase_				(							self :List[str]				)->				int:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_simple_qa(*lowercase_				)
 def 	UpperCAmelCase_				(							self :str				)->				Dict:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*lowercase_				)
 def 	UpperCAmelCase_				(							self :Dict				)->				Optional[int]:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase_				)
 def 	UpperCAmelCase_				(							self :List[Any]				)->				str:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_token_classif(*lowercase_				)
 def 	UpperCAmelCase_				(							self :List[str]				)->				Optional[int]:
     A__							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase_				)
 @slow
 def 	UpperCAmelCase_				(							self :Optional[int]				)->				Tuple:
     for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         A__							=     FlaubertModel.from_pretrained(lowercase_				)
         self.assertIsNotNone(lowercase_				)



 @slow
 @require_torch_gpu
 def 	UpperCAmelCase_				(							self :List[str]				)->				Optional[int]:
     A__,      A__							=     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

         A__							=     True
         A__							=     model_class(config=lowercase_				)

         A__							=     self._prepare_for_class(lowercase_		,			lowercase_				)
         A__							=     torch.jit.trace(
             lowercase_		,			(inputs_dict["input_ids"].to("cpu"				), inputs_dict["attention_mask"].to("cpu"				))				)

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






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 @slow
 def 	UpperCAmelCase_				(							self :List[str]				)->				Dict:
     A__							=     FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"				)
     A__							=     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]				)
     with torch.no_grad():
         A__							=     model(lowercase_				)[0]
     A__							=     torch.Size((1, 11, 7_68)				)
     self.assertEqual(output.shape		,			lowercase_				)
     A__							=     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]		,			lowercase_		,			atol=1E-4				)				)










'''simple docstring'''

import time
import unittest

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

from ..test_modeling_common import ids_tensor


if is_torch_available():
  import torch

  from transformers.generation import (
      MaxLengthCriteria,
      MaxNewTokensCriteria,
      MaxTimeCriteria,
      StoppingCriteriaList,
      validate_stopping_criteria,
  )






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :str				)->				Any:
     A__							=     3
     A__							=     2_50

     A__							=     ids_tensor((batch_size, length)		,			lowercase_				)
     A__							=     torch.ones((batch_size, length)		,			device=lowercase_		,			dtype=torch.float				) / length
     return input_ids, scores
 def 	UpperCAmelCase_				(							self :str				)->				Dict:
     A__,      A__							=     self._get_tensors(5				)

     A__							=     StoppingCriteriaList(
         [
             MaxLengthCriteria(max_length=10				),
             MaxTimeCriteria(max_time=0.1				),
         ]				)

     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(9				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(10				)
     self.assertTrue(criteria(lowercase_		,			lowercase_				)				)
 def 	UpperCAmelCase_				(							self :List[Any]				)->				List[Any]:
     A__							=     MaxLengthCriteria(max_length=10				)

     A__,      A__							=     self._get_tensors(5				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(9				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(10				)
     self.assertTrue(criteria(lowercase_		,			lowercase_				)				)
 def 	UpperCAmelCase_				(							self :Tuple				)->				int:
     A__							=     MaxNewTokensCriteria(start_length=5		,			max_new_tokens=5				)

     A__,      A__							=     self._get_tensors(5				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(9				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__,      A__							=     self._get_tensors(10				)
     self.assertTrue(criteria(lowercase_		,			lowercase_				)				)

     A__							=     StoppingCriteriaList([criteria]				)
     self.assertEqual(criteria_list.max_length		,			10				)
 def 	UpperCAmelCase_				(							self :Dict				)->				int:
     A__,      A__							=     self._get_tensors(5				)

     A__							=     MaxTimeCriteria(max_time=0.1				)
     self.assertFalse(criteria(lowercase_		,			lowercase_				)				)

     A__							=     MaxTimeCriteria(max_time=0.1		,			initial_timestamp=time.time() - 0.2				)
     self.assertTrue(criteria(lowercase_		,			lowercase_				)				)



 def 	UpperCAmelCase_				(							self :Union[str, Any]				)->				str:
     validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10				)]				)		,			10				)

     with self.assertWarns(lowercase_				):
         validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10				)]				)		,			11				)

     A__							=     validate_stopping_criteria(StoppingCriteriaList()		,			11				)

     self.assertEqual(len(lowercase_				)		,			1				)



