


"""simple docstring"""



from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, 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 tensorflow as tf

  from transformers.models.distilbert.modeling_tf_distilbert import (
      TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDistilBertForMaskedLM,
      TFDistilBertForMultipleChoice,
      TFDistilBertForQuestionAnswering,
      TFDistilBertForSequenceClassification,
      TFDistilBertForTokenClassification,
      TFDistilBertModel,
  )







class _lowerCAmelCase       :







   def __init__(			self		,			UpperCamelCase__		,			)			->	List[str]:

       '''simple docstring'''




       snake_case					:   Tuple         =					parent
       snake_case					:   str         =					13
       snake_case					:   Any         =					7
       snake_case					:   List[str]         =					True
       snake_case					:   Optional[Any]         =					True
       snake_case					:   Any         =					False
       snake_case					:   Any         =					True
       snake_case					:   Any         =					99
       snake_case					:   Any         =					32
       snake_case					:   Any         =					2
       snake_case					:   List[Any]         =					4
       snake_case					:   Union[str, Any]         =					37
       snake_case					:   Dict         =					"gelu"
       snake_case					:   List[Any]         =					0.1
       snake_case					:   int         =					0.1
       snake_case					:   Optional[int]         =					512
       snake_case					:   str         =					16
       snake_case					:   Dict         =					2
       snake_case					:   Optional[int]         =					0.02
       snake_case					:   List[Any]         =					3
       snake_case					:   Union[str, Any]         =					4
       snake_case					:   Optional[int]         =					None







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




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

       snake_case					:   Any         =					None
       if self.use_input_mask:
           snake_case					:   Optional[Any]         =					random_attention_mask([self.batch_size, self.seq_length]		)

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

       snake_case					:   Union[str, Any]         =					DistilBertConfig(
           vocab_size=self.vocab_size		,			dim=self.hidden_size		,			n_layers=self.num_hidden_layers		,			n_heads=self.num_attention_heads		,			hidden_dim=self.intermediate_size		,			hidden_act=self.hidden_act		,			dropout=self.hidden_dropout_prob		,			attention_dropout=self.attention_probs_dropout_prob		,			max_position_embeddings=self.max_position_embeddings		,			initializer_range=self.initializer_range		,			)

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







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

       '''simple docstring'''




       snake_case					:   List[Any]         =					TFDistilBertModel(config=lowerCAmelCase__		)
       snake_case					:   List[Any]         =					{"input_ids": input_ids, "attention_mask": input_mask}

       snake_case					:   Tuple         =					model(lowerCAmelCase__		)

       snake_case					:   List[Any]         =					[input_ids, input_mask]

       snake_case					:   Tuple         =					model(lowerCAmelCase__		)

       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__		)			->	int:

       '''simple docstring'''




       snake_case					:   List[Any]         =					TFDistilBertForMaskedLM(config=lowerCAmelCase__		)
       snake_case					:   List[str]         =					{"input_ids": input_ids, "attention_mask": input_mask}
       snake_case					:   Union[str, Any]         =					model(lowerCAmelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)		)







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

       '''simple docstring'''




       snake_case					:   Any         =					TFDistilBertForQuestionAnswering(config=lowerCAmelCase__		)
       snake_case					:   int         =					{
           "input_ids": input_ids,
           "attention_mask": input_mask,
       }
       snake_case					:   Tuple         =					model(lowerCAmelCase__		)
       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__		)			->	str:

       '''simple docstring'''




       snake_case					:   Dict         =					self.num_labels
       snake_case					:   Optional[int]         =					TFDistilBertForSequenceClassification(lowerCAmelCase__		)
       snake_case					:   List[Any]         =					{"input_ids": input_ids, "attention_mask": input_mask}
       snake_case					:   Any         =					model(lowerCAmelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)		)







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

       '''simple docstring'''




       snake_case					:   Dict         =					self.num_choices
       snake_case					:   Union[str, Any]         =					TFDistilBertForMultipleChoice(lowerCAmelCase__		)
       snake_case					:   Union[str, Any]         =					tf.tile(tf.expand_dims(lowerCAmelCase__		,			1		)		,			(1, self.num_choices, 1)		)
       snake_case					:   List[str]         =					tf.tile(tf.expand_dims(lowerCAmelCase__		,			1		)		,			(1, self.num_choices, 1)		)
       snake_case					:   List[Any]         =					{
           "input_ids": multiple_choice_inputs_ids,
           "attention_mask": multiple_choice_input_mask,
       }
       snake_case					:   List[str]         =					model(lowerCAmelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_choices)		)







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

       '''simple docstring'''




       snake_case					:   Dict         =					self.num_labels
       snake_case					:   Tuple         =					TFDistilBertForTokenClassification(lowerCAmelCase__		)
       snake_case					:   Tuple         =					{"input_ids": input_ids, "attention_mask": input_mask}
       snake_case					:   Optional[int]         =					model(lowerCAmelCase__		)
       self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.num_labels)		)







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

       '''simple docstring'''




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







