





"""simple docstring"""





from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer





@dataclass
class        _lowerCamelCase      (       _lowercase ):
						UpperCAmelCase_ =			42





class        _lowerCamelCase      (       _lowercase  ,			_lowercase ):


						@register_to_config
						def __init__(self   ,							__a = 3   ,							__a = 3   ,							__a = ("DownEncoderBlock2D",)   ,							__a = ("UpDecoderBlock2D",)   ,							__a = (64,)   ,							__a = 1   ,							__a = "silu"   ,							__a = 3   ,							__a = 32   ,							__a = 2_56   ,							__a = 32   ,							__a = None   ,							__a = 0.18215   ,							__a = "group"   ,							) ->   List[str]:
								super().__init__()

								# pass init params to Encoder
								UpperCamelCase   =	Encoder(
								    in_channels=__a   ,							out_channels=__a   ,							down_block_types=__a   ,							block_out_channels=__a   ,							layers_per_block=__a   ,							act_fn=__a   ,							norm_num_groups=__a   ,							double_z=__a   ,							)

								UpperCamelCase   =	vq_embed_dim if vq_embed_dim is not None else latent_channels

								UpperCamelCase   =	nn.Convad(__a   ,							__a   ,							1		)
								UpperCamelCase   =	VectorQuantizer(__a   ,							__a   ,							beta=0.25   ,							remap=__a   ,							sane_index_shape=__a		)
								UpperCamelCase   =	nn.Convad(__a   ,							__a   ,							1		)

								# pass init params to Decoder
								UpperCamelCase   =	Decoder(
								    in_channels=__a   ,							out_channels=__a   ,							up_block_types=__a   ,							block_out_channels=__a   ,							layers_per_block=__a   ,							act_fn=__a   ,							norm_num_groups=__a   ,							norm_type=__a   ,							)


						@apply_forward_hook
						def     snake_case_   (self   ,							__a   ,							__a = True		) ->   VQEncoderOutput:
								UpperCamelCase   =	self.encoder(__a		)
								UpperCamelCase   =	self.quant_conv(__a		)

								if not return_dict:
										return (h,)

								return VQEncoderOutput(latents=__a		)


						@apply_forward_hook
						def     snake_case_   (self   ,							__a   ,							__a = False   ,							__a = True		) ->   Union[DecoderOutput, torch.FloatTensor]:
								# also go through quantization layer
								if not force_not_quantize:
										UpperCamelCase   ,				UpperCamelCase   ,				UpperCamelCase   =	self.quantize(__a		)
								else:
										UpperCamelCase   =	h
								UpperCamelCase   =	self.post_quant_conv(__a		)
								UpperCamelCase   =	self.decoder(__a   ,							quant if self.config.norm_type == "spatial" else None		)

								if not return_dict:
										return (dec,)

								return DecoderOutput(sample=__a		)




						def     snake_case_   (self   ,							__a   ,							__a = True		) ->   Union[DecoderOutput, torch.FloatTensor]:
								UpperCamelCase   =	sample
								UpperCamelCase   =	self.encode(__a		).latents
								UpperCamelCase   =	self.decode(__a		).sample

								if not return_dict:
										return (dec,)

								return DecoderOutput(sample=__a		)













"""simple docstring"""





import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('''.''')


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	test_file.split(os.path.sep    )
		if components[0:2] != ["tests", "models"]:
				raise ValueError(
				    "`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got "
				    F"{test_file} instead."    )
		UpperCamelCase   =	components[-1]
		if not test_fn.endswith("py"    ):
				raise ValueError(F"`test_file` should be a python file. Got {test_fn} instead."    )
		if not test_fn.startswith("test_modeling_"    ):
				raise ValueError(
				    F"`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."    )

		UpperCamelCase   =	components[:-1] + [test_fn.replace(".py"  ,	""    )]
		UpperCamelCase   =	".".join(_SCREAMING_SNAKE_CASE    )

		return test_module_path


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_module_path(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	importlib.import_module(_SCREAMING_SNAKE_CASE    )

		return test_module


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	[]
		UpperCamelCase   =	get_test_module(_SCREAMING_SNAKE_CASE    )
		for attr in dir(_SCREAMING_SNAKE_CASE    ):
				if attr.endswith("ModelTester"    ):
						tester_classes.append(getattr(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	[]
		UpperCamelCase   =	get_test_module(_SCREAMING_SNAKE_CASE    )
		for attr in dir(_SCREAMING_SNAKE_CASE    ):
				UpperCamelCase   =	getattr(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )
				# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
				# `all_model_classes` is not empty (which also excludes other special classes).
				UpperCamelCase   =	getattr(_SCREAMING_SNAKE_CASE  ,	"all_model_classes"  ,	[]    )
				if len(_SCREAMING_SNAKE_CASE    ) > 0:
						test_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	set()
		for test_class in test_classes:
				model_classes.update(test_class.all_model_classes    )

		# sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	test_class()
		if hasattr(_SCREAMING_SNAKE_CASE  ,	"setUp"    ):
				test.setUp()

		UpperCamelCase   =	None
		if hasattr(_SCREAMING_SNAKE_CASE  ,	"model_tester"    ):
				# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
				if test.model_tester is not None:
						UpperCamelCase   =	test.model_tester.__class__

		return model_tester


def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )

		UpperCamelCase   =	[]
		for test_class in test_classes:
				if model_class in test_class.all_model_classes:
						target_test_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    )

		UpperCamelCase   =	[]
		for test_class in test_classes:
				UpperCamelCase   =	get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE    )
				if tester_class is not None:
						tester_classes.append(_SCREAMING_SNAKE_CASE    )

    # sort with class names
		return sorted(_SCREAMING_SNAKE_CASE  ,	key=lambda _SCREAMING_SNAKE_CASE    : x.__name__    )


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_test_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{test_class: get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE    ) for test_class in test_classes}
		return test_tester_mapping


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_model_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{
		    model_class: get_test_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ) for model_class in model_classes
		}
		return model_test_mapping


def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		UpperCamelCase   =	get_model_classes(_SCREAMING_SNAKE_CASE    )
		UpperCamelCase   =	{
		    model_class: get_tester_classes_for_model(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ) for model_class in model_classes
		}
		return model_to_tester_mapping






def       a__					(					_SCREAMING_SNAKE_CASE    ):




		"""simple docstring"""


		if isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return o
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return o.__name__
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	(list, tuple)    ):
				return [to_json(_SCREAMING_SNAKE_CASE    ) for x in o]
		elif isinstance(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE    ):
				return {to_json(_SCREAMING_SNAKE_CASE    ): to_json(_SCREAMING_SNAKE_CASE    ) for k, v in o.items()}
		else:
				return o






