






"""simple docstring"""



import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor






class 				__SCREAMING_SNAKE_CASE    (					unittest.TestCase		):


							def     _UpperCamelCase							(      self   :    Any      ,     snake_case   :    bool = False				):
													'''simple docstring'''





													A__    :	Optional[Any]			=	10

													if only_cross_attention:
																			A__    :	Dict			=	12
													else:
																			# when only cross attention is not set, the cross attention dim must be the same as the query dim
																			A__    :	str			=	query_dim

													return {
													    "query_dim": query_dim,
													    "cross_attention_dim": cross_attention_dim,
													    "heads": 2,
													    "dim_head": 4,
													    "added_kv_proj_dim": 6,
													    "norm_num_groups": 1,
													    "only_cross_attention": only_cross_attention,
													    "processor": AttnAddedKVProcessor(),
													}


							def     _UpperCamelCase							(      self   :    str      ,     snake_case   :    Dict      ,     snake_case   :    Tuple				):
													'''simple docstring'''





													A__    :	int			=	2

													A__    :	int			=	torch.rand(snake_case      ,     snake_case      ,     3      ,     2				)
													A__    :	Optional[int]			=	torch.rand(snake_case      ,     4      ,     snake_case				)
													A__    :	Tuple			=	None

													return {
													    "hidden_states": hidden_states,
													    "encoder_hidden_states": encoder_hidden_states,
													    "attention_mask": attention_mask,
													}


							def     _UpperCamelCase							(      self   :    Dict				):
													'''simple docstring'''





													torch.manual_seed(0				)

													A__    :	Union[str, Any]			=	self.get_constructor_arguments(only_cross_attention=snake_case				)
													A__    :	int			=	Attention(**snake_case				)

													self.assertTrue(attn.to_k is not None				)
													self.assertTrue(attn.to_v is not None				)

													A__    :	List[Any]			=	self.get_forward_arguments(
													    query_dim=constructor_args["""query_dim"""]      ,     added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]				)

													A__    :	str			=	attn(**snake_case				)

													# only self attention

													torch.manual_seed(0				)

													A__    :	Optional[int]			=	self.get_constructor_arguments(only_cross_attention=snake_case				)
													A__    :	List[Any]			=	Attention(**snake_case				)

													self.assertTrue(attn.to_k is None				)
													self.assertTrue(attn.to_v is None				)

													A__    :	Optional[int]			=	self.get_forward_arguments(
													    query_dim=constructor_args["""query_dim"""]      ,     added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]				)

													A__    :	List[str]			=	attn(**snake_case				)

													self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()				)






class 				__SCREAMING_SNAKE_CASE    (					unittest.TestCase		):


							def     _UpperCamelCase							(      self   :    str				):
													'''simple docstring'''





													A__    :	str			=	DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,     safety_checker=snake_case				)

													A__    :	List[str]			=	pipe(
													    """foo"""      ,     num_inference_steps=2      ,     generator=torch.Generator("""cpu"""				).manual_seed(0				)      ,     output_type="""np"""      ,     ).images

													# the initial conversion succeeds
													A__    :	Any			=	DiffusionPipeline.from_pretrained(
													    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,     device_map="""sequential"""      ,     safety_checker=snake_case				)

													A__    :	Any			=	pipe(
													    """foo"""      ,     num_inference_steps=2      ,     generator=torch.Generator("""cpu"""				).manual_seed(0				)      ,     output_type="""np"""      ,     ).images

													with tempfile.TemporaryDirectory() as tmpdir:
																			# save the converted model
																			pipe.save_pretrained(snake_case				)

																			# can also load the converted weights
																			A__    :	Dict			=	DiffusionPipeline.from_pretrained(snake_case      ,     device_map="""sequential"""      ,     safety_checker=snake_case				)

													A__    :	str			=	pipe(
													    """foo"""      ,     num_inference_steps=2      ,     generator=torch.Generator("""cpu"""				).manual_seed(0				)      ,     output_type="""np"""      ,     ).images

													self.assertTrue(np.allclose(snake_case      ,     snake_case      ,     atol=1e-5				)				)
													self.assertTrue(np.allclose(snake_case      ,     snake_case      ,     atol=1e-5				)				)














