

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
_SCREAMING_SNAKE_CASE  :						List[Any]         =    '''src/diffusers'''
_SCREAMING_SNAKE_CASE  :						Optional[Any]         =    '''.'''


# This is to make sure the diffusers module imported is the one in the repo.
_SCREAMING_SNAKE_CASE  :						int         =    importlib.util.spec_from_file_location(
    '''diffusers''',
    os.path.join(DIFFUSERS_PATH, '''__init__.py'''),
    submodule_search_locations=[DIFFUSERS_PATH],
)
_SCREAMING_SNAKE_CASE  :						Tuple         =    spec.loader.load_module()
def        UpperCAmelCase_     (		_A   ,	_A       ):





     '''simple docstring'''




     return line.startswith(__SCREAMING_SNAKE_CASE       ) or len(__SCREAMING_SNAKE_CASE       ) <= 1 or re.search(R'''^\s*\)(\s*->.*:|:)\s*$'''   ,	__SCREAMING_SNAKE_CASE       ) is not None
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__     :   Optional[int]					=					object_name.split('''.'''       )
     SCREAMING_SNAKE_CASE__     :   Dict					=					0

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

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

     # Now let's find the class / func in the code!
     SCREAMING_SNAKE_CASE__     :   Any					=					""
     SCREAMING_SNAKE_CASE__     :   Optional[Any]					=					0
     for name in parts[i + 1 :]:
          while (
              line_index < len(__SCREAMING_SNAKE_CASE       ) 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(__SCREAMING_SNAKE_CASE       ):
          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).
     SCREAMING_SNAKE_CASE__     :   Union[str, Any]					=					line_index
     while line_index < len(__SCREAMING_SNAKE_CASE       ) and _should_continue(lines[line_index]   ,	__SCREAMING_SNAKE_CASE       ):
          line_index += 1
     # Clean up empty lines at the end (if any).
     while len(lines[line_index - 1]       ) <= 1:
          line_index -= 1

     SCREAMING_SNAKE_CASE__     :   Dict					=					lines[start_index:line_index]
     return "".join(__SCREAMING_SNAKE_CASE       )


_SCREAMING_SNAKE_CASE  :						int         =    re.compile(r'''^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)''')
_SCREAMING_SNAKE_CASE  :						Any         =    re.compile(r'''^\s*(\S+)->(\S+)(\s+.*|$)''')
_SCREAMING_SNAKE_CASE  :						List[str]         =    re.compile(r'''<FILL\s+[^>]*>''')
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__     :   str					=					code.split('''\n'''       )
     SCREAMING_SNAKE_CASE__     :   Union[str, Any]					=					0
     while idx < len(__SCREAMING_SNAKE_CASE       ) and len(lines[idx]       ) == 0:
          idx += 1
     if idx < len(__SCREAMING_SNAKE_CASE       ):
          return re.search(R'''^(\s*)\S'''   ,	lines[idx]       ).groups()[0]
     return ""
