





import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_SCREAMING_SNAKE_CASE 		=						logging.get_logger(__name__)
class SCREAMING_SNAKE_CASE_  :
						__magic_name__:     Dict	 =    None


@experimental
def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a				,	__a				,	__a				,	__a				,	__a				):
	if ParallelBackendConfig.backend_name is None:
		return _map_with_multiprocessing_pool(
		    __a				,	__a				,	__a				,	__a				,	__a				,	__a				,	__a				)

	return _map_with_joblib(__a				,	__a				,	__a				,	__a				,	__a				,	__a				,	__a				)


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a				,	__a				,	__a				,	__a				,	__a				):
	snake_case_						:     Optional[Any]     =		num_proc if num_proc <= len(__a				) else len(__a				)
	snake_case_						:     Tuple     =		[]  # We organize the splits ourselve (contiguous splits)
	for index in range(__a				):
		snake_case_						:     List[str]     =		len(__a				) // num_proc
		snake_case_						:     int     =		len(__a				) % num_proc
		snake_case_						:     List[str]     =		div * index + min(__a				,	__a				)
		snake_case_						:     str     =		start + div + (1 if index < mod else 0)
		split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc)				)

	if len(__a				) != sum(len(i[1]				) for i in split_kwds				):
		raise ValueError(
		    f"""Error dividing inputs iterable among processes. """
		    f"""Total number of objects {len(__a				)}, """
		    f"""length: {sum(len(i[1]				) for i in split_kwds				)}"""				)

	logger.info(
	    f"""Spawning {num_proc} processes for {len(__a				)} objects in slices of {[len(i[1]				) for i in split_kwds]}"""				)
	snake_case_  ,snake_case_						:     Optional[int]     =		None, None
	if not disable_tqdm:
		snake_case_  ,snake_case_						:     Optional[Any]     =		(RLock(),), tqdm.set_lock
	with Pool(__a				,	initargs=__a				,	initializer=__a				) as pool:
		snake_case_						:     Dict     =		pool.map(__a				,	__a				)
	logger.info(f"""Finished {num_proc} processes"""				)
	snake_case_						:     Any     =		[obj for proc_res in mapped for obj in proc_res]
	logger.info(f"""Unpacked {len(__a				)} objects"""				)

	return mapped


def 							SCREAMING_SNAKE_CASE__  (      __a				,	__a				,	__a				,	__a				,	__a				,	__a				,	__a				):
	# progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib,
	# and it requires monkey-patching joblib internal classes which is subject to change
	import joblib

	with joblib.parallel_backend(ParallelBackendConfig.backend_name				,	n_jobs=__a				):
		return joblib.Parallel()(
		    joblib.delayed(__a				)((function, obj, types, None, True, None)				) for obj in iterable				)


@experimental
@contextlib.contextmanager
def 							SCREAMING_SNAKE_CASE__  (      __a				):
	snake_case_						:     Any     =		backend_name

	if backend_name == "spark":
		from joblibspark import register_spark

		register_spark()

		# TODO: call create_cache_and_write_probe if "download" in steps
		# TODO: raise NotImplementedError when Dataset.map etc is called

	try:
		yield
	finally:
		snake_case_						:     Tuple     =		None








import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin
class SCREAMING_SNAKE_CASE_  (   snake_case_    ,			snake_case_   ):






						@register_to_config
						def __init__( self : Optional[Any]						,				*,
						    _A : int = 4						,				_A : int = 768						,				_A : int						,				_A : Tuple						,				)							->      Dict:







							"""simple docstring"""


							super().__init__()

							snake_case_						:     int     =		nn.Parameter(torch.zeros(_A		)		)

							# parameters for additional clip time embeddings
							snake_case_						:     Tuple     =		nn.Linear(_A						,				_A		)
							snake_case_						:     List[Any]     =		nn.Linear(_A						,				_A		)

							# parameters for encoder hidden states
							snake_case_						:     Union[str, Any]     =		clip_extra_context_tokens
							snake_case_						:     str     =		nn.Linear(
							    _A						,				self.clip_extra_context_tokens * cross_attention_dim		)
							snake_case_						:     Any     =		nn.Linear(_A						,				_A		)
							snake_case_						:     Tuple     =		nn.LayerNorm(_A		)






						def UpperCAmelCase_			( self : List[str]						,				*, _A : Tuple						,				_A : List[Any]						,				_A : str						,				_A : Optional[Any]		)							->      List[Any]:







							"""simple docstring"""


							if do_classifier_free_guidance:
								# Add the classifier free guidance embeddings to the image embeddings
								snake_case_						:     Optional[int]     =		image_embeddings.shape[0]
								snake_case_						:     Optional[Any]     =		self.learned_classifier_free_guidance_embeddings.unsqueeze(0		)
								snake_case_						:     Optional[Any]     =		classifier_free_guidance_embeddings.expand(
								    _A						,				-1		)
								snake_case_						:     Any     =		torch.cat([classifier_free_guidance_embeddings, image_embeddings]						,				dim=0		)

							# The image embeddings batch size and the text embeddings batch size are equal
							assert image_embeddings.shape[0] == prompt_embeds.shape[0]

							snake_case_						:     str     =		prompt_embeds.shape[0]

							# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
							# adding CLIP embeddings to the existing timestep embedding, ...
							snake_case_						:     str     =		self.embedding_proj(_A		)
							snake_case_						:     Dict     =		self.clip_image_embeddings_project_to_time_embeddings(_A		)
							snake_case_						:     Tuple     =		time_projected_image_embeddings + time_projected_prompt_embeds

							# ... and by projecting CLIP embeddings into four
							# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
							snake_case_						:     List[str]     =		self.clip_extra_context_tokens_proj(_A		)
							snake_case_						:     Optional[Any]     =		clip_extra_context_tokens.reshape(_A						,				-1						,				self.clip_extra_context_tokens		)
							snake_case_						:     int     =		clip_extra_context_tokens.permute(0						,				2						,				1		)

							snake_case_						:     Optional[int]     =		self.encoder_hidden_states_proj(_A		)
							snake_case_						:     Any     =		self.text_encoder_hidden_states_norm(_A		)
							snake_case_						:     Dict     =		torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]						,				dim=1		)

							return text_encoder_hidden_states, additive_clip_time_embeddings

