



import math
from typing import Optional

import numpy as np
import torch
from torch import nn

from .activations import get_activation



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase = False				,		_lowerCAmelCase = 1				,		_lowerCAmelCase = 1				,		_lowerCAmelCase = 1_00_00				,		):
					assert len(timesteps.shape) == 1, "Timesteps should be a 1d-array"

					UpperCamelCase_											=				embedding_dim // 2
					UpperCamelCase_											=				-math.log(_lowerCAmelCase) * torch.arange(
					    start=0				,		end=_lowerCAmelCase				,		dtype=torch.floataa				,		device=timesteps.device)
					UpperCamelCase_											=				exponent / (half_dim - downscale_freq_shift)

					UpperCamelCase_											=				torch.exp(_lowerCAmelCase)
					UpperCamelCase_											=				timesteps[:, None].float() * emb[None, :]

					# scale embeddings
					UpperCamelCase_											=				scale * emb

					# concat sine and cosine embeddings
					UpperCamelCase_											=				torch.cat([torch.sin(_lowerCAmelCase), torch.cos(_lowerCAmelCase)]				,		dim=-1)

					# flip sine and cosine embeddings
					if flip_sin_to_cos:
										UpperCamelCase_											=				torch.cat([emb[:, half_dim:], emb[:, :half_dim]]				,		dim=-1)

					# zero pad
					if embedding_dim % 2 == 1:
										UpperCamelCase_											=				torch.nn.functional.pad(_lowerCAmelCase				,		(0, 1, 0, 0))
					return emb



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase				,		_lowerCAmelCase=False				,		_lowerCAmelCase=0):
					UpperCamelCase_											=				np.arange(_lowerCAmelCase				,		dtype=np.floataa)
					UpperCamelCase_											=				np.arange(_lowerCAmelCase				,		dtype=np.floataa)
					UpperCamelCase_											=				np.meshgrid(_lowerCAmelCase				,		_lowerCAmelCase)  # here w goes first
					UpperCamelCase_											=				np.stack(_lowerCAmelCase				,		axis=0)

					UpperCamelCase_											=				grid.reshape([2, 1, grid_size, grid_size])
					UpperCamelCase_											=				get_ad_sincos_pos_embed_from_grid(_lowerCAmelCase				,		_lowerCAmelCase)
					if cls_token and extra_tokens > 0:
										UpperCamelCase_											=				np.concatenate([np.zeros([extra_tokens, embed_dim]), pos_embed]				,		axis=0)
					return pos_embed



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase):
					if embed_dim % 2 != 0:
										raise ValueError("embed_dim must be divisible by 2")

					# use half of dimensions to encode grid_h
					UpperCamelCase_											=				get_ad_sincos_pos_embed_from_grid(embed_dim // 2				,		grid[0])  # (H*W, D/2)
					UpperCamelCase_											=				get_ad_sincos_pos_embed_from_grid(embed_dim // 2				,		grid[1])  # (H*W, D/2)

					UpperCamelCase_											=				np.concatenate([emb_h, emb_w]				,		axis=1)  # (H*W, D)
					return emb



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase):
					if embed_dim % 2 != 0:
										raise ValueError("embed_dim must be divisible by 2")

					UpperCamelCase_											=				np.arange(embed_dim // 2				,		dtype=np.floataa)
					omega /= embed_dim / 2.0
					UpperCamelCase_											=				1.0 / 1_00_00**omega  # (D/2,)

					UpperCamelCase_											=				pos.reshape(-1)  # (M,)
					UpperCamelCase_											=				np.einsum("m,d->md"				,		_lowerCAmelCase				,		_lowerCAmelCase)  # (M, D/2), outer product

					UpperCamelCase_											=				np.sin(_lowerCAmelCase)  # (M, D/2)
					UpperCamelCase_											=				np.cos(_lowerCAmelCase)  # (M, D/2)

					UpperCamelCase_											=				np.concatenate([emb_sin, emb_cos]				,		axis=1)  # (M, D)
					return emb


class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__=224       ,	snake_case__=224       ,	snake_case__=16       ,	snake_case__=3       ,	snake_case__=768       ,	snake_case__=False       ,	snake_case__=True       ,	snake_case__=True       ,	):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				(height // patch_size) * (width // patch_size)
									UpperCamelCase_											=				flatten
									UpperCamelCase_											=				layer_norm

									UpperCamelCase_											=				nn.Convad(
									    snake_case__       ,	snake_case__       ,	kernel_size=(patch_size, patch_size)       ,	stride=snake_case__       ,	bias=snake_case__      )
									if layer_norm:
														UpperCamelCase_											=				nn.LayerNorm(snake_case__       ,	elementwise_affine=snake_case__       ,	eps=1e-6      )
									else:
														UpperCamelCase_											=				None

									UpperCamelCase_											=				get_ad_sincos_pos_embed(snake_case__       ,	int(num_patches**0.5      )      )
									self.register_buffer("pos_embed"       ,	torch.from_numpy(snake_case__      ).float().unsqueeze(0      )       ,	persistent=snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.proj(snake_case__      )
									if self.flatten:
														UpperCamelCase_											=				latent.flatten(2      ).transpose(1       ,	2      )  # BCHW -> BNC
									if self.layer_norm:
														UpperCamelCase_											=				self.norm(snake_case__      )
									return latent + self.pos_embed




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__ = "silu"       ,	snake_case__ = None       ,	snake_case__ = None       ,	snake_case__=None       ,	):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )

									if cond_proj_dim is not None:
														UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__       ,	bias=snake_case__      )
									else:
														UpperCamelCase_											=				None

									UpperCamelCase_											=				get_activation(snake_case__      )

									if out_dim is not None:
														UpperCamelCase_											=				out_dim
									else:
														UpperCamelCase_											=				time_embed_dim
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )

									if post_act_fn is None:
														UpperCamelCase_											=				None
									else:
														UpperCamelCase_											=				get_activation(snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__=None      ):

									'''simple docstring'''


									if condition is not None:
														UpperCamelCase_											=				sample + self.cond_proj(snake_case__      )
									UpperCamelCase_											=				self.linear_a(snake_case__      )

									if self.act is not None:
														UpperCamelCase_											=				self.act(snake_case__      )

									UpperCamelCase_											=				self.linear_a(snake_case__      )

									if self.post_act is not None:
														UpperCamelCase_											=				self.post_act(snake_case__      )
									return sample




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				num_channels
									UpperCamelCase_											=				flip_sin_to_cos
									UpperCamelCase_											=				downscale_freq_shift

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				get_timestep_embedding(
									    snake_case__       ,	self.num_channels       ,	flip_sin_to_cos=self.flip_sin_to_cos       ,	downscale_freq_shift=self.downscale_freq_shift       ,	)
									return t_emb




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 256       ,	snake_case__ = 1.0       ,	snake_case__=True       ,	snake_case__=True       ,	snake_case__=False      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				nn.Parameter(torch.randn(snake_case__      ) * scale       ,	requires_grad=snake_case__      )
									UpperCamelCase_											=				log
									UpperCamelCase_											=				flip_sin_to_cos

									if set_W_to_weight:
														# to delete later
														UpperCamelCase_											=				nn.Parameter(torch.randn(snake_case__      ) * scale       ,	requires_grad=snake_case__      )

														UpperCamelCase_											=				self.W

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									if self.log:
														UpperCamelCase_											=				torch.log(snake_case__      )

									UpperCamelCase_											=				x[:, None] * self.weight[None, :] * 2 * np.pi

									if self.flip_sin_to_cos:
														UpperCamelCase_											=				torch.cat([torch.cos(snake_case__      ), torch.sin(snake_case__      )]       ,	dim=-1      )
									else:
														UpperCamelCase_											=				torch.cat([torch.sin(snake_case__      ), torch.cos(snake_case__      )]       ,	dim=-1      )
									return out




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__       ,	snake_case__       ,	):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				height
									UpperCamelCase_											=				width
									UpperCamelCase_											=				num_embed
									UpperCamelCase_											=				embed_dim

									UpperCamelCase_											=				nn.Embedding(self.num_embed       ,	snake_case__      )
									UpperCamelCase_											=				nn.Embedding(self.height       ,	snake_case__      )
									UpperCamelCase_											=				nn.Embedding(self.width       ,	snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.emb(snake_case__      )

									UpperCamelCase_											=				self.height_emb(torch.arange(self.height       ,	device=index.device      ).view(1       ,	self.height      )      )

									# 1 x H x D -> 1 x H x 1 x D
									UpperCamelCase_											=				height_emb.unsqueeze(2      )

									UpperCamelCase_											=				self.width_emb(torch.arange(self.width       ,	device=index.device      ).view(1       ,	self.width      )      )

									# 1 x W x D -> 1 x 1 x W x D
									UpperCamelCase_											=				width_emb.unsqueeze(1      )

									UpperCamelCase_											=				height_emb + width_emb

									# 1 x H x W x D -> 1 x L xD
									UpperCamelCase_											=				pos_emb.view(1       ,	self.height * self.width       ,	-1      )

									UpperCamelCase_											=				emb + pos_emb[:, : emb.shape[1], :]

									return emb




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				dropout_prob > 0
									UpperCamelCase_											=				nn.Embedding(num_classes + use_cfg_embedding       ,	snake_case__      )
									UpperCamelCase_											=				num_classes
									UpperCamelCase_											=				dropout_prob

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__=None      ):

									'''simple docstring'''


									if force_drop_ids is None:
														UpperCamelCase_											=				torch.rand(labels.shape[0]       ,	device=labels.device      ) < self.dropout_prob
									else:
														UpperCamelCase_											=				torch.tensor(force_drop_ids == 1      )
									UpperCamelCase_											=				torch.where(snake_case__       ,	self.num_classes       ,	snake_case__      )
									return labels

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__=None      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.dropout_prob > 0
									if (self.training and use_dropout) or (force_drop_ids is not None):
														UpperCamelCase_											=				self.token_drop(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				self.embedding_table(snake_case__      )
									return embeddings




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 1024       ,	snake_case__ = 768       ,	snake_case__ = 768       ,	snake_case__ = 10       ,	):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				num_image_text_embeds
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	self.num_image_text_embeds * cross_attention_dim      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				text_embeds.shape[0]

									# image
									UpperCamelCase_											=				self.image_embeds(snake_case__      )
									UpperCamelCase_											=				image_text_embeds.reshape(snake_case__       ,	self.num_image_text_embeds       ,	-1      )

									# text
									UpperCamelCase_											=				self.text_proj(snake_case__      )

									return torch.cat([image_text_embeds, text_embeds]       ,	dim=1      )




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 768       ,	snake_case__ = 768       ,	snake_case__ = 32       ,	):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				num_image_text_embeds
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	self.num_image_text_embeds * cross_attention_dim      )
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				image_embeds.shape[0]

									# image
									UpperCamelCase_											=				self.image_embeds(snake_case__      )
									UpperCamelCase_											=				image_embeds.reshape(snake_case__       ,	self.num_image_text_embeds       ,	-1      )
									UpperCamelCase_											=				self.norm(snake_case__      )
									return image_embeds




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__=0.1      ):

									'''simple docstring'''


									super().__init__()

									UpperCamelCase_											=				Timesteps(num_channels=256       ,	flip_sin_to_cos=snake_case__       ,	downscale_freq_shift=1      )
									UpperCamelCase_											=				TimestepEmbedding(in_channels=256       ,	time_embed_dim=snake_case__      )
									UpperCamelCase_											=				LabelEmbedding(snake_case__       ,	snake_case__       ,	snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__       ,	snake_case__=None      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.time_proj(snake_case__      )
									UpperCamelCase_											=				self.timestep_embedder(timesteps_proj.to(dtype=snake_case__      )      )  # (N, D)

									UpperCamelCase_											=				self.class_embedder(snake_case__      )  # (N, D)

									UpperCamelCase_											=				timesteps_emb + class_labels  # (N, D)

									return conditioning




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__ = 64      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )
									UpperCamelCase_											=				AttentionPooling(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.norma(snake_case__      )
									UpperCamelCase_											=				self.pool(snake_case__      )
									UpperCamelCase_											=				self.proj(snake_case__      )
									UpperCamelCase_											=				self.norma(snake_case__      )
									return hidden_states




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 768       ,	snake_case__ = 768       ,	snake_case__ = 1536      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.text_proj(snake_case__      )
									UpperCamelCase_											=				self.text_norm(snake_case__      )

									# image
									UpperCamelCase_											=				self.image_proj(snake_case__      )

									return time_image_embeds + time_text_embeds




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 768       ,	snake_case__ = 1536      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.image_proj(snake_case__      )
									UpperCamelCase_											=				self.image_norm(snake_case__      )
									return time_image_embeds




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__ = 768       ,	snake_case__ = 1536      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__      )
									UpperCamelCase_											=				nn.LayerNorm(snake_case__      )
									UpperCamelCase_											=				nn.Sequential(
									    nn.Convad(3       ,	16       ,	3       ,	padding=1      )       ,	nn.SiLU()       ,	nn.Convad(16       ,	16       ,	3       ,	padding=1      )       ,	nn.SiLU()       ,	nn.Convad(16       ,	32       ,	3       ,	padding=1       ,	stride=2      )       ,	nn.SiLU()       ,	nn.Convad(32       ,	32       ,	3       ,	padding=1      )       ,	nn.SiLU()       ,	nn.Convad(32       ,	96       ,	3       ,	padding=1       ,	stride=2      )       ,	nn.SiLU()       ,	nn.Convad(96       ,	96       ,	3       ,	padding=1      )       ,	nn.SiLU()       ,	nn.Convad(96       ,	256       ,	3       ,	padding=1       ,	stride=2      )       ,	nn.SiLU()       ,	nn.Convad(256       ,	4       ,	3       ,	padding=1      )       ,	)

				def  _lowerCamelCase   (	self       ,	snake_case__       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_											=				self.image_proj(snake_case__      )
									UpperCamelCase_											=				self.image_norm(snake_case__      )
									UpperCamelCase_											=				self.input_hint_block(snake_case__      )
									return time_image_embeds, hint




class  _lowercase				(nn.Module ):
				'''simple docstring'''







				def __init__(	self       ,	snake_case__       ,	snake_case__       ,	snake_case__=None      ):

									'''simple docstring'''


									super().__init__()
									UpperCamelCase_											=				dtype
									UpperCamelCase_											=				nn.Parameter(torch.randn(1       ,	snake_case__      ) / embed_dim**0.5      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__       ,	dtype=self.dtype      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__       ,	dtype=self.dtype      )
									UpperCamelCase_											=				nn.Linear(snake_case__       ,	snake_case__       ,	dtype=self.dtype      )
									UpperCamelCase_											=				num_heads
									UpperCamelCase_											=				embed_dim // self.num_heads

				def  _lowerCamelCase   (	self       ,	snake_case__      ):

									'''simple docstring'''


									UpperCamelCase_					,      UpperCamelCase_					,      UpperCamelCase_											=				x.size()

									def shape(snake_case__      ):
														# (bs, length, width) --> (bs, length, n_heads, dim_per_head)
														UpperCamelCase_											=				x.view(snake_case__       ,	-1       ,	self.num_heads       ,	self.dim_per_head      )
														# (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
														UpperCamelCase_											=				x.transpose(1       ,	2      )
														# (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
														UpperCamelCase_											=				x.reshape(bs * self.num_heads       ,	-1       ,	self.dim_per_head      )
														# (bs*n_heads, length, dim_per_head) --> (bs*n_heads, dim_per_head, length)
														UpperCamelCase_											=				x.transpose(1       ,	2      )
														return x

									UpperCamelCase_											=				x.mean(dim=1       ,	keepdim=snake_case__      ) + self.positional_embedding.to(x.dtype      )
									UpperCamelCase_											=				torch.cat([class_token, x]       ,	dim=1      )  # (bs, length+1, width)

									# (bs*n_heads, class_token_length, dim_per_head)
									UpperCamelCase_											=				shape(self.q_proj(snake_case__      )      )
									# (bs*n_heads, length+class_token_length, dim_per_head)
									UpperCamelCase_											=				shape(self.k_proj(snake_case__      )      )
									UpperCamelCase_											=				shape(self.v_proj(snake_case__      )      )

									# (bs*n_heads, class_token_length, length+class_token_length):
									UpperCamelCase_											=				1 / math.sqrt(math.sqrt(self.dim_per_head      )      )
									UpperCamelCase_											=				torch.einsum("bct,bcs->bts"       ,	q * scale       ,	k * scale      )  # More stable with f16 than dividing afterwards
									UpperCamelCase_											=				torch.softmax(weight.float()       ,	dim=-1      ).type(weight.dtype      )

									# (bs*n_heads, dim_per_head, class_token_length)
									UpperCamelCase_											=				torch.einsum("bts,bcs->bct"       ,	snake_case__       ,	snake_case__      )

									# (bs, length+1, width)
									UpperCamelCase_											=				a.reshape(snake_case__       ,	-1       ,	1      ).transpose(1       ,	2      )

									return a[:, 0, :]  # cls_token





from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
	import torch

UpperCAmelCase				:							Any								=logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
	from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):



	def 				_lowerCAmelCase      (cls):
						return cls



