code: def __init__(
      self,
      in_channels=4,
      flip_sin_to_cos=True,
      freq_shift=0,
      down_block_types=(
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "CrossAttnDownBlock2D",
          "DownBlock2D",
      ),
      only_cross_attention=False,
      block_out_channels=(320, 640, 1280, 1280),
      layers_per_block=2,
      downsample_padding=1,
      mid_block_scale_factor=1,
      act_fn="silu",
      norm_num_groups=32,
      norm_eps=1e-5,
      cross_attention_dim=1280,
      transformer_layers_per_block=1,
      attention_head_dim=8,
      use_linear_projection=False,
      upcast_attention=False,
      resnet_time_scale_shift="default",
      conditioning_embedding_out_channels=(16, 32, 96, 256),
      **kwargs,
  ):
      super().__init__()
  
      # Check inputs
      if len(block_out_channels) != len(down_block_types):
          raise ValueError(
              f"Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}."
          )
  
      if not isinstance(only_cross_attention, bool) and len(
          only_cross_attention
      ) != len(down_block_types):
          raise ValueError(
              f"Must provide the same number of `only_cross_attention` as `down_block_types`. `only_cross_attention`: {only_cross_attention}. `down_block_types`: {down_block_types}."
          )
  
      if not isinstance(attention_head_dim, int) and len(
          attention_head_dim
      ) != len(down_block_types):
          raise ValueError(
              f"Must provide the same number of `attention_head_dim` as `down_block_types`. `attention_head_dim`: {attention_head_dim}. `down_block_types`: {down_block_types}."
          )
  
      self._register_load_state_dict_pre_hook(linear_to_conv2d_map)
  
      # input
      conv_in_kernel = 3
      conv_in_padding = (conv_in_kernel - 1) // 2
      self.conv_in = nn.Conv2d(
          in_channels,
          block_out_channels[0],
          kernel_size=conv_in_kernel,
          padding=conv_in_padding,
      )
  
      # time
      time_embed_dim = block_out_channels[0] * 4
  
      self.time_proj = Timesteps(
          block_out_channels[0], flip_sin_to_cos, freq_shift
      )
      timestep_input_dim = block_out_channels[0]
  
      self.time_embedding = TimestepEmbedding(
          timestep_input_dim,
          time_embed_dim,
      )
  
      # control net conditioning embedding
      self.controlnet_cond_embedding = ControlNetConditioningEmbedding(
          conditioning_embedding_channels=block_out_channels[0],
          block_out_channels=conditioning_embedding_out_channels,
      )
  
      self.down_blocks = nn.ModuleList([])
      self.controlnet_down_blocks = nn.ModuleList([])
  
      if isinstance(only_cross_attention, bool):
          only_cross_attention = [only_cross_attention] * len(
              down_block_types
          )
  
      if isinstance(attention_head_dim, int):
          attention_head_dim = (attention_head_dim,) * len(
              down_block_types
          )
  
      if isinstance(transformer_layers_per_block, int):
          transformer_layers_per_block = [
              transformer_layers_per_block
          ] * len(down_block_types)
  
      # down
      output_channel = block_out_channels[0]
  
      controlnet_block = nn.Conv2d(
          output_channel, output_channel, kernel_size=1
      )
      self.controlnet_down_blocks.append(controlnet_block)
  
      for i, down_block_type in enumerate(down_block_types):
          input_channel = output_channel
          output_channel = block_out_channels[i]
          is_final_block = i == len(block_out_channels) - 1
  
          down_block = get_down_block(
              down_block_type,
              transformer_layers_per_block=transformer_layers_per_block[
                  i
              ],
              num_layers=layers_per_block,
              in_channels=input_channel,
              out_channels=output_channel,
              temb_channels=time_embed_dim,
              resnet_eps=norm_eps,
              resnet_act_fn=act_fn,
              cross_attention_dim=cross_attention_dim,
              attn_num_head_channels=attention_head_dim[i],
              downsample_padding=downsample_padding,
              add_downsample=not is_final_block,
          )
          self.down_blocks.append(down_block)
  
          for _ in range(layers_per_block):
              controlnet_block = nn.Conv2d(
                  output_channel, output_channel, kernel_size=1
              )
              self.controlnet_down_blocks.append(controlnet_block)
  
          if not is_final_block:
              controlnet_block = nn.Conv2d(
                  output_channel, output_channel, kernel_size=1
              )
              self.controlnet_down_blocks.append(controlnet_block)
  
      # mid
      mid_block_channel = block_out_channels[-1]
  
      controlnet_block = nn.Conv2d(
          mid_block_channel, mid_block_channel, kernel_size=1
      )
      self.controlnet_mid_block = controlnet_block
  
      self.mid_block = UNetMidBlock2DCrossAttn(
          in_channels=mid_block_channel,
          temb_channels=time_embed_dim,
          resnet_eps=norm_eps,
          resnet_act_fn=act_fn,
          output_scale_factor=mid_block_scale_factor,
          resnet_time_scale_shift=resnet_time_scale_shift,
          cross_attention_dim=cross_attention_dim,
          attn_num_head_channels=attention_head_dim[-1],
          resnet_groups=norm_num_groups,
          use_linear_projection=use_linear_projection,
          upcast_attention=upcast_attention,
      )
  
file_path: ml-stable-diffusion/python_coreml_stable_diffusion/controlnet.py
imports: from diffusers.configuration_utils import ConfigMixin, register_to_config
  from diffusers import ModelMixin
  import torch
  import torch.nn
  import torch.nn.functional
  from .unet import Timesteps, TimestepEmbedding, get_down_block, UNetMidBlock2DCrossAttn, linear_to_conv2d_map
label_desc: Use image node_id 1 to create a new ControlNetModel object from inherited base classes: ModelMixin, ConfigMixin with example: obj = ControlNetModel(in_channels, flip_sin_to_cos, freq_shift, down_block_types, only_cross_attention, block_out_channels, layers_per_block, downsample_padding, mid_block_scale_factor, act_fn, norm_num_groups, norm_eps, cross_attention_dim, transformer_layers_per_block, attention_head_dim, use_linear_projection, upcast_attention, resnet_time_scale_shift, conditioning_embedding_out_channels)