





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        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->Union[str, Any]:



   '''simple docstring'''



   return self.get_dummy_input()

 @property
 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->Optional[int]:



   '''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 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       int=True	,			UpperCAmelCase_    :       Union[str, Any]=False	,			UpperCAmelCase_    :       List[str]=False	,			UpperCAmelCase_    :       List[str]=False	,			)	->str:



   '''simple docstring'''



   lowerCamelCase__:				str        =4
   lowerCamelCase__:				Dict        =32
   lowerCamelCase__:				int        =(32, 32)

   lowerCamelCase__:				Optional[int]        =torch.manual_seed(0)
   lowerCamelCase__:				Dict        =torch.device(UpperCAmelCase_)
   lowerCamelCase__:				int        =(batch_size, num_channels) + sizes
   lowerCamelCase__:				Union[str, Any]        =randn_tensor(UpperCAmelCase_	,			generator=UpperCAmelCase_	,			device=UpperCAmelCase_)
   lowerCamelCase__:				Optional[Any]        ={"hidden_states": hidden_states}

   if include_temb:
     lowerCamelCase__:				Any        =128
     lowerCamelCase__:				Any        =randn_tensor((batch_size, temb_channels)	,			generator=UpperCAmelCase_	,			device=UpperCAmelCase_)

   if include_res_hidden_states_tuple:
     lowerCamelCase__:				Tuple        =torch.manual_seed(1)
     lowerCamelCase__:				int        =(randn_tensor(UpperCAmelCase_	,			generator=UpperCAmelCase_	,			device=UpperCAmelCase_),)

   if include_encoder_hidden_states:
     lowerCamelCase__:				List[Any]        =floats_tensor((batch_size, 32, 32)).to(UpperCAmelCase_)

   if include_skip_sample:
     lowerCamelCase__:				int        =randn_tensor(((batch_size, 3) + sizes)	,			generator=UpperCAmelCase_	,			device=UpperCAmelCase_)

   return dummy_input

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Any:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        ={
       "in_channels": 32,
       "out_channels": 32,
       "temb_channels": 128,
   }
   if self.block_type == "up":
     lowerCamelCase__:				Any        =32

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

   lowerCamelCase__:				Tuple        =self.dummy_input
   return init_dict, inputs_dict

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict	,			UpperCAmelCase_    :       Optional[int])	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =self.prepare_init_args_and_inputs_for_common()
   lowerCamelCase__:				Optional[int]        =self.block_class(**UpperCAmelCase_)
   unet_block.to(UpperCAmelCase_)
   unet_block.eval()

   with torch.no_grad():
     lowerCamelCase__:				Union[str, Any]        =unet_block(**UpperCAmelCase_)

   if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     lowerCamelCase__:				Tuple        =output[0]

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

   lowerCamelCase__:				Optional[Any]        =output[0, -1, -3:, -3:]
   lowerCamelCase__:				str        =torch.tensor(UpperCAmelCase_).to(UpperCAmelCase_)
   assert torch_all_close(output_slice.flatten()	,			UpperCAmelCase_	,			atol=5E-3)







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



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self.prepare_init_args_and_inputs_for_common()
   lowerCamelCase__:				Optional[int]        =self.block_class(**UpperCAmelCase_)
   model.to(UpperCAmelCase_)
   model.train()
   lowerCamelCase__:				Any        =model(**UpperCAmelCase_)

   if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     lowerCamelCase__:				List[Any]        =output[0]

   lowerCamelCase__:				Dict        =torch.device(UpperCAmelCase_)
   lowerCamelCase__:				Optional[int]        =randn_tensor(output.shape	,			device=UpperCAmelCase_)
   lowerCamelCase__:				Dict        =torch.nn.functional.mse_loss(UpperCAmelCase_	,			UpperCAmelCase_)
   loss.backward()








from math import pow





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a     ,   __a     ,   __a     ,   )					->		tuple[int, int]:







  """simple docstring"""





  if current_sum == needed_sum:
    # If the sum of the powers is equal to needed_sum, then we have a solution.
    solutions_count += 1
    return current_sum, solutions_count

  lowerCamelCase__:				Optional[Any]        =int(pow(__a     ,   __a		)		)
  if current_sum + i_to_n <= needed_sum:
    # If the sum of the powers is less than needed_sum, then continue adding powers.
    current_sum += i_to_n
    lowerCamelCase__  , lowerCamelCase__:				int        =backtrack(
        __a     ,   __a     ,   current_number + 1     ,   __a     ,   __a		)
    current_sum -= i_to_n
  if i_to_n < needed_sum:
    # If the power of i is less than needed_sum, then try with the next power.
    lowerCamelCase__  , lowerCamelCase__:				Dict        =backtrack(
        __a     ,   __a     ,   current_number + 1     ,   __a     ,   __a		)
  return current_sum, solutions_count





def 					lowerCAmelCase_			(	__a     ,   __a		)					->		int:







  """simple docstring"""





  if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
    raise ValueError(
        "Invalid input\n"
        "needed_sum must be between 1 and 1000, power between 2 and 10."		)

  return backtrack(__a     ,   __a     ,   1     ,   0     ,   0		)[1]  # Return the solutions_count


if __name__ == "__main__":
     import doctest

     doctest.testmod()