def        UpperCAmelCase_     (		_A       ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__     :   Optional[Any]					=					len(get_indent(__SCREAMING_SNAKE_CASE       )       ) > 0
     if has_indent:
          SCREAMING_SNAKE_CASE__     :   Optional[Any]					=					F'''class Bla:\n{code}'''
     SCREAMING_SNAKE_CASE__     :   List[Any]					=					black.Mode(target_versions={black.TargetVersion.PYaa}   ,	line_length=1_19   ,	preview=__SCREAMING_SNAKE_CASE       )
     SCREAMING_SNAKE_CASE__     :   List[str]					=					black.format_str(__SCREAMING_SNAKE_CASE   ,	mode=__SCREAMING_SNAKE_CASE       )
     SCREAMING_SNAKE_CASE__     :   Optional[Any]					=					style_docstrings_in_code(__SCREAMING_SNAKE_CASE       )
     return result[len('''class Bla:\n'''       ) :] if has_indent else result
def        UpperCAmelCase_     (		_A   ,	_A=False       ):





     '''simple docstring'''




     with open(__SCREAMING_SNAKE_CASE   ,	'''r'''   ,	encoding='''utf-8'''   ,	newline='''\n'''       ) as f:
          SCREAMING_SNAKE_CASE__     :   List[Any]					=					f.readlines()
     SCREAMING_SNAKE_CASE__     :   str					=					[]
     SCREAMING_SNAKE_CASE__     :   Tuple					=					0
     # Not a for loop cause `lines` is going to change (if `overwrite=True`).
     while line_index < len(__SCREAMING_SNAKE_CASE       ):
          SCREAMING_SNAKE_CASE__     :   Union[str, 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.
          SCREAMING_SNAKE_CASE__     :   Union[str, Any]					=					search.groups()
          SCREAMING_SNAKE_CASE__     :   int					=					find_code_in_diffusers(__SCREAMING_SNAKE_CASE       )
          SCREAMING_SNAKE_CASE__     :   List[Any]					=					get_indent(__SCREAMING_SNAKE_CASE       )

          SCREAMING_SNAKE_CASE__     :   Dict					=					line_index + 1 if indent == theoretical_indent else line_index + 2
          SCREAMING_SNAKE_CASE__     :   Dict					=					theoretical_indent
          SCREAMING_SNAKE_CASE__     :   Tuple					=					start_index

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

          SCREAMING_SNAKE_CASE__     :   Optional[int]					=					lines[start_index:line_index]
          SCREAMING_SNAKE_CASE__     :   Optional[Any]					=					"".join(__SCREAMING_SNAKE_CASE       )

          # Remove any nested `Copied from` comments to avoid circular copies
          SCREAMING_SNAKE_CASE__     :   int					=					[line for line in theoretical_code.split('''\n'''       ) if _re_copy_warning.search(__SCREAMING_SNAKE_CASE       ) is None]
          SCREAMING_SNAKE_CASE__     :   Dict					=					"\n".join(__SCREAMING_SNAKE_CASE       )

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

            # Blackify after replacement. To be able to do that, we need the header (class or function definition)
            # from the previous line
               SCREAMING_SNAKE_CASE__     :   Optional[int]					=					blackify(lines[start_index - 1] + theoretical_code       )
               SCREAMING_SNAKE_CASE__     :   Optional[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:
                    SCREAMING_SNAKE_CASE__     :   List[str]					=					lines[:start_index] + [theoretical_code] + lines[line_index:]
                    SCREAMING_SNAKE_CASE__     :   List[str]					=					start_index + 1

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





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__     :   List[str]					=					glob.glob(os.path.join(__SCREAMING_SNAKE_CASE   ,	'''**/*.py'''       )   ,	recursive=__SCREAMING_SNAKE_CASE       )
     SCREAMING_SNAKE_CASE__     :   List[Any]					=					[]
     for filename in all_files:
          SCREAMING_SNAKE_CASE__     :   Tuple					=					is_copy_consistent(__SCREAMING_SNAKE_CASE   ,	__SCREAMING_SNAKE_CASE       )
          diffs += [F'''- {filename}: copy does not match {d[0]} at line {d[1]}''' for d in new_diffs]
     if not overwrite and len(__SCREAMING_SNAKE_CASE       ) > 0:
          SCREAMING_SNAKE_CASE__     :   str					=					"\n".join(__SCREAMING_SNAKE_CASE       )
          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__":
       _SCREAMING_SNAKE_CASE  :						str         =    argparse.ArgumentParser()
       parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''')
       _SCREAMING_SNAKE_CASE  :						List[Any]         =    parser.parse_args()

       check_copies(args.fix_and_overwrite)





import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
       import torch_xla.core.xla_model as xm
       import torch_xla.debug.metrics as met






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  def __init__(							self		:	Tuple			,     *__lowerCamelCase		:	List[str]			,     __lowerCamelCase		:	Any=None			,     __lowerCamelCase		:	Optional[int]=None			,     **__lowerCamelCase		:	Optional[int]      )	->							List[str]:
       super().__init__(*__lowerCamelCase			,     **__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					eval_examples
       SCREAMING_SNAKE_CASE__					=					post_process_function






  def    lowercase_	(							self		:	Optional[Any]			,     __lowerCamelCase		:	Dict=None			,     __lowerCamelCase		:	int=None			,     __lowerCamelCase		:	str=None			,     __lowerCamelCase		:	str = "eval"      )	->							Optional[Any]:
       SCREAMING_SNAKE_CASE__					=					self.eval_dataset if eval_dataset is None else eval_dataset
       SCREAMING_SNAKE_CASE__					=					self.get_eval_dataloader(__lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					self.eval_examples if eval_examples is None else eval_examples

       # Temporarily disable metric computation, we will do it in the loop here.
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics
       SCREAMING_SNAKE_CASE__					=					None
       SCREAMING_SNAKE_CASE__					=					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       SCREAMING_SNAKE_CASE__					=					time.time()
       try:
            SCREAMING_SNAKE_CASE__					=					eval_loop(
                __lowerCamelCase			,     description='''Evaluation'''			,     prediction_loss_only=True if compute_metrics is None else None			,     ignore_keys=__lowerCamelCase			,     metric_key_prefix=__lowerCamelCase			,     )
       finally:
            SCREAMING_SNAKE_CASE__					=					compute_metrics
       SCREAMING_SNAKE_CASE__					=					self.args.eval_batch_size * self.args.world_size
       if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
            start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
       output.metrics.update(
           speed_metrics(
               __lowerCamelCase			,     __lowerCamelCase			,     num_samples=output.num_samples			,     num_steps=math.ceil(output.num_samples / total_batch_size      )			,     )      )
       if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
            # Only the main node write the results by default
            SCREAMING_SNAKE_CASE__					=					self.post_process_function(__lowerCamelCase			,     __lowerCamelCase			,     output.predictions      )
            SCREAMING_SNAKE_CASE__					=					self.compute_metrics(__lowerCamelCase      )

            # Prefix all keys with metric_key_prefix + '_'
            for key in list(metrics.keys()      ):
                 if not key.startswith(f'''{metric_key_prefix}_'''      ):
                      SCREAMING_SNAKE_CASE__					=					metrics.pop(__lowerCamelCase      )
            metrics.update(output.metrics      )
       else:
            SCREAMING_SNAKE_CASE__					=					output.metrics

       if self.args.should_log:
            # Only the main node log the results by default
            self.log(__lowerCamelCase      )

       if self.args.tpu_metrics_debug or self.args.debug:
            # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
            xm.master_print(met.metrics_report()      )

       SCREAMING_SNAKE_CASE__					=					self.callback_handler.on_evaluate(self.args			,     self.state			,     self.control			,     __lowerCamelCase      )
       return metrics







  def    lowercase_	(							self		:	Dict			,     __lowerCamelCase		:	Dict			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	str=None			,     __lowerCamelCase		:	str = "test"      )	->							Any:
       SCREAMING_SNAKE_CASE__					=					self.get_test_dataloader(__lowerCamelCase      )

       # Temporarily disable metric computation, we will do it in the loop here.
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics
       SCREAMING_SNAKE_CASE__					=					None
       SCREAMING_SNAKE_CASE__					=					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       SCREAMING_SNAKE_CASE__					=					time.time()
       try:
            SCREAMING_SNAKE_CASE__					=					eval_loop(
                __lowerCamelCase			,     description='''Prediction'''			,     prediction_loss_only=True if compute_metrics is None else None			,     ignore_keys=__lowerCamelCase			,     metric_key_prefix=__lowerCamelCase			,     )
       finally:
            SCREAMING_SNAKE_CASE__					=					compute_metrics
       SCREAMING_SNAKE_CASE__					=					self.args.eval_batch_size * self.args.world_size
       if f'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
            start_time += output.metrics[f'''{metric_key_prefix}_jit_compilation_time''']
       output.metrics.update(
           speed_metrics(
               __lowerCamelCase			,     __lowerCamelCase			,     num_samples=output.num_samples			,     num_steps=math.ceil(output.num_samples / total_batch_size      )			,     )      )

       if self.post_process_function is None or self.compute_metrics is None:
            return output

       SCREAMING_SNAKE_CASE__					=					self.post_process_function(__lowerCamelCase			,     __lowerCamelCase			,     output.predictions			,     '''predict'''      )
       SCREAMING_SNAKE_CASE__					=					self.compute_metrics(__lowerCamelCase      )

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()      ):
            if not key.startswith(f'''{metric_key_prefix}_'''      ):
                 SCREAMING_SNAKE_CASE__					=					metrics.pop(__lowerCamelCase      )
       metrics.update(output.metrics      )
       return PredictionOutput(predictions=predictions.predictions			,     label_ids=predictions.label_ids			,     metrics=__lowerCamelCase      )


