

'''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        A_ (				_UpperCAmelCase							):






     '''simple docstring'''



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


     def    UpperCAmelCase_       (		self :     Optional[int]       )						->	int:
      UpperCAmelCase       :	Optional[int]											= ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size       )
      UpperCAmelCase       :	List[Any]											= random_attention_mask([self.batch_size, self.seq_length]       )

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

      UpperCAmelCase       :	List[str]											= None
      if self.use_token_type_ids:
       UpperCAmelCase       :	Dict											= ids_tensor([self.batch_size, self.seq_length]     ,			self.n_langs       )

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

      UpperCAmelCase       :	List[str]											= 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 :     Union[str, Any]       )						->	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    UpperCAmelCase_       (		self :     Optional[int]     ,			lowercase_ :     Dict     ,			lowercase_ :     Any     ,			lowercase_ :     Dict     ,			lowercase_ :     List[Any]     ,			lowercase_ :     List[Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     int     ,			lowercase_ :     Tuple     ,			lowercase_ :     Tuple     ,			)						->	Any:
      UpperCAmelCase       :	Any											= FlaubertModel(config=lowercase_       )
      model.to(lowercase_       )
      model.eval()
      UpperCAmelCase       :	Union[str, Any]											= model(lowercase_     ,			lengths=lowercase_     ,			langs=lowercase_       )
      UpperCAmelCase       :	Dict											= model(lowercase_     ,			langs=lowercase_       )
      UpperCAmelCase       :	List[str]											= model(lowercase_       )
      self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)       )


     def    UpperCAmelCase_       (		self :     Union[str, Any]     ,			lowercase_ :     List[Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     Dict     ,			lowercase_ :     Tuple     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     str     ,			lowercase_ :     Optional[int]     ,			)						->	Tuple:
      UpperCAmelCase       :	int											= FlaubertWithLMHeadModel(lowercase_       )
      model.to(lowercase_       )
      model.eval()

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

      UpperCAmelCase       :	Optional[Any]											= model(lowercase_       )

      UpperCAmelCase       :	int											= 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 :     Optional[Any]     ,			lowercase_ :     List[Any]     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     Any     ,			lowercase_ :     int     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     Any     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     Optional[int]     ,			)						->	Dict:
      UpperCAmelCase       :	Union[str, Any]											= FlaubertForQuestionAnswering(lowercase_       )
      model.to(lowercase_       )
      model.eval()

      UpperCAmelCase       :	Dict											= model(lowercase_       )

      UpperCAmelCase       :	Optional[int]											= model(
          lowercase_     ,			start_positions=lowercase_     ,			end_positions=lowercase_     ,			cls_index=lowercase_     ,			is_impossible=lowercase_     ,			p_mask=lowercase_     ,			)

      UpperCAmelCase       :	Optional[int]											= model(
          lowercase_     ,			start_positions=lowercase_     ,			end_positions=lowercase_     ,			cls_index=lowercase_     ,			is_impossible=lowercase_     ,			)

      ((UpperCAmelCase		)					, 		)       :	List[str]											= result_with_labels.to_tuple()

      UpperCAmelCase       :	Optional[int]											= model(lowercase_     ,			start_positions=lowercase_     ,			end_positions=lowercase_       )

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

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

      UpperCAmelCase       :	Optional[int]											= 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 :     Tuple     ,			lowercase_ :     Tuple     ,			lowercase_ :     Optional[int]     ,			lowercase_ :     int     ,			lowercase_ :     int     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     List[Any]     ,			lowercase_ :     Dict     ,			lowercase_ :     Any     ,			lowercase_ :     List[str]     ,			)						->	Tuple:
      UpperCAmelCase       :	Tuple											= self.num_choices
      UpperCAmelCase       :	Any											= FlaubertForMultipleChoice(config=lowercase_       )
      model.to(lowercase_       )
      model.eval()
      UpperCAmelCase       :	Tuple											= input_ids.unsqueeze(1       ).expand(-1     ,			self.num_choices     ,			-1       ).contiguous()
      UpperCAmelCase       :	List[Any]											= token_type_ids.unsqueeze(1       ).expand(-1     ,			self.num_choices     ,			-1       ).contiguous()
      UpperCAmelCase       :	Any											= input_mask.unsqueeze(1       ).expand(-1     ,			self.num_choices     ,			-1       ).contiguous()
      UpperCAmelCase       :	Union[str, Any]											= 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 :     Any       )						->	List[Any]:
      UpperCAmelCase       :	int											= self.prepare_config_and_inputs()
      (
          (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, (
          UpperCAmelCase		
      )					, 		
      )       :	Any											= config_and_inputs
      UpperCAmelCase       :	Optional[Any]											= {
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'lengths': input_lengths,
          'attention_mask': input_mask,
      }
      return config, inputs_dict




@require_torch
class        A_ (				_UpperCAmelCase       ,   _UpperCAmelCase       ,   unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:	Tuple  		=  (
         (
             FlaubertModel,
             FlaubertWithLMHeadModel,
             FlaubertForQuestionAnswering,
             FlaubertForQuestionAnsweringSimple,
             FlaubertForSequenceClassification,
             FlaubertForTokenClassification,
             FlaubertForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     UpperCAmelCase_						:	List[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    UpperCAmelCase_       (		self :     Union[str, Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     Optional[Any]     ,			lowercase_ :     List[str]     ,			lowercase_ :     Dict     ,			lowercase_ :     int       )						->	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    UpperCAmelCase_       (		self :     Tuple     ,			lowercase_ :     Union[str, Any]     ,			lowercase_ :     str     ,			lowercase_ :     Any=False       )						->	Optional[Any]:
      UpperCAmelCase       :	List[str]											= super()._prepare_for_class(lowercase_     ,			lowercase_     ,			return_labels=lowercase_       )

      if return_labels:
       if model_class.__name__ == "FlaubertForQuestionAnswering":
        UpperCAmelCase       :	List[str]											= torch.zeros(
            self.model_tester.batch_size     ,			dtype=torch.long     ,			device=lowercase_       )
        UpperCAmelCase       :	Tuple											= torch.zeros(
            self.model_tester.batch_size     ,			dtype=torch.long     ,			device=lowercase_       )

      return inputs_dict


     def    UpperCAmelCase_       (		self :     Dict       )						->	List[str]:
      UpperCAmelCase       :	Dict											= FlaubertModelTester(self       )
      UpperCAmelCase       :	Union[str, Any]											= ConfigTester(self     ,			config_class=lowercase_     ,			emb_dim=37       )


     def    UpperCAmelCase_       (		self :     Union[str, Any]       )						->	Tuple:
      self.config_tester.run_common_tests()


     def    UpperCAmelCase_       (		self :     Union[str, Any]       )						->	Tuple:
      UpperCAmelCase       :	str											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*lowercase_       )


     def    UpperCAmelCase_       (		self :     str       )						->	Tuple:
      UpperCAmelCase       :	List[str]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*lowercase_       )


     def    UpperCAmelCase_       (		self :     int       )						->	Optional[int]:
      UpperCAmelCase       :	Tuple											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_simple_qa(*lowercase_       )


     def    UpperCAmelCase_       (		self :     int       )						->	Union[str, Any]:
      UpperCAmelCase       :	Union[str, Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*lowercase_       )


     def    UpperCAmelCase_       (		self :     int       )						->	Any:
      UpperCAmelCase       :	Optional[Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase_       )


     def    UpperCAmelCase_       (		self :     List[str]       )						->	Any:
      UpperCAmelCase       :	Any											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_token_classif(*lowercase_       )


     def    UpperCAmelCase_       (		self :     Optional[int]       )						->	Any:
      UpperCAmelCase       :	Optional[Any]											= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase_       )


     @slow
     def    UpperCAmelCase_       (		self :     List[str]       )						->	Tuple:
      for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase       :	Dict											= FlaubertModel.from_pretrained(lowercase_       )
       self.assertIsNotNone(lowercase_       )




     @slow
     @require_torch_gpu
     def    UpperCAmelCase_       (		self :     Union[str, Any]       )						->	List[Any]:
      UpperCAmelCase					, UpperCAmelCase       :	List[str]											= 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

       UpperCAmelCase       :	Optional[int]											= True
       UpperCAmelCase       :	Tuple											= model_class(config=lowercase_       )

       UpperCAmelCase       :	Union[str, Any]											= self._prepare_for_class(lowercase_     ,			lowercase_       )
       UpperCAmelCase       :	int											= 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'       )       )
        UpperCAmelCase       :	List[str]											= 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        A_ (				unittest.TestCase							):






     '''simple docstring'''



     @slow
     def    UpperCAmelCase_       (		self :     Union[str, Any]       )						->	Optional[int]:
      UpperCAmelCase       :	Optional[int]											= FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'       )
      UpperCAmelCase       :	List[str]											= torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
      with torch.no_grad():
       UpperCAmelCase       :	List[str]											= model(lowercase_       )[0]
      UpperCAmelCase       :	Union[str, Any]											= torch.Size((1, 11, 768)       )
      self.assertEqual(output.shape     ,			lowercase_       )
      UpperCAmelCase       :	Union[str, Any]											= torch.tensor(
          [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]       )

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











"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


SCREAMING_SNAKE_CASE__					=   {}

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

if TYPE_CHECKING:
       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_bartpho import BartphoTokenizer

else:
       import sys

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






