'''simple docstring'''
import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						''''''
     SCREAMING_SNAKE_CASE__		   =						(
         None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
     )
     SCREAMING_SNAKE_CASE__		   =						None  # compression type in fsspec. ex: "gzip"
     SCREAMING_SNAKE_CASE__		   =						None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz







     def __init__(		self     ,			_lowerCamelCase = ""     ,			_lowerCamelCase = None     ,			_lowerCamelCase = None     ,			**_lowerCamelCase    ):
         super().__init__(self     ,			**_lowerCamelCase    )
         # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
         __UpperCamelCase	:	str		    =  fsspec.open(
             _lowerCamelCase     ,			mode='rb'     ,			protocol=_lowerCamelCase     ,			compression=self.compression     ,			client_kwargs={
                 'requote_redirect_url': False,  # see https://github.com/huggingface/datasets/pull/5459
                 'trust_env': True,  # Enable reading proxy env variables.
                 **(target_options or {}).pop('client_kwargs'     ,			{}    ),  # To avoid issues if it was already passed.
             }     ,			**(target_options or {})     ,			)
         __UpperCamelCase	:	Any		    =  os.path.basename(self.file.path.split('::'    )[0]    )
         __UpperCamelCase	:	List[Any]		    =  (
             self.compressed_name[: self.compressed_name.rindex('.'    )]
             if "." in self.compressed_name
             else self.compressed_name
         )
         __UpperCamelCase	:	Optional[int]		    =  None







     @classmethod
     def lowerCAmelCase     (		cls     ,			_lowerCamelCase    ):
         # compressed file paths are always relative to the archive root
         return super()._strip_protocol(_lowerCamelCase    ).lstrip('/'    )







     def lowerCAmelCase     (		self    ):
         if self.dir_cache is None:
             __UpperCamelCase	:	Any		    =  {**self.file.fs.info(self.file.path    ), "name": self.uncompressed_name}
             __UpperCamelCase	:	List[str]		    =  {f["name"]: f}







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         return self.file.open().read()







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase = "rb"     ,			_lowerCamelCase=None     ,			_lowerCamelCase=True     ,			_lowerCamelCase=None     ,			**_lowerCamelCase     ,			):
         __UpperCamelCase	:	List[str]		    =  self._strip_protocol(_lowerCamelCase    )
         if mode != "rb":
             raise ValueError(f"""Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'"""    )
         return self.file.open()






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''bz2'''
     SCREAMING_SNAKE_CASE__		   =						'''bz2'''
     SCREAMING_SNAKE_CASE__		   =						'''.bz2'''






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''gzip'''
     SCREAMING_SNAKE_CASE__		   =						'''gzip'''
     SCREAMING_SNAKE_CASE__		   =						'''.gz'''






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''lz4'''
     SCREAMING_SNAKE_CASE__		   =						'''lz4'''
     SCREAMING_SNAKE_CASE__		   =						'''.lz4'''






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''xz'''
     SCREAMING_SNAKE_CASE__		   =						'''xz'''
     SCREAMING_SNAKE_CASE__		   =						'''.xz'''






