

import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD

class 						__UpperCamelCase    (					nn.Module						):
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     0.0
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     True
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     jnp.floataa




	def       __A					(		self	:						Optional[Any]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			[]
		UpperCAmelCase_       				=			[]

		for i in range(self.num_layers							):
			UpperCAmelCase_       				=			self.in_channels if i == 0 else self.out_channels

			UpperCAmelCase_       				=			FlaxResnetBlockaD(
			    in_channels=lowerCAmelCase		,	out_channels=self.out_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
			resnets.append(lowerCAmelCase							)

			UpperCAmelCase_       				=			FlaxTransformeraDModel(
			    in_channels=self.out_channels		,	n_heads=self.num_attention_heads		,	d_head=self.out_channels // self.num_attention_heads		,	depth=1		,	use_linear_projection=self.use_linear_projection		,	only_cross_attention=self.only_cross_attention		,	use_memory_efficient_attention=self.use_memory_efficient_attention		,	dtype=self.dtype		,	)
			attentions.append(lowerCAmelCase							)

		UpperCAmelCase_       				=			resnets
		UpperCAmelCase_       				=			attentions

		if self.add_downsample:
			UpperCAmelCase_       				=			FlaxDownsampleaD(self.out_channels		,	dtype=self.dtype							)




	def __call__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						str		,	lowerCAmelCase	:						Tuple		,	lowerCAmelCase	:						Union[str, Any]		,	lowerCAmelCase	:						int=True							):


		'''simple docstring'''







		UpperCAmelCase_       				=			()

		for resnet, attn in zip(self.resnets		,	self.attentions							):
			UpperCAmelCase_       				=			resnet(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)
			UpperCAmelCase_       				=			attn(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)
			output_states += (hidden_states,)

		if self.add_downsample:
			UpperCAmelCase_       				=			self.downsamplers_a(lowerCAmelCase							)
			output_states += (hidden_states,)

		return hidden_states, output_states


class 						__UpperCamelCase    (					nn.Module						):
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     0.0
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     True
	SCREAMING_SNAKE_CASE__    				=     jnp.floataa




	def       __A					(		self	:						Optional[int]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			[]

		for i in range(self.num_layers							):
			UpperCAmelCase_       				=			self.in_channels if i == 0 else self.out_channels

			UpperCAmelCase_       				=			FlaxResnetBlockaD(
			    in_channels=lowerCAmelCase		,	out_channels=self.out_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
			resnets.append(lowerCAmelCase							)
		UpperCAmelCase_       				=			resnets

		if self.add_downsample:
			UpperCAmelCase_       				=			FlaxDownsampleaD(self.out_channels		,	dtype=self.dtype							)




	def __call__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						List[str]		,	lowerCAmelCase	:						List[Any]		,	lowerCAmelCase	:						int=True							):


		'''simple docstring'''







		UpperCAmelCase_       				=			()

		for resnet in self.resnets:
			UpperCAmelCase_       				=			resnet(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)
			output_states += (hidden_states,)

		if self.add_downsample:
			UpperCAmelCase_       				=			self.downsamplers_a(lowerCAmelCase							)
			output_states += (hidden_states,)

		return hidden_states, output_states


class 						__UpperCamelCase    (					nn.Module						):
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     0.0
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     True
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     jnp.floataa




	def       __A					(		self	:						Any							):


		'''simple docstring'''







		UpperCAmelCase_       				=			[]
		UpperCAmelCase_       				=			[]

		for i in range(self.num_layers							):
			UpperCAmelCase_       				=			self.in_channels if (i == self.num_layers - 1) else self.out_channels
			UpperCAmelCase_       				=			self.prev_output_channel if i == 0 else self.out_channels

			UpperCAmelCase_       				=			FlaxResnetBlockaD(
			    in_channels=resnet_in_channels + res_skip_channels		,	out_channels=self.out_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
			resnets.append(lowerCAmelCase							)

			UpperCAmelCase_       				=			FlaxTransformeraDModel(
			    in_channels=self.out_channels		,	n_heads=self.num_attention_heads		,	d_head=self.out_channels // self.num_attention_heads		,	depth=1		,	use_linear_projection=self.use_linear_projection		,	only_cross_attention=self.only_cross_attention		,	use_memory_efficient_attention=self.use_memory_efficient_attention		,	dtype=self.dtype		,	)
			attentions.append(lowerCAmelCase							)

		UpperCAmelCase_       				=			resnets
		UpperCAmelCase_       				=			attentions

		if self.add_upsample:
			UpperCAmelCase_       				=			FlaxUpsampleaD(self.out_channels		,	dtype=self.dtype							)




	def __call__(		self	:						List[Any]		,	lowerCAmelCase	:						List[str]		,	lowerCAmelCase	:						Union[str, Any]		,	lowerCAmelCase	:						Optional[Any]		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						Any=True							):


		'''simple docstring'''







		for resnet, attn in zip(self.resnets		,	self.attentions							):
			# pop res hidden states
			UpperCAmelCase_       				=			res_hidden_states_tuple[-1]
			UpperCAmelCase_       				=			res_hidden_states_tuple[:-1]
			UpperCAmelCase_       				=			jnp.concatenate((hidden_states, res_hidden_states)		,	axis=-1							)

			UpperCAmelCase_       				=			resnet(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)
			UpperCAmelCase_       				=			attn(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)

		if self.add_upsample:
			UpperCAmelCase_       				=			self.upsamplers_a(lowerCAmelCase							)

		return hidden_states


class 						__UpperCamelCase    (					nn.Module						):
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     0.0
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     True
	SCREAMING_SNAKE_CASE__    				=     jnp.floataa




	def       __A					(		self	:						Union[str, Any]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			[]

		for i in range(self.num_layers							):
			UpperCAmelCase_       				=			self.in_channels if (i == self.num_layers - 1) else self.out_channels
			UpperCAmelCase_       				=			self.prev_output_channel if i == 0 else self.out_channels

			UpperCAmelCase_       				=			FlaxResnetBlockaD(
			    in_channels=resnet_in_channels + res_skip_channels		,	out_channels=self.out_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
			resnets.append(lowerCAmelCase							)

		UpperCAmelCase_       				=			resnets

		if self.add_upsample:
			UpperCAmelCase_       				=			FlaxUpsampleaD(self.out_channels		,	dtype=self.dtype							)




	def __call__(		self	:						Dict		,	lowerCAmelCase	:						List[Any]		,	lowerCAmelCase	:						Optional[Any]		,	lowerCAmelCase	:						Union[str, Any]		,	lowerCAmelCase	:						Optional[Any]=True							):


		'''simple docstring'''







		for resnet in self.resnets:
			# pop res hidden states
			UpperCAmelCase_       				=			res_hidden_states_tuple[-1]
			UpperCAmelCase_       				=			res_hidden_states_tuple[:-1]
			UpperCAmelCase_       				=			jnp.concatenate((hidden_states, res_hidden_states)		,	axis=-1							)

			UpperCAmelCase_       				=			resnet(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)

		if self.add_upsample:
			UpperCAmelCase_       				=			self.upsamplers_a(lowerCAmelCase							)

		return hidden_states


class 						__UpperCamelCase    (					nn.Module						):
	SCREAMING_SNAKE_CASE__    				=     42
	SCREAMING_SNAKE_CASE__    				=     0.0
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     1
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     False
	SCREAMING_SNAKE_CASE__    				=     jnp.floataa




	def       __A					(		self	:						int							):


		'''simple docstring'''







		UpperCAmelCase_       				=			[
		    FlaxResnetBlockaD(
		        in_channels=self.in_channels		,	out_channels=self.in_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
		]

		UpperCAmelCase_       				=			[]

		for _ in range(self.num_layers							):
			UpperCAmelCase_       				=			FlaxTransformeraDModel(
			    in_channels=self.in_channels		,	n_heads=self.num_attention_heads		,	d_head=self.in_channels // self.num_attention_heads		,	depth=1		,	use_linear_projection=self.use_linear_projection		,	use_memory_efficient_attention=self.use_memory_efficient_attention		,	dtype=self.dtype		,	)
			attentions.append(lowerCAmelCase							)

			UpperCAmelCase_       				=			FlaxResnetBlockaD(
			    in_channels=self.in_channels		,	out_channels=self.in_channels		,	dropout_prob=self.dropout		,	dtype=self.dtype		,	)
			resnets.append(lowerCAmelCase							)

		UpperCAmelCase_       				=			resnets
		UpperCAmelCase_       				=			attentions




	def __call__(		self	:						List[Any]		,	lowerCAmelCase	:						Dict		,	lowerCAmelCase	:						Optional[Any]		,	lowerCAmelCase	:						Tuple		,	lowerCAmelCase	:						Union[str, Any]=True							):


		'''simple docstring'''







		UpperCAmelCase_       				=			self.resnets[0](lowerCAmelCase		,	lowerCAmelCase							)
		for attn, resnet in zip(self.attentions		,	self.resnets[1:]							):
			UpperCAmelCase_       				=			attn(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)
			UpperCAmelCase_       				=			resnet(lowerCAmelCase		,	lowerCAmelCase		,	deterministic=lowerCAmelCase							)

		return hidden_states


from .imports import is_tqdm_available


if is_tqdm_available():
				from tqdm.auto import tqdm as _tqdm

from ..state import PartialState



def       __lowerCAmelCase      (						A = True       ,     *A       ,     **A    ):
	if not is_tqdm_available():
		raise ImportError("Accelerate's `tqdm` module requires `tqdm` to be installed. Please run `pip install tqdm`."    )
	UpperCAmelCase_       				=			False
	if main_process_only:
		UpperCAmelCase_       				=			PartialState().local_process_index == 0
	return _tqdm(*A       ,     **A       ,     disable=A    )