






import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
UpperCAmelCase__				   =		{"LayoutLMv2Config", "LayoutLMv3Config"}






@is_pipeline_test
class 		lowercase_					(	unittest.TestCase   ):






      '''simple docstring'''




      __snake_case       =  MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
      __snake_case       =  TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

      if model_mapping is not None:
            __snake_case       =  {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
      if tf_model_mapping is not None:
            __snake_case       =  {
                config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
            }







      def       __lowerCAmelCase					(      self      :						int ,					__UpperCAmelCase      :						Dict ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Optional[Any]  )    ->Optional[int]:




       """simple docstring"""


       a		  =      ZeroShotClassificationPipeline(
           model=__UpperCAmelCase ,					tokenizer=__UpperCAmelCase ,					candidate_labels=['''polics''', '''health''']  )
       return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]







      def       __lowerCAmelCase					(      self      :						Tuple ,					__UpperCAmelCase      :						Any ,					__UpperCAmelCase      :						List[Any]  )    ->int:




       """simple docstring"""


       a		  =      classifier('''Who are you voting for in 2020?''' ,					candidate_labels='''politics'''  )
       self.assertEqual(__UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  )]}  )

       # No kwarg
       a		  =      classifier('''Who are you voting for in 2020?''' ,					['''politics''']  )
       self.assertEqual(__UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  )]}  )

       a		  =      classifier('''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''']  )
       self.assertEqual(__UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  )]}  )

       a		  =      classifier('''Who are you voting for in 2020?''' ,					candidate_labels='''politics, public health'''  )
       self.assertEqual(
           __UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )]}  )
       self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']  )  ) ,					1.0  )

       a		  =      classifier('''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''', '''public health''']  )
       self.assertEqual(
           __UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )]}  )
       self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']  )  ) ,					1.0  )

       a		  =      classifier(
           '''Who are you voting for in 2020?''' ,					candidate_labels='''politics''' ,					hypothesis_template='''This text is about {}'''  )
       self.assertEqual(__UpperCAmelCase ,					{'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  )]}  )

       # https://github.com/huggingface/transformers/issues/13846
       a		  =      classifier(['''I am happy'''] ,					['''positive''', '''negative''']  )
       self.assertEqual(
           __UpperCAmelCase ,					[
               {'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )]}
               for i in range(1  )
           ] ,					)
       a		  =      classifier(['''I am happy''', '''I am sad'''] ,					['''positive''', '''negative''']  )
       self.assertEqual(
           __UpperCAmelCase ,					[
               {'''sequence''': ANY(__UpperCAmelCase  ), '''labels''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )], '''scores''': [ANY(__UpperCAmelCase  ), ANY(__UpperCAmelCase  )]}
               for i in range(2  )
           ] ,					)

       with self.assertRaises(__UpperCAmelCase  ):
        classifier('''''' ,					candidate_labels='''politics'''  )

       with self.assertRaises(__UpperCAmelCase  ):
        classifier(__UpperCAmelCase ,					candidate_labels='''politics'''  )

       with self.assertRaises(__UpperCAmelCase  ):
        classifier('''Who are you voting for in 2020?''' ,					candidate_labels=''''''  )

       with self.assertRaises(__UpperCAmelCase  ):
        classifier('''Who are you voting for in 2020?''' ,					candidate_labels=__UpperCAmelCase  )

       with self.assertRaises(__UpperCAmelCase  ):
        classifier(
            '''Who are you voting for in 2020?''' ,					candidate_labels='''politics''' ,					hypothesis_template='''Not formatting template''' ,					)

       with self.assertRaises(__UpperCAmelCase  ):
        classifier(
            '''Who are you voting for in 2020?''' ,					candidate_labels='''politics''' ,					hypothesis_template=__UpperCAmelCase ,					)

       self.run_entailment_id(__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						int ,					__UpperCAmelCase      :						Pipeline  )    ->int:




       """simple docstring"""


       a		  =      zero_shot_classifier.model.config
       a		  =      config.labelaid
       a		  =      zero_shot_classifier.entailment_id

       a		  =      {'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
       self.assertEqual(zero_shot_classifier.entailment_id ,					-1  )

       a		  =      {'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2}
       self.assertEqual(zero_shot_classifier.entailment_id ,					0  )

       a		  =      {'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
       self.assertEqual(zero_shot_classifier.entailment_id ,					0  )

       a		  =      {'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
       self.assertEqual(zero_shot_classifier.entailment_id ,					2  )

       a		  =      original_labelaid
       self.assertEqual(__UpperCAmelCase ,					zero_shot_classifier.entailment_id  )







      @require_torch
      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->str:




       """simple docstring"""


       a		  =      pipeline(
           '''zero-shot-classification''' ,					model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' ,					framework='''pt''' ,					)
       # There was a regression in 4.10 for this
       # Adding a test so we don't make the mistake again.
       # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
       zero_shot_classifier(
           '''Who are you voting for in 2020?''' * 100 ,					candidate_labels=['''politics''', '''public health''', '''science''']  )







      @require_torch
      def       __lowerCAmelCase					(      self      :						int  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      pipeline(
           '''zero-shot-classification''' ,					model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' ,					framework='''pt''' ,					)
       a		  =      zero_shot_classifier(
           '''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''', '''public health''', '''science''']  )

       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': '''Who are you voting for in 2020?''',
               '''labels''': ['''science''', '''public health''', '''politics'''],
               '''scores''': [0.333, 0.333, 0.333],
           } ,					)







      @require_tf
      def       __lowerCAmelCase					(      self      :						Optional[int]  )    ->Union[str, Any]:




       """simple docstring"""


       a		  =      pipeline(
           '''zero-shot-classification''' ,					model='''sshleifer/tiny-distilbert-base-cased-distilled-squad''' ,					framework='''tf''' ,					)
       a		  =      zero_shot_classifier(
           '''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''', '''public health''', '''science''']  )

       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': '''Who are you voting for in 2020?''',
               '''labels''': ['''science''', '''public health''', '''politics'''],
               '''scores''': [0.333, 0.333, 0.333],
           } ,					)







      @slow
      @require_torch
      def       __lowerCAmelCase					(      self      :						int  )    ->List[Any]:




       """simple docstring"""


       a		  =      pipeline('''zero-shot-classification''' ,					model='''roberta-large-mnli''' ,					framework='''pt'''  )
       a		  =      zero_shot_classifier(
           '''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''', '''public health''', '''science''']  )

       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': '''Who are you voting for in 2020?''',
               '''labels''': ['''politics''', '''public health''', '''science'''],
               '''scores''': [0.976, 0.015, 0.009],
           } ,					)
       a		  =      zero_shot_classifier(
           '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
           ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
           ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
           ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
           ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
           ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
           ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
           ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
           ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
           ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
           ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
           ''' large and limited training data.''' ,					candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''] ,					multi_label=__UpperCAmelCase ,					)
       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': (
                   '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                   ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                   ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                   ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                   ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                   ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                   ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                   ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                   ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                   ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                   ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                   ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                   ''' limited training data.'''
               ),
               '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
               '''scores''': [0.817, 0.713, 0.018, 0.018],
           } ,					)







      @slow
      @require_tf
      def       __lowerCAmelCase					(      self      :						Optional[int]  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      pipeline('''zero-shot-classification''' ,					model='''roberta-large-mnli''' ,					framework='''tf'''  )
       a		  =      zero_shot_classifier(
           '''Who are you voting for in 2020?''' ,					candidate_labels=['''politics''', '''public health''', '''science''']  )

       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': '''Who are you voting for in 2020?''',
               '''labels''': ['''politics''', '''public health''', '''science'''],
               '''scores''': [0.976, 0.015, 0.009],
           } ,					)
       a		  =      zero_shot_classifier(
           '''The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'''
           ''' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'''
           ''' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'''
           ''' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'''
           ''' machine translation tasks show these models to be superior in quality while being more parallelizable'''
           ''' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'''
           ''' English-to-German translation task, improving over the existing best results, including ensembles by'''
           ''' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'''
           ''' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'''
           ''' fraction of the training costs of the best models from the literature. We show that the Transformer'''
           ''' generalizes well to other tasks by applying it successfully to English constituency parsing both with'''
           ''' large and limited training data.''' ,					candidate_labels=['''machine learning''', '''statistics''', '''translation''', '''vision'''] ,					multi_label=__UpperCAmelCase ,					)
       self.assertEqual(
           nested_simplify(__UpperCAmelCase  ) ,					{
               '''sequence''': (
                   '''The dominant sequence transduction models are based on complex recurrent or convolutional neural'''
                   ''' networks in an encoder-decoder configuration. The best performing models also connect the'''
                   ''' encoder and decoder through an attention mechanism. We propose a new simple network'''
                   ''' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'''
                   ''' and convolutions entirely. Experiments on two machine translation tasks show these models to be'''
                   ''' superior in quality while being more parallelizable and requiring significantly less time to'''
                   ''' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'''
                   ''' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'''
                   ''' English-to-French translation task, our model establishes a new single-model state-of-the-art'''
                   ''' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'''
                   ''' costs of the best models from the literature. We show that the Transformer generalizes well to'''
                   ''' other tasks by applying it successfully to English constituency parsing both with large and'''
                   ''' limited training data.'''
               ),
               '''labels''': ['''translation''', '''machine learning''', '''vision''', '''statistics'''],
               '''scores''': [0.817, 0.713, 0.018, 0.018],
           } ,					)









"""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 		lowercase					(					_UpperCAmelCase					):





      def __init__(       self      ,						lowercase      ,						lowercase=13      ,						lowercase=7      ,						lowercase=True      ,						lowercase=True      ,						lowercase=True      ,						lowercase=True      ,						lowercase=True      ,						lowercase=False      ,						lowercase=False      ,						lowercase=False      ,						lowercase=2      ,						lowercase=99      ,						lowercase=0      ,						lowercase=32      ,						lowercase=5      ,						lowercase=4      ,						lowercase=0.1      ,						lowercase=0.1      ,						lowercase=512      ,						lowercase=12      ,						lowercase=2      ,						lowercase=0.02      ,						lowercase=3      ,						lowercase=4      ,						lowercase="last"      ,						lowercase=None      ,						lowercase=None      ,						)		->     int:
            lowerCAmelCase  						=       parent
            lowerCAmelCase  						=       batch_size
            lowerCAmelCase  						=       seq_length
            lowerCAmelCase  						=       is_training
            lowerCAmelCase  						=       use_input_lengths
            lowerCAmelCase  						=       use_token_type_ids
            lowerCAmelCase  						=       use_labels
            lowerCAmelCase  						=       gelu_activation
            lowerCAmelCase  						=       sinusoidal_embeddings
            lowerCAmelCase  						=       causal
            lowerCAmelCase  						=       asm
            lowerCAmelCase  						=       n_langs
            lowerCAmelCase  						=       vocab_size
            lowerCAmelCase  						=       n_special
            lowerCAmelCase  						=       hidden_size
            lowerCAmelCase  						=       num_hidden_layers
            lowerCAmelCase  						=       num_attention_heads
            lowerCAmelCase  						=       hidden_dropout_prob
            lowerCAmelCase  						=       attention_probs_dropout_prob
            lowerCAmelCase  						=       max_position_embeddings
            lowerCAmelCase  						=       type_vocab_size
            lowerCAmelCase  						=       type_sequence_label_size
            lowerCAmelCase  						=       initializer_range
            lowerCAmelCase  						=       num_labels
            lowerCAmelCase  						=       num_choices
            lowerCAmelCase  						=       summary_type
            lowerCAmelCase  						=       use_proj
            lowerCAmelCase  						=       scope





      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size )
            lowerCAmelCase  						=       random_attention_mask([self.batch_size, self.seq_length] )

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

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

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

            lowerCAmelCase  						=       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 )		->     List[Any]:
            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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Any:
            lowerCAmelCase  						=       FlaubertModel(config=lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       model(lowercase      ,						lengths=lowercase      ,						langs=lowercase )
            lowerCAmelCase  						=       model(lowercase      ,						langs=lowercase )
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Tuple:
            lowerCAmelCase  						=       FlaubertWithLMHeadModel(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     str:
            lowerCAmelCase  						=       FlaubertForQuestionAnsweringSimple(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       model(lowercase )

            lowerCAmelCase  						=       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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Dict:
            lowerCAmelCase  						=       FlaubertForQuestionAnswering(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       model(lowercase )

            lowerCAmelCase  						=       model(
                lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase      ,						cls_index=lowercase      ,						is_impossible=lowercase      ,						p_mask=lowercase      ,						)

            lowerCAmelCase  						=       model(
                lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase      ,						cls_index=lowercase      ,						is_impossible=lowercase      ,						)

            ((lowerCAmelCase)						,		)  						=       result_with_labels.to_tuple()

            lowerCAmelCase  						=       model(lowercase      ,						start_positions=lowercase      ,						end_positions=lowercase )

            ((lowerCAmelCase)						,		)  						=       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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     int:
            lowerCAmelCase  						=       FlaubertForSequenceClassification(lowercase )
            model.to(lowercase )
            model.eval()

            lowerCAmelCase  						=       model(lowercase )
            lowerCAmelCase  						=       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     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     int:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       FlaubertForTokenClassification(lowercase )
            model.to(lowercase )
            model.eval()

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





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						)		->     Tuple:
            lowerCAmelCase  						=       self.num_choices
            lowerCAmelCase  						=       FlaubertForMultipleChoice(config=lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       input_ids.unsqueeze(1 ).expand(-1      ,						self.num_choices      ,						-1 ).contiguous()
            lowerCAmelCase  						=       token_type_ids.unsqueeze(1 ).expand(-1      ,						self.num_choices      ,						-1 ).contiguous()
            lowerCAmelCase  						=       input_mask.unsqueeze(1 ).expand(-1      ,						self.num_choices      ,						-1 ).contiguous()
            lowerCAmelCase  						=       model(
                lowercase      ,						attention_mask=lowercase      ,						token_type_ids=lowercase      ,						labels=lowercase      ,						)
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_choices) )





      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       self.prepare_config_and_inputs()
            (
                (
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		(
                lowerCAmelCase
            )						,		
            )  						=       config_and_inputs
            lowerCAmelCase  						=       {
                """input_ids""": input_ids,
                """token_type_ids""": token_type_ids,
                """lengths""": input_lengths,
                """attention_mask""": input_mask,
            }
            return config, inputs_dict







@require_torch
class 		lowercase					(					_UpperCAmelCase     ,  _UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _SCREAMING_SNAKE_CASE					=      (
          {
              '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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Optional[int]:
            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      ,						lowercase      ,						lowercase      ,						lowercase=False )		->     Optional[Any]:
            lowerCAmelCase  						=       super()._prepare_for_class(lowercase      ,						lowercase      ,						return_labels=lowercase )

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

            return inputs_dict





      def     _snake_case			(       self )		->     List[str]:
            lowerCAmelCase  						=       FlaubertModelTester(self )
            lowerCAmelCase  						=       ConfigTester(self      ,						config_class=lowercase      ,						emb_dim=37 )





      def     _snake_case			(       self )		->     Tuple:
            self.config_tester.run_common_tests()





      def     _snake_case			(       self )		->     Tuple:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_model(*lowercase )





      def     _snake_case			(       self )		->     Tuple:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_lm_head(*lowercase )





      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_simple_qa(*lowercase )





      def     _snake_case			(       self )		->     Union[str, Any]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_qa(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_token_classif(*lowercase )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase )





      @slow
      def     _snake_case			(       self )		->     Tuple:
            for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase  						=       FlaubertModel.from_pretrained(lowercase )
                  self.assertIsNotNone(lowercase )





      @slow
      @require_torch_gpu
      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase						,		lowerCAmelCase  						=       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  						=       True
                  lowerCAmelCase  						=       model_class(config=lowercase )

                  lowerCAmelCase  						=       self._prepare_for_class(lowercase      ,						lowercase )
                  lowerCAmelCase  						=       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""" ) )
                        lowerCAmelCase  						=       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 		lowercase					(					unittest.TestCase					):





      @slow
      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased""" )
            lowerCAmelCase  						=       torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] )
            with torch.no_grad():
                  lowerCAmelCase  						=       model(lowercase )[0]
            lowerCAmelCase  						=       torch.Size((1, 11, 768) )
            self.assertEqual(output.shape      ,						lowercase )
            lowerCAmelCase  						=       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]      ,						lowercase      ,						atol=1e-4 ) )