class 			__lowercase		(						_A						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						'''zstd'''
     SCREAMING_SNAKE_CASE__		   =						'''zstd'''
     SCREAMING_SNAKE_CASE__		   =						'''.zst'''







     def __init__(		self     ,			_lowerCamelCase     ,			_lowerCamelCase = "rb"     ,			_lowerCamelCase = None     ,			_lowerCamelCase = None     ,			_lowerCamelCase = DEFAULT_BLOCK_SIZE     ,			**_lowerCamelCase     ,			):
         super().__init__(
             fo=_lowerCamelCase     ,			mode=_lowerCamelCase     ,			target_protocol=_lowerCamelCase     ,			target_options=_lowerCamelCase     ,			block_size=_lowerCamelCase     ,			**_lowerCamelCase     ,			)
         # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
         #
         # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
         #     out.close = close
         # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
         #
         # see https://github.com/intake/filesystem_spec/issues/725
         __UpperCamelCase	:	Optional[Any]		    =  self.file.__enter__






         class 			__lowercase		:







              """simple docstring"""






              def __init__(		self     ,			_lowerCamelCase    ):
                  __UpperCamelCase	:	Dict		    =  file_







              def __enter__(		self    ):
                  self._file.__enter__()
                  return self







              def __exit__(		self     ,			*_lowerCamelCase     ,			**_lowerCamelCase    ):
                  self._file.__exit__(*_lowerCamelCase     ,			**_lowerCamelCase    )







              def __iter__(		self    ):
                  return iter(self._file    )







              def lowerCAmelCase     (		self    ):
                  return next(self._file    )







              def __getattr__(		self     ,			_lowerCamelCase    ):
                  return getattr(self._file     ,			_lowerCamelCase    )

         def fixed_enter(*_lowerCamelCase     ,			**_lowerCamelCase    ):
             return WrappedFile(_enter(*_lowerCamelCase     ,			**_lowerCamelCase    )    )

         __UpperCamelCase	:	List[str]		    =  fixed_enter




'''simple docstring'''
import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class 			__lowercase		(						_lowerCamelCase			,							_lowerCamelCase						):







     """simple docstring"""






     @register_to_config
     def __init__(		self     ,			*,
         _lowerCamelCase = 4     ,			_lowerCamelCase = 7_6_8     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			):
         super().__init__()

         __UpperCamelCase	:	Optional[int]		    =  nn.Parameter(torch.zeros(_lowerCamelCase    )    )

         # parameters for additional clip time embeddings
         __UpperCamelCase	:	Dict		    =  nn.Linear(_lowerCamelCase     ,			_lowerCamelCase    )
         __UpperCamelCase	:	int		    =  nn.Linear(_lowerCamelCase     ,			_lowerCamelCase    )

         # parameters for encoder hidden states
         __UpperCamelCase	:	Optional[Any]		    =  clip_extra_context_tokens
         __UpperCamelCase	:	Dict		    =  nn.Linear(
             _lowerCamelCase     ,			self.clip_extra_context_tokens * cross_attention_dim    )
         __UpperCamelCase	:	str		    =  nn.Linear(_lowerCamelCase     ,			_lowerCamelCase    )
         __UpperCamelCase	:	List[str]		    =  nn.LayerNorm(_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			*, _lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
         if do_classifier_free_guidance:
             # Add the classifier free guidance embeddings to the image embeddings
             __UpperCamelCase	:	int		    =  image_embeddings.shape[0]
             __UpperCamelCase	:	Optional[Any]		    =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0    )
             __UpperCamelCase	:	List[Any]		    =  classifier_free_guidance_embeddings.expand(
                 _lowerCamelCase     ,			-1    )
             __UpperCamelCase	:	Optional[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]

         __UpperCamelCase	:	Optional[Any]		    =  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, ...
         __UpperCamelCase	:	Optional[int]		    =  self.embedding_proj(_lowerCamelCase    )
         __UpperCamelCase	:	List[str]		    =  self.clip_image_embeddings_project_to_time_embeddings(_lowerCamelCase    )
         __UpperCamelCase	:	Optional[int]		    =  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"
         __UpperCamelCase	:	Optional[int]		    =  self.clip_extra_context_tokens_proj(_lowerCamelCase    )
         __UpperCamelCase	:	Tuple		    =  clip_extra_context_tokens.reshape(_lowerCamelCase     ,			-1     ,			self.clip_extra_context_tokens    )
         __UpperCamelCase	:	Dict		    =  clip_extra_context_tokens.permute(0     ,			2     ,			1    )

         __UpperCamelCase	:	Union[str, Any]		    =  self.encoder_hidden_states_proj(_lowerCamelCase    )
         __UpperCamelCase	:	Optional[Any]		    =  self.text_encoder_hidden_states_norm(_lowerCamelCase    )
         __UpperCamelCase	:	Tuple		    =  torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]     ,			dim=1    )

         return text_encoder_hidden_states, additive_clip_time_embeddings



