




"""simple docstring"""


import os
from typing import BinaryIO, Optional, Union

import numpy as np
import pyarrow.parquet as pq

from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config
from ..features.features import FeatureType, _visit
from ..formatting import query_table
from ..packaged_modules import _PACKAGED_DATASETS_MODULES
from ..packaged_modules.parquet.parquet import Parquet
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader


def  UpperCAmelCase__	(snake_case__							: Features   ):
  """simple docstring"""

  _snake_case							:		Optional[Any]								=  np.inf

  def set_batch_size(snake_case__							: FeatureType   ) -> None:
    nonlocal batch_size
    if isinstance(a__		,       a__   ):
      _snake_case							:		int								=  min(a__		,       config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS   )
    elif isinstance(a__		,       a__   ):
      _snake_case							:		Dict								=  min(a__		,       config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS   )
    elif isinstance(a__		,       a__   ) and feature.dtype == "binary":
      _snake_case							:		Union[str, Any]								=  min(a__		,       config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS   )

  _visit(a__		,       a__   )

  return None if batch_size is np.inf else batch_size






class  lowercase(					_a			):







 '''simple docstring'''





 def __init__(    self:		Optional[int],				a_:		List[str],				a_:		Optional[int] = None,				a_:		List[str] = None,				a_:		Any = None,				a_:		List[Any] = False,				a_:		List[str] = False,				a_:		Optional[Any] = None,				**a_:		int,				):




   '''simple docstring'''



   super().__init__(
       __lowerCAmelCase,				split=__lowerCAmelCase,				features=__lowerCAmelCase,				cache_dir=__lowerCAmelCase,				keep_in_memory=__lowerCAmelCase,				streaming=__lowerCAmelCase,				num_proc=__lowerCAmelCase,				**__lowerCAmelCase,				)
   _snake_case							:		List[str]								=  path_or_paths if isinstance(__lowerCAmelCase,				__lowerCAmelCase		) else {self.split: path_or_paths}
   _snake_case							:		Optional[int]								=  _PACKAGED_DATASETS_MODULES["""parquet"""][1]
   _snake_case							:		Dict								=  Parquet(
       cache_dir=__lowerCAmelCase,				data_files=__lowerCAmelCase,				features=__lowerCAmelCase,				hash=__lowerCAmelCase,				**__lowerCAmelCase,				)


 def 			UpperCamelCase_       (    self:		Tuple		):




   '''simple docstring'''



   if self.streaming:
     _snake_case							:		List[Any]								=  self.builder.as_streaming_dataset(split=self.split		)
   # Build regular (map-style) dataset
   else:
     _snake_case							:		int								=  None
     _snake_case							:		int								=  None
     _snake_case							:		Optional[Any]								=  None
     _snake_case							:		List[str]								=  None

     self.builder.download_and_prepare(
         download_config=__lowerCAmelCase,				download_mode=__lowerCAmelCase,				verification_mode=__lowerCAmelCase,				base_path=__lowerCAmelCase,				num_proc=self.num_proc,				)
     _snake_case							:		str								=  self.builder.as_dataset(
         split=self.split,				verification_mode=__lowerCAmelCase,				in_memory=self.keep_in_memory		)
   return dataset