@require_tf
class _lowerCAmelCase       (   a__					,							a__					,							unittest.TestCase			):
   __UpperCAmelCase				: Optional[Any]													=						(
       (
           TFDistilBertModel,
           TFDistilBertForMaskedLM,
           TFDistilBertForQuestionAnswering,
           TFDistilBertForSequenceClassification,
           TFDistilBertForTokenClassification,
           TFDistilBertForMultipleChoice,
       )
       if is_tf_available()
       else None
   )
   __UpperCAmelCase				: Any													=						(
       {
           """feature-extraction""": TFDistilBertModel,
           """fill-mask""": TFDistilBertForMaskedLM,
           """question-answering""": TFDistilBertForQuestionAnswering,
           """text-classification""": TFDistilBertForSequenceClassification,
           """token-classification""": TFDistilBertForTokenClassification,
           """zero-shot""": TFDistilBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   __UpperCAmelCase				: List[str]													=						False
   __UpperCAmelCase				: Optional[int]													=						False







   def 				lowerCamelCase	(			self		)			->	str:

       '''simple docstring'''




       snake_case					:   Optional[Any]         =					TFDistilBertModelTester(self		)
       snake_case					:   List[str]         =					ConfigTester(self		,			config_class=lowerCAmelCase__		,			dim=37		)







   def 				lowerCamelCase	(			self		)			->	Any:

       '''simple docstring'''




       self.config_tester.run_common_tests()







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

       '''simple docstring'''




       snake_case					:   Optional[int]         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__		)







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




       snake_case					:   Optional[int]         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__		)







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

       '''simple docstring'''




       snake_case					:   str         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__		)







   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   Dict         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__		)







   def 				lowerCamelCase	(			self		)			->	Dict:

       '''simple docstring'''




       snake_case					:   Optional[int]         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase__		)







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

       '''simple docstring'''




       snake_case					:   Any         =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase__		)







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

       '''simple docstring'''




       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
           snake_case					:   Tuple         =					TFDistilBertModel.from_pretrained(lowerCAmelCase__		)
           self.assertIsNotNone(lowerCAmelCase__		)







