





import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch


@require_torch
class   A			:
			'''simple docstring'''




			@property
			def    a_       (			self   :    int   )	->	Optional[Any]:







					"""simple docstring"""






					return self.get_dummy_input()






			@property
			def    a_       (			self   :    Union[str, Any]   )	->	Any:







					"""simple docstring"""






					if self.block_type == "down":
							return (4, 32, 16, 16)
					elif self.block_type == "mid":
							return (4, 32, 32, 32)
					elif self.block_type == "up":
							return (4, 32, 64, 64)

					raise ValueError(f'\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'   )






			def    a_       (			self   :    Any							,	__lowerCAmelCase   :    Optional[Any]=True							,	__lowerCAmelCase   :    str=False							,	__lowerCAmelCase   :    str=False							,	__lowerCAmelCase   :    str=False							,	)	->	Tuple:







					"""simple docstring"""






					A__        =   4
					A__        =   32
					A__        =   (32, 32)

					A__        =   torch.manual_seed(0   )
					A__        =   torch.device(__lowerCAmelCase   )
					A__        =   (batch_size, num_channels) + sizes
					A__        =   randn_tensor(__lowerCAmelCase							,	generator=__lowerCAmelCase							,	device=__lowerCAmelCase   )
					A__        =   {"""hidden_states""": hidden_states}

					if include_temb:
							A__        =   1_28
							A__        =   randn_tensor((batch_size, temb_channels)							,	generator=__lowerCAmelCase							,	device=__lowerCAmelCase   )

					if include_res_hidden_states_tuple:
							A__        =   torch.manual_seed(1   )
							A__        =   (randn_tensor(__lowerCAmelCase							,	generator=__lowerCAmelCase							,	device=__lowerCAmelCase   ),)

					if include_encoder_hidden_states:
							A__        =   floats_tensor((batch_size, 32, 32)   ).to(__lowerCAmelCase   )

					if include_skip_sample:
							A__        =   randn_tensor(((batch_size, 3) + sizes)							,	generator=__lowerCAmelCase							,	device=__lowerCAmelCase   )

					return dummy_input






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







					"""simple docstring"""






					A__        =   {
					    """in_channels""": 32,
					    """out_channels""": 32,
					    """temb_channels""": 1_28,
					}
					if self.block_type == "up":
							A__        =   32

					if self.block_type == "mid":
							init_dict.pop("""out_channels"""   )

					A__        =   self.dummy_input
					return init_dict, inputs_dict






			def    a_       (			self   :    Tuple							,	__lowerCAmelCase   :    Optional[Any]   )	->	Optional[int]:







					"""simple docstring"""






					A__      ,			A__        =   self.prepare_init_args_and_inputs_for_common()
					A__        =   self.block_class(**__lowerCAmelCase   )
					unet_block.to(__lowerCAmelCase   )
					unet_block.eval()

					with torch.no_grad():
							A__        =   unet_block(**__lowerCAmelCase   )

					if isinstance(__lowerCAmelCase							,	__lowerCAmelCase   ):
							A__        =   output[0]

					self.assertEqual(output.shape							,	self.output_shape   )

					A__        =   output[0, -1, -3:, -3:]
					A__        =   torch.tensor(__lowerCAmelCase   ).to(__lowerCAmelCase   )
					assert torch_all_close(output_slice.flatten()							,	__lowerCAmelCase							,	atol=5e-3   )







			@unittest.skipIf(torch_device == """mps"""							,	"""Training is not supported in mps"""   )
			def    a_       (			self   :    Tuple   )	->	Any:







					"""simple docstring"""






					A__      ,			A__        =   self.prepare_init_args_and_inputs_for_common()
					A__        =   self.block_class(**__lowerCAmelCase   )
					model.to(__lowerCAmelCase   )
					model.train()
					A__        =   model(**__lowerCAmelCase   )

					if isinstance(__lowerCAmelCase							,	__lowerCAmelCase   ):
							A__        =   output[0]

					A__        =   torch.device(__lowerCAmelCase   )
					A__        =   randn_tensor(output.shape							,	device=__lowerCAmelCase   )
					A__        =   torch.nn.functional.mse_loss(__lowerCAmelCase							,	__lowerCAmelCase   )
					loss.backward()










import argparse
from collections import defaultdict

import yaml


A    :					str =      '''docs/source/en/_toctree.yml'''







