



'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowercase							       =			logging.get_logger(__name__)

lowercase							       =			{
    '''google/bit-50''': '''https://huggingface.co/google/bit-50/resolve/main/config.json''',
}







class 	UpperCAmelCase	(		__a					,	__a):


 '''simple docstring'''




 __magic_name__					: List[Any]  =      "bit"
 __magic_name__					: List[Any]  =      ["preactivation", "bottleneck"]
 __magic_name__					: Optional[int]  =      ["SAME", "VALID"]




 def __init__(   self							,  lowerCAmelCase_=3							,  lowerCAmelCase_=6_4							,  lowerCAmelCase_=[2_5_6, 5_1_2, 1_0_2_4, 2_0_4_8]							,  lowerCAmelCase_=[3, 4, 6, 3]							,  lowerCAmelCase_="preactivation"							,  lowerCAmelCase_="relu"							,  lowerCAmelCase_=None							,  lowerCAmelCase_=3_2							,  lowerCAmelCase_=0.0							,  lowerCAmelCase_=False							,  lowerCAmelCase_=3_2							,  lowerCAmelCase_=1							,  lowerCAmelCase_=None							,  lowerCAmelCase_=None							,  **lowerCAmelCase_							,  ) ->      List[str]:



      """simple docstring"""







      super().__init__(**lowerCAmelCase_)
      if layer_type not in self.layer_types:
           raise ValueError(f"""layer_type={layer_type} is not one of {','.join(self.layer_types)}""")
      if global_padding is not None:
           if global_padding.upper() in self.supported_padding:
                a_       		=global_padding.upper()
           else:
                raise ValueError(f"""Padding strategy {global_padding} not supported""")
      a_       		=num_channels
      a_       		=embedding_size
      a_       		=hidden_sizes
      a_       		=depths
      a_       		=layer_type
      a_       		=hidden_act
      a_       		=global_padding
      a_       		=num_groups
      a_       		=drop_path_rate
      a_       		=embedding_dynamic_padding
      a_       		=output_stride
      a_       		=width_factor

      a_       		=["stem"] + [f"""stage{idx}""" for idx in range(1							,  len(lowerCAmelCase_) + 1)]
      a_		,						a_       		=get_aligned_output_features_output_indices(
          out_features=lowerCAmelCase_							,  out_indices=lowerCAmelCase_							,  stage_names=self.stage_names)







'''simple docstring'''


import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 	UpperCAmelCase	:


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      return self.get_dummy_input()




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      if self.block_type == "down":
           return (4, 3_2, 1_6, 1_6)
      elif self.block_type == "mid":
           return (4, 3_2, 3_2, 3_2)
      elif self.block_type == "up":
           return (4, 3_2, 6_4, 6_4)

      raise ValueError(f"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'.""")




 def       lowercase_  (   self							,  lowerCAmelCase_=True							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  lowerCAmelCase_=False							,  ) ->      Dict:



      """simple docstring"""







      a_       		=4
      a_       		=3_2
      a_       		=(3_2, 3_2)

      a_       		=torch.manual_seed(0)
      a_       		=torch.device(lowerCAmelCase_)
      a_       		=(batch_size, num_channels) + sizes
      a_       		=randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)
      a_       		={"hidden_states": hidden_states}

      if include_temb:
           a_       		=1_2_8
           a_       		=randn_tensor((batch_size, temb_channels)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      if include_res_hidden_states_tuple:
           a_       		=torch.manual_seed(1)
           a_       		=(randn_tensor(lowerCAmelCase_							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_),)

      if include_encoder_hidden_states:
           a_       		=floats_tensor((batch_size, 3_2, 3_2)).to(lowerCAmelCase_)

      if include_skip_sample:
           a_       		=randn_tensor(((batch_size, 3) + sizes)							,  generator=lowerCAmelCase_							,  device=lowerCAmelCase_)

      return dummy_input




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



      """simple docstring"""







      a_       		={
          "in_channels": 3_2,
          "out_channels": 3_2,
          "temb_channels": 1_2_8,
      }
      if self.block_type == "up":
           a_       		=3_2

      if self.block_type == "mid":
           init_dict.pop("out_channels")

      a_       		=self.dummy_input
      return init_dict, inputs_dict




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      Dict:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      unet_block.to(lowerCAmelCase_)
      unet_block.eval()

      with torch.no_grad():
           a_       		=unet_block(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      self.assertEqual(output.shape							,  self.output_shape)

      a_       		=output[0, -1, -3:, -3:]
      a_       		=torch.tensor(lowerCAmelCase_).to(lowerCAmelCase_)
      assert torch_all_close(output_slice.flatten()							,  lowerCAmelCase_							,  atol=5e-3)




 @unittest.skipIf(torch_device == "mps"							,  "Training is not supported in mps")
 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      a_		,						a_       		=self.prepare_init_args_and_inputs_for_common()
      a_       		=self.block_class(**lowerCAmelCase_)
      model.to(lowerCAmelCase_)
      model.train()
      a_       		=model(**lowerCAmelCase_)

      if isinstance(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=output[0]

      a_       		=torch.device(lowerCAmelCase_)
      a_       		=randn_tensor(output.shape							,  device=lowerCAmelCase_)
      a_       		=torch.nn.functional.mse_loss(lowerCAmelCase_							,  lowerCAmelCase_)
      loss.backward()


