



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 			UpperCAmelCase		(						self  :			Optional[Any])	->				List[str]:




         """simple docstring"""

         return self.get_dummy_input()


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




         """simple docstring"""

         if self.block_type == "down":
               return (4, 32, 16, 16)
         elif self.block_type == "mid":
               return (4, 32, 32, 32)
         elif self.block_type == "up":
               return (4, 32, 64, 64)

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


   def 			UpperCAmelCase		(						self  :			Optional[int]			,				lowerCAmelCase  :			Any=True			,				lowerCAmelCase  :			int=False			,				lowerCAmelCase  :			Optional[int]=False			,				lowerCAmelCase  :			str=False			,				)	->				Any:




         """simple docstring"""

         lowercase__     =  4
         lowercase__     =  32
         lowercase__     =  (32, 32)

         lowercase__     =  torch.manual_seed(0)
         lowercase__     =  torch.device(lowerCAmelCase)
         lowercase__     =  (batch_size, num_channels) + sizes
         lowercase__     =  randn_tensor(lowerCAmelCase			,				generator=lowerCAmelCase			,				device=lowerCAmelCase)
         lowercase__     =  {'hidden_states': hidden_states}

         if include_temb:
               lowercase__     =  1_28
               lowercase__     =  randn_tensor((batch_size, temb_channels)			,				generator=lowerCAmelCase			,				device=lowerCAmelCase)

         if include_res_hidden_states_tuple:
               lowercase__     =  torch.manual_seed(1)
               lowercase__     =  (randn_tensor(lowerCAmelCase			,				generator=lowerCAmelCase			,				device=lowerCAmelCase),)

         if include_encoder_hidden_states:
               lowercase__     =  floats_tensor((batch_size, 32, 32)).to(lowerCAmelCase)

         if include_skip_sample:
               lowercase__     =  randn_tensor(((batch_size, 3) + sizes)			,				generator=lowerCAmelCase			,				device=lowerCAmelCase)

         return dummy_input


   def 			UpperCAmelCase		(						self  :			Union[str, Any])	->				List[Any]:




         """simple docstring"""

         lowercase__     =  {
             'in_channels': 32,
             'out_channels': 32,
             'temb_channels': 1_28,
         }
         if self.block_type == "up":
               lowercase__     =  32

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

         lowercase__     =  self.dummy_input
         return init_dict, inputs_dict


   def 			UpperCAmelCase		(						self  :			Dict			,				lowerCAmelCase  :			Dict)	->				Optional[Any]:




         """simple docstring"""

         lowercase__,	lowercase__     =  self.prepare_init_args_and_inputs_for_common()
         lowercase__     =  self.block_class(**lowerCAmelCase)
         unet_block.to(lowerCAmelCase)
         unet_block.eval()

         with torch.no_grad():
               lowercase__     =  unet_block(**lowerCAmelCase)

         if isinstance(lowerCAmelCase			,				lowerCAmelCase):
               lowercase__     =  output[0]

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

         lowercase__     =  output[0, -1, -3:, -3:]
         lowercase__     =  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 			UpperCAmelCase		(						self  :			Any)	->				Tuple:




         """simple docstring"""

         lowercase__,	lowercase__     =  self.prepare_init_args_and_inputs_for_common()
         lowercase__     =  self.block_class(**lowerCAmelCase)
         model.to(lowerCAmelCase)
         model.train()
         lowercase__     =  model(**lowerCAmelCase)

         if isinstance(lowerCAmelCase			,				lowerCAmelCase):
               lowercase__     =  output[0]

         lowercase__     =  torch.device(lowerCAmelCase)
         lowercase__     =  randn_tensor(output.shape			,				device=lowerCAmelCase)
         lowercase__     =  torch.nn.functional.mse_loss(lowerCAmelCase			,				lowerCAmelCase)
         loss.backward()





def   _lowerCAmelCase   (						A__						,  A__						,  A__	):
      if principal <= 0:
            raise Exception('Principal borrowed must be > 0'	)
      if rate_per_annum < 0:
            raise Exception('Rate of interest must be >= 0'	)
      if years_to_repay <= 0 or not isinstance(A__						,  A__	):
            raise Exception('Years to repay must be an integer > 0'	)

      # Yearly rate is divided by 12 to get monthly rate
      lowercase__     =  rate_per_annum / 12

      # Years to repay is multiplied by 12 to get number of payments as payment is monthly
      lowercase__     =  years_to_repay * 12

      return (
          principal
          * rate_per_month
          * (1 + rate_per_month) ** number_of_payments
          / ((1 + rate_per_month) ** number_of_payments - 1)
      )


if __name__ == "__main__":
       import doctest

       doctest.testmod()
