'''simple docstring'''
import itertools
from dataclasses import dataclass
from typing import List, Optional

import pyarrow as pa
import pyarrow.parquet as pq

import datasets
from datasets.table import table_cast


a=  datasets.utils.logging.get_logger(__name__)






@dataclass
class 			__lowercase		(						datasets.BuilderConfig						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						10000
     SCREAMING_SNAKE_CASE__		   =						None
     SCREAMING_SNAKE_CASE__		   =						None






class 			__lowercase		(						datasets.ArrowBasedBuilder						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						ParquetConfig







     def lowerCAmelCase     (		self    ):
         return datasets.DatasetInfo(features=self.config.features    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         if not self.config.data_files:
             raise ValueError(f"""At least one data file must be specified, but got data_files={self.config.data_files}"""    )
         __UpperCamelCase	:	str		    =  dl_manager.download_and_extract(self.config.data_files    )
         if isinstance(_lowerCamelCase     ,			(str, list, tuple)    ):
             __UpperCamelCase	:	Tuple		    =  data_files
             if isinstance(_lowerCamelCase     ,			_lowerCamelCase    ):
                 __UpperCamelCase	:	Union[str, Any]		    =  [files]
             # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
             __UpperCamelCase	:	Union[str, Any]		    =  [dl_manager.iter_files(_lowerCamelCase    ) for file in files]
             return [datasets.SplitGenerator(name=datasets.Split.TRAIN     ,			gen_kwargs={'files': files}    )]
         __UpperCamelCase	:	List[str]		    =  []
         for split_name, files in data_files.items():
             if isinstance(_lowerCamelCase     ,			_lowerCamelCase    ):
                 __UpperCamelCase	:	List[Any]		    =  [files]
             # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
             __UpperCamelCase	:	Optional[int]		    =  [dl_manager.iter_files(_lowerCamelCase    ) for file in files]
             # Infer features is they are stoed in the arrow schema
             if self.info.features is None:
                 for file in itertools.chain.from_iterable(_lowerCamelCase    ):
                     with open(_lowerCamelCase     ,			'rb'    ) as f:
                         __UpperCamelCase	:	Any		    =  datasets.Features.from_arrow_schema(pq.read_schema(_lowerCamelCase    )    )
                     break
             splits.append(datasets.SplitGenerator(name=_lowerCamelCase     ,			gen_kwargs={'files': files}    )    )
         return splits







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         if self.info.features is not None:
             # more expensive cast to support nested features with keys in a different order
             # allows str <-> int/float or str to Audio for example
             __UpperCamelCase	:	List[Any]		    =  table_cast(_lowerCamelCase     ,			self.info.features.arrow_schema    )
         return pa_table







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	Union[str, Any]		    =  self.info.features.arrow_schema if self.info.features is not None else None
         if self.info.features is not None and self.config.columns is not None:
             if sorted(field.name for field in schema    ) != sorted(self.config.columns    ):
                 raise ValueError(
                     f"""Tried to load parquet data with columns '{self.config.columns}' with mismatching features '{self.info.features}'"""    )
         for file_idx, file in enumerate(itertools.chain.from_iterable(_lowerCamelCase    )    ):
             with open(_lowerCamelCase     ,			'rb'    ) as f:
                 __UpperCamelCase	:	Union[str, Any]		    =  pq.ParquetFile(_lowerCamelCase    )
                 try:
                     for batch_idx, record_batch in enumerate(
                         parquet_file.iter_batches(batch_size=self.config.batch_size     ,			columns=self.config.columns    )    ):
                         __UpperCamelCase	:	Tuple		    =  pa.Table.from_batches([record_batch]    )
                         # Uncomment for debugging (will print the Arrow table size and elements)
                         # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                         # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                         yield f"""{file_idx}_{batch_idx}""", self._cast_table(_lowerCamelCase    )
                 except ValueError as e:
                     logger.error(f"""Failed to read file '{file}' with error {type(_lowerCamelCase    )}: {e}"""    )
                     raise




'''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



