'''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 							a__(   lowerCAmelCase__      ):


			'''simple docstring'''






			UpperCAmelCase_     :	jnp.ndarray
			UpperCAmelCase_     :	jnp.ndarray







class 							a__(   nn.Module      ):


			'''simple docstring'''






			UpperCAmelCase_     :	int
			UpperCAmelCase_     :	Tuple[int]						=			(1_6, 3_2, 9_6, 2_5_6)
			UpperCAmelCase_     :	jnp.dtype						=			jnp.floataa



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    nn.Conv(
					    self.block_out_channels[0]			,       kernel_size=(3, 3)			,       padding=((1, 1), (1, 1))			,       dtype=self.dtype			,       )

					lowerCAmelCase     =    []
					for i in range(len(self.block_out_channels) - 1):
							lowerCAmelCase     =    self.block_out_channels[i]
							lowerCAmelCase     =    self.block_out_channels[i + 1]
							lowerCAmelCase     =    nn.Conv(
							    __lowerCAmelCase			,       kernel_size=(3, 3)			,       padding=((1, 1), (1, 1))			,       dtype=self.dtype			,       )
							blocks.append(__lowerCAmelCase)
							lowerCAmelCase     =    nn.Conv(
							    __lowerCAmelCase			,       kernel_size=(3, 3)			,       strides=(2, 2)			,       padding=((1, 1), (1, 1))			,       dtype=self.dtype			,       )
							blocks.append(__lowerCAmelCase)
					lowerCAmelCase     =    blocks

					lowerCAmelCase     =    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			,       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    self.conv_in(__lowerCAmelCase)
					lowerCAmelCase     =    nn.silu(__lowerCAmelCase)

					for block in self.blocks:
							lowerCAmelCase     =    block(__lowerCAmelCase)
							lowerCAmelCase     =    nn.silu(__lowerCAmelCase)

					lowerCAmelCase     =    self.conv_out(__lowerCAmelCase)

					return embedding







@flax_register_to_config
class 							a__(   nn.Module					,		lowerCAmelCase__					,		lowerCAmelCase__      ):


			'''simple docstring'''






			UpperCAmelCase_     :	int						=			3_2
			UpperCAmelCase_     :	int						=			4
			UpperCAmelCase_     :	Tuple[str]						=			(
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "CrossAttnDownBlock2D",
			    "DownBlock2D",
			)
			UpperCAmelCase_     :	Union[bool, Tuple[bool]]						=			False
			UpperCAmelCase_     :	Tuple[int]						=			(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
			UpperCAmelCase_     :	int						=			2
			UpperCAmelCase_     :	Union[int, Tuple[int]]						=			8
			UpperCAmelCase_     :	Optional[Union[int, Tuple[int]]]						=			None
			UpperCAmelCase_     :	int						=			1_2_8_0
			UpperCAmelCase_     :	float						=			0.0
			UpperCAmelCase_     :	bool						=			False
			UpperCAmelCase_     :	jnp.dtype						=			jnp.floataa
			UpperCAmelCase_     :	bool						=			True
			UpperCAmelCase_     :	int						=			0
			UpperCAmelCase_     :	str						=			"rgb"
			UpperCAmelCase_     :	Tuple[int]						=			(1_6, 3_2, 9_6, 2_5_6)



			def 				a_			(  self			,       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    (1, self.in_channels, self.sample_size, self.sample_size)
					lowerCAmelCase     =    jnp.zeros(__lowerCAmelCase			,       dtype=jnp.floataa)
					lowerCAmelCase     =    jnp.ones((1,)			,       dtype=jnp.intaa)
					lowerCAmelCase     =    jnp.zeros((1, 1, self.cross_attention_dim)			,       dtype=jnp.floataa)
					lowerCAmelCase     =    (1, 3, self.sample_size * 8, self.sample_size * 8)
					lowerCAmelCase     =    jnp.zeros(__lowerCAmelCase			,       dtype=jnp.floataa)

					lowerCAmelCase   ,	lowerCAmelCase     =    jax.random.split(__lowerCAmelCase)
					lowerCAmelCase     =    {"""params""": params_rng, """dropout""": dropout_rng}

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



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.block_out_channels
					lowerCAmelCase     =    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.
					lowerCAmelCase     =    self.num_attention_heads or self.attention_head_dim

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

					# time
					lowerCAmelCase     =    FlaxTimesteps(
					    block_out_channels[0]			,       flip_sin_to_cos=self.flip_sin_to_cos			,       freq_shift=self.config.freq_shift)
					lowerCAmelCase     =    FlaxTimestepEmbedding(__lowerCAmelCase			,       dtype=self.dtype)

					lowerCAmelCase     =    FlaxControlNetConditioningEmbedding(
					    conditioning_embedding_channels=block_out_channels[0]			,       block_out_channels=self.conditioning_embedding_out_channels			,       )

					lowerCAmelCase     =    self.only_cross_attention
					if isinstance(__lowerCAmelCase			,       __lowerCAmelCase):
							lowerCAmelCase     =    (only_cross_attention,) * len(self.down_block_types)

					if isinstance(__lowerCAmelCase			,       __lowerCAmelCase):
							lowerCAmelCase     =    (num_attention_heads,) * len(self.down_block_types)

					# down
					lowerCAmelCase     =    []
					lowerCAmelCase     =    []

					lowerCAmelCase     =    block_out_channels[0]

					lowerCAmelCase     =    nn.Conv(
					    __lowerCAmelCase			,       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(__lowerCAmelCase)

					for i, down_block_type in enumerate(self.down_block_types):
							lowerCAmelCase     =    output_channel
							lowerCAmelCase     =    block_out_channels[i]
							lowerCAmelCase     =    i == len(__lowerCAmelCase) - 1

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

							down_blocks.append(__lowerCAmelCase)

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

							if not is_final_block:
									lowerCAmelCase     =    nn.Conv(
									    __lowerCAmelCase			,       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(__lowerCAmelCase)

					lowerCAmelCase     =    down_blocks
					lowerCAmelCase     =    controlnet_down_blocks

					# mid
					lowerCAmelCase     =    block_out_channels[-1]
					lowerCAmelCase     =    FlaxUNetMidBlockaDCrossAttn(
					    in_channels=__lowerCAmelCase			,       dropout=self.dropout			,       num_attention_heads=num_attention_heads[-1]			,       use_linear_projection=self.use_linear_projection			,       dtype=self.dtype			,       )

					lowerCAmelCase     =    nn.Conv(
					    __lowerCAmelCase			,       kernel_size=(1, 1)			,       padding="""VALID"""			,       kernel_init=nn.initializers.zeros_init()			,       bias_init=nn.initializers.zeros_init()			,       dtype=self.dtype			,       )







			def __call__(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase = 1.0			,       __lowerCAmelCase = True			,       __lowerCAmelCase = False			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    self.controlnet_conditioning_channel_order
					if channel_order == "bgr":
							lowerCAmelCase     =    jnp.flip(__lowerCAmelCase			,       axis=1)

					# 1. time
					if not isinstance(__lowerCAmelCase			,       jnp.ndarray):
							lowerCAmelCase     =    jnp.array([timesteps]			,       dtype=jnp.intaa)
					elif isinstance(__lowerCAmelCase			,       jnp.ndarray) and len(timesteps.shape) == 0:
							lowerCAmelCase     =    timesteps.astype(dtype=jnp.floataa)
							lowerCAmelCase     =    jnp.expand_dims(__lowerCAmelCase			,       0)

					lowerCAmelCase     =    self.time_proj(__lowerCAmelCase)
					lowerCAmelCase     =    self.time_embedding(__lowerCAmelCase)

					# 2. pre-process
					lowerCAmelCase     =    jnp.transpose(__lowerCAmelCase			,       (0, 2, 3, 1))
					lowerCAmelCase     =    self.conv_in(__lowerCAmelCase)

					lowerCAmelCase     =    jnp.transpose(__lowerCAmelCase			,       (0, 2, 3, 1))
					lowerCAmelCase     =    self.controlnet_cond_embedding(__lowerCAmelCase)
					sample += controlnet_cond

					# 3. down
					lowerCAmelCase     =    (sample,)
					for down_block in self.down_blocks:
							if isinstance(__lowerCAmelCase			,       __lowerCAmelCase):
									lowerCAmelCase   ,	lowerCAmelCase     =    down_block(__lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       deterministic=not train)
							else:
									lowerCAmelCase   ,	lowerCAmelCase     =    down_block(__lowerCAmelCase			,       __lowerCAmelCase			,       deterministic=not train)
							down_block_res_samples += res_samples

					# 4. mid
					lowerCAmelCase     =    self.mid_block(__lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       deterministic=not train)

					# 5. contronet blocks
					lowerCAmelCase     =    ()
					for down_block_res_sample, controlnet_block in zip(__lowerCAmelCase			,       self.controlnet_down_blocks):
							lowerCAmelCase     =    controlnet_block(__lowerCAmelCase)
							controlnet_down_block_res_samples += (down_block_res_sample,)

					lowerCAmelCase     =    controlnet_down_block_res_samples

					lowerCAmelCase     =    self.controlnet_mid_block(__lowerCAmelCase)

					# 6. scaling
					lowerCAmelCase     =    [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=__lowerCAmelCase			,       mid_block_res_sample=__lowerCAmelCase)





'''simple docstring'''




# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path

import torch

from ...utils import is_npu_available, is_xpu_available
from .config_args import ClusterConfig, default_json_config_file
from .config_utils import SubcommandHelpFormatter


__lowercase        =    '''Create a default config file for Accelerate with only a few flags set.'''






def 							snake_case__				(     _A:   Dict="no" ,  _A:   str = default_json_config_file ,  _A:   bool = False					)       ->      List[str]:

		'''simple docstring'''


		lowerCAmelCase     =    Path(_A					)
		path.parent.mkdir(parents=_A ,  exist_ok=_A					)
		if path.exists():
				print(
				    f"Configuration already exists at {save_location}, will not override. Run `accelerate config` manually or pass a different `save_location`."					)
				return False
		lowerCAmelCase     =    mixed_precision.lower()
		if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
				raise ValueError(
				    f"`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}"					)
		lowerCAmelCase     =    {
		    """compute_environment""": """LOCAL_MACHINE""",
		    """mixed_precision""": mixed_precision,
		}
		if torch.cuda.is_available():
				lowerCAmelCase     =    torch.cuda.device_count()
				lowerCAmelCase     =    num_gpus
				lowerCAmelCase     =    False
				if num_gpus > 1:
						lowerCAmelCase     =    """MULTI_GPU"""
				else:
						lowerCAmelCase     =    """NO"""
		elif is_xpu_available() and use_xpu:
				lowerCAmelCase     =    torch.xpu.device_count()
				lowerCAmelCase     =    num_xpus
				lowerCAmelCase     =    False
				if num_xpus > 1:
						lowerCAmelCase     =    """MULTI_XPU"""
				else:
						lowerCAmelCase     =    """NO"""
		elif is_npu_available():
				lowerCAmelCase     =    torch.npu.device_count()
				lowerCAmelCase     =    num_npus
				lowerCAmelCase     =    False
				if num_npus > 1:
						lowerCAmelCase     =    """MULTI_NPU"""
				else:
						lowerCAmelCase     =    """NO"""
		else:
				lowerCAmelCase     =    0
				lowerCAmelCase     =    True
				lowerCAmelCase     =    1
				lowerCAmelCase     =    """NO"""
		lowerCAmelCase     =    ClusterConfig(**_A					)
		config.to_json_file(_A					)
		return path






def 							snake_case__				(     _A:   str ,  _A:   Any					)       ->      Optional[Any]:

		'''simple docstring'''


		lowerCAmelCase     =    parser.add_parser("""default""" ,  parents=_A ,  help=_A ,  formatter_class=_A					)
		parser.add_argument(
		    """--config_file""" ,  default=_A ,  help=(
		        """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """
		        """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """
		        """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """
		        """with 'huggingface'."""
		    ) ,  dest="""save_location""" ,  )

		parser.add_argument(
		    """--mixed_precision""" ,  choices=["""no""", """fp16""", """bf16"""] ,  type=_A ,  help="""Whether or not to use mixed precision training. """
		    """Choose between FP16 and BF16 (bfloat16) training. """
		    """BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.""" ,  default="""no""" ,  )
		parser.set_defaults(func=_A					)
		return parser






def 							snake_case__				(     _A:   Optional[int]					)       ->      Optional[int]:

		'''simple docstring'''


		lowerCAmelCase     =    write_basic_config(args.mixed_precision ,  args.save_location					)
		if config_file:
				print(f"accelerate configuration saved at {config_file}"					)




