import json
import os
import re
import unicodedata
from json.encoder import INFINITY
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import regex

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_flax_available, is_tf_available, is_torch_available, logging
from ...utils.generic import _is_jax, _is_numpy


lowerCAmelCase__     						=			logging.get_logger(__name__)

lowerCAmelCase__     						=			{
    'artists_file': 'artists.json',
    'lyrics_file': 'lyrics.json',
    'genres_file': 'genres.json',
}

lowerCAmelCase__     						=			{
    'artists_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/artists.json',
    },
    'genres_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/genres.json',
    },
    'lyrics_file': {
        'jukebox': 'https://huggingface.co/ArthurZ/jukebox/blob/main/lyrics.json',
    },
}

lowerCAmelCase__     						=			{
    'jukebox': 512,
}
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= VOCAB_FILES_NAMES
 UpperCAmelCase_     			= PRETRAINED_VOCAB_FILES_MAP
 UpperCAmelCase_     			= PRETRAINED_LYRIC_TOKENS_SIZES
 UpperCAmelCase_     			= ['input_ids', 'attention_mask']




 def __init__(      self :  Any      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  Dict      ,   lowercase__ :  int      ,   lowercase__ :  Optional[int]=["v3", "v2", "v2"]      ,   lowercase__ :  int=512      ,   lowercase__ :  Optional[int]=5      ,   lowercase__ :  List[Any]="<|endoftext|>"      ,   **lowercase__ :  List[Any]      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = AddedToken(lowercase__      ,   lstrip=lowercase__      ,   rstrip=lowercase__) if isinstance(lowercase__      ,   lowercase__) else unk_token
        super().__init__(
            unk_token=lowercase__      ,   n_genres=lowercase__      ,   version=lowercase__      ,   max_n_lyric_tokens=lowercase__      ,   **lowercase__      ,   )
        lowerCAmelCase__					     = version
        lowerCAmelCase__					     = max_n_lyric_tokens
        lowerCAmelCase__					     = n_genres

        with open(lowercase__      ,   encoding='utf-8') as vocab_handle:
               lowerCAmelCase__					     = json.load(lowercase__)

        with open(lowercase__      ,   encoding='utf-8') as vocab_handle:
               lowerCAmelCase__					     = json.load(lowercase__)

        with open(lowercase__      ,   encoding='utf-8') as vocab_handle:
               lowerCAmelCase__					     = json.load(lowercase__)

        lowerCAmelCase__					     = r'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+'
        # In v2, we had a n_vocab=80 and in v3 we missed + and so n_vocab=79 of characters.
        if len(self.lyrics_encoder) == 79:
               lowerCAmelCase__					     = oov.replace(r'\-\''      ,   r'\-+\'')

        lowerCAmelCase__					     = regex.compile(lowercase__)
        lowerCAmelCase__					     = {v: k for k, v in self.artists_encoder.items()}
        lowerCAmelCase__					     = {v: k for k, v in self.genres_encoder.items()}
        lowerCAmelCase__					     = {v: k for k, v in self.lyrics_encoder.items()}




 @property
 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        return len(self.artists_encoder) + len(self.genres_encoder) + len(self.lyrics_encoder)




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        return dict(self.artists_encoder      ,   self.genres_encoder      ,   self.lyrics_encoder)




 def 		__snake_case						(      self :  str      ,   lowercase__ :  Dict      ,   lowercase__ :  int      ,   lowercase__ :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = [self.artists_encoder.get(lowercase__      ,   0) for artist in list_artists]
        for genres in range(len(lowercase__)):
               lowerCAmelCase__					     = [self.genres_encoder.get(lowercase__      ,   0) for genre in list_genres[genres]]
               lowerCAmelCase__					     = list_genres[genres] + [-1] * (self.n_genres - len(list_genres[genres]))

        lowerCAmelCase__					     = [[self.lyrics_encoder.get(lowercase__      ,   0) for character in list_lyrics[0]], [], []]
        return artists_id, list_genres, lyric_ids




 def 		__snake_case						(      self :  int      ,   lowercase__ :  List[Any]):







        '''simple docstring'''



        return list(lowercase__)




 def 		__snake_case						(      self :  Dict      ,   lowercase__ :  List[Any]      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  Dict      ,   **lowercase__ :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.prepare_for_tokenization(lowercase__      ,   lowercase__      ,   lowercase__)
        lowerCAmelCase__					     = self._tokenize(lowercase__)
        return artist, genre, lyrics




 def 		__snake_case						(      self :  Union[str, Any]      ,   lowercase__ :  str      ,   lowercase__ :  str      ,   lowercase__ :  str      ,   lowercase__ :  bool = False):







        '''simple docstring'''



        for idx in range(len(self.version)):
               if self.version[idx] == "v3":
                      lowerCAmelCase__					     = artists[idx].lower()
                      lowerCAmelCase__					     = [genres[idx].lower()]
               else:
                      lowerCAmelCase__					     = self._normalize(artists[idx]) + '.v2'
                      lowerCAmelCase__					     = [
                          self._normalize(lowercase__) + '.v2' for genre in genres[idx].split('_')
                      ]  # split is for the full dictionary with combined genres

        if self.version[0] == "v2":
               lowerCAmelCase__					     = regex.compile(r'[^A-Za-z0-9.,:;!?\-\'\"()\[\] \t\n]+')
               lowerCAmelCase__					     = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;!?-+\'\"()[] \t\n'
               lowerCAmelCase__					     = {vocab[index]: index + 1 for index in range(len(lowercase__))}
               lowerCAmelCase__					     = 0
               lowerCAmelCase__					     = len(lowercase__) + 1
               lowerCAmelCase__					     = self.vocab
               lowerCAmelCase__					     = {v: k for k, v in self.vocab.items()}
               lowerCAmelCase__					     = ''
        else:
               lowerCAmelCase__					     = regex.compile(r'[^A-Za-z0-9.,:;!?\-+\'\"()\[\] \t\n]+')

        lowerCAmelCase__					     = self._run_strip_accents(lowercase__)
        lowerCAmelCase__					     = lyrics.replace('\\'      ,   '\n')
        lowerCAmelCase__					     = self.out_of_vocab.sub(''      ,   lowercase__), [], []
        return artists, genres, lyrics




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = unicodedata.normalize('NFD'      ,   lowercase__)
        lowerCAmelCase__					     = []
        for char in text:
               lowerCAmelCase__					     = unicodedata.category(lowercase__)
               if cat == "Mn":
                      continue
               output.append(lowercase__)
        return "".join(lowercase__)




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = (
            [chr(lowercase__) for i in range(ord('a')      ,   ord('z') + 1)]
            + [chr(lowercase__) for i in range(ord('A')      ,   ord('Z') + 1)]
            + [chr(lowercase__) for i in range(ord('0')      ,   ord('9') + 1)]
            + ['.']
        )
        lowerCAmelCase__					     = frozenset(lowercase__)
        lowerCAmelCase__					     = re.compile(r'_+')
        lowerCAmelCase__					     = ''.join([c if c in accepted else '_' for c in text.lower()])
        lowerCAmelCase__					     = pattern.sub('_'      ,   lowercase__).strip('_')
        return text




 def 		__snake_case						(      self :  int      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        return " ".join(lowercase__)




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  List[str]      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  bool = False):







        '''simple docstring'''



        if not isinstance(lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = TensorType(lowercase__)

        # Get a function reference for the correct framework
        if tensor_type == TensorType.TENSORFLOW:
               if not is_tf_available():
                      raise ImportError(
                          'Unable to convert output to TensorFlow tensors format, TensorFlow is not installed.')
               import tensorflow as tf

               lowerCAmelCase__					     = tf.constant
               lowerCAmelCase__					     = tf.is_tensor
        elif tensor_type == TensorType.PYTORCH:
               if not is_torch_available():
                      raise ImportError('Unable to convert output to PyTorch tensors format, PyTorch is not installed.')
               import torch

               lowerCAmelCase__					     = torch.tensor
               lowerCAmelCase__					     = torch.is_tensor
        elif tensor_type == TensorType.JAX:
               if not is_flax_available():
                      raise ImportError('Unable to convert output to JAX tensors format, JAX is not installed.')
               import jax.numpy as jnp  # noqa: F811

               lowerCAmelCase__					     = jnp.array
               lowerCAmelCase__					     = _is_jax
        else:
               lowerCAmelCase__					     = np.asarray
               lowerCAmelCase__					     = _is_numpy

        # Do the tensor conversion in batch

        try:
               if prepend_batch_axis:
                      lowerCAmelCase__					     = [inputs]

               if not is_tensor(lowercase__):
                      lowerCAmelCase__					     = as_tensor(lowercase__)
        except:  # noqa E722
               raise ValueError(
                   'Unable to create tensor, you should probably activate truncation and/or padding '
                   'with \'padding=True\' \'truncation=True\' to have batched tensors with the same length.')

        return inputs




 def __call__(      self :  Tuple      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  Any      ,   lowercase__ :  List[Any]=""      ,   lowercase__ :  int="pt"):







        '''simple docstring'''



        lowerCAmelCase__					     = [0, 0, 0]
        lowerCAmelCase__					     = [artist] * len(self.version)
        lowerCAmelCase__					     = [genres] * len(self.version)

        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self.tokenize(lowercase__      ,   lowercase__      ,   lowercase__)
        lowerCAmelCase__     , lowerCAmelCase__     , lowerCAmelCase__					     = self._convert_token_to_id(lowercase__      ,   lowercase__      ,   lowercase__)

        lowerCAmelCase__					     = [-INFINITY] * len(full_tokens[-1])
        lowerCAmelCase__					     = [
            self.convert_to_tensors(
                [input_ids + [artists_id[i]] + genres_ids[i] + full_tokens[i]]      ,   tensor_type=lowercase__)
            for i in range(len(self.version))
        ]
        return BatchEncoding({'input_ids': input_ids, 'attention_masks': attention_masks})




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  str      ,   lowercase__ :  Optional[str] = None):







        '''simple docstring'''



        if not os.path.isdir(lowercase__):
               logger.error(F"""Vocabulary path ({save_directory}) should be a directory""")
               return

        lowerCAmelCase__					     = os.path.join(
            lowercase__      ,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['artists_file'])
        with open(lowercase__      ,   'w'      ,   encoding='utf-8') as f:
               f.write(json.dumps(self.artists_encoder      ,   ensure_ascii=lowercase__))

        lowerCAmelCase__					     = os.path.join(
            lowercase__      ,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['genres_file'])
        with open(lowercase__      ,   'w'      ,   encoding='utf-8') as f:
               f.write(json.dumps(self.genres_encoder      ,   ensure_ascii=lowercase__))

        lowerCAmelCase__					     = os.path.join(
            lowercase__      ,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['lyrics_file'])
        with open(lowercase__      ,   'w'      ,   encoding='utf-8') as f:
               f.write(json.dumps(self.lyrics_encoder      ,   ensure_ascii=lowercase__))

        return (artists_file, genres_file, lyrics_file)




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.artists_decoder.get(lowercase__)
        lowerCAmelCase__					     = [self.genres_decoder.get(lowercase__) for genre in genres_index]
        lowerCAmelCase__					     = [self.lyrics_decoder.get(lowercase__) for character in lyric_index]
        return artist, genres, lyrics

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,
    FlaxCrossAttnUpBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
    FlaxUpBlockaD,
)
@flax.struct.dataclass
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= 42






@flax_register_to_config
class     a_			(						nn.Module	,					SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= 32
 UpperCAmelCase_     			= 4
 UpperCAmelCase_     			= 4
 UpperCAmelCase_     			= (
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "CrossAttnDownBlock2D",
     "DownBlock2D",
 )
 UpperCAmelCase_     			= ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= (320, 640, 1_280, 1_280)
 UpperCAmelCase_     			= 2
 UpperCAmelCase_     			= 8
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= 1_280
 UpperCAmelCase_     			= 0.0
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= jnp.floataa
 UpperCAmelCase_     			= True
 UpperCAmelCase_     			= 0
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  jax.random.KeyArray):







        '''simple docstring'''



        lowerCAmelCase__					     = (1, self.in_channels, self.sample_size, self.sample_size)
        lowerCAmelCase__					     = jnp.zeros(lowercase__      ,   dtype=jnp.floataa)
        lowerCAmelCase__					     = jnp.ones((1,)      ,   dtype=jnp.intaa)
        lowerCAmelCase__					     = jnp.zeros((1, 1, self.cross_attention_dim)      ,   dtype=jnp.floataa)

        lowerCAmelCase__     , lowerCAmelCase__					     = jax.random.split(lowercase__)
        lowerCAmelCase__					     = {'params': params_rng, 'dropout': dropout_rng}

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




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







        '''simple docstring'''



        lowerCAmelCase__					     = self.block_out_channels
        lowerCAmelCase__					     = block_out_channels[0] * 4

        if self.num_attention_heads is not None:
               raise ValueError(
                   'At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19.')

        # 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(lowercase__      ,   dtype=self.dtype)

        lowerCAmelCase__					     = self.only_cross_attention
        if isinstance(lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = (only_cross_attention,) * len(self.down_block_types)

        if isinstance(lowercase__      ,   lowercase__):
               lowerCAmelCase__					     = (num_attention_heads,) * len(self.down_block_types)

        # down
        lowerCAmelCase__					     = []
        lowerCAmelCase__					     = block_out_channels[0]
        for i, down_block_type in enumerate(self.down_block_types):
               lowerCAmelCase__					     = output_channel
               lowerCAmelCase__					     = block_out_channels[i]
               lowerCAmelCase__					     = i == len(lowercase__) - 1

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

               down_blocks.append(lowercase__)
        lowerCAmelCase__					     = down_blocks

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

        # up
        lowerCAmelCase__					     = []
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = list(reversed(lowercase__))
        lowerCAmelCase__					     = reversed_block_out_channels[0]
        for i, up_block_type in enumerate(self.up_block_types):
               lowerCAmelCase__					     = output_channel
               lowerCAmelCase__					     = reversed_block_out_channels[i]
               lowerCAmelCase__					     = reversed_block_out_channels[min(i + 1      ,   len(lowercase__) - 1)]

               lowerCAmelCase__					     = i == len(lowercase__) - 1

               if up_block_type == "CrossAttnUpBlock2D":
                      lowerCAmelCase__					     = FlaxCrossAttnUpBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   prev_output_channel=lowercase__      ,   num_layers=self.layers_per_block + 1      ,   num_attention_heads=reversed_num_attention_heads[i]      ,   add_upsample=not is_final_block      ,   dropout=self.dropout      ,   use_linear_projection=self.use_linear_projection      ,   only_cross_attention=only_cross_attention[i]      ,   use_memory_efficient_attention=self.use_memory_efficient_attention      ,   dtype=self.dtype      ,   )
               else:
                      lowerCAmelCase__					     = FlaxUpBlockaD(
                          in_channels=lowercase__      ,   out_channels=lowercase__      ,   prev_output_channel=lowercase__      ,   num_layers=self.layers_per_block + 1      ,   add_upsample=not is_final_block      ,   dropout=self.dropout      ,   dtype=self.dtype      ,   )

               up_blocks.append(lowercase__)
               lowerCAmelCase__					     = output_channel
        lowerCAmelCase__					     = up_blocks

        # out
        lowerCAmelCase__					     = nn.GroupNorm(num_groups=32      ,   epsilon=1e-5)
        lowerCAmelCase__					     = nn.Conv(
            self.out_channels      ,   kernel_size=(3, 3)      ,   strides=(1, 1)      ,   padding=((1, 1), (1, 1))      ,   dtype=self.dtype      ,   )




 def __call__(      self :  Optional[Any]      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  List[str]      ,   lowercase__ :  int      ,   lowercase__ :  List[Any]=None      ,   lowercase__ :  Union[str, Any]=None      ,   lowercase__ :  bool = True      ,   lowercase__ :  bool = False      ,   ):







        '''simple docstring'''



        if not isinstance(lowercase__      ,   jnp.ndarray):
               lowerCAmelCase__					     = jnp.array([timesteps]      ,   dtype=jnp.intaa)
        elif isinstance(lowercase__      ,   jnp.ndarray) and len(timesteps.shape) == 0:
               lowerCAmelCase__					     = timesteps.astype(dtype=jnp.floataa)
               lowerCAmelCase__					     = jnp.expand_dims(lowercase__      ,   0)

        lowerCAmelCase__					     = self.time_proj(lowercase__)
        lowerCAmelCase__					     = self.time_embedding(lowercase__)

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

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

        if down_block_additional_residuals is not None:
               lowerCAmelCase__					     = ()

               for down_block_res_sample, down_block_additional_residual in zip(
                   lowercase__      ,   lowercase__):
                      down_block_res_sample += down_block_additional_residual
                      new_down_block_res_samples += (down_block_res_sample,)

               lowerCAmelCase__					     = new_down_block_res_samples

        # 4. mid
        lowerCAmelCase__					     = self.mid_block(lowercase__      ,   lowercase__      ,   lowercase__      ,   deterministic=not train)

        if mid_block_additional_residual is not None:
               sample += mid_block_additional_residual

        # 5. up
        for up_block in self.up_blocks:
               lowerCAmelCase__					     = down_block_res_samples[-(self.layers_per_block + 1) :]
               lowerCAmelCase__					     = down_block_res_samples[: -(self.layers_per_block + 1)]
               if isinstance(lowercase__      ,   lowercase__):
                      lowerCAmelCase__					     = up_block(
                          lowercase__      ,   temb=lowercase__      ,   encoder_hidden_states=lowercase__      ,   res_hidden_states_tuple=lowercase__      ,   deterministic=not train      ,   )
               else:
                      lowerCAmelCase__					     = up_block(lowercase__      ,   temb=lowercase__      ,   res_hidden_states_tuple=lowercase__      ,   deterministic=not train)

        # 6. post-process
        lowerCAmelCase__					     = self.conv_norm_out(lowercase__)
        lowerCAmelCase__					     = nn.silu(lowercase__)
        lowerCAmelCase__					     = self.conv_out(lowercase__)
        lowerCAmelCase__					     = jnp.transpose(lowercase__      ,   (0, 3, 1, 2))

        if not return_dict:
               return (sample,)

        return FlaxUNetaDConditionOutput(sample=lowercase__)
