




import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
lowerCamelCase				:		Union[str, Any]     =   "src/diffusers"

# Matches is_xxx_available()
lowerCamelCase				:		Dict     =   re.compile(r"is\_([a-z_]*)_available\(\)")
# Matches from xxx import bla
lowerCamelCase				:		Union[str, Any]     =   re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n")


lowerCamelCase				:		Any     =   "\n{0} = None\n"

lowerCamelCase				:		List[str]     =   "\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, {1})\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, {1})\n"


lowerCamelCase				:		str     =   "\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n"

def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       Optional[Any]     ):


      '''simple docstring'''



      lowerCamelCase_		     =    _re_backend.findall(lowercase     )
      if len(lowercase     ) == 0:
            return None

      return "_and_".join(lowercase     )

def 			_SCREAMING_SNAKE_CASE			(							):


      '''simple docstring'''



      with open(os.path.join(lowercase	,							'__init__.py'     )	,							'r'	,							encoding='utf-8'	,							newline='\n'     ) as f:
            lowerCamelCase_		     =    f.readlines()

      # Get to the point we do the actual imports for type checking
      lowerCamelCase_		     =    0
      lowerCamelCase_		     =    {}
      # Go through the end of the file
      while line_index < len(lowercase     ):
            # If the line contains is_backend_available, we grab all objects associated with the `else` block
            lowerCamelCase_		     =    find_backend(lines[line_index]     )
            if backend is not None:
                  while not lines[line_index].startswith('else:'     ):
                        line_index += 1
                  line_index += 1
                  lowerCamelCase_		     =    []
                  # Until we unindent, add backend objects to the list
                  while line_index < len(lowercase     ) and len(lines[line_index]     ) > 1:
                        lowerCamelCase_		     =    lines[line_index]
                        lowerCamelCase_		     =    _re_single_line_import.search(lowercase     )
                        if single_line_import_search is not None:
                              objects.extend(single_line_import_search.groups()[0].split(', '     )     )
                        elif line.startswith(' ' * 8     ):
                              objects.append(line[8:-2]     )
                        line_index += 1

                  if len(lowercase     ) > 0:
                        lowerCamelCase_		     =    objects
            else:
                  line_index += 1

      return backend_specific_objects

def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       List[str]	,							lowercase	:       str     ):


      '''simple docstring'''



      if name.isupper():
            return DUMMY_CONSTANT.format(lowercase     )
      elif name.islower():
            return DUMMY_FUNCTION.format(lowercase	,							lowercase     )
      else:
            return DUMMY_CLASS.format(lowercase	,							lowercase     )

def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       Optional[int]=None     ):


      '''simple docstring'''



      if backend_specific_objects is None:
            lowerCamelCase_		     =    read_init()
      # For special correspondence backend to module name as used in the function requires_modulename
      lowerCamelCase_		     =    {}

      for backend, objects in backend_specific_objects.items():
            lowerCamelCase_		     =    '[' + ', '.join(f"""\"{b}\"""" for b in backend.split('_and_'     )     ) + ']'
            lowerCamelCase_		     =    '# This file is autogenerated by the command `make fix-copies`, do not edit.\n'
            dummy_file += "from ..utils import DummyObject, requires_backends\n\n"
            dummy_file += "\n".join([create_dummy_object(lowercase	,							lowercase     ) for o in objects]     )
            lowerCamelCase_		     =    dummy_file

      return dummy_files

def 			_SCREAMING_SNAKE_CASE			(							lowercase	:       Optional[int]=False     ):


      '''simple docstring'''



      lowerCamelCase_		     =    create_dummy_files()
      # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
      lowerCamelCase_		     =    {'torch': 'pt'}

      # Locate actual dummy modules and read their content.
      lowerCamelCase_		     =    os.path.join(lowercase	,							'utils'     )
      lowerCamelCase_		     =    {
          backend: os.path.join(lowercase	,							f"""dummy_{short_names.get(lowercase	,							lowercase     )}_objects.py"""     )
          for backend in dummy_files.keys()
      }

      lowerCamelCase_		     =    {}
      for backend, file_path in dummy_file_paths.items():
            if os.path.isfile(lowercase     ):
                  with open(lowercase	,							'r'	,							encoding='utf-8'	,							newline='\n'     ) as f:
                        lowerCamelCase_		     =    f.read()
            else:
                  lowerCamelCase_		     =    ''

      for backend in dummy_files.keys():
            if dummy_files[backend] != actual_dummies[backend]:
                  if overwrite:
                        print(
                            f"""Updating diffusers.utils.dummy_{short_names.get(lowercase	,							lowercase     )}_objects.py as the main """
                            '__init__ has new objects.'     )
                        with open(dummy_file_paths[backend]	,							'w'	,							encoding='utf-8'	,							newline='\n'     ) as f:
                              f.write(dummy_files[backend]     )
                  else:
                        raise ValueError(
                            'The main __init__ has objects that are not present in '
                            f"""diffusers.utils.dummy_{short_names.get(lowercase	,							lowercase     )}_objects.py. Run `make fix-copies` """
                            'to fix this.'     )


