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 					lowercase__						(   UpperCamelCase_	,							UpperCamelCase_	,							unittest.TestCase):
						UpperCamelCase_     =		VQModel
						UpperCamelCase_     =		"""sample"""


						@property
						def        __A       (       self	:				Tuple    ,		UpperCamelCase__	:				Any=(32, 32)						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							4
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							3

												SCREAMING_SNAKE_CASE							:    Tuple							=							floats_tensor((batch_size, num_channels) + sizes						).to(UpperCamelCase__						)

												return {"sample": image}


						@property
						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												return (3, 32, 32)


						@property
						def        __A       (       self	:				Union[str, Any]						):





												'''simple docstring'''





												return (3, 32, 32)


						def        __A       (       self	:				Union[str, Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							{
												    '''block_out_channels''': [32, 64],
												    '''in_channels''': 3,
												    '''out_channels''': 3,
												    '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
												    '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
												    '''latent_channels''': 3,
												}
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.dummy_input
												return init_dict, inputs_dict


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												pass


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												pass


						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Tuple							=							VQModel.from_pretrained('''fusing/vqgan-dummy'''    ,		output_loading_info=UpperCamelCase__						)
												self.assertIsNotNone(UpperCamelCase__						)
												self.assertEqual(len(loading_info['''missing_keys''']						)    ,		0						)

												model.to(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							model(**self.dummy_input						)

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



						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							VQModel.from_pretrained('''fusing/vqgan-dummy'''						)
												model.to(UpperCamelCase__						).eval()

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

												SCREAMING_SNAKE_CASE							:    Any							=							torch.randn(1    ,		model.config.in_channels    ,		model.config.sample_size    ,		model.config.sample_size						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							image.to(UpperCamelCase__						)
												with torch.no_grad():
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							model(UpperCamelCase__						).sample

												SCREAMING_SNAKE_CASE							:    Dict							=							output[0, -1, -3:, -3:].flatten().cpu()
												# fmt: off
												SCREAMING_SNAKE_CASE							:    Dict							=							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(UpperCamelCase__    ,		UpperCamelCase__    ,		atol=1E-3						)						)






import pytest

import datasets.config
from datasets.utils.info_utils import is_small_dataset
@pytest.mark.parametrize('''dataset_size'''		,  [None, 400 * 2**20, 600 * 2**20]						)
@pytest.mark.parametrize('''input_in_memory_max_size'''		,  ['''default''', 0, 100 * 2**20, 900 * 2**20]						)
def       A	(    _lowercase		,  _lowercase		,  _lowercase						):
						if input_in_memory_max_size != "default":
												monkeypatch.setattr(datasets.config		,  '''IN_MEMORY_MAX_SIZE'''		,  _lowercase						)
						SCREAMING_SNAKE_CASE							:    Tuple							=							datasets.config.IN_MEMORY_MAX_SIZE
						if input_in_memory_max_size == "default":
												assert in_memory_max_size == 0
						else:
												assert in_memory_max_size == input_in_memory_max_size
						if dataset_size and in_memory_max_size:
												SCREAMING_SNAKE_CASE							:    str							=							dataset_size < in_memory_max_size
						else:
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							False
						SCREAMING_SNAKE_CASE							:    Any							=							is_small_dataset(_lowercase						)
						assert result == expected