def    __lowerCamelCase					(							__a   :str    )		->				List[Any]:






		"""simple docstring"""

		A__        =   defaultdict(__a    )
		A__        =   []
		A__        =   []
		for doc in doc_list:
				if "local" in doc:
						counts[doc["local"]] += 1

				if doc["title"].lower() == "overview":
						overview_doc.append({"""local""": doc["""local"""], """title""": doc["""title"""]}    )
				else:
						new_doc_list.append(__a    )

		A__        =   new_doc_list
		A__        =   [key for key, value in counts.items() if value > 1]

		A__        =   []
		for duplicate_key in duplicates:
				A__        =   list({doc["""title"""] for doc in doc_list if doc["""local"""] == duplicate_key}    )
				if len(__a    ) > 1:
						raise ValueError(
						    F'{duplicate_key} is present several times in the documentation table of content at '
						    """`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the """
						    """others."""    )
				# Only add this once
				new_doc.append({"""local""": duplicate_key, """title""": titles[0]}    )

		# Add none duplicate-keys
		new_doc.extend([doc for doc in doc_list if """local""" not in counts or counts[doc["""local"""]] == 1]    )
		A__        =   sorted(__a    ,     key=lambda __a    : s["title"].lower()    )

		# "overview" gets special treatment and is always first
		if len(__a    ) > 1:
				raise ValueError("""{doc_list} has two 'overview' docs which is not allowed."""    )

		overview_doc.extend(__a    )

		# Sort
		return overview_doc







def    __lowerCamelCase					(							__a   :Any=False    )		->				List[str]:






		"""simple docstring"""

		with open(__a    ,     encoding="""utf-8"""    ) as f:
				A__        =   yaml.safe_load(f.read()    )

		# Get to the API doc
		A__        =   0
		while content[api_idx]["title"] != "API":
				api_idx += 1
		A__        =   content[api_idx]["""sections"""]

		# Then to the model doc
		A__        =   0
		while api_doc[scheduler_idx]["title"] != "Schedulers":
				scheduler_idx += 1

		A__        =   api_doc[scheduler_idx]["""sections"""]
		A__        =   clean_doc_toc(__a    )

		A__        =   False
		if new_scheduler_doc != scheduler_doc:
				A__        =   True
				if overwrite:
						A__        =   new_scheduler_doc

		if diff:
				if overwrite:
						A__        =   api_doc
						with open(__a    ,     """w"""    ,     encoding="""utf-8"""    ) as f:
								f.write(yaml.dump(__a    ,     allow_unicode=__a    )    )
				else:
						raise ValueError(
						    """The model doc part of the table of content is not properly sorted, run `make style` to fix this."""    )







def    __lowerCamelCase					(							__a   :Optional[int]=False    )		->				Dict:






		"""simple docstring"""

		with open(__a    ,     encoding="""utf-8"""    ) as f:
				A__        =   yaml.safe_load(f.read()    )

		# Get to the API doc
		A__        =   0
		while content[api_idx]["title"] != "API":
				api_idx += 1
		A__        =   content[api_idx]["""sections"""]

		# Then to the model doc
		A__        =   0
		while api_doc[pipeline_idx]["title"] != "Pipelines":
				pipeline_idx += 1

		A__        =   False
		A__        =   api_doc[pipeline_idx]["""sections"""]
		A__        =   []

		# sort sub pipeline docs
		for pipeline_doc in pipeline_docs:
				if "section" in pipeline_doc:
						A__        =   pipeline_doc["""section"""]
						A__        =   clean_doc_toc(__a    )
						if overwrite:
								A__        =   new_sub_pipeline_doc
				new_pipeline_docs.append(__a    )

		# sort overall pipeline doc
		A__        =   clean_doc_toc(__a    )

		if new_pipeline_docs != pipeline_docs:
				A__        =   True
				if overwrite:
						A__        =   new_pipeline_docs

		if diff:
				if overwrite:
						A__        =   api_doc
						with open(__a    ,     """w"""    ,     encoding="""utf-8"""    ) as f:
								f.write(yaml.dump(__a    ,     allow_unicode=__a    )    )
				else:
						raise ValueError(
						    """The model doc part of the table of content is not properly sorted, run `make style` to fix this."""    )


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

					check_scheduler_doc(args.fix_and_overwrite)
					check_pipeline_doc(args.fix_and_overwrite)