if __name__ == "__main__":
 lowerCamelCase				:		List[Any]     =   argparse.ArgumentParser()
 parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
 lowerCamelCase				:		Tuple     =   parser.parse_args()

 check_dummies(args.fix_and_overwrite)









from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import numpy as np
 import tensorflow as tf

 from transformers import (
     TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
     FlaubertConfig,
     TFFlaubertForMultipleChoice,
     TFFlaubertForQuestionAnsweringSimple,
     TFFlaubertForSequenceClassification,
     TFFlaubertForTokenClassification,
     TFFlaubertModel,
     TFFlaubertWithLMHeadModel,
 )







class   A:



 '''simple docstring'''


 def __init__(				self					:  str						,							A_					:  Optional[Any]						,							)   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    parent
       lowerCamelCase_		     =    13
       lowerCamelCase_		     =    7
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    False
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    99
       lowerCamelCase_		     =    0
       lowerCamelCase_		     =    32
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    4
       lowerCamelCase_		     =    0.1
       lowerCamelCase_		     =    0.1
       lowerCamelCase_		     =    512
       lowerCamelCase_		     =    16
       lowerCamelCase_		     =    2
       lowerCamelCase_		     =    0.02
       lowerCamelCase_		     =    3
       lowerCamelCase_		     =    4
       lowerCamelCase_		     =    'last'
       lowerCamelCase_		     =    True
       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    0

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

       """simple docstring"""






       lowerCamelCase_		     =    ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size  )
       lowerCamelCase_		     =    random_attention_mask([self.batch_size, self.seq_length]						,							dtype=tf.floataa  )

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

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

       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    None
       lowerCamelCase_		     =    None
       if self.use_labels:
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							self.type_sequence_label_size  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size, self.seq_length]						,							self.num_labels  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							2						,							dtype=tf.floataa  )
             lowerCamelCase_		     =    ids_tensor([self.batch_size]						,							self.num_choices  )

       lowerCamelCase_		     =    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						,							bos_token_id=self.bos_token_id						,							)

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

 def 				a__ (				self					:  int						,							A_					:  List[str]						,							A_					:  List[Any]						,							A_					:  str						,							A_					:  List[Any]						,							A_					:  int						,							A_					:  Tuple						,							A_					:  Optional[int]						,							A_					:  Optional[int]						,							A_					:  str						,							)   ->	Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertModel(config=A_  )
       lowerCamelCase_		     =    {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
       lowerCamelCase_		     =    model(A_  )

       lowerCamelCase_		     =    [input_ids, input_mask]
       lowerCamelCase_		     =    model(A_  )
       self.parent.assertEqual(result.last_hidden_state.shape						,							(self.batch_size, self.seq_length, self.hidden_size)  )

 def 				a__ (				self					:  Tuple						,							A_					:  List[str]						,							A_					:  int						,							A_					:  List[Any]						,							A_					:  Any						,							A_					:  Any						,							A_					:  Dict						,							A_					:  str						,							A_					:  List[Any]						,							A_					:  Union[str, Any]						,							)   ->	List[str]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertWithLMHeadModel(A_  )

       lowerCamelCase_		     =    {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
       lowerCamelCase_		     =    model(A_  )

       self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.vocab_size)  )

 def 				a__ (				self					:  str						,							A_					:  Tuple						,							A_					:  Any						,							A_					:  Any						,							A_					:  List[Any]						,							A_					:  Dict						,							A_					:  List[Any]						,							A_					:  Union[str, Any]						,							A_					:  Optional[int]						,							A_					:  List[Any]						,							)   ->	Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertForQuestionAnsweringSimple(A_  )

       lowerCamelCase_		     =    {'input_ids': input_ids, 'lengths': input_lengths}

       lowerCamelCase_		     =    model(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 				a__ (				self					:  Optional[int]						,							A_					:  List[Any]						,							A_					:  str						,							A_					:  List[str]						,							A_					:  Dict						,							A_					:  Optional[Any]						,							A_					:  Tuple						,							A_					:  str						,							A_					:  Optional[int]						,							A_					:  Tuple						,							)   ->	List[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertForSequenceClassification(A_  )

       lowerCamelCase_		     =    {'input_ids': input_ids, 'lengths': input_lengths}

       lowerCamelCase_		     =    model(A_  )

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

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

       """simple docstring"""






       lowerCamelCase_		     =    self.num_labels
       lowerCamelCase_		     =    TFFlaubertForTokenClassification(config=A_  )
       lowerCamelCase_		     =    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
       lowerCamelCase_		     =    model(A_  )
       self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.num_labels)  )

 def 				a__ (				self					:  List[Any]						,							A_					:  Optional[int]						,							A_					:  List[Any]						,							A_					:  Optional[int]						,							A_					:  Tuple						,							A_					:  Union[str, Any]						,							A_					:  int						,							A_					:  str						,							A_					:  Tuple						,							A_					:  str						,							)   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    self.num_choices
       lowerCamelCase_		     =    TFFlaubertForMultipleChoice(config=A_  )
       lowerCamelCase_		     =    tf.tile(tf.expand_dims(A_						,							1  )						,							(1, self.num_choices, 1)  )
       lowerCamelCase_		     =    tf.tile(tf.expand_dims(A_						,							1  )						,							(1, self.num_choices, 1)  )
       lowerCamelCase_		     =    tf.tile(tf.expand_dims(A_						,							1  )						,							(1, self.num_choices, 1)  )
       lowerCamelCase_		     =    {
           'input_ids': multiple_choice_inputs_ids,
           'attention_mask': multiple_choice_input_mask,
           'token_type_ids': multiple_choice_token_type_ids,
       }
       lowerCamelCase_		     =    model(A_  )
       self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_choices)  )





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

       """simple docstring"""






       lowerCamelCase_		     =    self.prepare_config_and_inputs()
       (
           (
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				(
           lowerCamelCase_
       )     ,				
       )		     =    config_and_inputs
       lowerCamelCase_		     =    {
           'input_ids': input_ids,
           'token_type_ids': token_type_ids,
           'langs': token_type_ids,
           'lengths': input_lengths,
       }
       return config, inputs_dict







@require_tf
class   A(						UpperCamelCase							,      UpperCamelCase							,      unittest.TestCase   ):



 '''simple docstring'''


 UpperCamelCase						   =						(
     (
         TFFlaubertModel,
         TFFlaubertWithLMHeadModel,
         TFFlaubertForSequenceClassification,
         TFFlaubertForQuestionAnsweringSimple,
         TFFlaubertForTokenClassification,
         TFFlaubertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 UpperCamelCase						   =						(
     (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 UpperCamelCase						   =						(
     {
         '''feature-extraction''': TFFlaubertModel,
         '''fill-mask''': TFFlaubertWithLMHeadModel,
         '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
         '''text-classification''': TFFlaubertForSequenceClassification,
         '''token-classification''': TFFlaubertForTokenClassification,
         '''zero-shot''': TFFlaubertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCamelCase						   =						False
 UpperCamelCase						   =						False

 def 				a__ (				self					:  Union[str, Any]						,							A_					:  Any						,							A_					:  List[Any]						,							A_					:  Union[str, Any]						,							A_					:  str						,							A_					:  List[str]  )   ->	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 				a__ (				self					:  List[str]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertModelTester(self  )
       lowerCamelCase_		     =    ConfigTester(self						,							config_class=A_						,							emb_dim=37  )

 def 				a__ (				self					:  List[str]  )   ->	str:

       """simple docstring"""






       self.config_tester.run_common_tests()

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_model(*A_  )

 def 				a__ (				self					:  List[str]  )   ->	Any:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_lm_head(*A_  )

 def 				a__ (				self					:  Dict  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_qa(*A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_sequence_classif(*A_  )

 def 				a__ (				self					:  List[Any]  )   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_for_token_classification(*A_  )

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

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_flaubert_for_multiple_choice(*A_  )





 @slow
 def 				a__ (				self					:  Union[str, Any]  )   ->	Optional[int]:

       """simple docstring"""






       for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_		     =    TFFlaubertModel.from_pretrained(A_  )
             self.assertIsNotNone(A_  )







@require_tf
@require_sentencepiece
@require_tokenizers
class   A(						unittest.TestCase   ):



 '''simple docstring'''


 @slow
 def 				a__ (				self					:  Dict  )   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    TFFlaubertModel.from_pretrained('jplu/tf-flaubert-small-cased'  )

       lowerCamelCase_		     =    tf.convert_to_tensor(
           [[0, 158, 735, 2592, 1424, 6727, 82, 1]]						,							dtype=tf.intaa						,							)  # "J'aime flaubert !"

       lowerCamelCase_		     =    model(A_  )[0]
       lowerCamelCase_		     =    tf.TensorShape((1, 8, 512)  )
       self.assertEqual(output.shape						,							A_  )
       # compare the actual values for a slice.
       lowerCamelCase_		     =    tf.convert_to_tensor(
           [
               [
                   [-1.8768773, -1.566555, 0.27072418],
                   [-1.6920038, -0.5873505, 1.9329599],
                   [-2.9563985, -1.6993835, 1.7972052],
               ]
           ]						,							dtype=tf.floataa						,							)

       self.assertTrue(np.allclose(output[:, :3, :3].numpy()						,							expected_slice.numpy()						,							atol=1E-4  )  )



