"""simple docstring"""







import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)


class      _lowercase						:

				def __init__(       self    :      Optional[int]    ,      _lowercase    :      int       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						foo

				def __call__(       self    :      List[Any]       ):


										'''simple docstring'''
										return self.foo


class      _lowercase						(				datasets.Dataset					):

				@fingerprint_transform(inplace=_lowercase       )
				def     A	(       self    :      Optional[int]    ,      _lowercase    :      Any    ,      *_lowercase    :      Optional[int]    ,      **_lowercase    :      Tuple       ):


										'''simple docstring'''
										return DatasetChild(self.data    ,      fingerprint=_lowercase       )

				@fingerprint_transform(inplace=_lowercase       )
				def     A	(       self    :      str    ,      _lowercase    :      List[Any]    ,      *_lowercase    :      List[str]    ,      **_lowercase    :      int       ):


										'''simple docstring'''
										return DatasetChild(self.data    ,      fingerprint=_lowercase       )


class      _lowercase						:

				def __init__(       self    :      Optional[Any]    ,      _lowercase    :      List[str]       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						callable

				def __call__(       self    :      List[Any]    ,      *_lowercase    :      Optional[Any]    ,      **_lowercase    :      int       ):


										'''simple docstring'''
										if self.callable is not None:
																return self.callable(*_lowercase    ,      **_lowercase       )

				def __getstate__(       self    :      int       ):


										'''simple docstring'''
										raise pickle.PicklingError()


class      _lowercase						(				__lowerCamelCase					):

				@require_transformers
				@pytest.mark.integration
				def     A	(       self    :      List[str]       ):


										'''simple docstring'''
										from transformers import AutoTokenizer

										def encode(_lowercase    :      Dict       ):
																return tokenizer(_lowercase       )

										# TODO: add hash consistency tests across sessions
										UpperCAmelCase__   		=						AutoTokenizer.from_pretrained("""bert-base-uncased"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(lambda _lowercase       : tokenizer(_lowercase       )       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						AutoTokenizer.from_pretrained("""bert-base-cased"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(lambda _lowercase       : tokenizer(_lowercase       )       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						AutoTokenizer.from_pretrained("""bert-base-uncased"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(lambda _lowercase       : tokenizer(_lowercase       )       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				@require_transformers
				@pytest.mark.integration
				def     A	(       self    :      int       ):


										'''simple docstring'''
										from transformers import AutoTokenizer

										UpperCAmelCase__   		=						AutoTokenizer.from_pretrained("""gpt2"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										tokenizer("""Hello world !"""       )  # call once to change the tokenizer's cache
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )

				@require_regex
				def     A	(       self    :      List[str]       ):


										'''simple docstring'''
										import regex

										UpperCAmelCase__   		=						regex.Regex("""foo"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						regex.Regex("""bar"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						regex.Regex("""foo"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )


class      _lowercase						(				__lowerCamelCase					):

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


										'''simple docstring'''
										def func():
																return foo

										UpperCAmelCase__   		=						[0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						[1]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						[0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Any       ):


										'''simple docstring'''
										def func():
																pass

										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()

										def func():
																pass

										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Optional[Any]       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([0]       )       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([1]       )       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([0]       )       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Tuple       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([0]       ).__call__       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([1]       ).__call__       )       ).hexdigest()
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(Foo([0]       ).__call__       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Optional[int]       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						(
										    """co_argcount""",
										    """co_kwonlyargcount""",
										    """co_nlocals""",
										    """co_stacksize""",
										    """co_flags""",
										    """co_code""",
										    """co_consts""",
										    """co_names""",
										    """co_varnames""",
										    """co_filename""",
										    """co_name""",
										    """co_firstlineno""",
										    """co_lnotab""",
										    """co_freevars""",
										    """co_cellvars""",
										)

										def _create_code_pyaa(*_lowercase    :      Dict       ):
																if hasattr(args[-3]    ,      """encode"""       ):
																						UpperCAmelCase__   		=						list(_lowercase       )
																						UpperCAmelCase__   		=						args[-3].encode()  # co_lnotab
																						UpperCAmelCase__   		=						args[-1_0].encode()  # co_code
																if hasattr(_lowercase    ,      """co_posonlyargcount"""       ):
																						if len(_lowercase       ) == 1_6:
																												return CodeType(*_lowercase       )
																						elif len(_lowercase       ) == 1_5:
																												return CodeType(args[0]    ,      0    ,      *args[1:]       )
																						return CodeType(args[0]    ,      0    ,      0    ,      *args[1:]       )
																elif hasattr(_lowercase    ,      """co_kwonlyargcount"""       ):
																						if len(_lowercase       ) == 1_6:
																												return CodeType(args[0]    ,      *args[2:]       )
																						elif len(_lowercase       ) == 1_5:
																												return CodeType(*_lowercase       )
																						return CodeType(args[0]    ,      0    ,      *args[1:]       )
																if len(_lowercase       ) == 1_6:
																						return CodeType(args[0]    ,      *args[3:]       )
																elif len(_lowercase       ) == 1_5:
																						return CodeType(args[0]    ,      *args[2:]       )
																return CodeType(*_lowercase       )

										def create_ipython_func(_lowercase    :      Optional[int]    ,      _lowercase    :      List[Any]       ):
																def func():
																						return returned_obj

																UpperCAmelCase__   		=						func.__code__
																# Use _create_code from dill in order to make it work for different python versions
																if datasets.config.PY_VERSION < version.parse("""3.8"""       ):
																						UpperCAmelCase__   		=						_create_code_pyaa(
																						    *[getattr(_lowercase    ,      _lowercase       ) if k != """co_filename""" else co_filename for k in code_args_pyaa]       )
																else:
																						UpperCAmelCase__   		=						code.replace(co_filename=_lowercase       )
																return FunctionType(_lowercase    ,      func.__globals__    ,      func.__name__    ,      func.__defaults__    ,      func.__closure__       )

										UpperCAmelCase__		, UpperCAmelCase__   		=						"""<ipython-input-2-e0383a102aae>""", [0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										UpperCAmelCase__		, UpperCAmelCase__   		=						"""<ipython-input-2-e0383a102aae>""", [1]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										UpperCAmelCase__		, UpperCAmelCase__   		=						"""<ipython-input-5-713f6613acf3>""", [0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

										UpperCAmelCase__		, UpperCAmelCase__   		=						os.path.join(gettempdir()    ,      """ipykernel_12345"""    ,      """321456789.py"""       ), [0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										UpperCAmelCase__		, UpperCAmelCase__   		=						os.path.join(gettempdir()    ,      """ipykernel_12345"""    ,      """321456789.py"""       ), [1]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										UpperCAmelCase__		, UpperCAmelCase__   		=						os.path.join(gettempdir()    ,      """ipykernel_12345"""    ,      """654123987.py"""       ), [0]
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(create_ipython_func(_lowercase    ,      _lowercase       )       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Any       ):


										'''simple docstring'''
										UpperCAmelCase__		, UpperCAmelCase__   		=						[0], [1]

										def func():
																return foo, bar

										UpperCAmelCase__   		=						"""__main__"""

										def globalvars_mocka_side_effect(_lowercase    :      List[Any]    ,      *_lowercase    :      Optional[Any]    ,      **_lowercase    :      List[Any]       ):
																return {"foo": foo, "bar": bar}

										def globalvars_mocka_side_effect(_lowercase    :      Union[str, Any]    ,      *_lowercase    :      Optional[Any]    ,      **_lowercase    :      Any       ):
																return {"bar": bar, "foo": foo}

										with patch("""dill.detect.globalvars"""    ,      side_effect=_lowercase       ) as globalvars_mocka:
																UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
																self.assertGreater(globalvars_mocka.call_count    ,      0       )
										with patch("""dill.detect.globalvars"""    ,      side_effect=_lowercase       ) as globalvars_mocka:
																UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
																self.assertGreater(globalvars_mocka.call_count    ,      0       )
										self.assertEqual(_lowercase    ,      _lowercase       )


class      _lowercase						(				__lowerCamelCase					):

				def     A	(       self    :      int       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						Hasher.hash("""hello"""       )
										UpperCAmelCase__   		=						Hasher.hash("""hello"""       )
										UpperCAmelCase__   		=						Hasher.hash("""there"""       )
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      str       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						Hasher.hash(Foo("""hello"""       )       )
										UpperCAmelCase__   		=						Hasher.hash(Foo("""hello"""       )       )
										UpperCAmelCase__   		=						Hasher.hash(Foo("""there"""       )       )
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      Dict       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						Hasher()
										for x in ["hello", Foo("""hello"""       )]:
																hasher.update(_lowercase       )
										UpperCAmelCase__   		=						hasher.hexdigest()
										UpperCAmelCase__   		=						Hasher()
										for x in ["hello", Foo("""hello"""       )]:
																hasher.update(_lowercase       )
										UpperCAmelCase__   		=						hasher.hexdigest()
										UpperCAmelCase__   		=						Hasher()
										for x in ["there", Foo("""there"""       )]:
																hasher.update(_lowercase       )
										UpperCAmelCase__   		=						hasher.hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				def     A	(       self    :      str       ):


										'''simple docstring'''
										with self.assertRaises(pickle.PicklingError       ):
																Hasher.hash(UnpicklableCallable(Foo("""hello"""       )       )       )

				def     A	(       self    :      Optional[int]       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						"""abc"""
										UpperCAmelCase__   		=						[string, string]  # two strings have the same ids
										UpperCAmelCase__   		=						[string, string]
										UpperCAmelCase__   		=						json.loads(f'["{string}", "{string}"]'       )  # two strings have different ids
										self.assertIs(obja[0]    ,      _lowercase       )
										self.assertIs(obja[0]    ,      obja[1]       )
										self.assertIs(obja[0]    ,      _lowercase       )
										self.assertIs(obja[0]    ,      obja[1]       )
										self.assertIsNot(obja[0]    ,      _lowercase       )
										self.assertIsNot(obja[0]    ,      obja[1]       )
										UpperCAmelCase__   		=						Hasher.hash(_lowercase       )
										UpperCAmelCase__   		=						Hasher.hash(_lowercase       )
										UpperCAmelCase__   		=						Hasher.hash(_lowercase       )
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertEqual(_lowercase    ,      _lowercase       )

				@require_tiktoken
				def     A	(       self    :      List[str]       ):


										'''simple docstring'''
										import tiktoken

										UpperCAmelCase__   		=						tiktoken.get_encoding("""gpt2"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						tiktoken.get_encoding("""r50k_base"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						tiktoken.get_encoding("""gpt2"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				@require_torch
				def     A	(       self    :      str       ):


										'''simple docstring'''
										import torch

										UpperCAmelCase__   		=						torch.tensor([1.0]       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						torch.tensor([2.0]       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						torch.tensor([1.0]       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )

				@require_spacy
				@require_spacy_model("""en_core_web_sm"""       )
				@require_spacy_model("""fr_core_news_sm"""       )
				@pytest.mark.integration
				def     A	(       self    :      Any       ):


										'''simple docstring'''
										import spacy

										UpperCAmelCase__   		=						spacy.load("""en_core_web_sm"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						spacy.load("""fr_core_news_sm"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										UpperCAmelCase__   		=						spacy.load("""en_core_web_sm"""       )
										UpperCAmelCase__   		=						mda(datasets.utils.py_utils.dumps(_lowercase       )       ).hexdigest()
										self.assertEqual(_lowercase    ,      _lowercase       )
										self.assertNotEqual(_lowercase    ,      _lowercase       )


@pytest.mark.integration
def 					a__ (						UpperCAmelCase__    :     Path     )->		Tuple:
						UpperCAmelCase__   		=						tmp_path / """dir1"""
						UpperCAmelCase__   		=						tmp_path / """dir2"""
						dira.mkdir()
						dira.mkdir()
						UpperCAmelCase__   		=						"""script.py"""
						UpperCAmelCase__   		=						dedent(
						    """
    from datasets.fingerprint import Hasher
    def foo():
        pass
    print(Hasher.hash(foo))
    """     )
						UpperCAmelCase__   		=						dira / script_filename
						UpperCAmelCase__   		=						dira / script_filename
						with script_patha.open("""w"""     ) as f:
												f.write(UpperCAmelCase__     )
						with script_patha.open("""w"""     ) as f:
												f.write(UpperCAmelCase__     )
						UpperCAmelCase__   		=						subprocess.check_output(["""python""", str(UpperCAmelCase__     )]     )
						UpperCAmelCase__   		=						subprocess.check_output(["""python""", str(UpperCAmelCase__     )]     )
						assert fingerprinta == fingerprinta


def 					a__ (						)->		Tuple:
						UpperCAmelCase__   		=						{"""a""": [0, 1, 2]}
						UpperCAmelCase__   		=						DatasetChild(InMemoryTable.from_pydict(UpperCAmelCase__     )     )
						UpperCAmelCase__   		=						dataset.funca()._fingerprint
						assert expected_fingerprint == dataset.funca()._fingerprint
						assert expected_fingerprint != dataset.funca()._fingerprint

						with Pool(2     ) as p:
												assert expected_fingerprint == p.apply_async(dataset.funca     ).get()._fingerprint
												assert expected_fingerprint != p.apply_async(dataset.funca     ).get()._fingerprint


def 					a__ (						)->		Any:
						UpperCAmelCase__   		=						{"""a""": [0, 1, 2]}


						class      _lowercase						(				datasets.Dataset					):

										@fingerprint_transform(inplace=_lowercase       )
										def     A	(       self    :      List[str]    ,      _lowercase    :      List[Any]       ):


																'''simple docstring'''
																return DummyDatasetChild(self.data    ,      fingerprint=_lowercase       )
						UpperCAmelCase__   		=						DummyDatasetChild(InMemoryTable.from_pydict(UpperCAmelCase__     )     ).func()


						class      _lowercase						(				datasets.Dataset					):

										@fingerprint_transform(inplace=_lowercase    ,      version="""1.0.0"""       )
										def     A	(       self    :      List[Any]    ,      _lowercase    :      Union[str, Any]       ):


																'''simple docstring'''
																return DummyDatasetChild(self.data    ,      fingerprint=_lowercase       )
						UpperCAmelCase__   		=						DummyDatasetChild(InMemoryTable.from_pydict(UpperCAmelCase__     )     ).func()


						class      _lowercase						(				datasets.Dataset					):

										@fingerprint_transform(inplace=_lowercase    ,      version="""2.0.0"""       )
										def     A	(       self    :      Optional[int]    ,      _lowercase    :      Dict       ):


																'''simple docstring'''
																return DummyDatasetChild(self.data    ,      fingerprint=_lowercase       )
						UpperCAmelCase__   		=						DummyDatasetChild(InMemoryTable.from_pydict(UpperCAmelCase__     )     ).func()

						assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}     ) == 3







def 					a__ (						)->		List[str]:
						# AttributeError: module 'dill._dill' has no attribute 'stack'
						UpperCAmelCase__   		=						Hasher()
						hasher.update(lambda UpperCAmelCase__     : x     )


"""simple docstring"""







import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()


class      _lowercase						(				unittest.TestCase					):

				@property
				def     A	(       self    :      Any       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						1
										UpperCAmelCase__   		=						3
										UpperCAmelCase__   		=						(3_2, 3_2)

										UpperCAmelCase__   		=						floats_tensor((batch_size, num_channels) + sizes    ,      rng=random.Random(0       )       ).to(_lowercase       )
										return image

				@property
				def     A	(       self    :      List[str]       ):


										'''simple docstring'''
										torch.manual_seed(0       )
										UpperCAmelCase__   		=						UNetaDModel(
										    block_out_channels=(3_2, 6_4)    ,      layers_per_block=2    ,      sample_size=3_2    ,      in_channels=6    ,      out_channels=3    ,      down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")    ,      up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")    ,      )
										return model

				@property
				def     A	(       self    :      Tuple       ):


										'''simple docstring'''
										torch.manual_seed(0       )
										UpperCAmelCase__   		=						VQModel(
										    block_out_channels=[3_2, 6_4]    ,      in_channels=3    ,      out_channels=3    ,      down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]    ,      up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]    ,      latent_channels=3    ,      )
										return model

				def     A	(       self    :      Any       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						"""cpu"""
										UpperCAmelCase__   		=						self.dummy_uncond_unet
										UpperCAmelCase__   		=						DDIMScheduler()
										UpperCAmelCase__   		=						self.dummy_vq_model

										UpperCAmelCase__   		=						LDMSuperResolutionPipeline(unet=_lowercase    ,      vqvae=_lowercase    ,      scheduler=_lowercase       )
										ldm.to(_lowercase       )
										ldm.set_progress_bar_config(disable=_lowercase       )

										UpperCAmelCase__   		=						self.dummy_image.to(_lowercase       )

										UpperCAmelCase__   		=						torch.Generator(device=_lowercase       ).manual_seed(0       )
										UpperCAmelCase__   		=						ldm(image=_lowercase    ,      generator=_lowercase    ,      num_inference_steps=2    ,      output_type="""numpy"""       ).images

										UpperCAmelCase__   		=						image[0, -3:, -3:, -1]

										assert image.shape == (1, 6_4, 6_4, 3)
										UpperCAmelCase__   		=						np.array([0.8678, 0.8245, 0.6381, 0.6830, 0.4385, 0.5599, 0.4641, 0.6201, 0.5150]       )

										assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

				@unittest.skipIf(torch_device != """cuda"""    ,      """This test requires a GPU"""       )
				def     A	(       self    :      Dict       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						self.dummy_uncond_unet
										UpperCAmelCase__   		=						DDIMScheduler()
										UpperCAmelCase__   		=						self.dummy_vq_model

										# put models in fp16
										UpperCAmelCase__   		=						unet.half()
										UpperCAmelCase__   		=						vqvae.half()

										UpperCAmelCase__   		=						LDMSuperResolutionPipeline(unet=_lowercase    ,      vqvae=_lowercase    ,      scheduler=_lowercase       )
										ldm.to(_lowercase       )
										ldm.set_progress_bar_config(disable=_lowercase       )

										UpperCAmelCase__   		=						self.dummy_image.to(_lowercase       )

										UpperCAmelCase__   		=						ldm(_lowercase    ,      num_inference_steps=2    ,      output_type="""numpy"""       ).images

										assert image.shape == (1, 6_4, 6_4, 3)


@slow
@require_torch
class      _lowercase						(				unittest.TestCase					):

				def     A	(       self    :      List[Any]       ):


										'''simple docstring'''
										UpperCAmelCase__   		=						load_image(
										    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
										    """/vq_diffusion/teddy_bear_pool.png"""       )
										UpperCAmelCase__   		=						init_image.resize((6_4, 6_4)    ,      resample=PIL_INTERPOLATION["""lanczos"""]       )

										UpperCAmelCase__   		=						LDMSuperResolutionPipeline.from_pretrained("""duongna/ldm-super-resolution"""    ,      device_map="""auto"""       )
										ldm.set_progress_bar_config(disable=_lowercase       )

										UpperCAmelCase__   		=						torch.manual_seed(0       )
										UpperCAmelCase__   		=						ldm(image=_lowercase    ,      generator=_lowercase    ,      num_inference_steps=2_0    ,      output_type="""numpy"""       ).images

										UpperCAmelCase__   		=						image[0, -3:, -3:, -1]

										assert image.shape == (1, 2_5_6, 2_5_6, 3)
										UpperCAmelCase__   		=						np.array([0.7644, 0.7679, 0.7642, 0.7633, 0.7666, 0.7560, 0.7425, 0.7257, 0.6907]       )

										assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2

