


'''simple docstring'''






import collections
import importlib.util
import os
import re
from pathlib import Path


lowercase							='src/transformers'


# Matches is_xxx_available()
lowercase							=re.compile(r'is\_([a-z_]*)_available()')
# Catches a one-line _import_struct = {xxx}
lowercase							=re.compile(r'^_import_structure\s+=\s+\{([^\}]+)\}')
# Catches a line with a key-values pattern: "bla": ["foo", "bar"]
lowercase							=re.compile(r'\s+"\S*":\s+\[([^\]]*)\]')
# Catches a line if not is_foo_available
lowercase							=re.compile(r'^\s*if\s+not\s+is\_[a-z_]*\_available\(\)')
# Catches a line _import_struct["bla"].append("foo")
lowercase							=re.compile(r'^\s*_import_structure\["\S*"\]\.append\("(\S*)"\)')
# Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"]
lowercase							=re.compile(r'^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]')
# Catches a line with an object between quotes and a comma:     "MyModel",
lowercase							=re.compile('^\s+"([^"]+)",')
# Catches a line with objects between brackets only:    ["foo", "bar"],
lowercase							=re.compile('^\s+\[([^\]]+)\]')
# Catches a line with from foo import bar, bla, boo
lowercase							=re.compile(r'\s+from\s+\S*\s+import\s+([^\(\s].*)\n')
# Catches a line with try:
lowercase							=re.compile(r'^\s*try:')
# Catches a line with else:
lowercase							=re.compile(r'^\s*else:')





def lowerCamelCase__      (						__lowerCamelCase							):







   '''simple docstring'''




   if _re_test_backend.search(_A							) is None:
      return None
   _UpperCAmelCase       :       Dict							   =[b[0] for b in _re_backend.findall(_A							)]
   backends.sort()
   return "_and_".join(_A							)