def 				_lowerCAmelCase      (_lowerCAmelCase				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		_lowerCAmelCase = None				,		):
					UpperCamelCase_											=				device
					UpperCamelCase_											=				shape[0]

					UpperCamelCase_											=				layout or torch.strided
					UpperCamelCase_											=				device or torch.device("cpu")

					if generator is not None:
										UpperCamelCase_											=				generator.device.type if not isinstance(_lowerCAmelCase				,		_lowerCAmelCase) else generator[0].device.type
										if gen_device_type != device.type and gen_device_type == "cpu":
															UpperCamelCase_											=				"cpu"
															if device != "mps":
																				logger.info(
																				    f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																				    f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																				    f""" slighly speed up this function by passing a generator that was created on the {device} device.""")
										elif gen_device_type != device.type and gen_device_type == "cuda":
															raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}.""")

					if isinstance(_lowerCAmelCase				,		_lowerCAmelCase):
										UpperCamelCase_											=				(1,) + shape[1:]
										UpperCamelCase_											=				[
										    torch.randn(_lowerCAmelCase				,		generator=generator[i]				,		device=_lowerCAmelCase				,		dtype=_lowerCAmelCase				,		layout=_lowerCAmelCase)
										    for i in range(_lowerCAmelCase)
										]
										UpperCamelCase_											=				torch.cat(_lowerCAmelCase				,		dim=0).to(_lowerCAmelCase)
					else:
										UpperCamelCase_											=				torch.randn(_lowerCAmelCase				,		generator=_lowerCAmelCase				,		device=_lowerCAmelCase				,		dtype=_lowerCAmelCase				,		layout=_lowerCAmelCase).to(_lowerCAmelCase)

					return latents






def 				_lowerCAmelCase      (_lowerCAmelCase):
					if is_torch_version("<"				,		"2.0.0") or not hasattr(_lowerCAmelCase				,		"_dynamo"):
										return False
					return isinstance(_lowerCAmelCase				,		torch._dynamo.eval_frame.OptimizedModule)
