


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







   def __init__(	self						,	UpperCamelCase__						,	UpperCamelCase__=13						,	UpperCamelCase__=7						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=False						,	UpperCamelCase__=False						,	UpperCamelCase__=False						,	UpperCamelCase__=2						,	UpperCamelCase__=99						,	UpperCamelCase__=0						,	UpperCamelCase__=32						,	UpperCamelCase__=5						,	UpperCamelCase__=4						,	UpperCamelCase__=0.1						,	UpperCamelCase__=0.1						,	UpperCamelCase__=512						,	UpperCamelCase__=12						,	UpperCamelCase__=2						,	UpperCamelCase__=0.02						,	UpperCamelCase__=3						,	UpperCamelCase__=4						,	UpperCamelCase__="last"						,	UpperCamelCase__=None						,	UpperCamelCase__=None						,	)	->		List[Any]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Any      					=    ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size						)
       snake_case    :      Union[str, Any]      					=    random_attention_mask([self.batch_size, self.seq_length]						)

       snake_case    :      Union[str, Any]      					=    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    :      Dict      					=    None
       if self.use_token_type_ids:
           snake_case    :      int      					=    ids_tensor([self.batch_size, self.seq_length]						,	self.n_langs						)

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

       snake_case    :      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  lowerCamelCase		(	self						)	->		Any:

       '''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  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Optional[int]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Union[str, Any]:

       '''simple docstring'''




       snake_case    :      Any      					=    FlaubertWithLMHeadModel(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case    :      Optional[Any]      					=    model(UpperCamelCase__						,	token_type_ids=UpperCamelCase__						,	labels=UpperCamelCase__						)
       self.parent.assertEqual(result.loss.shape						,	()						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		str:

       '''simple docstring'''




       snake_case    :      List[str]      					=    FlaubertForQuestionAnsweringSimple(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case    :      Union[str, Any]      					=    model(UpperCamelCase__						)

       snake_case    :      Tuple      					=    model(UpperCamelCase__						,	start_positions=UpperCamelCase__						,	end_positions=UpperCamelCase__						)
       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  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Any:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    FlaubertForQuestionAnswering(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case    :      Union[str, Any]      					=    model(UpperCamelCase__						)

       snake_case    :      Dict      					=    model(
           UpperCamelCase__						,	start_positions=UpperCamelCase__						,	end_positions=UpperCamelCase__						,	cls_index=UpperCamelCase__						,	is_impossible=UpperCamelCase__						,	p_mask=UpperCamelCase__						,	)

       snake_case    :      List[Any]      					=    model(
           UpperCamelCase__						,	start_positions=UpperCamelCase__						,	end_positions=UpperCamelCase__						,	cls_index=UpperCamelCase__						,	is_impossible=UpperCamelCase__						,	)

       ((snake_case)					,)    :      Optional[int]      					=    result_with_labels.to_tuple()

       snake_case    :      Optional[int]      					=    model(UpperCamelCase__						,	start_positions=UpperCamelCase__						,	end_positions=UpperCamelCase__						)

       ((snake_case)					,)    :      int      					=    result_with_labels.to_tuple()

       self.parent.assertEqual(result_with_labels.loss.shape						,	()						)
       self.parent.assertEqual(result.start_top_log_probs.shape						,	(self.batch_size, model.config.start_n_top)						)
       self.parent.assertEqual(result.start_top_index.shape						,	(self.batch_size, model.config.start_n_top)						)
       self.parent.assertEqual(
           result.end_top_log_probs.shape						,	(self.batch_size, model.config.start_n_top * model.config.end_n_top)						)
       self.parent.assertEqual(
           result.end_top_index.shape						,	(self.batch_size, model.config.start_n_top * model.config.end_n_top)						)
       self.parent.assertEqual(result.cls_logits.shape						,	(self.batch_size,)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Dict:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    FlaubertForSequenceClassification(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case    :      Tuple      					=    model(UpperCamelCase__						)
       snake_case    :      Any      					=    model(UpperCamelCase__						,	labels=UpperCamelCase__						)

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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Tuple:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    self.num_labels
       snake_case    :      List[str]      					=    FlaubertForTokenClassification(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       snake_case    :      Dict      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	labels=UpperCamelCase__						)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.num_labels)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	)	->		Optional[Any]:

       '''simple docstring'''




       snake_case    :      List[str]      					=    self.num_choices
       snake_case    :      str      					=    FlaubertForMultipleChoice(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       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    :      Optional[Any]      					=    input_mask.unsqueeze(1						).expand(-1						,	self.num_choices						,	-1						).contiguous()
       snake_case    :      List[Any]      					=    model(
           UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	token_type_ids=UpperCamelCase__						,	labels=UpperCamelCase__						,	)
       self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_choices)						)







   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       snake_case    :      Dict      					=    self.prepare_config_and_inputs()
       (
           (
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,(
           snake_case
       )					,
       )    :      Union[str, Any]      					=    config_and_inputs
       snake_case    :      int      					=    {
           "input_ids": input_ids,
           "token_type_ids": token_type_ids,
           "lengths": input_lengths,
           "attention_mask": input_mask,
       }
       return config, inputs_dict







@require_torch
class _lowerCAmelCase				(      snake_case_    ,							snake_case_    ,							unittest.TestCase	):
   __UpperCAmelCase :       List[Any]							  =  (
       (
           FlaubertModel,
           FlaubertWithLMHeadModel,
           FlaubertForQuestionAnswering,
           FlaubertForQuestionAnsweringSimple,
           FlaubertForSequenceClassification,
           FlaubertForTokenClassification,
           FlaubertForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   __UpperCAmelCase :       List[str]							  =  (
       {
           '''feature-extraction''': FlaubertModel,
           '''fill-mask''': FlaubertWithLMHeadModel,
           '''question-answering''': FlaubertForQuestionAnsweringSimple,
           '''text-classification''': FlaubertForSequenceClassification,
           '''token-classification''': FlaubertForTokenClassification,
           '''zero-shot''': FlaubertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		int:

       '''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  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__=False						)	->		Optional[int]:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    super()._prepare_for_class(UpperCamelCase__						,	UpperCamelCase__						,	return_labels=UpperCamelCase__						)

       if return_labels:
           if model_class.__name__ == "FlaubertForQuestionAnswering":
               snake_case    :      int      					=    torch.zeros(
                   self.model_tester.batch_size						,	dtype=torch.long						,	device=UpperCamelCase__						)
               snake_case    :      str      					=    torch.zeros(
                   self.model_tester.batch_size						,	dtype=torch.long						,	device=UpperCamelCase__						)

       return inputs_dict







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    FlaubertModelTester(self						)
       snake_case    :      Any      					=    ConfigTester(self						,	config_class=UpperCamelCase__						,	emb_dim=37						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      List[str]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Optional[int]:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_simple_qa(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		List[Any]:

       '''simple docstring'''




       snake_case    :      int      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      List[str]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Optional[Any]:

       '''simple docstring'''




       snake_case    :      str      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_token_classif(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case    :      Tuple      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_multiple_choice(*UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		List[Any]:

       '''simple docstring'''




       for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case    :      List[str]      					=    FlaubertModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







   @slow
   @require_torch_gpu
   def  lowerCamelCase		(	self						)	->		Optional[Any]:

       '''simple docstring'''




       snake_case					,snake_case    :      List[Any]      					=    self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
           # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
           if model_class == FlaubertForMultipleChoice:
               return

           snake_case    :      Union[str, Any]      					=    True
           snake_case    :      List[Any]      					=    model_class(config=UpperCamelCase__						)

           snake_case    :      List[str]      					=    self._prepare_for_class(UpperCamelCase__						,	UpperCamelCase__						)
           snake_case    :      str      					=    torch.jit.trace(
               UpperCamelCase__						,	(inputs_dict["input_ids"].to("cpu"						), inputs_dict["attention_mask"].to("cpu"						))						)

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







@require_torch
class _lowerCAmelCase				(      unittest.TestCase	):







   @slow
   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"						)
       snake_case    :      List[Any]      					=    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
       with torch.no_grad():
           snake_case    :      Optional[int]      					=    model(UpperCamelCase__						)[0]
       snake_case    :      str      					=    torch.Size((1, 11, 768)						)
       self.assertEqual(output.shape						,	UpperCamelCase__						)
       snake_case    :      List[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]						,	UpperCamelCase__						,	atol=1e-4						)						)









"""simple docstring"""



def   __lowerCAmelCase						(						lowercase  :  list )	->						int:


    """simple docstring"""





    if not grid or not grid[0]:
        raise TypeError("The grid does not contain the appropriate information" )

    for cell_n in range(1						,							len(grid[0] ) ):
        grid[0][cell_n] += grid[0][cell_n - 1]
    snake_case    :      int      					=    grid[0]

    for row_n in range(1						,							len(lowercase ) ):
        snake_case    :      Optional[Any]      					=    grid[row_n]
        snake_case    :      List[str]      					=    fill_row(lowercase						,							lowercase )
        snake_case    :      Optional[int]      					=    grid[row_n]

    return grid[-1][-1]







def   __lowerCAmelCase						(						lowercase  :  list						,							lowercase  :  list )	->						list:


    """simple docstring"""





    current_row[0] += row_above[0]
    for cell_n in range(1						,							len(lowercase ) ):
        current_row[cell_n] += min(current_row[cell_n - 1]						,							row_above[cell_n] )

    return current_row


if __name__ == "__main__":
  import doctest

  doctest.testmod()





