



import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()


class 	lowerCamelCase__	(       _A							,  _A							,  unittest.TestCase):



 """simple docstring"""

 a__					: Optional[Any] =  VQModel
 a__					: Optional[int] =  "sample"

 @property
 def     snake_case_      (    self					:					Optional[int]			,						__lowerCAmelCase					:					Any=(32, 32)      )   ->	int:
    _A      =       4
    _A      =       3

    _A      =       floats_tensor((batch_size, num_channels) + sizes      ).to(__lowerCAmelCase      )

    return {"sample": image}

 @property
 def     snake_case_      (    self					:					Optional[Any]      )   ->	List[Any]:
    return (3, 32, 32)

 @property
 def     snake_case_      (    self					:					Tuple      )   ->	Tuple:
    return (3, 32, 32)

 def     snake_case_      (    self					:					Any      )   ->	Tuple:
    _A      =       {
        '''block_out_channels''': [32, 64],
        '''in_channels''': 3,
        '''out_channels''': 3,
        '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
        '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
        '''latent_channels''': 3,
    }
    _A      =       self.dummy_input
    return init_dict, inputs_dict

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Tuple:
    pass

 def     snake_case_      (    self					:					Any      )   ->	Optional[int]:
    pass

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Any:
    _A  ,    _A      =       VQModel.from_pretrained('''fusing/vqgan-dummy'''			,						output_loading_info=__lowerCAmelCase      )
    self.assertIsNotNone(__lowerCAmelCase      )
    self.assertEqual(len(loading_info['''missing_keys''']      )			,						0      )

    model.to(__lowerCAmelCase      )
    _A      =       model(**self.dummy_input      )

    assert image is not None, "Make sure output is not None"

 def     snake_case_      (    self					:					List[Any]      )   ->	Optional[Any]:
    _A      =       VQModel.from_pretrained('''fusing/vqgan-dummy'''      )
    model.to(__lowerCAmelCase      ).eval()

    torch.manual_seed(0      )
    if torch.cuda.is_available():
       torch.cuda.manual_seed_all(0      )

    _A      =       torch.randn(1			,						model.config.in_channels			,						model.config.sample_size			,						model.config.sample_size      )
    _A      =       image.to(__lowerCAmelCase      )
    with torch.no_grad():
       _A      =       model(__lowerCAmelCase      ).sample

    _A      =       output[0, -1, -3:, -3:].flatten().cpu()
    # fmt: off
    _A      =       torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143]      )
    # fmt: on
    self.assertTrue(torch.allclose(__lowerCAmelCase			,						__lowerCAmelCase			,						atol=1E-3      )      )




'''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    lowerCAmelCase       (		UpperCamelCase__					: Dict							):


	"""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(UpperCamelCase__							)

	return test_module_path





def    lowerCAmelCase       (		UpperCamelCase__					: int							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_module_path(UpperCamelCase__							)
	__UpperCAmelCase      					=      importlib.import_module(UpperCamelCase__							)

	return test_module





def    lowerCAmelCase       (		UpperCamelCase__					: Tuple							):


	"""simple docstring"""
	__UpperCAmelCase      					=      []
	__UpperCAmelCase      					=      get_test_module(UpperCamelCase__							)
	for attr in dir(UpperCamelCase__							):
		if attr.endswith('''ModelTester'''							):
			tester_classes.append(getattr(UpperCamelCase__     ,       UpperCamelCase__							)							)

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





def    lowerCAmelCase       (		UpperCamelCase__					: List[Any]							):


	"""simple docstring"""
	__UpperCAmelCase      					=      []
	__UpperCAmelCase      					=      get_test_module(UpperCamelCase__							)
	for attr in dir(UpperCamelCase__							):
		__UpperCAmelCase      					=      getattr(UpperCamelCase__     ,       UpperCamelCase__							)
		# (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(UpperCamelCase__     ,       '''all_model_classes'''     ,       []							)
		if len(UpperCamelCase__							) > 0:
			test_classes.append(UpperCamelCase__							)

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





def    lowerCAmelCase       (		UpperCamelCase__					: str							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_test_classes(UpperCamelCase__							)
	__UpperCAmelCase      					=      set()
	for test_class in test_classes:
		model_classes.update(test_class.all_model_classes							)

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





def    lowerCAmelCase       (		UpperCamelCase__					: Any							):


	"""simple docstring"""
	__UpperCAmelCase      					=      test_class()
	if hasattr(UpperCamelCase__     ,       '''setUp'''							):
		test.setUp()

	__UpperCAmelCase      					=      None
	if hasattr(UpperCamelCase__     ,       '''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    lowerCAmelCase       (		UpperCamelCase__					: Dict     ,       UpperCamelCase__					: int							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_test_classes(UpperCamelCase__							)

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

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





def    lowerCAmelCase       (		UpperCamelCase__					: Tuple     ,       UpperCamelCase__					: Any							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_test_classes_for_model(UpperCamelCase__     ,       UpperCamelCase__							)

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

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





def    lowerCAmelCase       (		UpperCamelCase__					: int							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_test_classes(UpperCamelCase__							)
	__UpperCAmelCase      					=      {test_class: get_model_tester_from_test_class(UpperCamelCase__							) for test_class in test_classes}
	return test_tester_mapping





def    lowerCAmelCase       (		UpperCamelCase__					: str							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_model_classes(UpperCamelCase__							)
	__UpperCAmelCase      					=      {
	    model_class: get_test_classes_for_model(UpperCamelCase__     ,       UpperCamelCase__							) for model_class in model_classes
	}
	return model_test_mapping





def    lowerCAmelCase       (		UpperCamelCase__					: Optional[int]							):


	"""simple docstring"""
	__UpperCAmelCase      					=      get_model_classes(UpperCamelCase__							)
	__UpperCAmelCase      					=      {
	    model_class: get_tester_classes_for_model(UpperCamelCase__     ,       UpperCamelCase__							) for model_class in model_classes
	}
	return model_to_tester_mapping





def    lowerCAmelCase       (		UpperCamelCase__					: Tuple							):


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