


'''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 						__SCREAMING_SNAKE_CASE					:






     @property
     def  a_					(			self		)       ->    List[Any]:
          return self.get_dummy_input()






     @property
     def  a_					(			self		)       ->    Any:
          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  a_					(			self     ,							__UpperCamelCase=True     ,							__UpperCamelCase=False     ,							__UpperCamelCase=False     ,							__UpperCamelCase=False     ,							)       ->    int:
          _a						=		4
          _a						=		32
          _a						=		(32, 32)

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

          if include_temb:
               _a						=		128
               _a						=		randn_tensor((batch_size, temb_channels)     ,							generator=__UpperCamelCase     ,							device=__UpperCamelCase		)

          if include_res_hidden_states_tuple:
               _a						=		torch.manual_seed(1		)
               _a						=		(randn_tensor(__UpperCamelCase     ,							generator=__UpperCamelCase     ,							device=__UpperCamelCase		),)

          if include_encoder_hidden_states:
               _a						=		floats_tensor((batch_size, 32, 32)		).to(__UpperCamelCase		)

          if include_skip_sample:
               _a						=		randn_tensor(((batch_size, 3) + sizes)     ,							generator=__UpperCamelCase     ,							device=__UpperCamelCase		)

          return dummy_input






     def  a_					(			self		)       ->    str:
          _a						=		{
              "in_channels": 32,
              "out_channels": 32,
              "temb_channels": 128,
          }
          if self.block_type == "up":
               _a						=		32

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

          _a						=		self.dummy_input
          return init_dict, inputs_dict






     def  a_					(			self     ,							__UpperCamelCase		)       ->    List[Any]:
          _a			,			_a						=		self.prepare_init_args_and_inputs_for_common()
          _a						=		self.block_class(**__UpperCamelCase		)
          unet_block.to(__UpperCamelCase		)
          unet_block.eval()

          with torch.no_grad():
               _a						=		unet_block(**__UpperCamelCase		)

          if isinstance(__UpperCamelCase     ,							__UpperCamelCase		):
               _a						=		output[0]

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

          _a						=		output[0, -1, -3:, -3:]
          _a						=		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  a_					(			self		)       ->    List[str]:
          _a			,			_a						=		self.prepare_init_args_and_inputs_for_common()
          _a						=		self.block_class(**__UpperCamelCase		)
          model.to(__UpperCamelCase		)
          model.train()
          _a						=		model(**__UpperCamelCase		)

          if isinstance(__UpperCamelCase     ,							__UpperCamelCase		):
               _a						=		output[0]

          _a						=		torch.device(__UpperCamelCase		)
          _a						=		randn_tensor(output.shape     ,							device=__UpperCamelCase		)
          _a						=		torch.nn.functional.mse_loss(__UpperCamelCase     ,							__UpperCamelCase		)
          loss.backward()





'''simple docstring'''

import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowercase__				     =		object()

# For specifying empty leaf dict `{}`
lowercase__				     =		object()






def __UpperCamelCase   (			__lowerCamelCase		: Tuple      ,  __lowerCamelCase		: Tuple				)      ->     Union[str, Any]:







     '''simple docstring'''






     _a						=		tuple((re.compile(x + "$"				) for x in qs)				)
     for i in range(len(__lowerCamelCase				) - len(__lowerCamelCase				) + 1				):
          _a						=		[x.match(__lowerCamelCase				) for x, y in zip(__lowerCamelCase      ,  ks[i:]				)]
          if matches and all(__lowerCamelCase				):
               return True
     return False






def __UpperCamelCase   (			__lowerCamelCase		: int				)      ->     Union[str, Any]:







     '''simple docstring'''






     def replace(__lowerCamelCase		: Tuple      ,  __lowerCamelCase		: Any				):
          for rule, replacement in rules:
               if _match(__lowerCamelCase      ,  __lowerCamelCase				):
                    return replacement
          return val

     return replace






def __UpperCamelCase   (			)      ->     Tuple:







     '''simple docstring'''






     return [
         # embeddings
         (("transformer", "wpe", "embedding"), P("mp"      ,  __lowerCamelCase				)),
         (("transformer", "wte", "embedding"), P("mp"      ,  __lowerCamelCase				)),
         # atention
         (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__lowerCamelCase      ,  "mp"				)),
         (("attention", "out_proj", "kernel"), P("mp"      ,  __lowerCamelCase				)),
         (("attention", "out_proj", "bias"), None),
         # mlp
         (("mlp", "c_fc", "kernel"), P(__lowerCamelCase      ,  "mp"				)),
         (("mlp", "c_fc", "bias"), P("mp"				)),
         (("mlp", "c_proj", "kernel"), P("mp"      ,  __lowerCamelCase				)),
         (("mlp", "c_proj", "bias"), None),
         # layer norms
         ((r"ln_\d+", "bias"), None),
         ((r"\d+", r"ln_\d+", "scale"), None),
         (("ln_f", "bias"), None),
         (("ln_f", "scale"), None),
     ]






def __UpperCamelCase   (			__lowerCamelCase		: Dict				)      ->     Tuple:







     '''simple docstring'''






     _a						=		_get_partition_rules()
     _a						=		_replacement_rules(__lowerCamelCase				)
     _a						=		{k: _unmatched for k in flatten_dict(__lowerCamelCase				)}
     _a						=		{k: replace(__lowerCamelCase      ,  __lowerCamelCase				) for k, v in initd.items()}
     assert _unmatched not in result.values(), "Incomplete partition spec."
     return freeze(unflatten_dict(__lowerCamelCase				)				)

