



'''simple docstring'''


import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch

  from transformers import (
      FlaubertForMultipleChoice,
      FlaubertForQuestionAnswering,
      FlaubertForQuestionAnsweringSimple,
      FlaubertForSequenceClassification,
      FlaubertForTokenClassification,
      FlaubertModel,
      FlaubertWithLMHeadModel,
  )
  from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST







class 	UpperCAmelCase	(		_lowerCamelCase):


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_							,  lowerCAmelCase_=1_3							,  lowerCAmelCase_=7							,  lowerCAmelCase_=True							,  lowerCAmelCase_=True							,  lowerCAmelCase_=True							,  lowerCAmelCase_=True							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=2							,  lowerCAmelCase_=9_9							,  lowerCAmelCase_=0							,  lowerCAmelCase_=3_2							,  lowerCAmelCase_=5							,  lowerCAmelCase_=4							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_=5_1_2							,  lowerCAmelCase_=1_2							,  lowerCAmelCase_=2							,  lowerCAmelCase_=0.0_2							,  lowerCAmelCase_=3							,  lowerCAmelCase_=4							,  lowerCAmelCase_="last"							,  lowerCAmelCase_=None							,  lowerCAmelCase_=None							,  ) ->      Dict:



      """simple docstring"""







      a_       		=parent
      a_       		=batch_size
      a_       		=seq_length
      a_       		=is_training
      a_       		=use_input_lengths
      a_       		=use_token_type_ids
      a_       		=use_labels
      a_       		=gelu_activation
      a_       		=sinusoidal_embeddings
      a_       		=causal
      a_       		=asm
      a_       		=n_langs
      a_       		=vocab_size
      a_       		=n_special
      a_       		=hidden_size
      a_       		=num_hidden_layers
      a_       		=num_attention_heads
      a_       		=hidden_dropout_prob
      a_       		=attention_probs_dropout_prob
      a_       		=max_position_embeddings
      a_       		=type_vocab_size
      a_       		=type_sequence_label_size
      a_       		=initializer_range
      a_       		=num_labels
      a_       		=num_choices
      a_       		=summary_type
      a_       		=use_proj
      a_       		=scope




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size)
      a_       		=random_attention_mask([self.batch_size, self.seq_length])

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

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

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

      a_       		=self.get_config()

      return (
          config,
          input_ids,
          token_type_ids,
          input_lengths,
          sequence_labels,
          token_labels,
          is_impossible_labels,
          choice_labels,
          input_mask,
      )




 def       lowercase_  (   self) ->      Optional[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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      Tuple:



      """simple docstring"""







      a_       		=FlaubertModel(config=A__)
      model.to(A__)
      model.eval()
      a_       		=model(A__							,  lengths=A__							,  langs=A__)
      a_       		=model(A__							,  langs=A__)
      a_       		=model(A__)
      self.parent.assertEqual(result.last_hidden_state.shape							,  (self.batch_size, self.seq_length, self.hidden_size))




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      Dict:



      """simple docstring"""







      a_       		=FlaubertWithLMHeadModel(A__)
      model.to(A__)
      model.eval()

      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      Optional[Any]:



      """simple docstring"""







      a_       		=FlaubertForQuestionAnsweringSimple(A__)
      model.to(A__)
      model.eval()

      a_       		=model(A__)

      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      Optional[int]:



      """simple docstring"""







      a_       		=FlaubertForQuestionAnswering(A__)
      model.to(A__)
      model.eval()

      a_       		=model(A__)

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

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

      ((a_							)		,													)       		=result_with_labels.to_tuple()

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

      ((a_							)		,													)       		=result_with_labels.to_tuple()

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      List[str]:



      """simple docstring"""







      a_       		=FlaubertForSequenceClassification(A__)
      model.to(A__)
      model.eval()

      a_       		=model(A__)
      a_       		=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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      Optional[Any]:



      """simple docstring"""







      a_       		=self.num_labels
      a_       		=FlaubertForTokenClassification(A__)
      model.to(A__)
      model.eval()

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  ) ->      int:



      """simple docstring"""







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




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



      """simple docstring"""







      a_       		=self.prepare_config_and_inputs()
      (
          (
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,						(
          a_							
      )		,													
      )       		=config_and_inputs
      a_       		={
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "lengths": input_lengths,
          "attention_mask": input_mask,
      }
      return config, inputs_dict







@require_torch
class 	UpperCAmelCase	(		_lowerCamelCase					,	_lowerCamelCase					,	unittest.TestCase):


 '''simple docstring'''




 __magic_name__					: Optional[int]  =      (
     (
         FlaubertModel,
         FlaubertWithLMHeadModel,
         FlaubertForQuestionAnswering,
         FlaubertForQuestionAnsweringSimple,
         FlaubertForSequenceClassification,
         FlaubertForTokenClassification,
         FlaubertForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __magic_name__					: Optional[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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      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       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_=False) ->      List[Any]:



      """simple docstring"""







      a_       		=super()._prepare_for_class(A__							,  A__							,  return_labels=A__)

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

      return inputs_dict




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



      """simple docstring"""







      a_       		=FlaubertModelTester(self)
      a_       		=ConfigTester(self							,  config_class=A__							,  emb_dim=3_7)




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



      """simple docstring"""







      self.config_tester.run_common_tests()




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



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_model(*A__)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_lm_head(*A__)




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



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_simple_qa(*A__)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_qa(*A__)




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_sequence_classif(*A__)




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_token_classif(*A__)




 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      a_       		=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_flaubert_multiple_choice(*A__)




 @slow
 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







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




 @slow
 @require_torch_gpu
 def       lowercase_  (   self) ->      Union[str, Any]:



      """simple docstring"""







      a_		,						a_       		=self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes:
           # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
           if model_class == FlaubertForMultipleChoice:
                return

           a_       		=True
           a_       		=model_class(config=A__)

           a_       		=self._prepare_for_class(A__							,  A__)
           a_       		=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"))
                a_       		=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 	UpperCAmelCase	(		unittest.TestCase):


 '''simple docstring'''




 @slow
 def       lowercase_  (   self) ->      Dict:



      """simple docstring"""







      a_       		=FlaubertModel.from_pretrained("flaubert/flaubert_base_cased")
      a_       		=torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]])
      with torch.no_grad():
           a_       		=model(A__)[0]
      a_       		=torch.Size((1, 1_1, 7_6_8))
      self.assertEqual(output.shape							,  A__)
      a_       		=torch.tensor(
          [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]])

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







