






'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


UpperCamelCase    :      Any  			=		logging.get_logger(__name__)




class 		UpperCamelCase__    (A_			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,**_lowerCAmelCase       ):
								lowerCamelCase__         =  feature_size
								lowerCamelCase__         =  sampling_rate
								lowerCamelCase__         =  padding_value

								lowerCamelCase__         =  kwargs.pop("""padding_side"""   ,"""right"""       )
								lowerCamelCase__         =  kwargs.pop("""return_attention_mask"""   ,_lowerCAmelCase       )

								super().__init__(**_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = True   ,_lowerCAmelCase = None   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,):
								# If we have a list of dicts, let's convert it in a dict of lists
								# We do this to allow using this method as a collate_fn function in PyTorch Dataloader
								if isinstance(_lowerCAmelCase   ,(list, tuple)       ) and isinstance(processed_features[0]   ,(dict, BatchFeature)       ):
														lowerCamelCase__         =  {
														    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
														}

								# The model's main input name, usually `input_values`, has be passed for padding
								if self.model_input_names[0] not in processed_features:
														raise ValueError(
														    """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
														    F''' to this method that includes {self.model_input_names[0]}, but you provided'''
														    F''' {list(processed_features.keys()       )}'''       )

								lowerCamelCase__         =  processed_features[self.model_input_names[0]]
								lowerCamelCase__         =  (
								    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
								)

								if len(_lowerCAmelCase       ) == 0:
														if return_attention_mask:
																				lowerCamelCase__         =  []
														return processed_features

								# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
								# and rebuild them afterwards if no return_tensors is specified
								# Note that we lose the specific device the tensor may be on for PyTorch

								lowerCamelCase__         =  required_input[0]
								if isinstance(_lowerCAmelCase   ,(list, tuple)       ):
														# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
														lowerCamelCase__         =  0
														while len(required_input[index]       ) == 0:
																				index += 1
														if index < len(_lowerCAmelCase       ):
																				lowerCamelCase__         =  required_input[index][0]

								if return_tensors is None:
														if is_tf_tensor(_lowerCAmelCase       ):
																				lowerCamelCase__         =  """tf"""
														elif is_torch_tensor(_lowerCAmelCase       ):
																				lowerCamelCase__         =  """pt"""
														elif isinstance(_lowerCAmelCase   ,(int, float, list, tuple, np.ndarray)       ):
																				lowerCamelCase__         =  """np"""
														else:
																				raise ValueError(
																				    F'''type of {first_element} unknown: {type(_lowerCAmelCase       )}. '''
																				    """Should be one of a python, numpy, pytorch or tensorflow object."""       )

								for key, value in processed_features.items():
														if isinstance(value[0]   ,(int, float)       ):
																				lowerCamelCase__         =  to_numpy(_lowerCAmelCase       )
														else:
																				lowerCamelCase__         =  [to_numpy(_lowerCAmelCase       ) for v in value]

        # Convert padding_strategy in PaddingStrategy
								lowerCamelCase__         =  self._get_padding_strategies(padding=_lowerCAmelCase   ,max_length=_lowerCAmelCase       )

								lowerCamelCase__         =  processed_features[self.model_input_names[0]]

								lowerCamelCase__         =  len(_lowerCAmelCase       )
								if not all(len(_lowerCAmelCase       ) == batch_size for v in processed_features.values()       ):
														raise ValueError("""Some items in the output dictionary have a different batch size than others."""       )

								lowerCamelCase__         =  []
								for i in range(_lowerCAmelCase       ):
														lowerCamelCase__         =  {k: v[i] for k, v in processed_features.items()}
														# truncation
														lowerCamelCase__         =  self._truncate(
														    _lowerCAmelCase   ,max_length=_lowerCAmelCase   ,pad_to_multiple_of=_lowerCAmelCase   ,truncation=_lowerCAmelCase   ,)
														truncated_inputs.append(_lowerCAmelCase       )

								if padding_strategy == PaddingStrategy.LONGEST:
														# make sure that `max_length` cannot be longer than the longest truncated length
														lowerCamelCase__         =  max(len(input_slice[self.model_input_names[0]]       ) for input_slice in truncated_inputs       )
														lowerCamelCase__         =  PaddingStrategy.MAX_LENGTH

								lowerCamelCase__         =  {}
								for i in range(_lowerCAmelCase       ):
														# padding
														lowerCamelCase__         =  self._pad(
														    truncated_inputs[i]   ,max_length=_lowerCAmelCase   ,padding_strategy=_lowerCAmelCase   ,pad_to_multiple_of=_lowerCAmelCase   ,return_attention_mask=_lowerCAmelCase   ,)

														for key, value in outputs.items():
																				if key not in batch_outputs:
																										lowerCamelCase__         =  []
																				if value.dtype is np.dtype(np.floataa       ):
																										lowerCamelCase__         =  value.astype(np.floataa       )
																				batch_outputs[key].append(_lowerCAmelCase       )

								return BatchFeature(_lowerCAmelCase   ,tensor_type=_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = None   ,_lowerCAmelCase = PaddingStrategy.DO_NOT_PAD   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,):
								lowerCamelCase__         =  processed_features[self.model_input_names[0]]

								if padding_strategy == PaddingStrategy.LONGEST:
														lowerCamelCase__         =  len(_lowerCAmelCase       )

								if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
														lowerCamelCase__         =  ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

								lowerCamelCase__         =  padding_strategy != PaddingStrategy.DO_NOT_PAD and len(_lowerCAmelCase       ) < max_length

								if return_attention_mask and "attention_mask" not in processed_features:
														lowerCamelCase__         =  np.ones(len(_lowerCAmelCase       )   ,dtype=np.intaa       )

								if needs_to_be_padded:
														lowerCamelCase__         =  max_length - len(_lowerCAmelCase       )
														if self.padding_side == "right":
																				if return_attention_mask:
																										lowerCamelCase__         =  np.pad(
																										    processed_features["""attention_mask"""]   ,(0, difference)       )
																				lowerCamelCase__         =  ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
																				lowerCamelCase__         =  np.pad(
																				    _lowerCAmelCase   ,_lowerCAmelCase   ,"""constant"""   ,constant_values=self.padding_value       )
														elif self.padding_side == "left":
																				if return_attention_mask:
																										lowerCamelCase__         =  np.pad(
																										    processed_features["""attention_mask"""]   ,(difference, 0)       )
																				lowerCamelCase__         =  ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
																				lowerCamelCase__         =  np.pad(
																				    _lowerCAmelCase   ,_lowerCAmelCase   ,"""constant"""   ,constant_values=self.padding_value       )
														else:
																				raise ValueError("""Invalid padding strategy:""" + str(self.padding_side       )       )

								return processed_features






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,_lowerCAmelCase = None   ,):
								if not truncation:
														return processed_features
								elif truncation and max_length is None:
														raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""       )

								lowerCamelCase__         =  processed_features[self.model_input_names[0]]

								# find `max_length` that fits `pad_to_multiple_of`
								if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
														lowerCamelCase__         =  ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

								lowerCamelCase__         =  len(_lowerCAmelCase       ) > max_length

								if needs_to_be_truncated:
														lowerCamelCase__         =  processed_features[self.model_input_names[0]][:max_length]
														if "attention_mask" in processed_features:
																				lowerCamelCase__         =  processed_features["""attention_mask"""][:max_length]

								return processed_features






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase=False   ,_lowerCAmelCase=None       ):

								# Get padding strategy
								if padding is not False:
														if padding is True:
																				lowerCamelCase__         =  PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
														elif not isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
																				lowerCamelCase__         =  PaddingStrategy(_lowerCAmelCase       )
														elif isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
																				lowerCamelCase__         =  padding
								else:
														lowerCamelCase__         =  PaddingStrategy.DO_NOT_PAD

								# Set max length if needed
								if max_length is None:
														if padding_strategy == PaddingStrategy.MAX_LENGTH:
																				raise ValueError(
																				    F'''When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined'''       )

        # Test if we have a padding value
								if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
														raise ValueError(
														    """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
														    """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""       )

								return padding_strategy









'''simple docstring'''







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

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


UpperCamelCase    :      Optional[int]  			=		'\n{0} = None\n'

UpperCamelCase    :      Optional[Any]  			=		'\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'


UpperCamelCase    :      Any  			=		'\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n'
def 						A__			(				__lowerCAmelCase		:			Union[str, Any]						):
						lowerCamelCase__         =  _re_backend.findall(__lowerCAmelCase						)
						if len(__lowerCAmelCase						) == 0:
												return None

						return "_and_".join(__lowerCAmelCase						)
def 						A__			(				):
						with open(os.path.join(__lowerCAmelCase   ,    """__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(__lowerCAmelCase						):
												# 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(__lowerCAmelCase						) and len(lines[line_index]						) > 1:
																								lowerCamelCase__         =  lines[line_index]
																								lowerCamelCase__         =  _re_single_line_import.search(__lowerCAmelCase						)
																								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(__lowerCAmelCase						) > 0:
																								lowerCamelCase__         =  objects
												else:
																		line_index += 1

						return backend_specific_objects
def 						A__			(				__lowerCAmelCase		:			Optional[int]   ,    __lowerCAmelCase		:			Dict						):
						if name.isupper():
												return DUMMY_CONSTANT.format(__lowerCAmelCase						)
						elif name.islower():
												return DUMMY_FUNCTION.format(__lowerCAmelCase   ,    __lowerCAmelCase						)
						else:
												return DUMMY_CLASS.format(__lowerCAmelCase   ,    __lowerCAmelCase						)
def 						A__			(				__lowerCAmelCase		:			Optional[int]=None						):
						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(__lowerCAmelCase   ,    __lowerCAmelCase						) for o in objects]						)
												lowerCamelCase__         =  dummy_file

						return dummy_files
def 						A__			(				__lowerCAmelCase		:			List[str]=False						):
						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(__lowerCAmelCase   ,    """utils"""						)
						lowerCamelCase__         =  {
						    backend: os.path.join(__lowerCAmelCase   ,    F'''dummy_{short_names.get(__lowerCAmelCase   ,    __lowerCAmelCase						)}_objects.py'''						)
						    for backend in dummy_files.keys()
						}

						lowerCamelCase__         =  {}
						for backend, file_path in dummy_file_paths.items():
												if os.path.isfile(__lowerCAmelCase						):
																		with open(__lowerCAmelCase   ,    """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(__lowerCAmelCase   ,    __lowerCAmelCase						)}_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(__lowerCAmelCase   ,    __lowerCAmelCase						)}_objects.py. Run `make fix-copies` '''
																								    """to fix this."""						)


if __name__ == "__main__":
							UpperCamelCase    :      Union[str, Any]  			=		argparse.ArgumentParser()
							parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
							UpperCamelCase    :      Any  			=		parser.parse_args()

							check_dummies(args.fix_and_overwrite)