def lowerCamelCase__      (						__lowerCamelCase							):







   '''simple docstring'''




   with open(_A	,   'r'	,   encoding='utf-8'	,   newline='\n'							) as f:
      _UpperCAmelCase       :       Optional[int]							   =f.readlines()

   _UpperCAmelCase       :       Tuple							   =0
   while line_index < len(_A							) and not lines[line_index].startswith('_import_structure = {'							):
      line_index += 1

   # If this is a traditional init, just return.
   if line_index >= len(_A							):
      return None

   # First grab the objects without a specific backend in _import_structure
   _UpperCAmelCase       :       Optional[int]							   =[]
   while not lines[line_index].startswith('if TYPE_CHECKING'							) and find_backend(lines[line_index]							) is None:
      _UpperCAmelCase       :       Dict							   =lines[line_index]
      # If we have everything on a single line, let's deal with it.
      if _re_one_line_import_struct.search(_A							):
         _UpperCAmelCase       :       Any							   =_re_one_line_import_struct.search(_A							).groups()[0]
         _UpperCAmelCase       :       Union[str, Any]							   =re.findall('\[([^\]]+)\]'	,   _A							)
         for imp in imports:
            objects.extend([obj[1:-1] for obj in imp.split(', '							)]							)
         line_index += 1
         continue
      _UpperCAmelCase       :       List[Any]							   =_re_import_struct_key_value.search(_A							)
      if single_line_import_search is not None:
         _UpperCAmelCase       :       Union[str, Any]							   =[obj[1:-1] for obj in single_line_import_search.groups()[0].split(', '							) if len(_A							) > 0]
         objects.extend(_A							)
      elif line.startswith(' ' * 8 + '"'							):
         objects.append(line[9:-3]							)
      line_index += 1

   _UpperCAmelCase       :       List[str]							   ={'none': objects}
   # Let's continue with backend-specific objects in _import_structure
   while not lines[line_index].startswith('if TYPE_CHECKING'							):
      # If the line is an if not is_backend_available, we grab all objects associated.
      _UpperCAmelCase       :       Dict							   =find_backend(lines[line_index]							)
      # Check if the backend declaration is inside a try block:
      if _re_try.search(lines[line_index - 1]							) is None:
         _UpperCAmelCase       :       List[Any]							   =None

      if backend is not None:
         line_index += 1

         # Scroll until we hit the else block of try-except-else
         while _re_else.search(lines[line_index]							) is None:
            line_index += 1

         line_index += 1

         _UpperCAmelCase       :       int							   =[]
         # Until we unindent, add backend objects to the list
         while len(lines[line_index]							) <= 1 or lines[line_index].startswith(' ' * 4							):
            _UpperCAmelCase       :       Union[str, Any]							   =lines[line_index]
            if _re_import_struct_add_one.search(_A							) is not None:
               objects.append(_re_import_struct_add_one.search(_A							).groups()[0]							)
            elif _re_import_struct_add_many.search(_A							) is not None:
               _UpperCAmelCase       :       Tuple							   =_re_import_struct_add_many.search(_A							).groups()[0].split(', '							)
               _UpperCAmelCase       :       Any							   =[obj[1:-1] for obj in imports if len(_A							) > 0]
               objects.extend(_A							)
            elif _re_between_brackets.search(_A							) is not None:
               _UpperCAmelCase       :       Dict							   =_re_between_brackets.search(_A							).groups()[0].split(', '							)
               _UpperCAmelCase       :       List[str]							   =[obj[1:-1] for obj in imports if len(_A							) > 0]
               objects.extend(_A							)
            elif _re_quote_object.search(_A							) is not None:
               objects.append(_re_quote_object.search(_A							).groups()[0]							)
            elif line.startswith(' ' * 8 + '"'							):
               objects.append(line[9:-3]							)
            elif line.startswith(' ' * 1_2 + '"'							):
               objects.append(line[1_3:-3]							)
            line_index += 1

         _UpperCAmelCase       :       Dict							   =objects
      else:
         line_index += 1

      # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend
   _UpperCAmelCase       :       Optional[Any]							   =[]
   while (
       line_index < len(_A							)
       and find_backend(lines[line_index]							) is None
       and not lines[line_index].startswith('else'							)
   ):
      _UpperCAmelCase       :       Any							   =lines[line_index]
      _UpperCAmelCase       :       Dict							   =_re_import.search(_A							)
      if single_line_import_search is not None:
         objects.extend(single_line_import_search.groups()[0].split(', '							)							)
      elif line.startswith(' ' * 8							):
         objects.append(line[8:-2]							)
      line_index += 1

   _UpperCAmelCase       :       List[str]							   ={'none': objects}
   # Let's continue with backend-specific objects
   while line_index < len(_A							):
      # If the line is an if is_backend_available, we grab all objects associated.
      _UpperCAmelCase       :       Tuple							   =find_backend(lines[line_index]							)
      # Check if the backend declaration is inside a try block:
      if _re_try.search(lines[line_index - 1]							) is None:
         _UpperCAmelCase       :       Optional[Any]							   =None

      if backend is not None:
         line_index += 1

         # Scroll until we hit the else block of try-except-else
         while _re_else.search(lines[line_index]							) is None:
            line_index += 1

         line_index += 1

         _UpperCAmelCase       :       Dict							   =[]
         # Until we unindent, add backend objects to the list
         while len(lines[line_index]							) <= 1 or lines[line_index].startswith(' ' * 8							):
            _UpperCAmelCase       :       List[Any]							   =lines[line_index]
            _UpperCAmelCase       :       int							   =_re_import.search(_A							)
            if single_line_import_search is not None:
               objects.extend(single_line_import_search.groups()[0].split(', '							)							)
            elif line.startswith(' ' * 1_2							):
               objects.append(line[1_2:-2]							)
            line_index += 1

         _UpperCAmelCase       :       Tuple							   =objects
      else:
         line_index += 1

   return import_dict_objects, type_hint_objects





def lowerCamelCase__      (						__lowerCamelCase	,   __lowerCamelCase							):







   '''simple docstring'''




   def find_duplicates(__lowerCamelCase							):
      return [k for k, v in collections.Counter(_A							).items() if v > 1]

   if list(import_dict_objects.keys()							) != list(type_hint_objects.keys()							):
      return ["Both sides of the init do not have the same backends!"]

   _UpperCAmelCase       :       Dict							   =[]
   for key in import_dict_objects.keys():
      _UpperCAmelCase       :       Tuple							   =find_duplicates(import_dict_objects[key]							)
      if duplicate_imports:
         errors.append(f"Duplicate _import_structure definitions for: {duplicate_imports}"							)
      _UpperCAmelCase       :       Dict							   =find_duplicates(type_hint_objects[key]							)
      if duplicate_type_hints:
         errors.append(f"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}"							)

      if sorted(set(import_dict_objects[key]							)							) != sorted(set(type_hint_objects[key]							)							):
         _UpperCAmelCase       :       Dict							   ='base imports' if key == 'none' else f"{key} backend"
         errors.append(f"Differences for {name}:"							)
         for a in type_hint_objects[key]:
            if a not in import_dict_objects[key]:
               errors.append(f"  {a} in TYPE_HINT but not in _import_structure."							)
         for a in import_dict_objects[key]:
            if a not in type_hint_objects[key]:
               errors.append(f"  {a} in _import_structure but not in TYPE_HINT."							)
   return errors