'''simple docstring'''


import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 	UpperCAmelCase	:


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      return self.get_dummy_input()




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      if self.block_type == "down":
           return (4, 3_2, 1_6, 1_6)
      elif self.block_type == "mid":
           return (4, 3_2, 3_2, 3_2)
      elif self.block_type == "up":
           return (4, 3_2, 6_4, 6_4)

      raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""")




 def       lowercase_  (   self							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  ) ->      Dict:



      """simple docstring"""







      a_       		=4
      a_       		=3_2
      a_       		=(3_2, 3_2)

      a_       		=torch.manual_seed(0)
      a_       		=torch.device(lowerCAmelCase_)
      a_       		=(batch_size, num_channels) + sizes
      a_       		=randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)
      a_       		={"hidden_states": hidden_states}

      if include_temb:
           a_       		=1_2_8
           a_       		=randn_tensor((batch_size, temb_channels)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      if include_res_hidden_states_tuple:
           a_       		=torch.manual_seed(1)
           a_       		=(randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_),)

      if include_encoder_hidden_states:
           a_       		=floats_tensor((batch_size, 3_2, 3_2)).to(lowerCAmelCase_)

      if include_skip_sample:
           a_       		=randn_tensor(((batch_size, 3) + sizes)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      return dummy_input




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



      """simple docstring"""







      a_       		={
          "in_channels": 3_2,
          "out_channels": 3_2,
          "temb_channels": 1_2_8,
      }
      if self.block_type == "up":
           a_       		=3_2

      if self.block_type == "mid":
           init_dict.pop("out_channels")

      a_       		=self.dummy_input
      return init_dict, inputs_dict




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      unet_block.to(lowerCAmelCase_)
      unet_block.eval()

      with torch.no_grad():
           a_       		=unet_block(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      self.assertEqual(output.shape							,  self.output_shape)

      a_       		=output[0, -1, -3:, -3:]
      a_       		=torch.tensor(lowerCAmelCase_).to(lowerCAmelCase_)
      assert torch_all_close(output_slice.flatten()							,  lowerCAmelCase_							,  atol=5e-3)




 @unittest.skipIf(torch_device == "mps"							,  "Training is not supported in mps")
 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      model.to(lowerCAmelCase_)
      model.train()
      a_       		=model(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      a_       		=torch.device(lowerCAmelCase_)
      a_       		=randn_tensor(output.shape							,  device=lowerCAmelCase_)
      a_       		=torch.nn.functional.mse_loss(lowerCAmelCase_							,  lowerCAmelCase_)
      loss.backward()