@require_tf
class _lowerCAmelCase       (   unittest.TestCase			):







   @slow
   def 				lowerCamelCase	(			self		)			->	Tuple:

       '''simple docstring'''




       snake_case					:   List[Any]         =					TFDistilBertModel.from_pretrained("distilbert-base-uncased"		)
       snake_case					:   Dict         =					tf.constant([[0, 1, 2, 3, 4, 5]]		)
       snake_case					:   int         =					model(lowerCAmelCase__		)[0]

       snake_case					:   int         =					[1, 6, 768]
       self.assertEqual(output.shape		,			lowerCAmelCase__		)

       snake_case					:   List[Any]         =					tf.constant(
           [
               [
                   [0.19261885, -0.13732955, 0.4119799],
                   [0.22150156, -0.07422661, 0.39037204],
                   [0.22756018, -0.0896414, 0.3701467],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]		,			lowerCAmelCase__		,			atol=1e-4		)









"""simple docstring"""



import argparse
import glob
import importlib.util
import os
import re

import black
from doc_builder.style_doc import style_docstrings_in_code


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_copies.py
__snake_case         =					"""src/diffusers"""
__snake_case         =					"""."""


# This is to make sure the diffusers module imported is the one in the repo.
__snake_case         =					importlib.util.spec_from_file_location(
    """diffusers""",
    os.path.join(DIFFUSERS_PATH, """__init__.py"""),
    submodule_search_locations=[DIFFUSERS_PATH],
)
__snake_case         =					spec.loader.load_module()







def 						__lowerCAmelCase		(		lowercase						:						List[str]		,							lowercase						:						Optional[int]				)			->		Union[str, Any]:


    """simple docstring"""





    return line.startswith(lowercase				) or len(lowercase				) <= 1 or re.search(R"^\s*\)(\s*->.*:|:)\s*$"		,							lowercase				) is not None







def 						__lowerCAmelCase		(		lowercase						:						List[Any]				)			->		Union[str, Any]:


    """simple docstring"""





    snake_case					:   Any         =					object_name.split("."				)
    snake_case					:   Dict         =					0

    # First let's find the module where our object lives.
    snake_case					:   Optional[Any]         =					parts[i]
    while i < len(lowercase				) and not os.path.isfile(os.path.join(lowercase		,							F'{module}.py'				)				):
        i += 1
        if i < len(lowercase				):
            snake_case					:   List[Any]         =					os.path.join(lowercase		,							parts[i]				)
    if i >= len(lowercase				):
        raise ValueError(F'`object_name` should begin with the name of a module of diffusers but got {object_name}.'				)

    with open(os.path.join(lowercase		,							F'{module}.py'				)		,							"r"		,							encoding="utf-8"		,							newline="\n"				) as f:
        snake_case					:   str         =					f.readlines()

    # Now let's find the class / func in the code!
    snake_case					:   List[str]         =					""
    snake_case					:   Optional[Any]         =					0
    for name in parts[i + 1 :]:
        while (
            line_index < len(lowercase				) and re.search(RF'^{indent}(class|def)\s+{name}(\(|\:)'		,							lines[line_index]				) is None
        ):
            line_index += 1
        indent += "    "
        line_index += 1

    if line_index >= len(lowercase				):
        raise ValueError(F' {object_name} does not match any function or class in {module}.'				)

    # We found the beginning of the class / func, now let's find the end (when the indent diminishes).
    snake_case					:   Union[str, Any]         =					line_index
    while line_index < len(lowercase				) and _should_continue(lines[line_index]		,							lowercase				):
        line_index += 1
    # Clean up empty lines at the end (if any).
    while len(lines[line_index - 1]				) <= 1:
        line_index -= 1

    snake_case					:   Tuple         =					lines[start_index:line_index]
    return "".join(lowercase				)


__snake_case         =					re.compile(R"""^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)""")
__snake_case         =					re.compile(R"""^\s*(\S+)->(\S+)(\s+.*|$)""")
__snake_case         =					re.compile(R"""<FILL\s+[^>]*>""")







def 						__lowerCAmelCase		(		lowercase						:						Optional[int]				)			->		Dict:


    """simple docstring"""





    snake_case					:   List[str]         =					code.split("\n"				)
    snake_case					:   Tuple         =					0
    while idx < len(lowercase				) and len(lines[idx]				) == 0:
        idx += 1
    if idx < len(lowercase				):
        return re.search(R"^(\s*)\S"		,							lines[idx]				).groups()[0]
    return ""







def 						__lowerCAmelCase		(		lowercase						:						Any				)			->		Optional[int]:


    """simple docstring"""





    snake_case					:   Optional[int]         =					len(get_indent(lowercase				)				) > 0
    if has_indent:
        snake_case					:   List[Any]         =					F'class Bla:\n{code}'
    snake_case					:   List[Any]         =					black.Mode(target_versions={black.TargetVersion.PYaa}		,							line_length=119		,							preview=lowercase				)
    snake_case					:   str         =					black.format_str(lowercase		,							mode=lowercase				)
    snake_case      ,snake_case					:   int         =					style_docstrings_in_code(lowercase				)
    return result[len("class Bla:\n"				) :] if has_indent else result







def 						__lowerCAmelCase		(		lowercase						:						Dict		,							lowercase						:						List[Any]=False				)			->		Dict:


    """simple docstring"""





    with open(lowercase		,							"r"		,							encoding="utf-8"		,							newline="\n"				) as f:
        snake_case					:   str         =					f.readlines()
    snake_case					:   Optional[int]         =					[]
    snake_case					:   List[Any]         =					0
    # Not a for loop cause `lines` is going to change (if `overwrite=True`).
    while line_index < len(lowercase				):
        snake_case					:   List[Any]         =					_re_copy_warning.search(lines[line_index]				)
        if search is None:
            line_index += 1
            continue

        # There is some copied code here, let's retrieve the original.
        snake_case      ,snake_case      ,snake_case					:   List[str]         =					search.groups()
        snake_case					:   List[Any]         =					find_code_in_diffusers(lowercase				)
        snake_case					:   Dict         =					get_indent(lowercase				)

        snake_case					:   Union[str, Any]         =					line_index + 1 if indent == theoretical_indent else line_index + 2
        snake_case					:   Any         =					theoretical_indent
        snake_case					:   Optional[int]         =					start_index

        # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment.
        snake_case					:   Dict         =					True
        while line_index < len(lowercase				) and should_continue:
            line_index += 1
            if line_index >= len(lowercase				):
                break
            snake_case					:   int         =					lines[line_index]
            snake_case					:   Tuple         =					_should_continue(lowercase		,							lowercase				) and re.search(F'^{indent}# End copy'		,							lowercase				) is None
        # Clean up empty lines at the end (if any).
        while len(lines[line_index - 1]				) <= 1:
            line_index -= 1

        snake_case					:   Optional[int]         =					lines[start_index:line_index]
        snake_case					:   List[Any]         =					"".join(lowercase				)

        # Remove any nested `Copied from` comments to avoid circular copies
        snake_case					:   List[str]         =					[line for line in theoretical_code.split("\n"				) if _re_copy_warning.search(lowercase				) is None]
        snake_case					:   Optional[int]         =					"\n".join(lowercase				)

        # Before comparing, use the `replace_pattern` on the original code.
        if len(lowercase				) > 0:
            snake_case					:   List[str]         =					replace_pattern.replace("with"		,							""				).split(","				)
            snake_case					:   Union[str, Any]         =					[_re_replace_pattern.search(lowercase				) for p in patterns]
            for pattern in patterns:
                if pattern is None:
                    continue
                snake_case      ,snake_case      ,snake_case					:   str         =					pattern.groups()
                snake_case					:   Any         =					re.sub(lowercase		,							lowercase		,							lowercase				)
                if option.strip() == "all-casing":
                    snake_case					:   int         =					re.sub(obja.lower()		,							obja.lower()		,							lowercase				)
                    snake_case					:   Optional[int]         =					re.sub(obja.upper()		,							obja.upper()		,							lowercase				)

            # Blackify after replacement. To be able to do that, we need the header (class or function definition)
            # from the previous line
            snake_case					:   Tuple         =					blackify(lines[start_index - 1] + theoretical_code				)
            snake_case					:   Any         =					theoretical_code[len(lines[start_index - 1]				) :]

        # Test for a diff and act accordingly.
        if observed_code != theoretical_code:
            diffs.append([object_name, start_index]				)
            if overwrite:
                snake_case					:   Any         =					lines[:start_index] + [theoretical_code] + lines[line_index:]
                snake_case					:   Tuple         =					start_index + 1

    if overwrite and len(lowercase				) > 0:
        # Warn the user a file has been modified.
        print(F'Detected changes, rewriting {filename}.'				)
        with open(lowercase		,							"w"		,							encoding="utf-8"		,							newline="\n"				) as f:
            f.writelines(lowercase				)
    return diffs







def 						__lowerCAmelCase		(		lowercase						:						bool = False				)			->		Optional[Any]:


    """simple docstring"""





    snake_case					:   str         =					glob.glob(os.path.join(lowercase		,							"**/*.py"				)		,							recursive=lowercase				)
    snake_case					:   List[str]         =					[]
    for filename in all_files:
        snake_case					:   List[str]         =					is_copy_consistent(lowercase		,							lowercase				)
        diffs += [F'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs]
    if not overwrite and len(lowercase				) > 0:
        snake_case					:   List[str]         =					"\n".join(lowercase				)
        raise Exception(
            "Found the following copy inconsistencies:\n"
            + diff
            + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them."				)


if __name__ == "__main__":
  __snake_case         =					argparse.ArgumentParser()
  parser.add_argument("""--fix_and_overwrite""", action="""store_true""", help="""Whether to fix inconsistencies.""")
  __snake_case         =					parser.parse_args()

  check_copies(args.fix_and_overwrite)