"""simple docstring"""



import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


A_     =				get_logger(__name__)


if TYPE_CHECKING:
						from .builder import DatasetBuilder




def   _lowerCAmelCase		(				UpperCAmelCase__							:       Optional[int],    UpperCAmelCase__							:       Optional[Union[str, bool]] = None   )		->Optional[Any]:
						A__    :	int			=	importlib.import_module(UpperCAmelCase__   )

						# TODO(QL): always update the module to add subsequent new authentication
						if hasattr(UpperCAmelCase__,    """_patched_for_streaming"""   ) and module._patched_for_streaming:
												return

						def wrap_auth(UpperCAmelCase__							:       List[str]   ):
												@wraps(UpperCAmelCase__   )
												def wrapper(*UpperCAmelCase__							:       Dict,    **UpperCAmelCase__							:       Dict   ):
																		return function(*UpperCAmelCase__,    use_auth_token=UpperCAmelCase__,    **UpperCAmelCase__   )

												A__    :	List[Any]			=	"""wrap_auth"""
												return wrapper

						# open files in a streaming fashion
						patch_submodule(UpperCAmelCase__,    """open""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.listdir""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.walk""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """glob.glob""",    wrap_auth(UpperCAmelCase__   )   ).start()
						# allow to navigate in remote zip files
						patch_submodule(UpperCAmelCase__,    """os.path.join""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.dirname""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.basename""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.relpath""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.split""",    UpperCAmelCase__   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.splitext""",    UpperCAmelCase__   ).start()
						# allow checks on paths
						patch_submodule(UpperCAmelCase__,    """os.path.exists""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.isdir""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.isfile""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """os.path.getsize""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """pathlib.Path""",    UpperCAmelCase__   ).start()
						# file readers
						patch_submodule(UpperCAmelCase__,    """gzip.open""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """numpy.load""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """pandas.read_csv""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """pandas.read_excel""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """scipy.io.loadmat""",    wrap_auth(UpperCAmelCase__   ),    attrs=["""__version__"""]   ).start()
						patch_submodule(UpperCAmelCase__,    """xml.etree.ElementTree.parse""",    wrap_auth(UpperCAmelCase__   )   ).start()
						patch_submodule(UpperCAmelCase__,    """xml.dom.minidom.parse""",    wrap_auth(UpperCAmelCase__   )   ).start()
						A__    :	Any			=	True




def   _lowerCAmelCase		(				UpperCAmelCase__							:       "DatasetBuilder"   )		->Tuple:
						extend_module_for_streaming(builder.__module__,    use_auth_token=builder.use_auth_token   )
						# if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
						if not builder.__module__.startswith("""datasets."""   ):  # check that it's not a packaged builder like csv
												for imports in get_imports(inspect.getfile(builder.__class__   )   ):
																		if imports[0] == "internal":
																								A__    :	Any			=	imports[1]
																								A__    :	Any			=	""".""".join(builder.__module__.split("""."""   )[:-1] + [internal_import_name]   )
																								extend_module_for_streaming(UpperCAmelCase__,    use_auth_token=builder.use_auth_token   )

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
						from .builder import DatasetBuilder

						A__    :	Any			=	[
						    cls.__module__
						    for cls in type(UpperCAmelCase__   ).__mro__[1:]  # make sure it's not the same module we've already patched
						    if issubclass(cls,    UpperCAmelCase__   ) and cls.__module__ != DatasetBuilder.__module__
						]  # check it's not a standard builder from datasets.builder
						for module in parent_builder_modules:
												extend_module_for_streaming(UpperCAmelCase__,    use_auth_token=builder.use_auth_token   )






