




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







 '''simple docstring'''





 def __init__(				self:		str,      a_:		Optional[Any],      a_:		Tuple=13,      a_:		str=7,      a_:		Optional[int]=True,      a_:		int=True,      a_:		Dict=True,      a_:		int=True,      a_:		List[str]=True,      a_:		Tuple=False,      a_:		str=False,      a_:		Optional[Any]=False,      a_:		int=2,      a_:		List[str]=99,      a_:		Tuple=0,      a_:		Optional[Any]=32,      a_:		Optional[Any]=5,      a_:		List[Any]=4,      a_:		List[Any]=0.1,      a_:		List[Any]=0.1,      a_:		List[Any]=512,      a_:		int=12,      a_:		Union[str, Any]=2,      a_:		Any=0.02,      a_:		Union[str, Any]=3,      a_:		Dict=4,      a_:		Dict="last",      a_:		Union[str, Any]=None,      a_:		Optional[Any]=None,      ):




   '''simple docstring'''



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


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  ids_tensor([self.batch_size, self.seq_length],      self.vocab_size		)
   _snake_case							:		List[str]								=  random_attention_mask([self.batch_size, self.seq_length]		)

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

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

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

   _snake_case							:		List[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  UpperCamelCase_   (				self:		int		):




   '''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  UpperCamelCase_   (				self:		List[str],      a_:		List[Any],      a_:		Optional[int],      a_:		Optional[Any],      a_:		str,      a_:		Dict,      a_:		Optional[Any],      a_:		Optional[int],      a_:		Any,      a_:		int,      ):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  FlaubertModel(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		Tuple								=  model(a_,      lengths=a_,      langs=a_		)
   _snake_case							:		Optional[Any]								=  model(a_,      langs=a_		)
   _snake_case							:		Dict								=  model(a_		)
   self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size)		)


 def  UpperCamelCase_   (				self:		List[Any],      a_:		int,      a_:		Any,      a_:		Tuple,      a_:		List[str],      a_:		Optional[int],      a_:		Dict,      a_:		List[str],      a_:		Any,      a_:		Optional[int],      ):




   '''simple docstring'''



   _snake_case							:		Dict								=  FlaubertWithLMHeadModel(a_		)
   model.to(a_		)
   model.eval()

   _snake_case							:		Optional[int]								=  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  UpperCamelCase_   (				self:		List[str],      a_:		int,      a_:		str,      a_:		List[str],      a_:		Optional[Any],      a_:		Dict,      a_:		Tuple,      a_:		List[str],      a_:		Dict,      a_:		List[Any],      ):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  FlaubertForQuestionAnsweringSimple(a_		)
   model.to(a_		)
   model.eval()

   _snake_case							:		Tuple								=  model(a_		)

   _snake_case							:		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  UpperCamelCase_   (				self:		Dict,      a_:		str,      a_:		List[str],      a_:		List[str],      a_:		List[str],      a_:		List[str],      a_:		int,      a_:		Tuple,      a_:		str,      a_:		List[Any],      ):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  FlaubertForQuestionAnswering(a_		)
   model.to(a_		)
   model.eval()

   _snake_case							:		Optional[Any]								=  model(a_		)

   _snake_case							:		List[Any]								=  model(
       a_,      start_positions=a_,      end_positions=a_,      cls_index=a_,      is_impossible=a_,      p_mask=a_,      )

   _snake_case							:		Optional[Any]								=  model(
       a_,      start_positions=a_,      end_positions=a_,      cls_index=a_,      is_impossible=a_,      )

   (_snake_case   )							:		Union[str, Any]								=  result_with_labels.to_tuple()

   _snake_case							:		Any								=  model(a_,      start_positions=a_,      end_positions=a_		)

   (_snake_case   )							:		Tuple								=  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:		Tuple,      a_:		Dict,      a_:		Tuple,      a_:		Union[str, Any],      a_:		Tuple,      a_:		Any,      a_:		Optional[Any],      a_:		List[Any],      a_:		List[Any],      a_:		Dict,      ):




   '''simple docstring'''



   _snake_case							:		int								=  FlaubertForSequenceClassification(a_		)
   model.to(a_		)
   model.eval()

   _snake_case							:		Any								=  model(a_		)
   _snake_case							:		str								=  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  UpperCamelCase_   (				self:		List[Any],      a_:		Tuple,      a_:		List[str],      a_:		Union[str, Any],      a_:		str,      a_:		Optional[Any],      a_:		List[Any],      a_:		Dict,      a_:		Optional[Any],      a_:		Optional[int],      ):




   '''simple docstring'''



   _snake_case							:		Any								=  self.num_labels
   _snake_case							:		Tuple								=  FlaubertForTokenClassification(a_		)
   model.to(a_		)
   model.eval()

   _snake_case							:		str								=  model(a_,      attention_mask=a_,      labels=a_		)
   self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.num_labels)		)


 def  UpperCamelCase_   (				self:		Union[str, Any],      a_:		int,      a_:		List[str],      a_:		Optional[Any],      a_:		Tuple,      a_:		Optional[int],      a_:		Union[str, Any],      a_:		Dict,      a_:		Tuple,      a_:		List[str],      ):




   '''simple docstring'''



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


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  self.prepare_config_and_inputs()
   (
       _snake_case   
   )							:		Union[str, Any]								=  config_and_inputs
   _snake_case							:		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 					lowercase(     __lowerCamelCase       ,			__lowerCamelCase       ,			unittest.TestCase ):







 '''simple docstring'''





 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:		str,      a_:		Union[str, Any],      a_:		List[str],      a_:		List[Any],      a_:		str,      a_:		Optional[Any]		):




   '''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  UpperCamelCase_   (				self:		List[str],      a_:		Optional[Any],      a_:		int,      a_:		Any=False		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  super()._prepare_for_class(a_,      a_,      return_labels=a_		)

   if return_labels:
     if model_class.__name__ == "FlaubertForQuestionAnswering":
       _snake_case							:		Tuple								=  torch.zeros(
           self.model_tester.batch_size,      dtype=torch.long,      device=a_		)
       _snake_case							:		str								=  torch.zeros(
           self.model_tester.batch_size,      dtype=torch.long,      device=a_		)

   return inputs_dict


 def  UpperCamelCase_   (				self:		Union[str, Any]		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  FlaubertModelTester(self		)
   _snake_case							:		Any								=  ConfigTester(self,      config_class=a_,      emb_dim=37		)


 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   self.config_tester.run_common_tests()


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		List[Any]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_model(*a_		)


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		int								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_lm_head(*a_		)


 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_simple_qa(*a_		)


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_qa(*a_		)


 def  UpperCamelCase_   (				self:		Any		):




   '''simple docstring'''



   _snake_case							:		Any								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_sequence_classif(*a_		)


 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		int								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_token_classif(*a_		)


 def  UpperCamelCase_   (				self:		Union[str, Any]		):




   '''simple docstring'''



   _snake_case							:		int								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_flaubert_multiple_choice(*a_		)


 @slow
 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		Any								=  FlaubertModel.from_pretrained(a_		)
     self.assertIsNotNone(a_		)


 @slow
 @require_torch_gpu
 def  UpperCamelCase_   (				self:		int		):




   '''simple docstring'''



   _snake_case							:		Dict								=  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

     _snake_case							:		Any								=  True
     _snake_case							:		str								=  model_class(config=a_		)

     _snake_case							:		List[str]								=  self._prepare_for_class(a_,      a_		)
     _snake_case							:		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"""		)		)
       _snake_case							:		List[Any]								=  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 					lowercase(     unittest.TestCase ):







 '''simple docstring'''





 @slow
 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		int								=  FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""		)
   _snake_case							:		List[str]								=  torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
   with torch.no_grad():
     _snake_case							:		int								=  model(a_		)[0]
   _snake_case							:		Union[str, Any]								=  torch.Size((1, 11, 768)		)
   self.assertEqual(output.shape,      a_		)
   _snake_case							:		Optional[int]								=  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		)		)









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__UpperCAmelCase       :   Union[str, Any]				       =							{}

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

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

else:
      import sys

      __UpperCAmelCase       :   Optional[Any]				       =							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






