





'''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				):
   _UpperCamelCase       		=   42
   _UpperCamelCase       		=   42







class   _lowercase						(							nn.Module				):
   _UpperCamelCase       		=   42
   _UpperCamelCase       		=   (16, 32, 96, 2_56)
   _UpperCamelCase       		=   jnp.floataa


   def 					snake_case				( self					):
       A       :       Optional[int]							=						nn.Conv(
           self.block_out_channels[0]    ,   kernel_size=(3, 3)    ,   padding=((1, 1), (1, 1))    ,   dtype=self.dtype    ,   )

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

       A       :       List[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    ,   _UpperCAmelCase					):
       A       :       Optional[Any]							=						self.conv_in(_UpperCAmelCase					)
       A       :       Any							=						nn.silu(_UpperCAmelCase					)

       for block in self.blocks:
           A       :       Union[str, Any]							=						block(_UpperCAmelCase					)
           A       :       Optional[int]							=						nn.silu(_UpperCAmelCase					)

       A       :       Dict							=						self.conv_out(_UpperCAmelCase					)

       return embedding







@flax_register_to_config
class   _lowercase						(							nn.Module		,					a		,					a				):
   _UpperCamelCase       		=   32
   _UpperCamelCase       		=   4
   _UpperCamelCase       		=   (
       """CrossAttnDownBlock2D""",
       """CrossAttnDownBlock2D""",
       """CrossAttnDownBlock2D""",
       """DownBlock2D""",
   )
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   (3_20, 6_40, 12_80, 12_80)
   _UpperCamelCase       		=   2
   _UpperCamelCase       		=   8
   _UpperCamelCase       		=   None
   _UpperCamelCase       		=   12_80
   _UpperCamelCase       		=   0.0
   _UpperCamelCase       		=   False
   _UpperCamelCase       		=   jnp.floataa
   _UpperCamelCase       		=   True
   _UpperCamelCase       		=   0
   _UpperCamelCase       		=   """rgb"""
   _UpperCamelCase       		=   (16, 32, 96, 2_56)


   def 					snake_case				( self    ,   _UpperCAmelCase					):
       # init input tensors
       A       :       Dict							=						(1, self.in_channels, self.sample_size, self.sample_size)
       A       :       Dict							=						jnp.zeros(_UpperCAmelCase    ,   dtype=jnp.floataa					)
       A       :       List[Any]							=						jnp.ones((1,)    ,   dtype=jnp.intaa					)
       A       :       Optional[Any]							=						jnp.zeros((1, 1, self.cross_attention_dim)    ,   dtype=jnp.floataa					)
       A       :       Tuple							=						(1, 3, self.sample_size * 8, self.sample_size * 8)
       A       :       Tuple							=						jnp.zeros(_UpperCAmelCase    ,   dtype=jnp.floataa					)

       A       :       List[Any]							=						jax.random.split(_UpperCAmelCase					)
       A       :       Union[str, Any]							=						{'''params''': params_rng, '''dropout''': dropout_rng}

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


   def 					snake_case				( self					):
       A       :       int							=						self.block_out_channels
       A       :       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.
       A       :       int							=						self.num_attention_heads or self.attention_head_dim

       # input
       A       :       Tuple							=						nn.Conv(
           block_out_channels[0]    ,   kernel_size=(3, 3)    ,   strides=(1, 1)    ,   padding=((1, 1), (1, 1))    ,   dtype=self.dtype    ,   )

       # time
       A       :       Optional[int]							=						FlaxTimesteps(
           block_out_channels[0]    ,   flip_sin_to_cos=self.flip_sin_to_cos    ,   freq_shift=self.config.freq_shift					)
       A       :       Union[str, Any]							=						FlaxTimestepEmbedding(_UpperCAmelCase    ,   dtype=self.dtype					)

       A       :       Tuple							=						FlaxControlNetConditioningEmbedding(
           conditioning_embedding_channels=block_out_channels[0]    ,   block_out_channels=self.conditioning_embedding_out_channels    ,   )

       A       :       Optional[int]							=						self.only_cross_attention
       if isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					):
           A       :       Optional[int]							=						(only_cross_attention,) * len(self.down_block_types					)

       if isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					):
           A       :       List[str]							=						(num_attention_heads,) * len(self.down_block_types					)

       # down
       A       :       List[str]							=						[]
       A       :       Optional[Any]							=						[]

       A       :       Union[str, Any]							=						block_out_channels[0]

       A       :       int							=						nn.Conv(
           _UpperCAmelCase    ,   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(_UpperCAmelCase					)

       for i, down_block_type in enumerate(self.down_block_types					):
           A       :       List[Any]							=						output_channel
           A       :       Optional[Any]							=						block_out_channels[i]
           A       :       Tuple							=						i == len(_UpperCAmelCase					) - 1

           if down_block_type == "CrossAttnDownBlock2D":
               A       :       Dict							=						FlaxCrossAttnDownBlockaD(
                   in_channels=_UpperCAmelCase    ,   out_channels=_UpperCAmelCase    ,   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:
               A       :       Any							=						FlaxDownBlockaD(
                   in_channels=_UpperCAmelCase    ,   out_channels=_UpperCAmelCase    ,   dropout=self.dropout    ,   num_layers=self.layers_per_block    ,   add_downsample=not is_final_block    ,   dtype=self.dtype    ,   )

           down_blocks.append(_UpperCAmelCase					)

           for _ in range(self.layers_per_block					):
               A       :       str							=						nn.Conv(
                   _UpperCAmelCase    ,   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(_UpperCAmelCase					)

           if not is_final_block:
               A       :       Tuple							=						nn.Conv(
                   _UpperCAmelCase    ,   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(_UpperCAmelCase					)

       A       :       Tuple							=						down_blocks
       A       :       Dict							=						controlnet_down_blocks

       # mid
       A       :       Union[str, Any]							=						block_out_channels[-1]
       A       :       int							=						FlaxUNetMidBlockaDCrossAttn(
           in_channels=_UpperCAmelCase    ,   dropout=self.dropout    ,   num_attention_heads=num_attention_heads[-1]    ,   use_linear_projection=self.use_linear_projection    ,   dtype=self.dtype    ,   )

       A       :       Optional[int]							=						nn.Conv(
           _UpperCAmelCase    ,   kernel_size=(1, 1)    ,   padding='''VALID'''    ,   kernel_init=nn.initializers.zeros_init()    ,   bias_init=nn.initializers.zeros_init()    ,   dtype=self.dtype    ,   )


   def __call__( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase = 1.0    ,   _UpperCAmelCase = True    ,   _UpperCAmelCase = False    ,   ):
       A       :       int							=						self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
           A       :       Optional[int]							=						jnp.flip(_UpperCAmelCase    ,   axis=1					)

       # 1. time
       if not isinstance(_UpperCAmelCase    ,   jnp.ndarray					):
           A       :       str							=						jnp.array([timesteps]    ,   dtype=jnp.intaa					)
       elif isinstance(_UpperCAmelCase    ,   jnp.ndarray					) and len(timesteps.shape					) == 0:
           A       :       Optional[int]							=						timesteps.astype(dtype=jnp.floataa					)
           A       :       List[str]							=						jnp.expand_dims(_UpperCAmelCase    ,   0					)

       A       :       List[str]							=						self.time_proj(_UpperCAmelCase					)
       A       :       Tuple							=						self.time_embedding(_UpperCAmelCase					)

       # 2. pre-process
       A       :       int							=						jnp.transpose(_UpperCAmelCase    ,   (0, 2, 3, 1)					)
       A       :       List[Any]							=						self.conv_in(_UpperCAmelCase					)

       A       :       Optional[int]							=						jnp.transpose(_UpperCAmelCase    ,   (0, 2, 3, 1)					)
       A       :       int							=						self.controlnet_cond_embedding(_UpperCAmelCase					)
       sample += controlnet_cond

       # 3. down
       A       :       str							=						(sample,)
       for down_block in self.down_blocks:
           if isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					):
               A       :       Union[str, Any]							=						down_block(_UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   deterministic=not train					)
           else:
               A       :       Any							=						down_block(_UpperCAmelCase    ,   _UpperCAmelCase    ,   deterministic=not train					)
           down_block_res_samples += res_samples

       # 4. mid
       A       :       List[str]							=						self.mid_block(_UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   deterministic=not train					)

       # 5. contronet blocks
       A       :       int							=						()
       for down_block_res_sample, controlnet_block in zip(_UpperCAmelCase    ,   self.controlnet_down_blocks					):
           A       :       Optional[Any]							=						controlnet_block(_UpperCAmelCase					)
           controlnet_down_block_res_samples += (down_block_res_sample,)

       A       :       str							=						controlnet_down_block_res_samples

       A       :       List[Any]							=						self.controlnet_mid_block(_UpperCAmelCase					)

       # 6. scaling
       A       :       str							=						[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=_UpperCAmelCase    ,   mid_block_res_sample=_UpperCAmelCase					)










'''simple docstring'''






import importlib.util
import os
import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import (
    is_accelerate_available,
    is_flax_available,
    is_safetensors_available,
    is_tf_available,
    is_torch_available,
)
from . import BaseTransformersCLICommand
def 							_lowerCamelCase(  UpperCamelCase__    : Optional[int]		)		->  List[str]:
    return EnvironmentCommand()
def 							_lowerCamelCase(  UpperCamelCase__    : List[Any]		)		->  Tuple:
    return EnvironmentCommand(args.accelerate_config_file		)





class   _lowercase						(							a				):


   @staticmethod
   def 					snake_case				( _UpperCAmelCase					):
       A       :       Optional[Any]							=						parser.add_parser('''env'''					)
       download_parser.set_defaults(func=_UpperCAmelCase					)
       download_parser.add_argument(
           '''--accelerate-config_file'''    ,   default=_UpperCAmelCase    ,   help='''The accelerate config file to use for the default values in the launching script.'''    ,   )
       download_parser.set_defaults(func=_UpperCAmelCase					)


   def __init__( self    ,   _UpperCAmelCase    ,   *_UpperCAmelCase					):
       A       :       Any							=						accelerate_config_file


   def 					snake_case				( self					):
       A       :       Any							=						'''not installed'''
       if is_safetensors_available():
           import safetensors

           A       :       Dict							=						safetensors.__version__
       elif importlib.util.find_spec('''safetensors'''					) is not None:
           import safetensors

           A       :       int							=						f'''{safetensors.__version__} but is ignored because of PyTorch version too old.'''

       A       :       Tuple							=						'''not installed'''
       A       :       Tuple							=						'''not found'''
       if is_accelerate_available():
           import accelerate
           from accelerate.commands.config import default_config_file, load_config_from_file

           A       :       Any							=						accelerate.__version__
           # Get the default from the config file.
           if self._accelerate_config_file is not None or os.path.isfile(_UpperCAmelCase					):
               A       :       int							=						load_config_from_file(self._accelerate_config_file					).to_dict()

           A       :       Tuple							=						(
               '''\n'''.join([f'''\t- {prop}: {val}''' for prop, val in accelerate_config.items()]					)
               if isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					)
               else f'''\t{accelerate_config}'''
           )

       A       :       str							=						'''not installed'''
       A       :       Optional[Any]							=						'''NA'''
       if is_torch_available():
           import torch

           A       :       Optional[int]							=						torch.__version__
           A       :       List[Any]							=						torch.cuda.is_available()

       A       :       Dict							=						'''not installed'''
       A       :       Any							=						'''NA'''
       if is_tf_available():
           import tensorflow as tf

           A       :       Optional[int]							=						tf.__version__
           try:
               # deprecated in v2.1
               A       :       int							=						tf.test.is_gpu_available()
           except AttributeError:
               # returns list of devices, convert to bool
               A       :       Optional[int]							=						bool(tf.config.list_physical_devices('''GPU'''					)					)

       A       :       str							=						'''not installed'''
       A       :       Dict							=						'''not installed'''
       A       :       int							=						'''not installed'''
       A       :       Tuple							=						'''NA'''
       if is_flax_available():
           import flax
           import jax
           import jaxlib

           A       :       Any							=						flax.__version__
           A       :       Tuple							=						jax.__version__
           A       :       int							=						jaxlib.__version__
           A       :       List[Any]							=						jax.lib.xla_bridge.get_backend().platform

       A       :       Union[str, Any]							=						{
           '''`transformers` version''': version,
           '''Platform''': platform.platform(),
           '''Python version''': platform.python_version(),
           '''Huggingface_hub version''': huggingface_hub.__version__,
           '''Safetensors version''': f'''{safetensors_version}''',
           '''Accelerate version''': f'''{accelerate_version}''',
           '''Accelerate config''': f'''{accelerate_config_str}''',
           '''PyTorch version (GPU?)''': f'''{pt_version} ({pt_cuda_available})''',
           '''Tensorflow version (GPU?)''': f'''{tf_version} ({tf_cuda_available})''',
           '''Flax version (CPU?/GPU?/TPU?)''': f'''{flax_version} ({jax_backend})''',
           '''Jax version''': f'''{jax_version}''',
           '''JaxLib version''': f'''{jaxlib_version}''',
           '''Using GPU in script?''': '''<fill in>''',
           '''Using distributed or parallel set-up in script?''': '''<fill in>''',
       }

       print('''\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n'''					)
       print(self.format_dict(_UpperCAmelCase					)					)

       return info


   @staticmethod
   def 					snake_case				( _UpperCAmelCase					):
       return "\n".join([f'''- {prop}: {val}''' for prop, val in d.items()]					) + "\n"
