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 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






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


          """simple docstring"""


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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->str:


          """simple docstring"""


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

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

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

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

          _lowerCamelCase     :		Union[str, 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 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->Union[str, 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 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[Any]    ,  )				->List[str]:


          """simple docstring"""


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



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Tuple    ,  )				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						FlaubertWithLMHeadModel(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		str 						=						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 	_SCREAMING_SNAKE_CASE    (					self     :				List[str]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Any    ,  )				->str:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						FlaubertForQuestionAnsweringSimple(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		Optional[int] 						=						model(_UpperCamelCase)

          _lowerCamelCase     :		int 						=						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 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  )				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						FlaubertForQuestionAnswering(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		str 						=						model(_UpperCamelCase)

          _lowerCamelCase     :		Tuple 						=						model(
              _UpperCamelCase    ,  start_positions=_UpperCamelCase    ,  end_positions=_UpperCamelCase    ,  cls_index=_UpperCamelCase    ,  is_impossible=_UpperCamelCase    ,  p_mask=_UpperCamelCase    ,  )

          _lowerCamelCase     :		List[Any] 						=						model(
              _UpperCamelCase    ,  start_positions=_UpperCamelCase    ,  end_positions=_UpperCamelCase    ,  cls_index=_UpperCamelCase    ,  is_impossible=_UpperCamelCase    ,  )

          ((_lowerCamelCase)						,							)     :		List[Any] 						=						result_with_labels.to_tuple()

          _lowerCamelCase     :		Dict 						=						model(_UpperCamelCase    ,  start_positions=_UpperCamelCase    ,  end_positions=_UpperCamelCase)

          ((_lowerCamelCase)						,							)     :		Dict 						=						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 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				Union[str, Any]    ,  )				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						FlaubertForSequenceClassification(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		Tuple 						=						model(_UpperCamelCase)
          _lowerCamelCase     :		Union[str, 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 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Any    ,  )				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						self.num_labels
          _lowerCamelCase     :		int 						=						FlaubertForTokenClassification(_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()

          _lowerCamelCase     :		Optional[int] 						=						model(_UpperCamelCase    ,  attention_mask=_UpperCamelCase    ,  labels=_UpperCamelCase)
          self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.num_labels))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				Any    ,  )				->int:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						self.num_choices
          _lowerCamelCase     :		List[Any] 						=						FlaubertForMultipleChoice(config=_UpperCamelCase)
          model.to(_UpperCamelCase)
          model.eval()
          _lowerCamelCase     :		Optional[Any] 						=						input_ids.unsqueeze(1).expand(-1    ,  self.num_choices    ,  -1).contiguous()
          _lowerCamelCase     :		Tuple 						=						token_type_ids.unsqueeze(1).expand(-1    ,  self.num_choices    ,  -1).contiguous()
          _lowerCamelCase     :		Union[str, Any] 						=						input_mask.unsqueeze(1).expand(-1    ,  self.num_choices    ,  -1).contiguous()
          _lowerCamelCase     :		Dict 						=						model(
              _UpperCamelCase    ,  attention_mask=_UpperCamelCase    ,  token_type_ids=_UpperCamelCase    ,  labels=_UpperCamelCase    ,  )
          self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_choices))




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[int] 						=						self.prepare_config_and_inputs()
          (
              (
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							(
              _lowerCamelCase
          )						,							
          )     :		Union[str, Any] 						=						config_and_inputs
          _lowerCamelCase     :		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 	__snake_case	( __lowerCAmelCase ,  __lowerCAmelCase ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _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 	_SCREAMING_SNAKE_CASE    (					self     :				str    ,  _UpperCamelCase     :				List[str]    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				str)				->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 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				Dict    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Tuple=False)				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		List[Any] 						=						super()._prepare_for_class(_UpperCamelCase    ,  _UpperCamelCase    ,  return_labels=_UpperCamelCase)

          if return_labels:
              if model_class.__name__ == "FlaubertForQuestionAnswering":
                  _lowerCamelCase     :		Union[str, Any] 						=						torch.zeros(
                      self.model_tester.batch_size    ,  dtype=torch.long    ,  device=_UpperCamelCase)
                  _lowerCamelCase     :		int 						=						torch.zeros(
                      self.model_tester.batch_size    ,  dtype=torch.long    ,  device=_UpperCamelCase)

          return inputs_dict



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any])				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		int 						=						FlaubertModelTester(self)
          _lowerCamelCase     :		List[str] 						=						ConfigTester(self    ,  config_class=_UpperCamelCase    ,  emb_dim=37)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->int:


          """simple docstring"""


          self.config_tester.run_common_tests()



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_model(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->str:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_lm_head(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_simple_qa(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Any:


          """simple docstring"""


          _lowerCamelCase     :		Tuple 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_qa(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_sequence_classif(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple)				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_token_classif(*_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Optional[Any]:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_multiple_choice(*_UpperCamelCase)



      @slow
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Dict:


          """simple docstring"""


          for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCamelCase     :		Optional[int] 						=						FlaubertModel.from_pretrained(_UpperCamelCase)
              self.assertIsNotNone(_UpperCamelCase)




      @slow
      @require_torch_gpu
      def 	_SCREAMING_SNAKE_CASE    (					self     :				Dict)				->Tuple:


          """simple docstring"""


          _lowerCamelCase						,							_lowerCamelCase     :		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

              _lowerCamelCase     :		Any 						=						True
              _lowerCamelCase     :		List[Any] 						=						model_class(config=_UpperCamelCase)

              _lowerCamelCase     :		Tuple 						=						self._prepare_for_class(_UpperCamelCase    ,  _UpperCamelCase)
              _lowerCamelCase     :		Optional[Any] 						=						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"""))
                  _lowerCamelCase     :		List[Any] 						=						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 	__snake_case	( unittest.TestCase		):





      '''simple docstring'''






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


          """simple docstring"""


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





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


lowerCAmelCase			:							Tuple     =logging.get_logger(__name__)




class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       ['pixel_values']



      def __init__(					self     :				Optional[Any]    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				PILImageResampling = PIL.Image.BICUBIC    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				Union[int, float] = 1 / 255    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				bool = True    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  **_UpperCamelCase     :				str    ,  )				->None:


          """simple docstring"""


          super().__init__(**_UpperCamelCase)
          _lowerCamelCase     :		Tuple 						=						size if size is not None else {"""height""": 256, """width""": 256}
          _lowerCamelCase     :		Optional[Any] 						=						get_size_dict(_UpperCamelCase)
          _lowerCamelCase     :		Any 						=						crop_size if crop_size is not None else {"""height""": 224, """width""": 224}
          _lowerCamelCase     :		Any 						=						get_size_dict(_UpperCamelCase    ,  param_name="""crop_size""")

          _lowerCamelCase     :		int 						=						do_resize
          _lowerCamelCase     :		int 						=						size
          _lowerCamelCase     :		Optional[int] 						=						resample
          _lowerCamelCase     :		int 						=						do_center_crop
          _lowerCamelCase     :		Optional[Any] 						=						crop_size
          _lowerCamelCase     :		Union[str, Any] 						=						do_rescale
          _lowerCamelCase     :		List[str] 						=						rescale_factor
          _lowerCamelCase     :		List[Any] 						=						do_normalize
          _lowerCamelCase     :		Dict 						=						image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          _lowerCamelCase     :		int 						=						image_std if image_std is not None else IMAGENET_STANDARD_STD



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Dict[str, int]    ,  _UpperCamelCase     :				PILImageResampling = PIL.Image.BICUBIC    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->np.ndarray:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						get_size_dict(_UpperCamelCase)
          if "height" not in size or "width" not in size:
              raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""")
          return resize(
              _UpperCamelCase    ,  size=(size["""height"""], size["""width"""])    ,  resample=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Dict[str, int]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				List[str]    ,  )				->np.ndarray:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						get_size_dict(_UpperCamelCase)
          if "height" not in size or "width" not in size:
              raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""")
          return center_crop(_UpperCamelCase    ,  size=(size["""height"""], size["""width"""])    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Union[int, float]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->str:


          """simple docstring"""


          return rescale(_UpperCamelCase    ,  scale=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any]    ,  _UpperCamelCase     :				np.ndarray    ,  _UpperCamelCase     :				Union[float, List[float]]    ,  _UpperCamelCase     :				Union[float, List[float]]    ,  _UpperCamelCase     :				Optional[Union[str, ChannelDimension]] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->np.ndarray:


          """simple docstring"""


          return normalize(_UpperCamelCase    ,  mean=_UpperCamelCase    ,  std=_UpperCamelCase    ,  data_format=_UpperCamelCase    ,  **_UpperCamelCase)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[Any]    ,  _UpperCamelCase     :				ImageInput    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				Tuple=None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Dict[str, int] = None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				float = None    ,  _UpperCamelCase     :				bool = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[float, List[float]]] = None    ,  _UpperCamelCase     :				Optional[Union[str, TensorType]] = None    ,  _UpperCamelCase     :				ChannelDimension = ChannelDimension.FIRST    ,  **_UpperCamelCase     :				List[Any]    ,  )				->PIL.Image.Image:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						do_resize if do_resize is not None else self.do_resize
          _lowerCamelCase     :		List[str] 						=						resample if resample is not None else self.resample
          _lowerCamelCase     :		Dict 						=						do_center_crop if do_center_crop is not None else self.do_center_crop
          _lowerCamelCase     :		Optional[Any] 						=						do_rescale if do_rescale is not None else self.do_rescale
          _lowerCamelCase     :		Dict 						=						rescale_factor if rescale_factor is not None else self.rescale_factor
          _lowerCamelCase     :		Dict 						=						do_normalize if do_normalize is not None else self.do_normalize
          _lowerCamelCase     :		int 						=						image_mean if image_mean is not None else self.image_mean
          _lowerCamelCase     :		Dict 						=						image_std if image_std is not None else self.image_std

          _lowerCamelCase     :		Optional[Any] 						=						size if size is not None else self.size
          _lowerCamelCase     :		Optional[int] 						=						get_size_dict(_UpperCamelCase)
          _lowerCamelCase     :		List[str] 						=						crop_size if crop_size is not None else self.crop_size
          _lowerCamelCase     :		Dict 						=						get_size_dict(_UpperCamelCase    ,  param_name="""crop_size""")

          _lowerCamelCase     :		int 						=						make_list_of_images(_UpperCamelCase)

          if not valid_images(_UpperCamelCase):
              raise ValueError(
                  """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                  """torch.Tensor, tf.Tensor or jax.ndarray.""")

          if do_resize and size is None or resample is None:
              raise ValueError("""Size and resample must be specified if do_resize is True.""")

          if do_center_crop and crop_size is None:
              raise ValueError("""Crop size must be specified if do_center_crop is True.""")

          if do_rescale and rescale_factor is None:
              raise ValueError("""Rescale factor must be specified if do_rescale is True.""")

          if do_normalize and (image_mean is None or image_std is None):
              raise ValueError("""Image mean and std must be specified if do_normalize is True.""")

          # All transformations expect numpy arrays.
          _lowerCamelCase     :		Union[str, Any] 						=						[to_numpy_array(_UpperCamelCase) for image in images]

          if do_resize:
              _lowerCamelCase     :		Any 						=						[self.resize(image=_UpperCamelCase    ,  size=_UpperCamelCase    ,  resample=_UpperCamelCase) for image in images]

          if do_center_crop:
              _lowerCamelCase     :		str 						=						[self.center_crop(image=_UpperCamelCase    ,  size=_UpperCamelCase) for image in images]

          if do_rescale:
              _lowerCamelCase     :		Optional[int] 						=						[self.rescale(image=_UpperCamelCase    ,  scale=_UpperCamelCase) for image in images]

          if do_normalize:
              _lowerCamelCase     :		List[str] 						=						[self.normalize(image=_UpperCamelCase    ,  mean=_UpperCamelCase    ,  std=_UpperCamelCase) for image in images]

          _lowerCamelCase     :		List[str] 						=						[to_channel_dimension_format(_UpperCamelCase    ,  _UpperCamelCase) for image in images]

          _lowerCamelCase     :		int 						=						{"""pixel_values""": images}
          return BatchFeature(data=_UpperCamelCase    ,  tensor_type=_UpperCamelCase)