def lowerCamelCase__      (						):







   '''simple docstring'''




   _UpperCAmelCase       :       Dict							   =[]
   for root, _, files in os.walk(_A							):
      if "__init__.py" in files:
         _UpperCAmelCase       :       Tuple							   =os.path.join(_A	,   '__init__.py'							)
         _UpperCAmelCase       :       Dict							   =parse_init(_A							)
         if objects is not None:
            _UpperCAmelCase       :       Optional[Any]							   =analyze_results(*_A							)
            if len(_A							) > 0:
               _UpperCAmelCase       :       List[str]							   =f"Problem in {fname}, both halves do not define the same objects.\n{errors[0]}"
               failures.append('\n'.join(_A							)							)
   if len(_A							) > 0:
      raise ValueError('\n\n'.join(_A							)							)





def lowerCamelCase__      (						):







   '''simple docstring'''




   _UpperCAmelCase       :       str							   =[]
   for path, directories, files in os.walk(_A							):
      for folder in directories:
         # Ignore private modules
         if folder.startswith('_'							):
            directories.remove(_A							)
            continue
         # Ignore leftovers from branches (empty folders apart from pycache)
         if len(list((Path(_A							) / folder).glob('*.py'							)							)							) == 0:
            continue
         _UpperCAmelCase       :       Dict							   =str((Path(_A							) / folder).relative_to(_A							)							)
         _UpperCAmelCase       :       Any							   =short_path.replace(os.path.sep	,   '.'							)
         submodules.append(_A							)
      for fname in files:
         if fname == "__init__.py":
            continue
         _UpperCAmelCase       :       int							   =str((Path(_A							) / fname).relative_to(_A							)							)
         _UpperCAmelCase       :       Dict							   =short_path.replace('.py'	,   ''							).replace(os.path.sep	,   '.'							)
         if len(submodule.split('.'							)							) == 1:
            submodules.append(_A							)
   return submodules


lowercase							=[
    'convert_pytorch_checkpoint_to_tf2',
    'modeling_flax_pytorch_utils',
]





def lowerCamelCase__      (						):







   '''simple docstring'''




   _UpperCAmelCase       :       Dict							   =importlib.util.spec_from_file_location(
       'transformers'	,   os.path.join(_A	,   '__init__.py'							)	,   submodule_search_locations=[PATH_TO_TRANSFORMERS]	,   )
   _UpperCAmelCase       :       Union[str, Any]							   =spec.loader.load_module()

   _UpperCAmelCase       :       int							   =[
       module
       for module in get_transformers_submodules()
       if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys()
   ]
   if len(_A							) > 0:
      _UpperCAmelCase       :       Optional[Any]							   ='\n'.join(f"- {module}" for module in module_not_registered							)
      raise ValueError(
          'The following submodules are not properly registered in the main init of Transformers:\n'
          f"{list_of_modules}\n"
          'Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value.'							)


if __name__ == "__main__":
 check_all_inits()
 check_submodules()