class  lowercase:







 '''simple docstring'''





 def __init__(    self:		int,				a_:		Dict,				a_:		int,				a_:		Optional[Any] = None,				**a_:		List[str],				):




   '''simple docstring'''



   _snake_case							:		str								=  dataset
   _snake_case							:		int								=  path_or_buf
   _snake_case							:		Optional[int]								=  batch_size or get_writer_batch_size(dataset.features		)
   _snake_case							:		List[Any]								=  parquet_writer_kwargs


 def 			UpperCamelCase_       (    self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE

   if isinstance(self.path_or_buf,				(str, bytes, os.PathLike)		):
     with open(self.path_or_buf,				"""wb+"""		) as buffer:
       _snake_case							:		Tuple								=  self._write(file_obj=__lowerCAmelCase,				batch_size=__lowerCAmelCase,				**self.parquet_writer_kwargs		)
   else:
     _snake_case							:		Union[str, Any]								=  self._write(file_obj=self.path_or_buf,				batch_size=__lowerCAmelCase,				**self.parquet_writer_kwargs		)
   return written


 def 			UpperCamelCase_       (    self:		List[str],				a_:		Optional[Any],				a_:		Union[str, Any],				**a_:		List[Any]		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  0
   _snake_case							:		Optional[int]								=  parquet_writer_kwargs.pop("""path_or_buf""",				__lowerCAmelCase		)
   _snake_case							:		Dict								=  self.dataset.features.arrow_schema

   _snake_case							:		Tuple								=  pq.ParquetWriter(__lowerCAmelCase,				schema=__lowerCAmelCase,				**__lowerCAmelCase		)

   for offset in logging.tqdm(
       range(0,				len(self.dataset		),				__lowerCAmelCase		),				unit="""ba""",				disable=not logging.is_progress_bar_enabled(),				desc="""Creating parquet from Arrow format""",				):
     _snake_case							:		int								=  query_table(
         table=self.dataset._data,				key=slice(__lowerCAmelCase,				offset + batch_size		),				indices=self.dataset._indices if self.dataset._indices is not None else None,				)
     writer.write_table(__lowerCAmelCase		)
     written += batch.nbytes
   writer.close()
   return written








"""simple docstring"""


from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)






@flax.struct.dataclass
class  lowercase(					__a			):







 '''simple docstring'''





 lowercase__			 = 42
 lowercase__			 = 42







class  lowercase(					nn.Module			):







 '''simple docstring'''





 lowercase__			 = 42
 lowercase__			 = (16, 32, 96, 2_56)
 lowercase__			 = jnp.floataa


 def 			UpperCamelCase_       (    self:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  nn.Conv(
       self.block_out_channels[0],				kernel_size=(3, 3),				padding=((1, 1), (1, 1)),				dtype=self.dtype,				)

   _snake_case							:		int								=  []
   for i in range(len(self.block_out_channels		) - 1		):
     _snake_case							:		int								=  self.block_out_channels[i]
     _snake_case							:		Tuple								=  self.block_out_channels[i + 1]
     _snake_case							:		Dict								=  nn.Conv(
         a_,				kernel_size=(3, 3),				padding=((1, 1), (1, 1)),				dtype=self.dtype,				)
     blocks.append(a_		)
     _snake_case							:		List[Any]								=  nn.Conv(
         a_,				kernel_size=(3, 3),				strides=(2, 2),				padding=((1, 1), (1, 1)),				dtype=self.dtype,				)
     blocks.append(a_		)
   _snake_case							:		Any								=  blocks

   _snake_case							:		Optional[Any]								=  nn.Conv(
       self.conditioning_embedding_channels,				kernel_size=(3, 3),				padding=((1, 1), (1, 1)),				kernel_init=nn.initializers.zeros_init(),				bias_init=nn.initializers.zeros_init(),				dtype=self.dtype,				)


 def __call__(    self:		Optional[Any],				a_:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		int								=  self.conv_in(a_		)
   _snake_case							:		Optional[int]								=  nn.silu(a_		)

   for block in self.blocks:
     _snake_case							:		Tuple								=  block(a_		)
     _snake_case							:		int								=  nn.silu(a_		)

   _snake_case							:		Optional[int]								=  self.conv_out(a_		)

   return embedding







@flax_register_to_config
class  lowercase(					nn.Module	,      __a	,      __a			):







 '''simple docstring'''





 lowercase__			 = 32
 lowercase__			 = 4
 lowercase__			 = (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 lowercase__			 = False
 lowercase__			 = (3_20, 6_40, 12_80, 12_80)
 lowercase__			 = 2
 lowercase__			 = 8
 lowercase__			 = None
 lowercase__			 = 12_80
 lowercase__			 = 0.0
 lowercase__			 = False
 lowercase__			 = jnp.floataa
 lowercase__			 = True
 lowercase__			 = 0
 lowercase__			 = "rgb"
 lowercase__			 = (16, 32, 96, 2_56)


 def 			UpperCamelCase_       (    self:		int,				a_:		jax.random.KeyArray		):




   '''simple docstring'''



   _snake_case							:		str								=  (1, self.in_channels, self.sample_size, self.sample_size)
   _snake_case							:		Optional[Any]								=  jnp.zeros(a_,				dtype=jnp.floataa		)
   _snake_case							:		List[str]								=  jnp.ones((1,),				dtype=jnp.intaa		)
   _snake_case							:		List[Any]								=  jnp.zeros((1, 1, self.cross_attention_dim),				dtype=jnp.floataa		)
   _snake_case							:		Any								=  (1, 3, self.sample_size * 8, self.sample_size * 8)
   _snake_case							:		Optional[int]								=  jnp.zeros(a_,				dtype=jnp.floataa		)

   _snake_case		, _snake_case							:		Tuple								=  jax.random.split(a_		)
   _snake_case							:		str								=  {"""params""": params_rng, """dropout""": dropout_rng}

   return self.init(a_,				a_,				a_,				a_,				a_		)["params"]


 def 			UpperCamelCase_       (    self:		Any		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.block_out_channels
   _snake_case							:		Optional[int]								=  block_out_channels[0] * 4

   # If `num_attention_heads` is not defined (which is the case for most models)
   # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
   # The reason for this behavior is to correct for incorrectly named variables that were introduced
   # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
   # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
   # which is why we correct for the naming here.
   _snake_case							:		int								=  self.num_attention_heads or self.attention_head_dim

   # input
   _snake_case							:		Union[str, Any]								=  nn.Conv(
       block_out_channels[0],				kernel_size=(3, 3),				strides=(1, 1),				padding=((1, 1), (1, 1)),				dtype=self.dtype,				)

   # time
   _snake_case							:		int								=  FlaxTimesteps(
       block_out_channels[0],				flip_sin_to_cos=self.flip_sin_to_cos,				freq_shift=self.config.freq_shift		)
   _snake_case							:		Any								=  FlaxTimestepEmbedding(a_,				dtype=self.dtype		)

   _snake_case							:		Optional[Any]								=  FlaxControlNetConditioningEmbedding(
       conditioning_embedding_channels=block_out_channels[0],				block_out_channels=self.conditioning_embedding_out_channels,				)

   _snake_case							:		List[str]								=  self.only_cross_attention
   if isinstance(a_,				a_		):
     _snake_case							:		List[Any]								=  (only_cross_attention,) * len(self.down_block_types		)

   if isinstance(a_,				a_		):
     _snake_case							:		Optional[Any]								=  (num_attention_heads,) * len(self.down_block_types		)

   # down
   _snake_case							:		List[str]								=  []
   _snake_case							:		Tuple								=  []

   _snake_case							:		int								=  block_out_channels[0]

   _snake_case							:		Optional[Any]								=  nn.Conv(
       a_,				kernel_size=(1, 1),				padding="""VALID""",				kernel_init=nn.initializers.zeros_init(),				bias_init=nn.initializers.zeros_init(),				dtype=self.dtype,				)
   controlnet_down_blocks.append(a_		)

   for i, down_block_type in enumerate(self.down_block_types		):
     _snake_case							:		List[Any]								=  output_channel
     _snake_case							:		Any								=  block_out_channels[i]
     _snake_case							:		List[str]								=  i == len(a_		) - 1

     if down_block_type == "CrossAttnDownBlock2D":
       _snake_case							:		Optional[int]								=  FlaxCrossAttnDownBlockaD(
           in_channels=a_,				out_channels=a_,				dropout=self.dropout,				num_layers=self.layers_per_block,				num_attention_heads=num_attention_heads[i],				add_downsample=not is_final_block,				use_linear_projection=self.use_linear_projection,				only_cross_attention=only_cross_attention[i],				dtype=self.dtype,				)
     else:
       _snake_case							:		List[Any]								=  FlaxDownBlockaD(
           in_channels=a_,				out_channels=a_,				dropout=self.dropout,				num_layers=self.layers_per_block,				add_downsample=not is_final_block,				dtype=self.dtype,				)

     down_blocks.append(a_		)

     for _ in range(self.layers_per_block		):
       _snake_case							:		List[Any]								=  nn.Conv(
           a_,				kernel_size=(1, 1),				padding="""VALID""",				kernel_init=nn.initializers.zeros_init(),				bias_init=nn.initializers.zeros_init(),				dtype=self.dtype,				)
       controlnet_down_blocks.append(a_		)

     if not is_final_block:
       _snake_case							:		List[Any]								=  nn.Conv(
           a_,				kernel_size=(1, 1),				padding="""VALID""",				kernel_init=nn.initializers.zeros_init(),				bias_init=nn.initializers.zeros_init(),				dtype=self.dtype,				)
       controlnet_down_blocks.append(a_		)

   _snake_case							:		str								=  down_blocks
   _snake_case							:		Union[str, Any]								=  controlnet_down_blocks

   # mid
   _snake_case							:		Tuple								=  block_out_channels[-1]
   _snake_case							:		Optional[int]								=  FlaxUNetMidBlockaDCrossAttn(
       in_channels=a_,				dropout=self.dropout,				num_attention_heads=num_attention_heads[-1],				use_linear_projection=self.use_linear_projection,				dtype=self.dtype,				)

   _snake_case							:		Tuple								=  nn.Conv(
       a_,				kernel_size=(1, 1),				padding="""VALID""",				kernel_init=nn.initializers.zeros_init(),				bias_init=nn.initializers.zeros_init(),				dtype=self.dtype,				)


 def __call__(    self:		str,				a_:		Any,				a_:		Tuple,				a_:		Any,				a_:		int,				a_:		float = 1.0,				a_:		bool = True,				a_:		bool = False,				):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.controlnet_conditioning_channel_order
   if channel_order == "bgr":
     _snake_case							:		List[Any]								=  jnp.flip(a_,				axis=1		)

   # 1. time
   if not isinstance(a_,				jnp.ndarray		):
     _snake_case							:		Any								=  jnp.array([timesteps],				dtype=jnp.intaa		)
   elif isinstance(a_,				jnp.ndarray		) and len(timesteps.shape		) == 0:
     _snake_case							:		Union[str, Any]								=  timesteps.astype(dtype=jnp.floataa		)
     _snake_case							:		List[str]								=  jnp.expand_dims(a_,				0		)

   _snake_case							:		List[str]								=  self.time_proj(a_		)
   _snake_case							:		str								=  self.time_embedding(a_		)

   # 2. pre-process
   _snake_case							:		List[str]								=  jnp.transpose(a_,				(0, 2, 3, 1)		)
   _snake_case							:		List[Any]								=  self.conv_in(a_		)

   _snake_case							:		Union[str, Any]								=  jnp.transpose(a_,				(0, 2, 3, 1)		)
   _snake_case							:		Any								=  self.controlnet_cond_embedding(a_		)
   sample += controlnet_cond

   # 3. down
   _snake_case							:		List[str]								=  (sample,)
   for down_block in self.down_blocks:
     if isinstance(a_,				a_		):
       _snake_case		, _snake_case							:		Optional[Any]								=  down_block(a_,				a_,				a_,				deterministic=not train		)
     else:
       _snake_case		, _snake_case							:		Dict								=  down_block(a_,				a_,				deterministic=not train		)
     down_block_res_samples += res_samples

   # 4. mid
   _snake_case							:		Dict								=  self.mid_block(a_,				a_,				a_,				deterministic=not train		)

   # 5. contronet blocks
   _snake_case							:		Tuple								=  ()
   for down_block_res_sample, controlnet_block in zip(a_,				self.controlnet_down_blocks		):
     _snake_case							:		Any								=  controlnet_block(a_		)
     controlnet_down_block_res_samples += (down_block_res_sample,)

   _snake_case							:		List[Any]								=  controlnet_down_block_res_samples

   _snake_case							:		int								=  self.controlnet_mid_block(a_		)

   # 6. scaling
   _snake_case							:		int								=  [sample * conditioning_scale for sample in down_block_res_samples]
   mid_block_res_sample *= conditioning_scale

   if not return_dict:
     return (down_block_res_samples, mid_block_res_sample)

   return FlaxControlNetOutput(
       down_block_res_samples=a_,				mid_block_res_sample=a_		)