'''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  __magic_name__							(		lowerCAmelCase    ):
       UpperCAmelCase										=42
       UpperCAmelCase										=42






class  __magic_name__							(		nn.Module    ):
       UpperCAmelCase										=42
       UpperCAmelCase										=(1_6, 3_2, 9_6, 2_5_6)
       UpperCAmelCase										=jnp.floataa






       def       lowerCAmelCase		(      self)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase       :       str							   =nn.Conv(
              self.block_out_channels[0]  ,							kernel_size=(3, 3)  ,							padding=((1, 1), (1, 1))  ,							dtype=self.dtype  ,							)

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

          _UpperCAmelCase       :       Tuple							   =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  ,							snake_case)						->  List[str]:




          '''simple docstring'''
          _UpperCAmelCase       :       int							   =self.conv_in(snake_case)
          _UpperCAmelCase       :       Any							   =nn.silu(snake_case)

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

          _UpperCAmelCase       :       str							   =self.conv_out(snake_case)

          return embedding






@flax_register_to_config
class  __magic_name__							(		nn.Module ,lowerCAmelCase ,lowerCAmelCase    ):
       UpperCAmelCase										=3_2
       UpperCAmelCase										=4
       UpperCAmelCase										=(
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "CrossAttnDownBlock2D",
           "DownBlock2D",
       )
       UpperCAmelCase										=False
       UpperCAmelCase										=(3_2_0, 6_4_0, 1_2_8_0, 1_2_8_0)
       UpperCAmelCase										=2
       UpperCAmelCase										=8
       UpperCAmelCase										=None
       UpperCAmelCase										=1_2_8_0
       UpperCAmelCase										=0.0
       UpperCAmelCase										=False
       UpperCAmelCase										=jnp.floataa
       UpperCAmelCase										=True
       UpperCAmelCase										=0
       UpperCAmelCase										="rgb"
       UpperCAmelCase										=(1_6, 3_2, 9_6, 2_5_6)






       def       lowerCAmelCase		(      self  ,							snake_case)						->  FrozenDict:




          '''simple docstring'''
          # init input tensors
          _UpperCAmelCase       :       Any							   =(1, self.in_channels, self.sample_size, self.sample_size)
          _UpperCAmelCase       :       Optional[Any]							   =jnp.zeros(snake_case  ,							dtype=jnp.floataa)
          _UpperCAmelCase       :       Optional[int]							   =jnp.ones((1,)  ,							dtype=jnp.intaa)
          _UpperCAmelCase       :       str							   =jnp.zeros((1, 1, self.cross_attention_dim)  ,							dtype=jnp.floataa)
          _UpperCAmelCase       :       Optional[Any]							   =(1, 3, self.sample_size * 8, self.sample_size * 8)
          _UpperCAmelCase       :       int							   =jnp.zeros(snake_case  ,							dtype=jnp.floataa)

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

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






       def       lowerCAmelCase		(      self)						->  Tuple:




          '''simple docstring'''
          _UpperCAmelCase       :       Optional[Any]							   =self.block_out_channels
          _UpperCAmelCase       :       Tuple							   =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.
          _UpperCAmelCase       :       Optional[Any]							   =self.num_attention_heads or self.attention_head_dim

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

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

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

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

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

          # down
          _UpperCAmelCase       :       int							   =[]
          _UpperCAmelCase       :       Optional[int]							   =[]

          _UpperCAmelCase       :       List[str]							   =block_out_channels[0]

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

          for i, down_block_type in enumerate(self.down_block_types):
             _UpperCAmelCase       :       Tuple							   =output_channel
             _UpperCAmelCase       :       Dict							   =block_out_channels[i]
             _UpperCAmelCase       :       str							   =i == len(snake_case) - 1

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

             down_blocks.append(snake_case)

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

             if not is_final_block:
                _UpperCAmelCase       :       List[str]							   =nn.Conv(
                    snake_case  ,							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(snake_case)

          _UpperCAmelCase       :       List[Any]							   =down_blocks
          _UpperCAmelCase       :       Optional[Any]							   =controlnet_down_blocks

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

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






       def __call__(      self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case = 1.0  ,							snake_case = True  ,							snake_case = False  ,							)						->  Union[FlaxControlNetOutput, Tuple]:




          '''simple docstring'''
          _UpperCAmelCase       :       Union[str, Any]							   =self.controlnet_conditioning_channel_order
          if channel_order == "bgr":
             _UpperCAmelCase       :       Optional[int]							   =jnp.flip(snake_case  ,							axis=1)

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

          _UpperCAmelCase       :       int							   =self.time_proj(snake_case)
          _UpperCAmelCase       :       Any							   =self.time_embedding(snake_case)

          # 2. pre-process
          _UpperCAmelCase       :       str							   =jnp.transpose(snake_case  ,							(0, 2, 3, 1))
          _UpperCAmelCase       :       Any							   =self.conv_in(snake_case)

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

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

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

          # 5. contronet blocks
          _UpperCAmelCase       :       Union[str, Any]							   =()
          for down_block_res_sample, controlnet_block in zip(snake_case  ,							self.controlnet_down_blocks):
             _UpperCAmelCase       :       List[str]							   =controlnet_block(snake_case)
             controlnet_down_block_res_samples += (down_block_res_sample,)

          _UpperCAmelCase       :       Optional[int]							   =controlnet_down_block_res_samples

          _UpperCAmelCase       :       List[str]							   =self.controlnet_mid_block(snake_case)

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

